Commit | Line | Data |
---|---|---|
58a4ff04 KH |
1 | ;;; executable.el --- base functionality for executable interpreter scripts |
2 | ;; Copyright (C) 1994, 1995 by Free Software Foundation, Inc. | |
3 | ||
4 | ;; Author: Daniel.Pfeiffer@Informatik.START.dbp.de, fax (+49 69) 7588-2389 | |
5 | ;; Keywords: languages, unix | |
6 | ||
7 | ;; This file is part of GNU Emacs. | |
8 | ||
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 2, or (at your option) | |
12 | ;; any later version. | |
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 | |
20 | ;; along with GNU Emacs; see the file COPYING. If not, write to | |
21 | ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
22 | ||
23 | ;;; Commentary: | |
314001b5 RS |
24 | |
25 | ;; executable.el is used by certain major modes to insert a suitable | |
26 | ;; #! line at the beginning of the file, if the file does not already | |
27 | ;; have one. | |
28 | ||
7204084e RS |
29 | ;; This is support code for the likes of sh-, awk-, perl-, tcl- or |
30 | ;; makefile-mode. Those mode-setting commands can call the like of | |
31 | ;; `(executable-set-magic "sh")' or `(executable-set-magic "perl" "-f")'. | |
32 | ;; Unless the file name matches `executable-magicless-file-regexp' this will | |
33 | ;; search $PATH if the given interpreter isn't absolute, and then insert a | |
314001b5 RS |
34 | ;; first line like `#! /bin/sh' or `#! /usr/local/bin/perl -f'. |
35 | ;; Also it makes the file executable as soon as it's saved, if it wasn't. | |
4a789a51 | 36 | |
58a4ff04 KH |
37 | ;;; Code: |
38 | ||
39 | (defvar executable-insert 'not-modified | |
40 | "*What to do when newly found file has no or wrong magic number: | |
41 | nil do nothing | |
42 | t insert or update magic number | |
43 | other insert or update magic number, but mark as unmodified. | |
44 | When the insertion is marked as unmodified, you can save it with \\[write-file] RET. | |
45 | This variable is used when `executable-set-magic' is called as a function, | |
46 | e.g. when Emacs sets some Un*x interpreter script mode. | |
47 | With \\[executable-set-magic], this is always treated as if it were `t'.") | |
48 | ||
49 | ||
50 | (defvar executable-query 'function | |
51 | "*If non-`nil', ask user before inserting or changing magic number. | |
52 | When this is `function', only ask when called non-interactively.") | |
53 | ||
54 | ||
55 | (defvar executable-magicless-file-regexp "/[Mm]akefile$\\|/\\.\\(z?profile\\|bash_profile\\|z?login\\|bash_login\\|z?logout\\|bash_logout\\|.+shrc\\|esrc\\|rcrc\\|[kz]shenv\\)$" | |
56 | "*On files with this kind of name no magic is inserted or changed.") | |
57 | ||
58 | ||
59 | (defvar executable-prefix "#! " | |
60 | "*Interpreter magic number prefix inserted when there was no magic number.") | |
61 | ||
62 | ||
63 | ||
64 | (defvar executable-chmod 73 | |
65 | "*After saving, if the file is not executable, set this mode. | |
66 | This mode passed to `set-file-modes' is taken absolutely when negative, or | |
67 | relative to the files existing modes. Do nothing if this is nil. | |
68 | Typical values are 73 (+x) or -493 (rwxr-xr-x).") | |
69 | ||
70 | ||
71 | (defvar executable-command nil) | |
72 | ||
73 | ||
4a789a51 RS |
74 | ;; Autoload cookie deleted here because it made loaddefs.el fail to load. |
75 | ;; -rms | |
58a4ff04 KH |
76 | (or (assoc "tail" interpreter-mode-alist) |
77 | (nconc interpreter-mode-alist | |
78 | '(("tail" . text-mode) | |
79 | ("more" . text-mode) | |
80 | ("less" . text-mode) | |
81 | ("pg" . text-mode)))) | |
82 | ||
83 | (defvar executable-self-display "tail" | |
84 | "*Command you use with argument `+2' to make text files self-display. | |
85 | Note that the like of `more' doesn't work too well under Emacs \\[shell].") | |
86 | ||
87 | ||
88 | (defvar executable-font-lock-keywords | |
89 | '(("\\`#!.*/\\([^ \t\n]+\\)" 1 font-lock-keyword-face t)) | |
90 | "*Rules for highlighting executable scripts' magic number. | |
91 | This can be included in `font-lock-keywords' by modes that call `executable'.") | |
92 | ||
93 | ||
94 | (defvar executable-error-regexp-alist | |
95 | '(;; /bin/xyz: syntax error at line 14: `(' unexpected | |
96 | ;; /bin/xyz[5]: syntax error at line 8 : ``' unmatched | |
97 | ("^\\(.*[^[/]\\)\\(\\[[0-9]+\\]\\)?: .* error .* line \\([0-9]+\\)" 1 3) | |
98 | ;; /bin/xyz[27]: ehco: not found | |
99 | ("^\\(.*[^/]\\)\\[\\([0-9]+\\)\\]: .*: " 1 2) | |
100 | ;; /bin/xyz: syntax error near unexpected token `)' | |
101 | ;; /bin/xyz: /bin/xyz: line 2: `)' | |
102 | ("^\\(.*[^/]\\): [^0-9\n]+\n\\1: \\1: line \\([0-9]+\\):" 1 2) | |
103 | ;; /usr/bin/awk: syntax error at line 5 of file /bin/xyz | |
104 | (" error .* line \\([0-9]+\\) of file \\(.+\\)$" 2 1) | |
105 | ;; /usr/bin/awk: calling undefined function toto | |
106 | ;; input record number 3, file awktestdata | |
107 | ;; source line 4 of file /bin/xyz | |
108 | ("^[^ ].+\n\\( .+\n\\)* line \\([0-9]+\\) of file \\(.+\\)$" 3 2) | |
109 | ;; makefile:1: *** target pattern contains no `%'. Stop. | |
110 | ("^\\(.+\\):\\([0-9]+\\): " 1 2)) | |
111 | "Alist of regexps used to match script errors. | |
112 | See `compilation-error-regexp-alist'.") | |
113 | ||
114 | ;; The C function openp() slightly modified would do the trick fine | |
115 | (defun executable (command) | |
116 | "If COMMAND is an executable in $PATH its full name is returned. Else nil." | |
117 | (let ((list exec-path) | |
118 | path) | |
119 | (while list | |
120 | (setq list (if (and (setq path (expand-file-name command (car list))) | |
121 | (file-executable-p path) | |
122 | (not (file-directory-p path))) | |
123 | nil | |
124 | (setq path nil) | |
125 | (cdr list)))) | |
126 | path)) | |
127 | ||
128 | ||
129 | (defun executable-chmod () | |
130 | "This gets called after saving a file to assure that it be executable. | |
131 | You can set the absolute or relative mode in variable `executable-chmod' for | |
132 | non-executable files." | |
133 | (and executable-chmod | |
134 | buffer-file-name | |
135 | (or (file-executable-p buffer-file-name) | |
136 | (set-file-modes buffer-file-name | |
137 | (if (< executable-chmod 0) | |
138 | (- executable-chmod) | |
139 | (logior executable-chmod | |
140 | (file-modes buffer-file-name))))))) | |
141 | ||
142 | ||
143 | (defun executable-interpret (command) | |
144 | "Run script with user-specified args, and collect output in a buffer. | |
145 | While script runs asynchronously, you can use the \\[next-error] command | |
146 | to find the next error." | |
147 | (interactive (list (read-string "Run script: " | |
148 | (or executable-command | |
149 | buffer-file-name)))) | |
150 | (require 'compile) | |
151 | (save-some-buffers (not compilation-ask-about-save)) | |
152 | (make-local-variable 'executable-command) | |
153 | (compile-internal (setq executable-command command) | |
154 | "No more errors." "Interpretation" | |
155 | ;; Give it a simpler regexp to match. | |
156 | nil executable-error-regexp-alist)) | |
157 | ||
158 | ||
159 | ||
160 | ;;;###autoload | |
161 | (defun executable-set-magic (interpreter &optional argument) | |
162 | "Set this buffer's interpreter to INTERPRETER with optional ARGUMENT. | |
163 | The variables `executable-magicless-file-regexp', `executable-prefix', | |
164 | `executable-insert', `executable-query' and `executable-chmod' control | |
165 | when and how magic numbers are inserted or replaced and scripts made | |
166 | executable." | |
167 | (interactive "sName or path of interpreter: \nsArgument for %s: ") | |
168 | (setq interpreter (if (file-name-absolute-p interpreter) | |
169 | interpreter | |
170 | (or (executable interpreter) | |
171 | (error "Cannot find %s." interpreter))) | |
172 | argument (concat interpreter | |
173 | (and argument (string< "" argument) " ") | |
174 | argument)) | |
175 | (or buffer-read-only | |
176 | (if buffer-file-name | |
177 | (string-match executable-magicless-file-regexp | |
178 | buffer-file-name)) | |
179 | (not (or (eq this-command 'executable-set-magic) | |
180 | executable-insert)) | |
181 | (> (point-min) 1) | |
182 | (let ((point (point-marker)) | |
183 | (buffer-modified-p (buffer-modified-p))) | |
184 | (goto-char (point-min)) | |
185 | (make-local-variable 'after-save-hook) | |
186 | (add-hook 'after-save-hook 'executable-chmod) | |
187 | (if (looking-at "#![ \t]*\\(.*\\)$") | |
188 | (and (goto-char (match-beginning 1)) | |
189 | (not (string= argument | |
190 | (buffer-substring (point) (match-end 1)))) | |
191 | (save-window-excursion | |
192 | ;; make buffer visible before question or message | |
193 | (switch-to-buffer (current-buffer)) | |
194 | (if (or (not executable-query) | |
195 | (and (eq executable-query 'function) | |
196 | (eq this-command 'executable-set-magic))) | |
197 | (message "%s Magic number ``%s'' replaced." this-command | |
198 | (buffer-substring (point-min) (match-end 1))) | |
199 | (y-or-n-p (concat "Replace magic number by ``" | |
200 | executable-prefix argument "''? ")))) | |
201 | (not (delete-region (point) (match-end 1))) | |
202 | (insert argument)) | |
203 | (insert executable-prefix argument ?\n)) | |
204 | (or (< (marker-position point) (point)) | |
205 | (goto-char point)) | |
206 | (or (eq this-command 'executable-set-magic)) | |
207 | (eq executable-insert t) | |
208 | (set-buffer-modified-p buffer-modified-p))) | |
209 | interpreter) | |
210 | ||
211 | ||
212 | ||
213 | ;;;###autoload | |
214 | (defun executable-self-display () | |
215 | "Turn a text file into a self-displaying Un*x command. | |
216 | The magic number of such a command displays all lines but itself." | |
217 | (interactive) | |
218 | (if (eq this-command 'executable-self-display) | |
219 | (setq this-command 'executable-set-magic)) | |
220 | (executable-set-magic executable-self-display "+2")) | |
221 | ||
222 | ||
223 | ||
224 | (provide 'executable) | |
225 | ||
226 | ;; executable.el ends here |