| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
(provide 'em-script) |
|---|
| 26 |
|
|---|
| 27 |
(eval-when-compile (require 'esh-maint)) |
|---|
| 28 |
|
|---|
| 29 |
(require 'eshell) |
|---|
| 30 |
|
|---|
| 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) |
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
(defcustom eshell-script-load-hook '(eshell-script-initialize) |
|---|
| 42 |
"*A list of functions to call when loading `eshell-script'." |
|---|
| 43 |
:type 'hook |
|---|
| 44 |
:group 'eshell-script) |
|---|
| 45 |
|
|---|
| 46 |
(defcustom eshell-login-script (concat eshell-directory-name "login") |
|---|
| 47 |
"*If non-nil, a file to invoke when starting up Eshell interactively. |
|---|
| 48 |
This file should be a file containing Eshell commands, where comment |
|---|
| 49 |
lines begin with '#'." |
|---|
| 50 |
:type 'file |
|---|
| 51 |
:group 'eshell-script) |
|---|
| 52 |
|
|---|
| 53 |
(defcustom eshell-rc-script (concat eshell-directory-name "profile") |
|---|
| 54 |
"*If non-nil, a file to invoke whenever Eshell is started. |
|---|
| 55 |
This includes when running `eshell-command'." |
|---|
| 56 |
:type 'file |
|---|
| 57 |
:group 'eshell-script) |
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 |
(defun eshell-script-initialize () |
|---|
| 62 |
"Initialize the script parsing code." |
|---|
| 63 |
(make-local-variable 'eshell-interpreter-alist) |
|---|
| 64 |
(setq eshell-interpreter-alist |
|---|
| 65 |
(cons '((lambda (file) |
|---|
| 66 |
(string= (file-name-nondirectory file) |
|---|
| 67 |
"eshell")) . 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 |
|
|---|
| 73 |
|
|---|
| 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)))) |
|---|
| 89 |
|
|---|
| 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 |
after-change-functions) |
|---|
| 98 |
(goto-char (point-max)) |
|---|
| 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 |
|
|---|
| 141 |
|
|---|
| 142 |
|
|---|
| 143 |
|
|---|
| 144 |
|
|---|