1 ;;; hippie-exp.el --- expand text trying various ways to find its expansion
3 ;; Copyright (C) 1992, 2001-2015 Free Software Foundation, Inc.
5 ;; Author: Anders Holst <aho@sans.kth.se>
6 ;; Maintainer: emacs-devel@gnu.org
8 ;; Keywords: abbrev convenience
10 ;; This file is part of GNU Emacs.
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 3 of the License, or
15 ;; (at your option) any later version.
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.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
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
39 ;; If the variable `hippie-expand-verbose' is non-nil, `hippie-expand'
40 ;; outputs in a message which try-function in the list that is used
41 ;; currently (ie. was used currently and will be tried first the next
43 ;; The variable `hippie-expand-max-buffers' determines in how many
44 ;; buffers, apart from the current, to search for expansions in. It
45 ;; is used by the try-functions named "-all-buffers".
46 ;; The variable `hippie-expand-ignore-buffers' is a list of regexps
47 ;; matching buffer names (as strings) or major modes (as atoms) of
48 ;; buffers that should not be searched by the try-functions named
50 ;; If set, the variable `hippie-expand-only-buffers' does the opposite
51 ;; of `hippie-expand-ignore-buffers', in that the search is restricted
52 ;; to only the kind of buffers listed.
53 ;; If the variable `hippie-expand-no-restriction' is non-nil, narrowed
54 ;; buffers are widened before they are searched.
55 ;; The variable `hippie-expand-dabbrev-skip-space' controls whether
56 ;; trailing spaces will be included in the abbreviation to search for,
57 ;; which then gives the same behavior as the original `dabbrev-expand'.
58 ;; The variable `hippie-expand-dabbrev-as-symbol' controls whether
59 ;; characters of syntax '_' is considered part of the words to expand
61 ;; See also the macro `make-hippie-expand-function' below.
63 ;; A short description of the current try-functions in this file:
64 ;; `try-complete-file-name' : very convenient to have in any buffer,
65 ;; and not just in the minibuffer or (some) shell-mode. It goes
66 ;; through all possible completions instead of just completing as
68 ;; `try-complete-file-name-partially' : To insert in the list just
69 ;; before `try-complete-file-name' for those who want first to get
70 ;; a file name completed only as many characters as is unique.
71 ;; `try-expand-all-abbrevs' : can be removed if you don't use abbrevs.
72 ;; Otherwise it looks through all abbrev-tables, starting with
73 ;; the local followed by the global.
74 ;; `try-expand-line' : Searches the buffer for an entire line that
75 ;; begins exactly as the current line. Convenient sometimes, for
76 ;; example as a substitute for (or complement to) the history
77 ;; list in shell-like buffers. At other times, only confusing.
78 ;; `try-expand-line-all-buffers' : Like `try-expand-line' but searches
79 ;; in all buffers (except the current). (This may be a little
80 ;; slow, don't use it unless you are really fond of `hippie-expand'.)
81 ;; `try-expand-list' : Tries to expand the text back to the nearest
82 ;; open delimiter, to a whole list from the buffer. Convenient for
83 ;; example when writing Lisp or TeX.
84 ;; `try-expand-list-all-buffers' : Like `try-expand-list' but searches
85 ;; in all buffers (except the current).
86 ;; `try-expand-dabbrev' : works exactly as dabbrev-expand (but of
87 ;; course in a way compatible with the other try-functions).
88 ;; `try-expand-dabbrev-all-buffers' : perhaps the most useful of them,
89 ;; like `dabbrev-expand' but searches all Emacs buffers (except the
90 ;; current) for matching words. (No, I don't find this one
91 ;; particularly slow.)
92 ;; `try-expand-dabbrev-visible': Searches the currently visible parts of
93 ;; all windows. Can be put before `try-expand-dabbrev-all-buffers' to
94 ;; first try the expansions you can see.
95 ;; `try-expand-dabbrev-from-kill': Searches the kill ring for a suitable
96 ;; completion of the word. Good to have, just in case the word was not
98 ;; `try-expand-whole-kill' : Tries to complete text with a whole entry
99 ;; from the kill ring. May be good if you don't know how far up in
100 ;; the kill-ring the required entry is, and don't want to mess with
101 ;; "Choose Next Paste".
102 ;; `try-complete-lisp-symbol' : like `lisp-complete-symbol', but goes
103 ;; through all possibilities instead of completing what is unique.
104 ;; Might be tedious (usually a lot of possible completions) and
105 ;; since its function is much like `lisp-complete-symbol', which
106 ;; already has a key of its own, you might want to remove this.
107 ;; `try-complete-lisp-symbol-partially' : To insert in the list just
108 ;; before `try-complete-lisp-symbol' for those who first want to get
109 ;; completion of what is unique in the name.
111 ;; Not all of the above functions are by default in
112 ;; `hippie-expand-try-functions-list'. This variable is better set
113 ;; in ".emacs" to make `hippie-expand' behave maximally convenient
114 ;; according to personal taste. Also, instead of loading the
115 ;; variable with all kinds of try-functions above, it might be an
116 ;; idea to use `make-hippie-expand-function' to construct different
117 ;; `hippie-expand'-like functions, with different try-lists and bound
118 ;; to different keys. It is also possible to make
119 ;; `hippie-expand-try-functions-list' a buffer local variable, and
120 ;; let it depend on the mode (by setting it in the mode-hooks).
122 ;; To write new try-functions, consider the following:
123 ;; Each try-function takes one argument OLD which is nil the first
124 ;; time the function is called and true in succeeding calls for the
125 ;; same string to complete. The first time the function has to
126 ;; extract the string before point to complete, and substitute the
127 ;; first completion alternative for it. On following calls it has to
128 ;; substitute the next possible completion for the last tried string.
129 ;; The try-function is to return t as long as it finds new
130 ;; possible completions. When there are no more alternatives it has
131 ;; to restore the text before point to its original contents, and
132 ;; return nil (don't beep or message or anything).
133 ;; The try-function can (should) use the following functions:
134 ;; `he-init-string' : Initializes the text to substitute to the
135 ;; contents of the region BEGIN to END. Also sets the variable
136 ;; `he-search-string' to the text to expand.
137 ;; `he-substitute-string' : substitutes STR into the region
138 ;; initialized with `he-init-string'. (An optional second argument
139 ;; TRANS-CASE non-nil, means transfer of case from the abbreviation
140 ;; to the expansion is ok if that is enabled in the buffer.)
141 ;; `he-reset-string' : Resets the initialized region to its original
143 ;; There is also a variable: `he-tried-table' which is meant to contain
144 ;; all tried expansions so far. The try-function can check this
145 ;; variable to see whether an expansion has already been tried
146 ;; (hint: `he-string-member').
150 ;; It may happen that some completion suggestion occurs twice, in
151 ;; spite of the use of `he-tried-table' to prevent that. This is
152 ;; because different try-functions may try to complete different
153 ;; lengths of text, and thus put different amounts of the
154 ;; text in `he-tried-table'. Anyway this seems to occur seldom enough
155 ;; not to be too disturbing. Also it should NOT be possible for the
156 ;; opposite situation to occur, that `hippie-expand' misses some
157 ;; suggestion because it thinks it has already tried it.
161 ;; I want to thank Mikael Djurfeldt in discussions with whom the idea
162 ;; of this function took form.
163 ;; I am also grateful to all those who have given me suggestions on
164 ;; how to improve it, and all those who helped to find and remove bugs.
171 (defgroup hippie-expand nil
172 "Expand text trying various ways to find its expansion."
173 :link
'(custom-manual "(autotype)Hippie Expand")
174 :link
'(emacs-commentary-link "hippie-exp")
180 (defvar he-string-beg
(make-marker))
182 (defvar he-string-end
(make-marker))
184 (defvar he-search-string
())
186 (defvar he-expand-list
())
188 (defvar he-tried-table
())
190 (defvar he-search-loc
(make-marker))
192 (defvar he-search-loc2
())
194 (defvar he-search-bw
())
196 (defvar he-search-bufs
())
198 (defvar he-searched-n-bufs
())
200 (defvar he-search-window
())
202 ;;; Autoloaded for historical reasons (bug#12982)
204 (defcustom hippie-expand-try-functions-list
205 '(try-complete-file-name-partially
206 try-complete-file-name
207 try-expand-all-abbrevs
211 try-expand-dabbrev-all-buffers
212 try-expand-dabbrev-from-kill
213 try-complete-lisp-symbol-partially
214 try-complete-lisp-symbol
)
215 "The list of expansion functions tried in order by `hippie-expand'.
216 To change the behavior of `hippie-expand', remove, change the order of,
217 or insert functions in this list."
218 :type
'(repeat function
)
219 :group
'hippie-expand
)
221 (defcustom hippie-expand-verbose t
222 "Non-nil makes `hippie-expand' output which function it is trying."
224 :group
'hippie-expand
)
226 (defcustom hippie-expand-dabbrev-skip-space nil
227 "Non-nil means tolerate trailing spaces in the abbreviation to expand."
228 :group
'hippie-expand
231 (defcustom hippie-expand-dabbrev-as-symbol t
232 "Non-nil means expand as symbols, i.e. syntax `_' is considered a letter."
233 :group
'hippie-expand
236 (defcustom hippie-expand-no-restriction t
237 "Non-nil means that narrowed buffers are widened during search."
238 :group
'hippie-expand
241 (defcustom hippie-expand-max-buffers
()
242 "The maximum number of buffers (apart from the current) searched.
243 If nil, all buffers are searched."
244 :type
'(choice (const :tag
"All" nil
)
246 :group
'hippie-expand
)
248 (defcustom hippie-expand-ignore-buffers
'("^ \\*.*\\*$" dired-mode
)
249 "A list specifying which buffers not to search (if not current).
250 Can contain both regexps matching buffer names (as strings) and major modes
252 :type
'(repeat (choice regexp
(symbol :tag
"Major Mode")))
253 :group
'hippie-expand
)
255 (defcustom hippie-expand-only-buffers
()
256 "A list specifying the only buffers to search (in addition to current).
257 Can contain both regexps matching buffer names (as strings) and major modes
258 \(as atoms). If non-nil, this variable overrides the variable
259 `hippie-expand-ignore-buffers'."
260 :type
'(repeat (choice regexp
(symbol :tag
"Major Mode")))
261 :group
'hippie-expand
)
264 (defun hippie-expand (arg)
265 "Try to expand text before point, using multiple methods.
266 The expansion functions in `hippie-expand-try-functions-list' are
267 tried in order, until a possible expansion is found. Repeated
268 application of `hippie-expand' inserts successively possible
270 With a positive numeric argument, jumps directly to the ARG next
271 function in this list. With a negative argument or just \\[universal-argument],
272 undoes the expansion."
275 (and (integerp arg
) (> arg
0)))
276 (let ((first (or (= he-num -
1)
277 (not (equal this-command last-command
)))))
281 (setq he-tried-table nil
)))
283 (if (not first
) (he-reset-string))
285 (let ((i (max (+ he-num arg
) 0)))
286 (while (not (or (>= i
(length hippie-expand-try-functions-list
))
287 (apply (nth i hippie-expand-try-functions-list
)
288 (list (= he-num i
)))))
291 (if (>= he-num
(length hippie-expand-try-functions-list
))
295 (message "No expansion found")
296 (message "No further expansions found"))
298 (if (and hippie-expand-verbose
299 (not (window-minibuffer-p)))
301 (nth he-num hippie-expand-try-functions-list
)))))
302 (if (and (>= he-num
0)
303 (eq (marker-buffer he-string-beg
) (current-buffer)))
307 (if (and hippie-expand-verbose
308 (not (window-minibuffer-p)))
309 (message "Undoing expansions"))))))
311 ;; Initializes the region to expand (to between BEG and END).
312 (defun he-init-string (beg end
)
313 (set-marker he-string-beg beg
)
314 (set-marker he-string-end end
)
315 (setq he-search-string
(buffer-substring-no-properties beg end
)))
317 ;; Resets the expanded region to its original contents.
318 (defun he-reset-string ()
319 (let ((newpos (point-marker)))
320 (goto-char he-string-beg
)
321 (insert he-search-string
)
322 (delete-region (point) he-string-end
)
325 ;; Substitutes an expansion STR into the correct region (the region
326 ;; initialized with `he-init-string').
327 ;; An optional argument TRANS-CASE means that it is ok to transfer case
328 ;; from the abbreviation to the expansion if that is possible, and is
329 ;; enabled in the buffer.
330 (defun he-substitute-string (str &optional trans-case
)
331 (let ((trans-case (and trans-case
334 (newpos (point-marker))
336 (goto-char he-string-beg
)
337 (setq subst
(if trans-case
(he-transfer-case he-search-string str
) str
))
338 (setq he-tried-table
(cons subst he-tried-table
))
340 (delete-region (point) he-string-end
)
343 (defun he-capitalize-first (str)
345 (if (string-match "\\Sw*\\(\\sw\\).*" str
)
346 (let ((res (downcase str
))
347 (no (match-beginning 1)))
348 (aset res no
(upcase (aref str no
)))
352 (defun he-ordinary-case-p (str)
353 (or (string= str
(downcase str
))
354 (string= str
(upcase str
))
355 (string= str
(capitalize str
))
356 (string= str
(he-capitalize-first str
))))
358 (defun he-transfer-case (from-str to-str
)
359 (cond ((string= from-str
(substring to-str
0 (min (length from-str
)
362 ((not (he-ordinary-case-p to-str
))
364 ((string= from-str
(downcase from-str
))
366 ((string= from-str
(upcase from-str
))
368 ((string= from-str
(he-capitalize-first from-str
))
369 (he-capitalize-first to-str
))
370 ((string= from-str
(capitalize from-str
))
376 ;; Check if STR is a member of LST.
377 ;; Transform to the final case if optional TRANS-CASE is non-nil.
378 (defun he-string-member (str lst
&optional trans-case
)
380 (member (if (and trans-case
383 (he-transfer-case he-search-string str
)
387 ;; Check if current buffer matches any atom or regexp in LST.
388 ;; Atoms are interpreted as major modes, strings as regexps matching the name.
389 (defun he-buffer-member (lst)
390 (or (memq major-mode lst
)
393 (or (not (stringp (car lst
)))
394 (not (string-match (car lst
) (buffer-name)))))
395 (setq lst
(cdr lst
)))
398 ;; For the real hippie-expand enthusiast: A macro that makes it
399 ;; possible to use many functions like hippie-expand, but with
400 ;; different try-functions-lists.
401 ;; Usage is for example:
402 ;; (fset 'my-complete-file (make-hippie-expand-function
403 ;; '(try-complete-file-name-partially
404 ;; try-complete-file-name)))
405 ;; (fset 'my-complete-line (make-hippie-expand-function
407 ;; try-expand-line-all-buffers)))
410 (defmacro make-hippie-expand-function
(try-list &optional verbose
)
411 "Construct a function similar to `hippie-expand'.
412 Make it use the expansion functions in TRY-LIST. An optional second
413 argument VERBOSE non-nil makes the function verbose."
414 `(function (lambda (arg)
416 "Try to expand text before point, using the following functions: \n"
417 (mapconcat 'prin1-to-string
(eval try-list
) ", "))
419 (let ((hippie-expand-try-functions-list ,try-list
)
420 (hippie-expand-verbose ,verbose
))
421 (hippie-expand arg
)))))
424 ;;; Here follows the try-functions and their requisites:
427 (defun try-complete-file-name (old)
428 "Try to complete text as a file name.
429 The argument OLD has to be nil the first call of this function, and t
430 for subsequent calls (for further possible completions of the same
431 string). It returns t if a new completion is found, nil otherwise."
434 (he-init-string (he-file-name-beg) (point))
435 (let ((name-part (file-name-nondirectory he-search-string
))
436 (dir-part (expand-file-name (or (file-name-directory
437 he-search-string
) ""))))
438 (if (not (he-string-member name-part he-tried-table
))
439 (setq he-tried-table
(cons name-part he-tried-table
)))
440 (if (and (not (equal he-search-string
""))
441 (file-directory-p dir-part
))
442 (setq he-expand-list
(sort (file-name-all-completions
446 (setq he-expand-list
())))))
448 (while (and he-expand-list
449 (he-string-member (car he-expand-list
) he-tried-table
))
450 (setq he-expand-list
(cdr he-expand-list
)))
451 (if (null he-expand-list
)
453 (if old
(he-reset-string))
455 (let ((filename (he-concat-directory-file-name
456 (file-name-directory he-search-string
)
457 (car he-expand-list
))))
458 (he-substitute-string filename
)
459 (setq he-tried-table
(cons (car he-expand-list
) (cdr he-tried-table
)))
460 (setq he-expand-list
(cdr he-expand-list
))
463 (defun try-complete-file-name-partially (old)
464 "Try to complete text as a file name, as many characters as unique.
465 The argument OLD has to be nil the first call of this function. It
466 returns t if a unique, possibly partial, completion is found, nil
468 (let ((expansion ()))
471 (he-init-string (he-file-name-beg) (point))
472 (let ((name-part (file-name-nondirectory he-search-string
))
473 (dir-part (expand-file-name (or (file-name-directory
474 he-search-string
) ""))))
475 (if (and (not (equal he-search-string
""))
476 (file-directory-p dir-part
))
477 (setq expansion
(file-name-completion name-part
479 (if (or (eq expansion t
)
480 (string= expansion name-part
)
481 (he-string-member expansion he-tried-table
))
482 (setq expansion
())))))
486 (if old
(he-reset-string))
488 (let ((filename (he-concat-directory-file-name
489 (file-name-directory he-search-string
)
491 (he-substitute-string filename
)
492 (setq he-tried-table
(cons expansion
(cdr he-tried-table
)))
495 (defvar he-file-name-chars
496 (cond ((memq system-type
'(ms-dos windows-nt cygwin
))
497 "-a-zA-Z0-9_/.,~^#$+=:\\\\")
498 (t ;; More strange file formats ?
499 "-a-zA-Z0-9_/.,~^#$+="))
500 "Characters that are considered part of the file name to expand.")
502 (defun he-file-name-beg ()
505 (skip-chars-backward he-file-name-chars
)
506 (if (> (skip-syntax-backward "w") 0) ;; No words with non-file chars
510 ;; Thanks go to David Hughes <ukchugd@ukpmr.cs.philips.nl> who
511 ;; helped to make it work on PC.
512 (defun he-concat-directory-file-name (dir-part name-part
)
513 "Try to slam together two parts of a file specification, system dependently."
514 (cond ((null dir-part
) name-part
)
515 ((eq system-type
'ms-dos
)
516 (if (and (string-match "\\\\" dir-part
)
517 (not (string-match "/" dir-part
))
518 (= (aref name-part
(1- (length name-part
))) ?
/))
519 (aset name-part
(1- (length name-part
)) ?
\\))
520 (concat dir-part name-part
))
522 (concat dir-part name-part
))))
524 (defun try-complete-lisp-symbol (old)
525 "Try to complete word as an Emacs Lisp symbol.
526 The argument OLD has to be nil the first call of this function, and t
527 for subsequent calls (for further possible completions of the same
528 string). It returns t if a new completion is found, nil otherwise."
531 (he-init-string (he-lisp-symbol-beg) (point))
532 (if (not (he-string-member he-search-string he-tried-table
))
533 (setq he-tried-table
(cons he-search-string he-tried-table
)))
535 (and (not (equal he-search-string
""))
536 (sort (all-completions he-search-string obarray
537 (function (lambda (sym)
540 (symbol-plist sym
)))))
542 (while (and he-expand-list
543 (he-string-member (car he-expand-list
) he-tried-table
))
544 (setq he-expand-list
(cdr he-expand-list
)))
545 (if (null he-expand-list
)
547 (if old
(he-reset-string))
550 (he-substitute-string (car he-expand-list
))
551 (setq he-expand-list
(cdr he-expand-list
))
554 (defun try-complete-lisp-symbol-partially (old)
555 "Try to complete as an Emacs Lisp symbol, as many characters as unique.
556 The argument OLD has to be nil the first call of this function. It
557 returns t if a unique, possibly partial, completion is found, nil
559 (let ((expansion ()))
562 (he-init-string (he-lisp-symbol-beg) (point))
563 (if (not (string= he-search-string
""))
565 (try-completion he-search-string obarray
566 (function (lambda (sym)
569 (symbol-plist sym
)))))))
570 (if (or (eq expansion t
)
571 (string= expansion he-search-string
)
572 (he-string-member expansion he-tried-table
))
573 (setq expansion
()))))
577 (if old
(he-reset-string))
580 (he-substitute-string expansion
)
583 (defun he-lisp-symbol-beg ()
585 (skip-syntax-backward "w_")
588 (defun try-expand-line (old)
589 "Try to complete the current line to an entire line in the buffer.
590 The argument OLD has to be nil the first call of this function, and t
591 for subsequent calls (for further possible completions of the same
592 string). It returns t if a new completion is found, nil otherwise."
594 (strip-prompt (and (get-buffer-process (current-buffer))
595 comint-use-prompt-regexp
596 comint-prompt-regexp
)))
599 (he-init-string (he-line-beg strip-prompt
) (point))
600 (set-marker he-search-loc he-string-beg
)
601 (setq he-search-bw t
)))
603 (if (not (equal he-search-string
""))
606 (if hippie-expand-no-restriction
608 ;; Try looking backward unless inhibited.
611 (goto-char he-search-loc
)
612 (setq expansion
(he-line-search he-search-string
614 (set-marker he-search-loc
(point))
617 (set-marker he-search-loc he-string-end
)
618 (setq he-search-bw
())))))
620 (if (not expansion
) ; Then look forward.
622 (goto-char he-search-loc
)
623 (setq expansion
(he-line-search he-search-string
625 (set-marker he-search-loc
(point)))))))
629 (if old
(he-reset-string))
632 (he-substitute-string expansion t
)
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."
641 (strip-prompt (and (get-buffer-process (current-buffer))
642 comint-use-prompt-regexp
643 comint-prompt-regexp
))
644 (buf (current-buffer))
645 (orig-case-fold-search case-fold-search
))
648 (he-init-string (he-line-beg strip-prompt
) (point))
649 (setq he-search-bufs
(buffer-list))
650 (setq he-searched-n-bufs
0)
651 (set-marker he-search-loc
1 (car he-search-bufs
))))
653 (if (not (equal he-search-string
""))
654 (while (and he-search-bufs
656 (or (not hippie-expand-max-buffers
)
657 (< he-searched-n-bufs hippie-expand-max-buffers
)))
658 (set-buffer (car he-search-bufs
))
659 (if (and (not (eq (current-buffer) buf
))
660 (if hippie-expand-only-buffers
661 (he-buffer-member hippie-expand-only-buffers
)
662 (not (he-buffer-member hippie-expand-ignore-buffers
))))
665 (if hippie-expand-no-restriction
667 (goto-char he-search-loc
)
668 (setq strip-prompt
(and (get-buffer-process (current-buffer))
669 comint-use-prompt-regexp
670 comint-prompt-regexp
))
672 (let ((case-fold-search orig-case-fold-search
))
673 (he-line-search he-search-string
675 (set-marker he-search-loc
(point))
678 (setq he-search-bufs
(cdr he-search-bufs
))
679 (setq he-searched-n-bufs
(1+ he-searched-n-bufs
))
680 (set-marker he-search-loc
1 (car he-search-bufs
))))))
681 (setq he-search-bufs
(cdr he-search-bufs
))
682 (set-marker he-search-loc
1 (car he-search-bufs
)))))
687 (if old
(he-reset-string))
690 (he-substitute-string expansion t
)
693 (defun he-line-search (str strip-prompt reverse
)
695 (while (and (not result
)
698 (he-line-search-regexp str strip-prompt
)
701 (he-line-search-regexp str strip-prompt
)
703 (setq result
(buffer-substring-no-properties (match-end 1)
705 (if (he-string-member result he-tried-table t
)
706 (setq result nil
))) ; if already in table, ignore
709 (defun he-line-beg (strip-prompt)
711 (if (re-search-backward (he-line-search-regexp "" strip-prompt
)
712 (line-beginning-position) t
)
716 (defun he-line-search-regexp (pat strip-prompt
)
718 (concat "\\(" comint-prompt-regexp
"\\|^\\s-*\\)\\(?2:"
721 (concat "^\\(\\s-*\\)\\("
723 "[^\n]*[^ \t\n]\\)")))
725 (defun try-expand-list (old)
726 "Try to complete the current beginning of a list.
727 The argument OLD has to be nil the first call of this function, and t
728 for subsequent calls (for further possible completions of the same
729 string). It returns t if a new completion is found, nil otherwise."
730 (let ((expansion ()))
733 (he-init-string (he-list-beg) (point))
734 (set-marker he-search-loc he-string-beg
)
735 (setq he-search-bw t
)))
737 (if (not (equal he-search-string
""))
740 (if hippie-expand-no-restriction
742 ;; Try looking backward unless inhibited.
745 (goto-char he-search-loc
)
746 (setq expansion
(he-list-search he-search-string t
))
747 (set-marker he-search-loc
(point))
750 (set-marker he-search-loc he-string-end
)
751 (setq he-search-bw
())))))
753 (if (not expansion
) ; Then look forward.
755 (goto-char he-search-loc
)
756 (setq expansion
(he-list-search he-search-string nil
))
757 (set-marker he-search-loc
(point)))))))
761 (if old
(he-reset-string))
764 (he-substitute-string expansion t
)
767 (defun try-expand-list-all-buffers (old)
768 "Try to complete the current list, searching all other buffers.
769 The argument OLD has to be nil the first call of this function, and t
770 for subsequent calls (for further possible completions of the same
771 string). It returns t if a new completion is found, nil otherwise."
773 (buf (current-buffer))
774 (orig-case-fold-search case-fold-search
))
777 (he-init-string (he-list-beg) (point))
778 (setq he-search-bufs
(buffer-list))
779 (setq he-searched-n-bufs
0)
780 (set-marker he-search-loc
1 (car he-search-bufs
))))
782 (if (not (equal he-search-string
""))
783 (while (and he-search-bufs
785 (or (not hippie-expand-max-buffers
)
786 (< he-searched-n-bufs hippie-expand-max-buffers
)))
787 (set-buffer (car he-search-bufs
))
788 (if (and (not (eq (current-buffer) buf
))
789 (if hippie-expand-only-buffers
790 (he-buffer-member hippie-expand-only-buffers
)
791 (not (he-buffer-member hippie-expand-ignore-buffers
))))
794 (if hippie-expand-no-restriction
796 (goto-char he-search-loc
)
798 (let ((case-fold-search orig-case-fold-search
))
799 (he-list-search he-search-string nil
)))
800 (set-marker he-search-loc
(point))
803 (setq he-search-bufs
(cdr he-search-bufs
))
804 (setq he-searched-n-bufs
(1+ he-searched-n-bufs
))
805 (set-marker he-search-loc
1 (car he-search-bufs
))))))
806 (setq he-search-bufs
(cdr he-search-bufs
))
807 (set-marker he-search-loc
1 (car he-search-bufs
)))))
812 (if old
(he-reset-string))
815 (he-substitute-string expansion t
)
818 (defun he-list-search (str reverse
)
821 (while (and (not result
)
823 (search-backward str nil t
)
824 (search-forward str nil t
)))
826 (setq beg
(match-beginning 0))
831 (error (setq err t
)))
833 (> (point) he-string-beg
))
837 (setq result
(buffer-substring-no-properties beg
(point)))
838 (if (he-string-member result he-tried-table t
)
839 (setq result nil
)))) ; if already in table, ignore
843 (defun he-list-beg ()
850 (defun try-expand-all-abbrevs (old)
851 "Try to expand word before point according to all abbrev tables.
852 The argument OLD has to be nil the first call of this function, and t
853 for subsequent calls (for further possible expansions of the same
854 string). It returns t if a new expansion is found, nil otherwise."
857 (he-init-string (he-dabbrev-beg) (point))
859 (and (not (equal he-search-string
""))
860 (mapcar (function (lambda (sym)
861 (if (and (boundp sym
) (vectorp (eval sym
)))
862 (abbrev-expansion (downcase he-search-string
)
864 (append '(local-abbrev-table
866 abbrev-table-name-list
))))))
867 (while (and he-expand-list
868 (or (not (car he-expand-list
))
869 (he-string-member (car he-expand-list
) he-tried-table t
)))
870 (setq he-expand-list
(cdr he-expand-list
)))
871 (if (null he-expand-list
)
873 (if old
(he-reset-string))
876 (he-substitute-string (car he-expand-list
) t
)
877 (setq he-expand-list
(cdr he-expand-list
))
880 (defun try-expand-dabbrev (old)
881 "Try to expand word \"dynamically\", searching the current buffer.
882 The argument OLD has to be nil the first call of this function, and t
883 for subsequent calls (for further possible expansions of the same
884 string). It returns t if a new expansion is found, nil otherwise."
885 (let ((expansion ()))
888 (he-init-string (he-dabbrev-beg) (point))
889 (set-marker he-search-loc he-string-beg
)
890 (setq he-search-bw t
)))
892 (if (not (equal he-search-string
""))
895 (if hippie-expand-no-restriction
897 ;; Try looking backward unless inhibited.
900 (goto-char he-search-loc
)
901 (setq expansion
(he-dabbrev-search he-search-string t
))
902 (set-marker he-search-loc
(point))
905 (set-marker he-search-loc he-string-end
)
906 (setq he-search-bw
())))))
908 (if (not expansion
) ; Then look forward.
910 (goto-char he-search-loc
)
911 (setq expansion
(he-dabbrev-search he-search-string nil
))
912 (set-marker he-search-loc
(point)))))))
916 (if old
(he-reset-string))
919 (he-substitute-string expansion t
)
922 (defun try-expand-dabbrev-all-buffers (old)
923 "Try to expand word \"dynamically\", searching all other buffers.
924 The argument OLD has to be nil the first call of this function, and t
925 for subsequent calls (for further possible expansions of the same
926 string). It returns t if a new expansion is found, nil otherwise."
928 (buf (current-buffer))
929 (orig-case-fold-search case-fold-search
))
932 (he-init-string (he-dabbrev-beg) (point))
933 (setq he-search-bufs
(buffer-list))
934 (setq he-searched-n-bufs
0)
935 (set-marker he-search-loc
1 (car he-search-bufs
))))
937 (if (not (equal he-search-string
""))
938 (while (and he-search-bufs
940 (or (not hippie-expand-max-buffers
)
941 (< he-searched-n-bufs hippie-expand-max-buffers
)))
942 (set-buffer (car he-search-bufs
))
943 (if (and (not (eq (current-buffer) buf
))
944 (if hippie-expand-only-buffers
945 (he-buffer-member hippie-expand-only-buffers
)
946 (not (he-buffer-member hippie-expand-ignore-buffers
))))
949 (if hippie-expand-no-restriction
951 (goto-char he-search-loc
)
953 (let ((case-fold-search orig-case-fold-search
))
954 (he-dabbrev-search he-search-string nil
)))
955 (set-marker he-search-loc
(point))
958 (setq he-search-bufs
(cdr he-search-bufs
))
959 (setq he-searched-n-bufs
(1+ he-searched-n-bufs
))
960 (set-marker he-search-loc
1 (car he-search-bufs
))))))
961 (setq he-search-bufs
(cdr he-search-bufs
))
962 (set-marker he-search-loc
1 (car he-search-bufs
)))))
967 (if old
(he-reset-string))
970 (he-substitute-string expansion t
)
973 ;; Thanks go to Jeff Dairiki <dairiki@faraday.apl.washington.edu> who
974 ;; suggested this one.
975 (defun try-expand-dabbrev-visible (old)
976 "Try to expand word \"dynamically\", searching visible window parts.
977 The argument OLD has to be nil the first call of this function, and t
978 for subsequent calls (for further possible expansions of the same
979 string). It returns t if a new expansion is found, nil otherwise."
981 (flag (if (frame-visible-p (window-frame))
984 (he-init-string (he-dabbrev-beg) (point))
985 (setq he-search-window
(selected-window))
986 (set-marker he-search-loc
987 (window-start he-search-window
)
988 (window-buffer he-search-window
)))
990 (while (and (not (equal he-search-string
""))
991 (marker-position he-search-loc
)
993 (with-current-buffer (marker-buffer he-search-loc
)
995 (goto-char he-search-loc
)
996 (setq expansion
(he-dabbrev-search he-search-string
()
997 (window-end he-search-window
)))
999 (eq (marker-buffer he-string-beg
) (current-buffer))
1000 (eq (marker-position he-string-beg
) (match-beginning 0)))
1002 (he-dabbrev-search he-search-string
()
1003 (window-end he-search-window
))))
1004 (set-marker he-search-loc
(point) (current-buffer))))
1006 (setq he-search-window
(next-window he-search-window nil flag
))
1007 (if (eq he-search-window
(selected-window))
1008 (set-marker he-search-loc nil
)
1009 (set-marker he-search-loc
(window-start he-search-window
)
1010 (window-buffer he-search-window
)))))
1014 (if old
(he-reset-string))
1017 (he-substitute-string expansion t
)
1020 (defun he-dabbrev-search (pattern &optional reverse limit
)
1022 (regpat (cond ((not hippie-expand-dabbrev-as-symbol
)
1023 (concat "\\<" (regexp-quote pattern
) "\\sw+"))
1024 ((eq (char-syntax (aref pattern
0)) ?_
)
1025 (concat (regexp-quote pattern
) "\\(\\sw\\|\\s_\\)+"))
1027 (concat "\\<" (regexp-quote pattern
)
1028 "\\(\\sw\\|\\s_\\)+")))))
1029 (while (and (not result
)
1031 (re-search-backward regpat limit t
)
1032 (re-search-forward regpat limit t
)))
1033 (setq result
(buffer-substring-no-properties (match-beginning 0)
1035 (if (or (and hippie-expand-dabbrev-as-symbol
1036 (> (match-beginning 0) (point-min))
1037 (memq (char-syntax (char-after (1- (match-beginning 0))))
1039 (he-string-member result he-tried-table t
))
1040 (setq result nil
))) ; ignore if bad prefix or already in table
1043 (defun he-dabbrev-beg ()
1046 (if hippie-expand-dabbrev-skip-space
1047 (skip-syntax-backward ". "))
1048 (if (= (skip-syntax-backward (if hippie-expand-dabbrev-as-symbol
1054 (defun try-expand-dabbrev-from-kill (old)
1055 "Try to expand word \"dynamically\", searching the kill ring.
1056 The argument OLD has to be nil the first call of this function, and t
1057 for subsequent calls (for further possible completions of the same
1058 string). It returns t if a new completion is found, nil otherwise."
1059 (let ((expansion ()))
1062 (he-init-string (he-dabbrev-beg) (point))
1063 (setq he-expand-list
1064 (if (not (equal he-search-string
""))
1066 (setq he-search-loc2
0)))
1067 (if (not (equal he-search-string
""))
1068 (setq expansion
(he-dabbrev-kill-search he-search-string
)))
1071 (if old
(he-reset-string))
1074 (he-substitute-string expansion t
)
1077 (defun he-dabbrev-kill-search (pattern)
1079 (regpat (cond ((not hippie-expand-dabbrev-as-symbol
)
1080 (concat "\\<" (regexp-quote pattern
) "\\sw+"))
1081 ((eq (char-syntax (aref pattern
0)) ?_
)
1082 (concat (regexp-quote pattern
) "\\(\\sw\\|\\s_\\)+"))
1084 (concat "\\<" (regexp-quote pattern
)
1085 "\\(\\sw\\|\\s_\\)+"))))
1086 (killstr (car he-expand-list
)))
1087 (while (and (not result
)
1089 (while (and (not result
)
1090 (string-match regpat killstr he-search-loc2
))
1091 (setq result
(substring killstr
(match-beginning 0) (match-end 0)))
1092 (set-text-properties 0 (length result
) () result
)
1093 (setq he-search-loc2
(1+ (match-beginning 0)))
1094 (if (or (and hippie-expand-dabbrev-as-symbol
1095 (> (match-beginning 0) 0)
1096 (memq (char-syntax (aref killstr
(1- (match-beginning 0))))
1098 (he-string-member result he-tried-table t
))
1099 (setq result nil
))) ; ignore if bad prefix or already in table
1100 (if (and (not result
)
1103 (setq he-expand-list
(cdr he-expand-list
))
1104 (setq killstr
(car he-expand-list
))
1105 (setq he-search-loc2
0))))
1108 (defun try-expand-whole-kill (old)
1109 "Try to complete text with something from the kill ring.
1110 The argument OLD has to be nil the first call of this function, and t
1111 for subsequent calls (for further possible completions of the same
1112 string). It returns t if a new completion is found, nil otherwise."
1113 (let ((expansion ()))
1116 (he-init-string (he-kill-beg) (point))
1117 (if (not (he-string-member he-search-string he-tried-table
))
1118 (setq he-tried-table
(cons he-search-string he-tried-table
)))
1119 (setq he-expand-list
1120 (if (not (equal he-search-string
""))
1122 (setq he-search-loc2
())))
1123 (if (not (equal he-search-string
""))
1124 (setq expansion
(he-whole-kill-search he-search-string
)))
1127 (if old
(he-reset-string))
1130 (he-substitute-string expansion
)
1133 (defun he-whole-kill-search (str)
1134 (let ((case-fold-search ())
1136 (str (regexp-quote str
))
1137 (killstr (car he-expand-list
))
1139 (while (and (not result
)
1141 (if (not he-search-loc2
)
1142 (while (setq pos
(string-match str killstr
(1+ pos
)))
1143 (setq he-search-loc2
(cons pos he-search-loc2
))))
1144 (while (and (not result
)
1146 (setq pos
(car he-search-loc2
))
1147 (setq he-search-loc2
(cdr he-search-loc2
))
1149 (goto-char he-string-beg
)
1150 (if (and (>= (- (point) pos
) (point-min)) ; avoid some string GC
1151 (eq (char-after (- (point) pos
)) (aref killstr
0))
1152 (search-backward (substring killstr
0 pos
)
1155 (setq result
(substring killstr pos
))
1156 (set-text-properties 0 (length result
) () result
))))
1158 (he-string-member result he-tried-table
))
1159 (setq result nil
))) ; ignore if already in table
1160 (if (and (not result
)
1163 (setq he-expand-list
(cdr he-expand-list
))
1164 (setq killstr
(car he-expand-list
))
1168 (defun he-kill-beg ()
1171 (skip-syntax-backward "^w_")
1172 (if (= (skip-syntax-backward "w_") 0)
1177 (provide 'hippie-exp
)
1179 ;;; hippie-exp.el ends here