Commit | Line | Data |
---|---|---|
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> |
de073e06 KH |
6 | ;; Last change: 28 May 1997 |
7 | ;; Version: 1.5 | |
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 | 159 | |
94114394 RS |
160 | (defgroup hippie-expand nil |
161 | "Expand text trying various ways to find its expansion." | |
162 | :group 'abbrev) | |
163 | ||
86414c3c RS |
164 | (defcustom he-dabbrev-skip-space nil |
165 | "Non-nil means tolerate trailing spaces in the abbreviation to expand." | |
1707b2da | 166 | :group 'hippie-expand |
86414c3c RS |
167 | :type 'boolean) |
168 | ||
169 | (defcustom he-dabbrev-as-symbol t | |
170 | "Non-nil means expand as symbols, i.e. syntax `_' is considered a letter." | |
1707b2da | 171 | :group 'hippie-expand |
86414c3c RS |
172 | :type 'boolean) |
173 | ||
652304c9 RS |
174 | (defvar he-num -1) |
175 | ||
8c6677ed | 176 | (defvar he-string-beg (make-marker)) |
652304c9 | 177 | |
8c6677ed | 178 | (defvar he-string-end (make-marker)) |
652304c9 RS |
179 | |
180 | (defvar he-search-string ()) | |
181 | ||
182 | (defvar he-expand-list ()) | |
183 | ||
184 | (defvar he-tried-table ()) | |
185 | ||
8c6677ed | 186 | (defvar he-search-loc (make-marker)) |
652304c9 | 187 | |
510cbc92 RS |
188 | (defvar he-search-loc2 ()) |
189 | ||
652304c9 RS |
190 | (defvar he-search-bw ()) |
191 | ||
192 | (defvar he-search-bufs ()) | |
193 | ||
8c6677ed JB |
194 | (defvar he-searched-n-bufs ()) |
195 | ||
510cbc92 RS |
196 | (defvar he-search-window ()) |
197 | ||
8c6677ed | 198 | ;;;###autoload |
510cbc92 RS |
199 | (defvar hippie-expand-try-functions-list '(try-complete-file-name-partially |
200 | try-complete-file-name | |
652304c9 | 201 | try-expand-all-abbrevs |
510cbc92 | 202 | try-expand-list |
652304c9 RS |
203 | try-expand-line |
204 | try-expand-dabbrev | |
205 | try-expand-dabbrev-all-buffers | |
510cbc92 RS |
206 | try-expand-dabbrev-from-kill |
207 | try-complete-lisp-symbol-partially | |
652304c9 RS |
208 | try-complete-lisp-symbol) |
209 | "The list of expansion functions tried in order by `hippie-expand'. | |
210 | To change the behavior of `hippie-expand', remove, change the order of, | |
211 | or insert functions in this list.") | |
212 | ||
8c6677ed | 213 | ;;;###autoload |
94114394 RS |
214 | (defcustom hippie-expand-verbose t |
215 | "*Non-nil makes `hippie-expand' output which function it is trying." | |
216 | :type 'boolean | |
217 | :group 'hippie-expand) | |
652304c9 | 218 | |
8c6677ed | 219 | ;;;###autoload |
94114394 | 220 | (defcustom hippie-expand-max-buffers () |
8c6677ed | 221 | "*The maximum number of buffers (apart from the current) searched. |
94114394 RS |
222 | If nil, all buffers are searched." |
223 | :type '(choice (const :tag "All" nil) | |
224 | integer) | |
225 | :group 'hippie-expand) | |
8c6677ed | 226 | |
d1d1ddbd | 227 | ;;;###autoload |
94114394 | 228 | (defcustom hippie-expand-ignore-buffers '("^ \\*.*\\*$" dired-mode) |
d1d1ddbd RS |
229 | "*A list specifying which buffers not to search (if not current). |
230 | Can contain both regexps matching buffer names (as strings) and major modes | |
94114394 RS |
231 | \(as atoms)" |
232 | :type '(repeat (choice regexp (symbol :tag "Major Mode"))) | |
233 | :group 'hippie-expand) | |
d1d1ddbd | 234 | |
8c6677ed | 235 | ;;;###autoload |
652304c9 RS |
236 | (defun hippie-expand (arg) |
237 | "Try to expand text before point, using multiple methods. | |
238 | The expansion functions in `hippie-expand-try-functions-list' are | |
239 | tried in order, until a possible expansion is found. Repeated | |
240 | application of `hippie-expand' inserts successively possible | |
241 | expansions. | |
242 | With a positive numeric argument, jumps directly to the ARG next | |
243 | function in this list. With a negative argument or just \\[universal-argument], | |
244 | undoes the expansion." | |
245 | (interactive "P") | |
246 | (if (or (not arg) | |
247 | (and (integerp arg) (> arg 0))) | |
248 | (let ((first (or (= he-num -1) | |
249 | (not (equal this-command last-command))))) | |
250 | (if first | |
251 | (progn | |
252 | (setq he-num -1) | |
253 | (setq he-tried-table nil))) | |
254 | (if arg | |
255 | (if (not first) (he-reset-string)) | |
256 | (setq arg 0)) | |
257 | (let ((i (max (+ he-num arg) 0))) | |
258 | (while (not (or (>= i (length hippie-expand-try-functions-list)) | |
259 | (apply (nth i hippie-expand-try-functions-list) | |
260 | (list (= he-num i))))) | |
261 | (setq i (1+ i))) | |
262 | (setq he-num i)) | |
263 | (if (>= he-num (length hippie-expand-try-functions-list)) | |
264 | (progn | |
265 | (setq he-num -1) | |
266 | (if first | |
267 | (message "No expansion found") | |
268 | (message "No further expansions found")) | |
269 | (ding)) | |
8c6677ed | 270 | (if (and hippie-expand-verbose |
510cbc92 | 271 | (not (window-minibuffer-p (selected-window)))) |
32561aba | 272 | (message "Using %s" |
de073e06 | 273 | (nth he-num hippie-expand-try-functions-list))))) |
510cbc92 RS |
274 | (if (and (>= he-num 0) |
275 | (eq (marker-buffer he-string-beg) (current-buffer))) | |
652304c9 RS |
276 | (progn |
277 | (setq he-num -1) | |
278 | (he-reset-string) | |
8c6677ed | 279 | (if (and hippie-expand-verbose |
510cbc92 | 280 | (not (window-minibuffer-p (selected-window)))) |
652304c9 | 281 | (message "Undoing expansions")))))) |
8c6677ed | 282 | |
652304c9 RS |
283 | ;; Initializes the region to expand (to between BEG and END). |
284 | (defun he-init-string (beg end) | |
8c6677ed JB |
285 | (set-marker he-string-beg beg) |
286 | (set-marker he-string-end end) | |
652304c9 RS |
287 | (setq he-search-string (buffer-substring beg end))) |
288 | ||
289 | ;; Resets the expanded region to its original contents. | |
290 | (defun he-reset-string () | |
8c6677ed | 291 | (let ((newpos (point-marker))) |
8c6677ed JB |
292 | (goto-char he-string-beg) |
293 | (insert he-search-string) | |
510cbc92 RS |
294 | (delete-region (point) he-string-end) |
295 | (goto-char newpos))) | |
652304c9 RS |
296 | |
297 | ;; Substitutes an expansion STR into the correct region (the region | |
298 | ;; initialized with `he-init-string'). | |
299 | ;; An optional argument TRANS-CASE means that it is ok to transfer case | |
300 | ;; from the abbreviation to the expansion if that is possible, and is | |
301 | ;; enabled in the buffer. | |
302 | (defun he-substitute-string (str &optional trans-case) | |
303 | (let ((trans-case (and trans-case | |
304 | case-replace | |
510cbc92 RS |
305 | case-fold-search)) |
306 | (newpos (point-marker)) | |
307 | (subst ())) | |
652304c9 | 308 | (goto-char he-string-beg) |
510cbc92 RS |
309 | (setq subst (if trans-case (he-transfer-case he-search-string str) str)) |
310 | (setq he-tried-table (cons subst he-tried-table)) | |
311 | (insert subst) | |
312 | (delete-region (point) he-string-end) | |
313 | (goto-char newpos))) | |
314 | ||
315 | (defun he-capitalize-first (str) | |
316 | (save-match-data | |
317 | (if (string-match "\\Sw*\\(\\sw\\).*" str) | |
318 | (let ((res (downcase str)) | |
319 | (no (match-beginning 1))) | |
320 | (aset res no (upcase (aref str no))) | |
321 | res) | |
322 | str))) | |
652304c9 RS |
323 | |
324 | (defun he-ordinary-case-p (str) | |
325 | (or (string= str (downcase str)) | |
326 | (string= str (upcase str)) | |
510cbc92 RS |
327 | (string= str (capitalize str)) |
328 | (string= str (he-capitalize-first str)))) | |
329 | ||
330 | (defun he-transfer-case (from-str to-str) | |
331 | (cond ((string= from-str (substring to-str 0 (min (length from-str) | |
332 | (length to-str)))) | |
333 | to-str) | |
334 | ((not (he-ordinary-case-p to-str)) | |
8d392c8e | 335 | to-str) |
510cbc92 RS |
336 | ((string= from-str (downcase from-str)) |
337 | (downcase to-str)) | |
338 | ((string= from-str (upcase from-str)) | |
339 | (upcase to-str)) | |
340 | ((string= from-str (he-capitalize-first from-str)) | |
341 | (he-capitalize-first to-str)) | |
342 | ((string= from-str (capitalize from-str)) | |
343 | (capitalize to-str)) | |
344 | (t | |
345 | to-str))) | |
346 | ||
652304c9 RS |
347 | |
348 | ;; Check if STR is a member of LST. | |
510cbc92 RS |
349 | ;; Transform to the final case if optional TRANS-CASE is non-NIL. |
350 | (defun he-string-member (str lst &optional trans-case) | |
351 | (if str | |
352 | (member (if (and trans-case | |
353 | case-replace | |
354 | case-fold-search) | |
355 | (he-transfer-case he-search-string str) | |
356 | str) | |
357 | lst))) | |
652304c9 | 358 | |
d1d1ddbd RS |
359 | ;; Check if STR matches any regexp in LST. |
360 | ;; Ignore possible non-strings in LST. | |
361 | (defun he-regexp-member (str lst) | |
362 | (while (and lst | |
363 | (or (not (stringp (car lst))) | |
510cbc92 | 364 | (not (string-match (car lst) str)))) |
d1d1ddbd RS |
365 | (setq lst (cdr lst))) |
366 | lst) | |
367 | ||
652304c9 RS |
368 | ;; For the real hippie-expand enthusiast: A macro that makes it |
369 | ;; possible to use many functions like hippie-expand, but with | |
370 | ;; different try-functions-lists. | |
371 | ;; Usage is for example: | |
372 | ;; (fset 'my-complete-file (make-hippie-expand-function | |
373 | ;; '(try-complete-file-name-partially | |
374 | ;; try-complete-file-name))) | |
375 | ;; (fset 'my-complete-line (make-hippie-expand-function | |
376 | ;; '(try-expand-line | |
377 | ;; try-expand-line-all-buffers))) | |
378 | ;; | |
8c6677ed | 379 | ;;;###autoload |
652304c9 RS |
380 | (defmacro make-hippie-expand-function (try-list &optional verbose) |
381 | "Construct a function similar to `hippie-expand'. | |
382 | Make it use the expansion functions in TRY-LIST. An optional second | |
383 | argument VERBOSE non-nil makes the function verbose." | |
8c6677ed | 384 | (` (function (lambda (arg) |
652304c9 | 385 | (, (concat |
510cbc92 | 386 | "Try to expand text before point, using the following functions: \n" |
652304c9 RS |
387 | (mapconcat 'prin1-to-string (eval try-list) ", "))) |
388 | (interactive "P") | |
389 | (let ((hippie-expand-try-functions-list (, try-list)) | |
390 | (hippie-expand-verbose (, verbose))) | |
8c6677ed | 391 | (hippie-expand arg)))))) |
652304c9 RS |
392 | |
393 | ||
394 | ;;; Here follows the try-functions and their requisites: | |
395 | ||
510cbc92 | 396 | |
652304c9 RS |
397 | (defun try-complete-file-name (old) |
398 | "Try to complete text as a file name. | |
399 | The argument OLD has to be nil the first call of this function, and t | |
400 | for subsequent calls (for further possible completions of the same | |
401 | string). It returns t if a new completion is found, nil otherwise." | |
402 | (if (not old) | |
403 | (progn | |
404 | (he-init-string (he-file-name-beg) (point)) | |
510cbc92 RS |
405 | (let ((name-part (he-file-name-nondirectory he-search-string)) |
406 | (dir-part (expand-file-name (or (he-file-name-directory | |
652304c9 RS |
407 | he-search-string) "")))) |
408 | (if (not (he-string-member name-part he-tried-table)) | |
409 | (setq he-tried-table (cons name-part he-tried-table))) | |
410 | (if (and (not (equal he-search-string "")) | |
510cbc92 | 411 | (he-file-directory-p dir-part)) |
652304c9 RS |
412 | (setq he-expand-list (sort (file-name-all-completions |
413 | name-part | |
414 | dir-part) | |
415 | 'string-lessp)) | |
416 | (setq he-expand-list ()))))) | |
417 | ||
418 | (while (and he-expand-list | |
419 | (he-string-member (car he-expand-list) he-tried-table)) | |
420 | (setq he-expand-list (cdr he-expand-list))) | |
421 | (if (null he-expand-list) | |
422 | (progn | |
510cbc92 | 423 | (if old (he-reset-string)) |
652304c9 | 424 | ()) |
510cbc92 RS |
425 | (let ((filename (he-concat-directory-file-name |
426 | (he-file-name-directory he-search-string) | |
427 | (car he-expand-list)))) | |
652304c9 | 428 | (he-substitute-string filename) |
510cbc92 | 429 | (setq he-tried-table (cons (car he-expand-list) (cdr he-tried-table))) |
652304c9 RS |
430 | (setq he-expand-list (cdr he-expand-list)) |
431 | t))) | |
432 | ||
433 | (defun try-complete-file-name-partially (old) | |
434 | "Try to complete text as a file name, as many characters as unique. | |
435 | The argument OLD has to be nil the first call of this function. It | |
436 | returns t if a unique, possibly partial, completion is found, nil | |
437 | otherwise." | |
438 | (let ((expansion ())) | |
439 | (if (not old) | |
440 | (progn | |
441 | (he-init-string (he-file-name-beg) (point)) | |
510cbc92 RS |
442 | (let ((name-part (he-file-name-nondirectory he-search-string)) |
443 | (dir-part (expand-file-name (or (he-file-name-directory | |
652304c9 RS |
444 | he-search-string) "")))) |
445 | (if (and (not (equal he-search-string "")) | |
510cbc92 | 446 | (he-file-directory-p dir-part)) |
652304c9 RS |
447 | (setq expansion (file-name-completion name-part |
448 | dir-part))) | |
449 | (if (or (eq expansion t) | |
510cbc92 RS |
450 | (string= expansion name-part) |
451 | (he-string-member expansion he-tried-table)) | |
652304c9 RS |
452 | (setq expansion ()))))) |
453 | ||
454 | (if (not expansion) | |
455 | (progn | |
510cbc92 | 456 | (if old (he-reset-string)) |
652304c9 | 457 | ()) |
510cbc92 RS |
458 | (let ((filename (he-concat-directory-file-name |
459 | (he-file-name-directory he-search-string) | |
460 | expansion))) | |
652304c9 | 461 | (he-substitute-string filename) |
510cbc92 | 462 | (setq he-tried-table (cons expansion (cdr he-tried-table))) |
652304c9 RS |
463 | t)))) |
464 | ||
510cbc92 RS |
465 | (defvar he-file-name-chars |
466 | (cond ((memq system-type '(vax-vms axp-vms)) | |
467 | "-a-zA-Z0-9_/.,~^#$+=:\\[\\]") | |
8a5a6d3e | 468 | ((memq system-type '(ms-dos windows-nt)) |
510cbc92 RS |
469 | "-a-zA-Z0-9_/.,~^#$+=:\\\\") |
470 | (t ;; More strange file formats ? | |
471 | "-a-zA-Z0-9_/.,~^#$+=")) | |
472 | "Characters that are considered part of the file name to expand.") | |
473 | ||
652304c9 | 474 | (defun he-file-name-beg () |
de073e06 KH |
475 | (let ((op (point))) |
476 | (save-excursion | |
477 | (skip-chars-backward he-file-name-chars) | |
478 | (if (> (skip-syntax-backward "w") 0) ;; No words with non-file chars | |
479 | op | |
480 | (point))))) | |
652304c9 | 481 | |
510cbc92 RS |
482 | ;; Thanks go to Richard Levitte <levitte@e.kth.se> who helped to make these |
483 | ;; work under VMS, and to David Hughes <ukchugd@ukpmr.cs.philips.nl> who | |
484 | ;; helped to make it work on PC. | |
485 | (defun he-file-name-nondirectory (file) | |
486 | "Fix to make `file-name-nondirectory' work for hippie-expand under VMS." | |
487 | (if (memq system-type '(axp-vms vax-vms)) | |
488 | (let ((n (file-name-nondirectory file))) | |
489 | (if (string-match "^\\(\\[.*\\)\\.\\([^\\.]*\\)$" n) | |
490 | (concat "[." (substring n (match-beginning 2) (match-end 2))) | |
491 | n)) | |
492 | (file-name-nondirectory file))) | |
493 | ||
494 | (defun he-file-name-directory (file) | |
495 | "Fix to make `file-name-directory' work for hippie-expand under VMS." | |
496 | (if (memq system-type '(axp-vms vax-vms)) | |
497 | (let ((n (file-name-nondirectory file)) | |
498 | (d (file-name-directory file))) | |
499 | (if (string-match "^\\(\\[.*\\)\\.\\([^\\.]*\\)$" n) | |
500 | (concat d (substring n (match-beginning 1) (match-end 1)) "]") | |
501 | d)) | |
502 | (file-name-directory file))) | |
503 | ||
504 | (defun he-file-directory-p (file) | |
505 | "Fix to make `file-directory-p' work for hippie-expand under VMS." | |
506 | (if (memq system-type '(vax-vms axp-vms)) | |
507 | (or (file-directory-p file) | |
508 | (file-directory-p (concat file "[000000]"))) | |
a0341d13 | 509 | (file-directory-p file))) |
510cbc92 RS |
510 | |
511 | (defun he-concat-directory-file-name (dir-part name-part) | |
512 | "Try to slam together two parts of a file specification, system dependently." | |
a0341d13 RS |
513 | (cond ((null dir-part) name-part) |
514 | ((memq system-type '(axp-vms vax-vms)) | |
510cbc92 RS |
515 | (if (and (string= (substring dir-part -1) "]") |
516 | (string= (substring name-part 0 2) "[.")) | |
517 | (concat (substring dir-part 0 -1) (substring name-part 1)) | |
518 | (concat dir-part name-part))) | |
b86b9918 | 519 | ((memq system-type '(ms-dos w32)) |
510cbc92 RS |
520 | (if (and (string-match "\\\\" dir-part) |
521 | (not (string-match "/" dir-part)) | |
522 | (= (aref name-part (1- (length name-part))) ?/)) | |
523 | (aset name-part (1- (length name-part)) ?\\)) | |
524 | (concat dir-part name-part)) | |
525 | (t | |
526 | (concat dir-part name-part)))) | |
527 | ||
652304c9 RS |
528 | (defun try-complete-lisp-symbol (old) |
529 | "Try to complete word as an Emacs Lisp symbol. | |
530 | The argument OLD has to be nil the first call of this function, and t | |
531 | for subsequent calls (for further possible completions of the same | |
532 | string). It returns t if a new completion is found, nil otherwise." | |
533 | (if (not old) | |
534 | (progn | |
535 | (he-init-string (he-lisp-symbol-beg) (point)) | |
536 | (if (not (he-string-member he-search-string he-tried-table)) | |
537 | (setq he-tried-table (cons he-search-string he-tried-table))) | |
538 | (setq he-expand-list | |
539 | (and (not (equal he-search-string "")) | |
540 | (sort (all-completions he-search-string obarray | |
541 | (function (lambda (sym) | |
542 | (or (boundp sym) | |
543 | (fboundp sym) | |
544 | (symbol-plist sym))))) | |
545 | 'string-lessp))))) | |
546 | (while (and he-expand-list | |
547 | (he-string-member (car he-expand-list) he-tried-table)) | |
548 | (setq he-expand-list (cdr he-expand-list))) | |
549 | (if (null he-expand-list) | |
550 | (progn | |
510cbc92 | 551 | (if old (he-reset-string)) |
652304c9 RS |
552 | ()) |
553 | (progn | |
554 | (he-substitute-string (car he-expand-list)) | |
652304c9 RS |
555 | (setq he-expand-list (cdr he-expand-list)) |
556 | t))) | |
557 | ||
558 | (defun try-complete-lisp-symbol-partially (old) | |
559 | "Try to complete as an Emacs Lisp symbol, as many characters as unique. | |
560 | The argument OLD has to be nil the first call of this function. It | |
561 | returns t if a unique, possibly partial, completion is found, nil | |
562 | otherwise." | |
563 | (let ((expansion ())) | |
564 | (if (not old) | |
565 | (progn | |
566 | (he-init-string (he-lisp-symbol-beg) (point)) | |
567 | (if (not (string= he-search-string "")) | |
568 | (setq expansion | |
569 | (try-completion he-search-string obarray | |
570 | (function (lambda (sym) | |
571 | (or (boundp sym) | |
572 | (fboundp sym) | |
573 | (symbol-plist sym))))))) | |
574 | (if (or (eq expansion t) | |
510cbc92 RS |
575 | (string= expansion he-search-string) |
576 | (he-string-member expansion he-tried-table)) | |
652304c9 RS |
577 | (setq expansion ())))) |
578 | ||
579 | (if (not expansion) | |
580 | (progn | |
510cbc92 | 581 | (if old (he-reset-string)) |
652304c9 RS |
582 | ()) |
583 | (progn | |
584 | (he-substitute-string expansion) | |
652304c9 RS |
585 | t)))) |
586 | ||
587 | (defun he-lisp-symbol-beg () | |
de073e06 KH |
588 | (save-excursion |
589 | (skip-syntax-backward "w_") | |
590 | (point))) | |
652304c9 RS |
591 | |
592 | (defun try-expand-line (old) | |
593 | "Try to complete the current line to an entire line in the buffer. | |
594 | The argument OLD has to be nil the first call of this function, and t | |
595 | for subsequent calls (for further possible completions of the same | |
596 | string). It returns t if a new completion is found, nil otherwise." | |
597 | (let ((expansion ()) | |
598 | (strip-prompt (and (get-buffer-process (current-buffer)) | |
8c6677ed | 599 | comint-prompt-regexp))) |
652304c9 RS |
600 | (if (not old) |
601 | (progn | |
602 | (he-init-string (he-line-beg strip-prompt) (point)) | |
8c6677ed | 603 | (set-marker he-search-loc he-string-beg) |
652304c9 RS |
604 | (setq he-search-bw t))) |
605 | ||
606 | (if (not (equal he-search-string "")) | |
607 | (save-excursion | |
608 | ;; Try looking backward unless inhibited. | |
609 | (if he-search-bw | |
610 | (progn | |
611 | (goto-char he-search-loc) | |
612 | (setq expansion (he-line-search he-search-string | |
613 | strip-prompt t)) | |
8c6677ed | 614 | (set-marker he-search-loc (point)) |
652304c9 RS |
615 | (if (not expansion) |
616 | (progn | |
8c6677ed | 617 | (set-marker he-search-loc he-string-end) |
652304c9 RS |
618 | (setq he-search-bw ()))))) |
619 | ||
620 | (if (not expansion) ; Then look forward. | |
621 | (progn | |
622 | (goto-char he-search-loc) | |
623 | (setq expansion (he-line-search he-search-string | |
624 | strip-prompt nil)) | |
8c6677ed | 625 | (set-marker he-search-loc (point)))))) |
652304c9 RS |
626 | |
627 | (if (not expansion) | |
628 | (progn | |
510cbc92 | 629 | (if old (he-reset-string)) |
652304c9 RS |
630 | ()) |
631 | (progn | |
632 | (he-substitute-string expansion t) | |
652304c9 RS |
633 | t)))) |
634 | ||
635 | (defun try-expand-line-all-buffers (old) | |
636 | "Try to complete the current line, searching all other buffers. | |
637 | The argument OLD has to be nil the first call of this function, and t | |
638 | for subsequent calls (for further possible completions of the same | |
639 | string). It returns t if a new completion is found, nil otherwise." | |
640 | (let ((expansion ()) | |
641 | (strip-prompt (and (get-buffer-process (current-buffer)) | |
8c6677ed | 642 | comint-prompt-regexp)) |
510cbc92 RS |
643 | (buf (current-buffer)) |
644 | (orig-case-fold-search case-fold-search)) | |
652304c9 RS |
645 | (if (not old) |
646 | (progn | |
647 | (he-init-string (he-line-beg strip-prompt) (point)) | |
8c6677ed | 648 | (setq he-search-bufs (buffer-list)) |
510cbc92 | 649 | (setq he-searched-n-bufs 0) |
8c6677ed | 650 | (set-marker he-search-loc 1 (car he-search-bufs)))) |
652304c9 RS |
651 | |
652 | (if (not (equal he-search-string "")) | |
8c6677ed | 653 | (while (and he-search-bufs |
510cbc92 RS |
654 | (not expansion) |
655 | (or (not hippie-expand-max-buffers) | |
656 | (< he-searched-n-bufs hippie-expand-max-buffers))) | |
652304c9 RS |
657 | (set-buffer (car he-search-bufs)) |
658 | (if (and (not (eq (current-buffer) buf)) | |
d1d1ddbd | 659 | (not (memq major-mode hippie-expand-ignore-buffers)) |
510cbc92 RS |
660 | (not (he-regexp-member (buffer-name) |
661 | hippie-expand-ignore-buffers))) | |
652304c9 RS |
662 | (save-excursion |
663 | (goto-char he-search-loc) | |
510cbc92 RS |
664 | (setq strip-prompt (and (get-buffer-process (current-buffer)) |
665 | comint-prompt-regexp)) | |
666 | (setq expansion (let ((case-fold-search orig-case-fold-search)) | |
667 | (he-line-search he-search-string | |
668 | strip-prompt nil))) | |
8c6677ed | 669 | (set-marker he-search-loc (point)) |
510cbc92 RS |
670 | (if (not expansion) |
671 | (progn | |
672 | (setq he-search-bufs (cdr he-search-bufs)) | |
673 | (setq he-searched-n-bufs (1+ he-searched-n-bufs)) | |
674 | (set-marker he-search-loc 1 (car he-search-bufs))))) | |
675 | (setq he-search-bufs (cdr he-search-bufs)) | |
676 | (set-marker he-search-loc 1 (car he-search-bufs))))) | |
652304c9 RS |
677 | |
678 | (set-buffer buf) | |
679 | (if (not expansion) | |
680 | (progn | |
510cbc92 | 681 | (if old (he-reset-string)) |
652304c9 RS |
682 | ()) |
683 | (progn | |
684 | (he-substitute-string expansion t) | |
685 | t)))) | |
686 | ||
687 | (defun he-line-search (str strip-prompt reverse) | |
688 | (let ((result ())) | |
689 | (while (and (not result) | |
690 | (if reverse | |
691 | (re-search-backward | |
692 | (he-line-search-regexp str strip-prompt) | |
693 | nil t) | |
694 | (re-search-forward | |
695 | (he-line-search-regexp str strip-prompt) | |
696 | nil t))) | |
697 | (setq result (buffer-substring (match-beginning 2) (match-end 2))) | |
510cbc92 RS |
698 | (if (he-string-member result he-tried-table t) |
699 | (setq result nil))) ; if already in table, ignore | |
652304c9 RS |
700 | result)) |
701 | ||
702 | (defun he-line-beg (strip-prompt) | |
703 | (save-excursion | |
652304c9 RS |
704 | (if (re-search-backward (he-line-search-regexp "" strip-prompt) |
705 | (save-excursion (beginning-of-line) | |
706 | (point)) t) | |
707 | (match-beginning 2) | |
652304c9 RS |
708 | (point)))) |
709 | ||
710 | (defun he-line-search-regexp (pat strip-prompt) | |
711 | (if strip-prompt | |
8c6677ed | 712 | (concat "\\(" comint-prompt-regexp "\\|^\\s-*\\)\\(" |
652304c9 RS |
713 | (regexp-quote pat) |
714 | "[^\n]*[^ \t\n]\\)") | |
715 | (concat "^\\(\\s-*\\)\\(" | |
716 | (regexp-quote pat) | |
717 | "[^\n]*[^ \t\n]\\)"))) | |
718 | ||
8c6677ed JB |
719 | (defun try-expand-list (old) |
720 | "Try to complete the current beginning of a list. | |
721 | The argument OLD has to be nil the first call of this function, and t | |
722 | for subsequent calls (for further possible completions of the same | |
723 | string). It returns t if a new completion is found, nil otherwise." | |
724 | (let ((expansion ())) | |
725 | (if (not old) | |
726 | (progn | |
727 | (he-init-string (he-list-beg) (point)) | |
728 | (set-marker he-search-loc he-string-beg) | |
729 | (setq he-search-bw t))) | |
730 | ||
731 | (if (not (equal he-search-string "")) | |
732 | (save-excursion | |
733 | ;; Try looking backward unless inhibited. | |
734 | (if he-search-bw | |
735 | (progn | |
736 | (goto-char he-search-loc) | |
737 | (setq expansion (he-list-search he-search-string t)) | |
738 | (set-marker he-search-loc (point)) | |
739 | (if (not expansion) | |
740 | (progn | |
741 | (set-marker he-search-loc he-string-end) | |
742 | (setq he-search-bw ()))))) | |
743 | ||
744 | (if (not expansion) ; Then look forward. | |
745 | (progn | |
746 | (goto-char he-search-loc) | |
747 | (setq expansion (he-list-search he-search-string nil)) | |
748 | (set-marker he-search-loc (point)))))) | |
749 | ||
750 | (if (not expansion) | |
751 | (progn | |
752 | (if old (he-reset-string)) | |
753 | ()) | |
754 | (progn | |
755 | (he-substitute-string expansion t) | |
8c6677ed JB |
756 | t)))) |
757 | ||
758 | (defun try-expand-list-all-buffers (old) | |
759 | "Try to complete the current list, searching all other buffers. | |
760 | The argument OLD has to be nil the first call of this function, and t | |
761 | for subsequent calls (for further possible completions of the same | |
762 | string). It returns t if a new completion is found, nil otherwise." | |
763 | (let ((expansion ()) | |
510cbc92 RS |
764 | (buf (current-buffer)) |
765 | (orig-case-fold-search case-fold-search)) | |
8c6677ed JB |
766 | (if (not old) |
767 | (progn | |
768 | (he-init-string (he-list-beg) (point)) | |
769 | (setq he-search-bufs (buffer-list)) | |
510cbc92 | 770 | (setq he-searched-n-bufs 0) |
8c6677ed JB |
771 | (set-marker he-search-loc 1 (car he-search-bufs)))) |
772 | ||
773 | (if (not (equal he-search-string "")) | |
774 | (while (and he-search-bufs | |
510cbc92 RS |
775 | (not expansion) |
776 | (or (not hippie-expand-max-buffers) | |
777 | (< he-searched-n-bufs hippie-expand-max-buffers))) | |
8c6677ed JB |
778 | (set-buffer (car he-search-bufs)) |
779 | (if (and (not (eq (current-buffer) buf)) | |
d1d1ddbd | 780 | (not (memq major-mode hippie-expand-ignore-buffers)) |
510cbc92 RS |
781 | (not (he-regexp-member (buffer-name) |
782 | hippie-expand-ignore-buffers))) | |
8c6677ed JB |
783 | (save-excursion |
784 | (goto-char he-search-loc) | |
510cbc92 RS |
785 | (setq expansion (let ((case-fold-search orig-case-fold-search)) |
786 | (he-list-search he-search-string nil))) | |
8c6677ed | 787 | (set-marker he-search-loc (point)) |
510cbc92 RS |
788 | (if (not expansion) |
789 | (progn | |
790 | (setq he-search-bufs (cdr he-search-bufs)) | |
791 | (setq he-searched-n-bufs (1+ he-searched-n-bufs)) | |
792 | (set-marker he-search-loc 1 (car he-search-bufs))))) | |
793 | (setq he-search-bufs (cdr he-search-bufs)) | |
794 | (set-marker he-search-loc 1 (car he-search-bufs))))) | |
8c6677ed JB |
795 | |
796 | (set-buffer buf) | |
797 | (if (not expansion) | |
798 | (progn | |
510cbc92 | 799 | (if old (he-reset-string)) |
8c6677ed JB |
800 | ()) |
801 | (progn | |
802 | (he-substitute-string expansion t) | |
803 | t)))) | |
804 | ||
805 | (defun he-list-search (str reverse) | |
806 | (let ((result ()) | |
510cbc92 | 807 | beg pos err) |
8c6677ed JB |
808 | (while (and (not result) |
809 | (if reverse | |
810 | (search-backward str nil t) | |
811 | (search-forward str nil t))) | |
812 | (setq pos (point)) | |
813 | (setq beg (match-beginning 0)) | |
814 | (goto-char beg) | |
815 | (setq err ()) | |
816 | (condition-case () | |
510cbc92 RS |
817 | (forward-list 1) |
818 | (error (setq err t))) | |
d1d1ddbd | 819 | (if (and reverse |
510cbc92 RS |
820 | (> (point) he-string-beg)) |
821 | (setq err t)) | |
8c6677ed | 822 | (if (not err) |
510cbc92 RS |
823 | (progn |
824 | (setq result (buffer-substring beg (point))) | |
825 | (if (he-string-member result he-tried-table t) | |
826 | (setq result nil)))) ; if already in table, ignore | |
8c6677ed JB |
827 | (goto-char pos)) |
828 | result)) | |
829 | ||
830 | (defun he-list-beg () | |
831 | (save-excursion | |
832 | (condition-case () | |
510cbc92 | 833 | (backward-up-list 1) |
8c6677ed JB |
834 | (error ())) |
835 | (point))) | |
836 | ||
652304c9 RS |
837 | (defun try-expand-all-abbrevs (old) |
838 | "Try to expand word before point according to all abbrev tables. | |
839 | The argument OLD has to be nil the first call of this function, and t | |
840 | for subsequent calls (for further possible expansions of the same | |
841 | string). It returns t if a new expansion is found, nil otherwise." | |
842 | (if (not old) | |
843 | (progn | |
844 | (he-init-string (he-dabbrev-beg) (point)) | |
845 | (setq he-expand-list | |
846 | (and (not (equal he-search-string "")) | |
847 | (mapcar (function (lambda (sym) | |
510cbc92 RS |
848 | (if (and (boundp sym) (vectorp (eval sym))) |
849 | (abbrev-expansion (downcase he-search-string) | |
850 | (eval sym))))) | |
652304c9 RS |
851 | (append '(local-abbrev-table |
852 | global-abbrev-table) | |
853 | abbrev-table-name-list)))))) | |
854 | (while (and he-expand-list | |
855 | (or (not (car he-expand-list)) | |
510cbc92 | 856 | (he-string-member (car he-expand-list) he-tried-table t))) |
652304c9 RS |
857 | (setq he-expand-list (cdr he-expand-list))) |
858 | (if (null he-expand-list) | |
859 | (progn | |
510cbc92 | 860 | (if old (he-reset-string)) |
652304c9 RS |
861 | ()) |
862 | (progn | |
8c6677ed | 863 | (he-substitute-string (car he-expand-list) t) |
652304c9 RS |
864 | (setq he-expand-list (cdr he-expand-list)) |
865 | t))) | |
866 | ||
867 | (defun try-expand-dabbrev (old) | |
868 | "Try to expand word \"dynamically\", searching the current buffer. | |
869 | The argument OLD has to be nil the first call of this function, and t | |
870 | for subsequent calls (for further possible expansions of the same | |
871 | string). It returns t if a new expansion is found, nil otherwise." | |
872 | (let ((expansion ())) | |
873 | (if (not old) | |
874 | (progn | |
875 | (he-init-string (he-dabbrev-beg) (point)) | |
8c6677ed | 876 | (set-marker he-search-loc he-string-beg) |
652304c9 RS |
877 | (setq he-search-bw t))) |
878 | ||
879 | (if (not (equal he-search-string "")) | |
880 | (save-excursion | |
881 | ;; Try looking backward unless inhibited. | |
882 | (if he-search-bw | |
883 | (progn | |
884 | (goto-char he-search-loc) | |
510cbc92 | 885 | (setq expansion (he-dabbrev-search he-search-string t)) |
8c6677ed | 886 | (set-marker he-search-loc (point)) |
652304c9 RS |
887 | (if (not expansion) |
888 | (progn | |
8c6677ed | 889 | (set-marker he-search-loc he-string-end) |
652304c9 RS |
890 | (setq he-search-bw ()))))) |
891 | ||
892 | (if (not expansion) ; Then look forward. | |
893 | (progn | |
894 | (goto-char he-search-loc) | |
510cbc92 | 895 | (setq expansion (he-dabbrev-search he-search-string nil)) |
8c6677ed | 896 | (set-marker he-search-loc (point)))))) |
652304c9 RS |
897 | |
898 | (if (not expansion) | |
899 | (progn | |
510cbc92 | 900 | (if old (he-reset-string)) |
652304c9 RS |
901 | ()) |
902 | (progn | |
903 | (he-substitute-string expansion t) | |
652304c9 RS |
904 | t)))) |
905 | ||
906 | (defun try-expand-dabbrev-all-buffers (old) | |
907 | "Tries to expand word \"dynamically\", searching all other buffers. | |
908 | The argument OLD has to be nil the first call of this function, and t | |
909 | for subsequent calls (for further possible expansions of the same | |
910 | string). It returns t if a new expansion is found, nil otherwise." | |
911 | (let ((expansion ()) | |
510cbc92 RS |
912 | (buf (current-buffer)) |
913 | (orig-case-fold-search case-fold-search)) | |
652304c9 RS |
914 | (if (not old) |
915 | (progn | |
916 | (he-init-string (he-dabbrev-beg) (point)) | |
8c6677ed | 917 | (setq he-search-bufs (buffer-list)) |
510cbc92 | 918 | (setq he-searched-n-bufs 0) |
8c6677ed | 919 | (set-marker he-search-loc 1 (car he-search-bufs)))) |
652304c9 RS |
920 | |
921 | (if (not (equal he-search-string "")) | |
8c6677ed | 922 | (while (and he-search-bufs |
510cbc92 RS |
923 | (not expansion) |
924 | (or (not hippie-expand-max-buffers) | |
925 | (< he-searched-n-bufs hippie-expand-max-buffers))) | |
652304c9 RS |
926 | (set-buffer (car he-search-bufs)) |
927 | (if (and (not (eq (current-buffer) buf)) | |
d1d1ddbd | 928 | (not (memq major-mode hippie-expand-ignore-buffers)) |
510cbc92 RS |
929 | (not (he-regexp-member (buffer-name) |
930 | hippie-expand-ignore-buffers))) | |
652304c9 RS |
931 | (save-excursion |
932 | (goto-char he-search-loc) | |
510cbc92 RS |
933 | (setq expansion (let ((case-fold-search orig-case-fold-search)) |
934 | (he-dabbrev-search he-search-string nil))) | |
8c6677ed | 935 | (set-marker he-search-loc (point)) |
510cbc92 RS |
936 | (if (not expansion) |
937 | (progn | |
938 | (setq he-search-bufs (cdr he-search-bufs)) | |
939 | (setq he-searched-n-bufs (1+ he-searched-n-bufs)) | |
940 | (set-marker he-search-loc 1 (car he-search-bufs))))) | |
941 | (setq he-search-bufs (cdr he-search-bufs)) | |
942 | (set-marker he-search-loc 1 (car he-search-bufs))))) | |
652304c9 RS |
943 | |
944 | (set-buffer buf) | |
945 | (if (not expansion) | |
946 | (progn | |
510cbc92 | 947 | (if old (he-reset-string)) |
652304c9 RS |
948 | ()) |
949 | (progn | |
950 | (he-substitute-string expansion t) | |
951 | t)))) | |
952 | ||
510cbc92 RS |
953 | ;; Thanks go to Jeff Dairiki <dairiki@faraday.apl.washington.edu> who |
954 | ;; suggested this one. | |
955 | (defun try-expand-dabbrev-visible (old) | |
956 | "Try to expand word \"dynamically\", searching visible window parts. | |
957 | The argument OLD has to be nil the first call of this function, and t | |
958 | for subsequent calls (for further possible expansions of the same | |
959 | string). It returns t if a new expansion is found, nil otherwise." | |
960 | (let ((expansion ()) | |
961 | (buf (current-buffer)) | |
962 | (flag (if (frame-visible-p (window-frame (selected-window))) | |
963 | 'visible t))) | |
964 | (if (not old) | |
965 | (progn | |
966 | (he-init-string (he-dabbrev-beg) (point)) | |
967 | (setq he-search-window (selected-window)) | |
968 | (set-marker he-search-loc | |
969 | (window-start he-search-window) | |
970 | (window-buffer he-search-window)))) | |
971 | ||
972 | (while (and (not (equal he-search-string "")) | |
973 | (marker-position he-search-loc) | |
974 | (not expansion)) | |
975 | (save-excursion | |
976 | (set-buffer (marker-buffer he-search-loc)) | |
977 | (goto-char he-search-loc) | |
978 | (setq expansion (he-dabbrev-search he-search-string () | |
979 | (window-end he-search-window))) | |
980 | (if (and expansion | |
981 | (eq (marker-buffer he-string-beg) (current-buffer)) | |
982 | (eq (marker-position he-string-beg) (match-beginning 0))) | |
983 | (setq expansion (he-dabbrev-search he-search-string () | |
984 | (window-end he-search-window)))) | |
985 | (set-marker he-search-loc (point) (current-buffer))) | |
986 | (if (not expansion) | |
987 | (progn | |
988 | (setq he-search-window (next-window he-search-window nil flag)) | |
989 | (if (eq he-search-window (selected-window)) | |
990 | (set-marker he-search-loc nil) | |
991 | (set-marker he-search-loc (window-start he-search-window) | |
992 | (window-buffer he-search-window)))))) | |
993 | ||
994 | (set-buffer buf) | |
995 | (if (not expansion) | |
996 | (progn | |
997 | (if old (he-reset-string)) | |
998 | ()) | |
999 | (progn | |
1000 | (he-substitute-string expansion t) | |
1001 | t)))) | |
652304c9 | 1002 | |
510cbc92 RS |
1003 | (defun he-dabbrev-search (pattern &optional reverse limit) |
1004 | (let ((result ()) | |
de073e06 KH |
1005 | (regpat (cond ((not he-dabbrev-as-symbol) |
1006 | (concat "\\<" (regexp-quote pattern) "\\sw+")) | |
1007 | ((eq (char-syntax (aref pattern 0)) ?_) | |
1008 | (concat (regexp-quote pattern) "\\(\\sw\\|\\s_\\)+")) | |
1009 | (t | |
1010 | (concat "\\<" (regexp-quote pattern) | |
1011 | "\\(\\sw\\|\\s_\\)+"))))) | |
652304c9 RS |
1012 | (while (and (not result) |
1013 | (if reverse | |
510cbc92 RS |
1014 | (re-search-backward regpat limit t) |
1015 | (re-search-forward regpat limit t))) | |
652304c9 | 1016 | (setq result (buffer-substring (match-beginning 0) (match-end 0))) |
de073e06 KH |
1017 | (if (or (and he-dabbrev-as-symbol |
1018 | (> (match-beginning 0) (point-min)) | |
510cbc92 RS |
1019 | (memq (char-syntax (char-after (1- (match-beginning 0)))) |
1020 | '(?_ ?w))) | |
1021 | (he-string-member result he-tried-table t)) | |
1022 | (setq result nil))) ; ignore if bad prefix or already in table | |
652304c9 RS |
1023 | result)) |
1024 | ||
1025 | (defun he-dabbrev-beg () | |
510cbc92 RS |
1026 | (let ((op (point))) |
1027 | (save-excursion | |
1028 | (if he-dabbrev-skip-space | |
1029 | (skip-syntax-backward ". ")) | |
de073e06 | 1030 | (if (= (skip-syntax-backward (if he-dabbrev-as-symbol "w_" "w")) 0) |
510cbc92 RS |
1031 | op |
1032 | (point))))) | |
1033 | ||
1034 | (defun try-expand-dabbrev-from-kill (old) | |
1035 | "Try to expand word \"dynamically\", searching the kill ring. | |
1036 | The argument OLD has to be nil the first call of this function, and t | |
1037 | for subsequent calls (for further possible completions of the same | |
1038 | string). It returns t if a new completion is found, nil otherwise." | |
1039 | (let ((expansion ())) | |
1040 | (if (not old) | |
1041 | (progn | |
1042 | (he-init-string (he-dabbrev-beg) (point)) | |
1043 | (setq he-expand-list | |
1044 | (if (not (equal he-search-string "")) | |
1045 | kill-ring)) | |
1046 | (setq he-search-loc2 0))) | |
1047 | (if (not (equal he-search-string "")) | |
1048 | (setq expansion (he-dabbrev-kill-search he-search-string))) | |
1049 | (if (not expansion) | |
1050 | (progn | |
1051 | (if old (he-reset-string)) | |
1052 | ()) | |
1053 | (progn | |
1054 | (he-substitute-string expansion t) | |
1055 | t)))) | |
1056 | ||
1057 | (defun he-dabbrev-kill-search (pattern) | |
1058 | (let ((result ()) | |
de073e06 KH |
1059 | (regpat (cond ((not he-dabbrev-as-symbol) |
1060 | (concat "\\<" (regexp-quote pattern) "\\sw+")) | |
1061 | ((eq (char-syntax (aref pattern 0)) ?_) | |
1062 | (concat (regexp-quote pattern) "\\(\\sw\\|\\s_\\)+")) | |
1063 | (t | |
1064 | (concat "\\<" (regexp-quote pattern) | |
1065 | "\\(\\sw\\|\\s_\\)+")))) | |
510cbc92 RS |
1066 | (killstr (car he-expand-list))) |
1067 | (while (and (not result) | |
1068 | he-expand-list) | |
1069 | (while (and (not result) | |
1070 | (string-match regpat killstr he-search-loc2)) | |
1071 | (setq result (substring killstr (match-beginning 0) (match-end 0))) | |
1072 | (setq he-search-loc2 (1+ (match-beginning 0))) | |
de073e06 KH |
1073 | (if (or (and he-dabbrev-as-symbol |
1074 | (> (match-beginning 0) 0) | |
510cbc92 RS |
1075 | (memq (char-syntax (aref killstr (1- (match-beginning 0)))) |
1076 | '(?_ ?w))) | |
1077 | (he-string-member result he-tried-table t)) | |
1078 | (setq result nil))) ; ignore if bad prefix or already in table | |
1079 | (if (and (not result) | |
1080 | he-expand-list) | |
1081 | (progn | |
1082 | (setq he-expand-list (cdr he-expand-list)) | |
1083 | (setq killstr (car he-expand-list)) | |
1084 | (setq he-search-loc2 0)))) | |
1085 | result)) | |
1086 | ||
1087 | (defun try-expand-whole-kill (old) | |
1088 | "Try to complete text with something from the kill ring. | |
1089 | The argument OLD has to be nil the first call of this function, and t | |
1090 | for subsequent calls (for further possible completions of the same | |
1091 | string). It returns t if a new completion is found, nil otherwise." | |
1092 | (let ((expansion ())) | |
1093 | (if (not old) | |
1094 | (progn | |
1095 | (he-init-string (he-kill-beg) (point)) | |
1096 | (if (not (he-string-member he-search-string he-tried-table)) | |
1097 | (setq he-tried-table (cons he-search-string he-tried-table))) | |
1098 | (setq he-expand-list | |
1099 | (if (not (equal he-search-string "")) | |
1100 | kill-ring)) | |
1101 | (setq he-search-loc2 ()))) | |
1102 | (if (not (equal he-search-string "")) | |
1103 | (setq expansion (he-whole-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) | |
1110 | t)))) | |
1111 | ||
1112 | (defun he-whole-kill-search (str) | |
1113 | (let ((case-fold-search ()) | |
1114 | (result ()) | |
1115 | (str (regexp-quote str)) | |
1116 | (killstr (car he-expand-list)) | |
1117 | (pos -1)) | |
1118 | (while (and (not result) | |
1119 | he-expand-list) | |
1120 | (if (not he-search-loc2) | |
1121 | (while (setq pos (string-match str killstr (1+ pos))) | |
1122 | (setq he-search-loc2 (cons pos he-search-loc2)))) | |
1123 | (while (and (not result) | |
1124 | he-search-loc2) | |
1125 | (setq pos (car he-search-loc2)) | |
1126 | (setq he-search-loc2 (cdr he-search-loc2)) | |
1127 | (save-excursion | |
1128 | (goto-char he-string-beg) | |
1129 | (if (and (>= (- (point) pos) (point-min)) ; avoid some string GC | |
1130 | (eq (char-after (- (point) pos)) (aref killstr 0)) | |
1131 | (search-backward (substring killstr 0 pos) | |
1132 | (- (point) pos) t)) | |
1133 | (setq result (substring killstr pos)))) | |
1134 | (if (and result | |
1135 | (he-string-member result he-tried-table)) | |
1136 | (setq result nil))) ; ignore if already in table | |
1137 | (if (and (not result) | |
1138 | he-expand-list) | |
1139 | (progn | |
1140 | (setq he-expand-list (cdr he-expand-list)) | |
1141 | (setq killstr (car he-expand-list)) | |
1142 | (setq pos -1)))) | |
1143 | result)) | |
1144 | ||
1145 | (defun he-kill-beg () | |
1146 | (let ((op (point))) | |
1147 | (save-excursion | |
1148 | (skip-syntax-backward "^w_") | |
1149 | (if (= (skip-syntax-backward "w_") 0) | |
1150 | op | |
1151 | (point))))) | |
1152 | ||
8c6677ed JB |
1153 | |
1154 | (provide 'hippie-exp) | |
652304c9 | 1155 | |
8c6677ed | 1156 | ;;; hippie-exp.el ends here |