]> code.delx.au - gnu-emacs-elpa/blob - packages/nameless/nameless.el
Merge commit '512b2ace3db9bf64e16f949ed90b78eb86c7fdda'
[gnu-emacs-elpa] / packages / nameless / nameless.el
1 ;;; nameless.el --- Hide package namespace in your emacs-lisp code -*- lexical-binding: t; -*-
2
3 ;; Copyright (C) 2015 Free Software Foundation, Inc.
4
5 ;; Author: Artur Malabarba <emacs@endlessparentheses.com>
6 ;; Keywords: convenience, lisp
7 ;; Version: 0.3
8 ;; Package-Requires: ((emacs "24.4"))
9
10 ;; This program is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
14
15 ;; This program is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;; Usage
26 ;; ─────
27 ;;
28 ;; To use this package add the following configuration to your Emacs init
29 ;; file.
30 ;;
31 ;; ┌────
32 ;; │ (add-hook 'emacs-lisp-mode-hook #'nameless-mode)
33 ;; └────
34 ;;
35 ;; You can configure a string to use instead of `:' by setting the
36 ;; `nameless-prefix', and the name of the face used is `nameless-face'.
37 ;;
38 ;; While the mode is active, the `_' key inserts the package
39 ;; namespace if appropriate.
40
41 ;;; Code:
42 (require 'lisp-mnt)
43
44 (defgroup nameless nil
45 "Customization group for nameless."
46 :group 'emacs)
47
48 (defcustom nameless-prefix ":"
49 "Prefix displayed instead of package namespace."
50 :type 'string)
51
52 (defcustom nameless-global-aliases '(("fl" . "font-lock"))
53 "Alist from aliases to namespaces.
54 This alist is used everywhere. It is designed for namespaces you
55 use commonly. To apply aliases specific to a file, set the
56 `nameless-aliases' variable with `add-file-local-variable'.
57
58 Each element of this list should have the form (ALIAS . NAMESPACE),
59 both strings. For example, if you set this variable to
60 ((\"fl\" . \"font-lock\"))
61 then expressions like `(font-lock-add-keywords nil kwds)' will
62 displayed as `(fl/add-keywords nil kwds)' instead.
63
64 Furthermore typing `fl' followed by `\\[nameless-insert-name]' will
65 automatically insert `font-lock-'."
66 :type '(alist string string))
67
68 (defvar nameless-aliases nil
69 "Alist from namespaces to aliases.
70 Samse syntax as `nameless-global-aliases', but designed to be
71 used as a file-local variable.")
72
73 (defface nameless-face
74 '((t :inherit font-lock-type-face))
75 "Face used on `nameless-prefix'")
76
77 (defcustom nameless-affect-indentation-and-filling 'outside-strings
78 "If non-nil, code is indented and filled according to what you see.
79 If nil, code is indented and filled according to its actual content.
80 If the value is `outside-strings', behave like nil inside strings
81 and behave like t otherwise.
82
83 After changing this variable, you must reenable `nameless-mode'
84 for it to take effect."
85 :type '(choice (const :tag "Always affect indentation" t)
86 (const :tag "Don't affect indentation" nil)
87 (const :tag "Only outside strings" 'outside-strings)))
88
89 \f
90 ;;; Font-locking
91 (defun nameless--make-composition (s)
92 "Return a list that composes S if passed to `compose-region'."
93 (cdr (apply #'append (mapcar (lambda (x) (list '(Br . Bl) x)) s))))
94
95 (defvar nameless-mode)
96 (defun nameless--compose-as (display)
97 "Compose the matched region and return a face spec."
98 (when nameless-mode
99 (let ((compose (save-match-data
100 (and nameless-affect-indentation-and-filling
101 (or (not (eq nameless-affect-indentation-and-filling 'outside-strings))
102 (not (nth 3 (syntax-ppss)))))))
103 (dis (concat display nameless-prefix)))
104 (when compose
105 (compose-region (match-beginning 1)
106 (match-end 1)
107 (nameless--make-composition dis)))
108 `(face nameless-face ,@(unless compose (list 'display dis))))))
109
110 (defvar-local nameless--font-lock-keywords nil)
111
112 (defun nameless--ensure ()
113 (save-excursion
114 (font-lock-fontify-region (point-min) (point-max))))
115
116 (defun nameless--remove-keywords ()
117 "Remove font-lock keywords set by `nameless--add-keywords'."
118 (font-lock-remove-keywords nil nameless--font-lock-keywords)
119 (setq nameless--font-lock-keywords nil)
120 (nameless--ensure))
121
122 (defun nameless--add-keywords (&rest r)
123 "Add font-lock keywords displaying REGEXP as DISPLAY.
124
125 \(fn (regexp . display) [(regexp . display) ...])"
126 (setq-local font-lock-extra-managed-props
127 `(composition display ,@font-lock-extra-managed-props))
128 (let ((kws (mapcar (lambda (x) `(,(nameless--name-regexp (cdr x)) 1 (nameless--compose-as ,(car x)) prepend)) r)))
129 (setq nameless--font-lock-keywords kws)
130 (font-lock-add-keywords nil kws t))
131 (nameless--ensure))
132
133 \f
134 ;;; Name and regexp
135 (defvar-local nameless-current-name nil)
136
137 (defun nameless--in-arglist-p ()
138 "Is point inside an arglist?"
139 (save-excursion
140 (ignore-errors
141 (backward-up-list)
142 (or (progn (forward-sexp -1)
143 (looking-at-p "[a-z-]lambda\\_>"))
144 (progn (forward-sexp -1)
145 (looking-at-p "\\(cl-\\)?def\\(un\\|macro\\|inline\\)\\*?\\_>"))))))
146
147 (defun nameless-insert-name (&optional noerror)
148 "Insert `nameless-current-name' or the alias at point.
149 If point is immediately after an alias configured in
150 `nameless-aliases' or `nameless-global-aliases', replace it with
151 the full name for that alias.
152 Otherwise, insert `nameless-current-name'.
153
154 If NOERROR is nil, signal an error if the alias at point is not
155 configured, or if `nameless-current-name' is nil."
156 (interactive)
157 (if (string-match (rx (or (syntax symbol)
158 (syntax word)))
159 (string (char-before)))
160 (let* ((r (point))
161 (l (save-excursion
162 (forward-sexp -1)
163 (skip-chars-forward "^[:alnum:]")
164 (point)))
165 (alias (buffer-substring l r))
166 (full-name (when alias
167 (cdr (or (assoc alias nameless-aliases)
168 (assoc alias nameless-global-aliases))))))
169 (if full-name
170 (progn (delete-region l r)
171 (insert full-name "-"))
172 (unless noerror
173 (user-error "No name for alias `%s', see `nameless-aliases'" alias))))
174 (if nameless-current-name
175 (progn (insert nameless-current-name "-")
176 t)
177 (unless noerror
178 (user-error "No name for current buffer, see `nameless-current-name'")))))
179
180 (defun nameless-insert-name-or-self-insert (&optional self-insert)
181 "Insert the name of current package, with a hyphen."
182 (interactive "P")
183 (if (or self-insert
184 (not nameless-current-name)
185 (eq (char-before) ?\\)
186 (nameless--in-arglist-p))
187 (call-interactively #'self-insert-command)
188 (or (nameless-insert-name 'noerror)
189 (call-interactively #'self-insert-command))))
190
191 (defun nameless--name-regexp (name)
192 "Return a regexp of the current name."
193 (concat "\\_<@?\\(" (regexp-quote name) "-\\)\\(\\s_\\|\\sw\\)"))
194
195 (defun nameless--filter-string (s)
196 "Remove from string S any disply or composition properties.
197 Return S."
198 (let ((length (length s)))
199 (remove-text-properties 0 length '(composition nil display nil) s)
200 s))
201
202 \f
203 ;;; Minor mode
204 ;;;###autoload
205 (define-minor-mode nameless-mode
206 nil nil " :" '(("_" . nameless-insert-name-or-self-insert))
207 (if nameless-mode
208 (if (or nameless-current-name
209 (ignore-errors (string-match "\\.el\\'" (lm-get-package-name))))
210 (progn
211 (unless nameless-current-name
212 (setq nameless-current-name (replace-regexp-in-string "\\.[^.]*\\'" "" (lm-get-package-name))))
213 (add-function :filter-return (local 'filter-buffer-substring-function)
214 #'nameless--filter-string)
215 (apply #'nameless--add-keywords
216 `((nil . ,nameless-current-name)
217 ,@nameless-global-aliases
218 ,@nameless-aliases)))
219 (nameless-mode -1))
220 (remove-function (local 'filter-buffer-substring-function)
221 #'nameless--filter-string)
222 (setq nameless-current-name nil)
223 (nameless--remove-keywords)))
224
225 (provide 'nameless)
226 ;;; nameless.el ends here