* lisp/vc/diff-mode.el (diff-wiggle): New command.
[emacs.git] / lisp / eshell / em-basic.el
blob5201076f4853a9960808780fa22250fea4169ad6
1 ;;; em-basic.el --- basic shell builtin commands -*- lexical-binding:t -*-
3 ;; Copyright (C) 1999-2018 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 <https://www.gnu.org/licenses/>.
22 ;;; Commentary:
24 ;; There are very few basic Eshell commands -- so-called builtins.
25 ;; They are: echo, umask, and version.
27 ;;;_* `echo'
29 ;; The `echo' command repeats its arguments to the screen. It is
30 ;; optional whether this is done in a Lisp-friendly fashion (so that
31 ;; the value of echo is useful to a Lisp command using the result of
32 ;; echo as an argument), or whether it should try to act like a normal
33 ;; shell echo, and always result in a flat string being returned.
35 ;; An example of the difference is the following:
37 ;; echo Hello world
39 ;; If `eshell-plain-echo-behavior' is non-nil, this will yield the
40 ;; string "Hello world". If Lisp behavior is enabled, however, it
41 ;; will yield a list whose two elements are the strings "Hello" and
42 ;; "world". The way to write an equivalent expression for both would
43 ;; be:
45 ;; echo "Hello world"
47 ;; This always returns a single string.
49 ;;;_* `umask'
51 ;; The umask command changes the default file permissions for newly
52 ;; created files. It uses the same syntax as bash.
54 ;;; Code:
56 (require 'esh-util)
57 (require 'eshell)
58 (require 'esh-opt)
60 ;;;###autoload
61 (progn
62 (defgroup eshell-basic nil
63 "The \"basic\" code provides a set of convenience functions which
64 are traditionally considered shell builtins. Since all of the
65 functionality provided by them is accessible through Lisp, they are
66 not really builtins at all, but offer a command-oriented way to do the
67 same thing."
68 :tag "Basic shell commands"
69 :group 'eshell-module))
71 (defcustom eshell-plain-echo-behavior nil
72 "If non-nil, `echo' tries to behave like an ordinary shell echo.
73 This comes at some detriment to Lisp functionality. However, the Lisp
74 equivalent of `echo' can always be achieved by using `identity'."
75 :type 'boolean
76 :group 'eshell-basic)
78 ;;; Functions:
80 (defun eshell-echo (args &optional output-newline)
81 "Implementation code for a Lisp version of `echo'.
82 It returns a formatted value that should be passed to `eshell-print'
83 or `eshell-printn' for display."
84 (if eshell-plain-echo-behavior
85 (concat (apply 'eshell-flatten-and-stringify args) "\n")
86 (let ((value
87 (cond
88 ((= (length args) 0) "")
89 ((= (length args) 1)
90 (car args))
92 (mapcar
93 (function
94 (lambda (arg)
95 (if (stringp arg)
96 (set-text-properties 0 (length arg) nil arg))
97 arg))
98 args)))))
99 (if output-newline
100 (cond
101 ((stringp value)
102 (concat value "\n"))
103 ((listp value)
104 (append value (list "\n")))
106 (concat (eshell-stringify value) "\n")))
107 value))))
109 (defun eshell/echo (&rest args)
110 "Implementation of `echo'. See `eshell-plain-echo-behavior'."
111 (eshell-eval-using-options
112 "echo" args
113 '((?n nil nil output-newline "terminate with a newline")
114 (?h "help" nil nil "output this help screen")
115 :preserve-args
116 :usage "[-n] [object]")
117 (eshell-echo args output-newline)))
119 (defun eshell/printnl (&rest args)
120 "Print out each of the arguments, separated by newlines."
121 (let ((elems (eshell-flatten-list args)))
122 (while elems
123 (eshell-printn (eshell-echo (list (car elems))))
124 (setq elems (cdr elems)))))
126 (defun eshell/listify (&rest args)
127 "Return the argument(s) as a single list."
128 (if (> (length args) 1)
129 args
130 (if (listp (car args))
131 (car args)
132 (list (car args)))))
134 (defun eshell/umask (&rest args)
135 "Shell-like implementation of `umask'."
136 (eshell-eval-using-options
137 "umask" args
138 '((?S "symbolic" nil symbolic-p "display umask symbolically")
139 (?h "help" nil nil "display this usage message")
140 :usage "[-S] [mode]")
141 (if (or (not args) symbolic-p)
142 (let ((modstr
143 (concat "000"
144 (format "%o"
145 (logand (lognot (default-file-modes))
146 511)))))
147 (setq modstr (substring modstr (- (length modstr) 3)))
148 (when symbolic-p
149 (let ((mode (default-file-modes)))
150 (setq modstr
151 (format
152 "u=%s,g=%s,o=%s"
153 (concat (and (= (logand mode 64) 64) "r")
154 (and (= (logand mode 128) 128) "w")
155 (and (= (logand mode 256) 256) "x"))
156 (concat (and (= (logand mode 8) 8) "r")
157 (and (= (logand mode 16) 16) "w")
158 (and (= (logand mode 32) 32) "x"))
159 (concat (and (= (logand mode 1) 1) "r")
160 (and (= (logand mode 2) 2) "w")
161 (and (= (logand mode 4) 4) "x"))))))
162 (eshell-printn modstr))
163 (setcar args (eshell-convert (car args)))
164 (if (numberp (car args))
165 (set-default-file-modes
166 (- 511 (car (read-from-string
167 (concat "?\\" (number-to-string (car args)))))))
168 (error "setting umask symbolically is not yet implemented"))
169 (eshell-print
170 "Warning: umask changed for all new files created by Emacs.\n"))
171 nil))
173 (provide 'em-basic)
175 ;; Local Variables:
176 ;; generated-autoload-file: "esh-groups.el"
177 ;; End:
179 ;;; em-basic.el ends here