Commit affbf647 authored by Gerd Moellmann's avatar Gerd Moellmann
Browse files

*** empty log message ***

parent 022499fa
2000-06-23 Gerd Moellmann <gerd@gnu.org>
* Makefile.in (DONTCOMPILE): Add eshell/esh-maint.el.
* eshell/esh-cmd.el (eshell-rewrite-for-command): Use cdr and
cddr instead of cdddr.
* eshell/esh-util.el (eshell-sublist): Use eshell-copy-list
instead of copy-list.
......
;;; em-alias --- creation and management of command aliases
;; Copyright (C) 1999, 2000 Free Sofware Foundation
;; 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 2, 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; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
(provide 'em-alias)
(eval-when-compile (require 'esh-maint))
(defgroup eshell-alias nil
"Command aliases allow for easy definition of alternate commands."
:tag "Command aliases"
:link '(info-link "(eshell.info)Command aliases")
:group 'eshell-module)
;;; 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
;; following variable:
(defcustom eshell-aliases-file (concat eshell-directory-name "alias")
"*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)
;;;
;; 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
;; mispelled command, once a given tolerance threshold has been
;; reached.
(defcustom eshell-bad-command-tolerance 3
"*The number of failed commands to ignore before creating an alias."
:type 'integer
:link '(custom-manual "(eshell.info)Auto-correction of bad commands")
:group 'eshell-alias)
;;;
;; Whenever the same bad command name is encountered this many 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:
(defcustom eshell-alias-load-hook '(eshell-alias-initialize)
"*A hook that gets run when `eshell-alias' is loaded."
: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 '.emacs' 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 ()
"Initialize the alias handling code."
(make-local-variable 'eshell-failed-commands-alist)
(make-local-hook 'eshell-alternate-command-hook)
(add-hook 'eshell-alternate-command-hook 'eshell-fix-bad-commands t t)
(eshell-read-aliases-list)
(make-local-hook 'eshell-named-command-hook)
(add-hook 'eshell-named-command-hook 'eshell-maybe-replace-by-alias t t))
(defun eshell/alias (&optional alias &rest definition)
"Define an ALIAS in the user's alias list using DEFINITION."
(if (not alias)
(eshell-for 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)
(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'."
(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))))
(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)
"If COMMAND has an alias definition, call that instead using RAGS."
(unless (and eshell-prevent-alias-expansion
(member command eshell-prevent-alias-expansion))
(let ((alias (eshell-lookup-alias command)))
(if alias
(throw 'eshell-replace-command
(list
'let
(list
(list 'eshell-command-name
(list 'quote eshell-last-command-name))
(list 'eshell-command-arguments
(list 'quote eshell-last-arguments))
(list 'eshell-prevent-alias-expansion
(list 'quote
(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)
(eshell-for 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))))))))))
;;; em-alias.el ends here
;;; em-banner --- sample module that displays a login banner
;; Copyright (C) 1999, 2000 Free Sofware Foundation
;; 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 2, 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; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
(provide 'em-banner)
(eval-when-compile (require 'esh-maint))
(defgroup eshell-banner nil
"This sample module displays a welcome banner at login.
It exists so that others wishing to create their own Eshell extension
modules may have a simple template to begin with."
:tag "Login banner"
:link '(info-link "(eshell.info)Login banner")
:group 'eshell-module)
;;; Commentary:
;; There is nothing to be done or configured in order to use this
;; module, other than to select it by customizing the variable
;; `eshell-modules-list'. It will then display a version information
;; message whenever Eshell is loaded.
;;
;; This code is only an example of a how to write a well-formed
;; extension module for Eshell. The better way to display login text
;; is to use the `eshell-script' module, and to echo the desired
;; strings from the user's `eshell-login-script' file.
;;
;; There is one configuration variable, which demonstrates how to
;; properly define a customization variable in an extension module.
;; In this case, it allows the user to change the string which
;; displays at login time.
;;; User Variables:
(defcustom eshell-banner-message "Welcome to the Emacs shell\n\n"
"*The banner message to be displayed when Eshell is loaded.
This can be any sexp, and should end with at least two newlines."
:type 'sexp
:group 'eshell-banner)
(put 'eshell-banner-message 'risky-local-variable t)
;;; Code:
(require 'esh-util)
(defcustom eshell-banner-load-hook '(eshell-banner-initialize)
"*A list of functions to run when `eshell-banner' is loaded."
:type 'hook
:group 'eshell-banner)
(defun eshell-banner-initialize ()
"Output a welcome banner on initialization."
;; it's important to use `eshell-interactive-print' rather than
;; `insert', because `insert' doesn't know how to interact with the
;; I/O code used by Eshell
(unless eshell-non-interactive-p
(assert eshell-mode)
(assert eshell-banner-message)
(let ((msg (eval eshell-banner-message)))
(assert msg)
(eshell-interactive-print msg))))
(eshell-deftest banner banner-displayed
"Startup banner is displayed at point-min"
(assert eshell-banner-message)
(let ((msg (eval eshell-banner-message)))
(assert msg)
(goto-char (point-min))
(looking-at msg)))
;;; em-banner.el ends here
;;; em-basic --- basic shell builtin commands
;; Copyright (C) 1999, 2000 Free Sofware Foundation
;; 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 2, 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; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
(provide 'em-basic)
(eval-when-compile (require 'esh-maint))
(defgroup eshell-basic nil
"The \"basic\" code provides a set of convenience functions which
are traditionally considered shell builtins. Since all of the
functionality provided by them is accessible through Lisp, they are
not really builtins at all, but offer a command-oriented way to do the
same thing."
:tag "Basic shell commands"
:group 'eshell-module)
;;; Commentary:
;; There are very few basic Eshell commands -- so-called builtins.
;; They are: echo, umask, and version.
;;
;;;_* `echo'
;;
;; The `echo' command repeats its arguments to the screen. It is
;; optional whether this is done in a Lisp-friendly fashion (so that
;; the value of echo is useful to a Lisp command using the result of
;; echo as an argument), or whether it should try to act like a normal
;; shell echo, and always result in a flat string being returned.
(defcustom eshell-plain-echo-behavior nil
"*If non-nil, `echo' tries to behave like an ordinary shell echo.
This comes at some detriment to Lisp functionality. However, the Lisp
equivalent of `echo' can always be achieved by using `identity'."
:type 'boolean
:group 'eshell-basic)
;;;
;; An example of the difference is the following:
;;
;; echo Hello world
;;
;; If `eshell-plain-echo-behavior' is non-nil, this will yield the
;; string "Hello world". If Lisp behavior is enabled, however, it
;; will yield a list whose two elements are the strings "Hello" and
;; "world". The way to write an equivalent expression for both would
;; be:
;;
;; echo "Hello world"
;;
;; This always returns a single string.
;;
;;;_* `umask'
;;
;; The umask command changes the default file permissions for newly
;; created files. It uses the same syntax as bash.
;;
;;;_* `version'
;;
;; This command reports the version number for Eshell and all its
;; dependent module, including the date when those modules were last
;; modified.
;;; Code:
(require 'esh-opt)
;;; Functions:
(defun eshell-echo (args &optional output-newline)
"Implementation code for a Lisp version of `echo'.
It returns a formatted value that should be passed to `eshell-print'
or `eshell-printn' for display."
(if eshell-plain-echo-behavior
(concat (apply 'eshell-flatten-and-stringify args) "\n")
(let ((value
(cond
((= (length args) 0) "")
((= (length args) 1)
(car args))
(t
(mapcar
(function
(lambda (arg)
(if (stringp arg)
(set-text-properties 0 (length arg) nil arg))
arg))
args)))))
(if output-newline
(cond
((stringp value)
(concat value "\n"))
((listp value)
(append value (list "\n")))
(t
(concat (eshell-stringify value) "\n")))
value))))
(defun eshell/echo (&rest args)
"Implementation of `echo'. See `eshell-plain-echo-behavior'."
(eshell-eval-using-options
"echo" args
'((?n nil nil output-newline "terminate with a newline")
(?h "help" nil nil "output this help screen")
:preserve-args
:usage "[-n] [object]")
(eshell-echo args output-newline)))
(defun eshell/printnl (&rest args)
"Print out each of the argument, separated by newlines."
(let ((elems (eshell-flatten-list args)))
(while elems
(eshell-printn (eshell-echo (list (car elems))))
(setq elems (cdr elems)))))
(defun eshell/listify (&rest args)
"Return the argument(s) as a single list."
(if (> (length args) 1)
args
(if (listp (car args))
(car args)
(list (car args)))))
(defun eshell/umask (&rest args)
"Shell-like implementation of `umask'."
(eshell-eval-using-options
"umask" args
'((?S "symbolic" nil symbolic-p "display umask symbolically")
(?h "help" nil nil "display this usage message")
:usage "[-S] [mode]")
(if (or (not args) symbolic-p)
(let ((modstr
(concat "000"
(format "%o"
(logand (lognot (default-file-modes))
511)))))
(setq modstr (substring modstr (- (length modstr) 3)))
(when symbolic-p
(let ((mode (default-file-modes)))
(setq modstr
(format
"u=%s,g=%s,o=%s"
(concat (and (= (logand mode 64) 64) "r")
(and (= (logand mode 128) 128) "w")
(and (= (logand mode 256) 256) "x"))
(concat (and (= (logand mode 8) 8) "r")
(and (= (logand mode 16) 16) "w")
(and (= (logand mode 32) 32) "x"))
(concat (and (= (logand mode 1) 1) "r")
(and (= (logand mode 2) 2) "w")
(and (= (logand mode 4) 4) "x"))))))
(eshell-printn modstr))
(setcar args (eshell-convert (car args)))
(if (numberp (car args))
(set-default-file-modes
(- 511 (car (read-from-string
(concat "?\\" (number-to-string (car args)))))))
(error "setting umask symbolically is not yet implemented"))
(eshell-print
"Warning: umask changed for all new files created by Emacs.\n"))
nil))
(eval-when-compile
(defvar print-func))
;;; em-basic.el ends here
;;; em-cmpl --- completion using the TAB key
;; Copyright (C) 1999, 2000 Free Sofware Foundation
;; 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 2, 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; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
(provide 'em-cmpl)
(eval-when-compile (require 'esh-maint))
(defgroup eshell-cmpl nil
"This module provides a programmable completion function bound to
the TAB key, which allows for completing command names, file names,
variable names, arguments, etc."
:tag "Argument completion"
:group 'eshell-module)
;;; Commentary:
;; Eshell, by using the pcomplete package, provides a full
;; programmable completion facility that is comparable to shells like
;; tcsh or zsh.
;;
;; Completions are context-sensitive, which means that pressing <TAB>
;; after the command 'rmdir' will result in a list of directories,
;; while doing so after 'rm' will result in a list of all file
;; entries.
;;
;; Many builtin completion rules are provided, for commands such as
;; `cvs', or RedHat's `rpm' utility. Adding new completion rules is
;; no more difficult than writing a plain Lisp functions, and they can
;; be debugged, profiled, and compiled using exactly the same
;; facilities (since in fact, they *are* just Lisp functions). See
;; the definition of the function `pcomplete/make' for an example of
;; how to write a completion function.
;;
;; The completion facility is very easy to use. Just press TAB. If
;; there are a large number of possible completions, a buffer will
;; appearing showing a list of them. Completions may be selected from
;; that buffer using the mouse. If no completion is selected, and the
;; user starts doing something else, the display buffer will
;; automatically disappear.
;;
;; If the list of possible completions is very small, Eshell will
;; "cycle" through them, selecting a different entry each time <TAB>
;; is pressed. <S-TAB> may be used to cycle in the opposite
;; direction.
;;
;; Glob patterns can also be cycled. For example, entering 'echo
;; x*<tab>' will cycle through all the filenames beginning with 'x'.
;; This is done because the glob list is treated as though it were a
;; list of possible completions. Pressing <C-c SPC> will insert all