-
Notifications
You must be signed in to change notification settings - Fork 0
/
executable-set-magic.el
166 lines (140 loc) · 5.91 KB
/
executable-set-magic.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
;;; executable-set-magic.el --- set script magic number -*- lexical-binding: t -*-
;;
;; Copyright © 2017,2022 Andrew L. Moore
;; Author: Andrew L. Moore <slewsys@gmail.com>
;; Keywords: editing, languages, script
;; URL: https://github.com/slewsys/emacs-extensions
;; This file is not part of GNU Emacs.
;;; Commentary:
;;
;; `executable-set-magic-mode' is a minor mode that makes a script
;; executable and sets its magic number, if any, based on the current
;; major mode. An alist of major-modes and associated script
;; interpreters is defined in `executable-set-magic-alist'.
;;
;; If variable `executable-prefix-env' is non-nil, then the magic
;; number inserted by function `executable-set-magic' takes the form
;; "#!/usr/bin/env interpreter", otherwise "#!/path/to/interpreter".
;;
;; If variable `executable-exclude-project-files' is non-nil, then
;; files contained in project directories - as defined by the function
;; `executable-project-directory' - are not processed.
;;; Installation:
;;
;; (require 'executable-set-magic)
;;
;; To enable `executable-set-magic-mode' globally:
;;
;; (executable-set-magic-mode)
;;
;;; Code:
(eval-when-compile
(require 'cl-lib))
(defcustom executable-set-magic-alist
'((awk-mode ("awk" . "-f"))
(ed-mode ("ed" . "-f"))
(go-mode ("go" . nil))
(js-mode ("node" . nil))
(julia-mode ("julia" . nil))
(lua-mode ("lua" . nil))
(perl-mode ("perl" . nil))
(php-mode ("php" . nil))
(python-mode ("python3" . nil))
(ruby-mode ("ruby" . nil))
(sed-mode ("sed" . "-f"))
(shell-mode ("bash" . nil))
(tcl-mode ("tclsh" . nil)))
"Alist of major modes and associated script interpreters used
by `executable-set-magic' to produce a magic number for a script."
:tag "Mode-interpreter alist."
:version "26.1"
:group 'executable
:type '(repeat (list symbol (cons string sexp))))
(defcustom executable-exclude-project-files t
"If non-nil, `executable-set-magic-hook' is not applied to files
contained in project directories as identified by the function
`executable-project-directory'."
:tag "Do not apply `executable-set-magic' to project files."
:version "26.1"
:group 'executable
:type '(boolean))
(defcustom executable-project-root-files
'(".git" ".svn" ".hgtags" ".bzr" "CVS" "_darcs" "GRTAGS")
"List of files used by function `executable-project-root-path'
to identify root of project hierarchy."
:tag "Project root files."
:version "26.1"
:group 'executable
:type '(repeat string))
(defvar executable-root-path nil)
(defun executable-project-root-path (project-directory)
"Searches PROJECT-DIRECTORY for one of the files in list
`executable-project-root-files'. If found, returns the pathname,
otherwise nil."
(cl-block project-block
(dolist (root-file executable-project-root-files)
(setq executable-root-path
(concat project-directory root-file))
(if (file-exists-p executable-root-path)
(cl-return-from project-block
(file-name-directory executable-root-path))))
nil))
(defun executable-project-directory (&optional project-directory)
"Searches hierarchy of PROJECT-DIRECTORY for top-most project
root, as defined by function `executable-project-root-path'. If
found, returns the pathname, other nil."
(cl-block root-block
(let* ((canonical-dir
(expand-file-name (file-name-directory (or project-directory
default-directory))))
(top-level-path "")
(separator "/")
(path-components (split-string canonical-dir separator)))
(dolist (directory path-components executable-root-path)
(setq top-level-path (concat top-level-path directory separator))
(setq executable-root-path
(executable-project-root-path top-level-path))
(if executable-root-path
(cl-return-from root-block executable-root-path))))))
(defun executable-set-magic-hook ()
"Calls `executable-set-magic' with arguments returned by look up
in `executable-set-magic-alist' of interpreter associated with
current major mode."
(let ((interpreter (car (alist-get major-mode executable-set-magic-alist))))
(if (and interpreter
(not (and executable-exclude-project-files
(executable-project-directory))))
(executable-set-magic (car interpreter) (cdr interpreter)))))
;;;###autoload
(define-minor-mode executable-set-magic-mode
"Toggles variable `executable-set-magic-mode'.
Interactively with no argument, this command toggles the mode.
With a positive prefix argument, enables the mode. With any other
prefix argument, disables it.
From Lisp (i.e., non-interactively), argument omitted or nil
enables the mode, `toggle' toggles the state.
`executable-set-magic-mode' is a minor mode that makes a script
executable and sets its magic number, if any, based on the current
major mode. An alist of major-modes and associated script
interpreters is defined in `executable-set-magic-alist'.
If variable `executable-prefix-env' is non-nil, then the magic
number inserted by function `executable-set-magic' takes the form
\"#!/usr/bin/env interpreter\", otherwise \"#!/path/to/interpreter\".
If variable `executable-exclude-project-files' is non-nil, then
files contained in project directories - as defined by the function
`executable-project-directory' - are not processed."
;;; No initial value.
:init-value nil
;;; No mode line indicator.
:lighter nil
;;; Minor mode keymap
:keymap nil
;;; Gobal variable `executable-set-magic-mode'.
:global t
;;; Customization group
:group 'executable
(if executable-set-magic-mode
(add-hook 'find-file-hook 'executable-set-magic-hook)
(remove-hook 'find-file-hook 'executable-set-magic-hook)))
(provide 'executable-set-magic)
;;; executable-set-magic.el ends here