123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285 |
- (defgroup executable nil
- "Base functionality for executable interpreter scripts."
- :group 'processes)
- (defcustom executable-insert t
- "*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 'boolean
- :group 'executable)
- (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)
- (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)
- (defcustom executable-prefix "#! "
- "*Interpreter magic number prefix inserted when there was no magic number."
- :type 'string
- :group 'executable)
- (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)."
- :type '(choice integer
- (const nil))
- :group 'executable)
- (defvar executable-command nil)
- (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]."
- :type 'string
- :group 'executable)
- (defvar executable-font-lock-keywords
- '(("\\`#!.*/\\([^ \t\n]+\\)" 1 font-lock-keyword-face t))
- "*Rules for highlighting executable scripts' magic number.
- This can be included in `font-lock-keywords' by modes that call `executable'.")
- (defvar executable-error-regexp-alist
- '(
-
- ("^\\(.*[^[/]\\)\\(\\[[0-9]+\\]\\)?: .* error .* line \\([0-9]+\\)" 1 3)
-
- ("^\\(.*[^/]\\)\\[\\([0-9]+\\)\\]: .*: " 1 2)
-
-
- ("^\\(.*[^/]\\): [^0-9\n]+\n\\1: \\1: line \\([0-9]+\\):" 1 2)
-
- (" error .* line \\([0-9]+\\) of file \\(.+\\)$" 2 1)
-
-
-
- ("^[^ ].+\n\\( .+\n\\)* line \\([0-9]+\\) of file \\(.+\\)$" 3 2)
-
- ("^\\(.+\\):\\([0-9]+\\): " 1 2))
- "Alist of regexps used to match script errors.
- See `compilation-error-regexp-alist'.")
- (defvaralias 'executable-binary-suffixes 'exec-suffixes)
- (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\"."
-
- (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))))
- (defun executable-chmod ()
- "This gets called after saving a file to assure that it be executable.
- You can set the absolute or relative mode in variable `executable-chmod' for
- non-executable files."
- (and executable-chmod
- buffer-file-name
- (or (file-executable-p buffer-file-name)
- (set-file-modes buffer-file-name
- (if (< executable-chmod 0)
- (- executable-chmod)
- (logior executable-chmod
- (file-modes buffer-file-name)))))))
- (defvar compilation-error-regexp-alist)
- (defun executable-interpret (command)
- "Run script with user-specified args, and collect output in a buffer.
- While script runs asynchronously, you can use the \\[next-error]
- command to find the next error. The buffer is also in `comint-mode' and
- `compilation-shell-minor-mode', so that you can answer any prompts."
- (interactive (list (read-string "Run script: "
- (or executable-command
- buffer-file-name))))
- (require 'compile)
- (save-some-buffers (not compilation-ask-about-save))
- (set (make-local-variable 'executable-command) command)
- (let ((compilation-error-regexp-alist executable-error-regexp-alist))
- (compilation-start command t (lambda (_x) "*interpretation*"))))
- (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
- (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-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 insert-flag executable-insert))
- (> (point-min) 1)
- (save-excursion
- (goto-char (point-min))
- (add-hook 'after-save-hook 'executable-chmod nil t)
- (if (looking-at "#![ \t]*\\(.*\\)$")
- (and (goto-char (match-beginning 1))
-
-
- (not (= (char-after (1- (match-end 1))) ?\s))
- (not (string= argument
- (buffer-substring (point) (match-end 1))))
- (if (or (not executable-query) no-query-flag
- (save-window-excursion
-
- (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)
- (defun executable-self-display ()
- "Turn a text file into a self-displaying Un*x command.
- The magic number of such a command displays all lines but itself."
- (interactive)
- (if (eq this-command 'executable-self-display)
- (setq this-command 'executable-set-magic))
- (executable-set-magic executable-self-display "+2"))
- (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)))))
- (condition-case nil
- (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))))
-
-
- (error (message "Unable to make file executable")))))
- (provide 'executable)
|