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