1 ;;; hippie-exp.el --- expand text trying various ways to find its expansion
3 ;; Copyright (C) 1992, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
4 ;; 2009, 2010 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 of the License, or
16 ;; (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>.
28 ;; `hippie-expand' is a single function for a lot of different kinds
29 ;; of completions and expansions. Called repeatedly it tries all
30 ;; possible completions in succession.
31 ;; Which kinds of completions to try, and in which order, is
32 ;; determined by the contents of `hippie-expand-try-functions-list'.
33 ;; Much customization of `hippie-expand' can be made by changing the
34 ;; order of, removing, or inserting new functions in this list.
35 ;; Given a positive numeric argument, `hippie-expand' jumps directly
36 ;; ARG functions forward in this list. Given some other argument
37 ;; (a negative argument or just Ctrl-U) it undoes the tried
40 ;; If the variable `hippie-expand-verbose' is non-nil, `hippie-expand'
41 ;; outputs in a message which try-function in the list that is used
42 ;; currently (ie. was used currently and will be tried first the next
44 ;; The variable `hippie-expand-max-buffers' determines in how many
45 ;; buffers, apart from the current, to search for expansions in. It
46 ;; is used by the try-functions named "-all-buffers".
47 ;; The variable `hippie-expand-ignore-buffers' is a list of regexps
48 ;; matching buffer names (as strings) or major modes (as atoms) of
49 ;; buffers that should not be searched by the try-functions named
51 ;; If set, the variable `hippie-expand-only-buffers' does the opposite
52 ;; of `hippie-expand-ignore-buffers', in that the search is restricted
53 ;; to only the kind of buffers listed.
54 ;; If the variable `hippie-expand-no-restriction' is non-nil, narrowed
55 ;; buffers are widened before they are searched.
56 ;; The variable `hippie-expand-dabbrev-skip-space' controls whether
57 ;; trailing spaces will be included in the abbreviation to search for,
58 ;; which then gives the same behavior as the original `dabbrev-expand'.
59 ;; The variable `hippie-expand-dabbrev-as-symbol' controls whether
60 ;; characters of syntax '_' is considered part of the words to expand
62 ;; See also the macro `make-hippie-expand-function' below.
64 ;; A short description of the current try-functions in this file:
65 ;; `try-complete-file-name' : very convenient to have in any buffer,
66 ;; and not just in the minibuffer or (some) shell-mode. It goes
67 ;; through all possible completions instead of just completing as
69 ;; `try-complete-file-name-partially' : To insert in the list just
70 ;; before `try-complete-file-name' for those who want first to get
71 ;; a file name completed only as many characters as is unique.
72 ;; `try-expand-all-abbrevs' : can be removed if you don't use abbrevs.
73 ;; Otherwise it looks through all abbrev-tables, starting with
74 ;; the local followed by the global.
75 ;; `try-expand-line' : Searches the buffer for an entire line that
76 ;; begins exactly as the current line. Convenient sometimes, for
77 ;; example as a substitute for (or complement to) the history
78 ;; list in shell-like buffers. At other times, only confusing.
79 ;; `try-expand-line-all-buffers' : Like `try-expand-line' but searches
80 ;; in all buffers (except the current). (This may be a little
81 ;; slow, don't use it unless you are really fond of `hippie-expand'.)
82 ;; `try-expand-list' : Tries to expand the text back to the nearest
83 ;; open delimiter, to a whole list from the buffer. Convenient for
84 ;; example when writing Lisp or TeX.
85 ;; `try-expand-list-all-buffers' : Like `try-expand-list' but searches
86 ;; in all buffers (except the current).
87 ;; `try-expand-dabbrev' : works exactly as dabbrev-expand (but of
88 ;; course in a way compatible with the other try-functions).
89 ;; `try-expand-dabbrev-all-buffers' : perhaps the most useful of them,
90 ;; like `dabbrev-expand' but searches all Emacs buffers (except the
91 ;; current) for matching words. (No, I don't find this one
92 ;; particularly slow.)
93 ;; `try-expand-dabbrev-visible': Searches the currently visible parts of
94 ;; all windows. Can be put before `try-expand-dabbrev-all-buffers' to
95 ;; first try the expansions you can see.
96 ;; `try-expand-dabbrev-from-kill': Searches the kill ring for a suitable
97 ;; completion of the word. Good to have, just in case the word was not
99 ;; `try-expand-whole-kill' : Tries to complete text with a whole entry
100 ;; from the kill ring. May be good if you don't know how far up in
101 ;; the kill-ring the required entry is, and don't want to mess with
102 ;; "Choose Next Paste".
103 ;; `try-complete-lisp-symbol' : like `lisp-complete-symbol', but goes
104 ;; through all possibilities instead of completing what is unique.
105 ;; Might be tedious (usually a lot of possible completions) and
106 ;; since its function is much like `lisp-complete-symbol', which
107 ;; already has a key of its own, you might want to remove this.
108 ;; `try-complete-lisp-symbol-partially' : To insert in the list just
109 ;; before `try-complete-lisp-symbol' for those who first want to get
110 ;; completion of what is unique in the name.
112 ;; Not all of the above functions are by default in
113 ;; `hippie-expand-try-functions-list'. This variable is better set
114 ;; in ".emacs" to make `hippie-expand' behave maximally convenient
115 ;; according to personal taste. Also, instead of loading the
116 ;; variable with all kinds of try-functions above, it might be an
117 ;; idea to use `make-hippie-expand-function' to construct different
118 ;; `hippie-expand'-like functions, with different try-lists and bound
119 ;; to different keys. It is also possible to make
120 ;; `hippie-expand-try-functions-list' a buffer local variable, and
121 ;; let it depend on the mode (by setting it in the mode-hooks).
123 ;; To write new try-functions, consider the following:
124 ;; Each try-function takes one argument OLD which is nil the first
125 ;; time the function is called and true in succeeding calls for the
126 ;; same string to complete. The first time the function has to
127 ;; extract the string before point to complete, and substitute the
128 ;; first completion alternative for it. On following calls it has to
129 ;; substitute the next possible completion for the last tried string.
130 ;; The try-function is to return t as long as it finds new
131 ;; possible completions. When there are no more alternatives it has
132 ;; to restore the text before point to its original contents, and
133 ;; return nil (don't beep or message or anything).
134 ;; The try-function can (should) use the following functions:
135 ;; `he-init-string' : Initializes the text to substitute to the
136 ;; contents of the region BEGIN to END. Also sets the variable
137 ;; `he-search-string' to the text to expand.
138 ;; `he-substitute-string' : substitutes STR into the region
139 ;; initialized with `he-init-string'. (An optional second argument
140 ;; TRANS-CASE non-nil, means transfer of case from the abbreviation
141 ;; to the expansion is ok if that is enabled in the buffer.)
142 ;; `he-reset-string' : Resets the initialized region to its original
144 ;; There is also a variable: `he-tried-table' which is meant to contain
145 ;; all tried expansions so far. The try-function can check this
146 ;; variable to see whether an expansion has already been tried
147 ;; (hint: `he-string-member').
151 ;; It may happen that some completion suggestion occurs twice, in
152 ;; spite of the use of `he-tried-table' to prevent that. This is
153 ;; because different try-functions may try to complete different
154 ;; lengths of text, and thus put different amounts of the
155 ;; text in `he-tried-table'. Anyway this seems to occur seldom enough
156 ;; not to be too disturbing. Also it should NOT be possible for the
157 ;; opposite situation to occur, that `hippie-expand' misses some
158 ;; suggestion because it thinks it has already tried it.
162 ;; I want to thank Mikael Djurfeldt in discussions with whom the idea
163 ;; of this function took form.
164 ;; I am also grateful to all those who have given me suggestions on
165 ;; how to improve it, and all those who helped to find and remove bugs.
172 (defgroup hippie-expand nil
173 "Expand text trying various ways to find its expansion."
174 :link
'(custom-manual "(autotype)Hippie Expand")
175 :link
'(emacs-commentary-link "hippie-exp")
181 (defvar he-string-beg
(make-marker))
183 (defvar he-string-end
(make-marker))
185 (defvar he-search-string
())
187 (defvar he-expand-list
())
189 (defvar he-tried-table
())
191 (defvar he-search-loc
(make-marker))
193 (defvar he-search-loc2
())
195 (defvar he-search-bw
())
197 (defvar he-search-bufs
())
199 (defvar he-searched-n-bufs
())
201 (defvar he-search-window
())
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
)
222 (defcustom hippie-expand-verbose t
223 "Non-nil makes `hippie-expand' output which function it is trying."
225 :group
'hippie-expand
)
228 (defcustom hippie-expand-dabbrev-skip-space nil
229 "Non-nil means tolerate trailing spaces in the abbreviation to expand."
230 :group
'hippie-expand
234 (defcustom hippie-expand-dabbrev-as-symbol t
235 "Non-nil means expand as symbols, i.e. syntax `_' is considered a letter."
236 :group
'hippie-expand
240 (defcustom hippie-expand-no-restriction t
241 "Non-nil means that narrowed buffers are widened during search."
242 :group
'hippie-expand
246 (defcustom hippie-expand-max-buffers
()
247 "The maximum number of buffers (apart from the current) searched.
248 If nil, all buffers are searched."
249 :type
'(choice (const :tag
"All" nil
)
251 :group
'hippie-expand
)
254 (defcustom hippie-expand-ignore-buffers
(list (purecopy "^ \\*.*\\*$") 'dired-mode
)
255 "A list specifying which buffers not to search (if not current).
256 Can contain both regexps matching buffer names (as strings) and major modes
258 :type
'(repeat (choice regexp
(symbol :tag
"Major Mode")))
259 :group
'hippie-expand
)
262 (defcustom hippie-expand-only-buffers
()
263 "A list specifying the only buffers to search (in addition to current).
264 Can contain both regexps matching buffer names (as strings) and major modes
265 \(as atoms). If non-nil, this variable overrides the variable
266 `hippie-expand-ignore-buffers'."
267 :type
'(repeat (choice regexp
(symbol :tag
"Major Mode")))
268 :group
'hippie-expand
)
271 (defun hippie-expand (arg)
272 "Try to expand text before point, using multiple methods.
273 The expansion functions in `hippie-expand-try-functions-list' are
274 tried in order, until a possible expansion is found. Repeated
275 application of `hippie-expand' inserts successively possible
277 With a positive numeric argument, jumps directly to the ARG next
278 function in this list. With a negative argument or just \\[universal-argument],
279 undoes the expansion."
282 (and (integerp arg
) (> arg
0)))
283 (let ((first (or (= he-num -
1)
284 (not (equal this-command last-command
)))))
288 (setq he-tried-table nil
)))
290 (if (not first
) (he-reset-string))
292 (let ((i (max (+ he-num arg
) 0)))
293 (while (not (or (>= i
(length hippie-expand-try-functions-list
))
294 (apply (nth i hippie-expand-try-functions-list
)
295 (list (= he-num i
)))))
298 (if (>= he-num
(length hippie-expand-try-functions-list
))
302 (message "No expansion found")
303 (message "No further expansions found"))
305 (if (and hippie-expand-verbose
306 (not (window-minibuffer-p (selected-window))))
308 (nth he-num hippie-expand-try-functions-list
)))))
309 (if (and (>= he-num
0)
310 (eq (marker-buffer he-string-beg
) (current-buffer)))
314 (if (and hippie-expand-verbose
315 (not (window-minibuffer-p (selected-window))))
316 (message "Undoing expansions"))))))
318 ;; Initializes the region to expand (to between BEG and END).
319 (defun he-init-string (beg end
)
320 (set-marker he-string-beg beg
)
321 (set-marker he-string-end end
)
322 (setq he-search-string
(buffer-substring-no-properties beg end
)))
324 ;; Resets the expanded region to its original contents.
325 (defun he-reset-string ()
326 (let ((newpos (point-marker)))
327 (goto-char he-string-beg
)
328 (insert he-search-string
)
329 (delete-region (point) he-string-end
)
332 ;; Substitutes an expansion STR into the correct region (the region
333 ;; initialized with `he-init-string').
334 ;; An optional argument TRANS-CASE means that it is ok to transfer case
335 ;; from the abbreviation to the expansion if that is possible, and is
336 ;; enabled in the buffer.
337 (defun he-substitute-string (str &optional trans-case
)
338 (let ((trans-case (and trans-case
341 (newpos (point-marker))
343 (goto-char he-string-beg
)
344 (setq subst
(if trans-case
(he-transfer-case he-search-string str
) str
))
345 (setq he-tried-table
(cons subst he-tried-table
))
347 (delete-region (point) he-string-end
)
350 (defun he-capitalize-first (str)
352 (if (string-match "\\Sw*\\(\\sw\\).*" str
)
353 (let ((res (downcase str
))
354 (no (match-beginning 1)))
355 (aset res no
(upcase (aref str no
)))
359 (defun he-ordinary-case-p (str)
360 (or (string= str
(downcase str
))
361 (string= str
(upcase str
))
362 (string= str
(capitalize str
))
363 (string= str
(he-capitalize-first str
))))
365 (defun he-transfer-case (from-str to-str
)
366 (cond ((string= from-str
(substring to-str
0 (min (length from-str
)
369 ((not (he-ordinary-case-p to-str
))
371 ((string= from-str
(downcase from-str
))
373 ((string= from-str
(upcase from-str
))
375 ((string= from-str
(he-capitalize-first from-str
))
376 (he-capitalize-first to-str
))
377 ((string= from-str
(capitalize from-str
))
383 ;; Check if STR is a member of LST.
384 ;; Transform to the final case if optional TRANS-CASE is non-nil.
385 (defun he-string-member (str lst
&optional trans-case
)
387 (member (if (and trans-case
390 (he-transfer-case he-search-string str
)
394 ;; Check if current buffer matches any atom or regexp in LST.
395 ;; Atoms are interpreted as major modes, strings as regexps mathing the name.
396 (defun he-buffer-member (lst)
397 (or (memq major-mode lst
)
400 (or (not (stringp (car lst
)))
401 (not (string-match (car lst
) (buffer-name)))))
402 (setq lst
(cdr lst
)))
405 ;; For the real hippie-expand enthusiast: A macro that makes it
406 ;; possible to use many functions like hippie-expand, but with
407 ;; different try-functions-lists.
408 ;; Usage is for example:
409 ;; (fset 'my-complete-file (make-hippie-expand-function
410 ;; '(try-complete-file-name-partially
411 ;; try-complete-file-name)))
412 ;; (fset 'my-complete-line (make-hippie-expand-function
414 ;; try-expand-line-all-buffers)))
417 (defmacro make-hippie-expand-function
(try-list &optional verbose
)
418 "Construct a function similar to `hippie-expand'.
419 Make it use the expansion functions in TRY-LIST. An optional second
420 argument VERBOSE non-nil makes the function verbose."
421 `(function (lambda (arg)
423 "Try to expand text before point, using the following functions: \n"
424 (mapconcat 'prin1-to-string
(eval try-list
) ", "))
426 (let ((hippie-expand-try-functions-list ,try-list
)
427 (hippie-expand-verbose ,verbose
))
428 (hippie-expand arg
)))))
431 ;;; Here follows the try-functions and their requisites:
434 (defun try-complete-file-name (old)
435 "Try to complete text as a file name.
436 The argument OLD has to be nil the first call of this function, and t
437 for subsequent calls (for further possible completions of the same
438 string). It returns t if a new completion is found, nil otherwise."
441 (he-init-string (he-file-name-beg) (point))
442 (let ((name-part (file-name-nondirectory he-search-string
))
443 (dir-part (expand-file-name (or (file-name-directory
444 he-search-string
) ""))))
445 (if (not (he-string-member name-part he-tried-table
))
446 (setq he-tried-table
(cons name-part he-tried-table
)))
447 (if (and (not (equal he-search-string
""))
448 (file-directory-p dir-part
))
449 (setq he-expand-list
(sort (file-name-all-completions
453 (setq he-expand-list
())))))
455 (while (and he-expand-list
456 (he-string-member (car he-expand-list
) he-tried-table
))
457 (setq he-expand-list
(cdr he-expand-list
)))
458 (if (null he-expand-list
)
460 (if old
(he-reset-string))
462 (let ((filename (he-concat-directory-file-name
463 (file-name-directory he-search-string
)
464 (car he-expand-list
))))
465 (he-substitute-string filename
)
466 (setq he-tried-table
(cons (car he-expand-list
) (cdr he-tried-table
)))
467 (setq he-expand-list
(cdr he-expand-list
))
470 (defun try-complete-file-name-partially (old)
471 "Try to complete text as a file name, as many characters as unique.
472 The argument OLD has to be nil the first call of this function. It
473 returns t if a unique, possibly partial, completion is found, nil
475 (let ((expansion ()))
478 (he-init-string (he-file-name-beg) (point))
479 (let ((name-part (file-name-nondirectory he-search-string
))
480 (dir-part (expand-file-name (or (file-name-directory
481 he-search-string
) ""))))
482 (if (and (not (equal he-search-string
""))
483 (file-directory-p dir-part
))
484 (setq expansion
(file-name-completion name-part
486 (if (or (eq expansion t
)
487 (string= expansion name-part
)
488 (he-string-member expansion he-tried-table
))
489 (setq expansion
())))))
493 (if old
(he-reset-string))
495 (let ((filename (he-concat-directory-file-name
496 (file-name-directory he-search-string
)
498 (he-substitute-string filename
)
499 (setq he-tried-table
(cons expansion
(cdr he-tried-table
)))
502 (defvar he-file-name-chars
503 (cond ((memq system-type
'(ms-dos windows-nt cygwin
))
504 "-a-zA-Z0-9_/.,~^#$+=:\\\\")
505 (t ;; More strange file formats ?
506 "-a-zA-Z0-9_/.,~^#$+="))
507 "Characters that are considered part of the file name to expand.")
509 (defun he-file-name-beg ()
512 (skip-chars-backward he-file-name-chars
)
513 (if (> (skip-syntax-backward "w") 0) ;; No words with non-file chars
517 ;; Thanks go to David Hughes <ukchugd@ukpmr.cs.philips.nl> who
518 ;; helped to make it work on PC.
519 (defun he-concat-directory-file-name (dir-part name-part
)
520 "Try to slam together two parts of a file specification, system dependently."
521 (cond ((null dir-part
) name-part
)
522 ((eq system-type
'ms-dos
)
523 (if (and (string-match "\\\\" dir-part
)
524 (not (string-match "/" dir-part
))
525 (= (aref name-part
(1- (length name-part
))) ?
/))
526 (aset name-part
(1- (length name-part
)) ?
\\))
527 (concat dir-part name-part
))
529 (concat dir-part name-part
))))
531 (defun try-complete-lisp-symbol (old)
532 "Try to complete word as an Emacs Lisp symbol.
533 The argument OLD has to be nil the first call of this function, and t
534 for subsequent calls (for further possible completions of the same
535 string). It returns t if a new completion is found, nil otherwise."
538 (he-init-string (he-lisp-symbol-beg) (point))
539 (if (not (he-string-member he-search-string he-tried-table
))
540 (setq he-tried-table
(cons he-search-string he-tried-table
)))
542 (and (not (equal he-search-string
""))
543 (sort (all-completions he-search-string obarray
544 (function (lambda (sym)
547 (symbol-plist sym
)))))
549 (while (and he-expand-list
550 (he-string-member (car he-expand-list
) he-tried-table
))
551 (setq he-expand-list
(cdr he-expand-list
)))
552 (if (null he-expand-list
)
554 (if old
(he-reset-string))
557 (he-substitute-string (car he-expand-list
))
558 (setq he-expand-list
(cdr he-expand-list
))
561 (defun try-complete-lisp-symbol-partially (old)
562 "Try to complete as an Emacs Lisp symbol, as many characters as unique.
563 The argument OLD has to be nil the first call of this function. It
564 returns t if a unique, possibly partial, completion is found, nil
566 (let ((expansion ()))
569 (he-init-string (he-lisp-symbol-beg) (point))
570 (if (not (string= he-search-string
""))
572 (try-completion he-search-string obarray
573 (function (lambda (sym)
576 (symbol-plist sym
)))))))
577 (if (or (eq expansion t
)
578 (string= expansion he-search-string
)
579 (he-string-member expansion he-tried-table
))
580 (setq expansion
()))))
584 (if old
(he-reset-string))
587 (he-substitute-string expansion
)
590 (defun he-lisp-symbol-beg ()
592 (skip-syntax-backward "w_")
595 (defun try-expand-line (old)
596 "Try to complete the current line to an entire line in the buffer.
597 The argument OLD has to be nil the first call of this function, and t
598 for subsequent calls (for further possible completions of the same
599 string). It returns t if a new completion is found, nil otherwise."
601 (strip-prompt (and (get-buffer-process (current-buffer))
602 comint-use-prompt-regexp
603 comint-prompt-regexp
)))
606 (he-init-string (he-line-beg strip-prompt
) (point))
607 (set-marker he-search-loc he-string-beg
)
608 (setq he-search-bw t
)))
610 (if (not (equal he-search-string
""))
613 (if hippie-expand-no-restriction
615 ;; Try looking backward unless inhibited.
618 (goto-char he-search-loc
)
619 (setq expansion
(he-line-search he-search-string
621 (set-marker he-search-loc
(point))
624 (set-marker he-search-loc he-string-end
)
625 (setq he-search-bw
())))))
627 (if (not expansion
) ; Then look forward.
629 (goto-char he-search-loc
)
630 (setq expansion
(he-line-search he-search-string
632 (set-marker he-search-loc
(point)))))))
636 (if old
(he-reset-string))
639 (he-substitute-string expansion t
)
642 (defun try-expand-line-all-buffers (old)
643 "Try to complete the current line, searching all other buffers.
644 The argument OLD has to be nil the first call of this function, and t
645 for subsequent calls (for further possible completions of the same
646 string). It returns t if a new completion is found, nil otherwise."
648 (strip-prompt (and (get-buffer-process (current-buffer))
649 comint-use-prompt-regexp
650 comint-prompt-regexp
))
651 (buf (current-buffer))
652 (orig-case-fold-search case-fold-search
))
655 (he-init-string (he-line-beg strip-prompt
) (point))
656 (setq he-search-bufs
(buffer-list))
657 (setq he-searched-n-bufs
0)
658 (set-marker he-search-loc
1 (car he-search-bufs
))))
660 (if (not (equal he-search-string
""))
661 (while (and he-search-bufs
663 (or (not hippie-expand-max-buffers
)
664 (< he-searched-n-bufs hippie-expand-max-buffers
)))
665 (set-buffer (car he-search-bufs
))
666 (if (and (not (eq (current-buffer) buf
))
667 (if hippie-expand-only-buffers
668 (he-buffer-member hippie-expand-only-buffers
)
669 (not (he-buffer-member hippie-expand-ignore-buffers
))))
672 (if hippie-expand-no-restriction
674 (goto-char he-search-loc
)
675 (setq strip-prompt
(and (get-buffer-process (current-buffer))
676 comint-use-prompt-regexp
677 comint-prompt-regexp
))
679 (let ((case-fold-search orig-case-fold-search
))
680 (he-line-search he-search-string
682 (set-marker he-search-loc
(point))
685 (setq he-search-bufs
(cdr he-search-bufs
))
686 (setq he-searched-n-bufs
(1+ he-searched-n-bufs
))
687 (set-marker he-search-loc
1 (car he-search-bufs
))))))
688 (setq he-search-bufs
(cdr he-search-bufs
))
689 (set-marker he-search-loc
1 (car he-search-bufs
)))))
694 (if old
(he-reset-string))
697 (he-substitute-string expansion t
)
700 (defun he-line-search (str strip-prompt reverse
)
702 (while (and (not result
)
705 (he-line-search-regexp str strip-prompt
)
708 (he-line-search-regexp str strip-prompt
)
710 (setq result
(buffer-substring-no-properties (match-end 1)
712 (if (he-string-member result he-tried-table t
)
713 (setq result nil
))) ; if already in table, ignore
716 (defun he-line-beg (strip-prompt)
718 (if (re-search-backward (he-line-search-regexp "" strip-prompt
)
719 (line-beginning-position) t
)
723 (defun he-line-search-regexp (pat strip-prompt
)
725 (concat "\\(" comint-prompt-regexp
"\\|^\\s-*\\)\\("
728 (concat "^\\(\\s-*\\)\\("
730 "[^\n]*[^ \t\n]\\)")))
732 (defun try-expand-list (old)
733 "Try to complete the current beginning of a list.
734 The argument OLD has to be nil the first call of this function, and t
735 for subsequent calls (for further possible completions of the same
736 string). It returns t if a new completion is found, nil otherwise."
737 (let ((expansion ()))
740 (he-init-string (he-list-beg) (point))
741 (set-marker he-search-loc he-string-beg
)
742 (setq he-search-bw t
)))
744 (if (not (equal he-search-string
""))
747 (if hippie-expand-no-restriction
749 ;; Try looking backward unless inhibited.
752 (goto-char he-search-loc
)
753 (setq expansion
(he-list-search he-search-string t
))
754 (set-marker he-search-loc
(point))
757 (set-marker he-search-loc he-string-end
)
758 (setq he-search-bw
())))))
760 (if (not expansion
) ; Then look forward.
762 (goto-char he-search-loc
)
763 (setq expansion
(he-list-search he-search-string nil
))
764 (set-marker he-search-loc
(point)))))))
768 (if old
(he-reset-string))
771 (he-substitute-string expansion t
)
774 (defun try-expand-list-all-buffers (old)
775 "Try to complete the current list, searching all other buffers.
776 The argument OLD has to be nil the first call of this function, and t
777 for subsequent calls (for further possible completions of the same
778 string). It returns t if a new completion is found, nil otherwise."
780 (buf (current-buffer))
781 (orig-case-fold-search case-fold-search
))
784 (he-init-string (he-list-beg) (point))
785 (setq he-search-bufs
(buffer-list))
786 (setq he-searched-n-bufs
0)
787 (set-marker he-search-loc
1 (car he-search-bufs
))))
789 (if (not (equal he-search-string
""))
790 (while (and he-search-bufs
792 (or (not hippie-expand-max-buffers
)
793 (< he-searched-n-bufs hippie-expand-max-buffers
)))
794 (set-buffer (car he-search-bufs
))
795 (if (and (not (eq (current-buffer) buf
))
796 (if hippie-expand-only-buffers
797 (he-buffer-member hippie-expand-only-buffers
)
798 (not (he-buffer-member hippie-expand-ignore-buffers
))))
801 (if hippie-expand-no-restriction
803 (goto-char he-search-loc
)
805 (let ((case-fold-search orig-case-fold-search
))
806 (he-list-search he-search-string nil
)))
807 (set-marker he-search-loc
(point))
810 (setq he-search-bufs
(cdr he-search-bufs
))
811 (setq he-searched-n-bufs
(1+ he-searched-n-bufs
))
812 (set-marker he-search-loc
1 (car he-search-bufs
))))))
813 (setq he-search-bufs
(cdr he-search-bufs
))
814 (set-marker he-search-loc
1 (car he-search-bufs
)))))
819 (if old
(he-reset-string))
822 (he-substitute-string expansion t
)
825 (defun he-list-search (str reverse
)
828 (while (and (not result
)
830 (search-backward str nil t
)
831 (search-forward str nil t
)))
833 (setq beg
(match-beginning 0))
838 (error (setq err t
)))
840 (> (point) he-string-beg
))
844 (setq result
(buffer-substring-no-properties beg
(point)))
845 (if (he-string-member result he-tried-table t
)
846 (setq result nil
)))) ; if already in table, ignore
850 (defun he-list-beg ()
857 (defun try-expand-all-abbrevs (old)
858 "Try to expand word before point according to all abbrev tables.
859 The argument OLD has to be nil the first call of this function, and t
860 for subsequent calls (for further possible expansions of the same
861 string). It returns t if a new expansion is found, nil otherwise."
864 (he-init-string (he-dabbrev-beg) (point))
866 (and (not (equal he-search-string
""))
867 (mapcar (function (lambda (sym)
868 (if (and (boundp sym
) (vectorp (eval sym
)))
869 (abbrev-expansion (downcase he-search-string
)
871 (append '(local-abbrev-table
873 abbrev-table-name-list
))))))
874 (while (and he-expand-list
875 (or (not (car he-expand-list
))
876 (he-string-member (car he-expand-list
) he-tried-table t
)))
877 (setq he-expand-list
(cdr he-expand-list
)))
878 (if (null he-expand-list
)
880 (if old
(he-reset-string))
883 (he-substitute-string (car he-expand-list
) t
)
884 (setq he-expand-list
(cdr he-expand-list
))
887 (defun try-expand-dabbrev (old)
888 "Try to expand word \"dynamically\", searching the current buffer.
889 The argument OLD has to be nil the first call of this function, and t
890 for subsequent calls (for further possible expansions of the same
891 string). It returns t if a new expansion is found, nil otherwise."
892 (let ((expansion ()))
895 (he-init-string (he-dabbrev-beg) (point))
896 (set-marker he-search-loc he-string-beg
)
897 (setq he-search-bw t
)))
899 (if (not (equal he-search-string
""))
902 (if hippie-expand-no-restriction
904 ;; Try looking backward unless inhibited.
907 (goto-char he-search-loc
)
908 (setq expansion
(he-dabbrev-search he-search-string t
))
909 (set-marker he-search-loc
(point))
912 (set-marker he-search-loc he-string-end
)
913 (setq he-search-bw
())))))
915 (if (not expansion
) ; Then look forward.
917 (goto-char he-search-loc
)
918 (setq expansion
(he-dabbrev-search he-search-string nil
))
919 (set-marker he-search-loc
(point)))))))
923 (if old
(he-reset-string))
926 (he-substitute-string expansion t
)
929 (defun try-expand-dabbrev-all-buffers (old)
930 "Try to expand word \"dynamically\", searching all other buffers.
931 The argument OLD has to be nil the first call of this function, and t
932 for subsequent calls (for further possible expansions of the same
933 string). It returns t if a new expansion is found, nil otherwise."
935 (buf (current-buffer))
936 (orig-case-fold-search case-fold-search
))
939 (he-init-string (he-dabbrev-beg) (point))
940 (setq he-search-bufs
(buffer-list))
941 (setq he-searched-n-bufs
0)
942 (set-marker he-search-loc
1 (car he-search-bufs
))))
944 (if (not (equal he-search-string
""))
945 (while (and he-search-bufs
947 (or (not hippie-expand-max-buffers
)
948 (< he-searched-n-bufs hippie-expand-max-buffers
)))
949 (set-buffer (car he-search-bufs
))
950 (if (and (not (eq (current-buffer) buf
))
951 (if hippie-expand-only-buffers
952 (he-buffer-member hippie-expand-only-buffers
)
953 (not (he-buffer-member hippie-expand-ignore-buffers
))))
956 (if hippie-expand-no-restriction
958 (goto-char he-search-loc
)
960 (let ((case-fold-search orig-case-fold-search
))
961 (he-dabbrev-search he-search-string nil
)))
962 (set-marker he-search-loc
(point))
965 (setq he-search-bufs
(cdr he-search-bufs
))
966 (setq he-searched-n-bufs
(1+ he-searched-n-bufs
))
967 (set-marker he-search-loc
1 (car he-search-bufs
))))))
968 (setq he-search-bufs
(cdr he-search-bufs
))
969 (set-marker he-search-loc
1 (car he-search-bufs
)))))
974 (if old
(he-reset-string))
977 (he-substitute-string expansion t
)
980 ;; Thanks go to Jeff Dairiki <dairiki@faraday.apl.washington.edu> who
981 ;; suggested this one.
982 (defun try-expand-dabbrev-visible (old)
983 "Try to expand word \"dynamically\", searching visible window parts.
984 The argument OLD has to be nil the first call of this function, and t
985 for subsequent calls (for further possible expansions of the same
986 string). It returns t if a new expansion is found, nil otherwise."
988 (flag (if (frame-visible-p (window-frame (selected-window)))
991 (he-init-string (he-dabbrev-beg) (point))
992 (setq he-search-window
(selected-window))
993 (set-marker he-search-loc
994 (window-start he-search-window
)
995 (window-buffer he-search-window
)))
997 (while (and (not (equal he-search-string
""))
998 (marker-position he-search-loc
)
1000 (with-current-buffer (marker-buffer he-search-loc
)
1002 (goto-char he-search-loc
)
1003 (setq expansion
(he-dabbrev-search he-search-string
()
1004 (window-end he-search-window
)))
1006 (eq (marker-buffer he-string-beg
) (current-buffer))
1007 (eq (marker-position he-string-beg
) (match-beginning 0)))
1009 (he-dabbrev-search he-search-string
()
1010 (window-end he-search-window
))))
1011 (set-marker he-search-loc
(point) (current-buffer))))
1013 (setq he-search-window
(next-window he-search-window nil flag
))
1014 (if (eq he-search-window
(selected-window))
1015 (set-marker he-search-loc nil
)
1016 (set-marker he-search-loc
(window-start he-search-window
)
1017 (window-buffer he-search-window
)))))
1021 (if old
(he-reset-string))
1024 (he-substitute-string expansion t
)
1027 (defun he-dabbrev-search (pattern &optional reverse limit
)
1029 (regpat (cond ((not hippie-expand-dabbrev-as-symbol
)
1030 (concat "\\<" (regexp-quote pattern
) "\\sw+"))
1031 ((eq (char-syntax (aref pattern
0)) ?_
)
1032 (concat (regexp-quote pattern
) "\\(\\sw\\|\\s_\\)+"))
1034 (concat "\\<" (regexp-quote pattern
)
1035 "\\(\\sw\\|\\s_\\)+")))))
1036 (while (and (not result
)
1038 (re-search-backward regpat limit t
)
1039 (re-search-forward regpat limit t
)))
1040 (setq result
(buffer-substring-no-properties (match-beginning 0)
1042 (if (or (and hippie-expand-dabbrev-as-symbol
1043 (> (match-beginning 0) (point-min))
1044 (memq (char-syntax (char-after (1- (match-beginning 0))))
1046 (he-string-member result he-tried-table t
))
1047 (setq result nil
))) ; ignore if bad prefix or already in table
1050 (defun he-dabbrev-beg ()
1053 (if hippie-expand-dabbrev-skip-space
1054 (skip-syntax-backward ". "))
1055 (if (= (skip-syntax-backward (if hippie-expand-dabbrev-as-symbol
1061 (defun try-expand-dabbrev-from-kill (old)
1062 "Try to expand word \"dynamically\", searching the kill ring.
1063 The argument OLD has to be nil the first call of this function, and t
1064 for subsequent calls (for further possible completions of the same
1065 string). It returns t if a new completion is found, nil otherwise."
1066 (let ((expansion ()))
1069 (he-init-string (he-dabbrev-beg) (point))
1070 (setq he-expand-list
1071 (if (not (equal he-search-string
""))
1073 (setq he-search-loc2
0)))
1074 (if (not (equal he-search-string
""))
1075 (setq expansion
(he-dabbrev-kill-search he-search-string
)))
1078 (if old
(he-reset-string))
1081 (he-substitute-string expansion t
)
1084 (defun he-dabbrev-kill-search (pattern)
1086 (regpat (cond ((not hippie-expand-dabbrev-as-symbol
)
1087 (concat "\\<" (regexp-quote pattern
) "\\sw+"))
1088 ((eq (char-syntax (aref pattern
0)) ?_
)
1089 (concat (regexp-quote pattern
) "\\(\\sw\\|\\s_\\)+"))
1091 (concat "\\<" (regexp-quote pattern
)
1092 "\\(\\sw\\|\\s_\\)+"))))
1093 (killstr (car he-expand-list
)))
1094 (while (and (not result
)
1096 (while (and (not result
)
1097 (string-match regpat killstr he-search-loc2
))
1098 (setq result
(substring killstr
(match-beginning 0) (match-end 0)))
1099 (set-text-properties 0 (length result
) () result
)
1100 (setq he-search-loc2
(1+ (match-beginning 0)))
1101 (if (or (and hippie-expand-dabbrev-as-symbol
1102 (> (match-beginning 0) 0)
1103 (memq (char-syntax (aref killstr
(1- (match-beginning 0))))
1105 (he-string-member result he-tried-table t
))
1106 (setq result nil
))) ; ignore if bad prefix or already in table
1107 (if (and (not result
)
1110 (setq he-expand-list
(cdr he-expand-list
))
1111 (setq killstr
(car he-expand-list
))
1112 (setq he-search-loc2
0))))
1115 (defun try-expand-whole-kill (old)
1116 "Try to complete text with something from the kill ring.
1117 The argument OLD has to be nil the first call of this function, and t
1118 for subsequent calls (for further possible completions of the same
1119 string). It returns t if a new completion is found, nil otherwise."
1120 (let ((expansion ()))
1123 (he-init-string (he-kill-beg) (point))
1124 (if (not (he-string-member he-search-string he-tried-table
))
1125 (setq he-tried-table
(cons he-search-string he-tried-table
)))
1126 (setq he-expand-list
1127 (if (not (equal he-search-string
""))
1129 (setq he-search-loc2
())))
1130 (if (not (equal he-search-string
""))
1131 (setq expansion
(he-whole-kill-search he-search-string
)))
1134 (if old
(he-reset-string))
1137 (he-substitute-string expansion
)
1140 (defun he-whole-kill-search (str)
1141 (let ((case-fold-search ())
1143 (str (regexp-quote str
))
1144 (killstr (car he-expand-list
))
1146 (while (and (not result
)
1148 (if (not he-search-loc2
)
1149 (while (setq pos
(string-match str killstr
(1+ pos
)))
1150 (setq he-search-loc2
(cons pos he-search-loc2
))))
1151 (while (and (not result
)
1153 (setq pos
(car he-search-loc2
))
1154 (setq he-search-loc2
(cdr he-search-loc2
))
1156 (goto-char he-string-beg
)
1157 (if (and (>= (- (point) pos
) (point-min)) ; avoid some string GC
1158 (eq (char-after (- (point) pos
)) (aref killstr
0))
1159 (search-backward (substring killstr
0 pos
)
1162 (setq result
(substring killstr pos
))
1163 (set-text-properties 0 (length result
) () result
))))
1165 (he-string-member result he-tried-table
))
1166 (setq result nil
))) ; ignore if already in table
1167 (if (and (not result
)
1170 (setq he-expand-list
(cdr he-expand-list
))
1171 (setq killstr
(car he-expand-list
))
1175 (defun he-kill-beg ()
1178 (skip-syntax-backward "^w_")
1179 (if (= (skip-syntax-backward "w_") 0)
1184 (provide 'hippie-exp
)
1186 ;;; hippie-exp.el ends here