1 ;;; nameless.el --- Hide package namespace in your emacs-lisp code -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2015 Free Software Foundation, Inc.
5 ;; Author: Artur Malabarba <emacs@endlessparentheses.com>
6 ;; Keywords: convenience, lisp
8 ;; Package-Requires: ((emacs "24.2"))
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.
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.
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/>.
28 ;; To use this package add the following configuration to your Emacs init
32 ;; │ (add-hook 'emacs-lisp-mode-hook #'nameless-mode)
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'.
38 ;; While the mode is active, the `_' key inserts the package
39 ;; namespace if appropriate.
44 (defgroup nameless nil
45 "Customization group for nameless."
48 (defcustom nameless-prefix "/"
49 "Prefix displayed instead of package namespace."
52 (defface nameless-face
53 '((t :inherit font-lock-type-face))
54 "Face used on `nameless-prefix'")
58 (defvar nameless-mode)
59 (defun nameless--compose-as (display)
60 "Compose the matched region and return a face spec."
62 (compose-region (match-beginning 1)
64 (concat display nameless-prefix))
65 '(face nameless-face)))
67 (defvar-local nameless--font-lock-keywords nil)
69 (defun nameless--ensure ()
71 (font-lock-fontify-region (point-min) (point-max))))
73 (defun nameless--remove-keywords ()
74 "Remove font-lock keywords set by `nameless--add-keywords'."
75 (font-lock-remove-keywords nil nameless--font-lock-keywords)
76 (setq nameless--font-lock-keywords nil)
79 (defun nameless--add-keywords (&rest r)
80 "Add font-lock keywords displaying REGEXP as DISPLAY.
82 \(fn regexp display [regexp display ...])"
83 (setq-local font-lock-extra-managed-props
84 (cons 'composition font-lock-extra-managed-props))
87 (push `(,(pop r) 1 (nameless--compose-as ,(pop r)) prepend) kws))
88 (setq nameless--font-lock-keywords kws)
89 (font-lock-add-keywords nil kws t))
94 (defvar-local nameless-current-name-regexp nil)
95 (defvar-local nameless-current-name nil)
97 (defun nameless--in-arglist-p ()
98 "Is point inside an arglist?"
102 (or (progn (forward-sexp -1)
103 (looking-at-p "[a-z-]lambda\\_>"))
104 (progn (forward-sexp -1)
105 (looking-at-p "\\(cl-\\)?def\\(un\\|macro\\|inline\\)\\*?\\_>"))))))
107 (defun nameless-insert-name (&optional self-insert)
108 "Insert the name of current package, with a hyphen."
111 (not nameless-current-name)
112 (eq (char-before) ?\\)
113 (nameless--in-arglist-p)
114 (string-match (rx (or (syntax symbol)
116 (string (char-before))))
117 (call-interactively #'self-insert-command)
118 (insert nameless-current-name "-")))
120 (defun nameless--name-regexp (name)
121 "Return a regexp of the current name."
122 (concat "\\_<\\(" (regexp-quote name) "-\\)\\(\\s_\\|\\sw\\)"))
127 (define-minor-mode nameless-mode
128 nil nil " :" '(("_" . nameless-insert-name))
130 (if (or nameless-current-name-regexp
131 nameless-current-name
132 (ignore-errors (string-match "\\.el\\'" (lm-get-package-name))))
134 (unless nameless-current-name-regexp
135 (unless nameless-current-name
136 (setq nameless-current-name (replace-regexp-in-string "\\.[^.]*\\'" "" (lm-get-package-name))))
137 (setq nameless-current-name-regexp (nameless--name-regexp nameless-current-name)))
138 (nameless--add-keywords nameless-current-name-regexp))
140 (setq nameless-current-name nil)
141 (setq nameless-current-name-regexp nil)
142 (nameless--remove-keywords)))
143 ;; (font-lock-remove-keywords)
146 ;;; nameless.el ends here