]> code.delx.au - gnu-emacs/blobdiff - lisp/progmodes/executable.el
Arch-tags shouldn't be outline headers.
[gnu-emacs] / lisp / progmodes / executable.el
index cc15ad964f144fff5e8bead19c30021c63eb7e01..a5d401a5f5e8ec969c1c633780a865a60246c63a 100644 (file)
@@ -1,7 +1,8 @@
-;;; executable.el --- base functionality for executable interpreter scripts
-;; Copyright (C) 1994, 1995 by Free Software Foundation, Inc.
+;;; executable.el --- base functionality for executable interpreter scripts -*- byte-compile-dynamic: t -*-
 
-;; Author: Daniel.Pfeiffer@Informatik.START.dbp.de, fax (+49 69) 7588-2389
+;; Copyright (C) 1994, 1995, 1996, 2000, 2003, 2004 by Free Software Foundation, Inc.
+
+;; Author: Daniel Pfeiffer <occitan@esperanto.org>
 ;; Keywords: languages, unix
 
 ;; This file is part of GNU Emacs.
 ;; GNU General Public License for more details.
 
 ;; You should have received a copy of the GNU General Public License
-;; along with GNU Emacs; see the file COPYING.  If not, write to
-;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
+;; along with GNU Emacs; see the file COPYING.  If not, write to the
+;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+;; Boston, MA 02111-1307, USA.
 
 ;;; Commentary:
 
-;; Provide or modify interpreter magic number for buffer, and make file
-;; executable if it isn't.  Support code for the likes of sh-, awk-, perl-,
-;; tcl- or makefile-mode.
+;; executable.el is used by certain major modes to insert a suitable
+;; #! line at the beginning of the file, if the file does not already
+;; have one.
+
+;; Unless it has a magic number, a Unix file with executable mode is passed to
+;; a new instance of the running shell (or to a Bourne shell if a csh is
+;; running and the file starts with `:').  Only a shell can start such a file,
+;; exec() cannot, which is why it is important to have a magic number in every
+;; executable script.  Such a magic number is made up by the characters `#!'
+;; the filename of an interpreter (in COFF, ELF or somesuch format) and one
+;; optional argument.
+
+;; This library is for certain major modes like sh-, awk-, perl-, tcl- or
+;; makefile-mode to insert or update a suitable #! line at the beginning of
+;; the file, if the file does not already have one and the file is not a
+;; default file of that interpreter (like .profile or makefile).  It also
+;; makes the file executable if it wasn't, as soon as it's saved.
+
+;; It also allows debugging scripts, with an adaptation of compile, as far
+;; as interpreters give out meaningful error messages.
+
+;; Modes that use this should nconc `executable-map' to the end of their own
+;; keymap and `executable-font-lock-keywords' to the end of their own font
+;; lock keywords.  Their mode-setting commands should call
+;; `executable-set-magic'.
 
 ;;; Code:
 
-(defvar executable-insert 'not-modified
-  "*What to do when newly found file has no or wrong magic number:
-       nil     do nothing
-       t       insert or update magic number
-       other   insert or update magic number, but mark as unmodified.
-When the insertion is marked as unmodified, you can save it with  \\[write-file] RET.
-This variable is used when `executable-set-magic' is called as a function,
-e.g. when Emacs sets some Un*x interpreter script mode.
-With \\[executable-set-magic], this is always treated as if it were `t'.")
+(defgroup executable nil
+  "Base functionality for executable interpreter scripts"
+  :group 'processes)
 
+;; This used to default to `other', but that doesn't seem to have any
+;; significance.  fx 2000-02-11.
+(defcustom executable-insert t         ; 'other
+  "*Non-nil means offer to add a magic number to a file.
+This takes effect when you switch to certain major modes,
+including Shell-script mode (`sh-mode').
+When you type \\[executable-set-magic], it always offers to add or
+update the magic number."
+;;;   :type '(choice (const :tag "off" nil)
+;;;             (const :tag "on" t)
+;;;             symbol)
+  :type 'boolean
+  :group 'executable)
 
-(defvar executable-query 'function
-  "*If non-`nil', ask user before inserting or changing magic number.
-When this is `function', only ask when called non-interactively.")
 
+(defcustom executable-query 'function
+  "*If non-nil, ask user before changing an existing magic number.
+When this is `function', only ask when called non-interactively."
+  :type '(choice (const :tag "Don't Ask" nil)
+                (const :tag "Ask when non-interactive" function)
+                (other :tag "Ask" t))
+  :group 'executable)
 
-(defvar executable-magicless-file-regexp "/[Mm]akefile$\\|/\\.\\(z?profile\\|bash_profile\\|z?login\\|bash_login\\|z?logout\\|bash_logout\\|.+shrc\\|esrc\\|rcrc\\|[kz]shenv\\)$"
-  "*On files with this kind of name no magic is inserted or changed.")
 
+(defcustom executable-magicless-file-regexp "/[Mm]akefile$\\|/\\.\\(z?profile\\|bash_profile\\|z?login\\|bash_login\\|z?logout\\|bash_logout\\|.+shrc\\|esrc\\|rcrc\\|[kz]shenv\\)$"
+  "*On files with this kind of name no magic is inserted or changed."
+  :type 'regexp
+  :group 'executable)
 
-(defvar executable-prefix "#! "
-  "*Interpreter magic number prefix inserted when there was no magic number.")
 
+(defcustom executable-prefix "#! "
+  "*Interpreter magic number prefix inserted when there was no magic number."
+  :type 'string
+  :group 'executable)
 
 
-(defvar executable-chmod 73
+(defcustom executable-chmod 73
   "*After saving, if the file is not executable, set this mode.
 This mode passed to `set-file-modes' is taken absolutely when negative, or
 relative to the files existing modes.  Do nothing if this is nil.
-Typical values are 73 (+x) or -493 (rwxr-xr-x).")
+Typical values are 73 (+x) or -493 (rwxr-xr-x)."
+  :type '(choice integer
+                (const nil))
+  :group 'executable)
 
 
 (defvar executable-command nil)
 
-
-;;;###autoload
-(or (assoc "tail" interpreter-mode-alist)
-    (nconc interpreter-mode-alist
-          '(("tail" . text-mode)
-            ("more" . text-mode)
-            ("less" . text-mode)
-            ("pg" . text-mode))))
-
-(defvar executable-self-display "tail"
+(defcustom executable-self-display "tail"
   "*Command you use with argument `+2' to make text files self-display.
-Note that the like of `more' doesn't work too well under Emacs  \\[shell].")
+Note that the like of `more' doesn't work too well under Emacs \\[shell]."
+  :type 'string
+  :group 'executable)
 
 
 (defvar executable-font-lock-keywords
@@ -102,20 +137,53 @@ This can be included in `font-lock-keywords' by modes that call `executable'.")
   "Alist of regexps used to match script errors.
 See `compilation-error-regexp-alist'.")
 
-;; The C function openp() slightly modified would do the trick fine
-(defun executable (command)
-  "If COMMAND is an executable in $PATH its full name is returned.  Else nil."
+;; The C function openp slightly modified would do the trick fine
+(defvaralias 'executable-binary-suffixes 'exec-suffixes)
+
+;;;###autoload
+(defun executable-command-find-posix-p (&optional program)
+  "Check if PROGRAM handles arguments Posix-style.
+If PROGRAM is non-nil, use that instead of \"find\"."
+  ;;  Pick file to search from location we know
+  (let* ((dir (file-truename data-directory))
+         (file (car (directory-files dir nil "^[^.]"))))
+    (with-temp-buffer
+      (call-process (or program "find")
+                    nil
+                    (current-buffer)
+                    nil
+                    dir
+                    "-name"
+                    file
+                    "-maxdepth"
+                    "1")
+        (goto-char (point-min))
+        (if (search-forward file nil t)
+            t))))
+
+;;;###autoload
+(defun executable-find (command)
+  "Search for COMMAND in `exec-path' and return the absolute file name.
+Return nil if COMMAND is not found anywhere in `exec-path'."
   (let ((list exec-path)
-       path)
+       file)
     (while list
-      (setq list (if (and (setq path (expand-file-name command (car list)))
-                         (file-executable-p path)
-                         (not (file-directory-p path)))
-                    nil
-                  (setq path nil)
-                  (cdr list))))
-    path))
-
+      (setq list
+           (if (and (setq file (expand-file-name command (car list)))
+                    (let ((suffixes exec-suffixes)
+                          candidate)
+                      (while suffixes
+                        (setq candidate (concat file (car suffixes)))
+                        (if (and (file-executable-p candidate)
+                                 (not (file-directory-p candidate)))
+                            (setq suffixes nil)
+                          (setq suffixes (cdr suffixes))
+                          (setq candidate nil)))
+                      (setq file candidate)))
+               nil
+             (setq file nil)
+             (cdr list))))
+    file))
 
 (defun executable-chmod ()
   "This gets called after saving a file to assure that it be executable.
@@ -149,55 +217,60 @@ to find the next error."
 
 
 ;;;###autoload
-(defun executable-set-magic (interpreter &optional argument)
+(defun executable-set-magic (interpreter &optional argument
+                                        no-query-flag insert-flag)
   "Set this buffer's interpreter to INTERPRETER with optional ARGUMENT.
 The variables `executable-magicless-file-regexp', `executable-prefix',
 `executable-insert', `executable-query' and `executable-chmod' control
 when and how magic numbers are inserted or replaced and scripts made
 executable."
-  (interactive "sName or path of interpreter: \nsArgument for %s: ")
+  (interactive
+   (let* ((name (read-string "Name or file name of interpreter: "))
+         (arg (read-string (format "Argument for %s: " name))))
+     (list name arg (eq executable-query 'function) t)))
+
   (setq interpreter (if (file-name-absolute-p interpreter)
                        interpreter
-                     (or (executable interpreter)
-                         (error "Cannot find %s." interpreter)))
-       argument (concat interpreter
+                     (or (executable-find interpreter)
+                         (error "Interpreter %s not recognized"
+                                interpreter))))
+
+  (setq argument (concat (if (string-match "\\`/:" interpreter)
+                            (replace-match "" nil nil interpreter)
+                          interpreter)
                         (and argument (string< "" argument) " ")
                         argument))
+
   (or buffer-read-only
       (if buffer-file-name
          (string-match executable-magicless-file-regexp
                        buffer-file-name))
-      (not (or (eq this-command 'executable-set-magic)
-              executable-insert))
+      (not (or insert-flag executable-insert))
       (> (point-min) 1)
-      (let ((point (point-marker))
-           (buffer-modified-p (buffer-modified-p)))
+      (save-excursion
        (goto-char (point-min))
-       (make-local-variable 'after-save-hook)
-       (add-hook 'after-save-hook 'executable-chmod)
+       (add-hook 'after-save-hook 'executable-chmod nil t)
        (if (looking-at "#![ \t]*\\(.*\\)$")
            (and (goto-char (match-beginning 1))
+                ;; If the line ends in a space,
+                ;; don't offer to change it.
+                (not (= (char-after (1- (match-end 1))) ?\ ))
                 (not (string= argument
                               (buffer-substring (point) (match-end 1))))
-                (save-window-excursion
-                  ;; make buffer visible before question or message
-                  (switch-to-buffer (current-buffer))
-                  (if (or (not executable-query)
-                          (and (eq executable-query 'function)
-                               (eq this-command 'executable-set-magic)))
-                      (message "%s Magic number ``%s'' replaced." this-command
-                               (buffer-substring (point-min) (match-end 1)))
-                    (y-or-n-p (concat "Replace magic number by ``"
-                                      executable-prefix argument "''? "))))
-                (not (delete-region (point) (match-end 1)))
-                (insert argument))
-         (insert executable-prefix argument ?\n))
-       (or (< (marker-position point) (point))
-           (goto-char point))
-       (or (eq this-command 'executable-set-magic))
-           (eq executable-insert t)
-           (set-buffer-modified-p buffer-modified-p)))
-  interpreter)
+                (if (or (not executable-query) no-query-flag
+                        (save-window-excursion
+                          ;; Make buffer visible before question.
+                          (switch-to-buffer (current-buffer))
+                          (y-or-n-p (concat "Replace magic number by `"
+                                            executable-prefix argument "'? "))))
+                    (progn
+                      (replace-match argument t t nil 1)
+                      (message "Magic number changed to `%s'"
+                               (concat executable-prefix argument)))))
+         (insert executable-prefix argument ?\n)
+         (message "Magic number changed to `%s'"
+                  (concat executable-prefix argument)))))
+    interpreter)
 
 
 
@@ -210,8 +283,23 @@ The magic number of such a command displays all lines but itself."
       (setq this-command 'executable-set-magic))
   (executable-set-magic executable-self-display "+2"))
 
-
+;;;###autoload
+(defun executable-make-buffer-file-executable-if-script-p ()
+  "Make file executable according to umask if not already executable.
+If file already has any execute bits set at all, do not change existing
+file modes."
+  (and (>= (buffer-size) 2)
+       (save-restriction
+        (widen)
+        (string= "#!" (buffer-substring (point-min) (+ 2 (point-min)))))
+       (let* ((current-mode (file-modes (buffer-file-name)))
+              (add-mode (logand ?\111 (default-file-modes))))
+         (or (/= (logand ?\111 current-mode) 0)
+             (zerop add-mode)
+             (set-file-modes (buffer-file-name)
+                             (logior current-mode add-mode))))))
 
 (provide 'executable)
 
-;; executable.el ends here
+;;; arch-tag: 58458d1c-d9db-45ec-942b-8bbb1d5e319d
+;;; executable.el ends here