(replace_buffer_in_all_windows):
[bpt/emacs.git] / lisp / hippie-exp.el
1 ;;; hippie-exp.el --- expand text trying various ways to find its expansion.
2
3 ;; Copyright (C) 1992 Free Software Foundation, Inc.
4
5 ;; Author: Anders Holst <aho@sans.kth.se>
6 ;; Last change: 28 May 1997
7 ;; Version: 1.5
8 ;; Keywords: abbrev
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Commentary:
28
29 ;; `hippie-expand' is a single function for a lot of different kinds
30 ;; of completions and expansions. Called repeatedly it tries all
31 ;; possible completions in succession.
32 ;; Which kinds of completions to try, and in which order, is
33 ;; determined by the contents of `hippie-expand-try-functions-list'.
34 ;; Much customization of `hippie-expand' can be made by changing the
35 ;; order of, removing, or inserting new functions in this list.
36 ;; Given a positive numeric argument, `hippie-expand' jumps directly
37 ;; ARG functions forward in this list. Given some other argument
38 ;; (a negative argument or just Ctrl-U) it undoes the tried
39 ;; completion.
40 ;;
41 ;; If the variable `hippie-expand-verbose' is non-nil, `hippie-expand'
42 ;; outputs in a message which try-function in the list that is used
43 ;; currently (ie. was used currently and will be tried first the next
44 ;; time).
45 ;; The variable `hippie-expand-max-buffers' determines in how many
46 ;; buffers, apart from the current, to search for expansions in. It
47 ;; is used by the try-functions named "-all-buffers".
48 ;; The variable `hippie-expand-ignore-buffers' is a list of regexps
49 ;; matching buffer names (as strings) or major modes (as atoms) of
50 ;; buffers that should not be searched by the try-functions named
51 ;; "-all-buffers".
52 ;; See also the macro `make-hippie-expand-function' below.
53 ;;
54 ;; A short description of the current try-functions in this file:
55 ;; `try-complete-file-name' : very convenient to have in any buffer,
56 ;; and not just in the minibuffer or (some) shell-mode. It goes
57 ;; through all possible completions instead of just completing as
58 ;; much as is unique.
59 ;; `try-complete-file-name-partially' : To insert in the list just
60 ;; before `try-complete-file-name' for those who want first to get
61 ;; a file name completed only as many characters as is unique.
62 ;; `try-expand-all-abbrevs' : can be removed if you don't use abbrevs.
63 ;; Otherwise it looks through all abbrev-tables, starting with
64 ;; the local followed by the global.
65 ;; `try-expand-line' : Searches the buffer for an entire line that
66 ;; begins exactly as the current line. Convenient sometimes, for
67 ;; example as a substitute for (or complement to) the history
68 ;; list in shell-like buffers. At other times, only confusing.
69 ;; `try-expand-line-all-buffers' : Like `try-expand-line' but searches
70 ;; in all buffers (except the current). (This may be a little
71 ;; slow, don't use it unless you are really fond of `hippie-expand'.)
72 ;; `try-expand-list' : Tries to expand the text back to the nearest
73 ;; open delimiter, to a whole list from the buffer. Convenient for
74 ;; example when writing lisp or TeX.
75 ;; `try-expand-list-all-buffers' : Like `try-expand-list' but searches
76 ;; in all buffers (except the current).
77 ;; `try-expand-dabbrev' : works exactly as dabbrev-expand (but of
78 ;; course in a way compatible with the other try-functions).
79 ;; `try-expand-dabbrev-all-buffers' : perhaps the most useful of them,
80 ;; like `dabbrev-expand' but searches all Emacs buffers (except the
81 ;; current) for matching words. (No, I don't find this one
82 ;; particularly slow.)
83 ;; `try-expand-dabbrev-visible': Searches the currently visible parts of
84 ;; all windows. Can be put before `try-expand-dabbrev-all-buffers' to
85 ;; first try the expansions you can see.
86 ;; `try-expand-dabbrev-from-kill': Searches the kill ring for a suitable
87 ;; completion of the word. Good to have, just in case the word was not
88 ;; found elsewhere.
89 ;; `try-expand-whole-kill' : Tries to complete text with a whole entry
90 ;; from the kill ring. May be good if you don't know how far up in
91 ;; the kill-ring the required entry is, and don't want to mess with
92 ;; "Choose Next Paste".
93 ;; `try-complete-lisp-symbol' : like `lisp-complete-symbol', but goes
94 ;; through all possibilities instead of completing what is unique.
95 ;; Might be tedious (usually a lot of possible completions) and
96 ;; since its function is much like `lisp-complete-symbol', which
97 ;; already has a key of its own, you might want to remove this.
98 ;; `try-complete-lisp-symbol-partially' : To insert in the list just
99 ;; before `try-complete-lisp-symbol' for those who first want to get
100 ;; completion of what is unique in the name.
101 ;;
102 ;; Not all of the above functions are by default in
103 ;; `hippie-expand-try-functions-list'. This variable is better set
104 ;; in ".emacs" to make `hippie-expand' behave maximally convenient
105 ;; according to personal taste. Also, instead of loading the
106 ;; variable with all kinds of try-functions above, it might be an
107 ;; idea to use `make-hippie-expand-function' to construct different
108 ;; `hippie-expand'-like functions, with different try-lists and bound
109 ;; to different keys. It is also possible to make
110 ;; `hippie-expand-try-functions-list' a buffer local variable, and
111 ;; let it depend on the mode (by setting it in the mode-hooks).
112 ;;
113 ;; To write new try-functions, consider the following:
114 ;; Each try-function takes one argument OLD which is nil the first
115 ;; time the function is called and true in succeeding calls for the
116 ;; same string to complete. The first time the function has to
117 ;; extract the string before point to complete, and substitute the
118 ;; first completion alternative for it. On following calls it has to
119 ;; substitute the next possible completion for the last tried string.
120 ;; The try-function is to return t as long as it finds new
121 ;; possible completions. When there are no more alternatives it has
122 ;; to restore the text before point to its original contents, and
123 ;; return nil (don't beep or message or anything).
124 ;; The try-function can (should) use the following functions:
125 ;; `he-init-string' : Initializes the text to substitute to the
126 ;; contents of the region BEGIN to END. Also sets the variable
127 ;; `he-search-string' to the text to expand.
128 ;; `he-substitute-string' : substitutes STR into the region
129 ;; initialized with `he-init-string'. (An optional second argument
130 ;; TRANS-CASE non-nil, means transfer of case from the abbreviation
131 ;; to the expansion is ok if that is enabled in the buffer.)
132 ;; `he-reset-string' : Resets the initialized region to its original
133 ;; contents.
134 ;; There is also a variable: `he-tried-table' which is meant to contain
135 ;; all tried expansions so far. The try-function can check this
136 ;; variable to see whether an expansion has already been tried
137 ;; (hint: `he-string-member').
138 ;;
139 ;; Known bugs
140 ;;
141 ;; It may happen that some completion suggestion occurs twice, in
142 ;; spite of the use of `he-tried-table' to prevent that. This is
143 ;; because different try-functions may try to complete different
144 ;; lengths of text, and thus put different amounts of the
145 ;; text in `he-tried-table'. Anyway this seems to occur seldom enough
146 ;; not to be too disturbing. Also it should NOT be possible for the
147 ;; opposite situation to occur, that `hippie-expand' misses some
148 ;; suggestion because it thinks it has already tried it.
149 ;;
150 ;; Acknowledgement
151 ;;
152 ;; I want to thank Mikael Djurfeldt in discussions with whom the idea
153 ;; of this function took form.
154 ;; I am also grateful to all those who have given me suggestions on
155 ;; how to improve it, and all those who helped to find and remove bugs.
156 ;;
157
158 ;;; Code:
159
160 (defgroup hippie-expand nil
161 "Expand text trying various ways to find its expansion."
162 :group 'abbrev)
163
164 (defcustom he-dabbrev-skip-space nil
165 "Non-nil means tolerate trailing spaces in the abbreviation to expand."
166 :group 'hippie-expand
167 :type 'boolean)
168
169 (defcustom he-dabbrev-as-symbol t
170 "Non-nil means expand as symbols, i.e. syntax `_' is considered a letter."
171 :group 'hippie-expand
172 :type 'boolean)
173
174 (defvar he-num -1)
175
176 (defvar he-string-beg (make-marker))
177
178 (defvar he-string-end (make-marker))
179
180 (defvar he-search-string ())
181
182 (defvar he-expand-list ())
183
184 (defvar he-tried-table ())
185
186 (defvar he-search-loc (make-marker))
187
188 (defvar he-search-loc2 ())
189
190 (defvar he-search-bw ())
191
192 (defvar he-search-bufs ())
193
194 (defvar he-searched-n-bufs ())
195
196 (defvar he-search-window ())
197
198 ;;;###autoload
199 (defvar hippie-expand-try-functions-list '(try-complete-file-name-partially
200 try-complete-file-name
201 try-expand-all-abbrevs
202 try-expand-list
203 try-expand-line
204 try-expand-dabbrev
205 try-expand-dabbrev-all-buffers
206 try-expand-dabbrev-from-kill
207 try-complete-lisp-symbol-partially
208 try-complete-lisp-symbol)
209 "The list of expansion functions tried in order by `hippie-expand'.
210 To change the behavior of `hippie-expand', remove, change the order of,
211 or insert functions in this list.")
212
213 ;;;###autoload
214 (defcustom hippie-expand-verbose t
215 "*Non-nil makes `hippie-expand' output which function it is trying."
216 :type 'boolean
217 :group 'hippie-expand)
218
219 ;;;###autoload
220 (defcustom hippie-expand-max-buffers ()
221 "*The maximum number of buffers (apart from the current) searched.
222 If nil, all buffers are searched."
223 :type '(choice (const :tag "All" nil)
224 integer)
225 :group 'hippie-expand)
226
227 ;;;###autoload
228 (defcustom hippie-expand-ignore-buffers '("^ \\*.*\\*$" dired-mode)
229 "*A list specifying which buffers not to search (if not current).
230 Can contain both regexps matching buffer names (as strings) and major modes
231 \(as atoms)"
232 :type '(repeat (choice regexp (symbol :tag "Major Mode")))
233 :group 'hippie-expand)
234
235 ;;;###autoload
236 (defun hippie-expand (arg)
237 "Try to expand text before point, using multiple methods.
238 The expansion functions in `hippie-expand-try-functions-list' are
239 tried in order, until a possible expansion is found. Repeated
240 application of `hippie-expand' inserts successively possible
241 expansions.
242 With a positive numeric argument, jumps directly to the ARG next
243 function in this list. With a negative argument or just \\[universal-argument],
244 undoes the expansion."
245 (interactive "P")
246 (if (or (not arg)
247 (and (integerp arg) (> arg 0)))
248 (let ((first (or (= he-num -1)
249 (not (equal this-command last-command)))))
250 (if first
251 (progn
252 (setq he-num -1)
253 (setq he-tried-table nil)))
254 (if arg
255 (if (not first) (he-reset-string))
256 (setq arg 0))
257 (let ((i (max (+ he-num arg) 0)))
258 (while (not (or (>= i (length hippie-expand-try-functions-list))
259 (apply (nth i hippie-expand-try-functions-list)
260 (list (= he-num i)))))
261 (setq i (1+ i)))
262 (setq he-num i))
263 (if (>= he-num (length hippie-expand-try-functions-list))
264 (progn
265 (setq he-num -1)
266 (if first
267 (message "No expansion found")
268 (message "No further expansions found"))
269 (ding))
270 (if (and hippie-expand-verbose
271 (not (window-minibuffer-p (selected-window))))
272 (message "Using %s"
273 (nth he-num hippie-expand-try-functions-list)))))
274 (if (and (>= he-num 0)
275 (eq (marker-buffer he-string-beg) (current-buffer)))
276 (progn
277 (setq he-num -1)
278 (he-reset-string)
279 (if (and hippie-expand-verbose
280 (not (window-minibuffer-p (selected-window))))
281 (message "Undoing expansions"))))))
282
283 ;; Initializes the region to expand (to between BEG and END).
284 (defun he-init-string (beg end)
285 (set-marker he-string-beg beg)
286 (set-marker he-string-end end)
287 (setq he-search-string (buffer-substring beg end)))
288
289 ;; Resets the expanded region to its original contents.
290 (defun he-reset-string ()
291 (let ((newpos (point-marker)))
292 (goto-char he-string-beg)
293 (insert he-search-string)
294 (delete-region (point) he-string-end)
295 (goto-char newpos)))
296
297 ;; Substitutes an expansion STR into the correct region (the region
298 ;; initialized with `he-init-string').
299 ;; An optional argument TRANS-CASE means that it is ok to transfer case
300 ;; from the abbreviation to the expansion if that is possible, and is
301 ;; enabled in the buffer.
302 (defun he-substitute-string (str &optional trans-case)
303 (let ((trans-case (and trans-case
304 case-replace
305 case-fold-search))
306 (newpos (point-marker))
307 (subst ()))
308 (goto-char he-string-beg)
309 (setq subst (if trans-case (he-transfer-case he-search-string str) str))
310 (setq he-tried-table (cons subst he-tried-table))
311 (insert subst)
312 (delete-region (point) he-string-end)
313 (goto-char newpos)))
314
315 (defun he-capitalize-first (str)
316 (save-match-data
317 (if (string-match "\\Sw*\\(\\sw\\).*" str)
318 (let ((res (downcase str))
319 (no (match-beginning 1)))
320 (aset res no (upcase (aref str no)))
321 res)
322 str)))
323
324 (defun he-ordinary-case-p (str)
325 (or (string= str (downcase str))
326 (string= str (upcase str))
327 (string= str (capitalize str))
328 (string= str (he-capitalize-first str))))
329
330 (defun he-transfer-case (from-str to-str)
331 (cond ((string= from-str (substring to-str 0 (min (length from-str)
332 (length to-str))))
333 to-str)
334 ((not (he-ordinary-case-p to-str))
335 to-str)
336 ((string= from-str (downcase from-str))
337 (downcase to-str))
338 ((string= from-str (upcase from-str))
339 (upcase to-str))
340 ((string= from-str (he-capitalize-first from-str))
341 (he-capitalize-first to-str))
342 ((string= from-str (capitalize from-str))
343 (capitalize to-str))
344 (t
345 to-str)))
346
347
348 ;; Check if STR is a member of LST.
349 ;; Transform to the final case if optional TRANS-CASE is non-NIL.
350 (defun he-string-member (str lst &optional trans-case)
351 (if str
352 (member (if (and trans-case
353 case-replace
354 case-fold-search)
355 (he-transfer-case he-search-string str)
356 str)
357 lst)))
358
359 ;; Check if STR matches any regexp in LST.
360 ;; Ignore possible non-strings in LST.
361 (defun he-regexp-member (str lst)
362 (while (and lst
363 (or (not (stringp (car lst)))
364 (not (string-match (car lst) str))))
365 (setq lst (cdr lst)))
366 lst)
367
368 ;; For the real hippie-expand enthusiast: A macro that makes it
369 ;; possible to use many functions like hippie-expand, but with
370 ;; different try-functions-lists.
371 ;; Usage is for example:
372 ;; (fset 'my-complete-file (make-hippie-expand-function
373 ;; '(try-complete-file-name-partially
374 ;; try-complete-file-name)))
375 ;; (fset 'my-complete-line (make-hippie-expand-function
376 ;; '(try-expand-line
377 ;; try-expand-line-all-buffers)))
378 ;;
379 ;;;###autoload
380 (defmacro make-hippie-expand-function (try-list &optional verbose)
381 "Construct a function similar to `hippie-expand'.
382 Make it use the expansion functions in TRY-LIST. An optional second
383 argument VERBOSE non-nil makes the function verbose."
384 (` (function (lambda (arg)
385 (, (concat
386 "Try to expand text before point, using the following functions: \n"
387 (mapconcat 'prin1-to-string (eval try-list) ", ")))
388 (interactive "P")
389 (let ((hippie-expand-try-functions-list (, try-list))
390 (hippie-expand-verbose (, verbose)))
391 (hippie-expand arg))))))
392
393
394 ;;; Here follows the try-functions and their requisites:
395
396
397 (defun try-complete-file-name (old)
398 "Try to complete text as a file name.
399 The argument OLD has to be nil the first call of this function, and t
400 for subsequent calls (for further possible completions of the same
401 string). It returns t if a new completion is found, nil otherwise."
402 (if (not old)
403 (progn
404 (he-init-string (he-file-name-beg) (point))
405 (let ((name-part (he-file-name-nondirectory he-search-string))
406 (dir-part (expand-file-name (or (he-file-name-directory
407 he-search-string) ""))))
408 (if (not (he-string-member name-part he-tried-table))
409 (setq he-tried-table (cons name-part he-tried-table)))
410 (if (and (not (equal he-search-string ""))
411 (he-file-directory-p dir-part))
412 (setq he-expand-list (sort (file-name-all-completions
413 name-part
414 dir-part)
415 'string-lessp))
416 (setq he-expand-list ())))))
417
418 (while (and he-expand-list
419 (he-string-member (car he-expand-list) he-tried-table))
420 (setq he-expand-list (cdr he-expand-list)))
421 (if (null he-expand-list)
422 (progn
423 (if old (he-reset-string))
424 ())
425 (let ((filename (he-concat-directory-file-name
426 (he-file-name-directory he-search-string)
427 (car he-expand-list))))
428 (he-substitute-string filename)
429 (setq he-tried-table (cons (car he-expand-list) (cdr he-tried-table)))
430 (setq he-expand-list (cdr he-expand-list))
431 t)))
432
433 (defun try-complete-file-name-partially (old)
434 "Try to complete text as a file name, as many characters as unique.
435 The argument OLD has to be nil the first call of this function. It
436 returns t if a unique, possibly partial, completion is found, nil
437 otherwise."
438 (let ((expansion ()))
439 (if (not old)
440 (progn
441 (he-init-string (he-file-name-beg) (point))
442 (let ((name-part (he-file-name-nondirectory he-search-string))
443 (dir-part (expand-file-name (or (he-file-name-directory
444 he-search-string) ""))))
445 (if (and (not (equal he-search-string ""))
446 (he-file-directory-p dir-part))
447 (setq expansion (file-name-completion name-part
448 dir-part)))
449 (if (or (eq expansion t)
450 (string= expansion name-part)
451 (he-string-member expansion he-tried-table))
452 (setq expansion ())))))
453
454 (if (not expansion)
455 (progn
456 (if old (he-reset-string))
457 ())
458 (let ((filename (he-concat-directory-file-name
459 (he-file-name-directory he-search-string)
460 expansion)))
461 (he-substitute-string filename)
462 (setq he-tried-table (cons expansion (cdr he-tried-table)))
463 t))))
464
465 (defvar he-file-name-chars
466 (cond ((memq system-type '(vax-vms axp-vms))
467 "-a-zA-Z0-9_/.,~^#$+=:\\[\\]")
468 ((memq system-type '(ms-dos windows-nt))
469 "-a-zA-Z0-9_/.,~^#$+=:\\\\")
470 (t ;; More strange file formats ?
471 "-a-zA-Z0-9_/.,~^#$+="))
472 "Characters that are considered part of the file name to expand.")
473
474 (defun he-file-name-beg ()
475 (let ((op (point)))
476 (save-excursion
477 (skip-chars-backward he-file-name-chars)
478 (if (> (skip-syntax-backward "w") 0) ;; No words with non-file chars
479 op
480 (point)))))
481
482 ;; Thanks go to Richard Levitte <levitte@e.kth.se> who helped to make these
483 ;; work under VMS, and to David Hughes <ukchugd@ukpmr.cs.philips.nl> who
484 ;; helped to make it work on PC.
485 (defun he-file-name-nondirectory (file)
486 "Fix to make `file-name-nondirectory' work for hippie-expand under VMS."
487 (if (memq system-type '(axp-vms vax-vms))
488 (let ((n (file-name-nondirectory file)))
489 (if (string-match "^\\(\\[.*\\)\\.\\([^\\.]*\\)$" n)
490 (concat "[." (substring n (match-beginning 2) (match-end 2)))
491 n))
492 (file-name-nondirectory file)))
493
494 (defun he-file-name-directory (file)
495 "Fix to make `file-name-directory' work for hippie-expand under VMS."
496 (if (memq system-type '(axp-vms vax-vms))
497 (let ((n (file-name-nondirectory file))
498 (d (file-name-directory file)))
499 (if (string-match "^\\(\\[.*\\)\\.\\([^\\.]*\\)$" n)
500 (concat d (substring n (match-beginning 1) (match-end 1)) "]")
501 d))
502 (file-name-directory file)))
503
504 (defun he-file-directory-p (file)
505 "Fix to make `file-directory-p' work for hippie-expand under VMS."
506 (if (memq system-type '(vax-vms axp-vms))
507 (or (file-directory-p file)
508 (file-directory-p (concat file "[000000]")))
509 (file-directory-p file)))
510
511 (defun he-concat-directory-file-name (dir-part name-part)
512 "Try to slam together two parts of a file specification, system dependently."
513 (cond ((null dir-part) name-part)
514 ((memq system-type '(axp-vms vax-vms))
515 (if (and (string= (substring dir-part -1) "]")
516 (string= (substring name-part 0 2) "[."))
517 (concat (substring dir-part 0 -1) (substring name-part 1))
518 (concat dir-part name-part)))
519 ((memq system-type '(ms-dos w32))
520 (if (and (string-match "\\\\" dir-part)
521 (not (string-match "/" dir-part))
522 (= (aref name-part (1- (length name-part))) ?/))
523 (aset name-part (1- (length name-part)) ?\\))
524 (concat dir-part name-part))
525 (t
526 (concat dir-part name-part))))
527
528 (defun try-complete-lisp-symbol (old)
529 "Try to complete word as an Emacs Lisp symbol.
530 The argument OLD has to be nil the first call of this function, and t
531 for subsequent calls (for further possible completions of the same
532 string). It returns t if a new completion is found, nil otherwise."
533 (if (not old)
534 (progn
535 (he-init-string (he-lisp-symbol-beg) (point))
536 (if (not (he-string-member he-search-string he-tried-table))
537 (setq he-tried-table (cons he-search-string he-tried-table)))
538 (setq he-expand-list
539 (and (not (equal he-search-string ""))
540 (sort (all-completions he-search-string obarray
541 (function (lambda (sym)
542 (or (boundp sym)
543 (fboundp sym)
544 (symbol-plist sym)))))
545 'string-lessp)))))
546 (while (and he-expand-list
547 (he-string-member (car he-expand-list) he-tried-table))
548 (setq he-expand-list (cdr he-expand-list)))
549 (if (null he-expand-list)
550 (progn
551 (if old (he-reset-string))
552 ())
553 (progn
554 (he-substitute-string (car he-expand-list))
555 (setq he-expand-list (cdr he-expand-list))
556 t)))
557
558 (defun try-complete-lisp-symbol-partially (old)
559 "Try to complete as an Emacs Lisp symbol, as many characters as unique.
560 The argument OLD has to be nil the first call of this function. It
561 returns t if a unique, possibly partial, completion is found, nil
562 otherwise."
563 (let ((expansion ()))
564 (if (not old)
565 (progn
566 (he-init-string (he-lisp-symbol-beg) (point))
567 (if (not (string= he-search-string ""))
568 (setq expansion
569 (try-completion he-search-string obarray
570 (function (lambda (sym)
571 (or (boundp sym)
572 (fboundp sym)
573 (symbol-plist sym)))))))
574 (if (or (eq expansion t)
575 (string= expansion he-search-string)
576 (he-string-member expansion he-tried-table))
577 (setq expansion ()))))
578
579 (if (not expansion)
580 (progn
581 (if old (he-reset-string))
582 ())
583 (progn
584 (he-substitute-string expansion)
585 t))))
586
587 (defun he-lisp-symbol-beg ()
588 (save-excursion
589 (skip-syntax-backward "w_")
590 (point)))
591
592 (defun try-expand-line (old)
593 "Try to complete the current line to an entire line in the buffer.
594 The argument OLD has to be nil the first call of this function, and t
595 for subsequent calls (for further possible completions of the same
596 string). It returns t if a new completion is found, nil otherwise."
597 (let ((expansion ())
598 (strip-prompt (and (get-buffer-process (current-buffer))
599 comint-prompt-regexp)))
600 (if (not old)
601 (progn
602 (he-init-string (he-line-beg strip-prompt) (point))
603 (set-marker he-search-loc he-string-beg)
604 (setq he-search-bw t)))
605
606 (if (not (equal he-search-string ""))
607 (save-excursion
608 ;; Try looking backward unless inhibited.
609 (if he-search-bw
610 (progn
611 (goto-char he-search-loc)
612 (setq expansion (he-line-search he-search-string
613 strip-prompt t))
614 (set-marker he-search-loc (point))
615 (if (not expansion)
616 (progn
617 (set-marker he-search-loc he-string-end)
618 (setq he-search-bw ())))))
619
620 (if (not expansion) ; Then look forward.
621 (progn
622 (goto-char he-search-loc)
623 (setq expansion (he-line-search he-search-string
624 strip-prompt nil))
625 (set-marker he-search-loc (point))))))
626
627 (if (not expansion)
628 (progn
629 (if old (he-reset-string))
630 ())
631 (progn
632 (he-substitute-string expansion t)
633 t))))
634
635 (defun try-expand-line-all-buffers (old)
636 "Try to complete the current line, searching all other buffers.
637 The argument OLD has to be nil the first call of this function, and t
638 for subsequent calls (for further possible completions of the same
639 string). It returns t if a new completion is found, nil otherwise."
640 (let ((expansion ())
641 (strip-prompt (and (get-buffer-process (current-buffer))
642 comint-prompt-regexp))
643 (buf (current-buffer))
644 (orig-case-fold-search case-fold-search))
645 (if (not old)
646 (progn
647 (he-init-string (he-line-beg strip-prompt) (point))
648 (setq he-search-bufs (buffer-list))
649 (setq he-searched-n-bufs 0)
650 (set-marker he-search-loc 1 (car he-search-bufs))))
651
652 (if (not (equal he-search-string ""))
653 (while (and he-search-bufs
654 (not expansion)
655 (or (not hippie-expand-max-buffers)
656 (< he-searched-n-bufs hippie-expand-max-buffers)))
657 (set-buffer (car he-search-bufs))
658 (if (and (not (eq (current-buffer) buf))
659 (not (memq major-mode hippie-expand-ignore-buffers))
660 (not (he-regexp-member (buffer-name)
661 hippie-expand-ignore-buffers)))
662 (save-excursion
663 (goto-char he-search-loc)
664 (setq strip-prompt (and (get-buffer-process (current-buffer))
665 comint-prompt-regexp))
666 (setq expansion (let ((case-fold-search orig-case-fold-search))
667 (he-line-search he-search-string
668 strip-prompt nil)))
669 (set-marker he-search-loc (point))
670 (if (not expansion)
671 (progn
672 (setq he-search-bufs (cdr he-search-bufs))
673 (setq he-searched-n-bufs (1+ he-searched-n-bufs))
674 (set-marker he-search-loc 1 (car he-search-bufs)))))
675 (setq he-search-bufs (cdr he-search-bufs))
676 (set-marker he-search-loc 1 (car he-search-bufs)))))
677
678 (set-buffer buf)
679 (if (not expansion)
680 (progn
681 (if old (he-reset-string))
682 ())
683 (progn
684 (he-substitute-string expansion t)
685 t))))
686
687 (defun he-line-search (str strip-prompt reverse)
688 (let ((result ()))
689 (while (and (not result)
690 (if reverse
691 (re-search-backward
692 (he-line-search-regexp str strip-prompt)
693 nil t)
694 (re-search-forward
695 (he-line-search-regexp str strip-prompt)
696 nil t)))
697 (setq result (buffer-substring (match-beginning 2) (match-end 2)))
698 (if (he-string-member result he-tried-table t)
699 (setq result nil))) ; if already in table, ignore
700 result))
701
702 (defun he-line-beg (strip-prompt)
703 (save-excursion
704 (if (re-search-backward (he-line-search-regexp "" strip-prompt)
705 (save-excursion (beginning-of-line)
706 (point)) t)
707 (match-beginning 2)
708 (point))))
709
710 (defun he-line-search-regexp (pat strip-prompt)
711 (if strip-prompt
712 (concat "\\(" comint-prompt-regexp "\\|^\\s-*\\)\\("
713 (regexp-quote pat)
714 "[^\n]*[^ \t\n]\\)")
715 (concat "^\\(\\s-*\\)\\("
716 (regexp-quote pat)
717 "[^\n]*[^ \t\n]\\)")))
718
719 (defun try-expand-list (old)
720 "Try to complete the current beginning of a list.
721 The argument OLD has to be nil the first call of this function, and t
722 for subsequent calls (for further possible completions of the same
723 string). It returns t if a new completion is found, nil otherwise."
724 (let ((expansion ()))
725 (if (not old)
726 (progn
727 (he-init-string (he-list-beg) (point))
728 (set-marker he-search-loc he-string-beg)
729 (setq he-search-bw t)))
730
731 (if (not (equal he-search-string ""))
732 (save-excursion
733 ;; Try looking backward unless inhibited.
734 (if he-search-bw
735 (progn
736 (goto-char he-search-loc)
737 (setq expansion (he-list-search he-search-string t))
738 (set-marker he-search-loc (point))
739 (if (not expansion)
740 (progn
741 (set-marker he-search-loc he-string-end)
742 (setq he-search-bw ())))))
743
744 (if (not expansion) ; Then look forward.
745 (progn
746 (goto-char he-search-loc)
747 (setq expansion (he-list-search he-search-string nil))
748 (set-marker he-search-loc (point))))))
749
750 (if (not expansion)
751 (progn
752 (if old (he-reset-string))
753 ())
754 (progn
755 (he-substitute-string expansion t)
756 t))))
757
758 (defun try-expand-list-all-buffers (old)
759 "Try to complete the current list, searching all other buffers.
760 The argument OLD has to be nil the first call of this function, and t
761 for subsequent calls (for further possible completions of the same
762 string). It returns t if a new completion is found, nil otherwise."
763 (let ((expansion ())
764 (buf (current-buffer))
765 (orig-case-fold-search case-fold-search))
766 (if (not old)
767 (progn
768 (he-init-string (he-list-beg) (point))
769 (setq he-search-bufs (buffer-list))
770 (setq he-searched-n-bufs 0)
771 (set-marker he-search-loc 1 (car he-search-bufs))))
772
773 (if (not (equal he-search-string ""))
774 (while (and he-search-bufs
775 (not expansion)
776 (or (not hippie-expand-max-buffers)
777 (< he-searched-n-bufs hippie-expand-max-buffers)))
778 (set-buffer (car he-search-bufs))
779 (if (and (not (eq (current-buffer) buf))
780 (not (memq major-mode hippie-expand-ignore-buffers))
781 (not (he-regexp-member (buffer-name)
782 hippie-expand-ignore-buffers)))
783 (save-excursion
784 (goto-char he-search-loc)
785 (setq expansion (let ((case-fold-search orig-case-fold-search))
786 (he-list-search he-search-string nil)))
787 (set-marker he-search-loc (point))
788 (if (not expansion)
789 (progn
790 (setq he-search-bufs (cdr he-search-bufs))
791 (setq he-searched-n-bufs (1+ he-searched-n-bufs))
792 (set-marker he-search-loc 1 (car he-search-bufs)))))
793 (setq he-search-bufs (cdr he-search-bufs))
794 (set-marker he-search-loc 1 (car he-search-bufs)))))
795
796 (set-buffer buf)
797 (if (not expansion)
798 (progn
799 (if old (he-reset-string))
800 ())
801 (progn
802 (he-substitute-string expansion t)
803 t))))
804
805 (defun he-list-search (str reverse)
806 (let ((result ())
807 beg pos err)
808 (while (and (not result)
809 (if reverse
810 (search-backward str nil t)
811 (search-forward str nil t)))
812 (setq pos (point))
813 (setq beg (match-beginning 0))
814 (goto-char beg)
815 (setq err ())
816 (condition-case ()
817 (forward-list 1)
818 (error (setq err t)))
819 (if (and reverse
820 (> (point) he-string-beg))
821 (setq err t))
822 (if (not err)
823 (progn
824 (setq result (buffer-substring beg (point)))
825 (if (he-string-member result he-tried-table t)
826 (setq result nil)))) ; if already in table, ignore
827 (goto-char pos))
828 result))
829
830 (defun he-list-beg ()
831 (save-excursion
832 (condition-case ()
833 (backward-up-list 1)
834 (error ()))
835 (point)))
836
837 (defun try-expand-all-abbrevs (old)
838 "Try to expand word before point according to all abbrev tables.
839 The argument OLD has to be nil the first call of this function, and t
840 for subsequent calls (for further possible expansions of the same
841 string). It returns t if a new expansion is found, nil otherwise."
842 (if (not old)
843 (progn
844 (he-init-string (he-dabbrev-beg) (point))
845 (setq he-expand-list
846 (and (not (equal he-search-string ""))
847 (mapcar (function (lambda (sym)
848 (if (and (boundp sym) (vectorp (eval sym)))
849 (abbrev-expansion (downcase he-search-string)
850 (eval sym)))))
851 (append '(local-abbrev-table
852 global-abbrev-table)
853 abbrev-table-name-list))))))
854 (while (and he-expand-list
855 (or (not (car he-expand-list))
856 (he-string-member (car he-expand-list) he-tried-table t)))
857 (setq he-expand-list (cdr he-expand-list)))
858 (if (null he-expand-list)
859 (progn
860 (if old (he-reset-string))
861 ())
862 (progn
863 (he-substitute-string (car he-expand-list) t)
864 (setq he-expand-list (cdr he-expand-list))
865 t)))
866
867 (defun try-expand-dabbrev (old)
868 "Try to expand word \"dynamically\", searching the current buffer.
869 The argument OLD has to be nil the first call of this function, and t
870 for subsequent calls (for further possible expansions of the same
871 string). It returns t if a new expansion is found, nil otherwise."
872 (let ((expansion ()))
873 (if (not old)
874 (progn
875 (he-init-string (he-dabbrev-beg) (point))
876 (set-marker he-search-loc he-string-beg)
877 (setq he-search-bw t)))
878
879 (if (not (equal he-search-string ""))
880 (save-excursion
881 ;; Try looking backward unless inhibited.
882 (if he-search-bw
883 (progn
884 (goto-char he-search-loc)
885 (setq expansion (he-dabbrev-search he-search-string t))
886 (set-marker he-search-loc (point))
887 (if (not expansion)
888 (progn
889 (set-marker he-search-loc he-string-end)
890 (setq he-search-bw ())))))
891
892 (if (not expansion) ; Then look forward.
893 (progn
894 (goto-char he-search-loc)
895 (setq expansion (he-dabbrev-search he-search-string nil))
896 (set-marker he-search-loc (point))))))
897
898 (if (not expansion)
899 (progn
900 (if old (he-reset-string))
901 ())
902 (progn
903 (he-substitute-string expansion t)
904 t))))
905
906 (defun try-expand-dabbrev-all-buffers (old)
907 "Tries to expand word \"dynamically\", searching all other buffers.
908 The argument OLD has to be nil the first call of this function, and t
909 for subsequent calls (for further possible expansions of the same
910 string). It returns t if a new expansion is found, nil otherwise."
911 (let ((expansion ())
912 (buf (current-buffer))
913 (orig-case-fold-search case-fold-search))
914 (if (not old)
915 (progn
916 (he-init-string (he-dabbrev-beg) (point))
917 (setq he-search-bufs (buffer-list))
918 (setq he-searched-n-bufs 0)
919 (set-marker he-search-loc 1 (car he-search-bufs))))
920
921 (if (not (equal he-search-string ""))
922 (while (and he-search-bufs
923 (not expansion)
924 (or (not hippie-expand-max-buffers)
925 (< he-searched-n-bufs hippie-expand-max-buffers)))
926 (set-buffer (car he-search-bufs))
927 (if (and (not (eq (current-buffer) buf))
928 (not (memq major-mode hippie-expand-ignore-buffers))
929 (not (he-regexp-member (buffer-name)
930 hippie-expand-ignore-buffers)))
931 (save-excursion
932 (goto-char he-search-loc)
933 (setq expansion (let ((case-fold-search orig-case-fold-search))
934 (he-dabbrev-search he-search-string nil)))
935 (set-marker he-search-loc (point))
936 (if (not expansion)
937 (progn
938 (setq he-search-bufs (cdr he-search-bufs))
939 (setq he-searched-n-bufs (1+ he-searched-n-bufs))
940 (set-marker he-search-loc 1 (car he-search-bufs)))))
941 (setq he-search-bufs (cdr he-search-bufs))
942 (set-marker he-search-loc 1 (car he-search-bufs)))))
943
944 (set-buffer buf)
945 (if (not expansion)
946 (progn
947 (if old (he-reset-string))
948 ())
949 (progn
950 (he-substitute-string expansion t)
951 t))))
952
953 ;; Thanks go to Jeff Dairiki <dairiki@faraday.apl.washington.edu> who
954 ;; suggested this one.
955 (defun try-expand-dabbrev-visible (old)
956 "Try to expand word \"dynamically\", searching visible window parts.
957 The argument OLD has to be nil the first call of this function, and t
958 for subsequent calls (for further possible expansions of the same
959 string). It returns t if a new expansion is found, nil otherwise."
960 (let ((expansion ())
961 (buf (current-buffer))
962 (flag (if (frame-visible-p (window-frame (selected-window)))
963 'visible t)))
964 (if (not old)
965 (progn
966 (he-init-string (he-dabbrev-beg) (point))
967 (setq he-search-window (selected-window))
968 (set-marker he-search-loc
969 (window-start he-search-window)
970 (window-buffer he-search-window))))
971
972 (while (and (not (equal he-search-string ""))
973 (marker-position he-search-loc)
974 (not expansion))
975 (save-excursion
976 (set-buffer (marker-buffer he-search-loc))
977 (goto-char he-search-loc)
978 (setq expansion (he-dabbrev-search he-search-string ()
979 (window-end he-search-window)))
980 (if (and expansion
981 (eq (marker-buffer he-string-beg) (current-buffer))
982 (eq (marker-position he-string-beg) (match-beginning 0)))
983 (setq expansion (he-dabbrev-search he-search-string ()
984 (window-end he-search-window))))
985 (set-marker he-search-loc (point) (current-buffer)))
986 (if (not expansion)
987 (progn
988 (setq he-search-window (next-window he-search-window nil flag))
989 (if (eq he-search-window (selected-window))
990 (set-marker he-search-loc nil)
991 (set-marker he-search-loc (window-start he-search-window)
992 (window-buffer he-search-window))))))
993
994 (set-buffer buf)
995 (if (not expansion)
996 (progn
997 (if old (he-reset-string))
998 ())
999 (progn
1000 (he-substitute-string expansion t)
1001 t))))
1002
1003 (defun he-dabbrev-search (pattern &optional reverse limit)
1004 (let ((result ())
1005 (regpat (cond ((not he-dabbrev-as-symbol)
1006 (concat "\\<" (regexp-quote pattern) "\\sw+"))
1007 ((eq (char-syntax (aref pattern 0)) ?_)
1008 (concat (regexp-quote pattern) "\\(\\sw\\|\\s_\\)+"))
1009 (t
1010 (concat "\\<" (regexp-quote pattern)
1011 "\\(\\sw\\|\\s_\\)+")))))
1012 (while (and (not result)
1013 (if reverse
1014 (re-search-backward regpat limit t)
1015 (re-search-forward regpat limit t)))
1016 (setq result (buffer-substring (match-beginning 0) (match-end 0)))
1017 (if (or (and he-dabbrev-as-symbol
1018 (> (match-beginning 0) (point-min))
1019 (memq (char-syntax (char-after (1- (match-beginning 0))))
1020 '(?_ ?w)))
1021 (he-string-member result he-tried-table t))
1022 (setq result nil))) ; ignore if bad prefix or already in table
1023 result))
1024
1025 (defun he-dabbrev-beg ()
1026 (let ((op (point)))
1027 (save-excursion
1028 (if he-dabbrev-skip-space
1029 (skip-syntax-backward ". "))
1030 (if (= (skip-syntax-backward (if he-dabbrev-as-symbol "w_" "w")) 0)
1031 op
1032 (point)))))
1033
1034 (defun try-expand-dabbrev-from-kill (old)
1035 "Try to expand word \"dynamically\", searching the kill ring.
1036 The argument OLD has to be nil the first call of this function, and t
1037 for subsequent calls (for further possible completions of the same
1038 string). It returns t if a new completion is found, nil otherwise."
1039 (let ((expansion ()))
1040 (if (not old)
1041 (progn
1042 (he-init-string (he-dabbrev-beg) (point))
1043 (setq he-expand-list
1044 (if (not (equal he-search-string ""))
1045 kill-ring))
1046 (setq he-search-loc2 0)))
1047 (if (not (equal he-search-string ""))
1048 (setq expansion (he-dabbrev-kill-search he-search-string)))
1049 (if (not expansion)
1050 (progn
1051 (if old (he-reset-string))
1052 ())
1053 (progn
1054 (he-substitute-string expansion t)
1055 t))))
1056
1057 (defun he-dabbrev-kill-search (pattern)
1058 (let ((result ())
1059 (regpat (cond ((not he-dabbrev-as-symbol)
1060 (concat "\\<" (regexp-quote pattern) "\\sw+"))
1061 ((eq (char-syntax (aref pattern 0)) ?_)
1062 (concat (regexp-quote pattern) "\\(\\sw\\|\\s_\\)+"))
1063 (t
1064 (concat "\\<" (regexp-quote pattern)
1065 "\\(\\sw\\|\\s_\\)+"))))
1066 (killstr (car he-expand-list)))
1067 (while (and (not result)
1068 he-expand-list)
1069 (while (and (not result)
1070 (string-match regpat killstr he-search-loc2))
1071 (setq result (substring killstr (match-beginning 0) (match-end 0)))
1072 (setq he-search-loc2 (1+ (match-beginning 0)))
1073 (if (or (and he-dabbrev-as-symbol
1074 (> (match-beginning 0) 0)
1075 (memq (char-syntax (aref killstr (1- (match-beginning 0))))
1076 '(?_ ?w)))
1077 (he-string-member result he-tried-table t))
1078 (setq result nil))) ; ignore if bad prefix or already in table
1079 (if (and (not result)
1080 he-expand-list)
1081 (progn
1082 (setq he-expand-list (cdr he-expand-list))
1083 (setq killstr (car he-expand-list))
1084 (setq he-search-loc2 0))))
1085 result))
1086
1087 (defun try-expand-whole-kill (old)
1088 "Try to complete text with something from the kill ring.
1089 The argument OLD has to be nil the first call of this function, and t
1090 for subsequent calls (for further possible completions of the same
1091 string). It returns t if a new completion is found, nil otherwise."
1092 (let ((expansion ()))
1093 (if (not old)
1094 (progn
1095 (he-init-string (he-kill-beg) (point))
1096 (if (not (he-string-member he-search-string he-tried-table))
1097 (setq he-tried-table (cons he-search-string he-tried-table)))
1098 (setq he-expand-list
1099 (if (not (equal he-search-string ""))
1100 kill-ring))
1101 (setq he-search-loc2 ())))
1102 (if (not (equal he-search-string ""))
1103 (setq expansion (he-whole-kill-search he-search-string)))
1104 (if (not expansion)
1105 (progn
1106 (if old (he-reset-string))
1107 ())
1108 (progn
1109 (he-substitute-string expansion)
1110 t))))
1111
1112 (defun he-whole-kill-search (str)
1113 (let ((case-fold-search ())
1114 (result ())
1115 (str (regexp-quote str))
1116 (killstr (car he-expand-list))
1117 (pos -1))
1118 (while (and (not result)
1119 he-expand-list)
1120 (if (not he-search-loc2)
1121 (while (setq pos (string-match str killstr (1+ pos)))
1122 (setq he-search-loc2 (cons pos he-search-loc2))))
1123 (while (and (not result)
1124 he-search-loc2)
1125 (setq pos (car he-search-loc2))
1126 (setq he-search-loc2 (cdr he-search-loc2))
1127 (save-excursion
1128 (goto-char he-string-beg)
1129 (if (and (>= (- (point) pos) (point-min)) ; avoid some string GC
1130 (eq (char-after (- (point) pos)) (aref killstr 0))
1131 (search-backward (substring killstr 0 pos)
1132 (- (point) pos) t))
1133 (setq result (substring killstr pos))))
1134 (if (and result
1135 (he-string-member result he-tried-table))
1136 (setq result nil))) ; ignore if already in table
1137 (if (and (not result)
1138 he-expand-list)
1139 (progn
1140 (setq he-expand-list (cdr he-expand-list))
1141 (setq killstr (car he-expand-list))
1142 (setq pos -1))))
1143 result))
1144
1145 (defun he-kill-beg ()
1146 (let ((op (point)))
1147 (save-excursion
1148 (skip-syntax-backward "^w_")
1149 (if (= (skip-syntax-backward "w_") 0)
1150 op
1151 (point)))))
1152
1153
1154 (provide 'hippie-exp)
1155
1156 ;;; hippie-exp.el ends here