Mercurial > emacs
annotate lisp/eshell/em-alias.el @ 49743:99784a2bc919
(sh-mode-map): Don't remap
beginning-of-defun to the non-existing command
sh-beginning-of-compound-command.
author | Andreas Schwab <schwab@suse.de> |
---|---|
date | Wed, 12 Feb 2003 10:21:06 +0000 |
parents | 78acbd1ebf65 |
children | 695cf19ef79e d7ddb3e565de |
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 (add-hook 'eshell-alternate-command-hook 'eshell-fix-bad-commands t t) | |
151 (eshell-read-aliases-list) | |
33020 | 152 (add-hook 'eshell-named-command-hook 'eshell-maybe-replace-by-alias t t) |
153 (make-local-variable 'eshell-complex-commands) | |
154 (add-to-list 'eshell-complex-commands 'eshell-command-aliased-p)) | |
155 | |
156 (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
|
157 (assoc name eshell-command-aliases-list)) |
29876 | 158 |
159 (defun eshell/alias (&optional alias &rest definition) | |
160 "Define an ALIAS in the user's alias list using DEFINITION." | |
161 (if (not alias) | |
162 (eshell-for alias eshell-command-aliases-list | |
163 (eshell-print (apply 'format "alias %s %s\n" alias))) | |
164 (if (not definition) | |
165 (setq eshell-command-aliases-list | |
166 (delq (assoc alias eshell-command-aliases-list) | |
167 eshell-command-aliases-list)) | |
168 (and (stringp definition) | |
169 (set-text-properties 0 (length definition) nil definition)) | |
170 (let ((def (assoc alias eshell-command-aliases-list)) | |
171 (alias-def (list alias | |
172 (eshell-flatten-and-stringify definition)))) | |
173 (if def | |
174 (setq eshell-command-aliases-list | |
175 (delq def eshell-command-aliases-list))) | |
176 (setq eshell-command-aliases-list | |
177 (cons alias-def eshell-command-aliases-list)))) | |
178 (eshell-write-aliases-list)) | |
179 nil) | |
180 | |
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 | |
275 ;;; em-alias.el ends here |