Mercurial > emacs
annotate lisp/eshell/em-alias.el @ 94976:6c274ef55b8d
*** empty log message ***
author | Michael Albinus <michael.albinus@gmx.de> |
---|---|
date | Wed, 14 May 2008 19:55:11 +0000 |
parents | b5b0801a7637 |
children | ad5d26b1d5d1 |
rev | line source |
---|---|
38414
67b464da13ec
Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents:
37322
diff
changeset
|
1 ;;; em-alias.el --- creation and management of command aliases |
29876 | 2 |
74509 | 3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, |
79707 | 4 ;; 2005, 2006, 2007, 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 ;; Command aliases greatly simplify the definition of new commands. | |
26 ;; They exist as an alternative to alias functions, which are | |
27 ;; otherwise quite superior, being more flexible and natural to the | |
28 ;; Emacs Lisp environment (if somewhat trickier to define; [Alias | |
29 ;; functions]). | |
30 ;; | |
31 ;;;_* Creating aliases | |
32 ;; | |
33 ;; The user interface is simple: type 'alias' followed by the command | |
34 ;; name followed by the definition. Argument references are made | |
35 ;; using '$1', '$2', etc., or '$*'. For example: | |
36 ;; | |
37 ;; alias ll 'ls -l $*' | |
38 ;; | |
39 ;; This will cause the command 'll NEWS' to be replaced by 'ls -l | |
40 ;; NEWS'. This is then passed back to the command parser for | |
41 ;; reparsing.{Only the command text specified in the alias definition | |
42 ;; will be reparsed. Argument references (such as '$*') are handled | |
43 ;; using variable values, which means that the expansion will not be | |
44 ;; reparsed, but used directly.} | |
45 ;; | |
46 ;; To delete an alias, specify its name without a definition: | |
47 ;; | |
48 ;; alias ll | |
49 ;; | |
50 ;; Aliases are written to disk immediately after being defined or | |
51 ;; deleted. The filename in which they are kept is defined by the | |
87062
dc33075c168e
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
78220
diff
changeset
|
52 ;; variable eshell-aliases-file. |
29876 | 53 |
54 ;; The format of this file is quite basic. It specifies the alias | |
55 ;; definitions in almost exactly the same way that the user entered | |
56 ;; them, minus any argument quoting (since interpolation is not done | |
57 ;; when the file is read). Hence, it is possible to add new aliases | |
58 ;; to the alias file directly, using a text editor rather than the | |
59 ;; `alias' command. Or, this method can be used for editing aliases | |
60 ;; that have already defined. | |
61 ;; | |
62 ;; Here is an example of a few different aliases, and they would | |
63 ;; appear in the aliases file: | |
64 ;; | |
65 ;; alias clean rm -fr **/.#*~ | |
66 ;; alias commit cvs commit -m changes $* | |
67 ;; alias ll ls -l $* | |
68 ;; alias info (info) | |
69 ;; alias reindex glimpseindex -o ~/Mail | |
70 ;; alias compact for i in ~/Mail/**/*~*.bz2(Lk+50) { bzip2 -9v $i } | |
71 ;; | |
72 ;;;_* Auto-correction of bad commands | |
73 ;; | |
74 ;; When a user enters the same unknown command many times during a | |
75 ;; session, it is likely that they are experiencing a spelling | |
76 ;; difficulty associated with a certain command. To combat this, | |
77 ;; Eshell will offer to automatically define an alias for that | |
78 ;; mispelled command, once a given tolerance threshold has been | |
79 ;; reached. | |
80 | |
87062
dc33075c168e
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
78220
diff
changeset
|
81 ;; Whenever the same bad command name is encountered |
dc33075c168e
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
78220
diff
changeset
|
82 ;; `eshell-bad-command-tolerance' times, the user will be prompted in |
dc33075c168e
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
78220
diff
changeset
|
83 ;; the minibuffer to provide an alias name. An alias definition will |
dc33075c168e
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
78220
diff
changeset
|
84 ;; then be created which will result in an equal call to the correct |
dc33075c168e
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
78220
diff
changeset
|
85 ;; name. In this way, Eshell gradually learns about the commands that |
dc33075c168e
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
78220
diff
changeset
|
86 ;; the user mistypes frequently, and will automatically correct them! |
29876 | 87 ;; |
88 ;; Note that a '$*' is automatically appended at the end of the alias | |
89 ;; definition, so that entering it is unnecessary when specifying the | |
90 ;; corrected command name. | |
91 | |
92 ;;; Code: | |
93 | |
87062
dc33075c168e
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
78220
diff
changeset
|
94 (eval-when-compile |
dc33075c168e
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
78220
diff
changeset
|
95 (require 'esh-util)) |
dc33075c168e
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
78220
diff
changeset
|
96 (require 'eshell) |
dc33075c168e
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
78220
diff
changeset
|
97 |
dc33075c168e
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
78220
diff
changeset
|
98 (defgroup eshell-alias nil |
dc33075c168e
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
78220
diff
changeset
|
99 "Command aliases allow for easy definition of alternate commands." |
dc33075c168e
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
78220
diff
changeset
|
100 :tag "Command aliases" |
dc33075c168e
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
78220
diff
changeset
|
101 ;; :link '(info-link "(eshell)Command aliases") |
dc33075c168e
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
78220
diff
changeset
|
102 :group 'eshell-module) |
dc33075c168e
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
78220
diff
changeset
|
103 |
dc33075c168e
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
78220
diff
changeset
|
104 (defcustom eshell-aliases-file (concat eshell-directory-name "alias") |
dc33075c168e
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
78220
diff
changeset
|
105 "*The file in which aliases are kept. |
dc33075c168e
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
78220
diff
changeset
|
106 Whenever an alias is defined by the user, using the `alias' command, |
dc33075c168e
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
78220
diff
changeset
|
107 it will be written to this file. Thus, alias definitions (and |
dc33075c168e
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
78220
diff
changeset
|
108 deletions) are always permanent. This approach was chosen for the |
dc33075c168e
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
78220
diff
changeset
|
109 sake of simplicity, since that's pretty much the only benefit to be |
dc33075c168e
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
78220
diff
changeset
|
110 gained by using this module." |
dc33075c168e
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
78220
diff
changeset
|
111 :type 'file |
dc33075c168e
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
78220
diff
changeset
|
112 :group 'eshell-alias) |
dc33075c168e
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
78220
diff
changeset
|
113 |
dc33075c168e
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
78220
diff
changeset
|
114 (defcustom eshell-bad-command-tolerance 3 |
dc33075c168e
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
78220
diff
changeset
|
115 "*The number of failed commands to ignore before creating an alias." |
dc33075c168e
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
78220
diff
changeset
|
116 :type 'integer |
dc33075c168e
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
78220
diff
changeset
|
117 ;; :link '(custom-manual "(eshell)Auto-correction of bad commands") |
dc33075c168e
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
78220
diff
changeset
|
118 :group 'eshell-alias) |
dc33075c168e
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
78220
diff
changeset
|
119 |
29876 | 120 (defcustom eshell-alias-load-hook '(eshell-alias-initialize) |
121 "*A hook that gets run when `eshell-alias' is loaded." | |
122 :type 'hook | |
123 :group 'eshell-alias) | |
124 | |
125 (defvar eshell-command-aliases-list nil | |
126 "A list of command aliases currently defined by the user. | |
127 Each element of this alias is a list of the form: | |
128 | |
129 (NAME DEFINITION) | |
130 | |
131 Where NAME is the textual name of the alias, and DEFINITION is the | |
132 command string to replace that command with. | |
133 | |
134 Note: this list should not be modified in your '.emacs' file. Rather, | |
135 any desired alias definitions should be declared using the `alias' | |
136 command, which will automatically write them to the file named by | |
137 `eshell-aliases-file'.") | |
138 | |
139 (put 'eshell-command-aliases-list 'risky-local-variable t) | |
140 | |
141 (defvar eshell-failed-commands-alist nil | |
142 "An alist of command name failures.") | |
143 | |
144 (defun eshell-alias-initialize () | |
145 "Initialize the alias handling code." | |
146 (make-local-variable 'eshell-failed-commands-alist) | |
147 (add-hook 'eshell-alternate-command-hook 'eshell-fix-bad-commands t t) | |
148 (eshell-read-aliases-list) | |
33020 | 149 (add-hook 'eshell-named-command-hook 'eshell-maybe-replace-by-alias t t) |
150 (make-local-variable 'eshell-complex-commands) | |
151 (add-to-list 'eshell-complex-commands 'eshell-command-aliased-p)) | |
152 | |
153 (defun eshell-command-aliased-p (name) | |
37322
d27be94466e0
(eshell-command-aliased-p): `assoc' was required where `member' was
John Wiegley <johnw@newartisans.com>
parents:
33020
diff
changeset
|
154 (assoc name eshell-command-aliases-list)) |
29876 | 155 |
156 (defun eshell/alias (&optional alias &rest definition) | |
157 "Define an ALIAS in the user's alias list using DEFINITION." | |
158 (if (not alias) | |
159 (eshell-for alias eshell-command-aliases-list | |
160 (eshell-print (apply 'format "alias %s %s\n" alias))) | |
161 (if (not definition) | |
162 (setq eshell-command-aliases-list | |
163 (delq (assoc alias eshell-command-aliases-list) | |
164 eshell-command-aliases-list)) | |
165 (and (stringp definition) | |
166 (set-text-properties 0 (length definition) nil definition)) | |
167 (let ((def (assoc alias eshell-command-aliases-list)) | |
168 (alias-def (list alias | |
169 (eshell-flatten-and-stringify definition)))) | |
170 (if def | |
171 (setq eshell-command-aliases-list | |
172 (delq def eshell-command-aliases-list))) | |
173 (setq eshell-command-aliases-list | |
174 (cons alias-def eshell-command-aliases-list)))) | |
175 (eshell-write-aliases-list)) | |
176 nil) | |
177 | |
87062
dc33075c168e
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
78220
diff
changeset
|
178 (defvar pcomplete-stub) |
dc33075c168e
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
78220
diff
changeset
|
179 (autoload 'pcomplete-here "pcomplete") |
dc33075c168e
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
78220
diff
changeset
|
180 |
29876 | 181 (defun pcomplete/eshell-mode/alias () |
182 "Completion function for Eshell's `alias' command." | |
183 (pcomplete-here (eshell-alias-completions pcomplete-stub))) | |
184 | |
185 (defun eshell-read-aliases-list () | |
186 "Read in an aliases list from `eshell-aliases-file'." | |
187 (let ((file eshell-aliases-file)) | |
188 (when (file-readable-p file) | |
189 (setq eshell-command-aliases-list | |
190 (with-temp-buffer | |
191 (let (eshell-command-aliases-list) | |
192 (insert-file-contents file) | |
193 (while (not (eobp)) | |
194 (if (re-search-forward | |
195 "^alias\\s-+\\(\\S-+\\)\\s-+\\(.+\\)") | |
196 (setq eshell-command-aliases-list | |
197 (cons (list (match-string 1) | |
198 (match-string 2)) | |
199 eshell-command-aliases-list))) | |
200 (forward-line 1)) | |
201 eshell-command-aliases-list)))))) | |
202 | |
203 (defun eshell-write-aliases-list () | |
204 "Write out the current aliases into `eshell-aliases-file'." | |
205 (if (file-writable-p (file-name-directory eshell-aliases-file)) | |
206 (let ((eshell-current-handles | |
207 (eshell-create-handles eshell-aliases-file 'overwrite))) | |
208 (eshell/alias) | |
209 (eshell-close-handles 0)))) | |
210 | |
211 (defsubst eshell-lookup-alias (name) | |
212 "Check whether NAME is aliased. Return the alias if there is one." | |
213 (assoc name eshell-command-aliases-list)) | |
214 | |
215 (defvar eshell-prevent-alias-expansion nil) | |
216 | |
217 (defun eshell-maybe-replace-by-alias (command args) | |
30270
1ba9e2802a23
(eshell-maybe-replace-by-alias): Doc fix.
Eli Zaretskii <eliz@gnu.org>
parents:
29934
diff
changeset
|
218 "If COMMAND has an alias definition, call that instead using ARGS." |
29876 | 219 (unless (and eshell-prevent-alias-expansion |
220 (member command eshell-prevent-alias-expansion)) | |
221 (let ((alias (eshell-lookup-alias command))) | |
222 (if alias | |
223 (throw 'eshell-replace-command | |
224 (list | |
225 'let | |
226 (list | |
227 (list 'eshell-command-name | |
228 (list 'quote eshell-last-command-name)) | |
229 (list 'eshell-command-arguments | |
230 (list 'quote eshell-last-arguments)) | |
231 (list 'eshell-prevent-alias-expansion | |
232 (list 'quote | |
233 (cons command | |
234 eshell-prevent-alias-expansion)))) | |
235 (eshell-parse-command (nth 1 alias)))))))) | |
236 | |
237 (defun eshell-alias-completions (name) | |
238 "Find all possible completions for NAME. | |
239 These are all the command aliases which begin with NAME." | |
240 (let (completions) | |
241 (eshell-for alias eshell-command-aliases-list | |
242 (if (string-match (concat "^" name) (car alias)) | |
243 (setq completions (cons (car alias) completions)))) | |
244 completions)) | |
245 | |
246 (defun eshell-fix-bad-commands (name) | |
247 "If the user repeatedly a bad command NAME, make an alias for them." | |
248 (ignore | |
249 (unless (file-name-directory name) | |
250 (let ((entry (assoc name eshell-failed-commands-alist))) | |
251 (if (not entry) | |
252 (setq eshell-failed-commands-alist | |
253 (cons (cons name 1) eshell-failed-commands-alist)) | |
254 (if (< (cdr entry) eshell-bad-command-tolerance) | |
255 (setcdr entry (1+ (cdr entry))) | |
256 (let ((alias (concat | |
257 (read-string | |
258 (format "Define alias for \"%s\": " name)) | |
259 " $*"))) | |
260 (eshell/alias name alias) | |
261 (throw 'eshell-replace-command | |
262 (list | |
263 'let | |
264 (list | |
265 (list 'eshell-command-name | |
266 (list 'quote name)) | |
267 (list 'eshell-command-arguments | |
268 (list 'quote eshell-last-arguments)) | |
269 (list 'eshell-prevent-alias-expansion | |
270 (list 'quote | |
271 (cons name | |
272 eshell-prevent-alias-expansion)))) | |
273 (eshell-parse-command alias)))))))))) | |
274 | |
87062
dc33075c168e
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
78220
diff
changeset
|
275 (provide 'em-alias) |
dc33075c168e
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
78220
diff
changeset
|
276 |
93975
1e3a407766b9
Fix up comment convention on the arch-tag lines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
87649
diff
changeset
|
277 ;; arch-tag: 8b018fc1-4e07-4ccc-aa73-c0a1ba361f82 |
29876 | 278 ;;; em-alias.el ends here |