Commit | Line | Data |
---|---|---|
e8af40ee | 1 | ;;; hippie-exp.el --- expand text trying various ways to find its expansion |
3b1e4dd1 | 2 | |
ba318903 | 3 | ;; Copyright (C) 1992, 2001-2014 Free Software Foundation, Inc. |
b578f267 | 4 | |
3b1e4dd1 | 5 | ;; Author: Anders Holst <aho@sans.kth.se> |
34dc21db | 6 | ;; Maintainer: emacs-devel@gnu.org |
41b8e71c | 7 | ;; Version: 1.6 |
f5f727f8 | 8 | ;; Keywords: abbrev convenience |
3b1e4dd1 | 9 | |
652304c9 RS |
10 | ;; This file is part of GNU Emacs. |
11 | ||
eb3fa2cf | 12 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
652304c9 | 13 | ;; it under the terms of the GNU General Public License as published by |
eb3fa2cf GM |
14 | ;; the Free Software Foundation, either version 3 of the License, or |
15 | ;; (at your option) any later version. | |
652304c9 RS |
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 | |
eb3fa2cf | 23 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
652304c9 | 24 | |
76550a57 | 25 | ;;; Commentary: |
b578f267 | 26 | |
652304c9 RS |
27 | ;; `hippie-expand' is a single function for a lot of different kinds |
28 | ;; of completions and expansions. Called repeatedly it tries all | |
c60ee5e7 | 29 | ;; possible completions in succession. |
652304c9 RS |
30 | ;; Which kinds of completions to try, and in which order, is |
31 | ;; determined by the contents of `hippie-expand-try-functions-list'. | |
32 | ;; Much customization of `hippie-expand' can be made by changing the | |
33 | ;; order of, removing, or inserting new functions in this list. | |
34 | ;; Given a positive numeric argument, `hippie-expand' jumps directly | |
35 | ;; ARG functions forward in this list. Given some other argument | |
36 | ;; (a negative argument or just Ctrl-U) it undoes the tried | |
37 | ;; completion. | |
d1d1ddbd | 38 | ;; |
652304c9 RS |
39 | ;; If the variable `hippie-expand-verbose' is non-nil, `hippie-expand' |
40 | ;; outputs in a message which try-function in the list that is used | |
41 | ;; currently (ie. was used currently and will be tried first the next | |
42 | ;; time). | |
8c6677ed JB |
43 | ;; The variable `hippie-expand-max-buffers' determines in how many |
44 | ;; buffers, apart from the current, to search for expansions in. It | |
45 | ;; is used by the try-functions named "-all-buffers". | |
d1d1ddbd RS |
46 | ;; The variable `hippie-expand-ignore-buffers' is a list of regexps |
47 | ;; matching buffer names (as strings) or major modes (as atoms) of | |
48 | ;; buffers that should not be searched by the try-functions named | |
49 | ;; "-all-buffers". | |
41b8e71c RS |
50 | ;; If set, the variable `hippie-expand-only-buffers' does the opposite |
51 | ;; of `hippie-expand-ignore-buffers', in that the search is restricted | |
52 | ;; to only the kind of buffers listed. | |
53 | ;; If the variable `hippie-expand-no-restriction' is non-nil, narrowed | |
54 | ;; buffers are widened before they are searched. | |
55 | ;; The variable `hippie-expand-dabbrev-skip-space' controls whether | |
56 | ;; trailing spaces will be included in the abbreviation to search for, | |
57 | ;; which then gives the same behavior as the original `dabbrev-expand'. | |
58 | ;; The variable `hippie-expand-dabbrev-as-symbol' controls whether | |
59 | ;; characters of syntax '_' is considered part of the words to expand | |
60 | ;; dynamically. | |
652304c9 | 61 | ;; See also the macro `make-hippie-expand-function' below. |
c60ee5e7 | 62 | ;; |
652304c9 RS |
63 | ;; A short description of the current try-functions in this file: |
64 | ;; `try-complete-file-name' : very convenient to have in any buffer, | |
65 | ;; and not just in the minibuffer or (some) shell-mode. It goes | |
66 | ;; through all possible completions instead of just completing as | |
67 | ;; much as is unique. | |
68 | ;; `try-complete-file-name-partially' : To insert in the list just | |
69 | ;; before `try-complete-file-name' for those who want first to get | |
70 | ;; a file name completed only as many characters as is unique. | |
652304c9 RS |
71 | ;; `try-expand-all-abbrevs' : can be removed if you don't use abbrevs. |
72 | ;; Otherwise it looks through all abbrev-tables, starting with | |
c60ee5e7 JB |
73 | ;; the local followed by the global. |
74 | ;; `try-expand-line' : Searches the buffer for an entire line that | |
75 | ;; begins exactly as the current line. Convenient sometimes, for | |
652304c9 | 76 | ;; example as a substitute for (or complement to) the history |
8c6677ed | 77 | ;; list in shell-like buffers. At other times, only confusing. |
652304c9 RS |
78 | ;; `try-expand-line-all-buffers' : Like `try-expand-line' but searches |
79 | ;; in all buffers (except the current). (This may be a little | |
8c6677ed JB |
80 | ;; slow, don't use it unless you are really fond of `hippie-expand'.) |
81 | ;; `try-expand-list' : Tries to expand the text back to the nearest | |
f72f0c23 SM |
82 | ;; open delimiter, to a whole list from the buffer. Convenient for |
83 | ;; example when writing Lisp or TeX. | |
c60ee5e7 JB |
84 | ;; `try-expand-list-all-buffers' : Like `try-expand-list' but searches |
85 | ;; in all buffers (except the current). | |
652304c9 RS |
86 | ;; `try-expand-dabbrev' : works exactly as dabbrev-expand (but of |
87 | ;; course in a way compatible with the other try-functions). | |
88 | ;; `try-expand-dabbrev-all-buffers' : perhaps the most useful of them, | |
89 | ;; like `dabbrev-expand' but searches all Emacs buffers (except the | |
90 | ;; current) for matching words. (No, I don't find this one | |
c60ee5e7 | 91 | ;; particularly slow.) |
510cbc92 RS |
92 | ;; `try-expand-dabbrev-visible': Searches the currently visible parts of |
93 | ;; all windows. Can be put before `try-expand-dabbrev-all-buffers' to | |
94 | ;; first try the expansions you can see. | |
95 | ;; `try-expand-dabbrev-from-kill': Searches the kill ring for a suitable | |
96 | ;; completion of the word. Good to have, just in case the word was not | |
97 | ;; found elsewhere. | |
98 | ;; `try-expand-whole-kill' : Tries to complete text with a whole entry | |
99 | ;; from the kill ring. May be good if you don't know how far up in | |
100 | ;; the kill-ring the required entry is, and don't want to mess with | |
101 | ;; "Choose Next Paste". | |
652304c9 RS |
102 | ;; `try-complete-lisp-symbol' : like `lisp-complete-symbol', but goes |
103 | ;; through all possibilities instead of completing what is unique. | |
104 | ;; Might be tedious (usually a lot of possible completions) and | |
105 | ;; since its function is much like `lisp-complete-symbol', which | |
106 | ;; already has a key of its own, you might want to remove this. | |
107 | ;; `try-complete-lisp-symbol-partially' : To insert in the list just | |
108 | ;; before `try-complete-lisp-symbol' for those who first want to get | |
c60ee5e7 | 109 | ;; completion of what is unique in the name. |
8c6677ed JB |
110 | ;; |
111 | ;; Not all of the above functions are by default in | |
112 | ;; `hippie-expand-try-functions-list'. This variable is better set | |
113 | ;; in ".emacs" to make `hippie-expand' behave maximally convenient | |
114 | ;; according to personal taste. Also, instead of loading the | |
115 | ;; variable with all kinds of try-functions above, it might be an | |
116 | ;; idea to use `make-hippie-expand-function' to construct different | |
117 | ;; `hippie-expand'-like functions, with different try-lists and bound | |
f72f0c23 | 118 | ;; to different keys. It is also possible to make |
8c6677ed JB |
119 | ;; `hippie-expand-try-functions-list' a buffer local variable, and |
120 | ;; let it depend on the mode (by setting it in the mode-hooks). | |
652304c9 RS |
121 | ;; |
122 | ;; To write new try-functions, consider the following: | |
123 | ;; Each try-function takes one argument OLD which is nil the first | |
124 | ;; time the function is called and true in succeeding calls for the | |
125 | ;; same string to complete. The first time the function has to | |
126 | ;; extract the string before point to complete, and substitute the | |
127 | ;; first completion alternative for it. On following calls it has to | |
128 | ;; substitute the next possible completion for the last tried string. | |
129 | ;; The try-function is to return t as long as it finds new | |
130 | ;; possible completions. When there are no more alternatives it has | |
131 | ;; to restore the text before point to its original contents, and | |
132 | ;; return nil (don't beep or message or anything). | |
133 | ;; The try-function can (should) use the following functions: | |
134 | ;; `he-init-string' : Initializes the text to substitute to the | |
135 | ;; contents of the region BEGIN to END. Also sets the variable | |
136 | ;; `he-search-string' to the text to expand. | |
137 | ;; `he-substitute-string' : substitutes STR into the region | |
138 | ;; initialized with `he-init-string'. (An optional second argument | |
139 | ;; TRANS-CASE non-nil, means transfer of case from the abbreviation | |
140 | ;; to the expansion is ok if that is enabled in the buffer.) | |
141 | ;; `he-reset-string' : Resets the initialized region to its original | |
142 | ;; contents. | |
143 | ;; There is also a variable: `he-tried-table' which is meant to contain | |
c60ee5e7 | 144 | ;; all tried expansions so far. The try-function can check this |
652304c9 | 145 | ;; variable to see whether an expansion has already been tried |
510cbc92 | 146 | ;; (hint: `he-string-member'). |
652304c9 | 147 | ;; |
8c6677ed | 148 | ;; Known bugs |
652304c9 RS |
149 | ;; |
150 | ;; It may happen that some completion suggestion occurs twice, in | |
c60ee5e7 | 151 | ;; spite of the use of `he-tried-table' to prevent that. This is |
652304c9 RS |
152 | ;; because different try-functions may try to complete different |
153 | ;; lengths of text, and thus put different amounts of the | |
510cbc92 RS |
154 | ;; text in `he-tried-table'. Anyway this seems to occur seldom enough |
155 | ;; not to be too disturbing. Also it should NOT be possible for the | |
652304c9 RS |
156 | ;; opposite situation to occur, that `hippie-expand' misses some |
157 | ;; suggestion because it thinks it has already tried it. | |
158 | ;; | |
09ae5da1 | 159 | ;; Acknowledgment |
652304c9 RS |
160 | ;; |
161 | ;; I want to thank Mikael Djurfeldt in discussions with whom the idea | |
162 | ;; of this function took form. | |
163 | ;; I am also grateful to all those who have given me suggestions on | |
510cbc92 | 164 | ;; how to improve it, and all those who helped to find and remove bugs. |
652304c9 RS |
165 | ;; |
166 | ||
76550a57 | 167 | ;;; Code: |
652304c9 | 168 | |
6df53d08 | 169 | (require 'comint) |
ddb6e2e2 | 170 | |
94114394 RS |
171 | (defgroup hippie-expand nil |
172 | "Expand text trying various ways to find its expansion." | |
ddb6e2e2 DL |
173 | :link '(custom-manual "(autotype)Hippie Expand") |
174 | :link '(emacs-commentary-link "hippie-exp") | |
f5f727f8 DN |
175 | :group 'abbrev |
176 | :group 'convenience) | |
94114394 | 177 | |
652304c9 RS |
178 | (defvar he-num -1) |
179 | ||
8c6677ed | 180 | (defvar he-string-beg (make-marker)) |
652304c9 | 181 | |
8c6677ed | 182 | (defvar he-string-end (make-marker)) |
652304c9 RS |
183 | |
184 | (defvar he-search-string ()) | |
185 | ||
186 | (defvar he-expand-list ()) | |
187 | ||
188 | (defvar he-tried-table ()) | |
189 | ||
8c6677ed | 190 | (defvar he-search-loc (make-marker)) |
652304c9 | 191 | |
510cbc92 RS |
192 | (defvar he-search-loc2 ()) |
193 | ||
652304c9 RS |
194 | (defvar he-search-bw ()) |
195 | ||
196 | (defvar he-search-bufs ()) | |
197 | ||
8c6677ed JB |
198 | (defvar he-searched-n-bufs ()) |
199 | ||
510cbc92 RS |
200 | (defvar he-search-window ()) |
201 | ||
50207191 GM |
202 | ;;; Autoloaded for historical reasons (bug#12982) |
203 | ;;;###autoload | |
ddb6e2e2 DL |
204 | (defcustom hippie-expand-try-functions-list |
205 | '(try-complete-file-name-partially | |
206 | try-complete-file-name | |
207 | try-expand-all-abbrevs | |
208 | try-expand-list | |
209 | try-expand-line | |
210 | try-expand-dabbrev | |
211 | try-expand-dabbrev-all-buffers | |
212 | try-expand-dabbrev-from-kill | |
213 | try-complete-lisp-symbol-partially | |
214 | try-complete-lisp-symbol) | |
652304c9 RS |
215 | "The list of expansion functions tried in order by `hippie-expand'. |
216 | To change the behavior of `hippie-expand', remove, change the order of, | |
ddb6e2e2 DL |
217 | or insert functions in this list." |
218 | :type '(repeat function) | |
219 | :group 'hippie-expand) | |
652304c9 | 220 | |
94114394 | 221 | (defcustom hippie-expand-verbose t |
9201cc28 | 222 | "Non-nil makes `hippie-expand' output which function it is trying." |
94114394 RS |
223 | :type 'boolean |
224 | :group 'hippie-expand) | |
652304c9 | 225 | |
41b8e71c | 226 | (defcustom hippie-expand-dabbrev-skip-space nil |
9201cc28 | 227 | "Non-nil means tolerate trailing spaces in the abbreviation to expand." |
41b8e71c RS |
228 | :group 'hippie-expand |
229 | :type 'boolean) | |
230 | ||
41b8e71c | 231 | (defcustom hippie-expand-dabbrev-as-symbol t |
9201cc28 | 232 | "Non-nil means expand as symbols, i.e. syntax `_' is considered a letter." |
41b8e71c RS |
233 | :group 'hippie-expand |
234 | :type 'boolean) | |
235 | ||
41b8e71c | 236 | (defcustom hippie-expand-no-restriction t |
9201cc28 | 237 | "Non-nil means that narrowed buffers are widened during search." |
41b8e71c RS |
238 | :group 'hippie-expand |
239 | :type 'boolean) | |
240 | ||
94114394 | 241 | (defcustom hippie-expand-max-buffers () |
9201cc28 | 242 | "The maximum number of buffers (apart from the current) searched. |
94114394 RS |
243 | If nil, all buffers are searched." |
244 | :type '(choice (const :tag "All" nil) | |
245 | integer) | |
246 | :group 'hippie-expand) | |
8c6677ed | 247 | |
3ef6d505 | 248 | (defcustom hippie-expand-ignore-buffers '("^ \\*.*\\*$" dired-mode) |
9201cc28 | 249 | "A list specifying which buffers not to search (if not current). |
d1d1ddbd | 250 | Can contain both regexps matching buffer names (as strings) and major modes |
94114394 RS |
251 | \(as atoms)" |
252 | :type '(repeat (choice regexp (symbol :tag "Major Mode"))) | |
253 | :group 'hippie-expand) | |
d1d1ddbd | 254 | |
41b8e71c | 255 | (defcustom hippie-expand-only-buffers () |
9201cc28 | 256 | "A list specifying the only buffers to search (in addition to current). |
41b8e71c | 257 | Can contain both regexps matching buffer names (as strings) and major modes |
0ff9b955 | 258 | \(as atoms). If non-nil, this variable overrides the variable |
41b8e71c RS |
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. | |
266 | The expansion functions in `hippie-expand-try-functions-list' are | |
267 | tried in order, until a possible expansion is found. Repeated | |
268 | application of `hippie-expand' inserts successively possible | |
c60ee5e7 | 269 | expansions. |
652304c9 | 270 | With a positive numeric argument, jumps directly to the ARG next |
c60ee5e7 JB |
271 | function in this list. With a negative argument or just \\[universal-argument], |
272 | undoes the expansion." | |
652304c9 | 273 | (interactive "P") |
c60ee5e7 | 274 | (if (or (not arg) |
652304c9 RS |
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)) | |
c60ee5e7 | 287 | (apply (nth i hippie-expand-try-functions-list) |
652304c9 RS |
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 |
290d5b58 | 299 | (not (window-minibuffer-p))) |
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 |
290d5b58 | 308 | (not (window-minibuffer-p))) |
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 | |
c60ee5e7 | 326 | ;; initialized with `he-init-string'). |
652304c9 RS |
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. | |
0ff9b955 | 377 | ;; Transform to the final case if optional TRANS-CASE is non-nil. |
510cbc92 RS |
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 | 387 | ;; Check if current buffer matches any atom or regexp in LST. |
53964682 | 388 | ;; Atoms are interpreted as major modes, strings as regexps matching the name. |
41b8e71c RS |
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))) | |
c60ee5e7 | 408 | ;; |
8c6677ed | 409 | ;;;###autoload |
652304c9 RS |
410 | (defmacro make-hippie-expand-function (try-list &optional verbose) |
411 | "Construct a function similar to `hippie-expand'. | |
412 | Make it use the expansion functions in TRY-LIST. An optional second | |
413 | argument VERBOSE non-nil makes the function verbose." | |
d7d20e6a | 414 | `(function (lambda (arg) |
c60ee5e7 | 415 | ,(concat |
d7d20e6a GM |
416 | "Try to expand text before point, using the following functions: \n" |
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)) | |
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. | |
429 | The argument OLD has to be nil the first call of this function, and t | |
430 | for subsequent calls (for further possible completions of the same | |
431 | string). It returns t if a new completion is found, nil otherwise." | |
432 | (if (not old) | |
c60ee5e7 | 433 | (progn |
652304c9 | 434 | (he-init-string (he-file-name-beg) (point)) |
7c2fb837 DN |
435 | (let ((name-part (file-name-nondirectory he-search-string)) |
436 | (dir-part (expand-file-name (or (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 "")) | |
7c2fb837 | 441 | (file-directory-p dir-part)) |
c60ee5e7 | 442 | (setq he-expand-list (sort (file-name-all-completions |
652304c9 RS |
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 | 455 | (let ((filename (he-concat-directory-file-name |
7c2fb837 | 456 | (file-name-directory he-search-string) |
510cbc92 | 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. | |
465 | The argument OLD has to be nil the first call of this function. It | |
c60ee5e7 | 466 | returns t if a unique, possibly partial, completion is found, nil |
652304c9 RS |
467 | otherwise." |
468 | (let ((expansion ())) | |
469 | (if (not old) | |
c60ee5e7 | 470 | (progn |
652304c9 | 471 | (he-init-string (he-file-name-beg) (point)) |
7c2fb837 DN |
472 | (let ((name-part (file-name-nondirectory he-search-string)) |
473 | (dir-part (expand-file-name (or (file-name-directory | |
652304c9 RS |
474 | he-search-string) "")))) |
475 | (if (and (not (equal he-search-string "")) | |
7c2fb837 | 476 | (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 | 488 | (let ((filename (he-concat-directory-file-name |
7c2fb837 | 489 | (file-name-directory he-search-string) |
510cbc92 | 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 | 495 | (defvar he-file-name-chars |
7c2fb837 | 496 | (cond ((memq system-type '(ms-dos windows-nt cygwin)) |
510cbc92 RS |
497 | "-a-zA-Z0-9_/.,~^#$+=:\\\\") |
498 | (t ;; More strange file formats ? | |
499 | "-a-zA-Z0-9_/.,~^#$+=")) | |
500 | "Characters that are considered part of the file name to expand.") | |
501 | ||
652304c9 | 502 | (defun he-file-name-beg () |
de073e06 KH |
503 | (let ((op (point))) |
504 | (save-excursion | |
505 | (skip-chars-backward he-file-name-chars) | |
506 | (if (> (skip-syntax-backward "w") 0) ;; No words with non-file chars | |
41b8e71c RS |
507 | op |
508 | (point))))) | |
652304c9 | 509 | |
7c2fb837 | 510 | ;; Thanks go to David Hughes <ukchugd@ukpmr.cs.philips.nl> who |
510cbc92 | 511 | ;; helped to make it work on PC. |
510cbc92 RS |
512 | (defun he-concat-directory-file-name (dir-part name-part) |
513 | "Try to slam together two parts of a file specification, system dependently." | |
a0341d13 | 514 | (cond ((null dir-part) name-part) |
a3374680 | 515 | ((eq system-type 'ms-dos) |
510cbc92 RS |
516 | (if (and (string-match "\\\\" dir-part) |
517 | (not (string-match "/" dir-part)) | |
518 | (= (aref name-part (1- (length name-part))) ?/)) | |
519 | (aset name-part (1- (length name-part)) ?\\)) | |
520 | (concat dir-part name-part)) | |
c60ee5e7 | 521 | (t |
510cbc92 | 522 | (concat dir-part name-part)))) |
c60ee5e7 | 523 | |
652304c9 RS |
524 | (defun try-complete-lisp-symbol (old) |
525 | "Try to complete word as an Emacs Lisp symbol. | |
526 | The argument OLD has to be nil the first call of this function, and t | |
527 | for subsequent calls (for further possible completions of the same | |
528 | string). It returns t if a new completion is found, nil otherwise." | |
529 | (if (not old) | |
c60ee5e7 | 530 | (progn |
652304c9 RS |
531 | (he-init-string (he-lisp-symbol-beg) (point)) |
532 | (if (not (he-string-member he-search-string he-tried-table)) | |
533 | (setq he-tried-table (cons he-search-string he-tried-table))) | |
c60ee5e7 | 534 | (setq he-expand-list |
652304c9 RS |
535 | (and (not (equal he-search-string "")) |
536 | (sort (all-completions he-search-string obarray | |
537 | (function (lambda (sym) | |
538 | (or (boundp sym) | |
539 | (fboundp sym) | |
540 | (symbol-plist sym))))) | |
541 | 'string-lessp))))) | |
542 | (while (and he-expand-list | |
543 | (he-string-member (car he-expand-list) he-tried-table)) | |
544 | (setq he-expand-list (cdr he-expand-list))) | |
545 | (if (null he-expand-list) | |
546 | (progn | |
510cbc92 | 547 | (if old (he-reset-string)) |
652304c9 RS |
548 | ()) |
549 | (progn | |
550 | (he-substitute-string (car he-expand-list)) | |
652304c9 RS |
551 | (setq he-expand-list (cdr he-expand-list)) |
552 | t))) | |
553 | ||
554 | (defun try-complete-lisp-symbol-partially (old) | |
555 | "Try to complete as an Emacs Lisp symbol, as many characters as unique. | |
556 | The argument OLD has to be nil the first call of this function. It | |
c60ee5e7 | 557 | returns t if a unique, possibly partial, completion is found, nil |
652304c9 RS |
558 | otherwise." |
559 | (let ((expansion ())) | |
560 | (if (not old) | |
c60ee5e7 | 561 | (progn |
652304c9 RS |
562 | (he-init-string (he-lisp-symbol-beg) (point)) |
563 | (if (not (string= he-search-string "")) | |
c60ee5e7 | 564 | (setq expansion |
652304c9 RS |
565 | (try-completion he-search-string obarray |
566 | (function (lambda (sym) | |
567 | (or (boundp sym) | |
568 | (fboundp sym) | |
569 | (symbol-plist sym))))))) | |
570 | (if (or (eq expansion t) | |
510cbc92 RS |
571 | (string= expansion he-search-string) |
572 | (he-string-member expansion he-tried-table)) | |
652304c9 RS |
573 | (setq expansion ())))) |
574 | ||
575 | (if (not expansion) | |
576 | (progn | |
510cbc92 | 577 | (if old (he-reset-string)) |
652304c9 RS |
578 | ()) |
579 | (progn | |
580 | (he-substitute-string expansion) | |
652304c9 RS |
581 | t)))) |
582 | ||
583 | (defun he-lisp-symbol-beg () | |
de073e06 KH |
584 | (save-excursion |
585 | (skip-syntax-backward "w_") | |
586 | (point))) | |
652304c9 RS |
587 | |
588 | (defun try-expand-line (old) | |
589 | "Try to complete the current line to an entire line in the buffer. | |
590 | The argument OLD has to be nil the first call of this function, and t | |
591 | for subsequent calls (for further possible completions of the same | |
592 | string). It returns t if a new completion is found, nil otherwise." | |
593 | (let ((expansion ()) | |
594 | (strip-prompt (and (get-buffer-process (current-buffer)) | |
d7210472 | 595 | comint-use-prompt-regexp |
8c6677ed | 596 | comint-prompt-regexp))) |
652304c9 RS |
597 | (if (not old) |
598 | (progn | |
599 | (he-init-string (he-line-beg strip-prompt) (point)) | |
8c6677ed | 600 | (set-marker he-search-loc he-string-beg) |
652304c9 RS |
601 | (setq he-search-bw t))) |
602 | ||
603 | (if (not (equal he-search-string "")) | |
604 | (save-excursion | |
41b8e71c RS |
605 | (save-restriction |
606 | (if hippie-expand-no-restriction | |
607 | (widen)) | |
608 | ;; Try looking backward unless inhibited. | |
609 | (if he-search-bw | |
c60ee5e7 | 610 | (progn |
41b8e71c RS |
611 | (goto-char he-search-loc) |
612 | (setq expansion (he-line-search he-search-string | |
613 | strip-prompt t)) | |
614 | (set-marker he-search-loc (point)) | |
615 | (if (not expansion) | |
616 | (progn | |
617 | (set-marker he-search-loc he-string-end) | |
618 | (setq he-search-bw ()))))) | |
619 | ||
620 | (if (not expansion) ; Then look forward. | |
c60ee5e7 | 621 | (progn |
41b8e71c | 622 | (goto-char he-search-loc) |
c60ee5e7 | 623 | (setq expansion (he-line-search he-search-string |
41b8e71c RS |
624 | strip-prompt nil)) |
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." | |
de825bbb SM |
640 | (he--all-buffers |
641 | old | |
642 | (lambda () (he-line-beg (and (get-buffer-process (current-buffer)) | |
643 | comint-use-prompt-regexp | |
644 | comint-prompt-regexp))) | |
645 | (lambda (string) | |
646 | (he-line-search string | |
647 | (and (get-buffer-process (current-buffer)) | |
648 | comint-use-prompt-regexp | |
649 | comint-prompt-regexp) | |
650 | nil)))) | |
651 | ||
652 | (defun he--all-buffers (old beg-function search-function) | |
652304c9 | 653 | (let ((expansion ()) |
de825bbb | 654 | (buf (current-buffer)) |
85762518 EL |
655 | (only-buffers hippie-expand-only-buffers) |
656 | (ignore-buffers hippie-expand-ignore-buffers) | |
de825bbb | 657 | (orig-case-fold-search case-fold-search)) |
652304c9 | 658 | (if (not old) |
de825bbb SM |
659 | (progn |
660 | (he-init-string (funcall beg-function) (point)) | |
8c6677ed | 661 | (setq he-search-bufs (buffer-list)) |
510cbc92 | 662 | (setq he-searched-n-bufs 0) |
8c6677ed | 663 | (set-marker he-search-loc 1 (car he-search-bufs)))) |
652304c9 RS |
664 | |
665 | (if (not (equal he-search-string "")) | |
c60ee5e7 | 666 | (while (and he-search-bufs |
510cbc92 RS |
667 | (not expansion) |
668 | (or (not hippie-expand-max-buffers) | |
de825bbb SM |
669 | (< he-searched-n-bufs hippie-expand-max-buffers))) |
670 | (set-buffer (car he-search-bufs)) | |
671 | (if (and (not (eq (current-buffer) buf)) | |
672 | (if only-buffers | |
673 | (he-buffer-member only-buffers) | |
674 | (not (he-buffer-member ignore-buffers)))) | |
675 | (save-excursion | |
676 | (save-restriction | |
677 | (if hippie-expand-no-restriction | |
41b8e71c RS |
678 | (widen)) |
679 | (goto-char he-search-loc) | |
c60ee5e7 | 680 | (setq expansion |
41b8e71c | 681 | (let ((case-fold-search orig-case-fold-search)) |
de825bbb | 682 | (funcall search-function he-search-string))) |
41b8e71c RS |
683 | (set-marker he-search-loc (point)) |
684 | (if (not expansion) | |
685 | (progn | |
686 | (setq he-search-bufs (cdr he-search-bufs)) | |
687 | (setq he-searched-n-bufs (1+ he-searched-n-bufs)) | |
688 | (set-marker he-search-loc 1 (car he-search-bufs)))))) | |
510cbc92 RS |
689 | (setq he-search-bufs (cdr he-search-bufs)) |
690 | (set-marker he-search-loc 1 (car he-search-bufs))))) | |
652304c9 RS |
691 | |
692 | (set-buffer buf) | |
693 | (if (not expansion) | |
694 | (progn | |
510cbc92 | 695 | (if old (he-reset-string)) |
652304c9 | 696 | ()) |
de825bbb SM |
697 | (progn |
698 | (he-substitute-string expansion t) | |
699 | t)))) | |
652304c9 | 700 | |
c60ee5e7 | 701 | (defun he-line-search (str strip-prompt reverse) |
652304c9 RS |
702 | (let ((result ())) |
703 | (while (and (not result) | |
704 | (if reverse | |
c60ee5e7 | 705 | (re-search-backward |
652304c9 RS |
706 | (he-line-search-regexp str strip-prompt) |
707 | nil t) | |
708 | (re-search-forward | |
709 | (he-line-search-regexp str strip-prompt) | |
710 | nil t))) | |
d7d20e6a GM |
711 | (setq result (buffer-substring-no-properties (match-end 1) |
712 | (match-end 0))) | |
510cbc92 RS |
713 | (if (he-string-member result he-tried-table t) |
714 | (setq result nil))) ; if already in table, ignore | |
652304c9 RS |
715 | result)) |
716 | ||
717 | (defun he-line-beg (strip-prompt) | |
718 | (save-excursion | |
c60ee5e7 | 719 | (if (re-search-backward (he-line-search-regexp "" strip-prompt) |
9b026d9f | 720 | (line-beginning-position) t) |
652304c9 | 721 | (match-beginning 2) |
652304c9 RS |
722 | (point)))) |
723 | ||
724 | (defun he-line-search-regexp (pat strip-prompt) | |
725 | (if strip-prompt | |
486eebac | 726 | (concat "\\(" comint-prompt-regexp "\\|^\\s-*\\)\\(?2:" |
652304c9 RS |
727 | (regexp-quote pat) |
728 | "[^\n]*[^ \t\n]\\)") | |
c60ee5e7 | 729 | (concat "^\\(\\s-*\\)\\(" |
652304c9 RS |
730 | (regexp-quote pat) |
731 | "[^\n]*[^ \t\n]\\)"))) | |
732 | ||
8c6677ed JB |
733 | (defun try-expand-list (old) |
734 | "Try to complete the current beginning of a list. | |
735 | The argument OLD has to be nil the first call of this function, and t | |
736 | for subsequent calls (for further possible completions of the same | |
737 | string). It returns t if a new completion is found, nil otherwise." | |
738 | (let ((expansion ())) | |
739 | (if (not old) | |
740 | (progn | |
741 | (he-init-string (he-list-beg) (point)) | |
742 | (set-marker he-search-loc he-string-beg) | |
743 | (setq he-search-bw t))) | |
744 | ||
745 | (if (not (equal he-search-string "")) | |
746 | (save-excursion | |
41b8e71c RS |
747 | (save-restriction |
748 | (if hippie-expand-no-restriction | |
749 | (widen)) | |
750 | ;; Try looking backward unless inhibited. | |
751 | (if he-search-bw | |
c60ee5e7 | 752 | (progn |
41b8e71c RS |
753 | (goto-char he-search-loc) |
754 | (setq expansion (he-list-search he-search-string t)) | |
755 | (set-marker he-search-loc (point)) | |
756 | (if (not expansion) | |
757 | (progn | |
758 | (set-marker he-search-loc he-string-end) | |
759 | (setq he-search-bw ()))))) | |
760 | ||
761 | (if (not expansion) ; Then look forward. | |
c60ee5e7 | 762 | (progn |
41b8e71c RS |
763 | (goto-char he-search-loc) |
764 | (setq expansion (he-list-search he-search-string nil)) | |
765 | (set-marker he-search-loc (point))))))) | |
8c6677ed JB |
766 | |
767 | (if (not expansion) | |
768 | (progn | |
769 | (if old (he-reset-string)) | |
770 | ()) | |
771 | (progn | |
772 | (he-substitute-string expansion t) | |
8c6677ed JB |
773 | t)))) |
774 | ||
775 | (defun try-expand-list-all-buffers (old) | |
776 | "Try to complete the current list, searching all other buffers. | |
777 | The argument OLD has to be nil the first call of this function, and t | |
778 | for subsequent calls (for further possible completions of the same | |
779 | string). It returns t if a new completion is found, nil otherwise." | |
de825bbb | 780 | (he--all-buffers old #'he-list-beg #'he-list-search)) |
8c6677ed | 781 | |
de825bbb | 782 | (defun he-list-search (str &optional reverse) |
8c6677ed | 783 | (let ((result ()) |
510cbc92 | 784 | beg pos err) |
8c6677ed JB |
785 | (while (and (not result) |
786 | (if reverse | |
787 | (search-backward str nil t) | |
788 | (search-forward str nil t))) | |
789 | (setq pos (point)) | |
790 | (setq beg (match-beginning 0)) | |
791 | (goto-char beg) | |
792 | (setq err ()) | |
793 | (condition-case () | |
510cbc92 RS |
794 | (forward-list 1) |
795 | (error (setq err t))) | |
c60ee5e7 | 796 | (if (and reverse |
510cbc92 RS |
797 | (> (point) he-string-beg)) |
798 | (setq err t)) | |
8c6677ed | 799 | (if (not err) |
510cbc92 | 800 | (progn |
41b8e71c | 801 | (setq result (buffer-substring-no-properties beg (point))) |
510cbc92 RS |
802 | (if (he-string-member result he-tried-table t) |
803 | (setq result nil)))) ; if already in table, ignore | |
8c6677ed JB |
804 | (goto-char pos)) |
805 | result)) | |
806 | ||
807 | (defun he-list-beg () | |
808 | (save-excursion | |
809 | (condition-case () | |
510cbc92 | 810 | (backward-up-list 1) |
8c6677ed JB |
811 | (error ())) |
812 | (point))) | |
813 | ||
652304c9 RS |
814 | (defun try-expand-all-abbrevs (old) |
815 | "Try to expand word before point according to all abbrev tables. | |
816 | The argument OLD has to be nil the first call of this function, and t | |
817 | for subsequent calls (for further possible expansions of the same | |
818 | string). It returns t if a new expansion is found, nil otherwise." | |
819 | (if (not old) | |
820 | (progn | |
821 | (he-init-string (he-dabbrev-beg) (point)) | |
c60ee5e7 | 822 | (setq he-expand-list |
652304c9 RS |
823 | (and (not (equal he-search-string "")) |
824 | (mapcar (function (lambda (sym) | |
510cbc92 RS |
825 | (if (and (boundp sym) (vectorp (eval sym))) |
826 | (abbrev-expansion (downcase he-search-string) | |
827 | (eval sym))))) | |
c60ee5e7 | 828 | (append '(local-abbrev-table |
652304c9 RS |
829 | global-abbrev-table) |
830 | abbrev-table-name-list)))))) | |
831 | (while (and he-expand-list | |
832 | (or (not (car he-expand-list)) | |
510cbc92 | 833 | (he-string-member (car he-expand-list) he-tried-table t))) |
652304c9 RS |
834 | (setq he-expand-list (cdr he-expand-list))) |
835 | (if (null he-expand-list) | |
836 | (progn | |
510cbc92 | 837 | (if old (he-reset-string)) |
652304c9 RS |
838 | ()) |
839 | (progn | |
8c6677ed | 840 | (he-substitute-string (car he-expand-list) t) |
652304c9 RS |
841 | (setq he-expand-list (cdr he-expand-list)) |
842 | t))) | |
843 | ||
844 | (defun try-expand-dabbrev (old) | |
845 | "Try to expand word \"dynamically\", searching the current buffer. | |
846 | The argument OLD has to be nil the first call of this function, and t | |
847 | for subsequent calls (for further possible expansions of the same | |
848 | string). It returns t if a new expansion is found, nil otherwise." | |
849 | (let ((expansion ())) | |
850 | (if (not old) | |
851 | (progn | |
852 | (he-init-string (he-dabbrev-beg) (point)) | |
8c6677ed | 853 | (set-marker he-search-loc he-string-beg) |
652304c9 RS |
854 | (setq he-search-bw t))) |
855 | ||
856 | (if (not (equal he-search-string "")) | |
857 | (save-excursion | |
41b8e71c RS |
858 | (save-restriction |
859 | (if hippie-expand-no-restriction | |
860 | (widen)) | |
861 | ;; Try looking backward unless inhibited. | |
862 | (if he-search-bw | |
c60ee5e7 | 863 | (progn |
41b8e71c RS |
864 | (goto-char he-search-loc) |
865 | (setq expansion (he-dabbrev-search he-search-string t)) | |
866 | (set-marker he-search-loc (point)) | |
867 | (if (not expansion) | |
868 | (progn | |
869 | (set-marker he-search-loc he-string-end) | |
870 | (setq he-search-bw ()))))) | |
871 | ||
872 | (if (not expansion) ; Then look forward. | |
c60ee5e7 | 873 | (progn |
41b8e71c RS |
874 | (goto-char he-search-loc) |
875 | (setq expansion (he-dabbrev-search he-search-string nil)) | |
876 | (set-marker he-search-loc (point))))))) | |
c60ee5e7 | 877 | |
652304c9 RS |
878 | (if (not expansion) |
879 | (progn | |
510cbc92 | 880 | (if old (he-reset-string)) |
652304c9 RS |
881 | ()) |
882 | (progn | |
883 | (he-substitute-string expansion t) | |
652304c9 RS |
884 | t)))) |
885 | ||
886 | (defun try-expand-dabbrev-all-buffers (old) | |
f72f0c23 | 887 | "Try to expand word \"dynamically\", searching all other buffers. |
652304c9 RS |
888 | The argument OLD has to be nil the first call of this function, and t |
889 | for subsequent calls (for further possible expansions of the same | |
890 | string). It returns t if a new expansion is found, nil otherwise." | |
de825bbb | 891 | (he--all-buffers old #'he-dabbrev-beg #'he-dabbrev-search)) |
652304c9 | 892 | |
510cbc92 RS |
893 | ;; Thanks go to Jeff Dairiki <dairiki@faraday.apl.washington.edu> who |
894 | ;; suggested this one. | |
895 | (defun try-expand-dabbrev-visible (old) | |
896 | "Try to expand word \"dynamically\", searching visible window parts. | |
897 | The argument OLD has to be nil the first call of this function, and t | |
898 | for subsequent calls (for further possible expansions of the same | |
899 | string). It returns t if a new expansion is found, nil otherwise." | |
900 | (let ((expansion ()) | |
290d5b58 | 901 | (flag (if (frame-visible-p (window-frame)) |
510cbc92 | 902 | 'visible t))) |
f72f0c23 SM |
903 | (unless old |
904 | (he-init-string (he-dabbrev-beg) (point)) | |
905 | (setq he-search-window (selected-window)) | |
906 | (set-marker he-search-loc | |
907 | (window-start he-search-window) | |
908 | (window-buffer he-search-window))) | |
510cbc92 RS |
909 | |
910 | (while (and (not (equal he-search-string "")) | |
f72f0c23 SM |
911 | (marker-position he-search-loc) |
912 | (not expansion)) | |
7fdbcd83 | 913 | (with-current-buffer (marker-buffer he-search-loc) |
f72f0c23 SM |
914 | (save-excursion |
915 | (goto-char he-search-loc) | |
916 | (setq expansion (he-dabbrev-search he-search-string () | |
917 | (window-end he-search-window))) | |
918 | (if (and expansion | |
919 | (eq (marker-buffer he-string-beg) (current-buffer)) | |
920 | (eq (marker-position he-string-beg) (match-beginning 0))) | |
921 | (setq expansion | |
922 | (he-dabbrev-search he-search-string () | |
923 | (window-end he-search-window)))) | |
924 | (set-marker he-search-loc (point) (current-buffer)))) | |
925 | (unless expansion | |
926 | (setq he-search-window (next-window he-search-window nil flag)) | |
927 | (if (eq he-search-window (selected-window)) | |
928 | (set-marker he-search-loc nil) | |
929 | (set-marker he-search-loc (window-start he-search-window) | |
930 | (window-buffer he-search-window))))) | |
c60ee5e7 | 931 | |
510cbc92 RS |
932 | (if (not expansion) |
933 | (progn | |
934 | (if old (he-reset-string)) | |
935 | ()) | |
936 | (progn | |
937 | (he-substitute-string expansion t) | |
938 | t)))) | |
652304c9 | 939 | |
510cbc92 RS |
940 | (defun he-dabbrev-search (pattern &optional reverse limit) |
941 | (let ((result ()) | |
41b8e71c RS |
942 | (regpat (cond ((not hippie-expand-dabbrev-as-symbol) |
943 | (concat "\\<" (regexp-quote pattern) "\\sw+")) | |
944 | ((eq (char-syntax (aref pattern 0)) ?_) | |
945 | (concat (regexp-quote pattern) "\\(\\sw\\|\\s_\\)+")) | |
946 | (t | |
947 | (concat "\\<" (regexp-quote pattern) | |
948 | "\\(\\sw\\|\\s_\\)+"))))) | |
c60ee5e7 | 949 | (while (and (not result) |
652304c9 | 950 | (if reverse |
510cbc92 RS |
951 | (re-search-backward regpat limit t) |
952 | (re-search-forward regpat limit t))) | |
41b8e71c RS |
953 | (setq result (buffer-substring-no-properties (match-beginning 0) |
954 | (match-end 0))) | |
955 | (if (or (and hippie-expand-dabbrev-as-symbol | |
956 | (> (match-beginning 0) (point-min)) | |
510cbc92 RS |
957 | (memq (char-syntax (char-after (1- (match-beginning 0)))) |
958 | '(?_ ?w))) | |
959 | (he-string-member result he-tried-table t)) | |
960 | (setq result nil))) ; ignore if bad prefix or already in table | |
652304c9 RS |
961 | result)) |
962 | ||
963 | (defun he-dabbrev-beg () | |
510cbc92 RS |
964 | (let ((op (point))) |
965 | (save-excursion | |
41b8e71c | 966 | (if hippie-expand-dabbrev-skip-space |
510cbc92 | 967 | (skip-syntax-backward ". ")) |
41b8e71c RS |
968 | (if (= (skip-syntax-backward (if hippie-expand-dabbrev-as-symbol |
969 | "w_" "w")) | |
970 | 0) | |
510cbc92 RS |
971 | op |
972 | (point))))) | |
973 | ||
974 | (defun try-expand-dabbrev-from-kill (old) | |
975 | "Try to expand word \"dynamically\", searching the kill ring. | |
976 | The argument OLD has to be nil the first call of this function, and t | |
977 | for subsequent calls (for further possible completions of the same | |
978 | string). It returns t if a new completion is found, nil otherwise." | |
979 | (let ((expansion ())) | |
980 | (if (not old) | |
c60ee5e7 | 981 | (progn |
510cbc92 RS |
982 | (he-init-string (he-dabbrev-beg) (point)) |
983 | (setq he-expand-list | |
984 | (if (not (equal he-search-string "")) | |
985 | kill-ring)) | |
986 | (setq he-search-loc2 0))) | |
987 | (if (not (equal he-search-string "")) | |
988 | (setq expansion (he-dabbrev-kill-search he-search-string))) | |
989 | (if (not expansion) | |
990 | (progn | |
991 | (if old (he-reset-string)) | |
992 | ()) | |
993 | (progn | |
994 | (he-substitute-string expansion t) | |
995 | t)))) | |
996 | ||
997 | (defun he-dabbrev-kill-search (pattern) | |
998 | (let ((result ()) | |
41b8e71c RS |
999 | (regpat (cond ((not hippie-expand-dabbrev-as-symbol) |
1000 | (concat "\\<" (regexp-quote pattern) "\\sw+")) | |
1001 | ((eq (char-syntax (aref pattern 0)) ?_) | |
1002 | (concat (regexp-quote pattern) "\\(\\sw\\|\\s_\\)+")) | |
1003 | (t | |
1004 | (concat "\\<" (regexp-quote pattern) | |
1005 | "\\(\\sw\\|\\s_\\)+")))) | |
510cbc92 | 1006 | (killstr (car he-expand-list))) |
c60ee5e7 | 1007 | (while (and (not result) |
510cbc92 RS |
1008 | he-expand-list) |
1009 | (while (and (not result) | |
1010 | (string-match regpat killstr he-search-loc2)) | |
1011 | (setq result (substring killstr (match-beginning 0) (match-end 0))) | |
41b8e71c | 1012 | (set-text-properties 0 (length result) () result) |
510cbc92 | 1013 | (setq he-search-loc2 (1+ (match-beginning 0))) |
41b8e71c RS |
1014 | (if (or (and hippie-expand-dabbrev-as-symbol |
1015 | (> (match-beginning 0) 0) | |
510cbc92 RS |
1016 | (memq (char-syntax (aref killstr (1- (match-beginning 0)))) |
1017 | '(?_ ?w))) | |
1018 | (he-string-member result he-tried-table t)) | |
1019 | (setq result nil))) ; ignore if bad prefix or already in table | |
c60ee5e7 | 1020 | (if (and (not result) |
510cbc92 RS |
1021 | he-expand-list) |
1022 | (progn | |
c60ee5e7 | 1023 | (setq he-expand-list (cdr he-expand-list)) |
510cbc92 RS |
1024 | (setq killstr (car he-expand-list)) |
1025 | (setq he-search-loc2 0)))) | |
1026 | result)) | |
1027 | ||
1028 | (defun try-expand-whole-kill (old) | |
1029 | "Try to complete text with something from the kill ring. | |
1030 | The argument OLD has to be nil the first call of this function, and t | |
1031 | for subsequent calls (for further possible completions of the same | |
1032 | string). It returns t if a new completion is found, nil otherwise." | |
1033 | (let ((expansion ())) | |
1034 | (if (not old) | |
c60ee5e7 | 1035 | (progn |
510cbc92 RS |
1036 | (he-init-string (he-kill-beg) (point)) |
1037 | (if (not (he-string-member he-search-string he-tried-table)) | |
1038 | (setq he-tried-table (cons he-search-string he-tried-table))) | |
c60ee5e7 | 1039 | (setq he-expand-list |
510cbc92 RS |
1040 | (if (not (equal he-search-string "")) |
1041 | kill-ring)) | |
1042 | (setq he-search-loc2 ()))) | |
1043 | (if (not (equal he-search-string "")) | |
1044 | (setq expansion (he-whole-kill-search he-search-string))) | |
1045 | (if (not expansion) | |
1046 | (progn | |
1047 | (if old (he-reset-string)) | |
1048 | ()) | |
1049 | (progn | |
1050 | (he-substitute-string expansion) | |
1051 | t)))) | |
1052 | ||
1053 | (defun he-whole-kill-search (str) | |
1054 | (let ((case-fold-search ()) | |
1055 | (result ()) | |
1056 | (str (regexp-quote str)) | |
1057 | (killstr (car he-expand-list)) | |
1058 | (pos -1)) | |
1059 | (while (and (not result) | |
1060 | he-expand-list) | |
1061 | (if (not he-search-loc2) | |
1062 | (while (setq pos (string-match str killstr (1+ pos))) | |
1063 | (setq he-search-loc2 (cons pos he-search-loc2)))) | |
1064 | (while (and (not result) | |
1065 | he-search-loc2) | |
1066 | (setq pos (car he-search-loc2)) | |
1067 | (setq he-search-loc2 (cdr he-search-loc2)) | |
1068 | (save-excursion | |
1069 | (goto-char he-string-beg) | |
1070 | (if (and (>= (- (point) pos) (point-min)) ; avoid some string GC | |
1071 | (eq (char-after (- (point) pos)) (aref killstr 0)) | |
1072 | (search-backward (substring killstr 0 pos) | |
1073 | (- (point) pos) t)) | |
41b8e71c RS |
1074 | (progn |
1075 | (setq result (substring killstr pos)) | |
1076 | (set-text-properties 0 (length result) () result)))) | |
510cbc92 RS |
1077 | (if (and result |
1078 | (he-string-member result he-tried-table)) | |
1079 | (setq result nil))) ; ignore if already in table | |
1080 | (if (and (not result) | |
1081 | he-expand-list) | |
1082 | (progn | |
c60ee5e7 | 1083 | (setq he-expand-list (cdr he-expand-list)) |
510cbc92 RS |
1084 | (setq killstr (car he-expand-list)) |
1085 | (setq pos -1)))) | |
1086 | result)) | |
1087 | ||
1088 | (defun he-kill-beg () | |
1089 | (let ((op (point))) | |
1090 | (save-excursion | |
1091 | (skip-syntax-backward "^w_") | |
1092 | (if (= (skip-syntax-backward "w_") 0) | |
1093 | op | |
1094 | (point))))) | |
1095 | ||
8c6677ed JB |
1096 | |
1097 | (provide 'hippie-exp) | |
652304c9 | 1098 | |
8c6677ed | 1099 | ;;; hippie-exp.el ends here |