mirror of
https://git.savannah.gnu.org/git/emacs.git
synced 2024-12-11 09:20:51 +00:00
ecf08f0621
dc4e6b1329
; Update copyright years in more files64b3777631
; Run set-copyright from admin.el8e1c56ae46
; Add 2024 to copyright years # Conflicts: # doc/misc/modus-themes.org # doc/misc/texinfo.tex # etc/NEWS # etc/refcards/ru-refcard.tex # etc/themes/modus-operandi-theme.el # etc/themes/modus-themes.el # etc/themes/modus-vivendi-theme.el # lib/alloca.in.h # lib/binary-io.h # lib/c-ctype.h # lib/c-strcasecmp.c # lib/c-strncasecmp.c # lib/careadlinkat.c # lib/cloexec.c # lib/close-stream.c # lib/diffseq.h # lib/dup2.c # lib/filemode.h # lib/fpending.c # lib/fpending.h # lib/fsusage.c # lib/getgroups.c # lib/getloadavg.c # lib/gettext.h # lib/gettime.c # lib/gettimeofday.c # lib/group-member.c # lib/malloc.c # lib/md5-stream.c # lib/md5.c # lib/md5.h # lib/memmem.c # lib/memrchr.c # lib/nanosleep.c # lib/save-cwd.h # lib/sha1.c # lib/sig2str.c # lib/stdlib.in.h # lib/strtoimax.c # lib/strtol.c # lib/strtoll.c # lib/time_r.c # lib/xalloc-oversized.h # lisp/auth-source-pass.el # lisp/emacs-lisp/lisp-mnt.el # lisp/emacs-lisp/timer.el # lisp/info-look.el # lisp/jit-lock.el # lisp/loadhist.el # lisp/mail/rmail.el # lisp/net/ntlm.el # lisp/net/webjump.el # lisp/progmodes/asm-mode.el # lisp/progmodes/project.el # lisp/progmodes/sh-script.el # lisp/textmodes/flyspell.el # lisp/textmodes/reftex-toc.el # lisp/textmodes/reftex.el # lisp/textmodes/tex-mode.el # lisp/url/url-gw.el # m4/alloca.m4 # m4/clock_time.m4 # m4/d-type.m4 # m4/dirent_h.m4 # m4/dup2.m4 # m4/euidaccess.m4 # m4/fchmodat.m4 # m4/filemode.m4 # m4/fsusage.m4 # m4/getgroups.m4 # m4/getloadavg.m4 # m4/getrandom.m4 # m4/gettime.m4 # m4/gettimeofday.m4 # m4/gnulib-common.m4 # m4/group-member.m4 # m4/inttypes.m4 # m4/malloc.m4 # m4/manywarnings.m4 # m4/mempcpy.m4 # m4/memrchr.m4 # m4/mkostemp.m4 # m4/mktime.m4 # m4/nproc.m4 # m4/nstrftime.m4 # m4/pathmax.m4 # m4/pipe2.m4 # m4/pselect.m4 # m4/pthread_sigmask.m4 # m4/readlink.m4 # m4/realloc.m4 # m4/sig2str.m4 # m4/ssize_t.m4 # m4/stat-time.m4 # m4/stddef_h.m4 # m4/stdint.m4 # m4/stdio_h.m4 # m4/stdlib_h.m4 # m4/stpcpy.m4 # m4/strnlen.m4 # m4/strtoimax.m4 # m4/strtoll.m4 # m4/time_h.m4 # m4/timegm.m4 # m4/timer_time.m4 # m4/timespec.m4 # m4/unistd_h.m4 # m4/warnings.m4 # nt/configure.bat # nt/preprep.c # test/lisp/register-tests.el
277 lines
10 KiB
EmacsLisp
277 lines
10 KiB
EmacsLisp
;;; em-alias.el --- creation and management of command aliases -*- lexical-binding:t -*-
|
|
|
|
;; Copyright (C) 1999-2024 Free Software Foundation, Inc.
|
|
|
|
;; Author: John Wiegley <johnw@gnu.org>
|
|
|
|
;; This file is part of GNU Emacs.
|
|
|
|
;; GNU Emacs is free software: you can redistribute it and/or modify
|
|
;; it under the terms of the GNU General Public License as published by
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
;; (at your option) any later version.
|
|
|
|
;; GNU Emacs is distributed in the hope that it will be useful,
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
;; GNU General Public License for more details.
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
|
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
;;; Commentary:
|
|
|
|
;; Command aliases greatly simplify the definition of new commands.
|
|
;; They exist as an alternative to alias functions, which are
|
|
;; otherwise quite superior, being more flexible and natural to the
|
|
;; Emacs Lisp environment (if somewhat trickier to define; [Alias
|
|
;; functions]).
|
|
;;
|
|
;;;_* Creating aliases
|
|
;;
|
|
;; The user interface is simple: type 'alias' followed by the command
|
|
;; name followed by the definition. Argument references are made
|
|
;; using '$1', '$2', etc., or '$*'. For example:
|
|
;;
|
|
;; alias ll 'ls -l $*'
|
|
;;
|
|
;; This will cause the command 'll NEWS' to be replaced by 'ls -l
|
|
;; NEWS'. This is then passed back to the command parser for
|
|
;; reparsing.{Only the command text specified in the alias definition
|
|
;; will be reparsed. Argument references (such as '$*') are handled
|
|
;; using variable values, which means that the expansion will not be
|
|
;; reparsed, but used directly.}
|
|
;;
|
|
;; To delete an alias, specify its name without a definition:
|
|
;;
|
|
;; alias ll
|
|
;;
|
|
;; Aliases are written to disk immediately after being defined or
|
|
;; deleted. The filename in which they are kept is defined by the
|
|
;; variable eshell-aliases-file.
|
|
|
|
;; The format of this file is quite basic. It specifies the alias
|
|
;; definitions in almost exactly the same way that the user entered
|
|
;; them, minus any argument quoting (since interpolation is not done
|
|
;; when the file is read). Hence, it is possible to add new aliases
|
|
;; to the alias file directly, using a text editor rather than the
|
|
;; `alias' command. Or, this method can be used for editing aliases
|
|
;; that have already defined.
|
|
;;
|
|
;; Here is an example of a few different aliases, and they would
|
|
;; appear in the aliases file:
|
|
;;
|
|
;; alias clean rm -fr **/.#*~
|
|
;; alias commit cvs commit -m changes $*
|
|
;; alias ll ls -l $*
|
|
;; alias info (info)
|
|
;; alias reindex glimpseindex -o ~/Mail
|
|
;; alias compact for i in ~/Mail/**/*~*.bz2(Lk+50) { bzip2 -9v $i }
|
|
;;
|
|
;;;_* Auto-correction of bad commands
|
|
;;
|
|
;; When a user enters the same unknown command many times during a
|
|
;; session, it is likely that they are experiencing a spelling
|
|
;; difficulty associated with a certain command. To combat this,
|
|
;; Eshell will offer to automatically define an alias for that
|
|
;; misspelled command, once a given tolerance threshold has been
|
|
;; reached.
|
|
|
|
;; Whenever the same bad command name is encountered
|
|
;; `eshell-bad-command-tolerance' times, the user will be prompted in
|
|
;; the minibuffer to provide an alias name. An alias definition will
|
|
;; then be created which will result in an equal call to the correct
|
|
;; name. In this way, Eshell gradually learns about the commands that
|
|
;; the user mistypes frequently, and will automatically correct them!
|
|
;;
|
|
;; Note that a '$*' is automatically appended at the end of the alias
|
|
;; definition, so that entering it is unnecessary when specifying the
|
|
;; corrected command name.
|
|
|
|
;;; Code:
|
|
|
|
(require 'esh-mode)
|
|
|
|
;;;###autoload
|
|
(progn
|
|
(defgroup eshell-alias nil
|
|
"Command aliases allow for easy definition of alternate commands."
|
|
:tag "Command aliases"
|
|
;; :link '(info-link "(eshell)Command aliases")
|
|
:group 'eshell-module))
|
|
|
|
(defcustom eshell-aliases-file (expand-file-name "alias" eshell-directory-name)
|
|
"The file in which aliases are kept.
|
|
Whenever an alias is defined by the user, using the `alias' command,
|
|
it will be written to this file. Thus, alias definitions (and
|
|
deletions) are always permanent. This approach was chosen for the
|
|
sake of simplicity, since that's pretty much the only benefit to be
|
|
gained by using this module."
|
|
:type 'file
|
|
:group 'eshell-alias)
|
|
|
|
(defcustom eshell-bad-command-tolerance 3
|
|
"The number of failed commands to ignore before creating an alias."
|
|
:type 'integer
|
|
;; :link '(custom-manual "(eshell)Auto-correction of bad commands")
|
|
:group 'eshell-alias)
|
|
|
|
(defcustom eshell-alias-load-hook nil
|
|
"A hook that gets run when `eshell-alias' is loaded."
|
|
:version "24.1" ; removed eshell-alias-initialize
|
|
:type 'hook
|
|
:group 'eshell-alias)
|
|
|
|
(defvar eshell-command-aliases-list nil
|
|
"A list of command aliases currently defined by the user.
|
|
Each element of this alias is a list of the form:
|
|
|
|
(NAME DEFINITION)
|
|
|
|
Where NAME is the textual name of the alias, and DEFINITION is the
|
|
command string to replace that command with.
|
|
|
|
Note: this list should not be modified in your init file.
|
|
Rather, any desired alias definitions should be declared using
|
|
the `alias' command, which will automatically write them to the
|
|
file named by `eshell-aliases-file'.")
|
|
|
|
(put 'eshell-command-aliases-list 'risky-local-variable t)
|
|
|
|
(defvar eshell-failed-commands-alist nil
|
|
"An alist of command name failures.")
|
|
|
|
(defun eshell-alias-initialize () ;Called from `eshell-mode' via intern-soft!
|
|
"Initialize the alias handling code."
|
|
(make-local-variable 'eshell-failed-commands-alist)
|
|
(add-hook 'eshell-alternate-command-hook #'eshell-fix-bad-commands t t)
|
|
(eshell-read-aliases-list)
|
|
(add-hook 'eshell-named-command-hook #'eshell-maybe-replace-by-alias t t)
|
|
(make-local-variable 'eshell-complex-commands)
|
|
(add-to-list 'eshell-complex-commands 'eshell-command-aliased-p))
|
|
|
|
(defun eshell-command-aliased-p (name)
|
|
(assoc name eshell-command-aliases-list))
|
|
|
|
(defun eshell/alias (&optional alias &rest definition)
|
|
"Define an ALIAS in the user's alias list using DEFINITION."
|
|
(if (not alias)
|
|
(dolist (alias eshell-command-aliases-list)
|
|
(eshell-print (apply 'format "alias %s %s\n" alias)))
|
|
(if (not definition)
|
|
(setq eshell-command-aliases-list
|
|
(delq (assoc alias eshell-command-aliases-list)
|
|
eshell-command-aliases-list))
|
|
(and (stringp definition)
|
|
(set-text-properties 0 (length definition) nil definition))
|
|
(let ((def (assoc alias eshell-command-aliases-list))
|
|
(alias-def (list alias
|
|
(eshell-flatten-and-stringify definition))))
|
|
(if def
|
|
(setq eshell-command-aliases-list
|
|
(delq def eshell-command-aliases-list)))
|
|
(setq eshell-command-aliases-list
|
|
(cons alias-def eshell-command-aliases-list))))
|
|
(eshell-write-aliases-list))
|
|
nil)
|
|
|
|
(defvar pcomplete-stub)
|
|
(autoload 'pcomplete-here "pcomplete")
|
|
|
|
(defun pcomplete/eshell-mode/alias ()
|
|
"Completion function for Eshell's `alias' command."
|
|
(pcomplete-here (eshell-alias-completions pcomplete-stub)))
|
|
|
|
(defun eshell-read-aliases-list ()
|
|
"Read in an aliases list from `eshell-aliases-file'.
|
|
This is useful after manually editing the contents of the file."
|
|
(interactive)
|
|
(let ((file eshell-aliases-file))
|
|
(when (file-readable-p file)
|
|
(setq eshell-command-aliases-list
|
|
(with-temp-buffer
|
|
(let (eshell-command-aliases-list)
|
|
(insert-file-contents file)
|
|
(while (not (eobp))
|
|
(if (re-search-forward
|
|
"^alias\\s-+\\(\\S-+\\)\\s-+\\(.+\\)")
|
|
(setq eshell-command-aliases-list
|
|
(cons (list (match-string 1)
|
|
(match-string 2))
|
|
eshell-command-aliases-list)))
|
|
(forward-line 1))
|
|
eshell-command-aliases-list))))))
|
|
|
|
(defun eshell-write-aliases-list ()
|
|
"Write out the current aliases into `eshell-aliases-file'."
|
|
(if (file-writable-p (file-name-directory eshell-aliases-file))
|
|
(let ((eshell-current-handles
|
|
(eshell-create-handles eshell-aliases-file 'overwrite)))
|
|
(eshell/alias)
|
|
(eshell-close-handles 0 'nil))))
|
|
|
|
(defsubst eshell-lookup-alias (name)
|
|
"Check whether NAME is aliased. Return the alias if there is one."
|
|
(assoc name eshell-command-aliases-list))
|
|
|
|
(defvar eshell-prevent-alias-expansion nil)
|
|
|
|
(defun eshell-maybe-replace-by-alias (command _args)
|
|
"Call COMMAND's alias definition, if it exists."
|
|
(unless (and eshell-prevent-alias-expansion
|
|
(member command eshell-prevent-alias-expansion))
|
|
(let ((alias (eshell-lookup-alias command)))
|
|
(if alias
|
|
(throw 'eshell-replace-command
|
|
`(let ((eshell-command-name ',eshell-last-command-name)
|
|
(eshell-command-arguments ',eshell-last-arguments)
|
|
(eshell-prevent-alias-expansion
|
|
',(cons command eshell-prevent-alias-expansion)))
|
|
,(eshell-parse-command (nth 1 alias))))))))
|
|
|
|
(defun eshell-alias-completions (name)
|
|
"Find all possible completions for NAME.
|
|
These are all the command aliases which begin with NAME."
|
|
(let (completions)
|
|
(dolist (alias eshell-command-aliases-list)
|
|
(if (string-match (concat "^" name) (car alias))
|
|
(setq completions (cons (car alias) completions))))
|
|
completions))
|
|
|
|
(defun eshell-fix-bad-commands (name)
|
|
"If the user repeatedly a bad command NAME, make an alias for them."
|
|
(ignore
|
|
(unless (file-name-directory name)
|
|
(let ((entry (assoc name eshell-failed-commands-alist)))
|
|
(if (not entry)
|
|
(setq eshell-failed-commands-alist
|
|
(cons (cons name 1) eshell-failed-commands-alist))
|
|
(if (< (cdr entry) eshell-bad-command-tolerance)
|
|
(setcdr entry (1+ (cdr entry)))
|
|
(let ((alias (concat
|
|
(read-string
|
|
(format "Define alias for \"%s\": " name))
|
|
" $*")))
|
|
(eshell/alias name alias)
|
|
(throw 'eshell-replace-command
|
|
(list
|
|
'let
|
|
(list
|
|
(list 'eshell-command-name
|
|
(list 'quote name))
|
|
(list 'eshell-command-arguments
|
|
(list 'quote eshell-last-arguments))
|
|
(list 'eshell-prevent-alias-expansion
|
|
(list 'quote
|
|
(cons name
|
|
eshell-prevent-alias-expansion))))
|
|
(eshell-parse-command alias))))))))))
|
|
|
|
(provide 'em-alias)
|
|
|
|
;; Local Variables:
|
|
;; generated-autoload-file: "esh-groups.el"
|
|
;; End:
|
|
|
|
;;; em-alias.el ends here
|