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