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