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