Switch to recommended form of GPLv3 permissions notice.
[bpt/emacs.git] / lisp / eshell / em-basic.el
CommitLineData
60370d40 1;;; em-basic.el --- basic shell builtin commands
affbf647 2
f2e3589a 3;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004,
8b72699e 4;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
affbf647 5
7de5b421
GM
6;; Author: John Wiegley <johnw@gnu.org>
7
affbf647
GM
8;; This file is part of GNU Emacs.
9
4ee57b2a 10;; GNU Emacs is free software: you can redistribute it and/or modify
affbf647 11;; it under the terms of the GNU General Public License as published by
4ee57b2a
GM
12;; the Free Software Foundation, either version 3 of the License, or
13;; (at your option) any later version.
affbf647
GM
14
15;; GNU Emacs is distributed in the hope that it will be useful,
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
4ee57b2a 21;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
affbf647 22
affbf647
GM
23;;; Commentary:
24
25;; There are very few basic Eshell commands -- so-called builtins.
26;; They are: echo, umask, and version.
27;;
28;;;_* `echo'
29;;
30;; The `echo' command repeats its arguments to the screen. It is
31;; optional whether this is done in a Lisp-friendly fashion (so that
32;; the value of echo is useful to a Lisp command using the result of
33;; echo as an argument), or whether it should try to act like a normal
34;; shell echo, and always result in a flat string being returned.
35
affbf647
GM
36;; An example of the difference is the following:
37;;
38;; echo Hello world
39;;
40;; If `eshell-plain-echo-behavior' is non-nil, this will yield the
41;; string "Hello world". If Lisp behavior is enabled, however, it
42;; will yield a list whose two elements are the strings "Hello" and
43;; "world". The way to write an equivalent expression for both would
44;; be:
45;;
46;; echo "Hello world"
47;;
48;; This always returns a single string.
49;;
50;;;_* `umask'
51;;
52;; The umask command changes the default file permissions for newly
53;; created files. It uses the same syntax as bash.
54;;
55;;;_* `version'
56;;
57;; This command reports the version number for Eshell and all its
58;; dependent module, including the date when those modules were last
59;; modified.
60
61;;; Code:
62
20d7538e
GM
63(eval-when-compile
64 (require 'esh-util))
65
affbf647
GM
66(require 'esh-opt)
67
20d7538e
GM
68(defgroup eshell-basic nil
69 "The \"basic\" code provides a set of convenience functions which
70are traditionally considered shell builtins. Since all of the
71functionality provided by them is accessible through Lisp, they are
72not really builtins at all, but offer a command-oriented way to do the
73same thing."
74 :tag "Basic shell commands"
75 :group 'eshell-module)
76
77(defcustom eshell-plain-echo-behavior nil
78 "*If non-nil, `echo' tries to behave like an ordinary shell echo.
79This comes at some detriment to Lisp functionality. However, the Lisp
80equivalent of `echo' can always be achieved by using `identity'."
81 :type 'boolean
82 :group 'eshell-basic)
83
affbf647
GM
84;;; Functions:
85
86(defun eshell-echo (args &optional output-newline)
87 "Implementation code for a Lisp version of `echo'.
88It returns a formatted value that should be passed to `eshell-print'
89or `eshell-printn' for display."
90 (if eshell-plain-echo-behavior
91 (concat (apply 'eshell-flatten-and-stringify args) "\n")
92 (let ((value
93 (cond
94 ((= (length args) 0) "")
95 ((= (length args) 1)
96 (car args))
97 (t
98 (mapcar
99 (function
100 (lambda (arg)
101 (if (stringp arg)
102 (set-text-properties 0 (length arg) nil arg))
103 arg))
104 args)))))
105 (if output-newline
106 (cond
107 ((stringp value)
108 (concat value "\n"))
109 ((listp value)
110 (append value (list "\n")))
111 (t
112 (concat (eshell-stringify value) "\n")))
113 value))))
114
115(defun eshell/echo (&rest args)
116 "Implementation of `echo'. See `eshell-plain-echo-behavior'."
117 (eshell-eval-using-options
118 "echo" args
119 '((?n nil nil output-newline "terminate with a newline")
120 (?h "help" nil nil "output this help screen")
121 :preserve-args
122 :usage "[-n] [object]")
123 (eshell-echo args output-newline)))
124
125(defun eshell/printnl (&rest args)
126 "Print out each of the argument, separated by newlines."
127 (let ((elems (eshell-flatten-list args)))
128 (while elems
129 (eshell-printn (eshell-echo (list (car elems))))
130 (setq elems (cdr elems)))))
131
132(defun eshell/listify (&rest args)
133 "Return the argument(s) as a single list."
134 (if (> (length args) 1)
135 args
136 (if (listp (car args))
137 (car args)
138 (list (car args)))))
139
140(defun eshell/umask (&rest args)
141 "Shell-like implementation of `umask'."
142 (eshell-eval-using-options
143 "umask" args
144 '((?S "symbolic" nil symbolic-p "display umask symbolically")
145 (?h "help" nil nil "display this usage message")
146 :usage "[-S] [mode]")
147 (if (or (not args) symbolic-p)
148 (let ((modstr
149 (concat "000"
150 (format "%o"
151 (logand (lognot (default-file-modes))
152 511)))))
153 (setq modstr (substring modstr (- (length modstr) 3)))
154 (when symbolic-p
155 (let ((mode (default-file-modes)))
156 (setq modstr
157 (format
158 "u=%s,g=%s,o=%s"
159 (concat (and (= (logand mode 64) 64) "r")
160 (and (= (logand mode 128) 128) "w")
161 (and (= (logand mode 256) 256) "x"))
162 (concat (and (= (logand mode 8) 8) "r")
163 (and (= (logand mode 16) 16) "w")
164 (and (= (logand mode 32) 32) "x"))
165 (concat (and (= (logand mode 1) 1) "r")
166 (and (= (logand mode 2) 2) "w")
167 (and (= (logand mode 4) 4) "x"))))))
168 (eshell-printn modstr))
169 (setcar args (eshell-convert (car args)))
170 (if (numberp (car args))
171 (set-default-file-modes
172 (- 511 (car (read-from-string
173 (concat "?\\" (number-to-string (car args)))))))
174 (error "setting umask symbolically is not yet implemented"))
175 (eshell-print
176 "Warning: umask changed for all new files created by Emacs.\n"))
177 nil))
178
20d7538e 179(provide 'em-basic)
affbf647 180
cbee283d 181;; arch-tag: 385a31b1-cb95-46f0-9829-9d352ee77db8
affbf647 182;;; em-basic.el ends here