em-basic.el 5.7 KB

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