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