all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: "Andrew L. Moore" <slewsys@gmail.com>
To: emacs-devel@gnu.org
Subject: executable-set-magic update
Date: Fri, 9 Jun 2017 17:31:30 -0400	[thread overview]
Message-ID: <AB65B7E3-CCF6-405B-B46A-26B269132CAF@gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 940 bytes --]

lisp/progmodes/executable.el does not appear to support magic numbers of the form `#/usr/bin/env interpreter’.  One way to extend support is via the attached diff which merely adds a new variable `executable-interpreter-path-absolute’.  Set the new variable to nil and variable `executable-prefix’ to “#!/usr/bin/env “.

It would also be nice if `executable-set-magic’ were supported beyond `sh-set-shell’ (in lisp/progmodes/sh-script.el), so I implemented a minor mode that adds the following hook to find-file:

(defun script-set-magic ()
  "Look up interpreter associated with current major mode in
`script-set-magic-alist' and call `executable-set-magic'."
  (let ((interpreter (alist-get major-mode script-set-magic-alist)))
    (if interpreter (executable-set-magic interpreter)))
  )

The minor mode file is available as: <https://github.com/slewsys/emacs-extensions/blob/master/script-set-magic.el>
-AM


[-- Attachment #2: lisp_progmodes_executable.el.diff --]
[-- Type: application/octet-stream, Size: 2707 bytes --]

diff --git a/lisp/progmodes/executable.el b/lisp/progmodes/executable.el
index da148bd39a..9464370a91 100644
--- a/lisp/progmodes/executable.el
+++ b/lisp/progmodes/executable.el
@@ -90,6 +90,12 @@ executable-prefix
   :type 'string
   :group 'executable)
 
+(defcustom executable-interpreter-path-absolute t
+  "If non-nil, `executable-set-magic' uses the interpreter's
+absolute path. Otherwise, it's basename is used."
+  :version "26.0"
+  :type 'boolean
+  :group 'executable)
 
 (defcustom executable-chmod 73
   "After saving, if the file is not executable, set this mode.
@@ -220,6 +226,9 @@ executable-set-magic
                          (and argument (string< "" argument) " ")
                          argument))
 
+  (if (not executable-interpreter-path-absolute)
+      (setq argument (file-name-nondirectory argument)))
+
   (or buffer-read-only
       (if buffer-file-name
           (string-match executable-magicless-file-regexp
@@ -229,27 +238,26 @@ executable-set-magic
       (save-excursion
         (goto-char (point-min))
         (add-hook 'after-save-hook 'executable-chmod nil t)
+        (let ((new-magic (concat (substring executable-prefix 2) argument)))
           (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))) ?\s))
-		 (not (string= argument
+                   (not (string= new-magic
                                  (buffer-substring (point) (match-end 1))))
                    (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 (format-message
-				      "Replace magic number by `%s%s'? "
-				      executable-prefix argument))))
+                                        "Replace magic number by `#!%s'? "
+                                        new-magic))))
                        (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)))))
+                         (replace-match new-magic t t nil 1)
+                         (message "Magic number changed to `#!%s'" new-magic))))
+            (insert "#!" new-magic ?\n)
+            (message "Magic number changed to `#!%s'" new-magic)))))
   interpreter)
 
 

             reply	other threads:[~2017-06-09 21:31 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-06-09 21:31 Andrew L. Moore [this message]
2017-06-10  7:17 ` executable-set-magic update Eli Zaretskii
2017-06-10 19:31   ` Andrew L. Moore
2017-07-22  7:36     ` Eli Zaretskii
2017-06-10 12:26 ` Stefan Monnier
  -- strict thread matches above, loose matches on Subject: below --
2017-07-24 18:22 Andrew L. Moore
2017-07-24 22:41 ` Andrew L. Moore

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=AB65B7E3-CCF6-405B-B46A-26B269132CAF@gmail.com \
    --to=slewsys@gmail.com \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.