1 ;;; names.el --- Namespaces for emacs-lisp. Avoid name clobbering without hiding symbols.
3 ;; Copyright (C) 2014 Free Software Foundation, Inc.
5 ;; Author: Artur Malabarba <bruce.connor.am@gmail.com>
6 ;; Maintainer: Artur Malabarba <bruce.connor.am@gmail.com>
7 ;; URL: http://github.com/Bruce-Connor/names
9 ;; Package-Requires: ((emacs "24.1") (cl-lib "0.5"))
10 ;; Keywords: extensions lisp
16 ;; The description is way too large to sanely write here, below is a
17 ;; summary. For a complete description, please visit the package's
18 ;; frontpage with `M-x names-view-manual', or see the Readme file on
19 ;; https://raw.githubusercontent.com/Bruce-Connor/names/master/Readme.org
23 ;; This file is part of GNU Emacs.
25 ;; GNU Emacs is free software: you can redistribute it and/or modify
26 ;; it under the terms of the GNU General Public License as published by
27 ;; the Free Software Foundation, either version 3 of the License, or
28 ;; (at your option) any later version.
30 ;; GNU Emacs is distributed in the hope that it will be useful,
31 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
32 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 ;; GNU General Public License for more details.
35 ;; You should have received a copy of the GNU General Public License
36 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
43 ;;; This is a patch because edebug binds under `C-x'.
44 ;; If `C-x' is not a prefix.
45 (unless (consp (key-binding "\C-x"))
46 ;; Disable the `C-xC-a' binds.
47 (setq edebug-inhibit-emacs-lisp-mode-bindings t)
48 ;; And the `C-xX' binds.
49 (when (or (null (boundp 'global-edebug-prefix))
50 (eq 24 (elt global-edebug-prefix 0)))
51 (setq global-edebug-prefix "")))
57 (declare-function names--autoload-do-load "names" 2)
58 (if (fboundp 'function-get)
59 (defalias 'names--function-get #'function-get)
60 (defun names--function-get (f prop &rest _)
61 "Return the value of property PROP of function F.
62 If F is an autoloaded macro, try to autoload it in the hope that
65 (while (and (symbolp f)
66 (null (setq val (get f prop)))
68 (let ((fundef (symbol-function f)))
69 (if (and (names--autoloadp fundef)
70 (not (equal fundef (names--autoload-do-load fundef f))))
71 nil ;Re-try `get' on the same `f'.
74 (defun names--autoload-do-load (def name)
75 "Load autoloaded definition DEF from function named NAME."
76 (unless (load (cadr def) 'noerror)
77 (error "Macro `%s' is autoloaded, but its file (%s) couldn't be loaded"
79 (symbol-function name)))
82 (defalias 'names--compat-macrop #'macrop)
83 (defun names--compat-macrop (object)
84 "Non-nil if and only if OBJECT is a macro."
85 (let ((def (indirect-function object t)))
87 (or (eq 'macro (car def))
88 (and (names--autoloadp def) (memq (nth 4 def) '(macro t))))))))
90 (if (fboundp 'autoloadp)
91 (defalias 'names--autoloadp #'autoloadp)
92 (defsubst names--autoloadp (object)
93 "Non-nil if OBJECT is an autoload."
94 (eq 'autoload (car-safe object))))
96 (unless (get-edebug-spec 'cl-defun)
97 (def-edebug-spec cl-defun defun*))
98 (unless (get-edebug-spec 'cl-defmacro)
99 (def-edebug-spec cl-defmacro defmacro*))
100 (unless (get-edebug-spec 'setq-local)
101 (def-edebug-spec setq-local setq))
102 (unless (get-edebug-spec 'loop)
103 (def-edebug-spec loop
105 ;; These are usually followed by a symbol, but it can
106 ;; actually be any destructuring-bind pattern, which
107 ;; would erroneously match `form'.
108 [[&or "for" "as" "with" "and"] sexp]
109 ;; These are followed by expressions which could
110 ;; erroneously match `symbolp'.
111 [[&or "from" "upfrom" "downfrom" "to" "upto" "downto"
112 "above" "below" "by" "in" "on" "=" "across"
113 "repeat" "while" "until" "always" "never"
114 "thereis" "collect" "append" "nconc" "sum"
115 "count" "maximize" "minimize" "if" "unless"
117 ;; Simple default, which covers 99% of the cases.
121 ;;; ---------------------------------------------------------------
123 (defconst names-version "20150115.1" "Version of the names.el package.")
125 (defvar names--name nil
126 "Name of the current namespace inside the `define-namespace' macro.")
127 (defvar names--regexp nil "Regexp matching `names--name'.")
129 (defvar names--load-file (and load-file-name (expand-file-name load-file-name))
130 "The file where the current version of Names was loaded.
131 This is used by `names--check-for-update' to check if a new
132 version has been installed.")
134 (defvar names--bound nil
135 "List of variables defined in this namespace.")
136 (defvar names--fbound nil
137 "List of functions defined in this namespace.")
138 (defvar names--macro nil
139 "List of macros defined in this namespace.")
141 (defvar names--keywords nil
142 "Keywords that were passed to the current namespace.
143 See `names--keyword-list' for a list and description of possible
146 (defvar names--local-vars nil
147 "Non-global vars that are let/lambda bound at the moment.
148 These won't be namespaced, as local takes priority over namespace.")
150 (defvar names--protection nil
151 "Leading chars used to identify protected symbols.
152 Don't customise this.
153 Instead use the :protection keyword when defining the
156 (defvar names--current-run nil
157 "Either 1 or 2, depending on which runthrough we're in.")
159 (defvar names--var-list
160 '(names--name names--regexp names--bound
162 names--package names--group-parent
163 names--macro names--current-run
164 names--fbound names--keywords
165 names--local-vars names--protection)
166 "List of variables the user shouldn't touch.")
169 (defvar names--inside-make-autoload nil
170 "Used in `make-autoload' to indicate we're making autoloads.")
172 (defvar names--package nil
173 "Package, name to be used by the :group and :version keywords.
174 Is derived from `load-file-name', unless the :package keyword is
175 passed to `define-namespace'.")
177 (defvar names--group-parent nil
178 "The name of the parent to be given to `defgroup'.
179 Is only non-nil if the :group keyword is passed to `define-namespace'.")
181 (defvar names--version nil
182 "The version number given by :version.
183 Used to define a constant and a command.")
185 (defconst names--keyword-list
188 (if (or (symbolp x) (listp x))
189 (setq names--group-parent x)
191 "Argument given to :group is not a symbol: %s" x)))
192 "Indicate `define-namespace' should make a `defgroup' for you.
193 The name of the group is the package name (see :package keyword).
194 This keyword should be given one argument, the name of the PARENT
195 group as an unquoted symbol.
197 Alternatively, the argument can be a list, in which case it is a
198 list of arguments to be passed to `defgroup' (essentially, a full
199 group definition without the leading `defgroup').
201 If this keyword is provided, besides including a defgroup, Names
202 will also include a :group keyword in every `defcustom' (and
203 similar forms) that don't already contain one.")
209 (setq names--version x)
211 "Argument given to :version is not a string: %s" x)))
212 "Indicate `define-namespace' should define the version number.
213 This keyword should be given one argument, a string describing
214 the package's version number.
216 With this, Names will generate a `defconst' and an interactive
217 `defun', each named `PACKAGE-NAME-version'. The function messages
218 and returns the version number. See the :package keyword.")
224 (setq names--package x)
226 "Argument given to :package is not a symbol: %s" x)))
227 "Set the name of this package to the given symbol.
228 This keyword should be given one argument, a symbol corresponding
229 to the name of this package.
231 If this keyword isn't used, the package name is taken as the the
232 file's basename, but only if its actually needed. This name is
233 needed by the :version and :group keywords.")
238 (let ((val (symbol-name x)))
239 (setq names--protection
240 (format "\\`%s" (regexp-quote val)))))
241 "Change the value of the `names--protection' variable.")
245 "Indicates variables assigned in let-bind are NOT candidates for namespacing.")
249 "Cause a message to be called on each special form.")
253 "Accept namespaced names from outside current namespace definition.")
257 "Indicate symbols quoted with `quote' should be considered variable names.")
259 (:dont-assume-function-quote
261 "Indicate symbols quoted with `function' should NOT be considered function names.")
265 "Indicate only forms actually inside the namespace should be present in the output.
266 This is for internal use. It is used by `names-eval-defun' to
267 prevent `define-namespace' from adding things like `defgroup' or
268 `defconst's to the output."))
269 "List of keywords used by `define-namespace'.
270 Each element is a list containing
271 (KEYWORD N DEFINITION DOCUMENTATION)
274 - KEYWORD is the keyword's name, a symbol satifying `keywordp'.
275 - N is the number of arguments it takes, an integer.
276 - DEFINITION is a function (symbol or lambda) that takes N
277 arguments and does whatever you need for implementing the
279 - DOCUMENTATION is a string explaining the keyword's
282 (defmacro names--prepend (sbl)
283 "Return namespace+SBL."
284 (declare (debug (symbolp)))
285 `(intern (format "%s%s" names--name ,sbl)))
288 (defmacro names--filter-if-bound (var &optional pred)
289 "If VAR is bound and is a list, take the car of its elements which satify PRED."
290 (declare (debug (symbolp &optional function-form)))
291 `(when (boundp ',var)
294 (mapcar (lambda (x) (when (funcall (or ,pred 'identity) (or (car-safe x) x))
295 (or (car-safe x) x)))
298 (defmacro names--next-keyword (body)
299 "If car of BODY is a known keyword, `pop' it (and its arguments) from body.
300 Returns a list (KEYWORD . ARGUMENTLIST)."
301 (declare (debug sexp))
302 `(let ((kar (car-safe ,body))
306 (setq n (assoc kar names--keyword-list))
308 (dotimes (it (1+ n) out)
309 (push (pop ,body) out))
312 (defvar names--has-reloaded nil
313 "Whether `names--reload-if-upgraded' has already been called in this run.")
316 ;;; ---------------------------------------------------------------
317 ;;; The Main Macro and Main Function.
319 (defmacro define-namespace (name &rest body)
320 "Inside the namespace NAME, execute BODY.
321 NAME can be any symbol (not quoted), but it's highly recommended
322 to use some form of separator (such as :, /, or -). For a
323 complete description of this macro, please visit the frontpage
324 with \\[names-view-manual].
326 In summary, this macro has two main effects:
328 1. Any definitions inside BODY will have NAME prepended to the
331 (define-namespace foo-
332 (defvar bar 1 \"docs\")
337 (defvar foo-bar 1 \"docs\")
340 2. Any function calls and variable names get NAME prepended to
341 them if such a variable or function exists. Ex:
343 (define-namespace foo:
344 (defun message (x y) nil)
345 (message \"%s\" my-var)
350 (defun foo:message (x y) nil)
351 (foo:message \"%s\" my-var)
353 Note how `message' is expanded to `foo:message' in the second
354 form, because that function exists. Meanwhile, `bar' is left
355 untouched because `foo:bar' is not a known variable name.
357 ===============================
361 In order for `define-namespace' to work with \";;;###autoload\"
362 comments must replace all instances of \";;;###autoload\" inside
363 your `define-namespace' with `:autoload'.
364 Afterwards, add an \";;;###autoload\" comment just above your
367 ===============================
371 Immediately after NAME you may add keywords which customize the
372 behaviour of `define-namespace'. For a list of possible keywords
373 and a description of their effects, see the variable
374 `names--keyword-list'.
376 \(fn NAME [KEYWORD ...] BODY)"
377 (declare (indent (lambda (&rest x) 0))
378 (debug (&define name [&rest keywordp &optional [&or symbolp (symbolp . symbolp)]] body)))
379 (let ((names--has-reloaded names--has-reloaded))
380 ;; This was to avoid an infinite recursion, but the bug turned out
381 ;; to be somewhere else. Still, I see no reason to erase this.
382 (unless names--has-reloaded
383 (setq names--has-reloaded t)
384 (names--reload-if-upgraded))
385 (names--error-if-using-vars)
386 (names--define-namespace-implementation name body)))
388 (defun names--define-namespace-implementation (name body)
389 "Namespace BODY using NAME.
390 See `define-namespace' for more information."
392 (let* ((names--name name)
394 (concat "\\`" (regexp-quote (symbol-name name))))
395 (names--current-run 0)
396 ;; Use the :protection keyword to change this.
397 (names--protection "\\`::")
399 (names--remove-namespace-from-list
400 (names--filter-if-bound byte-compile-bound-variables)
401 (names--filter-if-bound byte-compile-variables)))
403 (names--remove-namespace-from-list
404 (names--filter-if-bound byte-compile-macro-environment 'names--compat-macrop)
405 (names--filter-if-bound byte-compile-function-environment 'names--compat-macrop)))
407 (names--remove-namespace-from-list
408 (names--filter-if-bound byte-compile-macro-environment (lambda (x) (not (names--compat-macrop x))))
409 (names--filter-if-bound byte-compile-function-environment (lambda (x) (not (names--compat-macrop x))))))
410 names--keywords names--local-vars key-and-args
411 names--version names--package names--group-parent)
413 (while (setq key-and-args (names--next-keyword body))
414 (names--handle-keyword key-and-args)
415 (push key-and-args names--keywords))
417 ;; First have to populate the bound and fbound lists. So we read
418 ;; the entire form (without evaluating it).
419 (mapc 'names-convert-form body)
420 (setq names--current-run (1+ names--current-run))
422 ;; Then we go back and actually namespace the entire form, which
423 ;; we'll later return so that it can be evaluated.
428 (when (and names--group-parent
429 (null (names--keyword :clean-output)))
430 (list (names--generate-defgroup)))
431 (when (and names--version
432 (null (names--keyword :clean-output)))
433 ;; `names--generate-version' returns a list.
434 (names--generate-version))
435 (mapcar 'names-convert-form
436 ;; Unless we're in `make-autoload', then just return autoloads.
437 (if names--inside-make-autoload
438 (names--extract-autoloads body)
441 ;; On emacs-version < 24.4, the byte-compiler cannot expand a
442 ;; macro if it is being called in the same top-level form as
443 ;; it was defined. That's a problem for us, since the entire
444 ;; namespace is a single top-level form (we return a `progn').
445 ;; The solution is for us to add the macros to
446 ;; `byte-compile-macro-environment' ourselves.
447 (if (and (boundp 'byte-compile-current-buffer)
448 byte-compile-current-buffer
449 (null names--inside-make-autoload)
450 (version< emacs-version "24.4"))
451 (let ((byte-compile-macro-environment
452 (when (boundp 'byte-compile-macro-environment)
453 byte-compile-macro-environment)))
454 (mapc #'names--add-macro-to-environment (cdr body))
455 (macroexpand-all body byte-compile-macro-environment))
458 ;; Exiting the `unwind-protect'.
459 (mapc (lambda (x) (set x nil)) names--var-list)))
461 (defun names--reload-if-upgraded ()
462 "Verify if there's a more recent version of Names in the `load-path'.
466 (let ((lp (expand-file-name (find-library-name "names")))
469 (not (string= lp names--load-file))
470 (file-readable-p lp))
472 (insert-file-contents-literally lp)
473 (goto-char (point-min))
476 (when (search-forward-regexp
477 "(defconst\\s-+names-version\\s-+\"\\([^\"]+\\)\"" nil t)
478 (match-string-no-properties 1))))
479 (when (and new-version (version< names-version new-version))
480 (eval-buffer nil lp)))))))
482 (defun names-convert-form (form)
483 "Do namespace conversion on FORM.
484 FORM is any legal elisp form.
485 Namespace name is defined by the global variable `names--name'.
487 See macro `namespace' for more information."
492 (let ((kar (car form))
495 ;; If symbol is protected, clean it.
497 (setq func (names--remove-protection kar)))
498 (names--message "Protected: %s" kar)
499 ;; And decide what to do with it.
500 (names--handle-args func (cdr form)))
502 ;; If kar is a list, either 1) it's a lambda form, 2) it's a
503 ;; macro we don't know about yet, 3) we have a bug.
505 (when (and (null (functionp kar))
506 (> names--current-run 1))
507 (names--warn "Ran into the following strange form.
508 Either it's an undefined macro, a macro with a bad debug declaration, or we have a bug.\n%s" form))
509 (mapcar 'names-convert-form form))
511 ;; Namespaced Functions/Macros
512 ((names--fboundp kar)
513 (names--message "Namespaced: %s" kar)
514 (names--args-of-function-or-macro
515 (names--prepend kar) (cdr form) (names--macrop kar)))
517 ;; General functions/macros/special-forms
518 (t (names--handle-args kar (cdr form))))))
521 (names--message "Symbol handling: %s" form)
522 ;; If symbol is protected, clean it and don't namespace it.
523 (or (names--remove-protection form)
524 ;; Otherwise, namespace if possible.
525 (if (names--boundp form)
526 (names--prepend form)
532 ;;; ---------------------------------------------------------------
533 ;;; Some auxiliary functions
534 (defun names-view-manual ()
535 "Call `browse-url' to view the manual of the Names package."
537 (browse-url "http://github.com/Bruce-Connor/names"))
539 (defun names--package-name ()
540 "Return the package name as a symbol.
541 Decide package name based on several factors. In order:
542 1. The :package keyword,
543 2. The namespace NAME, removing the final char."
545 (let ((package (symbol-name names--name)))
546 (prog1 (setq names--package
547 (intern (substring package 0 -1)))
548 (names--warn "No :package given. Guessing `%s'"
551 (defun names--generate-defgroup ()
552 "Return a `defgroup' form for the current namespace."
553 (if (listp names--group-parent)
554 (cons 'defgroup names--group-parent)
555 (list 'defgroup (names--package-name) nil
556 (format "Customization group for %s." (names--package-name))
557 :prefix (symbol-name names--name)
558 :group `',names--group-parent)))
560 (defun names--generate-version ()
561 "Return a `defun' and a `defconst' forms declaring the package version.
562 Also adds `version' to `names--fbound' and `names--bound'."
563 (add-to-list 'names--fbound 'version)
564 (add-to-list 'names--bound 'version)
566 (list 'defconst (names--prepend 'version)
568 (format "Version of the %s package." (names--package-name)))
569 (list 'defun (names--prepend 'version) nil
570 (format "Version of the %s package." (names--package-name))
573 ,(format "%s version: %s" (names--package-name) names--version))
576 (defun names--add-macro-to-environment (form)
577 "If FORM declares a macro, add it to `byte-compile-macro-environment'."
578 (let ((expansion form))
579 (while (names--compat-macrop (car-safe expansion))
581 (ignore-errors (macroexpand
582 expansion byte-compile-macro-environment))))
585 (or (and (memq (car-safe expansion) '(progn prog1 prog2))
586 (mapc #'names--add-macro-to-environment (cdr expansion)))
587 (and (eq 'defalias (car-safe expansion))
588 (let ((def (ignore-errors (eval (nth 2 expansion)))))
589 (and (names--compat-macrop def)
590 (push (cons (ignore-errors
591 (eval (nth 1 expansion)))
593 byte-compile-macro-environment))))))))
596 (defadvice find-function-search-for-symbol
597 (around names-around-find-function-search-for-symbol-advice
598 (symbol type library) activate)
599 "Make sure `find-function-search-for-symbol' understands namespaces."
602 (unless (cdr ad-return-value)
603 (with-current-buffer (car ad-return-value)
604 (search-forward-regexp "^(define-namespace\\_>")
605 (skip-chars-forward "\r\n[:blank:]")
606 (let* ((names--regexp
607 (concat "\\`" (regexp-quote
608 (symbol-name (read (current-buffer))))))
610 ;; We manually implement `names--remove-namespace'
611 ;; because it might not be loaded.
612 (let ((name (symbol-name symbol)))
613 (when (string-match names--regexp name)
614 (intern (replace-match "" nil nil name))))))
616 (ad-set-arg 0 short-symbol)
619 (defun names--extract-autoloads (body)
620 "Return a list of the forms in BODY preceded by :autoload."
622 (when (setq acons (memq :autoload body))
625 (names--extract-autoloads (cdr (cdr acons)))))))
628 (defadvice make-autoload (around names-before-make-autoload-advice
629 (form file &optional expansion) activate)
630 "Make sure `make-autoload' understands `define-namespace'.
631 Use the `names--inside-make-autoload' variable to indicate to
632 `define-namespace' that we're generating autoloads."
633 ;; We used to have a letbind here, but this was causing a void
634 ;; variable bug on Emacs 24.3.
636 (if (null (eq (car-safe form) 'define-namespace))
638 (setq names--inside-make-autoload t)
639 (setq form (macroexpand form))
640 (setq names--inside-make-autoload nil)
641 ;; Up to 24.2 `make-autoload' couldn't handle `progn's.
642 (if (version< emacs-version "24.3")
643 (setq ad-return-value
645 (mapcar (lambda (x) (names--make-autoload-compat x file))
647 (ad-set-arg 2 'expansion)
651 (defun names--make-autoload-compat (form file)
652 (if (eq (car-safe form) 'defalias)
654 (make-autoload form file)))
656 (defvar names--ignored-forms '(declare)
657 "The name of functions/macros/special-forms which we return without reading.")
659 (defun names--handle-args (func args)
660 "Generic handling for the form (FUNC . ARGS), without namespacing FUNC."
661 (if (memq func names--ignored-forms)
663 ;; TODO: Speed this up. Just change it to an alist or a hash-table.
664 (let ((handler (intern-soft (format "names--convert-%s" func))))
665 ;; Some function-like forms get special handling.
666 ;; That's anything with a names--convert-%s function defined.
667 (if (fboundp handler)
668 (progn (names--message "Special handling: %s" handler)
669 (funcall handler (cons func args)))
670 ;; If it isn't special, it's either a function or a macro.
671 (names--args-of-function-or-macro func args (names--compat-macrop func))))))
673 (defun names--message (f &rest rest)
674 "If :verbose is on, pass F and REST to `message'."
675 (when (names--keyword :verbose)
676 (apply 'message (concat "[names] " f) rest)))
678 (defun names--warn (f &rest rest)
679 "Pass F and REST to `message', unless byte-compiling or non-interactive."
680 (unless (and (null (names--keyword :verbose))
681 (and (boundp 'byte-compile-function-environment)
682 byte-compile-function-environment))
683 (apply 'message (concat "[names] " f) rest)))
685 (defun names--error-if-using-vars ()
686 "Remind the developer that variables are not customizable."
690 (error "[names] Global value of variable %s should be nil! %s"
691 x "Set it using keywords instead")))
694 (defun names--remove-namespace-from-list (&rest lists)
695 "Return a concatenated un-namespaced version of LISTS.
696 Symbols in LISTS that aren't namespaced are removed, symbols that
697 are namespaced become un-namespaced."
698 (delq nil (mapcar 'names--remove-namespace (apply 'append lists))))
700 (defun names--remove-namespace (symbol)
701 "Return SYMBOL with namespace removed, or nil if it wasn't namespaced."
702 (names--remove-regexp symbol names--regexp))
704 (defun names--remove-protection (symbol)
705 "Remove the leading :: from SYMBOL if possible, otherwise return nil."
706 (names--remove-regexp symbol names--protection))
708 (defun names--remove-regexp (s r)
709 "Return S with regexp R removed, or nil if S didn't match."
710 (let ((name (symbol-name s)))
711 (when (string-match r name)
712 (intern (replace-match "" nil nil name)))))
714 (defun names--quote-p (sbl)
715 "Is SBL a function which quotes its argument?"
716 (memq sbl '(quote function)))
718 (defun names--fboundp (sbl)
719 "Is namespace+SBL a fboundp symbol?"
720 (or (memq sbl names--fbound)
721 (memq sbl names--macro)
722 (and (names--keyword :global)
723 (fboundp (names--prepend sbl)))))
725 (defun names--macrop (sbl)
726 "Is namespace+SBL a fboundp symbol?"
727 (or (memq sbl names--macro)
728 (and (names--keyword :global)
729 (names--compat-macrop (names--prepend sbl)))))
731 (defun names--keyword (keyword)
732 "Was KEYWORD one of the keywords passed to the `namespace' macro?"
733 (assoc keyword names--keywords))
735 (defun names--boundp (sbl)
736 "Is namespace+SBL a boundp symbol?
737 If SBL has a let binding, that takes precendence so this also
739 (and (null (memq sbl names--local-vars))
740 (or (memq sbl names--bound)
741 (and (names--keyword :global)
742 (boundp (names--prepend sbl))))))
744 ;;; This is calling edebug even on `when' and `unless'
745 (defun names--args-of-function-or-macro (function args macro)
746 "Namespace FUNCTION's arguments ARGS, with special treatment if MACRO is non-nil."
748 (let ((it (names--get-edebug-spec function))
749 (names--verbose (eq function 'push)))
750 (names--message "Edebug-spec of `%s' is %s" function it)
751 ;; Macros where we evaluate all arguments are like functions.
753 (names--args-of-function-or-macro function args nil)
754 ;; Macros where nothing is evaluated we can just return.
757 ;; Other macros are complicated. Ask edebug for help.
758 (names--macro-args-using-edebug (cons function args)))))
759 ;; We just convert the arguments of functions.
760 (cons function (mapcar 'names-convert-form args))))
762 (defun names--get-edebug-spec (name)
763 "Get 'edebug-form-spec property of symbol NAME."
764 ;; Get the spec of symbol resolving all indirection.
768 (and (symbolp indirect)
771 indirect 'edebug-form-spec 'macro))))
772 ;; (edebug-trace "indirection: %s" edebug-form-spec)
773 (setq spec indirect))
776 (defvar names--is-inside-macro nil
777 "Auxiliary var used in `names--macro-args-using-edebug'.")
779 (defvar names--gensym-counter 0
780 "Counter used to uniquify symbols generated `names--gensym'.")
782 (defun names--macro-args-using-edebug (form)
783 "Namespace the arguments of macro FORM by hacking into edebug.
784 This takes advantage of the fact that macros (should) declare a
785 `debug' specification which tells us which arguments are actually
788 Ideally, we would read this specification ourselves and see how
789 it matches (cdr FORM), but that would take a lot of work and
790 we'd be reimplementing something that edebug already does
791 phenomenally. So we hack into edebug instead."
795 ((max-lisp-eval-depth 3000)
798 (names--is-inside-macro form)
799 ;; Prevent excessive messaging.
800 ;; TODO: Don't do this if `message' is advised.
801 ((symbol-function 'message) #'names--edebug-message)
802 ;; Older edebugs have poor `get-edebug-spec'.
803 ((symbol-function 'get-edebug-spec) #'names--get-edebug-spec)
804 ;; Give symbols our own name.
805 ((symbol-function 'cl-gensym) #'names--gensym)
806 ;; Stop at one level deep.
807 ((symbol-function 'edebug-form) #'names--edebug-form)
808 ;; Don't actually wrap anything.
809 ((symbol-function 'edebug-make-enter-wrapper)
810 #'names--edebug-make-enter-wrapper))
814 (goto-char (point-min))
816 (edebug-read-top-level-form))
819 "Couldn't namespace this macro using its (debug ...) declaration: %s"
823 (when (equal (car-safe (cdr-safe er))
824 "Lisp nesting exceeds `max-lisp-eval-depth'")
826 "Lisp nesting exceeded `max-lisp-eval-depth' at the following form: %s"
830 (defvar names--message-backup
831 (if (ad-is-advised 'message)
832 (ad-get-orig-definition 'message)
833 (symbol-function 'message))
834 "Where names stores `message's definition while overriding it.")
836 (defvar names--verbose nil
837 "If non-nil, verbose message are printed regardless of the :verbose keyword.
838 Use this to easily turn on verbosity during tests.")
840 (defun names--edebug-message (&rest _)
841 (if (or (names--keyword :verbose) names--verbose)
842 (apply names--message-backup _)
843 (when _ (apply 'format _))))
845 (defun names--edebug-make-enter-wrapper (forms)
846 (setq edebug-def-name
849 (names--gensym "edebug-anon")))
852 (defun names--gensym (prefix)
853 "Generate a new uninterned symbol.
854 The name is made by appending a number to PREFIX and preppending \"names\", default \"G\"."
855 (let ((num (prog1 names--gensym-counter
856 (setq names--gensym-counter
857 (1+ names--gensym-counter)))))
858 (make-symbol (format "names-%s%d" (if (stringp prefix) prefix "G") num))))
860 (defun names--edebug-form (cursor)
861 "Parse form given by CURSOR using edebug, and namespace it if necessary."
863 ;; Return the instrumented form for the following form.
864 ;; Add the point offsets to the edebug-offset-list for the form.
865 (let* ((form (edebug-top-element-required cursor "Expected form"))
866 (offset (edebug-top-offset cursor))
867 ;; We don't want to convert the entire form that was passed
868 ;; to `names--macro-args-using-edebug', since the head of
869 ;; that was already converted and it would lead to an
871 ;; So we check for (equal names--is-inside-macro form)
872 ;; Simply incrementing a depth counter didn't work, for a
873 ;; reason I can no longer remember.
875 ;; We DO want to convert the arguments that edebug identifies
876 ;; as forms (level-1). And we do that ourselves, don't pass
878 (func (if (or (eq names--is-inside-macro t)
879 (equal names--is-inside-macro form))
880 'identity 'names-convert-form))
881 (names--is-inside-macro
882 (if (eq func 'names-convert-form)
883 t names--is-inside-macro)))
884 (names--message " [Edebug] Ran into this: %S" form)
885 (names--message " Cursor: %S" cursor)
888 ((consp form) ;; The first offset for a list form is for the list form itself.
889 (if (eq func 'names-convert-form)
890 (names-convert-form form)
891 (let* ((head (car form))
892 (spec (and (symbolp head) (get-edebug-spec head)))
893 (new-cursor (edebug-new-cursor form offset)))
894 ;; Find out if this is a defining form from first symbol.
895 ;; An indirect spec would not work here, yet.
896 (if (and (consp spec) (eq '&define (car spec)))
897 (edebug-defining-form
899 (car offset) ;; before the form
900 (edebug-after-offset cursor)
901 (cons (symbol-name head) (cdr spec)))
902 ;; Wrap a regular form.
903 (edebug-list-form new-cursor)))))
908 ;; Anything else is self-evaluating.
910 (edebug-move-cursor cursor))))
912 (defun names--maybe-append-group (form)
913 "Append (:group `names--package') to FORM.
914 Only if the :group keyword was passed to `define-namespace' and
915 if the form doesn't already have a :group."
916 (if (or (null names--group-parent) (memq :group form))
918 (append form `(:group ',(names--package-name)))))
921 ;;; ---------------------------------------------------------------
922 ;;; Interpreting keywords passed to the main macro.
923 (defun names--handle-keyword (body)
924 "Call the function that handles the keyword at the car of BODY.
925 Such function must be listed in `names--keyword-list'. If it is
926 nil, this function just returns.
928 Regardless of whether a function was called, the keyword is added
929 to the variable `names--keywords'.
931 The car of BODY is the keyword itself and the other elements are
932 the keyword arguments, if any."
933 (let ((func (nth 2 (assoc (car body) names--keyword-list))))
935 (apply func (cdr body))
939 ;;; ---------------------------------------------------------------
940 ;;; Interpreting the actual forms found in BODY of the main macro.
942 ;; This is where the heavy work is done.
944 ;; If you'd like to implement support for some special form, simply
945 ;; define a function called `names--convert-FORM-NAME' along the
946 ;; lines of the functions defined below. It will be automatically used
947 ;; whenever that form is found.
949 ;;; Defun, defmacro, and defsubst macros are pretty predictable.
950 (defun names--convert-defmacro (form)
951 "Special treatment for `defmacro' FORM."
952 (let* ((names--name-already-prefixed t)
954 (spaced-name (names--prepend name))
956 (add-to-list 'names--macro name)
957 (add-to-list 'names--fbound name)
958 ;; Set the macros debug spec if possible. It will be relevant on
960 (when (setq decl (ignore-errors (cond
961 ((eq (car-safe (nth 3 form)) 'declare)
963 ((and (stringp (nth 3 form))
964 (eq (car-safe (nth 4 form)) 'declare))
967 (setq decl (car (cdr-safe (assoc 'debug (cdr decl)))))
968 (when decl (put spaced-name 'edebug-form-spec decl)))
969 ;; Then convert the macro as a defalias.
972 (names--convert-lambda
973 (cons spaced-name (cddr form))))))
974 (defalias 'names--convert-defmacro* 'names--convert-defmacro)
976 (defun names--convert-defvaralias (form)
977 "Special treatment for `defvaralias' FORM."
978 (let ((form (cons (car form)
979 (mapcar #'names-convert-form (cdr form))))
981 (setq name (names--remove-namespace
982 (ignore-errors (eval (cadr form)))))
984 (add-to-list 'names--bound name))
987 (defun names--convert-defalias (form)
988 "Special treatment for `defalias' FORM."
989 (let ((form (cons (car form)
990 (mapcar #'names-convert-form (cdr form))))
992 (setq name (names--remove-namespace
993 (ignore-errors (eval (cadr form)))))
995 (add-to-list 'names--fbound name))
998 (defun names--convert-defvar (form &optional dont-add)
999 "Special treatment for `defvar' FORM.
1000 If DONT-ADD is nil, the FORM's `cadr' is added to `names--bound'."
1001 (let ((name (cadr form)))
1003 (add-to-list 'names--bound name))
1007 (names--prepend name))
1008 (mapcar 'names-convert-form (cdr (cdr form))))))
1010 (defalias 'names--convert-defconst 'names--convert-defvar
1011 "Special treatment for `defconst' FORM.")
1013 (defun names--convert-defcustom (form)
1014 "Special treatment for `defcustom' FORM."
1015 (names--maybe-append-group
1016 (names--convert-defvar form)))
1018 (defun names--convert-custom-declare-variable (form)
1019 "Special treatment for `custom-declare-variable' FORM."
1020 (let ((name (eval (cadr form))) ;;ignore-errors
1021 (val (car (cddr form))))
1022 (add-to-list 'names--bound name)
1026 (list 'quote (names--prepend name)) ;cadr
1027 ;; The DEFAULT argument is explicitly evaluated by
1028 ;; `custom-declare-variable', so it should be safe to namespace
1029 ;; even when quoted. Plus, we need to do this because
1030 ;; defcustom quotes this part.
1031 (if (names--quote-p (car-safe val))
1032 (list (car val) (names-convert-form (cadr val)))
1033 (names-convert-form val))
1034 (names-convert-form (car (cdr (cdr (cdr form))))))
1035 (mapcar 'names-convert-form (cdr (cdr (cdr (cdr form))))))))
1037 (defun names--convert-defface (form)
1038 "Special treatment for `defface' FORM.
1039 Identical to defvar, just doesn't add the symbol to the boundp
1040 list. And maybe use a :group."
1041 (names--maybe-append-group
1042 (names--convert-defvar form :dont-add)))
1044 (defun names--convert-define-derived-mode (form)
1045 "Special treatment for `define-derived-mode' FORM."
1046 (let ((name (cadr form)))
1047 (add-to-list 'names--fbound name)
1048 (add-to-list 'names--bound name)
1049 (add-to-list 'names--bound
1050 (intern (format "%s-map" name)))
1051 (add-to-list 'names--bound
1052 (intern (format "%s-hook" name)))
1054 (names--maybe-append-group
1055 ;; And here we namespace it.
1058 (names--prepend name)
1060 (names-convert-form (nth 3 form))
1061 (names-convert-form (nth 4 form))))
1062 (mapcar #'names-convert-form (cddr (cl-cdddr form))))))
1064 (defun names--convert-define-minor-mode (form)
1065 "Special treatment for `define-minor-mode' FORM."
1066 (let ((name (cadr form))
1067 (keymap (nth 5 form)))
1068 ;; Register the mode name
1069 (add-to-list 'names--fbound name)
1070 (add-to-list 'names--bound name)
1071 (add-to-list 'names--bound (intern (format "%s-hook" name)))
1072 ;; Register the keymap
1073 (if (or (null keymap) (null (symbolp keymap)))
1074 (add-to-list 'names--bound (intern (format "%s-map" name)))
1075 (when (setq keymap (names--remove-namespace keymap))
1076 (add-to-list 'names--bound keymap)))
1078 (names--maybe-append-group
1079 ;; And here we namespace it.
1082 (names--prepend name)
1084 (names-convert-form (nth 3 form))
1085 (names-convert-form (nth 4 form))
1086 (names-convert-form (nth 5 form))
1087 (names-convert-form (nth 6 form))))
1088 (mapcar #'names-convert-form (cddr form)))))
1090 (defun names--convert-define-globalized-minor-mode (form)
1091 "Special treatment for `define-globalized-minor-mode' FORM.
1092 The NAME of the global mode will NOT be namespaced, despite being
1093 a definition. It is kept verbatim.
1094 This is because people tend to name their global modes as
1095 `global-foo-mode', and namespacing would make this impossible.
1097 The MODE and TURN-ON arguments are converted as function names.
1098 Everything else is converted as regular forms (which usually
1099 means no conversion will happen since it's usually keywords and
1101 (let ((name (names--remove-namespace (cadr form)))
1102 (copy (cl-copy-list form)))
1103 ;; Register the mode name
1105 (add-to-list 'names--fbound name)
1106 (add-to-list 'names--bound name)
1107 (add-to-list 'names--bound (intern (format "%s-hook" name))))
1108 (names--maybe-append-group
1109 ;; And here we namespace it.
1114 (names--handle-symbol-as-function (pop copy))
1115 (names--handle-symbol-as-function (pop copy)))
1116 (mapcar #'names-convert-form copy)))))
1117 (defalias #'names--convert-define-global-minor-mode
1118 #'names--convert-define-globalized-minor-mode)
1119 (defalias #'names--convert-easy-mmode-define-global-mode
1120 #'names--convert-define-globalized-minor-mode)
1122 (defun names--convert-quote (form)
1123 "Special treatment for `quote' FORM.
1124 When FORM is (quote argument), argument too arbitrary to be
1125 logically namespaced and is never parsed for namespacing
1126 (but see :assume-var-quote in `names--keyword-list').
1128 When FORM is (function form), a symbol is namespaced as a
1129 function name, a list is namespaced as a lambda form."
1130 (let ((kadr (cadr form))
1131 (this-name (car form))
1133 (if (and (eq this-name 'function)
1135 (list this-name (names-convert-form kadr))
1138 ;; A symbol inside a function quote should be a function,
1139 ;; unless the user disabled that.
1140 ((and (eq this-name 'function)
1141 (null (names--keyword :dont-assume-function-quote)))
1143 (names--handle-symbol-as-function kadr)))
1145 ;; A symbol inside a regular quote should be a function, if
1146 ;; the user asked for that.
1147 ((and (eq this-name 'quote)
1148 (names--keyword :assume-var-quote))
1150 (or (names--remove-protection kadr)
1151 (if (names--boundp kadr)
1152 (names--prepend kadr)
1158 (defun names--handle-symbol-as-function (s)
1159 "Namespace symbol S as a function name."
1160 (or (names--remove-protection s)
1161 (if (names--fboundp s) (names--prepend s) s)))
1163 (defalias 'names--convert-function 'names--convert-quote)
1165 (defun names--convert-macro (form)
1166 "Special treatment for `macro' form.
1167 Return (macro . (names-convert-form (cdr FORM)))."
1168 (cons 'macro (names-convert-form (cdr form))))
1170 (defun names--convert-lambda (form)
1171 "Special treatment for `lambda' FORM."
1172 (let ((names--local-vars
1173 (append (names--vars-from-arglist (cadr form))
1175 (forms (cdr (cdr form))))
1179 (when (stringp (car forms))
1182 (setq forms (cdr forms))))
1183 (when (eq 'interactive (car-safe (car forms)))
1185 (list (list (car (car forms))
1186 (names-convert-form (cadr (car forms)))))
1187 (setq forms (cdr forms))))
1189 ;; (message "%S" forms)
1190 (mapcar 'names-convert-form forms)))))
1192 (defun names--convert-clojure (form)
1193 "Special treatment for `clojure' FORM."
1194 (names--warn "Found a `closure'! You should use `lambda's instead")
1195 (let ((names--local-vars
1196 (append (names--vars-from-arglist (cadr form))
1198 (forms (cdr (cdr form))))
1201 (names--convert-lambda (cdr form)))))
1203 (defun names--vars-from-arglist (args)
1204 "Get a list of local variables from a generalized arglist ARGS."
1209 (let ((symb (or (cdr-safe (car-safe x)) (car-safe x) x)))
1210 (when (and (symbolp symb)
1211 (null (string-match "^&" (symbol-name symb)))
1216 (defun names--convert-defun (form)
1217 "Special treatment for `defun' FORM."
1218 (let* ((name (cadr form)))
1219 (add-to-list 'names--fbound name)
1221 (names--convert-lambda
1222 (cons (names--prepend name) (cddr form))))))
1223 (defalias 'names--convert-defun* 'names--convert-defun)
1224 (defalias 'names--convert-defsubst 'names--convert-defun)
1225 (defalias 'names--convert-defsubst* 'names--convert-defun)
1227 (defun names--let-var-convert-then-add (sym add)
1228 "Try to convert SYM unless :no-let-vars is in use.
1229 If ADD is non-nil, add resulting symbol to `names--local-vars'."
1230 (let ((name (if (null (names--keyword :no-let-vars))
1231 (names-convert-form sym)
1233 (when add (add-to-list 'names--local-vars name))
1236 (defun names--convert-let (form &optional star)
1237 "Special treatment for `let' FORM.
1238 If STAR is non-nil, parse as a `let*'."
1239 (let* ((names--local-vars names--local-vars)
1244 (list (names--let-var-convert-then-add (car x) star)
1245 (names-convert-form (cadr x)))
1246 (names--let-var-convert-then-add x star)))
1248 ;; Each var defined in a regular `let' only becomes protected after
1249 ;; all others have been defined.
1251 (setq names--local-vars
1253 (mapcar (lambda (x) (or (car-safe x) x)) vars)
1254 names--local-vars)))
1256 (list (car form) vars)
1257 (mapcar 'names-convert-form (cddr form)))))
1259 (defun names--convert-let* (form)
1260 "Special treatment for `let' FORM."
1261 (names--convert-let form t))
1263 (defun names--convert-cond (form)
1264 "Special treatment for `cond' FORM."
1268 (lambda (x) (mapcar #'names-convert-form x))
1271 (defun names--convert-condition-case (form)
1272 "Special treatment for `condition-case' FORM."
1277 (names-convert-form (cadr (cdr form))))
1281 (mapcar 'names-convert-form (cdr x))))
1282 (cddr (cdr form)))))
1285 ;;; names.el ends here