comparison lisp/hippie-exp.el @ 4130:e5aee6a3bb75

* hippie-exp.el: Renamed from hippie.el. Changes from Anders Holst, to bring hippie-expand up to version 1.2: * hippie-exp.el (hippie-expand-max-buffers): New variable. (try-expand-line-all-buffers, try-expand-list-all-buffers, try-expand-dabbrev-all-buffers): Use it. (try-expand-list, try-expand-list-all-buffers): New functions. (he-string-beg, he-string-end, he-search-loc): These values are now markers, not integers. Uses changed. (he-reset-string, he-substitute-string): Use a marker to preserve the old position of point. (try-expand-all-abbrevs): handle case the same way as the usual expand-abbrev (which is not a very good way, but for consistency...). (he-dabbrev-beg): Use `skip-syntax' instead of `skip-chars', to adjust its behavior to different modes. (hippie-expand): Don't messages which try function it is using, when the expansion itself is done in the minibuffer (it was very annoying to have the message obscuring the expansion). (try-complete-file-name, try-complete-file-name-partially, try-complete-lisp-symbol, try-complete-lisp-symbol-partially, try-expand-line, try-expand-line-all-buffers, try-expand-all-abbrevs, try-expand-dabbrev, try-expand-dabbrev-all-buffers): No unnecessary "resetting" of the epansion, when no expansion is done (caused the buffer to be marked as changed, although nothing was done, among other things). (he-reset-string): Undoing of last expansion at a later occasion, now undoes correctly (before, it garbled things up). (make-hippie-expand-function): now uses "(function ...)" instead of "'" (matters for compilation). (try-expand-line, try-expand-line-all-buffers, he-line-search-regexp): uses `comint-prompt-regexp' instead of `shell-prompt-pattern', to strip off prompt in process buffers.
author Jim Blandy <jimb@redhat.com>
date Sun, 18 Jul 1993 06:20:15 +0000
parents fb0ed5a1d0f3
children 81b21ef5290d
comparison
equal deleted inserted replaced
4129:6be09b68817f 4130:e5aee6a3bb75
1 ;;; hippie.el --- expand a word trying various ways to find its expansion. 1 ;;; hippie-exp.el --- expand text trying various ways to find its expansion.
2 2
3 ;; Author: Anders Holst <aho@sans.kth.se> 3 ;; Author: Anders Holst <aho@sans.kth.se>
4 ;; Last change: 22 June 1993
5 ;; Version: 1.2
4 ;; Keywords: extensions 6 ;; Keywords: extensions
5 7
6 ;; Copyright (C) 1992 Free Software Foundation, Inc. 8 ;; Copyright (C) 1992 Free Software Foundation, Inc.
7 ;; 9 ;;
8 ;; This file is part of GNU Emacs. 10 ;; This file is part of GNU Emacs.
18 ;; GNU General Public License for more details. 20 ;; GNU General Public License for more details.
19 21
20 ;; You should have received a copy of the GNU General Public License 22 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to 23 ;; along with GNU Emacs; see the file COPYING. If not, write to
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 24 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23 ;;
24 ;; Last change: 4 January 1993
25 25
26 ;;; Commentary: 26 ;;; Commentary:
27 ;; 27 ;;
28 ;; `hippie-expand' is a single function for a lot of different kinds 28 ;; `hippie-expand' is a single function for a lot of different kinds
29 ;; of completions and expansions. Called repeatedly it tries all 29 ;; of completions and expansions. Called repeatedly it tries all
38 ;; completion. 38 ;; completion.
39 ;; If the variable `hippie-expand-verbose' is non-nil, `hippie-expand' 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 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 41 ;; currently (ie. was used currently and will be tried first the next
42 ;; time). 42 ;; time).
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".
43 ;; See also the macro `make-hippie-expand-function' below. 46 ;; See also the macro `make-hippie-expand-function' below.
44 ;; 47 ;;
45 ;; A short description of the current try-functions in this file: 48 ;; A short description of the current try-functions in this file:
46 ;; `try-complete-file-name' : very convenient to have in any buffer, 49 ;; `try-complete-file-name' : very convenient to have in any buffer,
47 ;; and not just in the minibuffer or (some) shell-mode. It goes 50 ;; and not just in the minibuffer or (some) shell-mode. It goes
48 ;; through all possible completions instead of just completing as 51 ;; through all possible completions instead of just completing as
49 ;; much as is unique. 52 ;; much as is unique.
50 ;; `try-complete-file-name-partially' : To insert in the list just 53 ;; `try-complete-file-name-partially' : To insert in the list just
51 ;; before `try-complete-file-name' for those who want first to get 54 ;; before `try-complete-file-name' for those who want first to get
52 ;; a file name completed only as many characters as is unique. 55 ;; a file name completed only as many characters as is unique.
53 ;; (NOTE: Not by default in `hippie-expand-try-functions-list'.)
54 ;; `try-expand-all-abbrevs' : can be removed if you don't use abbrevs. 56 ;; `try-expand-all-abbrevs' : can be removed if you don't use abbrevs.
55 ;; Otherwise it looks through all abbrev-tables, starting with 57 ;; Otherwise it looks through all abbrev-tables, starting with
56 ;; the local followed by the global. 58 ;; the local followed by the global.
57 ;; `try-expand-line' : Searches the buffer for an entire line that 59 ;; `try-expand-line' : Searches the buffer for an entire line that
58 ;; begins exactly as the current line. Convenient sometimes, for 60 ;; begins exactly as the current line. Convenient sometimes, for
59 ;; example as a substitute for (or complement to) the history 61 ;; example as a substitute for (or complement to) the history
60 ;; list in shell-like buffers. Remove it if you find it confusing. 62 ;; list in shell-like buffers. At other times, only confusing.
61 ;; `try-expand-line-all-buffers' : Like `try-expand-line' but searches 63 ;; `try-expand-line-all-buffers' : Like `try-expand-line' but searches
62 ;; in all buffers (except the current). (This may be a little 64 ;; in all buffers (except the current). (This may be a little
63 ;; slow, don't use it unless you are really fond of `hippie-expand'. 65 ;; slow, don't use it unless you are really fond of `hippie-expand'.)
64 ;; NOTE: Not by default in hippie-expand-try-functions-list.) 66 ;; `try-expand-list' : Tries to expand the text back to the nearest
67 ;; open delimiter, to a whole list from the buffer. Convenient for
68 ;; example when writing lisp or TeX.
69 ;; `try-expand-list-all-buffers' : Like `try-expand-list' but searches
70 ;; in all buffers (except the current).
65 ;; `try-expand-dabbrev' : works exactly as dabbrev-expand (but of 71 ;; `try-expand-dabbrev' : works exactly as dabbrev-expand (but of
66 ;; course in a way compatible with the other try-functions). 72 ;; course in a way compatible with the other try-functions).
67 ;; `try-expand-dabbrev-all-buffers' : perhaps the most useful of them, 73 ;; `try-expand-dabbrev-all-buffers' : perhaps the most useful of them,
68 ;; like `dabbrev-expand' but searches all Emacs buffers (except the 74 ;; like `dabbrev-expand' but searches all Emacs buffers (except the
69 ;; current) for matching words. (No, I don't find this one 75 ;; current) for matching words. (No, I don't find this one
73 ;; Might be tedious (usually a lot of possible completions) and 79 ;; Might be tedious (usually a lot of possible completions) and
74 ;; since its function is much like `lisp-complete-symbol', which 80 ;; since its function is much like `lisp-complete-symbol', which
75 ;; already has a key of its own, you might want to remove this. 81 ;; already has a key of its own, you might want to remove this.
76 ;; `try-complete-lisp-symbol-partially' : To insert in the list just 82 ;; `try-complete-lisp-symbol-partially' : To insert in the list just
77 ;; before `try-complete-lisp-symbol' for those who first want to get 83 ;; before `try-complete-lisp-symbol' for those who first want to get
78 ;; completion of what is unique in the name. (NOTE: Not by 84 ;; completion of what is unique in the name.
79 ;; default in hippie-expand-try-functions-list.) 85 ;;
86 ;; Not all of the above functions are by default in
87 ;; `hippie-expand-try-functions-list'. This variable is better set
88 ;; in ".emacs" to make `hippie-expand' behave maximally convenient
89 ;; according to personal taste. Also, instead of loading the
90 ;; variable with all kinds of try-functions above, it might be an
91 ;; idea to use `make-hippie-expand-function' to construct different
92 ;; `hippie-expand'-like functions, with different try-lists and bound
93 ;; to different keys. It is also possible to make
94 ;; `hippie-expand-try-functions-list' a buffer local variable, and
95 ;; let it depend on the mode (by setting it in the mode-hooks).
80 ;; 96 ;;
81 ;; To write new try-functions, consider the following: 97 ;; To write new try-functions, consider the following:
82 ;; Each try-function takes one argument OLD which is nil the first 98 ;; Each try-function takes one argument OLD which is nil the first
83 ;; time the function is called and true in succeeding calls for the 99 ;; time the function is called and true in succeeding calls for the
84 ;; same string to complete. The first time the function has to 100 ;; same string to complete. The first time the function has to
102 ;; There is also a variable: `he-tried-table' which is meant to contain 118 ;; There is also a variable: `he-tried-table' which is meant to contain
103 ;; all tried expansions so far. The try-function can check this 119 ;; all tried expansions so far. The try-function can check this
104 ;; variable to see whether an expansion has already been tried 120 ;; variable to see whether an expansion has already been tried
105 ;; (hint: `he-string-member'), and add its own tried expansions to it. 121 ;; (hint: `he-string-member'), and add its own tried expansions to it.
106 ;; 122 ;;
107 ;; 123 ;; Known bugs
108 ;; KNOWN BUGS
109 ;; 124 ;;
110 ;; It may happen that some completion suggestion occurs twice, in 125 ;; It may happen that some completion suggestion occurs twice, in
111 ;; spite of the use of `he-tried-table' to prevent that. This is 126 ;; spite of the use of `he-tried-table' to prevent that. This is
112 ;; because different try-functions may try to complete different 127 ;; because different try-functions may try to complete different
113 ;; lengths of text, and thus put different amounts of the 128 ;; lengths of text, and thus put different amounts of the
114 ;; text in `he-try-table'. Anyway this seems to occur seldom enough not 129 ;; text in `he-try-table'. Anyway this seems to occur seldom enough not
115 ;; to be too disturbing. Also it should NOT bee possible for the 130 ;; to be too disturbing. Also it should NOT bee possible for the
116 ;; opposite situation to occur, that `hippie-expand' misses some 131 ;; opposite situation to occur, that `hippie-expand' misses some
117 ;; suggestion because it thinks it has already tried it. 132 ;; suggestion because it thinks it has already tried it.
118 ;; 133 ;;
119 ;; 134 ;; Acknowledgement
120 ;; ACKNOWLEDGEMENT
121 ;; 135 ;;
122 ;; I want to thank Mikael Djurfeldt in discussions with whom the idea 136 ;; I want to thank Mikael Djurfeldt in discussions with whom the idea
123 ;; of this function took form. 137 ;; of this function took form.
124 ;; I am also grateful to all those who have given me suggestions on 138 ;; I am also grateful to all those who have given me suggestions on
125 ;; how to improve it. 139 ;; how to improve it.
127 141
128 ;;; Code: 142 ;;; Code:
129 143
130 (defvar he-num -1) 144 (defvar he-num -1)
131 145
132 (defvar he-string-beg ()) 146 (defvar he-string-beg (make-marker))
133 147
134 (defvar he-string-end ()) 148 (defvar he-string-end (make-marker))
135 149
136 (defvar he-search-string ()) 150 (defvar he-search-string ())
137 151
138 (defvar he-expand-list ()) 152 (defvar he-expand-list ())
139 153
140 (defvar he-tried-table ()) 154 (defvar he-tried-table ())
141 155
142 (defvar he-search-loc ()) 156 (defvar he-search-loc (make-marker))
143 157
144 (defvar he-search-bw ()) 158 (defvar he-search-bw ())
145 159
146 (defvar he-search-bufs ()) 160 (defvar he-search-bufs ())
147 161
162 (defvar he-searched-n-bufs ())
163
164 ;;;###autoload
148 (defvar hippie-expand-try-functions-list '(try-complete-file-name 165 (defvar hippie-expand-try-functions-list '(try-complete-file-name
149 try-expand-all-abbrevs 166 try-expand-all-abbrevs
150 try-expand-line 167 try-expand-line
151 try-expand-dabbrev 168 try-expand-dabbrev
152 try-expand-dabbrev-all-buffers 169 try-expand-dabbrev-all-buffers
153 try-complete-lisp-symbol) 170 try-complete-lisp-symbol)
154 "The list of expansion functions tried in order by `hippie-expand'. 171 "The list of expansion functions tried in order by `hippie-expand'.
155 To change the behavior of `hippie-expand', remove, change the order of, 172 To change the behavior of `hippie-expand', remove, change the order of,
156 or insert functions in this list.") 173 or insert functions in this list.")
157 174
175 ;;;###autoload
158 (defvar hippie-expand-verbose t 176 (defvar hippie-expand-verbose t
159 "*Non-nil makes `hippie-expand' output which function it is trying.") 177 "*Non-nil makes `hippie-expand' output which function it is trying.")
160 178
179 ;;;###autoload
180 (defvar hippie-expand-max-buffers ()
181 "*The maximum number of buffers (apart from the current) searched.
182 If nil, all buffers are searched.")
183
184 ;;;###autoload
161 (defun hippie-expand (arg) 185 (defun hippie-expand (arg)
162 "Try to expand text before point, using multiple methods. 186 "Try to expand text before point, using multiple methods.
163 The expansion functions in `hippie-expand-try-functions-list' are 187 The expansion functions in `hippie-expand-try-functions-list' are
164 tried in order, until a possible expansion is found. Repeated 188 tried in order, until a possible expansion is found. Repeated
165 application of `hippie-expand' inserts successively possible 189 application of `hippie-expand' inserts successively possible
190 (setq he-num -1) 214 (setq he-num -1)
191 (if first 215 (if first
192 (message "No expansion found") 216 (message "No expansion found")
193 (message "No further expansions found")) 217 (message "No further expansions found"))
194 (ding)) 218 (ding))
195 (if hippie-expand-verbose 219 (if (and hippie-expand-verbose
220 (not (window-minibuffer-p (selected-window))))
196 (message (concat "Using " 221 (message (concat "Using "
197 (prin1-to-string (nth he-num 222 (prin1-to-string (nth he-num
198 hippie-expand-try-functions-list))))))) 223 hippie-expand-try-functions-list)))))))
199 (if (>= he-num 0) 224 (if (>= he-num 0)
200 (progn 225 (progn
201 (setq he-num -1) 226 (setq he-num -1)
202 (he-reset-string) 227 (he-reset-string)
203 (if hippie-expand-verbose 228 (if (and hippie-expand-verbose
229 (not (window-minibuffer-p (selected-window))))
204 (message "Undoing expansions")))))) 230 (message "Undoing expansions"))))))
205 231
206 ;; Initializes the region to expand (to between BEG and END). 232 ;; Initializes the region to expand (to between BEG and END).
207 (defun he-init-string (beg end) 233 (defun he-init-string (beg end)
208 (setq he-string-beg beg) 234 (set-marker he-string-beg beg)
209 (setq he-string-end end) 235 (set-marker he-string-end end)
210 (setq he-search-string (buffer-substring beg end))) 236 (setq he-search-string (buffer-substring beg end)))
211 237
212 ;; Resets the expanded region to its original contents. 238 ;; Resets the expanded region to its original contents.
213 (defun he-reset-string () 239 (defun he-reset-string ()
214 (delete-region he-string-beg he-string-end) 240 (let ((newpos (point-marker)))
215 (insert he-search-string) 241 (delete-region he-string-beg he-string-end)
216 (setq he-string-end (point))) 242 (goto-char he-string-beg)
243 (insert he-search-string)
244 (set-marker he-string-end (point))
245 (if (= newpos he-string-beg)
246 (goto-char he-string-end)
247 (goto-char newpos))))
217 248
218 ;; Substitutes an expansion STR into the correct region (the region 249 ;; Substitutes an expansion STR into the correct region (the region
219 ;; initialized with `he-init-string'). 250 ;; initialized with `he-init-string').
220 ;; An optional argument TRANS-CASE means that it is ok to transfer case 251 ;; An optional argument TRANS-CASE means that it is ok to transfer case
221 ;; from the abbreviation to the expansion if that is possible, and is 252 ;; from the abbreviation to the expansion if that is possible, and is
222 ;; enabled in the buffer. 253 ;; enabled in the buffer.
223 (defun he-substitute-string (str &optional trans-case) 254 (defun he-substitute-string (str &optional trans-case)
224 (let ((trans-case (and trans-case 255 (let ((trans-case (and trans-case
225 case-replace 256 case-replace
226 case-fold-search 257 case-fold-search
227 (he-transfer-case-ok str he-search-string)))) 258 (he-transfer-case-ok str he-search-string)))
259 (newpos (point-marker)))
228 (he-reset-string) 260 (he-reset-string)
229 (goto-char he-string-beg) 261 (goto-char he-string-beg)
230 (search-forward he-search-string) 262 (search-forward he-search-string)
231 (replace-match (if trans-case (downcase str) str) 263 (replace-match (if trans-case (downcase str) str)
232 (not trans-case) 264 (not trans-case)
233 'literal) 265 'literal)
234 (setq he-string-end (point)))) 266 (set-marker he-string-end (point))
267 (if (= newpos he-string-beg)
268 (goto-char he-string-end)
269 (goto-char newpos))))
235 270
236 (defun he-ordinary-case-p (str) 271 (defun he-ordinary-case-p (str)
237 (or (string= str (downcase str)) 272 (or (string= str (downcase str))
238 (string= str (upcase str)) 273 (string= str (upcase str))
239 (string= str (capitalize str)))) 274 (string= str (capitalize str))))
240 275
241 (defun he-transfer-case-ok (to-str from-str) 276 (defun he-transfer-case-ok (to-str from-str)
242 (and (not (string= from-str (substring to-str 0 (length from-str)))) 277 (and (not (string= from-str (substring to-str 0 (min (length from-str)
278 (length to-str)))))
243 ;; otherwise transfer is not needed (and this also solves 279 ;; otherwise transfer is not needed (and this also solves
244 ;; some obscure situations) 280 ;; some obscure situations)
245 (he-ordinary-case-p to-str) 281 (he-ordinary-case-p to-str)
246 ;; otherwise case may be significant 282 ;; otherwise case may be significant
247 (he-ordinary-case-p from-str) 283 (he-ordinary-case-p from-str)
268 ;; try-complete-file-name))) 304 ;; try-complete-file-name)))
269 ;; (fset 'my-complete-line (make-hippie-expand-function 305 ;; (fset 'my-complete-line (make-hippie-expand-function
270 ;; '(try-expand-line 306 ;; '(try-expand-line
271 ;; try-expand-line-all-buffers))) 307 ;; try-expand-line-all-buffers)))
272 ;; 308 ;;
309 ;;;###autoload
273 (defmacro make-hippie-expand-function (try-list &optional verbose) 310 (defmacro make-hippie-expand-function (try-list &optional verbose)
274 "Construct a function similar to `hippie-expand'. 311 "Construct a function similar to `hippie-expand'.
275 Make it use the expansion functions in TRY-LIST. An optional second 312 Make it use the expansion functions in TRY-LIST. An optional second
276 argument VERBOSE non-nil makes the function verbose." 313 argument VERBOSE non-nil makes the function verbose."
277 (` '(lambda (arg) 314 (` (function (lambda (arg)
278 (, (concat 315 (, (concat
279 "Try to expand text before point, using the following functions: \n" 316 "Try to expand text before point, using the following functions: \n"
280 (mapconcat 'prin1-to-string (eval try-list) ", "))) 317 (mapconcat 'prin1-to-string (eval try-list) ", ")))
281 (interactive "P") 318 (interactive "P")
282 (let ((hippie-expand-try-functions-list (, try-list)) 319 (let ((hippie-expand-try-functions-list (, try-list))
283 (hippie-expand-verbose (, verbose))) 320 (hippie-expand-verbose (, verbose)))
284 (hippie-expand arg))))) 321 (hippie-expand arg))))))
285 322
286 323
287 ;;; Here follows the try-functions and their requisites: 324 ;;; Here follows the try-functions and their requisites:
288 325
289 (defun try-complete-file-name (old) 326 (defun try-complete-file-name (old)
310 (while (and he-expand-list 347 (while (and he-expand-list
311 (he-string-member (car he-expand-list) he-tried-table)) 348 (he-string-member (car he-expand-list) he-tried-table))
312 (setq he-expand-list (cdr he-expand-list))) 349 (setq he-expand-list (cdr he-expand-list)))
313 (if (null he-expand-list) 350 (if (null he-expand-list)
314 (progn 351 (progn
315 (he-reset-string) 352 (if old (he-reset-string))
316 ()) 353 ())
317 (let ((filename (concat (file-name-directory he-search-string) 354 (let ((filename (concat (file-name-directory he-search-string)
318 (car he-expand-list)))) 355 (car he-expand-list))))
319 (he-substitute-string filename) 356 (he-substitute-string filename)
320 (setq he-tried-table (cons (car he-expand-list) he-tried-table)) 357 (setq he-tried-table (cons (car he-expand-list) he-tried-table))
341 (string= expansion name-part)) 378 (string= expansion name-part))
342 (setq expansion ()))))) 379 (setq expansion ())))))
343 380
344 (if (not expansion) 381 (if (not expansion)
345 (progn 382 (progn
346 (he-reset-string) 383 (if old (he-reset-string))
347 ()) 384 ())
348 (let ((filename (concat (file-name-directory he-search-string) 385 (let ((filename (concat (file-name-directory he-search-string)
349 expansion))) 386 expansion)))
350 (he-substitute-string filename) 387 (he-substitute-string filename)
351 (setq he-tried-table (cons expansion he-tried-table)) 388 (setq he-tried-table (cons expansion he-tried-table))
378 (while (and he-expand-list 415 (while (and he-expand-list
379 (he-string-member (car he-expand-list) he-tried-table)) 416 (he-string-member (car he-expand-list) he-tried-table))
380 (setq he-expand-list (cdr he-expand-list))) 417 (setq he-expand-list (cdr he-expand-list)))
381 (if (null he-expand-list) 418 (if (null he-expand-list)
382 (progn 419 (progn
383 (he-reset-string) 420 (if old (he-reset-string))
384 ()) 421 ())
385 (progn 422 (progn
386 (he-substitute-string (car he-expand-list)) 423 (he-substitute-string (car he-expand-list))
387 (setq he-tried-table (cons (car he-expand-list) he-tried-table)) 424 (setq he-tried-table (cons (car he-expand-list) he-tried-table))
388 (setq he-expand-list (cdr he-expand-list)) 425 (setq he-expand-list (cdr he-expand-list))
408 (string= expansion he-search-string)) 445 (string= expansion he-search-string))
409 (setq expansion ())))) 446 (setq expansion ()))))
410 447
411 (if (not expansion) 448 (if (not expansion)
412 (progn 449 (progn
413 (he-reset-string) 450 (if old (he-reset-string))
414 ()) 451 ())
415 (progn 452 (progn
416 (he-substitute-string expansion) 453 (he-substitute-string expansion)
417 (setq he-tried-table (cons expansion he-tried-table)) 454 (setq he-tried-table (cons expansion he-tried-table))
418 t)))) 455 t))))
428 The argument OLD has to be nil the first call of this function, and t 465 The argument OLD has to be nil the first call of this function, and t
429 for subsequent calls (for further possible completions of the same 466 for subsequent calls (for further possible completions of the same
430 string). It returns t if a new completion is found, nil otherwise." 467 string). It returns t if a new completion is found, nil otherwise."
431 (let ((expansion ()) 468 (let ((expansion ())
432 (strip-prompt (and (get-buffer-process (current-buffer)) 469 (strip-prompt (and (get-buffer-process (current-buffer))
433 shell-prompt-pattern))) 470 comint-prompt-regexp)))
434 (if (not old) 471 (if (not old)
435 (progn 472 (progn
436 (he-init-string (he-line-beg strip-prompt) (point)) 473 (he-init-string (he-line-beg strip-prompt) (point))
437 (setq he-search-loc he-string-beg) 474 (set-marker he-search-loc he-string-beg)
438 (setq he-search-bw t))) 475 (setq he-search-bw t)))
439 476
440 (if (not (equal he-search-string "")) 477 (if (not (equal he-search-string ""))
441 (save-excursion 478 (save-excursion
442 ;; Try looking backward unless inhibited. 479 ;; Try looking backward unless inhibited.
443 (if he-search-bw 480 (if he-search-bw
444 (progn 481 (progn
445 (goto-char he-search-loc) 482 (goto-char he-search-loc)
446 (setq expansion (he-line-search he-search-string 483 (setq expansion (he-line-search he-search-string
447 strip-prompt t)) 484 strip-prompt t))
448 (setq he-search-loc (point-marker)) 485 (set-marker he-search-loc (point))
449 (if (not expansion) 486 (if (not expansion)
450 (progn 487 (progn
451 (setq he-search-loc he-string-end) 488 (set-marker he-search-loc he-string-end)
452 (setq he-search-bw ()))))) 489 (setq he-search-bw ())))))
453 490
454 (if (not expansion) ; Then look forward. 491 (if (not expansion) ; Then look forward.
455 (progn 492 (progn
456 (goto-char he-search-loc) 493 (goto-char he-search-loc)
457 (setq expansion (he-line-search he-search-string 494 (setq expansion (he-line-search he-search-string
458 strip-prompt nil)) 495 strip-prompt nil))
459 (setq he-search-loc (point-marker)))))) 496 (set-marker he-search-loc (point))))))
460 497
461 (if (not expansion) 498 (if (not expansion)
462 (progn 499 (progn
463 (he-reset-string) 500 (if old (he-reset-string))
464 ()) 501 ())
465 (progn 502 (progn
466 (he-substitute-string expansion t) 503 (he-substitute-string expansion t)
467 (setq he-tried-table (cons expansion he-tried-table)) 504 (setq he-tried-table (cons expansion he-tried-table))
468 t)))) 505 t))))
472 The argument OLD has to be nil the first call of this function, and t 509 The argument OLD has to be nil the first call of this function, and t
473 for subsequent calls (for further possible completions of the same 510 for subsequent calls (for further possible completions of the same
474 string). It returns t if a new completion is found, nil otherwise." 511 string). It returns t if a new completion is found, nil otherwise."
475 (let ((expansion ()) 512 (let ((expansion ())
476 (strip-prompt (and (get-buffer-process (current-buffer)) 513 (strip-prompt (and (get-buffer-process (current-buffer))
477 shell-prompt-pattern)) 514 comint-prompt-regexp))
478 (buf (current-buffer))) 515 (buf (current-buffer)))
479 (if (not old) 516 (if (not old)
480 (progn 517 (progn
481 (he-init-string (he-line-beg strip-prompt) (point)) 518 (he-init-string (he-line-beg strip-prompt) (point))
482 (setq he-search-loc 0) 519 (setq he-search-bufs (buffer-list))
483 (setq he-search-bufs (buffer-list)))) 520 (setq he-searched-n-bufs 0)
521 (set-marker he-search-loc 1 (car he-search-bufs))))
484 522
485 (if (not (equal he-search-string "")) 523 (if (not (equal he-search-string ""))
486 (while (and he-search-bufs (not expansion)) 524 (while (and he-search-bufs
525 (not expansion)
526 (or (not hippie-expand-max-buffers)
527 (< he-searched-n-bufs hippie-expand-max-buffers)))
487 (set-buffer (car he-search-bufs)) 528 (set-buffer (car he-search-bufs))
488 (if (and (not (eq (current-buffer) buf)) 529 (if (and (not (eq (current-buffer) buf))
530 (not (string-match " \\*Minibuf-[0-9]+\\*"
531 (buffer-name (current-buffer))))
489 (not (eq major-mode 'dired-mode))) 532 (not (eq major-mode 'dired-mode)))
490 ;; dont search dired buffers 533 ;; Dont search minibuffers nor dired buffers
491 (save-excursion 534 (save-excursion
492 (goto-char he-search-loc) 535 (goto-char he-search-loc)
536 (setq strip-prompt (and (get-buffer-process (current-buffer))
537 comint-prompt-regexp))
493 (setq expansion (he-line-search he-search-string 538 (setq expansion (he-line-search he-search-string
494 strip-prompt nil)) 539 strip-prompt nil))
495 (setq he-search-loc (point-marker)))) 540 (set-marker he-search-loc (point))
496 (if expansion 541 (if expansion
497 (setq he-tried-table (cons expansion he-tried-table)) 542 (setq he-tried-table (cons expansion he-tried-table))
498 (progn 543 (setq he-search-bufs (cdr he-search-bufs))
499 (setq he-search-loc 0) 544 (setq he-searched-n-bufs (1+ he-searched-n-bufs))
500 (setq he-search-bufs (cdr he-search-bufs)))))) 545 (set-marker he-search-loc 1 (car he-search-bufs))))
546 (setq he-search-bufs (cdr he-search-bufs))
547 (set-marker he-search-loc 1 (car he-search-bufs)))))
501 548
502 (set-buffer buf) 549 (set-buffer buf)
503 (if (not expansion) 550 (if (not expansion)
504 (progn 551 (progn
505 (he-reset-string) 552 (if old (he-reset-string))
506 ()) 553 ())
507 (progn 554 (progn
508 (he-substitute-string expansion t) 555 (he-substitute-string expansion t)
509 t)))) 556 t))))
510 557
533 (beginning-of-line) 580 (beginning-of-line)
534 (point)))) 581 (point))))
535 582
536 (defun he-line-search-regexp (pat strip-prompt) 583 (defun he-line-search-regexp (pat strip-prompt)
537 (if strip-prompt 584 (if strip-prompt
538 (concat "\\(" shell-prompt-pattern "\\|^\\s-*\\)\\(" 585 (concat "\\(" comint-prompt-regexp "\\|^\\s-*\\)\\("
539 (regexp-quote pat) 586 (regexp-quote pat)
540 "[^\n]*[^ \t\n]\\)") 587 "[^\n]*[^ \t\n]\\)")
541 (concat "^\\(\\s-*\\)\\(" 588 (concat "^\\(\\s-*\\)\\("
542 (regexp-quote pat) 589 (regexp-quote pat)
543 "[^\n]*[^ \t\n]\\)"))) 590 "[^\n]*[^ \t\n]\\)")))
591
592 (defun try-expand-list (old)
593 "Try to complete the current beginning of a list.
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 (if (not old)
599 (progn
600 (he-init-string (he-list-beg) (point))
601 (set-marker he-search-loc he-string-beg)
602 (setq he-search-bw t)))
603
604 (if (not (equal he-search-string ""))
605 (save-excursion
606 ;; Try looking backward unless inhibited.
607 (if he-search-bw
608 (progn
609 (goto-char he-search-loc)
610 (setq expansion (he-list-search he-search-string t))
611 (set-marker he-search-loc (point))
612 (if (not expansion)
613 (progn
614 (set-marker he-search-loc he-string-end)
615 (setq he-search-bw ())))))
616
617 (if (not expansion) ; Then look forward.
618 (progn
619 (goto-char he-search-loc)
620 (setq expansion (he-list-search he-search-string nil))
621 (set-marker he-search-loc (point))))))
622
623 (if (not expansion)
624 (progn
625 (if old (he-reset-string))
626 ())
627 (progn
628 (he-substitute-string expansion t)
629 (setq he-tried-table (cons expansion he-tried-table))
630 t))))
631
632 (defun try-expand-list-all-buffers (old)
633 "Try to complete the current list, searching all other buffers.
634 The argument OLD has to be nil the first call of this function, and t
635 for subsequent calls (for further possible completions of the same
636 string). It returns t if a new completion is found, nil otherwise."
637 (let ((expansion ())
638 (buf (current-buffer)))
639 (if (not old)
640 (progn
641 (he-init-string (he-list-beg) (point))
642 (setq he-search-bufs (buffer-list))
643 (setq he-searched-n-bufs 0)
644 (set-marker he-search-loc 1 (car he-search-bufs))))
645
646 (if (not (equal he-search-string ""))
647 (while (and he-search-bufs
648 (not expansion)
649 (or (not hippie-expand-max-buffers)
650 (< he-searched-n-bufs hippie-expand-max-buffers)))
651 (set-buffer (car he-search-bufs))
652 (if (and (not (eq (current-buffer) buf))
653 (not (string-match " \\*Minibuf-[0-9]+\\*"
654 (buffer-name (current-buffer))))
655 (not (eq major-mode 'dired-mode)))
656 ;; Dont search minibuffers nor dired buffers
657 (save-excursion
658 (goto-char he-search-loc)
659 (setq expansion (he-list-search he-search-string nil))
660 (set-marker he-search-loc (point))
661 (if expansion
662 (setq he-tried-table (cons expansion he-tried-table))
663 (setq he-search-bufs (cdr he-search-bufs))
664 (setq he-searched-n-bufs (1+ he-searched-n-bufs))
665 (set-marker he-search-loc 1 (car he-search-bufs))))
666 (setq he-search-bufs (cdr he-search-bufs))
667 (set-marker he-search-loc 1 (car he-search-bufs)))))
668
669 (set-buffer buf)
670 (if (not expansion)
671 (progn
672 (if old (he-reset-string))
673 ())
674 (progn
675 (he-substitute-string expansion t)
676 t))))
677
678 (defun he-list-search (str reverse)
679 (let ((result ())
680 beg pos err)
681 (while (and (not result)
682 (if reverse
683 (search-backward str nil t)
684 (search-forward str nil t)))
685 (setq pos (point))
686 (setq beg (match-beginning 0))
687 (goto-char beg)
688 (setq err ())
689 (condition-case ()
690 (forward-list 1)
691 (error (setq err t)))
692 (if (not err)
693 (progn
694 (setq result (buffer-substring beg (point)))
695 (if (he-string-member result he-tried-table)
696 (setq result nil)))) ; if already in table, ignore
697 (goto-char pos))
698 result))
699
700 (defun he-list-beg ()
701 (save-excursion
702 (condition-case ()
703 (backward-up-list 1)
704 (error ()))
705 (point)))
544 706
545 (defun try-expand-all-abbrevs (old) 707 (defun try-expand-all-abbrevs (old)
546 "Try to expand word before point according to all abbrev tables. 708 "Try to expand word before point according to all abbrev tables.
547 The argument OLD has to be nil the first call of this function, and t 709 The argument OLD has to be nil the first call of this function, and t
548 for subsequent calls (for further possible expansions of the same 710 for subsequent calls (for further possible expansions of the same
551 (progn 713 (progn
552 (he-init-string (he-dabbrev-beg) (point)) 714 (he-init-string (he-dabbrev-beg) (point))
553 (setq he-expand-list 715 (setq he-expand-list
554 (and (not (equal he-search-string "")) 716 (and (not (equal he-search-string ""))
555 (mapcar (function (lambda (sym) 717 (mapcar (function (lambda (sym)
556 (abbrev-expansion he-search-string 718 (abbrev-expansion (downcase he-search-string)
557 (eval sym)))) 719 (eval sym))))
558 (append '(local-abbrev-table 720 (append '(local-abbrev-table
559 global-abbrev-table) 721 global-abbrev-table)
560 abbrev-table-name-list)))))) 722 abbrev-table-name-list))))))
561 (while (and he-expand-list 723 (while (and he-expand-list
562 (or (not (car he-expand-list)) 724 (or (not (car he-expand-list))
563 (he-string-member (car he-expand-list) he-tried-table))) 725 (he-string-member (car he-expand-list) he-tried-table)))
564 (setq he-expand-list (cdr he-expand-list))) 726 (setq he-expand-list (cdr he-expand-list)))
565 (if (null he-expand-list) 727 (if (null he-expand-list)
566 (progn 728 (progn
567 (he-reset-string) 729 (if old (he-reset-string))
568 ()) 730 ())
569 (progn 731 (progn
570 (he-substitute-string (car he-expand-list)) 732 (he-substitute-string (car he-expand-list) t)
571 (setq he-tried-table (cons (car he-expand-list) he-tried-table)) 733 (setq he-tried-table (cons (car he-expand-list) he-tried-table))
572 (setq he-expand-list (cdr he-expand-list)) 734 (setq he-expand-list (cdr he-expand-list))
573 t))) 735 t)))
574 736
575 (defun try-expand-dabbrev (old) 737 (defun try-expand-dabbrev (old)
579 string). It returns t if a new expansion is found, nil otherwise." 741 string). It returns t if a new expansion is found, nil otherwise."
580 (let ((expansion ())) 742 (let ((expansion ()))
581 (if (not old) 743 (if (not old)
582 (progn 744 (progn
583 (he-init-string (he-dabbrev-beg) (point)) 745 (he-init-string (he-dabbrev-beg) (point))
584 (setq he-search-loc he-string-beg) 746 (set-marker he-search-loc he-string-beg)
585 (setq he-search-bw t))) 747 (setq he-search-bw t)))
586 748
587 (if (not (equal he-search-string "")) 749 (if (not (equal he-search-string ""))
588 (save-excursion 750 (save-excursion
589 ;; Try looking backward unless inhibited. 751 ;; Try looking backward unless inhibited.
590 (if he-search-bw 752 (if he-search-bw
591 (progn 753 (progn
592 (goto-char he-search-loc) 754 (goto-char he-search-loc)
593 (setq expansion (he-dab-search he-search-string t)) 755 (setq expansion (he-dab-search he-search-string t))
594 (setq he-search-loc (point-marker)) 756 (set-marker he-search-loc (point))
595 (if (not expansion) 757 (if (not expansion)
596 (progn 758 (progn
597 (setq he-search-loc he-string-end) 759 (set-marker he-search-loc he-string-end)
598 (setq he-search-bw ()))))) 760 (setq he-search-bw ())))))
599 761
600 (if (not expansion) ; Then look forward. 762 (if (not expansion) ; Then look forward.
601 (progn 763 (progn
602 (goto-char he-search-loc) 764 (goto-char he-search-loc)
603 (setq expansion (he-dab-search he-search-string nil)) 765 (setq expansion (he-dab-search he-search-string nil))
604 (setq he-search-loc (point-marker)))))) 766 (set-marker he-search-loc (point))))))
605 767
606 (if (not expansion) 768 (if (not expansion)
607 (progn 769 (progn
608 (he-reset-string) 770 (if old (he-reset-string))
609 ()) 771 ())
610 (progn 772 (progn
611 (he-substitute-string expansion t) 773 (he-substitute-string expansion t)
612 (setq he-tried-table (cons expansion he-tried-table)) 774 (setq he-tried-table (cons expansion he-tried-table))
613 t)))) 775 t))))
620 (let ((expansion ()) 782 (let ((expansion ())
621 (buf (current-buffer))) 783 (buf (current-buffer)))
622 (if (not old) 784 (if (not old)
623 (progn 785 (progn
624 (he-init-string (he-dabbrev-beg) (point)) 786 (he-init-string (he-dabbrev-beg) (point))
625 (setq he-search-loc 0) 787 (setq he-search-bufs (buffer-list))
626 (setq he-search-bufs (buffer-list)))) 788 (setq he-searched-n-bufs 0)
789 (set-marker he-search-loc 1 (car he-search-bufs))))
627 790
628 (if (not (equal he-search-string "")) 791 (if (not (equal he-search-string ""))
629 (while (and he-search-bufs (not expansion)) 792 (while (and he-search-bufs
793 (not expansion)
794 (or (not hippie-expand-max-buffers)
795 (< he-searched-n-bufs hippie-expand-max-buffers)))
630 (set-buffer (car he-search-bufs)) 796 (set-buffer (car he-search-bufs))
631 (if (and (not (eq (current-buffer) buf)) 797 (if (and (not (eq (current-buffer) buf))
632 (not (eq major-mode 'dired-mode))) 798 (not (string-match " \\*Minibuf-[0-9]+\\*"
633 ;; dont search dired buffers 799 (buffer-name (current-buffer))))
800 (not (eq major-mode 'dired-mode)))
801 ;; Dont search minibuffers nor dired buffers
634 (save-excursion 802 (save-excursion
635 (goto-char he-search-loc) 803 (goto-char he-search-loc)
636 (setq expansion (he-dab-search he-search-string nil)) 804 (setq expansion (he-dab-search he-search-string nil))
637 (setq he-search-loc (point-marker)))) 805 (set-marker he-search-loc (point))
638 (if expansion 806 (if expansion
639 (setq he-tried-table (cons expansion he-tried-table)) 807 (setq he-tried-table (cons expansion he-tried-table))
640 (progn 808 (setq he-search-bufs (cdr he-search-bufs))
641 (setq he-search-loc 0) 809 (setq he-searched-n-bufs (1+ he-searched-n-bufs))
642 (setq he-search-bufs (cdr he-search-bufs)))))) 810 (set-marker he-search-loc 1 (car he-search-bufs))))
811 (setq he-search-bufs (cdr he-search-bufs))
812 (set-marker he-search-loc 1 (car he-search-bufs)))))
643 813
644 (set-buffer buf) 814 (set-buffer buf)
645 (if (not expansion) 815 (if (not expansion)
646 (progn 816 (progn
647 (he-reset-string) 817 (if old (he-reset-string))
648 ()) 818 ())
649 (progn 819 (progn
650 (he-substitute-string expansion t) 820 (he-substitute-string expansion t)
651 t)))) 821 t))))
652 822
666 (if (he-string-member result he-tried-table) 836 (if (he-string-member result he-tried-table)
667 (setq result nil))) ; if already in table, ignore 837 (setq result nil))) ; if already in table, ignore
668 result)) 838 result))
669 839
670 (defun he-dabbrev-beg () 840 (defun he-dabbrev-beg ()
671 (let ((skips "-a-zA-Z0-9_.")) 841 (save-excursion
672 (save-excursion 842 (skip-syntax-backward "w_")
673 (skip-chars-backward skips) 843 (skip-syntax-forward "_")
674 (skip-chars-forward "-_.") 844 (point)))
675 (point)))) 845
676 846 (provide 'hippie-exp)
677 ;;; hippie.el ends here 847
848 ;;; hippie-exp.el ends here