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