Update copyright year to 2014 by running admin/update-copyright.
[emacs.git] / lisp / eshell / em-alias.el
blobe9cde39013970088d7ad1b03fa1532eda0475464
1 ;;; em-alias.el --- creation and management of command aliases -*- lexical-binding:t -*-
3 ;; Copyright (C) 1999-2014 Free Software Foundation, Inc.
5 ;; Author: John Wiegley <johnw@gnu.org>
7 ;; This file is part of GNU Emacs.
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 3 of the License, or
12 ;; (at your option) any later version.
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.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22 ;;; Commentary:
24 ;; Command aliases greatly simplify the definition of new commands.
25 ;; They exist as an alternative to alias functions, which are
26 ;; otherwise quite superior, being more flexible and natural to the
27 ;; Emacs Lisp environment (if somewhat trickier to define; [Alias
28 ;; functions]).
30 ;;;_* Creating aliases
32 ;; The user interface is simple: type 'alias' followed by the command
33 ;; name followed by the definition. Argument references are made
34 ;; using '$1', '$2', etc., or '$*'. For example:
36 ;; alias ll 'ls -l $*'
38 ;; This will cause the command 'll NEWS' to be replaced by 'ls -l
39 ;; NEWS'. This is then passed back to the command parser for
40 ;; reparsing.{Only the command text specified in the alias definition
41 ;; will be reparsed. Argument references (such as '$*') are handled
42 ;; using variable values, which means that the expansion will not be
43 ;; reparsed, but used directly.}
45 ;; To delete an alias, specify its name without a definition:
47 ;; alias ll
49 ;; Aliases are written to disk immediately after being defined or
50 ;; deleted. The filename in which they are kept is defined by the
51 ;; variable eshell-aliases-file.
53 ;; The format of this file is quite basic. It specifies the alias
54 ;; definitions in almost exactly the same way that the user entered
55 ;; them, minus any argument quoting (since interpolation is not done
56 ;; when the file is read). Hence, it is possible to add new aliases
57 ;; to the alias file directly, using a text editor rather than the
58 ;; `alias' command. Or, this method can be used for editing aliases
59 ;; that have already defined.
61 ;; Here is an example of a few different aliases, and they would
62 ;; appear in the aliases file:
64 ;; alias clean rm -fr **/.#*~
65 ;; alias commit cvs commit -m changes $*
66 ;; alias ll ls -l $*
67 ;; alias info (info)
68 ;; alias reindex glimpseindex -o ~/Mail
69 ;; alias compact for i in ~/Mail/**/*~*.bz2(Lk+50) { bzip2 -9v $i }
71 ;;;_* Auto-correction of bad commands
73 ;; When a user enters the same unknown command many times during a
74 ;; session, it is likely that they are experiencing a spelling
75 ;; difficulty associated with a certain command. To combat this,
76 ;; Eshell will offer to automatically define an alias for that
77 ;; misspelled command, once a given tolerance threshold has been
78 ;; reached.
80 ;; Whenever the same bad command name is encountered
81 ;; `eshell-bad-command-tolerance' times, the user will be prompted in
82 ;; the minibuffer to provide an alias name. An alias definition will
83 ;; then be created which will result in an equal call to the correct
84 ;; name. In this way, Eshell gradually learns about the commands that
85 ;; the user mistypes frequently, and will automatically correct them!
87 ;; Note that a '$*' is automatically appended at the end of the alias
88 ;; definition, so that entering it is unnecessary when specifying the
89 ;; corrected command name.
91 ;;; Code:
93 (require 'eshell)
95 ;;;###autoload
96 (progn
97 (defgroup eshell-alias nil
98 "Command aliases allow for easy definition of alternate commands."
99 :tag "Command aliases"
100 ;; :link '(info-link "(eshell)Command aliases")
101 :group 'eshell-module))
103 (defcustom eshell-aliases-file (expand-file-name "alias" eshell-directory-name)
104 "The file in which aliases are kept.
105 Whenever an alias is defined by the user, using the `alias' command,
106 it will be written to this file. Thus, alias definitions (and
107 deletions) are always permanent. This approach was chosen for the
108 sake of simplicity, since that's pretty much the only benefit to be
109 gained by using this module."
110 :type 'file
111 :group 'eshell-alias)
113 (defcustom eshell-bad-command-tolerance 3
114 "The number of failed commands to ignore before creating an alias."
115 :type 'integer
116 ;; :link '(custom-manual "(eshell)Auto-correction of bad commands")
117 :group 'eshell-alias)
119 (defcustom eshell-alias-load-hook nil
120 "A hook that gets run when `eshell-alias' is loaded."
121 :version "24.1" ; removed eshell-alias-initialize
122 :type 'hook
123 :group 'eshell-alias)
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:
129 (NAME DEFINITION)
131 Where NAME is the textual name of the alias, and DEFINITION is the
132 command string to replace that command with.
134 Note: this list should not be modified in your init file.
135 Rather, any desired alias definitions should be declared using
136 the `alias' command, which will automatically write them to the
137 file named by `eshell-aliases-file'.")
139 (put 'eshell-command-aliases-list 'risky-local-variable t)
141 (defvar eshell-failed-commands-alist nil
142 "An alist of command name failures.")
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)
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))
153 (defun eshell-command-aliased-p (name)
154 (assoc name eshell-command-aliases-list))
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 (dolist (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)
178 (defvar pcomplete-stub)
179 (autoload 'pcomplete-here "pcomplete")
181 (defun pcomplete/eshell-mode/alias ()
182 "Completion function for Eshell's `alias' command."
183 (pcomplete-here (eshell-alias-completions pcomplete-stub)))
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))))))
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))))
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))
215 (defvar eshell-prevent-alias-expansion nil)
217 (defun eshell-maybe-replace-by-alias (command args)
218 "If COMMAND has an alias definition, call that instead using ARGS."
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 `(let ((eshell-command-name ',eshell-last-command-name)
225 (eshell-command-arguments ',eshell-last-arguments)
226 (eshell-prevent-alias-expansion
227 ',(cons command eshell-prevent-alias-expansion)))
228 ,(eshell-parse-command (nth 1 alias))))))))
230 (defun eshell-alias-completions (name)
231 "Find all possible completions for NAME.
232 These are all the command aliases which begin with NAME."
233 (let (completions)
234 (dolist (alias eshell-command-aliases-list)
235 (if (string-match (concat "^" name) (car alias))
236 (setq completions (cons (car alias) completions))))
237 completions))
239 (defun eshell-fix-bad-commands (name)
240 "If the user repeatedly a bad command NAME, make an alias for them."
241 (ignore
242 (unless (file-name-directory name)
243 (let ((entry (assoc name eshell-failed-commands-alist)))
244 (if (not entry)
245 (setq eshell-failed-commands-alist
246 (cons (cons name 1) eshell-failed-commands-alist))
247 (if (< (cdr entry) eshell-bad-command-tolerance)
248 (setcdr entry (1+ (cdr entry)))
249 (let ((alias (concat
250 (read-string
251 (format "Define alias for \"%s\": " name))
252 " $*")))
253 (eshell/alias name alias)
254 (throw 'eshell-replace-command
255 (list
256 'let
257 (list
258 (list 'eshell-command-name
259 (list 'quote name))
260 (list 'eshell-command-arguments
261 (list 'quote eshell-last-arguments))
262 (list 'eshell-prevent-alias-expansion
263 (list 'quote
264 (cons name
265 eshell-prevent-alias-expansion))))
266 (eshell-parse-command alias))))))))))
268 (provide 'em-alias)
270 ;; Local Variables:
271 ;; generated-autoload-file: "esh-groups.el"
272 ;; End:
274 ;;; em-alias.el ends here