diff options
| author | Karl Heuer | 1995-07-07 19:41:57 +0000 |
|---|---|---|
| committer | Karl Heuer | 1995-07-07 19:41:57 +0000 |
| commit | 58a4ff0405043a4c8d0bb0b263968568d1cdfac3 (patch) | |
| tree | bfde8f3f4d51ccad6ab6b1cba01eddcad2c849f6 /lisp/progmodes/executable.el | |
| parent | 3f61a2e7d673bcef843261be9553407e859301dd (diff) | |
| download | emacs-58a4ff0405043a4c8d0bb0b263968568d1cdfac3.tar.gz emacs-58a4ff0405043a4c8d0bb0b263968568d1cdfac3.zip | |
Initial revision
Diffstat (limited to 'lisp/progmodes/executable.el')
| -rw-r--r-- | lisp/progmodes/executable.el | 217 |
1 files changed, 217 insertions, 0 deletions
diff --git a/lisp/progmodes/executable.el b/lisp/progmodes/executable.el new file mode 100644 index 00000000000..cc15ad964f1 --- /dev/null +++ b/lisp/progmodes/executable.el | |||
| @@ -0,0 +1,217 @@ | |||
| 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: | ||
| 24 | |||
| 25 | ;; Provide or modify interpreter magic number for buffer, and make file | ||
| 26 | ;; executable if it isn't. Support code for the likes of sh-, awk-, perl-, | ||
| 27 | ;; tcl- or makefile-mode. | ||
| 28 | |||
| 29 | ;;; Code: | ||
| 30 | |||
| 31 | (defvar executable-insert 'not-modified | ||
| 32 | "*What to do when newly found file has no or wrong magic number: | ||
| 33 | nil do nothing | ||
| 34 | t insert or update magic number | ||
| 35 | other insert or update magic number, but mark as unmodified. | ||
| 36 | When the insertion is marked as unmodified, you can save it with \\[write-file] RET. | ||
| 37 | This variable is used when `executable-set-magic' is called as a function, | ||
| 38 | e.g. when Emacs sets some Un*x interpreter script mode. | ||
| 39 | With \\[executable-set-magic], this is always treated as if it were `t'.") | ||
| 40 | |||
| 41 | |||
| 42 | (defvar executable-query 'function | ||
| 43 | "*If non-`nil', ask user before inserting or changing magic number. | ||
| 44 | When this is `function', only ask when called non-interactively.") | ||
| 45 | |||
| 46 | |||
| 47 | (defvar executable-magicless-file-regexp "/[Mm]akefile$\\|/\\.\\(z?profile\\|bash_profile\\|z?login\\|bash_login\\|z?logout\\|bash_logout\\|.+shrc\\|esrc\\|rcrc\\|[kz]shenv\\)$" | ||
| 48 | "*On files with this kind of name no magic is inserted or changed.") | ||
| 49 | |||
| 50 | |||
| 51 | (defvar executable-prefix "#! " | ||
| 52 | "*Interpreter magic number prefix inserted when there was no magic number.") | ||
| 53 | |||
| 54 | |||
| 55 | |||
| 56 | (defvar executable-chmod 73 | ||
| 57 | "*After saving, if the file is not executable, set this mode. | ||
| 58 | This mode passed to `set-file-modes' is taken absolutely when negative, or | ||
| 59 | relative to the files existing modes. Do nothing if this is nil. | ||
| 60 | Typical values are 73 (+x) or -493 (rwxr-xr-x).") | ||
| 61 | |||
| 62 | |||
| 63 | (defvar executable-command nil) | ||
| 64 | |||
| 65 | |||
| 66 | ;;;###autoload | ||
| 67 | (or (assoc "tail" interpreter-mode-alist) | ||
| 68 | (nconc interpreter-mode-alist | ||
| 69 | '(("tail" . text-mode) | ||
| 70 | ("more" . text-mode) | ||
| 71 | ("less" . text-mode) | ||
| 72 | ("pg" . text-mode)))) | ||
| 73 | |||
| 74 | (defvar executable-self-display "tail" | ||
| 75 | "*Command you use with argument `+2' to make text files self-display. | ||
| 76 | Note that the like of `more' doesn't work too well under Emacs \\[shell].") | ||
| 77 | |||
| 78 | |||
| 79 | (defvar executable-font-lock-keywords | ||
| 80 | '(("\\`#!.*/\\([^ \t\n]+\\)" 1 font-lock-keyword-face t)) | ||
| 81 | "*Rules for highlighting executable scripts' magic number. | ||
| 82 | This can be included in `font-lock-keywords' by modes that call `executable'.") | ||
| 83 | |||
| 84 | |||
| 85 | (defvar executable-error-regexp-alist | ||
| 86 | '(;; /bin/xyz: syntax error at line 14: `(' unexpected | ||
| 87 | ;; /bin/xyz[5]: syntax error at line 8 : ``' unmatched | ||
| 88 | ("^\\(.*[^[/]\\)\\(\\[[0-9]+\\]\\)?: .* error .* line \\([0-9]+\\)" 1 3) | ||
| 89 | ;; /bin/xyz[27]: ehco: not found | ||
| 90 | ("^\\(.*[^/]\\)\\[\\([0-9]+\\)\\]: .*: " 1 2) | ||
| 91 | ;; /bin/xyz: syntax error near unexpected token `)' | ||
| 92 | ;; /bin/xyz: /bin/xyz: line 2: `)' | ||
| 93 | ("^\\(.*[^/]\\): [^0-9\n]+\n\\1: \\1: line \\([0-9]+\\):" 1 2) | ||
| 94 | ;; /usr/bin/awk: syntax error at line 5 of file /bin/xyz | ||
| 95 | (" error .* line \\([0-9]+\\) of file \\(.+\\)$" 2 1) | ||
| 96 | ;; /usr/bin/awk: calling undefined function toto | ||
| 97 | ;; input record number 3, file awktestdata | ||
| 98 | ;; source line 4 of file /bin/xyz | ||
| 99 | ("^[^ ].+\n\\( .+\n\\)* line \\([0-9]+\\) of file \\(.+\\)$" 3 2) | ||
| 100 | ;; makefile:1: *** target pattern contains no `%'. Stop. | ||
| 101 | ("^\\(.+\\):\\([0-9]+\\): " 1 2)) | ||
| 102 | "Alist of regexps used to match script errors. | ||
| 103 | See `compilation-error-regexp-alist'.") | ||
| 104 | |||
| 105 | ;; The C function openp() slightly modified would do the trick fine | ||
| 106 | (defun executable (command) | ||
| 107 | "If COMMAND is an executable in $PATH its full name is returned. Else nil." | ||
| 108 | (let ((list exec-path) | ||
| 109 | path) | ||
| 110 | (while list | ||
| 111 | (setq list (if (and (setq path (expand-file-name command (car list))) | ||
| 112 | (file-executable-p path) | ||
| 113 | (not (file-directory-p path))) | ||
| 114 | nil | ||
| 115 | (setq path nil) | ||
| 116 | (cdr list)))) | ||
| 117 | path)) | ||
| 118 | |||
| 119 | |||
| 120 | (defun executable-chmod () | ||
| 121 | "This gets called after saving a file to assure that it be executable. | ||
| 122 | You can set the absolute or relative mode in variable `executable-chmod' for | ||
| 123 | non-executable files." | ||
| 124 | (and executable-chmod | ||
| 125 | buffer-file-name | ||
| 126 | (or (file-executable-p buffer-file-name) | ||
| 127 | (set-file-modes buffer-file-name | ||
| 128 | (if (< executable-chmod 0) | ||
| 129 | (- executable-chmod) | ||
| 130 | (logior executable-chmod | ||
| 131 | (file-modes buffer-file-name))))))) | ||
| 132 | |||
| 133 | |||
| 134 | (defun executable-interpret (command) | ||
| 135 | "Run script with user-specified args, and collect output in a buffer. | ||
| 136 | While script runs asynchronously, you can use the \\[next-error] command | ||
| 137 | to find the next error." | ||
| 138 | (interactive (list (read-string "Run script: " | ||
| 139 | (or executable-command | ||
| 140 | buffer-file-name)))) | ||
| 141 | (require 'compile) | ||
| 142 | (save-some-buffers (not compilation-ask-about-save)) | ||
| 143 | (make-local-variable 'executable-command) | ||
| 144 | (compile-internal (setq executable-command command) | ||
| 145 | "No more errors." "Interpretation" | ||
| 146 | ;; Give it a simpler regexp to match. | ||
| 147 | nil executable-error-regexp-alist)) | ||
| 148 | |||
| 149 | |||
| 150 | |||
| 151 | ;;;###autoload | ||
| 152 | (defun executable-set-magic (interpreter &optional argument) | ||
| 153 | "Set this buffer's interpreter to INTERPRETER with optional ARGUMENT. | ||
| 154 | The variables `executable-magicless-file-regexp', `executable-prefix', | ||
| 155 | `executable-insert', `executable-query' and `executable-chmod' control | ||
| 156 | when and how magic numbers are inserted or replaced and scripts made | ||
| 157 | executable." | ||
| 158 | (interactive "sName or path of interpreter: \nsArgument for %s: ") | ||
| 159 | (setq interpreter (if (file-name-absolute-p interpreter) | ||
| 160 | interpreter | ||
| 161 | (or (executable interpreter) | ||
| 162 | (error "Cannot find %s." interpreter))) | ||
| 163 | argument (concat interpreter | ||
| 164 | (and argument (string< "" argument) " ") | ||
| 165 | argument)) | ||
| 166 | (or buffer-read-only | ||
| 167 | (if buffer-file-name | ||
| 168 | (string-match executable-magicless-file-regexp | ||
| 169 | buffer-file-name)) | ||
| 170 | (not (or (eq this-command 'executable-set-magic) | ||
| 171 | executable-insert)) | ||
| 172 | (> (point-min) 1) | ||
| 173 | (let ((point (point-marker)) | ||
| 174 | (buffer-modified-p (buffer-modified-p))) | ||
| 175 | (goto-char (point-min)) | ||
| 176 | (make-local-variable 'after-save-hook) | ||
| 177 | (add-hook 'after-save-hook 'executable-chmod) | ||
| 178 | (if (looking-at "#![ \t]*\\(.*\\)$") | ||
| 179 | (and (goto-char (match-beginning 1)) | ||
| 180 | (not (string= argument | ||
| 181 | (buffer-substring (point) (match-end 1)))) | ||
| 182 | (save-window-excursion | ||
| 183 | ;; make buffer visible before question or message | ||
| 184 | (switch-to-buffer (current-buffer)) | ||
| 185 | (if (or (not executable-query) | ||
| 186 | (and (eq executable-query 'function) | ||
| 187 | (eq this-command 'executable-set-magic))) | ||
| 188 | (message "%s Magic number ``%s'' replaced." this-command | ||
| 189 | (buffer-substring (point-min) (match-end 1))) | ||
| 190 | (y-or-n-p (concat "Replace magic number by ``" | ||
| 191 | executable-prefix argument "''? ")))) | ||
| 192 | (not (delete-region (point) (match-end 1))) | ||
| 193 | (insert argument)) | ||
| 194 | (insert executable-prefix argument ?\n)) | ||
| 195 | (or (< (marker-position point) (point)) | ||
| 196 | (goto-char point)) | ||
| 197 | (or (eq this-command 'executable-set-magic)) | ||
| 198 | (eq executable-insert t) | ||
| 199 | (set-buffer-modified-p buffer-modified-p))) | ||
| 200 | interpreter) | ||
| 201 | |||
| 202 | |||
| 203 | |||
| 204 | ;;;###autoload | ||
| 205 | (defun executable-self-display () | ||
| 206 | "Turn a text file into a self-displaying Un*x command. | ||
| 207 | The magic number of such a command displays all lines but itself." | ||
| 208 | (interactive) | ||
| 209 | (if (eq this-command 'executable-self-display) | ||
| 210 | (setq this-command 'executable-set-magic)) | ||
| 211 | (executable-set-magic executable-self-display "+2")) | ||
| 212 | |||
| 213 | |||
| 214 | |||
| 215 | (provide 'executable) | ||
| 216 | |||
| 217 | ;; executable.el ends here | ||