]> code.delx.au - gnu-emacs/blob - lisp/progmodes/executable.el
(dsssl-sgml-declaration): Doc fix.
[gnu-emacs] / lisp / progmodes / executable.el
1 ;;; executable.el --- base functionality for executable interpreter scripts
2
3 ;; Copyright (C) 1994, 1995, 1996 by Free Software Foundation, Inc.
4
5 ;; Author: Daniel.Pfeiffer@Informatik.START.dbp.de, fax (+49 69) 7588-2389
6 ;; Keywords: languages, unix
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;; executable.el is used by certain major modes to insert a suitable
28 ;; #! line at the beginning of the file, if the file does not already
29 ;; have one.
30
31 ;; Unless it has a magic number, a Unix file with executable mode is passed to
32 ;; a new instance of the running shell (or to a Bourne shell if a csh is
33 ;; running and the file starts with `:'). Only a shell can start such a file,
34 ;; exec() cannot, which is why it is important to have a magic number in every
35 ;; executable script. Such a magic number is made up by the characters `#!'
36 ;; the filename of an interpreter (in COFF, ELF or somesuch format) and one
37 ;; optional argument.
38
39 ;; This library is for certain major modes like sh-, awk-, perl-, tcl- or
40 ;; makefile-mode to insert or update a suitable #! line at the beginning of
41 ;; the file, if the file does not already have one and the file is not a
42 ;; default file of that interpreter (like .profile or makefile). It also
43 ;; makes the file executable if it wasn't, as soon as it's saved.
44
45 ;; It also allows debugging scripts, with an adaptation of compile, as far
46 ;; as interpreters give out meaningful error messages.
47
48 ;; Modes that use this should nconc `executable-map' to the end of their own
49 ;; keymap and `executable-font-lock-keywords' to the end of their own font
50 ;; lock keywords. Their mode-setting commands should call
51 ;; `executable-set-magic'.
52
53 ;;; Code:
54
55 (defgroup executable nil
56 "Base functionality for executable interpreter scripts"
57 :group 'processes)
58
59 (defcustom executable-insert 'other
60 "*Non-nil means offer to add a magic number to a file.
61 This takes effect when you switch to certain major modes,
62 including Shell-script mode (`sh-mode').
63 When you type \\[executable-set-magic], it always offers to add or
64 update the magic number."
65 :type '(choice (const :tag "off" nil)
66 (const :tag "on" t)
67 symbol)
68 :group 'executable)
69
70
71 (defcustom executable-query 'function
72 "*If non-nil, ask user before changing an existing magic number.
73 When this is `function', only ask when called non-interactively."
74 :type '(choice (const :tag "Don't Ask" nil)
75 (const :tag "Ask" t)
76 (const :tag "Ask when non-interactive" function))
77 :group 'executable)
78
79
80 (defcustom executable-magicless-file-regexp "/[Mm]akefile$\\|/\\.\\(z?profile\\|bash_profile\\|z?login\\|bash_login\\|z?logout\\|bash_logout\\|.+shrc\\|esrc\\|rcrc\\|[kz]shenv\\)$"
81 "*On files with this kind of name no magic is inserted or changed."
82 :type 'regexp
83 :group 'executable)
84
85
86 (defcustom executable-prefix "#! "
87 "*Interpreter magic number prefix inserted when there was no magic number."
88 :type 'string
89 :group 'executable)
90
91
92 (defcustom executable-chmod 73
93 "*After saving, if the file is not executable, set this mode.
94 This mode passed to `set-file-modes' is taken absolutely when negative, or
95 relative to the files existing modes. Do nothing if this is nil.
96 Typical values are 73 (+x) or -493 (rwxr-xr-x)."
97 :type 'integer
98 :group 'executable)
99
100
101 (defvar executable-command nil)
102
103 (defcustom executable-self-display "tail"
104 "*Command you use with argument `+2' to make text files self-display.
105 Note that the like of `more' doesn't work too well under Emacs \\[shell]."
106 :type 'string
107 :group 'executable)
108
109
110 (defvar executable-font-lock-keywords
111 '(("\\`#!.*/\\([^ \t\n]+\\)" 1 font-lock-keyword-face t))
112 "*Rules for highlighting executable scripts' magic number.
113 This can be included in `font-lock-keywords' by modes that call `executable'.")
114
115
116 (defvar executable-error-regexp-alist
117 '(;; /bin/xyz: syntax error at line 14: `(' unexpected
118 ;; /bin/xyz[5]: syntax error at line 8 : ``' unmatched
119 ("^\\(.*[^[/]\\)\\(\\[[0-9]+\\]\\)?: .* error .* line \\([0-9]+\\)" 1 3)
120 ;; /bin/xyz[27]: ehco: not found
121 ("^\\(.*[^/]\\)\\[\\([0-9]+\\)\\]: .*: " 1 2)
122 ;; /bin/xyz: syntax error near unexpected token `)'
123 ;; /bin/xyz: /bin/xyz: line 2: `)'
124 ("^\\(.*[^/]\\): [^0-9\n]+\n\\1: \\1: line \\([0-9]+\\):" 1 2)
125 ;; /usr/bin/awk: syntax error at line 5 of file /bin/xyz
126 (" error .* line \\([0-9]+\\) of file \\(.+\\)$" 2 1)
127 ;; /usr/bin/awk: calling undefined function toto
128 ;; input record number 3, file awktestdata
129 ;; source line 4 of file /bin/xyz
130 ("^[^ ].+\n\\( .+\n\\)* line \\([0-9]+\\) of file \\(.+\\)$" 3 2)
131 ;; makefile:1: *** target pattern contains no `%'. Stop.
132 ("^\\(.+\\):\\([0-9]+\\): " 1 2))
133 "Alist of regexps used to match script errors.
134 See `compilation-error-regexp-alist'.")
135
136 ;; The C function openp slightly modified would do the trick fine
137 (defun executable-find (command)
138 "Search for COMMAND in exec-path and return the absolute file name.
139 Return nil if COMMAND is not found anywhere in `exec-path'."
140 (let ((list exec-path)
141 file)
142 (while list
143 (setq list (if (and (setq file (expand-file-name command (car list)))
144 (file-executable-p file)
145 (not (file-directory-p file)))
146 nil
147 (setq file nil)
148 (cdr list))))
149 file))
150
151
152 (defun executable-chmod ()
153 "This gets called after saving a file to assure that it be executable.
154 You can set the absolute or relative mode in variable `executable-chmod' for
155 non-executable files."
156 (and executable-chmod
157 buffer-file-name
158 (or (file-executable-p buffer-file-name)
159 (set-file-modes buffer-file-name
160 (if (< executable-chmod 0)
161 (- executable-chmod)
162 (logior executable-chmod
163 (file-modes buffer-file-name)))))))
164
165
166 (defun executable-interpret (command)
167 "Run script with user-specified args, and collect output in a buffer.
168 While script runs asynchronously, you can use the \\[next-error] command
169 to find the next error."
170 (interactive (list (read-string "Run script: "
171 (or executable-command
172 buffer-file-name))))
173 (require 'compile)
174 (save-some-buffers (not compilation-ask-about-save))
175 (make-local-variable 'executable-command)
176 (compile-internal (setq executable-command command)
177 "No more errors." "Interpretation"
178 ;; Give it a simpler regexp to match.
179 nil executable-error-regexp-alist))
180
181
182
183 ;;;###autoload
184 (defun executable-set-magic (interpreter &optional argument
185 no-query-flag insert-flag)
186 "Set this buffer's interpreter to INTERPRETER with optional ARGUMENT.
187 The variables `executable-magicless-file-regexp', `executable-prefix',
188 `executable-insert', `executable-query' and `executable-chmod' control
189 when and how magic numbers are inserted or replaced and scripts made
190 executable."
191 (interactive
192 (let* ((name (read-string "Name or file name of interpreter: "))
193 (arg (read-string (format "Argument for %s: " name))))
194 (list name arg (eq executable-query 'function) t)))
195 (setq interpreter (if (file-name-absolute-p interpreter)
196 interpreter
197 (or (executable-find interpreter)
198 (error "Interpreter %s not recognized" interpreter)))
199 argument (concat interpreter
200 (and argument (string< "" argument) " ")
201 argument))
202 (or buffer-read-only
203 (if buffer-file-name
204 (string-match executable-magicless-file-regexp
205 buffer-file-name))
206 (not (or insert-flag executable-insert))
207 (> (point-min) 1)
208 (save-excursion
209 (let ((point (point-marker))
210 (buffer-modified-p (buffer-modified-p)))
211 (goto-char (point-min))
212 (make-local-hook 'after-save-hook)
213 (add-hook 'after-save-hook 'executable-chmod nil t)
214 (if (looking-at "#![ \t]*\\(.*\\)$")
215 (and (goto-char (match-beginning 1))
216 ;; If the line ends in a space,
217 ;; don't offer to change it.
218 (not (= (char-after (1- (match-end 1))) ?\ ))
219 (not (string= argument
220 (buffer-substring (point) (match-end 1))))
221 (if (or (not executable-query) no-query-flag
222 (save-window-excursion
223 ;; Make buffer visible before question.
224 (switch-to-buffer (current-buffer))
225 (y-or-n-p (concat "Replace magic number by `"
226 executable-prefix argument "'? "))))
227 (progn
228 (replace-match argument t t nil 1)
229 (message "Magic number changed to `%s'"
230 (concat executable-prefix argument)))))
231 (insert executable-prefix argument ?\n)
232 (message "Magic number changed to `%s'"
233 (concat executable-prefix argument)))
234 ;;; (or insert-flag
235 ;;; (eq executable-insert t)
236 ;;; (set-buffer-modified-p buffer-modified-p))
237 )))
238 interpreter)
239
240
241
242 ;;;###autoload
243 (defun executable-self-display ()
244 "Turn a text file into a self-displaying Un*x command.
245 The magic number of such a command displays all lines but itself."
246 (interactive)
247 (if (eq this-command 'executable-self-display)
248 (setq this-command 'executable-set-magic))
249 (executable-set-magic executable-self-display "+2"))
250
251
252
253 (provide 'executable)
254
255 ;; executable.el ends here