Mercurial > emacs
annotate lisp/eshell/em-hist.el @ 110261:8a1ec5899552
gnus-async.el (gnus-html-prefetch-images): Autoload it when compiling; (gnus-async-article-callback): Fix typo.
author | Katsumi Yamaoka <yamaoka@jpl.org> |
---|---|
date | Tue, 07 Sep 2010 01:20:19 +0000 |
parents | 1d1d5d9bd884 |
children | f57f72bb4757 27e06e53f81b |
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, |
106815 | 4 ;; 2008, 2009, 2010 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 | |
98564
f79ec7c34dc5
Sven Joachim <svenjoac at gmx.de>
Glenn Morris <rgm@gnu.org>
parents:
95152
diff
changeset
|
58 (eval-when-compile |
f79ec7c34dc5
Sven Joachim <svenjoac at gmx.de>
Glenn Morris <rgm@gnu.org>
parents:
95152
diff
changeset
|
59 (require 'cl)) |
f79ec7c34dc5
Sven Joachim <svenjoac at gmx.de>
Glenn Morris <rgm@gnu.org>
parents:
95152
diff
changeset
|
60 |
29876 | 61 (require 'ring) |
62 (require 'esh-opt) | |
63 (require 'em-pred) | |
87068
27cfd1655fb2
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
78220
diff
changeset
|
64 (require 'eshell) |
27cfd1655fb2
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
78220
diff
changeset
|
65 |
95152
ad5d26b1d5d1
Use eshell-defgroup rather than defgroup.
Glenn Morris <rgm@gnu.org>
parents:
94661
diff
changeset
|
66 ;;;###autoload |
ad5d26b1d5d1
Use eshell-defgroup rather than defgroup.
Glenn Morris <rgm@gnu.org>
parents:
94661
diff
changeset
|
67 (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
|
68 "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
|
69 :tag "History list management" |
27cfd1655fb2
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
78220
diff
changeset
|
70 :group 'eshell-module) |
29876 | 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 | |
104994
48428e99bf83
* eshell/em-hist.el:
Chong Yidong <cyd@stupidchicken.com>
parents:
100908
diff
changeset
|
89 (expand-file-name "history" eshell-directory-name) |
29876 | 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 | |
99341
ec72437027f8
(eshell-save-history-on-exit): Change default to t.
Chong Yidong <cyd@stupidchicken.com>
parents:
98564
diff
changeset
|
107 (defcustom eshell-save-history-on-exit t |
29876 | 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. | |
49266
f4d32be2d504
(eshell-save-history-on-exit): Renamed `eshell-ask-to-save-history'
John Wiegley <johnw@newartisans.com>
parents:
46852
diff
changeset
|
112 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
|
113 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
|
114 If set to t, history will always be saved, silently." |
29876 | 115 :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
|
116 (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
|
117 (const :tag "Always save" t)) |
29876 | 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 | |
64384
54d5227ff298
(eshell-rebind-keys-alist): Add defvar.
Richard M. Stallman <rms@gnu.org>
parents:
64085
diff
changeset
|
206 (defvar eshell-rebind-keys-alist) |
54d5227ff298
(eshell-rebind-keys-alist): Add defvar.
Richard M. Stallman <rms@gnu.org>
parents:
64085
diff
changeset
|
207 |
29876 | 208 ;;; Functions: |
209 | |
210 (defun eshell-hist-initialize () | |
211 "Initialize the history management code for one Eshell buffer." | |
212 (add-hook 'eshell-expand-input-functions | |
213 'eshell-expand-history-references nil t) | |
214 | |
215 (when (eshell-using-module 'eshell-cmpl) | |
216 (add-hook 'pcomplete-try-first-hook | |
217 'eshell-complete-history-reference nil t)) | |
218 | |
43322
80c00f33bf18
(eshell-hist-initialize): When in the minibuffer, use the global value
John Wiegley <johnw@newartisans.com>
parents:
38414
diff
changeset
|
219 (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
|
220 (not eshell-non-interactive-p)) |
64384
54d5227ff298
(eshell-rebind-keys-alist): Add defvar.
Richard M. Stallman <rms@gnu.org>
parents:
64085
diff
changeset
|
221 (let ((rebind-alist eshell-rebind-keys-alist)) |
29876 | 222 (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
|
223 (setq eshell-rebind-keys-alist |
54d5227ff298
(eshell-rebind-keys-alist): Add defvar.
Richard M. Stallman <rms@gnu.org>
parents:
64085
diff
changeset
|
224 (append rebind-alist eshell-hist-rebind-keys-alist)) |
29876 | 225 (set (make-local-variable 'search-invisible) t) |
226 (set (make-local-variable 'search-exit-option) t) | |
227 (add-hook 'isearch-mode-hook | |
228 (function | |
229 (lambda () | |
230 (if (>= (point) eshell-last-output-end) | |
231 (setq overriding-terminal-local-map | |
232 eshell-isearch-map)))) nil t) | |
233 (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) | |
43322
80c00f33bf18
(eshell-hist-initialize): When in the minibuffer, use the global value
John Wiegley <johnw@newartisans.com>
parents:
38414
diff
changeset
|
272 |
80c00f33bf18
(eshell-hist-initialize): When in the minibuffer, use the global value
John Wiegley <johnw@newartisans.com>
parents:
38414
diff
changeset
|
273 (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
|
274 (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
|
275 (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
|
276 (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
|
277 (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
|
278 |
80c00f33bf18
(eshell-hist-initialize): When in the minibuffer, use the global value
John Wiegley <johnw@newartisans.com>
parents:
38414
diff
changeset
|
279 (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
|
280 |
29876 | 281 (unless eshell-history-ring |
282 (setq eshell-history-ring (make-ring eshell-history-size))) | |
283 | |
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 | |
49266
f4d32be2d504
(eshell-save-history-on-exit): Renamed `eshell-ask-to-save-history'
John Wiegley <johnw@newartisans.com>
parents:
46852
diff
changeset
|
301 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
|
302 (or (eq eshell-save-history-on-exit t) |
29876 | 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)) | |
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." | |
31241 | 365 (ring-ref (or ring eshell-history-ring) index)) |
29876 | 366 |
43322
80c00f33bf18
(eshell-hist-initialize): When in the minibuffer, use the global value
John Wiegley <johnw@newartisans.com>
parents:
38414
diff
changeset
|
367 (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
|
368 "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
|
369 Input is entered into the input history ring, if the value of |
29876 | 370 variable `eshell-input-filter' returns non-nil when called on the |
371 input." | |
43322
80c00f33bf18
(eshell-hist-initialize): When in the minibuffer, use the global value
John Wiegley <johnw@newartisans.com>
parents:
38414
diff
changeset
|
372 (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
|
373 (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
|
374 (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
|
375 (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
|
376 (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
|
377 (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
|
378 (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
|
379 (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
|
380 |
80c00f33bf18
(eshell-hist-initialize): When in the minibuffer, use the global value
John Wiegley <johnw@newartisans.com>
parents:
38414
diff
changeset
|
381 (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
|
382 "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
|
383 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
|
384 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
|
385 command. |
80c00f33bf18
(eshell-hist-initialize): When in the minibuffer, use the global value
John Wiegley <johnw@newartisans.com>
parents:
38414
diff
changeset
|
386 |
80c00f33bf18
(eshell-hist-initialize): When in the minibuffer, use the global value
John Wiegley <johnw@newartisans.com>
parents:
38414
diff
changeset
|
387 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
|
388 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
|
389 (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
|
390 (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
|
391 |
80c00f33bf18
(eshell-hist-initialize): When in the minibuffer, use the global value
John Wiegley <johnw@newartisans.com>
parents:
38414
diff
changeset
|
392 (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
|
393 "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
|
394 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
|
395 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
|
396 command." |
29876 | 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)))) | |
43322
80c00f33bf18
(eshell-hist-initialize): When in the minibuffer, use the global value
John Wiegley <johnw@newartisans.com>
parents:
38414
diff
changeset
|
400 (eshell-add-input-to-history input)))) |
29876 | 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))) | |
31241 | 442 (ring-insert-at-beginning |
443 ring (subst-char-in-string ?\177 ?\n history)))) | |
29876 | 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)) | |
31241 | 475 (let ((start (point))) |
476 (insert (ring-ref ring index) ?\n) | |
477 (subst-char-in-region start (1- (point)) ?\n ?\177))) | |
29876 | 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 | |
66114
13abee3a9bc6
* message.el (message-expand-group): Pass the common
Masatake YAMATO <jet@gyve.org>
parents:
64701
diff
changeset
|
508 (display-completion-list history prefix) |
29876 | 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) | |
53871
dbe1ef05fa24
(eshell-hist-word-reference): Fix format
Andreas Schwab <schwab@suse.de>
parents:
52401
diff
changeset
|
528 (error "`%%' history word designator not yet implemented")))) |
29876 | 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 | |
104994
48428e99bf83
* eshell/em-hist.el:
Chong Yidong <cyd@stupidchicken.com>
parents:
100908
diff
changeset
|
586 (defvar pcomplete-stub) |
48428e99bf83
* eshell/em-hist.el:
Chong Yidong <cyd@stupidchicken.com>
parents:
100908
diff
changeset
|
587 (defvar pcomplete-last-completion-raw) |
48428e99bf83
* eshell/em-hist.el:
Chong Yidong <cyd@stupidchicken.com>
parents:
100908
diff
changeset
|
588 (declare-function pcomplete-actual-arg "pcomplete") |
48428e99bf83
* eshell/em-hist.el:
Chong Yidong <cyd@stupidchicken.com>
parents:
100908
diff
changeset
|
589 |
29876 | 590 (defun eshell-complete-history-reference () |
591 "Complete a history reference, by completing the event designator." | |
592 (let ((arg (pcomplete-actual-arg))) | |
593 (when (string-match "\\`![^:^$*%]*\\'" arg) | |
594 (setq pcomplete-stub (substring arg 1) | |
595 pcomplete-last-completion-raw t) | |
596 (throw 'pcomplete-completions | |
597 (let ((history nil) | |
598 (index (1- (ring-length eshell-history-ring))) | |
599 (stublen (length pcomplete-stub))) | |
600 ;; We have to build up a list ourselves from the ring | |
601 ;; vector. | |
602 (while (>= index 0) | |
603 (let ((hist (eshell-get-history index))) | |
604 (if (and (>= (length hist) stublen) | |
605 (string= (substring hist 0 stublen) | |
606 pcomplete-stub) | |
607 (string-match "^\\([^:^$*% \t\n]+\\)" hist)) | |
608 (setq history (cons (match-string 1 hist) | |
609 history)))) | |
610 (setq index (1- index))) | |
611 (let ((fhist (list t))) | |
612 ;; uniqify the list, but preserve the order | |
613 (while history | |
614 (unless (member (car history) fhist) | |
615 (nconc fhist (list (car history)))) | |
616 (setq history (cdr history))) | |
617 (cdr fhist))))))) | |
618 | |
619 (defun eshell-history-reference (reference) | |
620 "Expand directory stack REFERENCE. | |
621 The syntax used here was taken from the Bash info manual. | |
622 Returns the resultant reference, or the same string REFERENCE if none | |
623 matched." | |
624 ;; `^string1^string2^' | |
625 ;; Quick Substitution. Repeat the last command, replacing | |
626 ;; STRING1 with STRING2. Equivalent to `!!:s/string1/string2/' | |
627 (if (and (eshell-using-module 'eshell-pred) | |
628 (string-match "\\^\\([^^]+\\)\\^\\([^^]+\\)\\^?\\s-*$" | |
629 reference)) | |
630 (setq reference (format "!!:s/%s/%s/" | |
631 (match-string 1 reference) | |
632 (match-string 2 reference)))) | |
633 ;; `!' | |
634 ;; Start a history substitution, except when followed by a | |
635 ;; space, tab, the end of the line, = or (. | |
636 (if (not (string-match "^![^ \t\n=\(]" reference)) | |
637 reference | |
638 (setq eshell-history-index nil) | |
639 (let ((event (eshell-hist-parse-event-designator reference))) | |
640 (unless event | |
641 (error "Could not find history event `%s'" reference)) | |
642 (setq eshell-history-index (car event) | |
643 reference (substring reference (cdr event)) | |
644 event (eshell-get-history eshell-history-index)) | |
645 (if (not (string-match "^[:^$*%]" reference)) | |
646 event | |
647 (let ((word (eshell-hist-parse-word-designator | |
648 event reference))) | |
649 (unless word | |
650 (error "Unable to honor word designator `%s'" reference)) | |
651 (unless (string-match "^[:^$*%][[$^*%0-9-]" reference) | |
652 (setcdr word 0)) | |
653 (setq event (car word) | |
654 reference (substring reference (cdr word))) | |
655 (if (not (and (eshell-using-module 'eshell-pred) | |
656 (string-match "^:" reference))) | |
657 event | |
658 (eshell-hist-parse-modifier event reference))))))) | |
659 | |
660 (defun eshell-hist-parse-event-designator (reference) | |
661 "Parse a history event designator beginning in REFERENCE." | |
662 (let* ((index (string-match eshell-hist-event-designator reference)) | |
663 (end (and index (match-end 0)))) | |
664 (unless index | |
665 (error "Invalid history event designator `%s'" reference)) | |
666 (let* ((event (match-string 1 reference)) | |
667 (pos | |
668 (cond | |
669 ((string= event "!") (ring-length eshell-history-ring)) | |
670 ((string= event "#") (error "!# not yet implemented")) | |
671 ((string-match "^-?[0-9]+$" event) | |
672 (let ((num (string-to-number event))) | |
673 (if (>= num 0) | |
674 (- (ring-length eshell-history-ring) num) | |
675 (1- (abs num))))) | |
676 ((string-match "^\\(\\??\\)\\([^?]+\\)\\??$" event) | |
677 (let ((pref (if (> (length (match-string 1 event)) 0) | |
678 "" "^")) | |
679 (str (match-string 2 event))) | |
680 (save-match-data | |
681 (eshell-previous-matching-input-string-position | |
682 (concat pref (regexp-quote str)) 1)))) | |
683 (t | |
684 (error "Failed to parse event designator `%s'" event))))) | |
685 (and pos (cons pos end))))) | |
686 | |
687 (defun eshell-hist-parse-word-designator (hist reference) | |
688 "Parse a history word designator beginning for HIST in REFERENCE." | |
689 (let* ((index (string-match eshell-hist-word-designator reference)) | |
690 (end (and index (match-end 0)))) | |
691 (unless (memq (aref reference 0) '(?: ?^ ?$ ?* ?%)) | |
692 (error "Invalid history word designator `%s'" reference)) | |
693 (let ((nth (match-string 1 reference)) | |
694 (mth (match-string 2 reference)) | |
695 (here (point)) | |
696 textargs) | |
697 (insert hist) | |
698 (setq textargs (car (eshell-hist-parse-arguments nil here (point)))) | |
699 (delete-region here (point)) | |
700 (if (string= nth "*") | |
701 (if mth | |
702 (error "Invalid history word designator `%s'" | |
703 reference) | |
704 (setq nth 1 mth "-$"))) | |
705 (if (not mth) | |
706 (if nth | |
707 (setq mth nth) | |
708 (setq nth 0 mth "$")) | |
709 (if (string= mth "-") | |
710 (setq mth (- (length textargs) 2)) | |
711 (if (string= mth "*") | |
712 (setq mth "$") | |
713 (if (not (and (> (length mth) 1) | |
714 (eq (aref mth 0) ?-))) | |
715 (error "Invalid history word designator `%s'" | |
716 reference) | |
717 (setq mth (substring mth 1)))))) | |
718 (unless (numberp nth) | |
719 (setq nth (eshell-hist-word-reference nth))) | |
720 (unless (numberp mth) | |
721 (setq mth (eshell-hist-word-reference mth))) | |
722 (cons (mapconcat 'identity (eshell-sublist textargs nth mth) "") | |
723 end)))) | |
724 | |
725 (defun eshell-hist-parse-modifier (hist reference) | |
726 "Parse a history modifier beginning for HIST in REFERENCE." | |
727 (let ((here (point))) | |
728 (insert reference) | |
729 (prog1 | |
730 (save-restriction | |
731 (narrow-to-region here (point)) | |
732 (goto-char (point-min)) | |
733 (let ((modifiers (cdr (eshell-parse-modifiers)))) | |
734 (eshell-for mod modifiers | |
735 (setq hist (funcall mod hist))) | |
736 hist)) | |
737 (delete-region here (point))))) | |
738 | |
739 (defun eshell-get-next-from-history () | |
740 "After fetching a line from input history, this fetches the next. | |
741 In other words, this recalls the input line after the line you | |
742 recalled last. You can use this to repeat a sequence of input lines." | |
743 (interactive) | |
744 (if eshell-save-history-index | |
745 (progn | |
746 (setq eshell-history-index (1+ eshell-save-history-index)) | |
747 (eshell-next-input 1)) | |
748 (message "No previous history command"))) | |
749 | |
750 (defun eshell-search-arg (arg) | |
751 ;; First make sure there is a ring and that we are after the process | |
752 ;; mark | |
753 (if (and eshell-hist-move-to-end | |
754 (< (point) eshell-last-output-end)) | |
755 (goto-char eshell-last-output-end)) | |
756 (cond ((or (null eshell-history-ring) | |
757 (ring-empty-p eshell-history-ring)) | |
758 (error "Empty input ring")) | |
759 ((zerop arg) | |
760 ;; arg of zero resets search from beginning, and uses arg of | |
761 ;; 1 | |
762 (setq eshell-history-index nil) | |
763 1) | |
764 (t | |
765 arg))) | |
766 | |
767 (defun eshell-search-start (arg) | |
768 "Index to start a directional search, starting at `eshell-history-index'." | |
769 (if eshell-history-index | |
770 ;; If a search is running, offset by 1 in direction of arg | |
771 (mod (+ eshell-history-index (if (> arg 0) 1 -1)) | |
772 (ring-length eshell-history-ring)) | |
773 ;; For a new search, start from beginning or end, as appropriate | |
774 (if (>= arg 0) | |
775 0 ; First elt for forward search | |
776 ;; Last elt for backward search | |
777 (1- (ring-length eshell-history-ring))))) | |
778 | |
779 (defun eshell-previous-input-string (arg) | |
780 "Return the string ARG places along the input ring. | |
781 Moves relative to `eshell-history-index'." | |
782 (eshell-get-history (if eshell-history-index | |
783 (mod (+ arg eshell-history-index) | |
784 (ring-length eshell-history-ring)) | |
785 arg))) | |
786 | |
787 (defun eshell-previous-input (arg) | |
788 "Cycle backwards through input history." | |
789 (interactive "*p") | |
790 (eshell-previous-matching-input "." arg)) | |
791 | |
792 (defun eshell-next-input (arg) | |
793 "Cycle forwards through input history." | |
794 (interactive "*p") | |
795 (eshell-previous-input (- arg))) | |
796 | |
797 (defun eshell-previous-matching-input-string (regexp arg) | |
798 "Return the string matching REGEXP ARG places along the input ring. | |
799 Moves relative to `eshell-history-index'." | |
800 (let* ((pos (eshell-previous-matching-input-string-position regexp arg))) | |
801 (if pos (eshell-get-history pos)))) | |
802 | |
803 (defun eshell-previous-matching-input-string-position | |
804 (regexp arg &optional start) | |
805 "Return the index matching REGEXP ARG places along the input ring. | |
806 Moves relative to START, or `eshell-history-index'." | |
807 (if (or (not (ring-p eshell-history-ring)) | |
808 (ring-empty-p eshell-history-ring)) | |
809 (error "No history")) | |
810 (let* ((len (ring-length eshell-history-ring)) | |
811 (motion (if (> arg 0) 1 -1)) | |
812 (n (mod (- (or start (eshell-search-start arg)) motion) len)) | |
813 (tried-each-ring-item nil) | |
46852
6eb625bead4f
Removed eshell-under-cygwin-p, and all uses of it.
John Wiegley <johnw@newartisans.com>
parents:
46820
diff
changeset
|
814 (case-fold-search (eshell-under-windows-p)) |
29876 | 815 (prev nil)) |
816 ;; Do the whole search as many times as the argument says. | |
817 (while (and (/= arg 0) (not tried-each-ring-item)) | |
818 ;; Step once. | |
819 (setq prev n | |
820 n (mod (+ n motion) len)) | |
821 ;; If we haven't reached a match, step some more. | |
822 (while (and (< n len) (not tried-each-ring-item) | |
823 (not (string-match regexp (eshell-get-history n)))) | |
824 (setq n (mod (+ n motion) len) | |
825 ;; If we have gone all the way around in this search. | |
826 tried-each-ring-item (= n prev))) | |
827 (setq arg (if (> arg 0) (1- arg) (1+ arg)))) | |
828 ;; Now that we know which ring element to use, if we found it, | |
829 ;; return that. | |
830 (if (string-match regexp (eshell-get-history n)) | |
831 n))) | |
832 | |
833 (defun eshell-previous-matching-input (regexp arg) | |
834 "Search backwards through input history for match for REGEXP. | |
835 \(Previous history elements are earlier commands.) | |
836 With prefix argument N, search for Nth previous match. | |
837 If N is negative, find the next or Nth next match." | |
838 (interactive (eshell-regexp-arg "Previous input matching (regexp): ")) | |
839 (setq arg (eshell-search-arg arg)) | |
840 (let ((pos (eshell-previous-matching-input-string-position regexp arg))) | |
841 ;; Has a match been found? | |
842 (if (null pos) | |
843 (error "Not found") | |
844 (setq eshell-history-index pos) | |
37323
11b33211a908
(eshell-previous-matching-input): Don't display "History item" if the
John Wiegley <johnw@newartisans.com>
parents:
32526
diff
changeset
|
845 (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
|
846 (message "History item: %d" (- (ring-length eshell-history-ring) pos))) |
29876 | 847 ;; 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
|
848 (delete-region eshell-last-output-end (point)) |
29876 | 849 (insert-and-inherit (eshell-get-history pos))))) |
850 | |
851 (defun eshell-next-matching-input (regexp arg) | |
852 "Search forwards through input history for match for REGEXP. | |
853 \(Later history elements are more recent commands.) | |
854 With prefix argument N, search for Nth following match. | |
855 If N is negative, find the previous or Nth previous match." | |
856 (interactive (eshell-regexp-arg "Next input matching (regexp): ")) | |
857 (eshell-previous-matching-input regexp (- arg))) | |
858 | |
859 (defun eshell-previous-matching-input-from-input (arg) | |
860 "Search backwards through input history for match for current input. | |
861 \(Previous history elements are earlier commands.) | |
862 With prefix argument N, search for Nth previous match. | |
863 If N is negative, search forwards for the -Nth following match." | |
864 (interactive "p") | |
865 (if (not (memq last-command '(eshell-previous-matching-input-from-input | |
866 eshell-next-matching-input-from-input))) | |
867 ;; Starting a new search | |
868 (setq eshell-matching-input-from-input-string | |
869 (buffer-substring (save-excursion (eshell-bol) (point)) | |
870 (point)) | |
871 eshell-history-index nil)) | |
872 (eshell-previous-matching-input | |
873 (concat "^" (regexp-quote eshell-matching-input-from-input-string)) | |
874 arg)) | |
875 | |
876 (defun eshell-next-matching-input-from-input (arg) | |
877 "Search forwards through input history for match for current input. | |
878 \(Following history elements are more recent commands.) | |
879 With prefix argument N, search for Nth following match. | |
880 If N is negative, search backwards for the -Nth previous match." | |
881 (interactive "p") | |
882 (eshell-previous-matching-input-from-input (- arg))) | |
883 | |
884 (defun eshell-test-imatch () | |
885 "If isearch match good, put point at the beginning and return non-nil." | |
886 (if (get-text-property (point) 'history) | |
887 (progn (beginning-of-line) t) | |
888 (let ((before (point))) | |
889 (eshell-bol) | |
890 (if (and (not (bolp)) | |
891 (<= (point) before)) | |
892 t | |
893 (if isearch-forward | |
894 (progn | |
895 (end-of-line) | |
896 (forward-char)) | |
897 (beginning-of-line) | |
898 (backward-char)))))) | |
899 | |
900 (defun eshell-return-to-prompt () | |
901 "Once a search string matches, insert it at the end and go there." | |
902 (setq isearch-other-end nil) | |
903 (let ((found (eshell-test-imatch)) before) | |
904 (while (and (not found) | |
905 (setq before | |
906 (funcall (if isearch-forward | |
907 're-search-forward | |
908 're-search-backward) | |
909 isearch-string nil t))) | |
910 (setq found (eshell-test-imatch))) | |
911 (if (not found) | |
912 (progn | |
913 (goto-char eshell-last-output-end) | |
914 (delete-region (point) (point-max))) | |
915 (setq before (point)) | |
916 (let ((text (buffer-substring-no-properties | |
917 (point) (line-end-position))) | |
918 (orig (marker-position eshell-last-output-end))) | |
919 (goto-char eshell-last-output-end) | |
920 (delete-region (point) (point-max)) | |
921 (when (and text (> (length text) 0)) | |
922 (insert text) | |
923 (put-text-property (1- (point)) (point) | |
924 'last-search-pos before) | |
925 (set-marker eshell-last-output-end orig) | |
926 (goto-char eshell-last-output-end)))))) | |
927 | |
928 (defun eshell-prepare-for-search () | |
929 "Make sure the old history file is at the beginning of the buffer." | |
930 (unless (get-text-property (point-min) 'history) | |
931 (save-excursion | |
932 (goto-char (point-min)) | |
933 (let ((end (copy-marker (point) t))) | |
934 (insert-file-contents eshell-history-file-name) | |
935 (set-text-properties (point-min) end | |
936 '(history t invisible t)))))) | |
937 | |
938 (defun eshell-isearch-backward (&optional invert) | |
939 "Do incremental regexp search backward through past commands." | |
940 (interactive) | |
941 (let ((inhibit-read-only t) end) | |
942 (eshell-prepare-for-search) | |
943 (goto-char (point-max)) | |
944 (set-marker eshell-last-output-end (point)) | |
945 (delete-region (point) (point-max))) | |
946 (isearch-mode invert t 'eshell-return-to-prompt)) | |
947 | |
948 (defun eshell-isearch-repeat-backward (&optional invert) | |
949 "Do incremental regexp search backward through past commands." | |
950 (interactive) | |
951 (let ((old-pos (get-text-property (1- (point-max)) | |
952 'last-search-pos))) | |
953 (when old-pos | |
954 (goto-char old-pos) | |
955 (if invert | |
956 (end-of-line) | |
957 (backward-char))) | |
958 (setq isearch-forward invert) | |
959 (isearch-search-and-update))) | |
960 | |
961 (defun eshell-isearch-forward () | |
962 "Do incremental regexp search backward through past commands." | |
963 (interactive) | |
964 (eshell-isearch-backward t)) | |
965 | |
966 (defun eshell-isearch-repeat-forward () | |
967 "Do incremental regexp search backward through past commands." | |
968 (interactive) | |
969 (eshell-isearch-repeat-backward t)) | |
970 | |
971 (defun eshell-isearch-cancel () | |
972 (interactive) | |
973 (goto-char eshell-last-output-end) | |
974 (delete-region (point) (point-max)) | |
975 (call-interactively 'isearch-cancel)) | |
976 | |
977 (defun eshell-isearch-abort () | |
978 (interactive) | |
979 (goto-char eshell-last-output-end) | |
980 (delete-region (point) (point-max)) | |
981 (call-interactively 'isearch-abort)) | |
982 | |
983 (defun eshell-isearch-delete-char () | |
984 (interactive) | |
985 (save-excursion | |
986 (isearch-delete-char))) | |
987 | |
988 (defun eshell-isearch-return () | |
989 (interactive) | |
990 (isearch-done) | |
991 (eshell-send-input)) | |
992 | |
87068
27cfd1655fb2
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
78220
diff
changeset
|
993 (provide 'em-hist) |
27cfd1655fb2
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
78220
diff
changeset
|
994 |
95152
ad5d26b1d5d1
Use eshell-defgroup rather than defgroup.
Glenn Morris <rgm@gnu.org>
parents:
94661
diff
changeset
|
995 ;; Local Variables: |
ad5d26b1d5d1
Use eshell-defgroup rather than defgroup.
Glenn Morris <rgm@gnu.org>
parents:
94661
diff
changeset
|
996 ;; generated-autoload-file: "esh-groups.el" |
ad5d26b1d5d1
Use eshell-defgroup rather than defgroup.
Glenn Morris <rgm@gnu.org>
parents:
94661
diff
changeset
|
997 ;; End: |
ad5d26b1d5d1
Use eshell-defgroup rather than defgroup.
Glenn Morris <rgm@gnu.org>
parents:
94661
diff
changeset
|
998 |
93975
1e3a407766b9
Fix up comment convention on the arch-tag lines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
87649
diff
changeset
|
999 ;; arch-tag: 1a847333-f864-4b96-9acd-b549d620b6c6 |
29876 | 1000 ;;; em-hist.el ends here |