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