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.3"))
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 `:' and `_' keys will insert the package
39 ;; namespace when 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-keyword-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 0)
64 (or display nameless-prefix))
65 '(face nameless-face)))
67 (defun nameless--add-keywords (&rest r)
68 "Add font-lock keywords displaying REGEXP as DISPLAY.
70 \(fn regexp display [regexp display ...])"
71 (setq-local font-lock-extra-managed-props
72 (cons 'composition font-lock-extra-managed-props))
74 (font-lock-add-keywords
75 nil `((,(pop r) 0 (nameless--compose-as ,(pop r)) prepend)) t))
77 (if (fboundp 'font-lock-ensure)
79 (font-lock-fontify-buffer))))
83 (defvar-local nameless-current-name-regexp nil)
84 (defvar-local nameless-current-name nil)
86 (defun nameless--in-arglist-p ()
87 "Is point inside an arglist?"
92 (looking-at-p "def\\(un\\|macro\\)\\_>"))))
94 (defun nameless-insert-name (&optional self-insert)
95 "Insert the name of current package, with a hyphen."
98 (not nameless-current-name)
99 (nameless--in-arglist-p)
100 (string-match (rx (or (syntax symbol)
102 (string (char-before))))
103 (call-interactively #'self-insert-command)
104 (insert nameless-current-name "-")))
106 (defun nameless--name-regexp (name)
107 "Return a regexp of the current name."
108 (concat "\\<" (regexp-quote name) "-"))
112 (define-minor-mode nameless-mode
113 nil nil " :" '(("_" . nameless-insert-name))
114 (if (and nameless-mode)
115 (if (or nameless-current-name-regexp
116 nameless-current-name
117 (ignore-errors (string-match "\\.el\\'" (lm-get-package-name))))
119 (unless nameless-current-name-regexp
120 (unless nameless-current-name
121 (setq nameless-current-name (replace-regexp-in-string "\\.[^.]*\\'" "" (lm-get-package-name))))
122 (setq nameless-current-name-regexp (nameless--name-regexp nameless-current-name)))
123 (nameless--add-keywords nameless-current-name-regexp))
124 (nameless-mode -1))))
125 ;; (font-lock-remove-keywords)
128 ;;; nameless.el ends here