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