1 ;;; hippie-exp.el --- expand text trying various ways to find its expansion
3 ;; Copyright (C) 1992, 2001, 2002, 2003, 2004, 2005,
4 ;; 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: Anders Holst <aho@sans.kth.se>
7 ;; Last change: 3 March 1998
9 ;; Keywords: abbrev convenience
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 3, or (at your option)
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
30 ;; `hippie-expand' is a single function for a lot of different kinds
31 ;; of completions and expansions. Called repeatedly it tries all
32 ;; possible completions in succession.
33 ;; Which kinds of completions to try, and in which order, is
34 ;; determined by the contents of `hippie-expand-try-functions-list'.
35 ;; Much customization of `hippie-expand' can be made by changing the
36 ;; order of, removing, or inserting new functions in this list.
37 ;; Given a positive numeric argument, `hippie-expand' jumps directly
38 ;; ARG functions forward in this list. Given some other argument
39 ;; (a negative argument or just Ctrl-U) it undoes the tried
42 ;; If the variable `hippie-expand-verbose' is non-nil, `hippie-expand'
43 ;; outputs in a message which try-function in the list that is used
44 ;; currently (ie. was used currently and will be tried first the next
46 ;; The variable `hippie-expand-max-buffers' determines in how many
47 ;; buffers, apart from the current, to search for expansions in. It
48 ;; is used by the try-functions named "-all-buffers".
49 ;; The variable `hippie-expand-ignore-buffers' is a list of regexps
50 ;; matching buffer names (as strings) or major modes (as atoms) of
51 ;; buffers that should not be searched by the try-functions named
53 ;; If set, the variable `hippie-expand-only-buffers' does the opposite
54 ;; of `hippie-expand-ignore-buffers', in that the search is restricted
55 ;; to only the kind of buffers listed.
56 ;; If the variable `hippie-expand-no-restriction' is non-nil, narrowed
57 ;; buffers are widened before they are searched.
58 ;; The variable `hippie-expand-dabbrev-skip-space' controls whether
59 ;; trailing spaces will be included in the abbreviation to search for,
60 ;; which then gives the same behavior as the original `dabbrev-expand'.
61 ;; The variable `hippie-expand-dabbrev-as-symbol' controls whether
62 ;; characters of syntax '_' is considered part of the words to expand
64 ;; See also the macro `make-hippie-expand-function' below.
66 ;; A short description of the current try-functions in this file:
67 ;; `try-complete-file-name' : very convenient to have in any buffer,
68 ;; and not just in the minibuffer or (some) shell-mode. It goes
69 ;; through all possible completions instead of just completing as
71 ;; `try-complete-file-name-partially' : To insert in the list just
72 ;; before `try-complete-file-name' for those who want first to get
73 ;; a file name completed only as many characters as is unique.
74 ;; `try-expand-all-abbrevs' : can be removed if you don't use abbrevs.
75 ;; Otherwise it looks through all abbrev-tables, starting with
76 ;; the local followed by the global.
77 ;; `try-expand-line' : Searches the buffer for an entire line that
78 ;; begins exactly as the current line. Convenient sometimes, for
79 ;; example as a substitute for (or complement to) the history
80 ;; list in shell-like buffers. At other times, only confusing.
81 ;; `try-expand-line-all-buffers' : Like `try-expand-line' but searches
82 ;; in all buffers (except the current). (This may be a little
83 ;; slow, don't use it unless you are really fond of `hippie-expand'.)
84 ;; `try-expand-list' : Tries to expand the text back to the nearest
85 ;; open delimiter, to a whole list from the buffer. Convenient for
86 ;; example when writing lisp or TeX.
87 ;; `try-expand-list-all-buffers' : Like `try-expand-list' but searches
88 ;; in all buffers (except the current).
89 ;; `try-expand-dabbrev' : works exactly as dabbrev-expand (but of
90 ;; course in a way compatible with the other try-functions).
91 ;; `try-expand-dabbrev-all-buffers' : perhaps the most useful of them,
92 ;; like `dabbrev-expand' but searches all Emacs buffers (except the
93 ;; current) for matching words. (No, I don't find this one
94 ;; particularly slow.)
95 ;; `try-expand-dabbrev-visible': Searches the currently visible parts of
96 ;; all windows. Can be put before `try-expand-dabbrev-all-buffers' to
97 ;; first try the expansions you can see.
98 ;; `try-expand-dabbrev-from-kill': Searches the kill ring for a suitable
99 ;; completion of the word. Good to have, just in case the word was not
101 ;; `try-expand-whole-kill' : Tries to complete text with a whole entry
102 ;; from the kill ring. May be good if you don't know how far up in
103 ;; the kill-ring the required entry is, and don't want to mess with
104 ;; "Choose Next Paste".
105 ;; `try-complete-lisp-symbol' : like `lisp-complete-symbol', but goes
106 ;; through all possibilities instead of completing what is unique.
107 ;; Might be tedious (usually a lot of possible completions) and
108 ;; since its function is much like `lisp-complete-symbol', which
109 ;; already has a key of its own, you might want to remove this.
110 ;; `try-complete-lisp-symbol-partially' : To insert in the list just
111 ;; before `try-complete-lisp-symbol' for those who first want to get
112 ;; completion of what is unique in the name.
114 ;; Not all of the above functions are by default in
115 ;; `hippie-expand-try-functions-list'. This variable is better set
116 ;; in ".emacs" to make `hippie-expand' behave maximally convenient
117 ;; according to personal taste. Also, instead of loading the
118 ;; variable with all kinds of try-functions above, it might be an
119 ;; idea to use `make-hippie-expand-function' to construct different
120 ;; `hippie-expand'-like functions, with different try-lists and bound
121 ;; to different keys. It is also possible to make
122 ;; `hippie-expand-try-functions-list' a buffer local variable, and
123 ;; let it depend on the mode (by setting it in the mode-hooks).
125 ;; To write new try-functions, consider the following:
126 ;; Each try-function takes one argument OLD which is nil the first
127 ;; time the function is called and true in succeeding calls for the
128 ;; same string to complete. The first time the function has to
129 ;; extract the string before point to complete, and substitute the
130 ;; first completion alternative for it. On following calls it has to
131 ;; substitute the next possible completion for the last tried string.
132 ;; The try-function is to return t as long as it finds new
133 ;; possible completions. When there are no more alternatives it has
134 ;; to restore the text before point to its original contents, and
135 ;; return nil (don't beep or message or anything).
136 ;; The try-function can (should) use the following functions:
137 ;; `he-init-string' : Initializes the text to substitute to the
138 ;; contents of the region BEGIN to END. Also sets the variable
139 ;; `he-search-string' to the text to expand.
140 ;; `he-substitute-string' : substitutes STR into the region
141 ;; initialized with `he-init-string'. (An optional second argument
142 ;; TRANS-CASE non-nil, means transfer of case from the abbreviation
143 ;; to the expansion is ok if that is enabled in the buffer.)
144 ;; `he-reset-string' : Resets the initialized region to its original
146 ;; There is also a variable: `he-tried-table' which is meant to contain
147 ;; all tried expansions so far. The try-function can check this
148 ;; variable to see whether an expansion has already been tried
149 ;; (hint: `he-string-member').
153 ;; It may happen that some completion suggestion occurs twice, in
154 ;; spite of the use of `he-tried-table' to prevent that. This is
155 ;; because different try-functions may try to complete different
156 ;; lengths of text, and thus put different amounts of the
157 ;; text in `he-tried-table'. Anyway this seems to occur seldom enough
158 ;; not to be too disturbing. Also it should NOT be possible for the
159 ;; opposite situation to occur, that `hippie-expand' misses some
160 ;; suggestion because it thinks it has already tried it.
164 ;; I want to thank Mikael Djurfeldt in discussions with whom the idea
165 ;; of this function took form.
166 ;; I am also grateful to all those who have given me suggestions on
167 ;; how to improve it, and all those who helped to find and remove bugs.
172 (eval-when-compile (require 'comint
))
174 (defgroup hippie-expand nil
175 "Expand text trying various ways to find its expansion."
176 :link
'(custom-manual "(autotype)Hippie Expand")
177 :link
'(emacs-commentary-link "hippie-exp")
183 (defvar he-string-beg
(make-marker))
185 (defvar he-string-end
(make-marker))
187 (defvar he-search-string
())
189 (defvar he-expand-list
())
191 (defvar he-tried-table
())
193 (defvar he-search-loc
(make-marker))
195 (defvar he-search-loc2
())
197 (defvar he-search-bw
())
199 (defvar he-search-bufs
())
201 (defvar he-searched-n-bufs
())
203 (defvar he-search-window
())
206 (defcustom hippie-expand-try-functions-list
207 '(try-complete-file-name-partially
208 try-complete-file-name
209 try-expand-all-abbrevs
213 try-expand-dabbrev-all-buffers
214 try-expand-dabbrev-from-kill
215 try-complete-lisp-symbol-partially
216 try-complete-lisp-symbol
)
217 "The list of expansion functions tried in order by `hippie-expand'.
218 To change the behavior of `hippie-expand', remove, change the order of,
219 or insert functions in this list."
220 :type
'(repeat function
)
221 :group
'hippie-expand
)
224 (defcustom hippie-expand-verbose t
225 "*Non-nil makes `hippie-expand' output which function it is trying."
227 :group
'hippie-expand
)
230 (defcustom hippie-expand-dabbrev-skip-space nil
231 "*Non-nil means tolerate trailing spaces in the abbreviation to expand."
232 :group
'hippie-expand
236 (defcustom hippie-expand-dabbrev-as-symbol t
237 "*Non-nil means expand as symbols, i.e. syntax `_' is considered a letter."
238 :group
'hippie-expand
242 (defcustom hippie-expand-no-restriction t
243 "*Non-nil means that narrowed buffers are widened during search."
244 :group
'hippie-expand
248 (defcustom hippie-expand-max-buffers
()
249 "*The maximum number of buffers (apart from the current) searched.
250 If nil, all buffers are searched."
251 :type
'(choice (const :tag
"All" nil
)
253 :group
'hippie-expand
)
256 (defcustom hippie-expand-ignore-buffers
'("^ \\*.*\\*$" dired-mode
)
257 "*A list specifying which buffers not to search (if not current).
258 Can contain both regexps matching buffer names (as strings) and major modes
260 :type
'(repeat (choice regexp
(symbol :tag
"Major Mode")))
261 :group
'hippie-expand
)
264 (defcustom hippie-expand-only-buffers
()
265 "*A list specifying the only buffers to search (in addition to current).
266 Can contain both regexps matching buffer names (as strings) and major modes
267 \(as atoms). If non-nil, this variable overrides the variable
268 `hippie-expand-ignore-buffers'."
269 :type
'(repeat (choice regexp
(symbol :tag
"Major Mode")))
270 :group
'hippie-expand
)
273 (defun hippie-expand (arg)
274 "Try to expand text before point, using multiple methods.
275 The expansion functions in `hippie-expand-try-functions-list' are
276 tried in order, until a possible expansion is found. Repeated
277 application of `hippie-expand' inserts successively possible
279 With a positive numeric argument, jumps directly to the ARG next
280 function in this list. With a negative argument or just \\[universal-argument],
281 undoes the expansion."
284 (and (integerp arg
) (> arg
0)))
285 (let ((first (or (= he-num -
1)
286 (not (equal this-command last-command
)))))
290 (setq he-tried-table nil
)))
292 (if (not first
) (he-reset-string))
294 (let ((i (max (+ he-num arg
) 0)))
295 (while (not (or (>= i
(length hippie-expand-try-functions-list
))
296 (apply (nth i hippie-expand-try-functions-list
)
297 (list (= he-num i
)))))
300 (if (>= he-num
(length hippie-expand-try-functions-list
))
304 (message "No expansion found")
305 (message "No further expansions found"))
307 (if (and hippie-expand-verbose
308 (not (window-minibuffer-p (selected-window))))
310 (nth he-num hippie-expand-try-functions-list
)))))
311 (if (and (>= he-num
0)
312 (eq (marker-buffer he-string-beg
) (current-buffer)))
316 (if (and hippie-expand-verbose
317 (not (window-minibuffer-p (selected-window))))
318 (message "Undoing expansions"))))))
320 ;; Initializes the region to expand (to between BEG and END).
321 (defun he-init-string (beg end
)
322 (set-marker he-string-beg beg
)
323 (set-marker he-string-end end
)
324 (setq he-search-string
(buffer-substring-no-properties beg end
)))
326 ;; Resets the expanded region to its original contents.
327 (defun he-reset-string ()
328 (let ((newpos (point-marker)))
329 (goto-char he-string-beg
)
330 (insert he-search-string
)
331 (delete-region (point) he-string-end
)
334 ;; Substitutes an expansion STR into the correct region (the region
335 ;; initialized with `he-init-string').
336 ;; An optional argument TRANS-CASE means that it is ok to transfer case
337 ;; from the abbreviation to the expansion if that is possible, and is
338 ;; enabled in the buffer.
339 (defun he-substitute-string (str &optional trans-case
)
340 (let ((trans-case (and trans-case
343 (newpos (point-marker))
345 (goto-char he-string-beg
)
346 (setq subst
(if trans-case
(he-transfer-case he-search-string str
) str
))
347 (setq he-tried-table
(cons subst he-tried-table
))
349 (delete-region (point) he-string-end
)
352 (defun he-capitalize-first (str)
354 (if (string-match "\\Sw*\\(\\sw\\).*" str
)
355 (let ((res (downcase str
))
356 (no (match-beginning 1)))
357 (aset res no
(upcase (aref str no
)))
361 (defun he-ordinary-case-p (str)
362 (or (string= str
(downcase str
))
363 (string= str
(upcase str
))
364 (string= str
(capitalize str
))
365 (string= str
(he-capitalize-first str
))))
367 (defun he-transfer-case (from-str to-str
)
368 (cond ((string= from-str
(substring to-str
0 (min (length from-str
)
371 ((not (he-ordinary-case-p to-str
))
373 ((string= from-str
(downcase from-str
))
375 ((string= from-str
(upcase from-str
))
377 ((string= from-str
(he-capitalize-first from-str
))
378 (he-capitalize-first to-str
))
379 ((string= from-str
(capitalize from-str
))
385 ;; Check if STR is a member of LST.
386 ;; Transform to the final case if optional TRANS-CASE is non-nil.
387 (defun he-string-member (str lst
&optional trans-case
)
389 (member (if (and trans-case
392 (he-transfer-case he-search-string str
)
396 ;; Check if current buffer matches any atom or regexp in LST.
397 ;; Atoms are interpreted as major modes, strings as regexps mathing the name.
398 (defun he-buffer-member (lst)
399 (or (memq major-mode lst
)
402 (or (not (stringp (car lst
)))
403 (not (string-match (car lst
) (buffer-name)))))
404 (setq lst
(cdr lst
)))
407 ;; For the real hippie-expand enthusiast: A macro that makes it
408 ;; possible to use many functions like hippie-expand, but with
409 ;; different try-functions-lists.
410 ;; Usage is for example:
411 ;; (fset 'my-complete-file (make-hippie-expand-function
412 ;; '(try-complete-file-name-partially
413 ;; try-complete-file-name)))
414 ;; (fset 'my-complete-line (make-hippie-expand-function
416 ;; try-expand-line-all-buffers)))
419 (defmacro make-hippie-expand-function
(try-list &optional verbose
)
420 "Construct a function similar to `hippie-expand'.
421 Make it use the expansion functions in TRY-LIST. An optional second
422 argument VERBOSE non-nil makes the function verbose."
423 `(function (lambda (arg)
425 "Try to expand text before point, using the following functions: \n"
426 (mapconcat 'prin1-to-string
(eval try-list
) ", "))
428 (let ((hippie-expand-try-functions-list ,try-list
)
429 (hippie-expand-verbose ,verbose
))
430 (hippie-expand arg
)))))
433 ;;; Here follows the try-functions and their requisites:
436 (defun try-complete-file-name (old)
437 "Try to complete text as a file name.
438 The argument OLD has to be nil the first call of this function, and t
439 for subsequent calls (for further possible completions of the same
440 string). It returns t if a new completion is found, nil otherwise."
443 (he-init-string (he-file-name-beg) (point))
444 (let ((name-part (he-file-name-nondirectory he-search-string
))
445 (dir-part (expand-file-name (or (he-file-name-directory
446 he-search-string
) ""))))
447 (if (not (he-string-member name-part he-tried-table
))
448 (setq he-tried-table
(cons name-part he-tried-table
)))
449 (if (and (not (equal he-search-string
""))
450 (he-file-directory-p dir-part
))
451 (setq he-expand-list
(sort (file-name-all-completions
455 (setq he-expand-list
())))))
457 (while (and he-expand-list
458 (he-string-member (car he-expand-list
) he-tried-table
))
459 (setq he-expand-list
(cdr he-expand-list
)))
460 (if (null he-expand-list
)
462 (if old
(he-reset-string))
464 (let ((filename (he-concat-directory-file-name
465 (he-file-name-directory he-search-string
)
466 (car he-expand-list
))))
467 (he-substitute-string filename
)
468 (setq he-tried-table
(cons (car he-expand-list
) (cdr he-tried-table
)))
469 (setq he-expand-list
(cdr he-expand-list
))
472 (defun try-complete-file-name-partially (old)
473 "Try to complete text as a file name, as many characters as unique.
474 The argument OLD has to be nil the first call of this function. It
475 returns t if a unique, possibly partial, completion is found, nil
477 (let ((expansion ()))
480 (he-init-string (he-file-name-beg) (point))
481 (let ((name-part (he-file-name-nondirectory he-search-string
))
482 (dir-part (expand-file-name (or (he-file-name-directory
483 he-search-string
) ""))))
484 (if (and (not (equal he-search-string
""))
485 (he-file-directory-p dir-part
))
486 (setq expansion
(file-name-completion name-part
488 (if (or (eq expansion t
)
489 (string= expansion name-part
)
490 (he-string-member expansion he-tried-table
))
491 (setq expansion
())))))
495 (if old
(he-reset-string))
497 (let ((filename (he-concat-directory-file-name
498 (he-file-name-directory he-search-string
)
500 (he-substitute-string filename
)
501 (setq he-tried-table
(cons expansion
(cdr he-tried-table
)))
504 (defvar he-file-name-chars
505 (cond ((memq system-type
'(vax-vms axp-vms
))
506 "-a-zA-Z0-9_/.,~^#$+=:\\[\\]")
507 ((memq system-type
'(ms-dos windows-nt cygwin
))
508 "-a-zA-Z0-9_/.,~^#$+=:\\\\")
509 (t ;; More strange file formats ?
510 "-a-zA-Z0-9_/.,~^#$+="))
511 "Characters that are considered part of the file name to expand.")
513 (defun he-file-name-beg ()
516 (skip-chars-backward he-file-name-chars
)
517 (if (> (skip-syntax-backward "w") 0) ;; No words with non-file chars
521 ;; Thanks go to Richard Levitte <levitte@e.kth.se> who helped to make these
522 ;; work under VMS, and to David Hughes <ukchugd@ukpmr.cs.philips.nl> who
523 ;; helped to make it work on PC.
524 (defun he-file-name-nondirectory (file)
525 "Fix to make `file-name-nondirectory' work for hippie-expand under VMS."
526 (if (memq system-type
'(axp-vms vax-vms
))
527 (let ((n (file-name-nondirectory file
)))
528 (if (string-match "^\\(\\[.*\\)\\.\\([^\\.]*\\)$" n
)
529 (concat "[." (substring n
(match-beginning 2) (match-end 2)))
531 (file-name-nondirectory file
)))
533 (defun he-file-name-directory (file)
534 "Fix to make `file-name-directory' work for hippie-expand under VMS."
535 (if (memq system-type
'(axp-vms vax-vms
))
536 (let ((n (file-name-nondirectory file
))
537 (d (file-name-directory file
)))
538 (if (string-match "^\\(\\[.*\\)\\.\\([^\\.]*\\)$" n
)
539 (concat d
(substring n
(match-beginning 1) (match-end 1)) "]")
541 (file-name-directory file
)))
543 (defun he-file-directory-p (file)
544 "Fix to make `file-directory-p' work for hippie-expand under VMS."
545 (if (memq system-type
'(vax-vms axp-vms
))
546 (or (file-directory-p file
)
547 (file-directory-p (concat file
"[000000]")))
548 (file-directory-p file
)))
550 (defun he-concat-directory-file-name (dir-part name-part
)
551 "Try to slam together two parts of a file specification, system dependently."
552 (cond ((null dir-part
) name-part
)
553 ((memq system-type
'(axp-vms vax-vms
))
554 (if (and (string= (substring dir-part -
1) "]")
555 (string= (substring name-part
0 2) "[."))
556 (concat (substring dir-part
0 -
1) (substring name-part
1))
557 (concat dir-part name-part
)))
558 ((memq system-type
'(ms-dos w32
))
559 (if (and (string-match "\\\\" dir-part
)
560 (not (string-match "/" dir-part
))
561 (= (aref name-part
(1- (length name-part
))) ?
/))
562 (aset name-part
(1- (length name-part
)) ?
\\))
563 (concat dir-part name-part
))
565 (concat dir-part name-part
))))
567 (defun try-complete-lisp-symbol (old)
568 "Try to complete word as an Emacs Lisp symbol.
569 The argument OLD has to be nil the first call of this function, and t
570 for subsequent calls (for further possible completions of the same
571 string). It returns t if a new completion is found, nil otherwise."
574 (he-init-string (he-lisp-symbol-beg) (point))
575 (if (not (he-string-member he-search-string he-tried-table
))
576 (setq he-tried-table
(cons he-search-string he-tried-table
)))
578 (and (not (equal he-search-string
""))
579 (sort (all-completions he-search-string obarray
580 (function (lambda (sym)
583 (symbol-plist sym
)))))
585 (while (and he-expand-list
586 (he-string-member (car he-expand-list
) he-tried-table
))
587 (setq he-expand-list
(cdr he-expand-list
)))
588 (if (null he-expand-list
)
590 (if old
(he-reset-string))
593 (he-substitute-string (car he-expand-list
))
594 (setq he-expand-list
(cdr he-expand-list
))
597 (defun try-complete-lisp-symbol-partially (old)
598 "Try to complete as an Emacs Lisp symbol, as many characters as unique.
599 The argument OLD has to be nil the first call of this function. It
600 returns t if a unique, possibly partial, completion is found, nil
602 (let ((expansion ()))
605 (he-init-string (he-lisp-symbol-beg) (point))
606 (if (not (string= he-search-string
""))
608 (try-completion he-search-string obarray
609 (function (lambda (sym)
612 (symbol-plist sym
)))))))
613 (if (or (eq expansion t
)
614 (string= expansion he-search-string
)
615 (he-string-member expansion he-tried-table
))
616 (setq expansion
()))))
620 (if old
(he-reset-string))
623 (he-substitute-string expansion
)
626 (defun he-lisp-symbol-beg ()
628 (skip-syntax-backward "w_")
631 (defun try-expand-line (old)
632 "Try to complete the current line to an entire line in the buffer.
633 The argument OLD has to be nil the first call of this function, and t
634 for subsequent calls (for further possible completions of the same
635 string). It returns t if a new completion is found, nil otherwise."
637 (strip-prompt (and (get-buffer-process (current-buffer))
638 comint-use-prompt-regexp
639 comint-prompt-regexp
)))
642 (he-init-string (he-line-beg strip-prompt
) (point))
643 (set-marker he-search-loc he-string-beg
)
644 (setq he-search-bw t
)))
646 (if (not (equal he-search-string
""))
649 (if hippie-expand-no-restriction
651 ;; Try looking backward unless inhibited.
654 (goto-char he-search-loc
)
655 (setq expansion
(he-line-search he-search-string
657 (set-marker he-search-loc
(point))
660 (set-marker he-search-loc he-string-end
)
661 (setq he-search-bw
())))))
663 (if (not expansion
) ; Then look forward.
665 (goto-char he-search-loc
)
666 (setq expansion
(he-line-search he-search-string
668 (set-marker he-search-loc
(point)))))))
672 (if old
(he-reset-string))
675 (he-substitute-string expansion t
)
678 (defun try-expand-line-all-buffers (old)
679 "Try to complete the current line, searching all other buffers.
680 The argument OLD has to be nil the first call of this function, and t
681 for subsequent calls (for further possible completions of the same
682 string). It returns t if a new completion is found, nil otherwise."
684 (strip-prompt (and (get-buffer-process (current-buffer))
685 comint-use-prompt-regexp
686 comint-prompt-regexp
))
687 (buf (current-buffer))
688 (orig-case-fold-search case-fold-search
))
691 (he-init-string (he-line-beg strip-prompt
) (point))
692 (setq he-search-bufs
(buffer-list))
693 (setq he-searched-n-bufs
0)
694 (set-marker he-search-loc
1 (car he-search-bufs
))))
696 (if (not (equal he-search-string
""))
697 (while (and he-search-bufs
699 (or (not hippie-expand-max-buffers
)
700 (< he-searched-n-bufs hippie-expand-max-buffers
)))
701 (set-buffer (car he-search-bufs
))
702 (if (and (not (eq (current-buffer) buf
))
703 (if hippie-expand-only-buffers
704 (he-buffer-member hippie-expand-only-buffers
)
705 (not (he-buffer-member hippie-expand-ignore-buffers
))))
708 (if hippie-expand-no-restriction
710 (goto-char he-search-loc
)
711 (setq strip-prompt
(and (get-buffer-process (current-buffer))
712 comint-use-prompt-regexp
713 comint-prompt-regexp
))
715 (let ((case-fold-search orig-case-fold-search
))
716 (he-line-search he-search-string
718 (set-marker he-search-loc
(point))
721 (setq he-search-bufs
(cdr he-search-bufs
))
722 (setq he-searched-n-bufs
(1+ he-searched-n-bufs
))
723 (set-marker he-search-loc
1 (car he-search-bufs
))))))
724 (setq he-search-bufs
(cdr he-search-bufs
))
725 (set-marker he-search-loc
1 (car he-search-bufs
)))))
730 (if old
(he-reset-string))
733 (he-substitute-string expansion t
)
736 (defun he-line-search (str strip-prompt reverse
)
738 (while (and (not result
)
741 (he-line-search-regexp str strip-prompt
)
744 (he-line-search-regexp str strip-prompt
)
746 (setq result
(buffer-substring-no-properties (match-end 1)
748 (if (he-string-member result he-tried-table t
)
749 (setq result nil
))) ; if already in table, ignore
752 (defun he-line-beg (strip-prompt)
754 (if (re-search-backward (he-line-search-regexp "" strip-prompt
)
755 (save-excursion (beginning-of-line)
760 (defun he-line-search-regexp (pat strip-prompt
)
762 (concat "\\(" comint-prompt-regexp
"\\|^\\s-*\\)\\("
765 (concat "^\\(\\s-*\\)\\("
767 "[^\n]*[^ \t\n]\\)")))
769 (defun try-expand-list (old)
770 "Try to complete the current beginning of a list.
771 The argument OLD has to be nil the first call of this function, and t
772 for subsequent calls (for further possible completions of the same
773 string). It returns t if a new completion is found, nil otherwise."
774 (let ((expansion ()))
777 (he-init-string (he-list-beg) (point))
778 (set-marker he-search-loc he-string-beg
)
779 (setq he-search-bw t
)))
781 (if (not (equal he-search-string
""))
784 (if hippie-expand-no-restriction
786 ;; Try looking backward unless inhibited.
789 (goto-char he-search-loc
)
790 (setq expansion
(he-list-search he-search-string t
))
791 (set-marker he-search-loc
(point))
794 (set-marker he-search-loc he-string-end
)
795 (setq he-search-bw
())))))
797 (if (not expansion
) ; Then look forward.
799 (goto-char he-search-loc
)
800 (setq expansion
(he-list-search he-search-string nil
))
801 (set-marker he-search-loc
(point)))))))
805 (if old
(he-reset-string))
808 (he-substitute-string expansion t
)
811 (defun try-expand-list-all-buffers (old)
812 "Try to complete the current list, searching all other buffers.
813 The argument OLD has to be nil the first call of this function, and t
814 for subsequent calls (for further possible completions of the same
815 string). It returns t if a new completion is found, nil otherwise."
817 (buf (current-buffer))
818 (orig-case-fold-search case-fold-search
))
821 (he-init-string (he-list-beg) (point))
822 (setq he-search-bufs
(buffer-list))
823 (setq he-searched-n-bufs
0)
824 (set-marker he-search-loc
1 (car he-search-bufs
))))
826 (if (not (equal he-search-string
""))
827 (while (and he-search-bufs
829 (or (not hippie-expand-max-buffers
)
830 (< he-searched-n-bufs hippie-expand-max-buffers
)))
831 (set-buffer (car he-search-bufs
))
832 (if (and (not (eq (current-buffer) buf
))
833 (if hippie-expand-only-buffers
834 (he-buffer-member hippie-expand-only-buffers
)
835 (not (he-buffer-member hippie-expand-ignore-buffers
))))
838 (if hippie-expand-no-restriction
840 (goto-char he-search-loc
)
842 (let ((case-fold-search orig-case-fold-search
))
843 (he-list-search he-search-string nil
)))
844 (set-marker he-search-loc
(point))
847 (setq he-search-bufs
(cdr he-search-bufs
))
848 (setq he-searched-n-bufs
(1+ he-searched-n-bufs
))
849 (set-marker he-search-loc
1 (car he-search-bufs
))))))
850 (setq he-search-bufs
(cdr he-search-bufs
))
851 (set-marker he-search-loc
1 (car he-search-bufs
)))))
856 (if old
(he-reset-string))
859 (he-substitute-string expansion t
)
862 (defun he-list-search (str reverse
)
865 (while (and (not result
)
867 (search-backward str nil t
)
868 (search-forward str nil t
)))
870 (setq beg
(match-beginning 0))
875 (error (setq err t
)))
877 (> (point) he-string-beg
))
881 (setq result
(buffer-substring-no-properties beg
(point)))
882 (if (he-string-member result he-tried-table t
)
883 (setq result nil
)))) ; if already in table, ignore
887 (defun he-list-beg ()
894 (defun try-expand-all-abbrevs (old)
895 "Try to expand word before point according to all abbrev tables.
896 The argument OLD has to be nil the first call of this function, and t
897 for subsequent calls (for further possible expansions of the same
898 string). It returns t if a new expansion is found, nil otherwise."
901 (he-init-string (he-dabbrev-beg) (point))
903 (and (not (equal he-search-string
""))
904 (mapcar (function (lambda (sym)
905 (if (and (boundp sym
) (vectorp (eval sym
)))
906 (abbrev-expansion (downcase he-search-string
)
908 (append '(local-abbrev-table
910 abbrev-table-name-list
))))))
911 (while (and he-expand-list
912 (or (not (car he-expand-list
))
913 (he-string-member (car he-expand-list
) he-tried-table t
)))
914 (setq he-expand-list
(cdr he-expand-list
)))
915 (if (null he-expand-list
)
917 (if old
(he-reset-string))
920 (he-substitute-string (car he-expand-list
) t
)
921 (setq he-expand-list
(cdr he-expand-list
))
924 (defun try-expand-dabbrev (old)
925 "Try to expand word \"dynamically\", searching the current buffer.
926 The argument OLD has to be nil the first call of this function, and t
927 for subsequent calls (for further possible expansions of the same
928 string). It returns t if a new expansion is found, nil otherwise."
929 (let ((expansion ()))
932 (he-init-string (he-dabbrev-beg) (point))
933 (set-marker he-search-loc he-string-beg
)
934 (setq he-search-bw t
)))
936 (if (not (equal he-search-string
""))
939 (if hippie-expand-no-restriction
941 ;; Try looking backward unless inhibited.
944 (goto-char he-search-loc
)
945 (setq expansion
(he-dabbrev-search he-search-string t
))
946 (set-marker he-search-loc
(point))
949 (set-marker he-search-loc he-string-end
)
950 (setq he-search-bw
())))))
952 (if (not expansion
) ; Then look forward.
954 (goto-char he-search-loc
)
955 (setq expansion
(he-dabbrev-search he-search-string nil
))
956 (set-marker he-search-loc
(point)))))))
960 (if old
(he-reset-string))
963 (he-substitute-string expansion t
)
966 (defun try-expand-dabbrev-all-buffers (old)
967 "Tries to expand word \"dynamically\", searching all other buffers.
968 The argument OLD has to be nil the first call of this function, and t
969 for subsequent calls (for further possible expansions of the same
970 string). It returns t if a new expansion is found, nil otherwise."
972 (buf (current-buffer))
973 (orig-case-fold-search case-fold-search
))
976 (he-init-string (he-dabbrev-beg) (point))
977 (setq he-search-bufs
(buffer-list))
978 (setq he-searched-n-bufs
0)
979 (set-marker he-search-loc
1 (car he-search-bufs
))))
981 (if (not (equal he-search-string
""))
982 (while (and he-search-bufs
984 (or (not hippie-expand-max-buffers
)
985 (< he-searched-n-bufs hippie-expand-max-buffers
)))
986 (set-buffer (car he-search-bufs
))
987 (if (and (not (eq (current-buffer) buf
))
988 (if hippie-expand-only-buffers
989 (he-buffer-member hippie-expand-only-buffers
)
990 (not (he-buffer-member hippie-expand-ignore-buffers
))))
993 (if hippie-expand-no-restriction
995 (goto-char he-search-loc
)
997 (let ((case-fold-search orig-case-fold-search
))
998 (he-dabbrev-search he-search-string nil
)))
999 (set-marker he-search-loc
(point))
1002 (setq he-search-bufs
(cdr he-search-bufs
))
1003 (setq he-searched-n-bufs
(1+ he-searched-n-bufs
))
1004 (set-marker he-search-loc
1 (car he-search-bufs
))))))
1005 (setq he-search-bufs
(cdr he-search-bufs
))
1006 (set-marker he-search-loc
1 (car he-search-bufs
)))))
1011 (if old
(he-reset-string))
1014 (he-substitute-string expansion t
)
1017 ;; Thanks go to Jeff Dairiki <dairiki@faraday.apl.washington.edu> who
1018 ;; suggested this one.
1019 (defun try-expand-dabbrev-visible (old)
1020 "Try to expand word \"dynamically\", searching visible window parts.
1021 The argument OLD has to be nil the first call of this function, and t
1022 for subsequent calls (for further possible expansions of the same
1023 string). It returns t if a new expansion is found, nil otherwise."
1024 (let ((expansion ())
1025 (buf (current-buffer))
1026 (flag (if (frame-visible-p (window-frame (selected-window)))
1030 (he-init-string (he-dabbrev-beg) (point))
1031 (setq he-search-window
(selected-window))
1032 (set-marker he-search-loc
1033 (window-start he-search-window
)
1034 (window-buffer he-search-window
))))
1036 (while (and (not (equal he-search-string
""))
1037 (marker-position he-search-loc
)
1040 (set-buffer (marker-buffer he-search-loc
))
1041 (goto-char he-search-loc
)
1042 (setq expansion
(he-dabbrev-search he-search-string
()
1043 (window-end he-search-window
)))
1045 (eq (marker-buffer he-string-beg
) (current-buffer))
1046 (eq (marker-position he-string-beg
) (match-beginning 0)))
1047 (setq expansion
(he-dabbrev-search he-search-string
()
1048 (window-end he-search-window
))))
1049 (set-marker he-search-loc
(point) (current-buffer)))
1052 (setq he-search-window
(next-window he-search-window nil flag
))
1053 (if (eq he-search-window
(selected-window))
1054 (set-marker he-search-loc nil
)
1055 (set-marker he-search-loc
(window-start he-search-window
)
1056 (window-buffer he-search-window
))))))
1061 (if old
(he-reset-string))
1064 (he-substitute-string expansion t
)
1067 (defun he-dabbrev-search (pattern &optional reverse limit
)
1069 (regpat (cond ((not hippie-expand-dabbrev-as-symbol
)
1070 (concat "\\<" (regexp-quote pattern
) "\\sw+"))
1071 ((eq (char-syntax (aref pattern
0)) ?_
)
1072 (concat (regexp-quote pattern
) "\\(\\sw\\|\\s_\\)+"))
1074 (concat "\\<" (regexp-quote pattern
)
1075 "\\(\\sw\\|\\s_\\)+")))))
1076 (while (and (not result
)
1078 (re-search-backward regpat limit t
)
1079 (re-search-forward regpat limit t
)))
1080 (setq result
(buffer-substring-no-properties (match-beginning 0)
1082 (if (or (and hippie-expand-dabbrev-as-symbol
1083 (> (match-beginning 0) (point-min))
1084 (memq (char-syntax (char-after (1- (match-beginning 0))))
1086 (he-string-member result he-tried-table t
))
1087 (setq result nil
))) ; ignore if bad prefix or already in table
1090 (defun he-dabbrev-beg ()
1093 (if hippie-expand-dabbrev-skip-space
1094 (skip-syntax-backward ". "))
1095 (if (= (skip-syntax-backward (if hippie-expand-dabbrev-as-symbol
1101 (defun try-expand-dabbrev-from-kill (old)
1102 "Try to expand word \"dynamically\", searching the kill ring.
1103 The argument OLD has to be nil the first call of this function, and t
1104 for subsequent calls (for further possible completions of the same
1105 string). It returns t if a new completion is found, nil otherwise."
1106 (let ((expansion ()))
1109 (he-init-string (he-dabbrev-beg) (point))
1110 (setq he-expand-list
1111 (if (not (equal he-search-string
""))
1113 (setq he-search-loc2
0)))
1114 (if (not (equal he-search-string
""))
1115 (setq expansion
(he-dabbrev-kill-search he-search-string
)))
1118 (if old
(he-reset-string))
1121 (he-substitute-string expansion t
)
1124 (defun he-dabbrev-kill-search (pattern)
1126 (regpat (cond ((not hippie-expand-dabbrev-as-symbol
)
1127 (concat "\\<" (regexp-quote pattern
) "\\sw+"))
1128 ((eq (char-syntax (aref pattern
0)) ?_
)
1129 (concat (regexp-quote pattern
) "\\(\\sw\\|\\s_\\)+"))
1131 (concat "\\<" (regexp-quote pattern
)
1132 "\\(\\sw\\|\\s_\\)+"))))
1133 (killstr (car he-expand-list
)))
1134 (while (and (not result
)
1136 (while (and (not result
)
1137 (string-match regpat killstr he-search-loc2
))
1138 (setq result
(substring killstr
(match-beginning 0) (match-end 0)))
1139 (set-text-properties 0 (length result
) () result
)
1140 (setq he-search-loc2
(1+ (match-beginning 0)))
1141 (if (or (and hippie-expand-dabbrev-as-symbol
1142 (> (match-beginning 0) 0)
1143 (memq (char-syntax (aref killstr
(1- (match-beginning 0))))
1145 (he-string-member result he-tried-table t
))
1146 (setq result nil
))) ; ignore if bad prefix or already in table
1147 (if (and (not result
)
1150 (setq he-expand-list
(cdr he-expand-list
))
1151 (setq killstr
(car he-expand-list
))
1152 (setq he-search-loc2
0))))
1155 (defun try-expand-whole-kill (old)
1156 "Try to complete text with something from the kill ring.
1157 The argument OLD has to be nil the first call of this function, and t
1158 for subsequent calls (for further possible completions of the same
1159 string). It returns t if a new completion is found, nil otherwise."
1160 (let ((expansion ()))
1163 (he-init-string (he-kill-beg) (point))
1164 (if (not (he-string-member he-search-string he-tried-table
))
1165 (setq he-tried-table
(cons he-search-string he-tried-table
)))
1166 (setq he-expand-list
1167 (if (not (equal he-search-string
""))
1169 (setq he-search-loc2
())))
1170 (if (not (equal he-search-string
""))
1171 (setq expansion
(he-whole-kill-search he-search-string
)))
1174 (if old
(he-reset-string))
1177 (he-substitute-string expansion
)
1180 (defun he-whole-kill-search (str)
1181 (let ((case-fold-search ())
1183 (str (regexp-quote str
))
1184 (killstr (car he-expand-list
))
1186 (while (and (not result
)
1188 (if (not he-search-loc2
)
1189 (while (setq pos
(string-match str killstr
(1+ pos
)))
1190 (setq he-search-loc2
(cons pos he-search-loc2
))))
1191 (while (and (not result
)
1193 (setq pos
(car he-search-loc2
))
1194 (setq he-search-loc2
(cdr he-search-loc2
))
1196 (goto-char he-string-beg
)
1197 (if (and (>= (- (point) pos
) (point-min)) ; avoid some string GC
1198 (eq (char-after (- (point) pos
)) (aref killstr
0))
1199 (search-backward (substring killstr
0 pos
)
1202 (setq result
(substring killstr pos
))
1203 (set-text-properties 0 (length result
) () result
))))
1205 (he-string-member result he-tried-table
))
1206 (setq result nil
))) ; ignore if already in table
1207 (if (and (not result
)
1210 (setq he-expand-list
(cdr he-expand-list
))
1211 (setq killstr
(car he-expand-list
))
1215 (defun he-kill-beg ()
1218 (skip-syntax-backward "^w_")
1219 (if (= (skip-syntax-backward "w_") 0)
1224 (provide 'hippie-exp
)
1226 ;;; arch-tag: 5e6e00bf-b061-4a7a-9b46-de0ae105ab99
1227 ;;; hippie-exp.el ends here