all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Re: executable-set-magic update
@ 2017-07-24 18:22 Andrew L. Moore
  2017-07-24 22:41 ` Andrew L. Moore
  0 siblings, 1 reply; 7+ messages in thread
From: Andrew L. Moore @ 2017-07-24 18:22 UTC (permalink / raw)
  To: monnier; +Cc: emacs-devel

>> 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)))
>>   )

> Stefan Monnier wrote:
> Doesn't this add a #! to every file using modes like
> js/ruby/awk/perl/python/...?
> 
> In multi-file programs, only the main file needs a "#!", so I don't
> understand why adding #! to all files would make sense.

Stefan,
Sorry I missed your comment.  Yeah, script libraries don’t use magic numbers, so
I updated the minor mode with a variable that allows skipping files in “project”
directories.  And since the minor mode leverages executable-set-magic, it's been
renamed `executable-set-magic-mode’:

 https://github.com/slewsys/emacs-extensions/blob/master/executable-set-magic.el

The current implementation searches for a “project root” in the file path
by brute force. Hopefully that can be updated with something more elegant.

In the mean time, function `executable-set-magic' only seems to be leveraged
by lisp/progmodes/sh-script.el for shell scripts.  So an alternative to
extending it might be removing it, e.g., to an external library.
-AM





^ permalink raw reply	[flat|nested] 7+ messages in thread
* executable-set-magic update
@ 2017-06-09 21:31 Andrew L. Moore
  2017-06-10  7:17 ` Eli Zaretskii
  2017-06-10 12:26 ` Stefan Monnier
  0 siblings, 2 replies; 7+ messages in thread
From: Andrew L. Moore @ 2017-06-09 21:31 UTC (permalink / raw)
  To: emacs-devel

[-- 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)
 
 

^ permalink raw reply related	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2017-07-24 22:41 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-07-24 18:22 executable-set-magic update Andrew L. Moore
2017-07-24 22:41 ` Andrew L. Moore
  -- strict thread matches above, loose matches on Subject: below --
2017-06-09 21:31 Andrew L. Moore
2017-06-10  7:17 ` Eli Zaretskii
2017-06-10 19:31   ` Andrew L. Moore
2017-07-22  7:36     ` Eli Zaretskii
2017-06-10 12:26 ` Stefan Monnier

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.