Mercurial > emacs
annotate lisp/progmodes/executable.el @ 14024:8a10a3485eef
(interpreter-mode-alist): Modification deleted.
| author | Richard M. Stallman <rms@gnu.org> |
|---|---|
| date | Fri, 05 Jan 1996 02:25:26 +0000 |
| parents | bc084caba958 |
| children | 787061ad42ba |
| rev | line source |
|---|---|
| 12504 | 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: | |
| 12818 | 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 | |
| 12817 | 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 | |
| 12818 | 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. | |
|
12812
39e721f1681f
(interpreter-mode-alist): Delete autoload cookie.
Richard M. Stallman <rms@gnu.org>
parents:
12504
diff
changeset
|
36 |
| 12504 | 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 (defvar executable-self-display "tail" | |
| 74 "*Command you use with argument `+2' to make text files self-display. | |
| 75 Note that the like of `more' doesn't work too well under Emacs \\[shell].") | |
| 76 | |
| 77 | |
| 78 (defvar executable-font-lock-keywords | |
| 79 '(("\\`#!.*/\\([^ \t\n]+\\)" 1 font-lock-keyword-face t)) | |
| 80 "*Rules for highlighting executable scripts' magic number. | |
| 81 This can be included in `font-lock-keywords' by modes that call `executable'.") | |
| 82 | |
| 83 | |
| 84 (defvar executable-error-regexp-alist | |
| 85 '(;; /bin/xyz: syntax error at line 14: `(' unexpected | |
| 86 ;; /bin/xyz[5]: syntax error at line 8 : ``' unmatched | |
| 87 ("^\\(.*[^[/]\\)\\(\\[[0-9]+\\]\\)?: .* error .* line \\([0-9]+\\)" 1 3) | |
| 88 ;; /bin/xyz[27]: ehco: not found | |
| 89 ("^\\(.*[^/]\\)\\[\\([0-9]+\\)\\]: .*: " 1 2) | |
| 90 ;; /bin/xyz: syntax error near unexpected token `)' | |
| 91 ;; /bin/xyz: /bin/xyz: line 2: `)' | |
| 92 ("^\\(.*[^/]\\): [^0-9\n]+\n\\1: \\1: line \\([0-9]+\\):" 1 2) | |
| 93 ;; /usr/bin/awk: syntax error at line 5 of file /bin/xyz | |
| 94 (" error .* line \\([0-9]+\\) of file \\(.+\\)$" 2 1) | |
| 95 ;; /usr/bin/awk: calling undefined function toto | |
| 96 ;; input record number 3, file awktestdata | |
| 97 ;; source line 4 of file /bin/xyz | |
| 98 ("^[^ ].+\n\\( .+\n\\)* line \\([0-9]+\\) of file \\(.+\\)$" 3 2) | |
| 99 ;; makefile:1: *** target pattern contains no `%'. Stop. | |
| 100 ("^\\(.+\\):\\([0-9]+\\): " 1 2)) | |
| 101 "Alist of regexps used to match script errors. | |
| 102 See `compilation-error-regexp-alist'.") | |
| 103 | |
| 104 ;; The C function openp() slightly modified would do the trick fine | |
| 105 (defun executable (command) | |
| 106 "If COMMAND is an executable in $PATH its full name is returned. Else nil." | |
| 107 (let ((list exec-path) | |
| 108 path) | |
| 109 (while list | |
| 110 (setq list (if (and (setq path (expand-file-name command (car list))) | |
| 111 (file-executable-p path) | |
| 112 (not (file-directory-p path))) | |
| 113 nil | |
| 114 (setq path nil) | |
| 115 (cdr list)))) | |
| 116 path)) | |
| 117 | |
| 118 | |
| 119 (defun executable-chmod () | |
| 120 "This gets called after saving a file to assure that it be executable. | |
| 121 You can set the absolute or relative mode in variable `executable-chmod' for | |
| 122 non-executable files." | |
| 123 (and executable-chmod | |
| 124 buffer-file-name | |
| 125 (or (file-executable-p buffer-file-name) | |
| 126 (set-file-modes buffer-file-name | |
| 127 (if (< executable-chmod 0) | |
| 128 (- executable-chmod) | |
| 129 (logior executable-chmod | |
| 130 (file-modes buffer-file-name))))))) | |
| 131 | |
| 132 | |
| 133 (defun executable-interpret (command) | |
| 134 "Run script with user-specified args, and collect output in a buffer. | |
| 135 While script runs asynchronously, you can use the \\[next-error] command | |
| 136 to find the next error." | |
| 137 (interactive (list (read-string "Run script: " | |
| 138 (or executable-command | |
| 139 buffer-file-name)))) | |
| 140 (require 'compile) | |
| 141 (save-some-buffers (not compilation-ask-about-save)) | |
| 142 (make-local-variable 'executable-command) | |
| 143 (compile-internal (setq executable-command command) | |
| 144 "No more errors." "Interpretation" | |
| 145 ;; Give it a simpler regexp to match. | |
| 146 nil executable-error-regexp-alist)) | |
| 147 | |
| 148 | |
| 149 | |
| 150 ;;;###autoload | |
| 151 (defun executable-set-magic (interpreter &optional argument) | |
| 152 "Set this buffer's interpreter to INTERPRETER with optional ARGUMENT. | |
| 153 The variables `executable-magicless-file-regexp', `executable-prefix', | |
| 154 `executable-insert', `executable-query' and `executable-chmod' control | |
| 155 when and how magic numbers are inserted or replaced and scripts made | |
| 156 executable." | |
| 157 (interactive "sName or path of interpreter: \nsArgument for %s: ") | |
| 158 (setq interpreter (if (file-name-absolute-p interpreter) | |
| 159 interpreter | |
| 160 (or (executable interpreter) | |
| 161 (error "Cannot find %s." interpreter))) | |
| 162 argument (concat interpreter | |
| 163 (and argument (string< "" argument) " ") | |
| 164 argument)) | |
| 165 (or buffer-read-only | |
| 166 (if buffer-file-name | |
| 167 (string-match executable-magicless-file-regexp | |
| 168 buffer-file-name)) | |
| 169 (not (or (eq this-command 'executable-set-magic) | |
| 170 executable-insert)) | |
| 171 (> (point-min) 1) | |
| 172 (let ((point (point-marker)) | |
| 173 (buffer-modified-p (buffer-modified-p))) | |
| 174 (goto-char (point-min)) | |
|
12962
bc084caba958
(executable-set-magic): Use make-local-hook for after-save-hook.
Richard M. Stallman <rms@gnu.org>
parents:
12818
diff
changeset
|
175 (make-local-hook 'after-save-hook) |
|
bc084caba958
(executable-set-magic): Use make-local-hook for after-save-hook.
Richard M. Stallman <rms@gnu.org>
parents:
12818
diff
changeset
|
176 (add-hook 'after-save-hook 'executable-chmod nil t) |
| 12504 | 177 (if (looking-at "#![ \t]*\\(.*\\)$") |
| 178 (and (goto-char (match-beginning 1)) | |
| 179 (not (string= argument | |
| 180 (buffer-substring (point) (match-end 1)))) | |
| 181 (save-window-excursion | |
| 182 ;; make buffer visible before question or message | |
| 183 (switch-to-buffer (current-buffer)) | |
| 184 (if (or (not executable-query) | |
| 185 (and (eq executable-query 'function) | |
| 186 (eq this-command 'executable-set-magic))) | |
| 187 (message "%s Magic number ``%s'' replaced." this-command | |
| 188 (buffer-substring (point-min) (match-end 1))) | |
| 189 (y-or-n-p (concat "Replace magic number by ``" | |
| 190 executable-prefix argument "''? ")))) | |
| 191 (not (delete-region (point) (match-end 1))) | |
| 192 (insert argument)) | |
| 193 (insert executable-prefix argument ?\n)) | |
| 194 (or (< (marker-position point) (point)) | |
| 195 (goto-char point)) | |
| 196 (or (eq this-command 'executable-set-magic)) | |
| 197 (eq executable-insert t) | |
| 198 (set-buffer-modified-p buffer-modified-p))) | |
| 199 interpreter) | |
| 200 | |
| 201 | |
| 202 | |
| 203 ;;;###autoload | |
| 204 (defun executable-self-display () | |
| 205 "Turn a text file into a self-displaying Un*x command. | |
| 206 The magic number of such a command displays all lines but itself." | |
| 207 (interactive) | |
| 208 (if (eq this-command 'executable-self-display) | |
| 209 (setq this-command 'executable-set-magic)) | |
| 210 (executable-set-magic executable-self-display "+2")) | |
| 211 | |
| 212 | |
| 213 | |
| 214 (provide 'executable) | |
| 215 | |
| 216 ;; executable.el ends here |
