Mercurial > emacs
annotate lisp/eshell/em-hist.el @ 33166:61e62bc5bd7f
(comint-completion-addsuffix): Fix custom type.
author | Dave Love <fx@gnu.org> |
---|---|
date | Fri, 03 Nov 2000 11:31:34 +0000 |
parents | 8e57189d61b4 |
children | 11b33211a908 |
rev | line source |
---|---|
29876 | 1 ;;; em-hist --- history list management |
2 | |
29934
34b1ab9d583d
Change spelling of the Free Software Foundation.
Gerd Moellmann <gerd@gnu.org>
parents:
29876
diff
changeset
|
3 ;; Copyright (C) 1999, 2000 Free Software Foundation |
29876 | 4 |
32526 | 5 ;; Author: John Wiegley <johnw@gnu.org> |
6 | |
29876 | 7 ;; This file is part of GNU Emacs. |
8 | |
9 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
10 ;; it under the terms of the GNU General Public License as published by | |
11 ;; the Free Software Foundation; either version 2, or (at your option) | |
12 ;; any later version. | |
13 | |
14 ;; GNU Emacs is distributed in the hope that it will be useful, | |
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 ;; GNU General Public License for more details. | |
18 | |
19 ;; You should have received a copy of the GNU General Public License | |
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
22 ;; Boston, MA 02111-1307, USA. | |
23 | |
24 (provide 'em-hist) | |
25 | |
26 (eval-when-compile (require 'esh-maint)) | |
27 | |
28 (defgroup eshell-hist nil | |
29 "This module provides command history management." | |
30 :tag "History list management" | |
31 :group 'eshell-module) | |
32 | |
33 ;;; Commentary: | |
34 | |
35 ;; Eshell's history facility imitates the syntax used by bash | |
36 ;; ([(bash)History Interaction]). Thus: | |
37 ;; | |
38 ;; !ls ; repeat the last command beginning with 'ls' | |
39 ;; !?ls ; repeat the last command containing ls | |
40 ;; echo !ls:2 ; echo the second arg of the last 'ls' command | |
41 ;; !ls<tab> ; complete against all possible words in this | |
42 ;; ; position, by looking at the history list | |
43 ;; !ls<C-c SPC> ; expand any matching history input at point | |
44 ;; | |
45 ;; Also, most of `comint-mode's keybindings are accepted: | |
46 ;; | |
47 ;; M-r ; search backward for a previous command by regexp | |
48 ;; M-s ; search forward for a previous command by regexp | |
49 ;; M-p ; access the last command entered, repeatable | |
50 ;; M-n ; access the first command entered, repeatable | |
51 ;; | |
52 ;; C-c M-r ; using current input, find a matching command thus, with | |
53 ;; ; 'ls' as the current input, it will go back to the same | |
54 ;; ; command that '!ls' would have selected | |
55 ;; C-c M-s ; same, but in reverse order | |
56 ;; | |
57 ;; Note that some of these keybindings are only available if the | |
58 ;; `eshell-rebind' is not in use, in which case M-p does what C-c M-r | |
59 ;; normally would do, and C-p is used instead of M-p. It may seem | |
60 ;; confusing, but the intention is to make the most useful | |
61 ;; functionality the most easily accessible. If `eshell-rebind' is | |
62 ;; not being used, history navigation will use comint's keybindings; | |
63 ;; if it is, history navigation tries to use similar keybindings to | |
64 ;; bash. This is all configurable, of course. | |
65 | |
66 ;;; Code: | |
67 | |
68 (require 'ring) | |
69 (require 'esh-opt) | |
70 (require 'em-pred) | |
71 | |
72 ;;; User Variables: | |
73 | |
74 (defcustom eshell-hist-load-hook '(eshell-hist-initialize) | |
75 "*A list of functions to call when loading `eshell-hist'." | |
76 :type 'hook | |
77 :group 'eshell-hist) | |
78 | |
79 (defcustom eshell-hist-unload-hook | |
80 (list | |
81 (function | |
82 (lambda () | |
83 (remove-hook 'kill-emacs-hook 'eshell-save-some-history)))) | |
84 "*A hook that gets run when `eshell-hist' is unloaded." | |
85 :type 'hook | |
86 :group 'eshell-hist) | |
87 | |
88 (defcustom eshell-history-file-name | |
89 (concat eshell-directory-name "history") | |
90 "*If non-nil, name of the file to read/write input history. | |
91 See also `eshell-read-history' and `eshell-write-history'. | |
92 If it is nil, Eshell will use the value of HISTFILE." | |
93 :type 'file | |
94 :group 'eshell-hist) | |
95 | |
96 (defcustom eshell-history-size 128 | |
97 "*Size of the input history ring. If nil, use envvar HISTSIZE." | |
98 :type 'integer | |
99 :group 'eshell-hist) | |
100 | |
101 (defcustom eshell-hist-ignoredups nil | |
102 "*If non-nil, don't add input matching the last on the input ring. | |
103 This mirrors the optional behavior of bash." | |
104 :type 'boolean | |
105 :group 'eshell-hist) | |
106 | |
107 (defcustom eshell-ask-to-save-history t | |
108 "*Determine if history should be automatically saved. | |
109 History is always preserved after sanely exiting an Eshell buffer. | |
110 However, when Emacs is being shut down, this variable determines | |
111 whether to prompt the user. | |
112 If set to nil, it means never ask whether history should be saved. | |
113 If set to t, always ask if any Eshell buffers are open at exit time. | |
114 If set to `always', history will always be saved, silently." | |
115 :type '(choice (const :tag "Never" nil) | |
116 (const :tag "Ask" t) | |
117 (const :tag "Always save" always)) | |
118 :group 'eshell-hist) | |
119 | |
120 (defcustom eshell-input-filter | |
121 (function | |
122 (lambda (str) | |
123 (not (string-match "\\`\\s-*\\'" str)))) | |
124 "*Predicate for filtering additions to input history. | |
125 Takes one argument, the input. If non-nil, the input may be saved on | |
126 the input history list. Default is to save anything that isn't all | |
127 whitespace." | |
128 :type 'function | |
129 :group 'eshell-hist) | |
130 | |
131 (put 'eshell-input-filter 'risky-local-variable t) | |
132 | |
133 (defcustom eshell-hist-match-partial t | |
134 "*If non-nil, movement through history is constrained by current input. | |
135 Otherwise, typing <M-p> and <M-n> will always go to the next history | |
136 element, regardless of any text on the command line. In that case, | |
137 <C-c M-r> and <C-c M-s> still offer that functionality." | |
138 :type 'boolean | |
139 :group 'eshell-hist) | |
140 | |
141 (defcustom eshell-hist-move-to-end t | |
142 "*If non-nil, move to the end of the buffer before cycling history." | |
143 :type 'boolean | |
144 :group 'eshell-hist) | |
145 | |
146 (defcustom eshell-hist-event-designator | |
147 "^!\\(!\\|-?[0-9]+\\|\\??[^:^$%*?]+\\??\\|#\\)" | |
148 "*The regexp used to identifier history event designators." | |
149 :type 'regexp | |
150 :group 'eshell-hist) | |
151 | |
152 (defcustom eshell-hist-word-designator | |
153 "^:?\\([0-9]+\\|[$^%*]\\)?\\(\\*\\|-[0-9]*\\|[$^%*]\\)?" | |
154 "*The regexp used to identify history word designators." | |
155 :type 'regexp | |
156 :group 'eshell-hist) | |
157 | |
158 (defcustom eshell-hist-modifier | |
159 "^\\(:\\([hretpqx&g]\\|s/\\([^/]*\\)/\\([^/]*\\)/\\)\\)*" | |
160 "*The regexp used to identity history modifiers." | |
161 :type 'regexp | |
162 :group 'eshell-hist) | |
163 | |
164 (defcustom eshell-hist-rebind-keys-alist | |
165 '(([(control ?p)] . eshell-previous-input) | |
166 ([(control ?n)] . eshell-next-input) | |
167 ([(control up)] . eshell-previous-input) | |
168 ([(control down)] . eshell-next-input) | |
169 ([(control ?r)] . eshell-isearch-backward) | |
170 ([(control ?s)] . eshell-isearch-forward) | |
171 ([(meta ?r)] . eshell-previous-matching-input) | |
172 ([(meta ?s)] . eshell-next-matching-input) | |
173 ([(meta ?p)] . eshell-previous-matching-input-from-input) | |
174 ([(meta ?n)] . eshell-next-matching-input-from-input) | |
175 ([up] . eshell-previous-matching-input-from-input) | |
176 ([down] . eshell-next-matching-input-from-input)) | |
177 "*History keys to bind differently if point is in input text." | |
178 :type '(repeat (cons (vector :tag "Keys to bind" | |
179 (repeat :inline t sexp)) | |
180 (function :tag "Command"))) | |
181 :group 'eshell-hist) | |
182 | |
183 ;;; Internal Variables: | |
184 | |
185 (defvar eshell-history-ring nil) | |
186 (defvar eshell-history-index nil) | |
187 (defvar eshell-matching-input-from-input-string "") | |
188 (defvar eshell-save-history-index nil) | |
189 | |
190 (defvar eshell-isearch-map nil) | |
191 | |
192 (unless eshell-isearch-map | |
193 (setq eshell-isearch-map (copy-keymap isearch-mode-map)) | |
194 (define-key eshell-isearch-map [(control ?m)] 'eshell-isearch-return) | |
195 (define-key eshell-isearch-map [return] 'eshell-isearch-return) | |
196 (define-key eshell-isearch-map [(control ?r)] 'eshell-isearch-repeat-backward) | |
197 (define-key eshell-isearch-map [(control ?s)] 'eshell-isearch-repeat-forward) | |
198 (define-key eshell-isearch-map [(control ?g)] 'eshell-isearch-abort) | |
199 (define-key eshell-isearch-map [backspace] 'eshell-isearch-delete-char) | |
200 (define-key eshell-isearch-map [delete] 'eshell-isearch-delete-char) | |
201 (defvar eshell-isearch-cancel-map) | |
202 (define-prefix-command 'eshell-isearch-cancel-map) | |
203 (define-key eshell-isearch-map [(control ?c)] 'eshell-isearch-cancel-map) | |
204 (define-key eshell-isearch-cancel-map [(control ?c)] 'eshell-isearch-cancel)) | |
205 | |
206 ;;; Functions: | |
207 | |
208 (defun eshell-hist-initialize () | |
209 "Initialize the history management code for one Eshell buffer." | |
210 (make-local-hook 'eshell-expand-input-functions) | |
211 (add-hook 'eshell-expand-input-functions | |
212 'eshell-expand-history-references nil t) | |
213 | |
214 (when (eshell-using-module 'eshell-cmpl) | |
215 (make-local-hook 'pcomplete-try-first-hook) | |
216 (add-hook 'pcomplete-try-first-hook | |
217 'eshell-complete-history-reference nil t)) | |
218 | |
219 (if (eshell-using-module 'eshell-rebind) | |
220 (let ((rebind-alist (symbol-value 'eshell-rebind-keys-alist))) | |
221 (make-local-variable 'eshell-rebind-keys-alist) | |
222 (set 'eshell-rebind-keys-alist | |
223 (append rebind-alist eshell-hist-rebind-keys-alist)) | |
224 (set (make-local-variable 'search-invisible) t) | |
225 (set (make-local-variable 'search-exit-option) t) | |
226 (make-local-hook 'isearch-mode-hook) | |
227 (add-hook 'isearch-mode-hook | |
228 (function | |
229 (lambda () | |
230 (if (>= (point) eshell-last-output-end) | |
231 (setq overriding-terminal-local-map | |
232 eshell-isearch-map)))) nil t) | |
233 (make-local-hook 'isearch-mode-end-hook) | |
234 (add-hook 'isearch-mode-end-hook | |
235 (function | |
236 (lambda () | |
237 (setq overriding-terminal-local-map nil))) nil t)) | |
238 (define-key eshell-mode-map [up] 'eshell-previous-matching-input-from-input) | |
239 (define-key eshell-mode-map [down] 'eshell-next-matching-input-from-input) | |
240 (define-key eshell-mode-map [(control up)] 'eshell-previous-input) | |
241 (define-key eshell-mode-map [(control down)] 'eshell-next-input) | |
242 (define-key eshell-mode-map [(meta ?r)] 'eshell-previous-matching-input) | |
243 (define-key eshell-mode-map [(meta ?s)] 'eshell-next-matching-input) | |
244 (define-key eshell-command-map [(meta ?r)] | |
245 'eshell-previous-matching-input-from-input) | |
246 (define-key eshell-command-map [(meta ?s)] | |
247 'eshell-next-matching-input-from-input) | |
248 (if eshell-hist-match-partial | |
249 (progn | |
250 (define-key eshell-mode-map [(meta ?p)] | |
251 'eshell-previous-matching-input-from-input) | |
252 (define-key eshell-mode-map [(meta ?n)] | |
253 'eshell-next-matching-input-from-input) | |
254 (define-key eshell-command-map [(meta ?p)] 'eshell-previous-input) | |
255 (define-key eshell-command-map [(meta ?n)] 'eshell-next-input)) | |
256 (define-key eshell-mode-map [(meta ?p)] 'eshell-previous-input) | |
257 (define-key eshell-mode-map [(meta ?n)] 'eshell-next-input) | |
258 (define-key eshell-command-map [(meta ?p)] | |
259 'eshell-previous-matching-input-from-input) | |
260 (define-key eshell-command-map [(meta ?n)] | |
261 'eshell-next-matching-input-from-input))) | |
262 | |
263 (make-local-variable 'eshell-history-size) | |
264 (or eshell-history-size | |
265 (setq eshell-history-size (getenv "HISTSIZE"))) | |
266 | |
267 (make-local-variable 'eshell-history-file-name) | |
268 (or eshell-history-file-name | |
269 (setq eshell-history-file-name (getenv "HISTFILE"))) | |
270 | |
271 (make-local-variable 'eshell-history-index) | |
272 (make-local-variable 'eshell-save-history-index) | |
273 (make-local-variable 'eshell-history-ring) | |
274 (if eshell-history-file-name | |
275 (eshell-read-history nil t)) | |
276 (unless eshell-history-ring | |
277 (setq eshell-history-ring (make-ring eshell-history-size))) | |
278 | |
279 (make-local-hook 'eshell-exit-hook) | |
280 (add-hook 'eshell-exit-hook 'eshell-write-history nil t) | |
281 | |
282 (add-hook 'kill-emacs-hook 'eshell-save-some-history) | |
283 | |
284 (make-local-variable 'eshell-input-filter-functions) | |
285 (add-hook 'eshell-input-filter-functions 'eshell-add-to-history nil t) | |
286 | |
287 (define-key eshell-command-map [(control ?l)] 'eshell-list-history) | |
288 (define-key eshell-command-map [(control ?x)] 'eshell-get-next-from-history)) | |
289 | |
290 (defun eshell-save-some-history () | |
291 "Save the history for any open Eshell buffers." | |
292 (eshell-for buf (buffer-list) | |
293 (if (buffer-live-p buf) | |
294 (with-current-buffer buf | |
295 (if (and eshell-mode | |
296 eshell-history-file-name | |
297 eshell-ask-to-save-history | |
298 (or (eq eshell-ask-to-save-history 'always) | |
299 (y-or-n-p | |
300 (format "Save input history for Eshell buffer `%s'? " | |
301 (buffer-name buf))))) | |
302 (eshell-write-history)))))) | |
303 | |
304 (defun eshell/history (&rest args) | |
305 "List in help buffer the buffer's input history." | |
306 (eshell-init-print-buffer) | |
307 (eshell-eval-using-options | |
308 "history" args | |
309 '((?r "read" nil read-history | |
310 "read from history file to current history list") | |
311 (?w "write" nil write-history | |
312 "write current history list to history file") | |
313 (?a "append" nil append-history | |
314 "append current history list to history file") | |
315 (?h "help" nil nil "display this usage message") | |
316 :usage "[n] [-rwa [filename]]" | |
317 :post-usage | |
318 "When Eshell is started, history is read from `eshell-history-file-name'. | |
319 This is also the location where history info will be saved by this command, | |
320 unless a different file is specified on the command line.") | |
321 (and (or (not (ring-p eshell-history-ring)) | |
322 (ring-empty-p eshell-history-ring)) | |
323 (error "No history")) | |
324 (let (length command file) | |
325 (when (and args (string-match "^[0-9]+$" (car args))) | |
326 (setq length (min (eshell-convert (car args)) | |
327 (ring-length eshell-history-ring)) | |
328 args (cdr args))) | |
329 (and length | |
330 (or read-history write-history append-history) | |
331 (error "history: extra arguments")) | |
332 (when (and args (stringp (car args))) | |
333 (setq file (car args) | |
334 args (cdr args))) | |
335 (cond | |
336 (read-history (eshell-read-history file)) | |
337 (write-history (eshell-write-history file)) | |
338 (append-history (eshell-write-history file t)) | |
339 (t | |
340 (let* ((history nil) | |
341 (index (1- (or length (ring-length eshell-history-ring)))) | |
342 (ref (- (ring-length eshell-history-ring) index))) | |
343 ;; We have to build up a list ourselves from the ring vector. | |
344 (while (>= index 0) | |
345 (eshell-buffered-print | |
346 (format "%5d %s\n" ref (eshell-get-history index))) | |
347 (setq index (1- index) | |
348 ref (1+ ref))))))) | |
349 (eshell-flush) | |
350 nil)) | |
351 | |
352 (defun eshell-put-history (input &optional ring at-beginning) | |
353 "Put a new input line into the history ring." | |
354 (unless ring (setq ring eshell-history-ring)) | |
355 (if at-beginning | |
356 (ring-insert-at-beginning ring input) | |
357 (ring-insert ring input))) | |
358 | |
359 (defun eshell-get-history (index &optional ring) | |
360 "Get an input line from the history ring." | |
31241 | 361 (ring-ref (or ring eshell-history-ring) index)) |
29876 | 362 |
363 (defun eshell-add-to-history () | |
364 "Add INPUT to the history ring. | |
365 The input is entered into the input history ring, if the value of | |
366 variable `eshell-input-filter' returns non-nil when called on the | |
367 input." | |
368 (when (> (1- eshell-last-input-end) eshell-last-input-start) | |
369 (let ((input (buffer-substring eshell-last-input-start | |
370 (1- eshell-last-input-end)))) | |
371 (if (and (funcall eshell-input-filter input) | |
372 (or (null eshell-hist-ignoredups) | |
373 (not (ring-p eshell-history-ring)) | |
374 (ring-empty-p eshell-history-ring) | |
375 (not (string-equal (eshell-get-history 0) input)))) | |
376 (eshell-put-history input)) | |
377 (setq eshell-save-history-index eshell-history-ring) | |
378 (setq eshell-history-index nil)))) | |
379 | |
380 (defun eshell-read-history (&optional filename silent) | |
381 "Sets the buffer's `eshell-history-ring' from a history file. | |
382 The name of the file is given by the variable | |
383 `eshell-history-file-name'. The history ring is of size | |
384 `eshell-history-size', regardless of file size. If | |
385 `eshell-history-file-name' is nil this function does nothing. | |
386 | |
387 If the optional argument SILENT is non-nil, we say nothing about a | |
388 failure to read the history file. | |
389 | |
390 This function is useful for major mode commands and mode hooks. | |
391 | |
392 The structure of the history file should be one input command per | |
393 line, with the most recent command last. See also | |
394 `eshell-hist-ignoredups' and `eshell-write-history'." | |
395 (let ((file (or filename eshell-history-file-name))) | |
396 (cond | |
397 ((or (null file) | |
398 (equal file "")) | |
399 nil) | |
400 ((not (file-readable-p file)) | |
401 (or silent | |
402 (message "Cannot read history file %s" file))) | |
403 (t | |
404 (let* ((count 0) | |
405 (size eshell-history-size) | |
406 (ring (make-ring size)) | |
407 (ignore-dups eshell-hist-ignoredups)) | |
408 (with-temp-buffer | |
409 (insert-file-contents file) | |
410 ;; Save restriction in case file is already visited... | |
411 ;; Watch for those date stamps in history files! | |
412 (goto-char (point-max)) | |
413 (while (and (< count size) | |
414 (re-search-backward "^[ \t]*\\([^#\n].*\\)[ \t]*$" | |
415 nil t)) | |
416 (let ((history (match-string 1))) | |
417 (if (or (null ignore-dups) | |
418 (ring-empty-p ring) | |
419 (not (string-equal (ring-ref ring 0) history))) | |
31241 | 420 (ring-insert-at-beginning |
421 ring (subst-char-in-string ?\177 ?\n history)))) | |
29876 | 422 (setq count (1+ count)))) |
423 (setq eshell-history-ring ring | |
424 eshell-history-index nil)))))) | |
425 | |
426 (defun eshell-write-history (&optional filename append) | |
427 "Writes the buffer's `eshell-history-ring' to a history file. | |
428 The name of the file is given by the variable | |
429 `eshell-history-file-name'. The original contents of the file are | |
430 lost if `eshell-history-ring' is not empty. If | |
431 `eshell-history-file-name' is nil this function does nothing. | |
432 | |
433 Useful within process sentinels. | |
434 | |
435 See also `eshell-read-history'." | |
436 (let ((file (or filename eshell-history-file-name))) | |
437 (cond | |
438 ((or (null file) | |
439 (equal file "") | |
440 (null eshell-history-ring) | |
441 (ring-empty-p eshell-history-ring)) | |
442 nil) | |
443 ((not (file-writable-p file)) | |
444 (message "Cannot write history file %s" file)) | |
445 (t | |
446 (let* ((ring eshell-history-ring) | |
447 (index (ring-length ring))) | |
448 ;; Write it all out into a buffer first. Much faster, but | |
449 ;; messier, than writing it one line at a time. | |
450 (with-temp-buffer | |
451 (while (> index 0) | |
452 (setq index (1- index)) | |
31241 | 453 (let ((start (point))) |
454 (insert (ring-ref ring index) ?\n) | |
455 (subst-char-in-region start (1- (point)) ?\n ?\177))) | |
29876 | 456 (eshell-with-private-file-modes |
457 (write-region (point-min) (point-max) file append | |
458 'no-message)))))))) | |
459 | |
460 (defun eshell-list-history () | |
461 "List in help buffer the buffer's input history." | |
462 (interactive) | |
463 (let (prefix prelen) | |
464 (save-excursion | |
465 (if (re-search-backward "!\\(.+\\)" (line-beginning-position) t) | |
466 (setq prefix (match-string 1) | |
467 prelen (length prefix)))) | |
468 (if (or (not (ring-p eshell-history-ring)) | |
469 (ring-empty-p eshell-history-ring)) | |
470 (message "No history") | |
471 (let ((history nil) | |
472 (history-buffer " *Input History*") | |
473 (index (1- (ring-length eshell-history-ring))) | |
474 (conf (current-window-configuration))) | |
475 ;; We have to build up a list ourselves from the ring vector. | |
476 (while (>= index 0) | |
477 (let ((hist (eshell-get-history index))) | |
478 (if (or (not prefix) | |
479 (and (>= (length hist) prelen) | |
480 (string= (substring hist 0 prelen) prefix))) | |
481 (setq history (cons hist history)))) | |
482 (setq index (1- index))) | |
483 ;; Change "completion" to "history reference" | |
484 ;; to make the display accurate. | |
485 (with-output-to-temp-buffer history-buffer | |
486 (display-completion-list history) | |
487 (set-buffer history-buffer) | |
488 (forward-line 3) | |
489 (while (search-backward "completion" nil 'move) | |
490 (replace-match "history reference"))) | |
491 (eshell-redisplay) | |
492 (message "Hit space to flush") | |
493 (let ((ch (read-event))) | |
494 (if (eq ch ?\ ) | |
495 (set-window-configuration conf) | |
496 (setq unread-command-events (list ch)))))))) | |
497 | |
498 (defun eshell-hist-word-reference (ref) | |
499 "Return the word designator index referred to by REF." | |
500 (cond | |
501 ((string-match "^[0-9]+$" ref) | |
502 (string-to-number ref)) | |
503 ((string= "^" ref) 1) | |
504 ((string= "$" ref) nil) | |
505 ((string= "%" ref) | |
506 (error "`%' history word designator not yet implemented")))) | |
507 | |
508 (defun eshell-hist-parse-arguments (&optional silent b e) | |
509 "Parse current command arguments in a history-code-friendly way." | |
510 (let ((end (or e (point))) | |
511 (begin (or b (save-excursion (eshell-bol) (point)))) | |
512 (posb (list t)) | |
513 (pose (list t)) | |
514 (textargs (list t)) | |
515 hist args) | |
516 (unless (catch 'eshell-incomplete | |
517 (ignore | |
518 (setq args (eshell-parse-arguments begin end)))) | |
519 (save-excursion | |
520 (goto-char begin) | |
521 (while (< (point) end) | |
522 (if (get-text-property (point) 'arg-begin) | |
523 (nconc posb (list (point)))) | |
524 (if (get-text-property (point) 'arg-end) | |
525 (nconc pose | |
526 (list (if (= (1+ (point)) end) | |
527 (1+ (point)) | |
528 (point))))) | |
529 (forward-char)) | |
530 (setq posb (cdr posb) | |
531 pose (cdr pose)) | |
532 (assert (= (length posb) (length args))) | |
533 (assert (<= (length posb) (length pose)))) | |
534 (setq hist (buffer-substring-no-properties begin end)) | |
535 (let ((b posb) (e pose)) | |
536 (while b | |
537 (nconc textargs | |
538 (list (substring hist (- (car b) begin) | |
539 (- (car e) begin)))) | |
540 (setq b (cdr b) | |
541 e (cdr e)))) | |
542 (setq textargs (cdr textargs)) | |
543 (assert (= (length textargs) (length args))) | |
544 (list textargs posb pose)))) | |
545 | |
546 (defun eshell-expand-history-references (beg end) | |
547 "Parse and expand any history references in current input." | |
548 (let ((result (eshell-hist-parse-arguments t beg end))) | |
549 (when result | |
550 (let ((textargs (nreverse (nth 0 result))) | |
551 (posb (nreverse (nth 1 result))) | |
552 (pose (nreverse (nth 2 result)))) | |
553 (save-excursion | |
554 (while textargs | |
555 (let ((str (eshell-history-reference (car textargs)))) | |
556 (unless (eq str (car textargs)) | |
557 (goto-char (car posb)) | |
558 (insert-and-inherit str) | |
559 (delete-char (- (car pose) (car posb))))) | |
560 (setq textargs (cdr textargs) | |
561 posb (cdr posb) | |
562 pose (cdr pose)))))))) | |
563 | |
564 (defun eshell-complete-history-reference () | |
565 "Complete a history reference, by completing the event designator." | |
566 (let ((arg (pcomplete-actual-arg))) | |
567 (when (string-match "\\`![^:^$*%]*\\'" arg) | |
568 (setq pcomplete-stub (substring arg 1) | |
569 pcomplete-last-completion-raw t) | |
570 (throw 'pcomplete-completions | |
571 (let ((history nil) | |
572 (index (1- (ring-length eshell-history-ring))) | |
573 (stublen (length pcomplete-stub))) | |
574 ;; We have to build up a list ourselves from the ring | |
575 ;; vector. | |
576 (while (>= index 0) | |
577 (let ((hist (eshell-get-history index))) | |
578 (if (and (>= (length hist) stublen) | |
579 (string= (substring hist 0 stublen) | |
580 pcomplete-stub) | |
581 (string-match "^\\([^:^$*% \t\n]+\\)" hist)) | |
582 (setq history (cons (match-string 1 hist) | |
583 history)))) | |
584 (setq index (1- index))) | |
585 (let ((fhist (list t))) | |
586 ;; uniqify the list, but preserve the order | |
587 (while history | |
588 (unless (member (car history) fhist) | |
589 (nconc fhist (list (car history)))) | |
590 (setq history (cdr history))) | |
591 (cdr fhist))))))) | |
592 | |
593 (defun eshell-history-reference (reference) | |
594 "Expand directory stack REFERENCE. | |
595 The syntax used here was taken from the Bash info manual. | |
596 Returns the resultant reference, or the same string REFERENCE if none | |
597 matched." | |
598 ;; `^string1^string2^' | |
599 ;; Quick Substitution. Repeat the last command, replacing | |
600 ;; STRING1 with STRING2. Equivalent to `!!:s/string1/string2/' | |
601 (if (and (eshell-using-module 'eshell-pred) | |
602 (string-match "\\^\\([^^]+\\)\\^\\([^^]+\\)\\^?\\s-*$" | |
603 reference)) | |
604 (setq reference (format "!!:s/%s/%s/" | |
605 (match-string 1 reference) | |
606 (match-string 2 reference)))) | |
607 ;; `!' | |
608 ;; Start a history substitution, except when followed by a | |
609 ;; space, tab, the end of the line, = or (. | |
610 (if (not (string-match "^![^ \t\n=\(]" reference)) | |
611 reference | |
612 (setq eshell-history-index nil) | |
613 (let ((event (eshell-hist-parse-event-designator reference))) | |
614 (unless event | |
615 (error "Could not find history event `%s'" reference)) | |
616 (setq eshell-history-index (car event) | |
617 reference (substring reference (cdr event)) | |
618 event (eshell-get-history eshell-history-index)) | |
619 (if (not (string-match "^[:^$*%]" reference)) | |
620 event | |
621 (let ((word (eshell-hist-parse-word-designator | |
622 event reference))) | |
623 (unless word | |
624 (error "Unable to honor word designator `%s'" reference)) | |
625 (unless (string-match "^[:^$*%][[$^*%0-9-]" reference) | |
626 (setcdr word 0)) | |
627 (setq event (car word) | |
628 reference (substring reference (cdr word))) | |
629 (if (not (and (eshell-using-module 'eshell-pred) | |
630 (string-match "^:" reference))) | |
631 event | |
632 (eshell-hist-parse-modifier event reference))))))) | |
633 | |
634 (defun eshell-hist-parse-event-designator (reference) | |
635 "Parse a history event designator beginning in REFERENCE." | |
636 (let* ((index (string-match eshell-hist-event-designator reference)) | |
637 (end (and index (match-end 0)))) | |
638 (unless index | |
639 (error "Invalid history event designator `%s'" reference)) | |
640 (let* ((event (match-string 1 reference)) | |
641 (pos | |
642 (cond | |
643 ((string= event "!") (ring-length eshell-history-ring)) | |
644 ((string= event "#") (error "!# not yet implemented")) | |
645 ((string-match "^-?[0-9]+$" event) | |
646 (let ((num (string-to-number event))) | |
647 (if (>= num 0) | |
648 (- (ring-length eshell-history-ring) num) | |
649 (1- (abs num))))) | |
650 ((string-match "^\\(\\??\\)\\([^?]+\\)\\??$" event) | |
651 (let ((pref (if (> (length (match-string 1 event)) 0) | |
652 "" "^")) | |
653 (str (match-string 2 event))) | |
654 (save-match-data | |
655 (eshell-previous-matching-input-string-position | |
656 (concat pref (regexp-quote str)) 1)))) | |
657 (t | |
658 (error "Failed to parse event designator `%s'" event))))) | |
659 (and pos (cons pos end))))) | |
660 | |
661 (defun eshell-hist-parse-word-designator (hist reference) | |
662 "Parse a history word designator beginning for HIST in REFERENCE." | |
663 (let* ((index (string-match eshell-hist-word-designator reference)) | |
664 (end (and index (match-end 0)))) | |
665 (unless (memq (aref reference 0) '(?: ?^ ?$ ?* ?%)) | |
666 (error "Invalid history word designator `%s'" reference)) | |
667 (let ((nth (match-string 1 reference)) | |
668 (mth (match-string 2 reference)) | |
669 (here (point)) | |
670 textargs) | |
671 (insert hist) | |
672 (setq textargs (car (eshell-hist-parse-arguments nil here (point)))) | |
673 (delete-region here (point)) | |
674 (if (string= nth "*") | |
675 (if mth | |
676 (error "Invalid history word designator `%s'" | |
677 reference) | |
678 (setq nth 1 mth "-$"))) | |
679 (if (not mth) | |
680 (if nth | |
681 (setq mth nth) | |
682 (setq nth 0 mth "$")) | |
683 (if (string= mth "-") | |
684 (setq mth (- (length textargs) 2)) | |
685 (if (string= mth "*") | |
686 (setq mth "$") | |
687 (if (not (and (> (length mth) 1) | |
688 (eq (aref mth 0) ?-))) | |
689 (error "Invalid history word designator `%s'" | |
690 reference) | |
691 (setq mth (substring mth 1)))))) | |
692 (unless (numberp nth) | |
693 (setq nth (eshell-hist-word-reference nth))) | |
694 (unless (numberp mth) | |
695 (setq mth (eshell-hist-word-reference mth))) | |
696 (cons (mapconcat 'identity (eshell-sublist textargs nth mth) "") | |
697 end)))) | |
698 | |
699 (defun eshell-hist-parse-modifier (hist reference) | |
700 "Parse a history modifier beginning for HIST in REFERENCE." | |
701 (let ((here (point))) | |
702 (insert reference) | |
703 (prog1 | |
704 (save-restriction | |
705 (narrow-to-region here (point)) | |
706 (goto-char (point-min)) | |
707 (let ((modifiers (cdr (eshell-parse-modifiers)))) | |
708 (eshell-for mod modifiers | |
709 (setq hist (funcall mod hist))) | |
710 hist)) | |
711 (delete-region here (point))))) | |
712 | |
713 (defun eshell-get-next-from-history () | |
714 "After fetching a line from input history, this fetches the next. | |
715 In other words, this recalls the input line after the line you | |
716 recalled last. You can use this to repeat a sequence of input lines." | |
717 (interactive) | |
718 (if eshell-save-history-index | |
719 (progn | |
720 (setq eshell-history-index (1+ eshell-save-history-index)) | |
721 (eshell-next-input 1)) | |
722 (message "No previous history command"))) | |
723 | |
724 (defun eshell-search-arg (arg) | |
725 ;; First make sure there is a ring and that we are after the process | |
726 ;; mark | |
727 (if (and eshell-hist-move-to-end | |
728 (< (point) eshell-last-output-end)) | |
729 (goto-char eshell-last-output-end)) | |
730 (cond ((or (null eshell-history-ring) | |
731 (ring-empty-p eshell-history-ring)) | |
732 (error "Empty input ring")) | |
733 ((zerop arg) | |
734 ;; arg of zero resets search from beginning, and uses arg of | |
735 ;; 1 | |
736 (setq eshell-history-index nil) | |
737 1) | |
738 (t | |
739 arg))) | |
740 | |
741 (defun eshell-search-start (arg) | |
742 "Index to start a directional search, starting at `eshell-history-index'." | |
743 (if eshell-history-index | |
744 ;; If a search is running, offset by 1 in direction of arg | |
745 (mod (+ eshell-history-index (if (> arg 0) 1 -1)) | |
746 (ring-length eshell-history-ring)) | |
747 ;; For a new search, start from beginning or end, as appropriate | |
748 (if (>= arg 0) | |
749 0 ; First elt for forward search | |
750 ;; Last elt for backward search | |
751 (1- (ring-length eshell-history-ring))))) | |
752 | |
753 (defun eshell-previous-input-string (arg) | |
754 "Return the string ARG places along the input ring. | |
755 Moves relative to `eshell-history-index'." | |
756 (eshell-get-history (if eshell-history-index | |
757 (mod (+ arg eshell-history-index) | |
758 (ring-length eshell-history-ring)) | |
759 arg))) | |
760 | |
761 (defun eshell-previous-input (arg) | |
762 "Cycle backwards through input history." | |
763 (interactive "*p") | |
764 (eshell-previous-matching-input "." arg)) | |
765 | |
766 (defun eshell-next-input (arg) | |
767 "Cycle forwards through input history." | |
768 (interactive "*p") | |
769 (eshell-previous-input (- arg))) | |
770 | |
771 (defun eshell-previous-matching-input-string (regexp arg) | |
772 "Return the string matching REGEXP ARG places along the input ring. | |
773 Moves relative to `eshell-history-index'." | |
774 (let* ((pos (eshell-previous-matching-input-string-position regexp arg))) | |
775 (if pos (eshell-get-history pos)))) | |
776 | |
777 (defun eshell-previous-matching-input-string-position | |
778 (regexp arg &optional start) | |
779 "Return the index matching REGEXP ARG places along the input ring. | |
780 Moves relative to START, or `eshell-history-index'." | |
781 (if (or (not (ring-p eshell-history-ring)) | |
782 (ring-empty-p eshell-history-ring)) | |
783 (error "No history")) | |
784 (let* ((len (ring-length eshell-history-ring)) | |
785 (motion (if (> arg 0) 1 -1)) | |
786 (n (mod (- (or start (eshell-search-start arg)) motion) len)) | |
787 (tried-each-ring-item nil) | |
788 (case-fold-search (eshell-under-windows-p)) | |
789 (prev nil)) | |
790 ;; Do the whole search as many times as the argument says. | |
791 (while (and (/= arg 0) (not tried-each-ring-item)) | |
792 ;; Step once. | |
793 (setq prev n | |
794 n (mod (+ n motion) len)) | |
795 ;; If we haven't reached a match, step some more. | |
796 (while (and (< n len) (not tried-each-ring-item) | |
797 (not (string-match regexp (eshell-get-history n)))) | |
798 (setq n (mod (+ n motion) len) | |
799 ;; If we have gone all the way around in this search. | |
800 tried-each-ring-item (= n prev))) | |
801 (setq arg (if (> arg 0) (1- arg) (1+ arg)))) | |
802 ;; Now that we know which ring element to use, if we found it, | |
803 ;; return that. | |
804 (if (string-match regexp (eshell-get-history n)) | |
805 n))) | |
806 | |
807 (defun eshell-previous-matching-input (regexp arg) | |
808 "Search backwards through input history for match for REGEXP. | |
809 \(Previous history elements are earlier commands.) | |
810 With prefix argument N, search for Nth previous match. | |
811 If N is negative, find the next or Nth next match." | |
812 (interactive (eshell-regexp-arg "Previous input matching (regexp): ")) | |
813 (setq arg (eshell-search-arg arg)) | |
814 (let ((pos (eshell-previous-matching-input-string-position regexp arg))) | |
815 ;; Has a match been found? | |
816 (if (null pos) | |
817 (error "Not found") | |
818 (setq eshell-history-index pos) | |
819 (message "History item: %d" (- (ring-length eshell-history-ring) pos)) | |
820 ;; Can't use kill-region as it sets this-command | |
821 (delete-region (save-excursion (eshell-bol) (point)) (point)) | |
822 (insert-and-inherit (eshell-get-history pos))))) | |
823 | |
824 (defun eshell-next-matching-input (regexp arg) | |
825 "Search forwards through input history for match for REGEXP. | |
826 \(Later history elements are more recent commands.) | |
827 With prefix argument N, search for Nth following match. | |
828 If N is negative, find the previous or Nth previous match." | |
829 (interactive (eshell-regexp-arg "Next input matching (regexp): ")) | |
830 (eshell-previous-matching-input regexp (- arg))) | |
831 | |
832 (defun eshell-previous-matching-input-from-input (arg) | |
833 "Search backwards through input history for match for current input. | |
834 \(Previous history elements are earlier commands.) | |
835 With prefix argument N, search for Nth previous match. | |
836 If N is negative, search forwards for the -Nth following match." | |
837 (interactive "p") | |
838 (if (not (memq last-command '(eshell-previous-matching-input-from-input | |
839 eshell-next-matching-input-from-input))) | |
840 ;; Starting a new search | |
841 (setq eshell-matching-input-from-input-string | |
842 (buffer-substring (save-excursion (eshell-bol) (point)) | |
843 (point)) | |
844 eshell-history-index nil)) | |
845 (eshell-previous-matching-input | |
846 (concat "^" (regexp-quote eshell-matching-input-from-input-string)) | |
847 arg)) | |
848 | |
849 (defun eshell-next-matching-input-from-input (arg) | |
850 "Search forwards through input history for match for current input. | |
851 \(Following history elements are more recent commands.) | |
852 With prefix argument N, search for Nth following match. | |
853 If N is negative, search backwards for the -Nth previous match." | |
854 (interactive "p") | |
855 (eshell-previous-matching-input-from-input (- arg))) | |
856 | |
857 (defun eshell-test-imatch () | |
858 "If isearch match good, put point at the beginning and return non-nil." | |
859 (if (get-text-property (point) 'history) | |
860 (progn (beginning-of-line) t) | |
861 (let ((before (point))) | |
862 (eshell-bol) | |
863 (if (and (not (bolp)) | |
864 (<= (point) before)) | |
865 t | |
866 (if isearch-forward | |
867 (progn | |
868 (end-of-line) | |
869 (forward-char)) | |
870 (beginning-of-line) | |
871 (backward-char)))))) | |
872 | |
873 (defun eshell-return-to-prompt () | |
874 "Once a search string matches, insert it at the end and go there." | |
875 (setq isearch-other-end nil) | |
876 (let ((found (eshell-test-imatch)) before) | |
877 (while (and (not found) | |
878 (setq before | |
879 (funcall (if isearch-forward | |
880 're-search-forward | |
881 're-search-backward) | |
882 isearch-string nil t))) | |
883 (setq found (eshell-test-imatch))) | |
884 (if (not found) | |
885 (progn | |
886 (goto-char eshell-last-output-end) | |
887 (delete-region (point) (point-max))) | |
888 (setq before (point)) | |
889 (let ((text (buffer-substring-no-properties | |
890 (point) (line-end-position))) | |
891 (orig (marker-position eshell-last-output-end))) | |
892 (goto-char eshell-last-output-end) | |
893 (delete-region (point) (point-max)) | |
894 (when (and text (> (length text) 0)) | |
895 (insert text) | |
896 (put-text-property (1- (point)) (point) | |
897 'last-search-pos before) | |
898 (set-marker eshell-last-output-end orig) | |
899 (goto-char eshell-last-output-end)))))) | |
900 | |
901 (defun eshell-prepare-for-search () | |
902 "Make sure the old history file is at the beginning of the buffer." | |
903 (unless (get-text-property (point-min) 'history) | |
904 (save-excursion | |
905 (goto-char (point-min)) | |
906 (let ((end (copy-marker (point) t))) | |
907 (insert-file-contents eshell-history-file-name) | |
908 (set-text-properties (point-min) end | |
909 '(history t invisible t)))))) | |
910 | |
911 (defun eshell-isearch-backward (&optional invert) | |
912 "Do incremental regexp search backward through past commands." | |
913 (interactive) | |
914 (let ((inhibit-read-only t) end) | |
915 (eshell-prepare-for-search) | |
916 (goto-char (point-max)) | |
917 (set-marker eshell-last-output-end (point)) | |
918 (delete-region (point) (point-max))) | |
919 (isearch-mode invert t 'eshell-return-to-prompt)) | |
920 | |
921 (defun eshell-isearch-repeat-backward (&optional invert) | |
922 "Do incremental regexp search backward through past commands." | |
923 (interactive) | |
924 (let ((old-pos (get-text-property (1- (point-max)) | |
925 'last-search-pos))) | |
926 (when old-pos | |
927 (goto-char old-pos) | |
928 (if invert | |
929 (end-of-line) | |
930 (backward-char))) | |
931 (setq isearch-forward invert) | |
932 (isearch-search-and-update))) | |
933 | |
934 (defun eshell-isearch-forward () | |
935 "Do incremental regexp search backward through past commands." | |
936 (interactive) | |
937 (eshell-isearch-backward t)) | |
938 | |
939 (defun eshell-isearch-repeat-forward () | |
940 "Do incremental regexp search backward through past commands." | |
941 (interactive) | |
942 (eshell-isearch-repeat-backward t)) | |
943 | |
944 (defun eshell-isearch-cancel () | |
945 (interactive) | |
946 (goto-char eshell-last-output-end) | |
947 (delete-region (point) (point-max)) | |
948 (call-interactively 'isearch-cancel)) | |
949 | |
950 (defun eshell-isearch-abort () | |
951 (interactive) | |
952 (goto-char eshell-last-output-end) | |
953 (delete-region (point) (point-max)) | |
954 (call-interactively 'isearch-abort)) | |
955 | |
956 (defun eshell-isearch-delete-char () | |
957 (interactive) | |
958 (save-excursion | |
959 (isearch-delete-char))) | |
960 | |
961 (defun eshell-isearch-return () | |
962 (interactive) | |
963 (isearch-done) | |
964 (eshell-send-input)) | |
965 | |
966 ;;; em-hist.el ends here |