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