]> code.delx.au - gnu-emacs/blob - lisp/eshell/em-script.el
Merge from emacs--rel--22
[gnu-emacs] / lisp / eshell / em-script.el
1 ;;; em-script.el --- Eshell script files
2
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5
6 ;; Author: John Wiegley <johnw@gnu.org>
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;;; Code:
26
27 (require 'eshell)
28
29 (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 (concat eshell-directory-name "login")
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 (concat eshell-directory-name "profile")
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 after-change-functions)
94 (goto-char (point-max))
95 (insert-file-contents file)
96 (goto-char (point-max))
97 (throw 'eshell-replace-command
98 (prog1
99 (list 'let
100 (list (list 'eshell-command-name (list 'quote file))
101 (list 'eshell-command-arguments
102 (list 'quote args)))
103 (let ((cmd (eshell-parse-command (cons here (point)))))
104 (if subcommand-p
105 (setq cmd (list 'eshell-as-subcommand cmd)))
106 cmd))
107 (delete-region here (point))
108 (goto-char orig)))))
109
110 (defun eshell/source (&rest args)
111 "Source a file in a subshell environment."
112 (eshell-eval-using-options
113 "source" args
114 '((?h "help" nil nil "show this usage screen")
115 :show-usage
116 :usage "FILE [ARGS]
117 Invoke the Eshell commands in FILE in a subshell, binding ARGS to $1,
118 $2, etc.")
119 (eshell-source-file (car args) (cdr args) t)))
120
121 (put 'eshell/source 'eshell-no-numeric-conversions t)
122
123 (defun eshell/. (&rest args)
124 "Source a file in the current environment."
125 (eshell-eval-using-options
126 "." args
127 '((?h "help" nil nil "show this usage screen")
128 :show-usage
129 :usage "FILE [ARGS]
130 Invoke the Eshell commands in FILE within the current shell
131 environment, binding ARGS to $1, $2, etc.")
132 (eshell-source-file (car args) (cdr args))))
133
134 (put 'eshell/. 'eshell-no-numeric-conversions t)
135
136 (provide 'em-script)
137
138 ;; arch-tag: a346439d-5ba8-4faf-ac2b-3aacfeaa4647
139 ;;; em-script.el ends here