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