em-script.el 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. ;;; em-script.el --- Eshell script files
  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. ;;; Code:
  17. (require 'eshell)
  18. ;;;###autoload
  19. (eshell-defgroup eshell-script nil
  20. "This module allows for the execution of files containing Eshell
  21. commands, as a script file."
  22. :tag "Running script files."
  23. :group 'eshell-module)
  24. ;;; User Variables:
  25. (defcustom eshell-script-load-hook nil
  26. "A list of functions to call when loading `eshell-script'."
  27. :version "24.1" ; removed eshell-script-initialize
  28. :type 'hook
  29. :group 'eshell-script)
  30. (defcustom eshell-login-script (expand-file-name "login" eshell-directory-name)
  31. "If non-nil, a file to invoke when starting up Eshell interactively.
  32. This file should be a file containing Eshell commands, where comment
  33. lines begin with '#'."
  34. :type 'file
  35. :group 'eshell-script)
  36. (defcustom eshell-rc-script (expand-file-name "profile" eshell-directory-name)
  37. "If non-nil, a file to invoke whenever Eshell is started.
  38. This includes when running `eshell-command'."
  39. :type 'file
  40. :group 'eshell-script)
  41. ;;; Functions:
  42. (defun eshell-script-initialize ()
  43. "Initialize the script parsing code."
  44. (make-local-variable 'eshell-interpreter-alist)
  45. (setq eshell-interpreter-alist
  46. (cons '((lambda (file)
  47. (string= (file-name-nondirectory file)
  48. "eshell")) . eshell/source)
  49. eshell-interpreter-alist))
  50. (make-local-variable 'eshell-complex-commands)
  51. (setq eshell-complex-commands
  52. (append '("source" ".") eshell-complex-commands))
  53. ;; these two variables are changed through usage, but we don't want
  54. ;; to ruin it for other modules
  55. (let (eshell-inside-quote-regexp
  56. eshell-outside-quote-regexp)
  57. (and (not eshell-non-interactive-p)
  58. eshell-login-script
  59. (file-readable-p eshell-login-script)
  60. (eshell-do-eval
  61. (list 'eshell-commands
  62. (catch 'eshell-replace-command
  63. (eshell-source-file eshell-login-script))) t))
  64. (and eshell-rc-script
  65. (file-readable-p eshell-rc-script)
  66. (eshell-do-eval
  67. (list 'eshell-commands
  68. (catch 'eshell-replace-command
  69. (eshell-source-file eshell-rc-script))) t))))
  70. (defun eshell-source-file (file &optional args subcommand-p)
  71. "Execute a series of Eshell commands in FILE, passing ARGS.
  72. Comments begin with '#'."
  73. (interactive "f")
  74. (let ((orig (point))
  75. (here (point-max))
  76. (inhibit-point-motion-hooks t))
  77. (goto-char (point-max))
  78. (with-silent-modifications
  79. ;; FIXME: Why not use a temporary buffer and avoid this
  80. ;; "insert&delete" business? --Stef
  81. (insert-file-contents file)
  82. (goto-char (point-max))
  83. (throw 'eshell-replace-command
  84. (prog1
  85. (list 'let
  86. (list (list 'eshell-command-name (list 'quote file))
  87. (list 'eshell-command-arguments
  88. (list 'quote args)))
  89. (let ((cmd (eshell-parse-command (cons here (point)))))
  90. (if subcommand-p
  91. (setq cmd (list 'eshell-as-subcommand cmd)))
  92. cmd))
  93. (delete-region here (point))
  94. (goto-char orig))))))
  95. (defun eshell/source (&rest args)
  96. "Source a file in a subshell environment."
  97. (eshell-eval-using-options
  98. "source" args
  99. '((?h "help" nil nil "show this usage screen")
  100. :show-usage
  101. :usage "FILE [ARGS]
  102. Invoke the Eshell commands in FILE in a subshell, binding ARGS to $1,
  103. $2, etc.")
  104. (eshell-source-file (car args) (cdr args) t)))
  105. (put 'eshell/source 'eshell-no-numeric-conversions t)
  106. (defun eshell/. (&rest args)
  107. "Source a file in the current environment."
  108. (eshell-eval-using-options
  109. "." args
  110. '((?h "help" nil nil "show this usage screen")
  111. :show-usage
  112. :usage "FILE [ARGS]
  113. Invoke the Eshell commands in FILE within the current shell
  114. environment, binding ARGS to $1, $2, etc.")
  115. (eshell-source-file (car args) (cdr args))))
  116. (put 'eshell/. 'eshell-no-numeric-conversions t)
  117. (provide 'em-script)
  118. ;; Local Variables:
  119. ;; generated-autoload-file: "esh-groups.el"
  120. ;; End:
  121. ;;; em-script.el ends here