em-alias.el 10 KB

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