diff options
| author | Richard M. Stallman | 1993-01-06 04:35:15 +0000 |
|---|---|---|
| committer | Richard M. Stallman | 1993-01-06 04:35:15 +0000 |
| commit | 652304c9d3b94a3969434145eeaa3844bb99afe2 (patch) | |
| tree | 8329d87c036d2aa7fe89d171a369866bf2a1ec18 | |
| parent | c2fabcbf95e924e4cb947cf89d3581b3f0c9ab20 (diff) | |
| download | emacs-652304c9d3b94a3969434145eeaa3844bb99afe2.tar.gz emacs-652304c9d3b94a3969434145eeaa3844bb99afe2.zip | |
entered into RCS
| -rw-r--r-- | lisp/hippie-exp.el | 674 |
1 files changed, 674 insertions, 0 deletions
diff --git a/lisp/hippie-exp.el b/lisp/hippie-exp.el new file mode 100644 index 00000000000..1d2f647ecf2 --- /dev/null +++ b/lisp/hippie-exp.el | |||
| @@ -0,0 +1,674 @@ | |||
| 1 | ;; Expand a word trying various ways to find its expansion. | ||
| 2 | ;; Copyright (C) 1992 Free Software Foundation, Inc. | ||
| 3 | ;; | ||
| 4 | ;; This file is part of GNU Emacs. | ||
| 5 | |||
| 6 | ;; GNU Emacs is free software; you can redistribute it and/or modify | ||
| 7 | ;; it under the terms of the GNU General Public License as published by | ||
| 8 | ;; the Free Software Foundation; either version 2, or (at your option) | ||
| 9 | ;; any later version. | ||
| 10 | |||
| 11 | ;; GNU Emacs is distributed in the hope that it will be useful, | ||
| 12 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 13 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 14 | ;; GNU General Public License for more details. | ||
| 15 | |||
| 16 | ;; You should have received a copy of the GNU General Public License | ||
| 17 | ;; along with GNU Emacs; see the file COPYING. If not, write to | ||
| 18 | ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | ||
| 19 | ;; | ||
| 20 | ;; Author: Anders Holst (aho@sans.kth.se) | ||
| 21 | ;; | ||
| 22 | ;; Last change: 4 January 1993 | ||
| 23 | |||
| 24 | ;; | ||
| 25 | ;; DESCRIPTION | ||
| 26 | ;; | ||
| 27 | ;; `hippie-expand' is a single function for a lot of different kinds | ||
| 28 | ;; of completions and expansions. Called repeatedly it tries all | ||
| 29 | ;; possible completions in succession. | ||
| 30 | ;; Which kinds of completions to try, and in which order, is | ||
| 31 | ;; determined by the contents of `hippie-expand-try-functions-list'. | ||
| 32 | ;; Much customization of `hippie-expand' can be made by changing the | ||
| 33 | ;; order of, removing, or inserting new functions in this list. | ||
| 34 | ;; Given a positive numeric argument, `hippie-expand' jumps directly | ||
| 35 | ;; ARG functions forward in this list. Given some other argument | ||
| 36 | ;; (a negative argument or just Ctrl-U) it undoes the tried | ||
| 37 | ;; completion. | ||
| 38 | ;; If the variable `hippie-expand-verbose' is non-nil, `hippie-expand' | ||
| 39 | ;; outputs in a message which try-function in the list that is used | ||
| 40 | ;; currently (ie. was used currently and will be tried first the next | ||
| 41 | ;; time). | ||
| 42 | ;; See also the macro `make-hippie-expand-function' below. | ||
| 43 | ;; | ||
| 44 | ;; A short description of the current try-functions in this file: | ||
| 45 | ;; `try-complete-file-name' : very convenient to have in any buffer, | ||
| 46 | ;; and not just in the minibuffer or (some) shell-mode. It goes | ||
| 47 | ;; through all possible completions instead of just completing as | ||
| 48 | ;; much as is unique. | ||
| 49 | ;; `try-complete-file-name-partially' : To insert in the list just | ||
| 50 | ;; before `try-complete-file-name' for those who want first to get | ||
| 51 | ;; a file name completed only as many characters as is unique. | ||
| 52 | ;; (NOTE: Not by default in `hippie-expand-try-functions-list'.) | ||
| 53 | ;; `try-expand-all-abbrevs' : can be removed if you don't use abbrevs. | ||
| 54 | ;; Otherwise it looks through all abbrev-tables, starting with | ||
| 55 | ;; the local followed by the global. | ||
| 56 | ;; `try-expand-line' : Searches the buffer for an entire line that | ||
| 57 | ;; begins exactly as the current line. Convenient sometimes, for | ||
| 58 | ;; example as a substitute for (or complement to) the history | ||
| 59 | ;; list in shell-like buffers. Remove it if you find it confusing. | ||
| 60 | ;; `try-expand-line-all-buffers' : Like `try-expand-line' but searches | ||
| 61 | ;; in all buffers (except the current). (This may be a little | ||
| 62 | ;; slow, don't use it unless you are really fond of `hippie-expand'. | ||
| 63 | ;; NOTE: Not by default in hippie-expand-try-functions-list.) | ||
| 64 | ;; `try-expand-dabbrev' : works exactly as dabbrev-expand (but of | ||
| 65 | ;; course in a way compatible with the other try-functions). | ||
| 66 | ;; `try-expand-dabbrev-all-buffers' : perhaps the most useful of them, | ||
| 67 | ;; like `dabbrev-expand' but searches all Emacs buffers (except the | ||
| 68 | ;; current) for matching words. (No, I don't find this one | ||
| 69 | ;; particularly slow.) | ||
| 70 | ;; `try-complete-lisp-symbol' : like `lisp-complete-symbol', but goes | ||
| 71 | ;; through all possibilities instead of completing what is unique. | ||
| 72 | ;; Might be tedious (usually a lot of possible completions) and | ||
| 73 | ;; since its function is much like `lisp-complete-symbol', which | ||
| 74 | ;; already has a key of its own, you might want to remove this. | ||
| 75 | ;; `try-complete-lisp-symbol-partially' : To insert in the list just | ||
| 76 | ;; before `try-complete-lisp-symbol' for those who first want to get | ||
| 77 | ;; completion of what is unique in the name. (NOTE: Not by | ||
| 78 | ;; default in hippie-expand-try-functions-list.) | ||
| 79 | ;; | ||
| 80 | ;; To write new try-functions, consider the following: | ||
| 81 | ;; Each try-function takes one argument OLD which is nil the first | ||
| 82 | ;; time the function is called and true in succeeding calls for the | ||
| 83 | ;; same string to complete. The first time the function has to | ||
| 84 | ;; extract the string before point to complete, and substitute the | ||
| 85 | ;; first completion alternative for it. On following calls it has to | ||
| 86 | ;; substitute the next possible completion for the last tried string. | ||
| 87 | ;; The try-function is to return t as long as it finds new | ||
| 88 | ;; possible completions. When there are no more alternatives it has | ||
| 89 | ;; to restore the text before point to its original contents, and | ||
| 90 | ;; return nil (don't beep or message or anything). | ||
| 91 | ;; The try-function can (should) use the following functions: | ||
| 92 | ;; `he-init-string' : Initializes the text to substitute to the | ||
| 93 | ;; contents of the region BEGIN to END. Also sets the variable | ||
| 94 | ;; `he-search-string' to the text to expand. | ||
| 95 | ;; `he-substitute-string' : substitutes STR into the region | ||
| 96 | ;; initialized with `he-init-string'. (An optional second argument | ||
| 97 | ;; TRANS-CASE non-nil, means transfer of case from the abbreviation | ||
| 98 | ;; to the expansion is ok if that is enabled in the buffer.) | ||
| 99 | ;; `he-reset-string' : Resets the initialized region to its original | ||
| 100 | ;; contents. | ||
| 101 | ;; There is also a variable: `he-tried-table' which is meant to contain | ||
| 102 | ;; all tried expansions so far. The try-function can check this | ||
| 103 | ;; variable to see whether an expansion has already been tried | ||
| 104 | ;; (hint: `he-string-member'), and add its own tried expansions to it. | ||
| 105 | ;; | ||
| 106 | ;; | ||
| 107 | ;; KNOWN BUGS | ||
| 108 | ;; | ||
| 109 | ;; It may happen that some completion suggestion occurs twice, in | ||
| 110 | ;; spite of the use of `he-tried-table' to prevent that. This is | ||
| 111 | ;; because different try-functions may try to complete different | ||
| 112 | ;; lengths of text, and thus put different amounts of the | ||
| 113 | ;; text in `he-try-table'. Anyway this seems to occur seldom enough not | ||
| 114 | ;; to be too disturbing. Also it should NOT bee possible for the | ||
| 115 | ;; opposite situation to occur, that `hippie-expand' misses some | ||
| 116 | ;; suggestion because it thinks it has already tried it. | ||
| 117 | ;; | ||
| 118 | ;; | ||
| 119 | ;; ACKNOWLEDGEMENT | ||
| 120 | ;; | ||
| 121 | ;; I want to thank Mikael Djurfeldt in discussions with whom the idea | ||
| 122 | ;; of this function took form. | ||
| 123 | ;; I am also grateful to all those who have given me suggestions on | ||
| 124 | ;; how to improve it. | ||
| 125 | ;; | ||
| 126 | |||
| 127 | |||
| 128 | (defvar he-num -1) | ||
| 129 | |||
| 130 | (defvar he-string-beg ()) | ||
| 131 | |||
| 132 | (defvar he-string-end ()) | ||
| 133 | |||
| 134 | (defvar he-search-string ()) | ||
| 135 | |||
| 136 | (defvar he-expand-list ()) | ||
| 137 | |||
| 138 | (defvar he-tried-table ()) | ||
| 139 | |||
| 140 | (defvar he-search-loc ()) | ||
| 141 | |||
| 142 | (defvar he-search-bw ()) | ||
| 143 | |||
| 144 | (defvar he-search-bufs ()) | ||
| 145 | |||
| 146 | (defvar hippie-expand-try-functions-list '(try-complete-file-name | ||
| 147 | try-expand-all-abbrevs | ||
| 148 | try-expand-line | ||
| 149 | try-expand-dabbrev | ||
| 150 | try-expand-dabbrev-all-buffers | ||
| 151 | try-complete-lisp-symbol) | ||
| 152 | "The list of expansion functions tried in order by `hippie-expand'. | ||
| 153 | To change the behavior of `hippie-expand', remove, change the order of, | ||
| 154 | or insert functions in this list.") | ||
| 155 | |||
| 156 | (defvar hippie-expand-verbose t | ||
| 157 | "*Non-nil makes `hippie-expand' output which function it is trying.") | ||
| 158 | |||
| 159 | (defun hippie-expand (arg) | ||
| 160 | "Try to expand text before point, using multiple methods. | ||
| 161 | The expansion functions in `hippie-expand-try-functions-list' are | ||
| 162 | tried in order, until a possible expansion is found. Repeated | ||
| 163 | application of `hippie-expand' inserts successively possible | ||
| 164 | expansions. | ||
| 165 | With a positive numeric argument, jumps directly to the ARG next | ||
| 166 | function in this list. With a negative argument or just \\[universal-argument], | ||
| 167 | undoes the expansion." | ||
| 168 | (interactive "P") | ||
| 169 | (if (or (not arg) | ||
| 170 | (and (integerp arg) (> arg 0))) | ||
| 171 | (let ((first (or (= he-num -1) | ||
| 172 | (not (equal this-command last-command))))) | ||
| 173 | (if first | ||
| 174 | (progn | ||
| 175 | (setq he-num -1) | ||
| 176 | (setq he-tried-table nil))) | ||
| 177 | (if arg | ||
| 178 | (if (not first) (he-reset-string)) | ||
| 179 | (setq arg 0)) | ||
| 180 | (let ((i (max (+ he-num arg) 0))) | ||
| 181 | (while (not (or (>= i (length hippie-expand-try-functions-list)) | ||
| 182 | (apply (nth i hippie-expand-try-functions-list) | ||
| 183 | (list (= he-num i))))) | ||
| 184 | (setq i (1+ i))) | ||
| 185 | (setq he-num i)) | ||
| 186 | (if (>= he-num (length hippie-expand-try-functions-list)) | ||
| 187 | (progn | ||
| 188 | (setq he-num -1) | ||
| 189 | (if first | ||
| 190 | (message "No expansion found") | ||
| 191 | (message "No further expansions found")) | ||
| 192 | (ding)) | ||
| 193 | (if hippie-expand-verbose | ||
| 194 | (message (concat "Using " | ||
| 195 | (prin1-to-string (nth he-num | ||
| 196 | hippie-expand-try-functions-list))))))) | ||
| 197 | (if (>= he-num 0) | ||
| 198 | (progn | ||
| 199 | (setq he-num -1) | ||
| 200 | (he-reset-string) | ||
| 201 | (if hippie-expand-verbose | ||
| 202 | (message "Undoing expansions")))))) | ||
| 203 | |||
| 204 | ;; Initializes the region to expand (to between BEG and END). | ||
| 205 | (defun he-init-string (beg end) | ||
| 206 | (setq he-string-beg beg) | ||
| 207 | (setq he-string-end end) | ||
| 208 | (setq he-search-string (buffer-substring beg end))) | ||
| 209 | |||
| 210 | ;; Resets the expanded region to its original contents. | ||
| 211 | (defun he-reset-string () | ||
| 212 | (delete-region he-string-beg he-string-end) | ||
| 213 | (insert he-search-string) | ||
| 214 | (setq he-string-end (point))) | ||
| 215 | |||
| 216 | ;; Substitutes an expansion STR into the correct region (the region | ||
| 217 | ;; initialized with `he-init-string'). | ||
| 218 | ;; An optional argument TRANS-CASE means that it is ok to transfer case | ||
| 219 | ;; from the abbreviation to the expansion if that is possible, and is | ||
| 220 | ;; enabled in the buffer. | ||
| 221 | (defun he-substitute-string (str &optional trans-case) | ||
| 222 | (let ((trans-case (and trans-case | ||
| 223 | case-replace | ||
| 224 | case-fold-search | ||
| 225 | (he-transfer-case-ok str he-search-string)))) | ||
| 226 | (he-reset-string) | ||
| 227 | (goto-char he-string-beg) | ||
| 228 | (search-forward he-search-string) | ||
| 229 | (replace-match (if trans-case (downcase str) str) | ||
| 230 | (not trans-case) | ||
| 231 | 'literal) | ||
| 232 | (setq he-string-end (point)))) | ||
| 233 | |||
| 234 | (defun he-ordinary-case-p (str) | ||
| 235 | (or (string= str (downcase str)) | ||
| 236 | (string= str (upcase str)) | ||
| 237 | (string= str (capitalize str)))) | ||
| 238 | |||
| 239 | (defun he-transfer-case-ok (to-str from-str) | ||
| 240 | (and (not (string= from-str (substring to-str 0 (length from-str)))) | ||
| 241 | ;; otherwise transfer is not needed (and this also solves | ||
| 242 | ;; some obscure situations) | ||
| 243 | (he-ordinary-case-p to-str) | ||
| 244 | ;; otherwise case may be significant | ||
| 245 | (he-ordinary-case-p from-str) | ||
| 246 | ;; otherwise replace-match wont know what to do | ||
| 247 | )) | ||
| 248 | |||
| 249 | ;; Check if STR is a member of LST. | ||
| 250 | ;; Ignore case if `case-replace' and `case-fold-search' are both t. | ||
| 251 | (defun he-string-member (str lst) | ||
| 252 | (while (and lst | ||
| 253 | (not | ||
| 254 | (if (and case-fold-search case-replace) | ||
| 255 | (string= (downcase (car lst)) (downcase str)) | ||
| 256 | (string= (car lst) str)))) | ||
| 257 | (setq lst (cdr lst))) | ||
| 258 | lst) | ||
| 259 | |||
| 260 | ;; For the real hippie-expand enthusiast: A macro that makes it | ||
| 261 | ;; possible to use many functions like hippie-expand, but with | ||
| 262 | ;; different try-functions-lists. | ||
| 263 | ;; Usage is for example: | ||
| 264 | ;; (fset 'my-complete-file (make-hippie-expand-function | ||
| 265 | ;; '(try-complete-file-name-partially | ||
| 266 | ;; try-complete-file-name))) | ||
| 267 | ;; (fset 'my-complete-line (make-hippie-expand-function | ||
| 268 | ;; '(try-expand-line | ||
| 269 | ;; try-expand-line-all-buffers))) | ||
| 270 | ;; | ||
| 271 | (defmacro make-hippie-expand-function (try-list &optional verbose) | ||
| 272 | "Construct a function similar to `hippie-expand'. | ||
| 273 | Make it use the expansion functions in TRY-LIST. An optional second | ||
| 274 | argument VERBOSE non-nil makes the function verbose." | ||
| 275 | (` '(lambda (arg) | ||
| 276 | (, (concat | ||
| 277 | "Try to expand text before point, using the following functions: \n" | ||
| 278 | (mapconcat 'prin1-to-string (eval try-list) ", "))) | ||
| 279 | (interactive "P") | ||
| 280 | (let ((hippie-expand-try-functions-list (, try-list)) | ||
| 281 | (hippie-expand-verbose (, verbose))) | ||
| 282 | (hippie-expand arg))))) | ||
| 283 | |||
| 284 | |||
| 285 | ;;; Here follows the try-functions and their requisites: | ||
| 286 | |||
| 287 | (defun try-complete-file-name (old) | ||
| 288 | "Try to complete text as a file name. | ||
| 289 | The argument OLD has to be nil the first call of this function, and t | ||
| 290 | for subsequent calls (for further possible completions of the same | ||
| 291 | string). It returns t if a new completion is found, nil otherwise." | ||
| 292 | (if (not old) | ||
| 293 | (progn | ||
| 294 | (he-init-string (he-file-name-beg) (point)) | ||
| 295 | (let ((name-part (file-name-nondirectory he-search-string)) | ||
| 296 | (dir-part (expand-file-name (or (file-name-directory | ||
| 297 | he-search-string) "")))) | ||
| 298 | (if (not (he-string-member name-part he-tried-table)) | ||
| 299 | (setq he-tried-table (cons name-part he-tried-table))) | ||
| 300 | (if (and (not (equal he-search-string "")) | ||
| 301 | (file-directory-p dir-part)) | ||
| 302 | (setq he-expand-list (sort (file-name-all-completions | ||
| 303 | name-part | ||
| 304 | dir-part) | ||
| 305 | 'string-lessp)) | ||
| 306 | (setq he-expand-list ()))))) | ||
| 307 | |||
| 308 | (while (and he-expand-list | ||
| 309 | (he-string-member (car he-expand-list) he-tried-table)) | ||
| 310 | (setq he-expand-list (cdr he-expand-list))) | ||
| 311 | (if (null he-expand-list) | ||
| 312 | (progn | ||
| 313 | (he-reset-string) | ||
| 314 | ()) | ||
| 315 | (let ((filename (concat (file-name-directory he-search-string) | ||
| 316 | (car he-expand-list)))) | ||
| 317 | (he-substitute-string filename) | ||
| 318 | (setq he-tried-table (cons (car he-expand-list) he-tried-table)) | ||
| 319 | (setq he-expand-list (cdr he-expand-list)) | ||
| 320 | t))) | ||
| 321 | |||
| 322 | (defun try-complete-file-name-partially (old) | ||
| 323 | "Try to complete text as a file name, as many characters as unique. | ||
| 324 | The argument OLD has to be nil the first call of this function. It | ||
| 325 | returns t if a unique, possibly partial, completion is found, nil | ||
| 326 | otherwise." | ||
| 327 | (let ((expansion ())) | ||
| 328 | (if (not old) | ||
| 329 | (progn | ||
| 330 | (he-init-string (he-file-name-beg) (point)) | ||
| 331 | (let ((name-part (file-name-nondirectory he-search-string)) | ||
| 332 | (dir-part (expand-file-name (or (file-name-directory | ||
| 333 | he-search-string) "")))) | ||
| 334 | (if (and (not (equal he-search-string "")) | ||
| 335 | (file-directory-p dir-part)) | ||
| 336 | (setq expansion (file-name-completion name-part | ||
| 337 | dir-part))) | ||
| 338 | (if (or (eq expansion t) | ||
| 339 | (string= expansion name-part)) | ||
| 340 | (setq expansion ()))))) | ||
| 341 | |||
| 342 | (if (not expansion) | ||
| 343 | (progn | ||
| 344 | (he-reset-string) | ||
| 345 | ()) | ||
| 346 | (let ((filename (concat (file-name-directory he-search-string) | ||
| 347 | expansion))) | ||
| 348 | (he-substitute-string filename) | ||
| 349 | (setq he-tried-table (cons expansion he-tried-table)) | ||
| 350 | t)))) | ||
| 351 | |||
| 352 | (defun he-file-name-beg () | ||
| 353 | (let ((skips "-a-zA-Z0-9_./~^#$")) | ||
| 354 | (save-excursion | ||
| 355 | (skip-chars-backward skips) | ||
| 356 | (point)))) | ||
| 357 | |||
| 358 | (defun try-complete-lisp-symbol (old) | ||
| 359 | "Try to complete word as an Emacs Lisp symbol. | ||
| 360 | The argument OLD has to be nil the first call of this function, and t | ||
| 361 | for subsequent calls (for further possible completions of the same | ||
| 362 | string). It returns t if a new completion is found, nil otherwise." | ||
| 363 | (if (not old) | ||
| 364 | (progn | ||
| 365 | (he-init-string (he-lisp-symbol-beg) (point)) | ||
| 366 | (if (not (he-string-member he-search-string he-tried-table)) | ||
| 367 | (setq he-tried-table (cons he-search-string he-tried-table))) | ||
| 368 | (setq he-expand-list | ||
| 369 | (and (not (equal he-search-string "")) | ||
| 370 | (sort (all-completions he-search-string obarray | ||
| 371 | (function (lambda (sym) | ||
| 372 | (or (boundp sym) | ||
| 373 | (fboundp sym) | ||
| 374 | (symbol-plist sym))))) | ||
| 375 | 'string-lessp))))) | ||
| 376 | (while (and he-expand-list | ||
| 377 | (he-string-member (car he-expand-list) he-tried-table)) | ||
| 378 | (setq he-expand-list (cdr he-expand-list))) | ||
| 379 | (if (null he-expand-list) | ||
| 380 | (progn | ||
| 381 | (he-reset-string) | ||
| 382 | ()) | ||
| 383 | (progn | ||
| 384 | (he-substitute-string (car he-expand-list)) | ||
| 385 | (setq he-tried-table (cons (car he-expand-list) he-tried-table)) | ||
| 386 | (setq he-expand-list (cdr he-expand-list)) | ||
| 387 | t))) | ||
| 388 | |||
| 389 | (defun try-complete-lisp-symbol-partially (old) | ||
| 390 | "Try to complete as an Emacs Lisp symbol, as many characters as unique. | ||
| 391 | The argument OLD has to be nil the first call of this function. It | ||
| 392 | returns t if a unique, possibly partial, completion is found, nil | ||
| 393 | otherwise." | ||
| 394 | (let ((expansion ())) | ||
| 395 | (if (not old) | ||
| 396 | (progn | ||
| 397 | (he-init-string (he-lisp-symbol-beg) (point)) | ||
| 398 | (if (not (string= he-search-string "")) | ||
| 399 | (setq expansion | ||
| 400 | (try-completion he-search-string obarray | ||
| 401 | (function (lambda (sym) | ||
| 402 | (or (boundp sym) | ||
| 403 | (fboundp sym) | ||
| 404 | (symbol-plist sym))))))) | ||
| 405 | (if (or (eq expansion t) | ||
| 406 | (string= expansion he-search-string)) | ||
| 407 | (setq expansion ())))) | ||
| 408 | |||
| 409 | (if (not expansion) | ||
| 410 | (progn | ||
| 411 | (he-reset-string) | ||
| 412 | ()) | ||
| 413 | (progn | ||
| 414 | (he-substitute-string expansion) | ||
| 415 | (setq he-tried-table (cons expansion he-tried-table)) | ||
| 416 | t)))) | ||
| 417 | |||
| 418 | (defun he-lisp-symbol-beg () | ||
| 419 | (let ((skips "-a-zA-Z0-9_.")) | ||
| 420 | (save-excursion | ||
| 421 | (skip-chars-backward skips) | ||
| 422 | (point)))) | ||
| 423 | |||
| 424 | (defun try-expand-line (old) | ||
| 425 | "Try to complete the current line to an entire line in the buffer. | ||
| 426 | The argument OLD has to be nil the first call of this function, and t | ||
| 427 | for subsequent calls (for further possible completions of the same | ||
| 428 | string). It returns t if a new completion is found, nil otherwise." | ||
| 429 | (let ((expansion ()) | ||
| 430 | (strip-prompt (and (get-buffer-process (current-buffer)) | ||
| 431 | shell-prompt-pattern))) | ||
| 432 | (if (not old) | ||
| 433 | (progn | ||
| 434 | (he-init-string (he-line-beg strip-prompt) (point)) | ||
| 435 | (setq he-search-loc he-string-beg) | ||
| 436 | (setq he-search-bw t))) | ||
| 437 | |||
| 438 | (if (not (equal he-search-string "")) | ||
| 439 | (save-excursion | ||
| 440 | ;; Try looking backward unless inhibited. | ||
| 441 | (if he-search-bw | ||
| 442 | (progn | ||
| 443 | (goto-char he-search-loc) | ||
| 444 | (setq expansion (he-line-search he-search-string | ||
| 445 | strip-prompt t)) | ||
| 446 | (setq he-search-loc (point-marker)) | ||
| 447 | (if (not expansion) | ||
| 448 | (progn | ||
| 449 | (setq he-search-loc he-string-end) | ||
| 450 | (setq he-search-bw ()))))) | ||
| 451 | |||
| 452 | (if (not expansion) ; Then look forward. | ||
| 453 | (progn | ||
| 454 | (goto-char he-search-loc) | ||
| 455 | (setq expansion (he-line-search he-search-string | ||
| 456 | strip-prompt nil)) | ||
| 457 | (setq he-search-loc (point-marker)))))) | ||
| 458 | |||
| 459 | (if (not expansion) | ||
| 460 | (progn | ||
| 461 | (he-reset-string) | ||
| 462 | ()) | ||
| 463 | (progn | ||
| 464 | (he-substitute-string expansion t) | ||
| 465 | (setq he-tried-table (cons expansion he-tried-table)) | ||
| 466 | t)))) | ||
| 467 | |||
| 468 | (defun try-expand-line-all-buffers (old) | ||
| 469 | "Try to complete the current line, searching all other buffers. | ||
| 470 | The argument OLD has to be nil the first call of this function, and t | ||
| 471 | for subsequent calls (for further possible completions of the same | ||
| 472 | string). It returns t if a new completion is found, nil otherwise." | ||
| 473 | (let ((expansion ()) | ||
| 474 | (strip-prompt (and (get-buffer-process (current-buffer)) | ||
| 475 | shell-prompt-pattern)) | ||
| 476 | (buf (current-buffer))) | ||
| 477 | (if (not old) | ||
| 478 | (progn | ||
| 479 | (he-init-string (he-line-beg strip-prompt) (point)) | ||
| 480 | (setq he-search-loc 0) | ||
| 481 | (setq he-search-bufs (buffer-list)))) | ||
| 482 | |||
| 483 | (if (not (equal he-search-string "")) | ||
| 484 | (while (and he-search-bufs (not expansion)) | ||
| 485 | (set-buffer (car he-search-bufs)) | ||
| 486 | (if (and (not (eq (current-buffer) buf)) | ||
| 487 | (not (eq major-mode 'dired-mode))) | ||
| 488 | ;; dont search dired buffers | ||
| 489 | (save-excursion | ||
| 490 | (goto-char he-search-loc) | ||
| 491 | (setq expansion (he-line-search he-search-string | ||
| 492 | strip-prompt nil)) | ||
| 493 | (setq he-search-loc (point-marker)))) | ||
| 494 | (if expansion | ||
| 495 | (setq he-tried-table (cons expansion he-tried-table)) | ||
| 496 | (progn | ||
| 497 | (setq he-search-loc 0) | ||
| 498 | (setq he-search-bufs (cdr he-search-bufs)))))) | ||
| 499 | |||
| 500 | (set-buffer buf) | ||
| 501 | (if (not expansion) | ||
| 502 | (progn | ||
| 503 | (he-reset-string) | ||
| 504 | ()) | ||
| 505 | (progn | ||
| 506 | (he-substitute-string expansion t) | ||
| 507 | t)))) | ||
| 508 | |||
| 509 | (defun he-line-search (str strip-prompt reverse) | ||
| 510 | (let ((result ())) | ||
| 511 | (while (and (not result) | ||
| 512 | (if reverse | ||
| 513 | (re-search-backward | ||
| 514 | (he-line-search-regexp str strip-prompt) | ||
| 515 | nil t) | ||
| 516 | (re-search-forward | ||
| 517 | (he-line-search-regexp str strip-prompt) | ||
| 518 | nil t))) | ||
| 519 | (setq result (buffer-substring (match-beginning 2) (match-end 2))) | ||
| 520 | (if (he-string-member result he-tried-table) | ||
| 521 | (setq result nil))) ; if already in table, ignore | ||
| 522 | result)) | ||
| 523 | |||
| 524 | (defun he-line-beg (strip-prompt) | ||
| 525 | (save-excursion | ||
| 526 | (end-of-line) | ||
| 527 | (if (re-search-backward (he-line-search-regexp "" strip-prompt) | ||
| 528 | (save-excursion (beginning-of-line) | ||
| 529 | (point)) t) | ||
| 530 | (match-beginning 2) | ||
| 531 | (beginning-of-line) | ||
| 532 | (point)))) | ||
| 533 | |||
| 534 | (defun he-line-search-regexp (pat strip-prompt) | ||
| 535 | (if strip-prompt | ||
| 536 | (concat "\\(" shell-prompt-pattern "\\|^\\s-*\\)\\(" | ||
| 537 | (regexp-quote pat) | ||
| 538 | "[^\n]*[^ \t\n]\\)") | ||
| 539 | (concat "^\\(\\s-*\\)\\(" | ||
| 540 | (regexp-quote pat) | ||
| 541 | "[^\n]*[^ \t\n]\\)"))) | ||
| 542 | |||
| 543 | (defun try-expand-all-abbrevs (old) | ||
| 544 | "Try to expand word before point according to all abbrev tables. | ||
| 545 | The argument OLD has to be nil the first call of this function, and t | ||
| 546 | for subsequent calls (for further possible expansions of the same | ||
| 547 | string). It returns t if a new expansion is found, nil otherwise." | ||
| 548 | (if (not old) | ||
| 549 | (progn | ||
| 550 | (he-init-string (he-dabbrev-beg) (point)) | ||
| 551 | (setq he-expand-list | ||
| 552 | (and (not (equal he-search-string "")) | ||
| 553 | (mapcar (function (lambda (sym) | ||
| 554 | (abbrev-expansion he-search-string | ||
| 555 | (eval sym)))) | ||
| 556 | (append '(local-abbrev-table | ||
| 557 | global-abbrev-table) | ||
| 558 | abbrev-table-name-list)))))) | ||
| 559 | (while (and he-expand-list | ||
| 560 | (or (not (car he-expand-list)) | ||
| 561 | (he-string-member (car he-expand-list) he-tried-table))) | ||
| 562 | (setq he-expand-list (cdr he-expand-list))) | ||
| 563 | (if (null he-expand-list) | ||
| 564 | (progn | ||
| 565 | (he-reset-string) | ||
| 566 | ()) | ||
| 567 | (progn | ||
| 568 | (he-substitute-string (car he-expand-list)) | ||
| 569 | (setq he-tried-table (cons (car he-expand-list) he-tried-table)) | ||
| 570 | (setq he-expand-list (cdr he-expand-list)) | ||
| 571 | t))) | ||
| 572 | |||
| 573 | (defun try-expand-dabbrev (old) | ||
| 574 | "Try to expand word \"dynamically\", searching the current buffer. | ||
| 575 | The argument OLD has to be nil the first call of this function, and t | ||
| 576 | for subsequent calls (for further possible expansions of the same | ||
| 577 | string). It returns t if a new expansion is found, nil otherwise." | ||
| 578 | (let ((expansion ())) | ||
| 579 | (if (not old) | ||
| 580 | (progn | ||
| 581 | (he-init-string (he-dabbrev-beg) (point)) | ||
| 582 | (setq he-search-loc he-string-beg) | ||
| 583 | (setq he-search-bw t))) | ||
| 584 | |||
| 585 | (if (not (equal he-search-string "")) | ||
| 586 | (save-excursion | ||
| 587 | ;; Try looking backward unless inhibited. | ||
| 588 | (if he-search-bw | ||
| 589 | (progn | ||
| 590 | (goto-char he-search-loc) | ||
| 591 | (setq expansion (he-dab-search he-search-string t)) | ||
| 592 | (setq he-search-loc (point-marker)) | ||
| 593 | (if (not expansion) | ||
| 594 | (progn | ||
| 595 | (setq he-search-loc he-string-end) | ||
| 596 | (setq he-search-bw ()))))) | ||
| 597 | |||
| 598 | (if (not expansion) ; Then look forward. | ||
| 599 | (progn | ||
| 600 | (goto-char he-search-loc) | ||
| 601 | (setq expansion (he-dab-search he-search-string nil)) | ||
| 602 | (setq he-search-loc (point-marker)))))) | ||
| 603 | |||
| 604 | (if (not expansion) | ||
| 605 | (progn | ||
| 606 | (he-reset-string) | ||
| 607 | ()) | ||
| 608 | (progn | ||
| 609 | (he-substitute-string expansion t) | ||
| 610 | (setq he-tried-table (cons expansion he-tried-table)) | ||
| 611 | t)))) | ||
| 612 | |||
| 613 | (defun try-expand-dabbrev-all-buffers (old) | ||
| 614 | "Tries to expand word \"dynamically\", searching all other buffers. | ||
| 615 | The argument OLD has to be nil the first call of this function, and t | ||
| 616 | for subsequent calls (for further possible expansions of the same | ||
| 617 | string). It returns t if a new expansion is found, nil otherwise." | ||
| 618 | (let ((expansion ()) | ||
| 619 | (buf (current-buffer))) | ||
| 620 | (if (not old) | ||
| 621 | (progn | ||
| 622 | (he-init-string (he-dabbrev-beg) (point)) | ||
| 623 | (setq he-search-loc 0) | ||
| 624 | (setq he-search-bufs (buffer-list)))) | ||
| 625 | |||
| 626 | (if (not (equal he-search-string "")) | ||
| 627 | (while (and he-search-bufs (not expansion)) | ||
| 628 | (set-buffer (car he-search-bufs)) | ||
| 629 | (if (and (not (eq (current-buffer) buf)) | ||
| 630 | (not (eq major-mode 'dired-mode))) | ||
| 631 | ;; dont search dired buffers | ||
| 632 | (save-excursion | ||
| 633 | (goto-char he-search-loc) | ||
| 634 | (setq expansion (he-dab-search he-search-string nil)) | ||
| 635 | (setq he-search-loc (point-marker)))) | ||
| 636 | (if expansion | ||
| 637 | (setq he-tried-table (cons expansion he-tried-table)) | ||
| 638 | (progn | ||
| 639 | (setq he-search-loc 0) | ||
| 640 | (setq he-search-bufs (cdr he-search-bufs)))))) | ||
| 641 | |||
| 642 | (set-buffer buf) | ||
| 643 | (if (not expansion) | ||
| 644 | (progn | ||
| 645 | (he-reset-string) | ||
| 646 | ()) | ||
| 647 | (progn | ||
| 648 | (he-substitute-string expansion t) | ||
| 649 | t)))) | ||
| 650 | |||
| 651 | (defun he-dab-search-regexp (pat) | ||
| 652 | (concat "\\b" (regexp-quote pat) | ||
| 653 | "\\(\\sw\\|\\s_\\)+")) | ||
| 654 | |||
| 655 | (defun he-dab-search (pattern reverse) | ||
| 656 | (let ((result ())) | ||
| 657 | (while (and (not result) | ||
| 658 | (if reverse | ||
| 659 | (re-search-backward (he-dab-search-regexp pattern) | ||
| 660 | nil t) | ||
| 661 | (re-search-forward (he-dab-search-regexp pattern) | ||
| 662 | nil t))) | ||
| 663 | (setq result (buffer-substring (match-beginning 0) (match-end 0))) | ||
| 664 | (if (he-string-member result he-tried-table) | ||
| 665 | (setq result nil))) ; if already in table, ignore | ||
| 666 | result)) | ||
| 667 | |||
| 668 | (defun he-dabbrev-beg () | ||
| 669 | (let ((skips "-a-zA-Z0-9_.")) | ||
| 670 | (save-excursion | ||
| 671 | (skip-chars-backward skips) | ||
| 672 | (skip-chars-forward "-_.") | ||
| 673 | (point)))) | ||
| 674 | |||