]> code.delx.au - gnu-emacs/blob - lisp/eshell/em-hist.el
(eshell-hist-initialize): When in the minibuffer, use the global value
[gnu-emacs] / lisp / eshell / em-hist.el
1 ;;; em-hist.el --- history list management
2
3 ;; Copyright (C) 1999, 2000 Free Software Foundation
4
5 ;; Author: John Wiegley <johnw@gnu.org>
6
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 (and (eshell-using-module 'eshell-rebind)
220 (not eshell-non-interactive-p))
221 (let ((rebind-alist (symbol-value 'eshell-rebind-keys-alist)))
222 (make-local-variable 'eshell-rebind-keys-alist)
223 (set 'eshell-rebind-keys-alist
224 (append rebind-alist eshell-hist-rebind-keys-alist))
225 (set (make-local-variable 'search-invisible) t)
226 (set (make-local-variable 'search-exit-option) t)
227 (make-local-hook 'isearch-mode-hook)
228 (add-hook 'isearch-mode-hook
229 (function
230 (lambda ()
231 (if (>= (point) eshell-last-output-end)
232 (setq overriding-terminal-local-map
233 eshell-isearch-map)))) nil t)
234 (make-local-hook 'isearch-mode-end-hook)
235 (add-hook 'isearch-mode-end-hook
236 (function
237 (lambda ()
238 (setq overriding-terminal-local-map nil))) nil t))
239 (define-key eshell-mode-map [up] 'eshell-previous-matching-input-from-input)
240 (define-key eshell-mode-map [down] 'eshell-next-matching-input-from-input)
241 (define-key eshell-mode-map [(control up)] 'eshell-previous-input)
242 (define-key eshell-mode-map [(control down)] 'eshell-next-input)
243 (define-key eshell-mode-map [(meta ?r)] 'eshell-previous-matching-input)
244 (define-key eshell-mode-map [(meta ?s)] 'eshell-next-matching-input)
245 (define-key eshell-command-map [(meta ?r)]
246 'eshell-previous-matching-input-from-input)
247 (define-key eshell-command-map [(meta ?s)]
248 'eshell-next-matching-input-from-input)
249 (if eshell-hist-match-partial
250 (progn
251 (define-key eshell-mode-map [(meta ?p)]
252 'eshell-previous-matching-input-from-input)
253 (define-key eshell-mode-map [(meta ?n)]
254 'eshell-next-matching-input-from-input)
255 (define-key eshell-command-map [(meta ?p)] 'eshell-previous-input)
256 (define-key eshell-command-map [(meta ?n)] 'eshell-next-input))
257 (define-key eshell-mode-map [(meta ?p)] 'eshell-previous-input)
258 (define-key eshell-mode-map [(meta ?n)] 'eshell-next-input)
259 (define-key eshell-command-map [(meta ?p)]
260 'eshell-previous-matching-input-from-input)
261 (define-key eshell-command-map [(meta ?n)]
262 'eshell-next-matching-input-from-input)))
263
264 (make-local-variable 'eshell-history-size)
265 (or eshell-history-size
266 (setq eshell-history-size (getenv "HISTSIZE")))
267
268 (make-local-variable 'eshell-history-file-name)
269 (or eshell-history-file-name
270 (setq eshell-history-file-name (getenv "HISTFILE")))
271
272 (make-local-variable 'eshell-history-index)
273 (make-local-variable 'eshell-save-history-index)
274
275 (if (minibuffer-window-active-p (selected-window))
276 (set (make-local-variable 'eshell-ask-to-save-history) nil)
277 (set (make-local-variable 'eshell-history-ring) nil)
278 (if eshell-history-file-name
279 (eshell-read-history nil t))
280
281 (make-local-hook 'eshell-exit-hook)
282 (add-hook 'eshell-exit-hook 'eshell-write-history nil t))
283
284 (unless eshell-history-ring
285 (setq eshell-history-ring (make-ring eshell-history-size)))
286
287 (make-local-hook 'eshell-exit-hook)
288 (add-hook 'eshell-exit-hook 'eshell-write-history nil t)
289
290 (add-hook 'kill-emacs-hook 'eshell-save-some-history)
291
292 (make-local-variable 'eshell-input-filter-functions)
293 (add-hook 'eshell-input-filter-functions 'eshell-add-to-history nil t)
294
295 (define-key eshell-command-map [(control ?l)] 'eshell-list-history)
296 (define-key eshell-command-map [(control ?x)] 'eshell-get-next-from-history))
297
298 (defun eshell-save-some-history ()
299 "Save the history for any open Eshell buffers."
300 (eshell-for buf (buffer-list)
301 (if (buffer-live-p buf)
302 (with-current-buffer buf
303 (if (and eshell-mode
304 eshell-history-file-name
305 eshell-ask-to-save-history
306 (or (eq eshell-ask-to-save-history 'always)
307 (y-or-n-p
308 (format "Save input history for Eshell buffer `%s'? "
309 (buffer-name buf)))))
310 (eshell-write-history))))))
311
312 (defun eshell/history (&rest args)
313 "List in help buffer the buffer's input history."
314 (eshell-init-print-buffer)
315 (eshell-eval-using-options
316 "history" args
317 '((?r "read" nil read-history
318 "read from history file to current history list")
319 (?w "write" nil write-history
320 "write current history list to history file")
321 (?a "append" nil append-history
322 "append current history list to history file")
323 (?h "help" nil nil "display this usage message")
324 :usage "[n] [-rwa [filename]]"
325 :post-usage
326 "When Eshell is started, history is read from `eshell-history-file-name'.
327 This is also the location where history info will be saved by this command,
328 unless a different file is specified on the command line.")
329 (and (or (not (ring-p eshell-history-ring))
330 (ring-empty-p eshell-history-ring))
331 (error "No history"))
332 (let (length command file)
333 (when (and args (string-match "^[0-9]+$" (car args)))
334 (setq length (min (eshell-convert (car args))
335 (ring-length eshell-history-ring))
336 args (cdr args)))
337 (and length
338 (or read-history write-history append-history)
339 (error "history: extra arguments"))
340 (when (and args (stringp (car args)))
341 (setq file (car args)
342 args (cdr args)))
343 (cond
344 (read-history (eshell-read-history file))
345 (write-history (eshell-write-history file))
346 (append-history (eshell-write-history file t))
347 (t
348 (let* ((history nil)
349 (index (1- (or length (ring-length eshell-history-ring))))
350 (ref (- (ring-length eshell-history-ring) index)))
351 ;; We have to build up a list ourselves from the ring vector.
352 (while (>= index 0)
353 (eshell-buffered-print
354 (format "%5d %s\n" ref (eshell-get-history index)))
355 (setq index (1- index)
356 ref (1+ ref)))))))
357 (eshell-flush)
358 nil))
359
360 (defun eshell-put-history (input &optional ring at-beginning)
361 "Put a new input line into the history ring."
362 (unless ring (setq ring eshell-history-ring))
363 (if at-beginning
364 (ring-insert-at-beginning ring input)
365 (ring-insert ring input)))
366
367 (defun eshell-get-history (index &optional ring)
368 "Get an input line from the history ring."
369 (ring-ref (or ring eshell-history-ring) index))
370
371 (defun eshell-add-input-to-history (input)
372 "Add the string INPUT to the history ring.
373 Input is entered into the input history ring, if the value of
374 variable `eshell-input-filter' returns non-nil when called on the
375 input."
376 (if (and (funcall eshell-input-filter input)
377 (or (null eshell-hist-ignoredups)
378 (not (ring-p eshell-history-ring))
379 (ring-empty-p eshell-history-ring)
380 (not (string-equal (eshell-get-history 0) input))))
381 (eshell-put-history input))
382 (setq eshell-save-history-index eshell-history-index)
383 (setq eshell-history-index nil))
384
385 (defun eshell-add-command-to-history ()
386 "Add the command entered at `eshell-command's prompt to the history ring.
387 The command is added to the input history ring, if the value of
388 variable `eshell-input-filter' returns non-nil when called on the
389 command.
390
391 This function is supposed to be called from the minibuffer, presumably
392 as a minibuffer-exit-hook."
393 (eshell-add-input-to-history
394 (buffer-substring (minibuffer-prompt-end) (point-max))))
395
396 (defun eshell-add-to-history ()
397 "Add last Eshell command to the history ring.
398 The command is entered into the input history ring, if the value of
399 variable `eshell-input-filter' returns non-nil when called on the
400 command."
401 (when (> (1- eshell-last-input-end) eshell-last-input-start)
402 (let ((input (buffer-substring eshell-last-input-start
403 (1- eshell-last-input-end))))
404 (eshell-add-input-to-history input))))
405
406 (defun eshell-read-history (&optional filename silent)
407 "Sets the buffer's `eshell-history-ring' from a history file.
408 The name of the file is given by the variable
409 `eshell-history-file-name'. The history ring is of size
410 `eshell-history-size', regardless of file size. If
411 `eshell-history-file-name' is nil this function does nothing.
412
413 If the optional argument SILENT is non-nil, we say nothing about a
414 failure to read the history file.
415
416 This function is useful for major mode commands and mode hooks.
417
418 The structure of the history file should be one input command per
419 line, with the most recent command last. See also
420 `eshell-hist-ignoredups' and `eshell-write-history'."
421 (let ((file (or filename eshell-history-file-name)))
422 (cond
423 ((or (null file)
424 (equal file ""))
425 nil)
426 ((not (file-readable-p file))
427 (or silent
428 (message "Cannot read history file %s" file)))
429 (t
430 (let* ((count 0)
431 (size eshell-history-size)
432 (ring (make-ring size))
433 (ignore-dups eshell-hist-ignoredups))
434 (with-temp-buffer
435 (insert-file-contents file)
436 ;; Save restriction in case file is already visited...
437 ;; Watch for those date stamps in history files!
438 (goto-char (point-max))
439 (while (and (< count size)
440 (re-search-backward "^[ \t]*\\([^#\n].*\\)[ \t]*$"
441 nil t))
442 (let ((history (match-string 1)))
443 (if (or (null ignore-dups)
444 (ring-empty-p ring)
445 (not (string-equal (ring-ref ring 0) history)))
446 (ring-insert-at-beginning
447 ring (subst-char-in-string ?\177 ?\n history))))
448 (setq count (1+ count))))
449 (setq eshell-history-ring ring
450 eshell-history-index nil))))))
451
452 (defun eshell-write-history (&optional filename append)
453 "Writes the buffer's `eshell-history-ring' to a history file.
454 The name of the file is given by the variable
455 `eshell-history-file-name'. The original contents of the file are
456 lost if `eshell-history-ring' is not empty. If
457 `eshell-history-file-name' is nil this function does nothing.
458
459 Useful within process sentinels.
460
461 See also `eshell-read-history'."
462 (let ((file (or filename eshell-history-file-name)))
463 (cond
464 ((or (null file)
465 (equal file "")
466 (null eshell-history-ring)
467 (ring-empty-p eshell-history-ring))
468 nil)
469 ((not (file-writable-p file))
470 (message "Cannot write history file %s" file))
471 (t
472 (let* ((ring eshell-history-ring)
473 (index (ring-length ring)))
474 ;; Write it all out into a buffer first. Much faster, but
475 ;; messier, than writing it one line at a time.
476 (with-temp-buffer
477 (while (> index 0)
478 (setq index (1- index))
479 (let ((start (point)))
480 (insert (ring-ref ring index) ?\n)
481 (subst-char-in-region start (1- (point)) ?\n ?\177)))
482 (eshell-with-private-file-modes
483 (write-region (point-min) (point-max) file append
484 'no-message))))))))
485
486 (defun eshell-list-history ()
487 "List in help buffer the buffer's input history."
488 (interactive)
489 (let (prefix prelen)
490 (save-excursion
491 (if (re-search-backward "!\\(.+\\)" (line-beginning-position) t)
492 (setq prefix (match-string 1)
493 prelen (length prefix))))
494 (if (or (not (ring-p eshell-history-ring))
495 (ring-empty-p eshell-history-ring))
496 (message "No history")
497 (let ((history nil)
498 (history-buffer " *Input History*")
499 (index (1- (ring-length eshell-history-ring)))
500 (conf (current-window-configuration)))
501 ;; We have to build up a list ourselves from the ring vector.
502 (while (>= index 0)
503 (let ((hist (eshell-get-history index)))
504 (if (or (not prefix)
505 (and (>= (length hist) prelen)
506 (string= (substring hist 0 prelen) prefix)))
507 (setq history (cons hist history))))
508 (setq index (1- index)))
509 ;; Change "completion" to "history reference"
510 ;; to make the display accurate.
511 (with-output-to-temp-buffer history-buffer
512 (display-completion-list history)
513 (set-buffer history-buffer)
514 (forward-line 3)
515 (while (search-backward "completion" nil 'move)
516 (replace-match "history reference")))
517 (eshell-redisplay)
518 (message "Hit space to flush")
519 (let ((ch (read-event)))
520 (if (eq ch ?\ )
521 (set-window-configuration conf)
522 (setq unread-command-events (list ch))))))))
523
524 (defun eshell-hist-word-reference (ref)
525 "Return the word designator index referred to by REF."
526 (cond
527 ((string-match "^[0-9]+$" ref)
528 (string-to-number ref))
529 ((string= "^" ref) 1)
530 ((string= "$" ref) nil)
531 ((string= "%" ref)
532 (error "`%' history word designator not yet implemented"))))
533
534 (defun eshell-hist-parse-arguments (&optional silent b e)
535 "Parse current command arguments in a history-code-friendly way."
536 (let ((end (or e (point)))
537 (begin (or b (save-excursion (eshell-bol) (point))))
538 (posb (list t))
539 (pose (list t))
540 (textargs (list t))
541 hist args)
542 (unless (catch 'eshell-incomplete
543 (ignore
544 (setq args (eshell-parse-arguments begin end))))
545 (save-excursion
546 (goto-char begin)
547 (while (< (point) end)
548 (if (get-text-property (point) 'arg-begin)
549 (nconc posb (list (point))))
550 (if (get-text-property (point) 'arg-end)
551 (nconc pose
552 (list (if (= (1+ (point)) end)
553 (1+ (point))
554 (point)))))
555 (forward-char))
556 (setq posb (cdr posb)
557 pose (cdr pose))
558 (assert (= (length posb) (length args)))
559 (assert (<= (length posb) (length pose))))
560 (setq hist (buffer-substring-no-properties begin end))
561 (let ((b posb) (e pose))
562 (while b
563 (nconc textargs
564 (list (substring hist (- (car b) begin)
565 (- (car e) begin))))
566 (setq b (cdr b)
567 e (cdr e))))
568 (setq textargs (cdr textargs))
569 (assert (= (length textargs) (length args)))
570 (list textargs posb pose))))
571
572 (defun eshell-expand-history-references (beg end)
573 "Parse and expand any history references in current input."
574 (let ((result (eshell-hist-parse-arguments t beg end)))
575 (when result
576 (let ((textargs (nreverse (nth 0 result)))
577 (posb (nreverse (nth 1 result)))
578 (pose (nreverse (nth 2 result))))
579 (save-excursion
580 (while textargs
581 (let ((str (eshell-history-reference (car textargs))))
582 (unless (eq str (car textargs))
583 (goto-char (car posb))
584 (insert-and-inherit str)
585 (delete-char (- (car pose) (car posb)))))
586 (setq textargs (cdr textargs)
587 posb (cdr posb)
588 pose (cdr pose))))))))
589
590 (defun eshell-complete-history-reference ()
591 "Complete a history reference, by completing the event designator."
592 (let ((arg (pcomplete-actual-arg)))
593 (when (string-match "\\`![^:^$*%]*\\'" arg)
594 (setq pcomplete-stub (substring arg 1)
595 pcomplete-last-completion-raw t)
596 (throw 'pcomplete-completions
597 (let ((history nil)
598 (index (1- (ring-length eshell-history-ring)))
599 (stublen (length pcomplete-stub)))
600 ;; We have to build up a list ourselves from the ring
601 ;; vector.
602 (while (>= index 0)
603 (let ((hist (eshell-get-history index)))
604 (if (and (>= (length hist) stublen)
605 (string= (substring hist 0 stublen)
606 pcomplete-stub)
607 (string-match "^\\([^:^$*% \t\n]+\\)" hist))
608 (setq history (cons (match-string 1 hist)
609 history))))
610 (setq index (1- index)))
611 (let ((fhist (list t)))
612 ;; uniqify the list, but preserve the order
613 (while history
614 (unless (member (car history) fhist)
615 (nconc fhist (list (car history))))
616 (setq history (cdr history)))
617 (cdr fhist)))))))
618
619 (defun eshell-history-reference (reference)
620 "Expand directory stack REFERENCE.
621 The syntax used here was taken from the Bash info manual.
622 Returns the resultant reference, or the same string REFERENCE if none
623 matched."
624 ;; `^string1^string2^'
625 ;; Quick Substitution. Repeat the last command, replacing
626 ;; STRING1 with STRING2. Equivalent to `!!:s/string1/string2/'
627 (if (and (eshell-using-module 'eshell-pred)
628 (string-match "\\^\\([^^]+\\)\\^\\([^^]+\\)\\^?\\s-*$"
629 reference))
630 (setq reference (format "!!:s/%s/%s/"
631 (match-string 1 reference)
632 (match-string 2 reference))))
633 ;; `!'
634 ;; Start a history substitution, except when followed by a
635 ;; space, tab, the end of the line, = or (.
636 (if (not (string-match "^![^ \t\n=\(]" reference))
637 reference
638 (setq eshell-history-index nil)
639 (let ((event (eshell-hist-parse-event-designator reference)))
640 (unless event
641 (error "Could not find history event `%s'" reference))
642 (setq eshell-history-index (car event)
643 reference (substring reference (cdr event))
644 event (eshell-get-history eshell-history-index))
645 (if (not (string-match "^[:^$*%]" reference))
646 event
647 (let ((word (eshell-hist-parse-word-designator
648 event reference)))
649 (unless word
650 (error "Unable to honor word designator `%s'" reference))
651 (unless (string-match "^[:^$*%][[$^*%0-9-]" reference)
652 (setcdr word 0))
653 (setq event (car word)
654 reference (substring reference (cdr word)))
655 (if (not (and (eshell-using-module 'eshell-pred)
656 (string-match "^:" reference)))
657 event
658 (eshell-hist-parse-modifier event reference)))))))
659
660 (defun eshell-hist-parse-event-designator (reference)
661 "Parse a history event designator beginning in REFERENCE."
662 (let* ((index (string-match eshell-hist-event-designator reference))
663 (end (and index (match-end 0))))
664 (unless index
665 (error "Invalid history event designator `%s'" reference))
666 (let* ((event (match-string 1 reference))
667 (pos
668 (cond
669 ((string= event "!") (ring-length eshell-history-ring))
670 ((string= event "#") (error "!# not yet implemented"))
671 ((string-match "^-?[0-9]+$" event)
672 (let ((num (string-to-number event)))
673 (if (>= num 0)
674 (- (ring-length eshell-history-ring) num)
675 (1- (abs num)))))
676 ((string-match "^\\(\\??\\)\\([^?]+\\)\\??$" event)
677 (let ((pref (if (> (length (match-string 1 event)) 0)
678 "" "^"))
679 (str (match-string 2 event)))
680 (save-match-data
681 (eshell-previous-matching-input-string-position
682 (concat pref (regexp-quote str)) 1))))
683 (t
684 (error "Failed to parse event designator `%s'" event)))))
685 (and pos (cons pos end)))))
686
687 (defun eshell-hist-parse-word-designator (hist reference)
688 "Parse a history word designator beginning for HIST in REFERENCE."
689 (let* ((index (string-match eshell-hist-word-designator reference))
690 (end (and index (match-end 0))))
691 (unless (memq (aref reference 0) '(?: ?^ ?$ ?* ?%))
692 (error "Invalid history word designator `%s'" reference))
693 (let ((nth (match-string 1 reference))
694 (mth (match-string 2 reference))
695 (here (point))
696 textargs)
697 (insert hist)
698 (setq textargs (car (eshell-hist-parse-arguments nil here (point))))
699 (delete-region here (point))
700 (if (string= nth "*")
701 (if mth
702 (error "Invalid history word designator `%s'"
703 reference)
704 (setq nth 1 mth "-$")))
705 (if (not mth)
706 (if nth
707 (setq mth nth)
708 (setq nth 0 mth "$"))
709 (if (string= mth "-")
710 (setq mth (- (length textargs) 2))
711 (if (string= mth "*")
712 (setq mth "$")
713 (if (not (and (> (length mth) 1)
714 (eq (aref mth 0) ?-)))
715 (error "Invalid history word designator `%s'"
716 reference)
717 (setq mth (substring mth 1))))))
718 (unless (numberp nth)
719 (setq nth (eshell-hist-word-reference nth)))
720 (unless (numberp mth)
721 (setq mth (eshell-hist-word-reference mth)))
722 (cons (mapconcat 'identity (eshell-sublist textargs nth mth) "")
723 end))))
724
725 (defun eshell-hist-parse-modifier (hist reference)
726 "Parse a history modifier beginning for HIST in REFERENCE."
727 (let ((here (point)))
728 (insert reference)
729 (prog1
730 (save-restriction
731 (narrow-to-region here (point))
732 (goto-char (point-min))
733 (let ((modifiers (cdr (eshell-parse-modifiers))))
734 (eshell-for mod modifiers
735 (setq hist (funcall mod hist)))
736 hist))
737 (delete-region here (point)))))
738
739 (defun eshell-get-next-from-history ()
740 "After fetching a line from input history, this fetches the next.
741 In other words, this recalls the input line after the line you
742 recalled last. You can use this to repeat a sequence of input lines."
743 (interactive)
744 (if eshell-save-history-index
745 (progn
746 (setq eshell-history-index (1+ eshell-save-history-index))
747 (eshell-next-input 1))
748 (message "No previous history command")))
749
750 (defun eshell-search-arg (arg)
751 ;; First make sure there is a ring and that we are after the process
752 ;; mark
753 (if (and eshell-hist-move-to-end
754 (< (point) eshell-last-output-end))
755 (goto-char eshell-last-output-end))
756 (cond ((or (null eshell-history-ring)
757 (ring-empty-p eshell-history-ring))
758 (error "Empty input ring"))
759 ((zerop arg)
760 ;; arg of zero resets search from beginning, and uses arg of
761 ;; 1
762 (setq eshell-history-index nil)
763 1)
764 (t
765 arg)))
766
767 (defun eshell-search-start (arg)
768 "Index to start a directional search, starting at `eshell-history-index'."
769 (if eshell-history-index
770 ;; If a search is running, offset by 1 in direction of arg
771 (mod (+ eshell-history-index (if (> arg 0) 1 -1))
772 (ring-length eshell-history-ring))
773 ;; For a new search, start from beginning or end, as appropriate
774 (if (>= arg 0)
775 0 ; First elt for forward search
776 ;; Last elt for backward search
777 (1- (ring-length eshell-history-ring)))))
778
779 (defun eshell-previous-input-string (arg)
780 "Return the string ARG places along the input ring.
781 Moves relative to `eshell-history-index'."
782 (eshell-get-history (if eshell-history-index
783 (mod (+ arg eshell-history-index)
784 (ring-length eshell-history-ring))
785 arg)))
786
787 (defun eshell-previous-input (arg)
788 "Cycle backwards through input history."
789 (interactive "*p")
790 (eshell-previous-matching-input "." arg))
791
792 (defun eshell-next-input (arg)
793 "Cycle forwards through input history."
794 (interactive "*p")
795 (eshell-previous-input (- arg)))
796
797 (defun eshell-previous-matching-input-string (regexp arg)
798 "Return the string matching REGEXP ARG places along the input ring.
799 Moves relative to `eshell-history-index'."
800 (let* ((pos (eshell-previous-matching-input-string-position regexp arg)))
801 (if pos (eshell-get-history pos))))
802
803 (defun eshell-previous-matching-input-string-position
804 (regexp arg &optional start)
805 "Return the index matching REGEXP ARG places along the input ring.
806 Moves relative to START, or `eshell-history-index'."
807 (if (or (not (ring-p eshell-history-ring))
808 (ring-empty-p eshell-history-ring))
809 (error "No history"))
810 (let* ((len (ring-length eshell-history-ring))
811 (motion (if (> arg 0) 1 -1))
812 (n (mod (- (or start (eshell-search-start arg)) motion) len))
813 (tried-each-ring-item nil)
814 (case-fold-search (eshell-under-windows-p))
815 (prev nil))
816 ;; Do the whole search as many times as the argument says.
817 (while (and (/= arg 0) (not tried-each-ring-item))
818 ;; Step once.
819 (setq prev n
820 n (mod (+ n motion) len))
821 ;; If we haven't reached a match, step some more.
822 (while (and (< n len) (not tried-each-ring-item)
823 (not (string-match regexp (eshell-get-history n))))
824 (setq n (mod (+ n motion) len)
825 ;; If we have gone all the way around in this search.
826 tried-each-ring-item (= n prev)))
827 (setq arg (if (> arg 0) (1- arg) (1+ arg))))
828 ;; Now that we know which ring element to use, if we found it,
829 ;; return that.
830 (if (string-match regexp (eshell-get-history n))
831 n)))
832
833 (defun eshell-previous-matching-input (regexp arg)
834 "Search backwards through input history for match for REGEXP.
835 \(Previous history elements are earlier commands.)
836 With prefix argument N, search for Nth previous match.
837 If N is negative, find the next or Nth next match."
838 (interactive (eshell-regexp-arg "Previous input matching (regexp): "))
839 (setq arg (eshell-search-arg arg))
840 (let ((pos (eshell-previous-matching-input-string-position regexp arg)))
841 ;; Has a match been found?
842 (if (null pos)
843 (error "Not found")
844 (setq eshell-history-index pos)
845 (unless (minibuffer-window-active-p (selected-window))
846 (message "History item: %d" (- (ring-length eshell-history-ring) pos)))
847 ;; Can't use kill-region as it sets this-command
848 (delete-region (save-excursion (eshell-bol) (point)) (point))
849 (insert-and-inherit (eshell-get-history pos)))))
850
851 (defun eshell-next-matching-input (regexp arg)
852 "Search forwards through input history for match for REGEXP.
853 \(Later history elements are more recent commands.)
854 With prefix argument N, search for Nth following match.
855 If N is negative, find the previous or Nth previous match."
856 (interactive (eshell-regexp-arg "Next input matching (regexp): "))
857 (eshell-previous-matching-input regexp (- arg)))
858
859 (defun eshell-previous-matching-input-from-input (arg)
860 "Search backwards through input history for match for current input.
861 \(Previous history elements are earlier commands.)
862 With prefix argument N, search for Nth previous match.
863 If N is negative, search forwards for the -Nth following match."
864 (interactive "p")
865 (if (not (memq last-command '(eshell-previous-matching-input-from-input
866 eshell-next-matching-input-from-input)))
867 ;; Starting a new search
868 (setq eshell-matching-input-from-input-string
869 (buffer-substring (save-excursion (eshell-bol) (point))
870 (point))
871 eshell-history-index nil))
872 (eshell-previous-matching-input
873 (concat "^" (regexp-quote eshell-matching-input-from-input-string))
874 arg))
875
876 (defun eshell-next-matching-input-from-input (arg)
877 "Search forwards through input history for match for current input.
878 \(Following history elements are more recent commands.)
879 With prefix argument N, search for Nth following match.
880 If N is negative, search backwards for the -Nth previous match."
881 (interactive "p")
882 (eshell-previous-matching-input-from-input (- arg)))
883
884 (defun eshell-test-imatch ()
885 "If isearch match good, put point at the beginning and return non-nil."
886 (if (get-text-property (point) 'history)
887 (progn (beginning-of-line) t)
888 (let ((before (point)))
889 (eshell-bol)
890 (if (and (not (bolp))
891 (<= (point) before))
892 t
893 (if isearch-forward
894 (progn
895 (end-of-line)
896 (forward-char))
897 (beginning-of-line)
898 (backward-char))))))
899
900 (defun eshell-return-to-prompt ()
901 "Once a search string matches, insert it at the end and go there."
902 (setq isearch-other-end nil)
903 (let ((found (eshell-test-imatch)) before)
904 (while (and (not found)
905 (setq before
906 (funcall (if isearch-forward
907 're-search-forward
908 're-search-backward)
909 isearch-string nil t)))
910 (setq found (eshell-test-imatch)))
911 (if (not found)
912 (progn
913 (goto-char eshell-last-output-end)
914 (delete-region (point) (point-max)))
915 (setq before (point))
916 (let ((text (buffer-substring-no-properties
917 (point) (line-end-position)))
918 (orig (marker-position eshell-last-output-end)))
919 (goto-char eshell-last-output-end)
920 (delete-region (point) (point-max))
921 (when (and text (> (length text) 0))
922 (insert text)
923 (put-text-property (1- (point)) (point)
924 'last-search-pos before)
925 (set-marker eshell-last-output-end orig)
926 (goto-char eshell-last-output-end))))))
927
928 (defun eshell-prepare-for-search ()
929 "Make sure the old history file is at the beginning of the buffer."
930 (unless (get-text-property (point-min) 'history)
931 (save-excursion
932 (goto-char (point-min))
933 (let ((end (copy-marker (point) t)))
934 (insert-file-contents eshell-history-file-name)
935 (set-text-properties (point-min) end
936 '(history t invisible t))))))
937
938 (defun eshell-isearch-backward (&optional invert)
939 "Do incremental regexp search backward through past commands."
940 (interactive)
941 (let ((inhibit-read-only t) end)
942 (eshell-prepare-for-search)
943 (goto-char (point-max))
944 (set-marker eshell-last-output-end (point))
945 (delete-region (point) (point-max)))
946 (isearch-mode invert t 'eshell-return-to-prompt))
947
948 (defun eshell-isearch-repeat-backward (&optional invert)
949 "Do incremental regexp search backward through past commands."
950 (interactive)
951 (let ((old-pos (get-text-property (1- (point-max))
952 'last-search-pos)))
953 (when old-pos
954 (goto-char old-pos)
955 (if invert
956 (end-of-line)
957 (backward-char)))
958 (setq isearch-forward invert)
959 (isearch-search-and-update)))
960
961 (defun eshell-isearch-forward ()
962 "Do incremental regexp search backward through past commands."
963 (interactive)
964 (eshell-isearch-backward t))
965
966 (defun eshell-isearch-repeat-forward ()
967 "Do incremental regexp search backward through past commands."
968 (interactive)
969 (eshell-isearch-repeat-backward t))
970
971 (defun eshell-isearch-cancel ()
972 (interactive)
973 (goto-char eshell-last-output-end)
974 (delete-region (point) (point-max))
975 (call-interactively 'isearch-cancel))
976
977 (defun eshell-isearch-abort ()
978 (interactive)
979 (goto-char eshell-last-output-end)
980 (delete-region (point) (point-max))
981 (call-interactively 'isearch-abort))
982
983 (defun eshell-isearch-delete-char ()
984 (interactive)
985 (save-excursion
986 (isearch-delete-char)))
987
988 (defun eshell-isearch-return ()
989 (interactive)
990 (isearch-done)
991 (eshell-send-input))
992
993 ;;; em-hist.el ends here