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