emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/names d1f33bd 20/44: Merge branch 'dev'


From: Stefan Monnier
Subject: [elpa] externals/names d1f33bd 20/44: Merge branch 'dev'
Date: Sat, 27 Mar 2021 14:40:11 -0400 (EDT)

branch: externals/names
commit d1f33bde51fb74dbe1270e84b9ccfb86adea350a
Merge: e7ed674 eca83e8
Author: Artur Malabarba <bruce.connor.am@gmail.com>
Commit: Artur Malabarba <bruce.connor.am@gmail.com>

    Merge branch 'dev'
---
 TheNittyGritty.org   | 35 +++++++++++++++++++++++++++++++++++
 UsageExample.org     |  5 ++++-
 names-dev.el         | 34 ++++++++++++++++++++++++----------
 names.el             | 31 +++++++++++++++++++++++++++----
 tests/names-tests.el | 13 +++++++++++++
 5 files changed, 103 insertions(+), 15 deletions(-)

diff --git a/TheNittyGritty.org b/TheNittyGritty.org
index 78bf88b..84bf6b8 100644
--- a/TheNittyGritty.org
+++ b/TheNittyGritty.org
@@ -139,6 +139,41 @@ need to worry about, it should just do what you expect 
from it.
 
 This is only relevant if you write your own macros. If you do,
 remember to add a debug declaration in them.
+
+*** The theading macros (~->~ and ~-->~)
+
+The threading macros would require special treatment to namespace
+correctly. However, you can use the ~:functionlike-macros~ keyword to
+tell *Names* to treat them as regular functions.
+
+For example, in the following snippet:
+#+BEGIN_SRC emacs-lisp
+(require 'dash)
+(define-namespace foo-
+:functionlike-macros (-> ->>)
+
+(defvar var nil)
+(defun fun (x &optional y)
+  (concat x y))
+
+(-> "some string"
+    (fun var)
+    fun)
+)
+#+END_SRC
+the ~(fun var)~ part would be namespaced prefectly fine (~fun~ and
+~var~ will be identified as a function and variable respectively),
+because it looks like a regular function call. However, the second use
+of ~fun~ will not be correctly namespaced, because that ~fun~ looks
+like a variable.
+
+In other words, you should use these macros like this instead:
+#+BEGIN_SRC emacs-lisp
+(-> "some string"
+    (fun var)
+    (fun))
+#+END_SRC
+
 ** Accessing Global Symbols
 If one of your definitions shadows a global definition, you can still
 access it by prefixing it with =::=.
diff --git a/UsageExample.org b/UsageExample.org
index b27160e..5730966 100644
--- a/UsageExample.org
+++ b/UsageExample.org
@@ -14,7 +14,10 @@ The important items are already listed in the Readme:
 
 ;;; Code:
 
-;; `define-namespace' is autoloaded, so there's no need to require `names'.
+;; `define-namespace' is autoloaded, so there's no need to require
+;; `names'. However, requiring it here means it will also work for
+;; people who don't install through package.el.
+(eval-when-compile (require 'names))
 
 ;;;###autoload
 (define-namespace example-
diff --git a/names-dev.el b/names-dev.el
index 4efc12e..0c2dc20 100644
--- a/names-dev.el
+++ b/names-dev.el
@@ -62,11 +62,11 @@
 (defmacro names-print (name &rest forms)
   "Return the expanded results of (namespace NAME :global :verbose FORMS).
 Ideal for determining why a specific form isn't being parsed
-correctly."
+correctly. You may need to set `eval-expression-print-level' and
+`eval-expression-print-length' to nil in order to see your full
+expansion."
   (declare (indent (lambda (&rest x) 0)) (debug 0))
-  `(let ((eval-expression-print-level (max eval-expression-print-level 300))
-         (eval-expression-print-length (max eval-expression-print-length 300)))
-     (macroexpand '(define-namespace ,name :global :verbose ,@forms))))
+  `(define-namespace ,name :global :verbose ,@forms))
 
 (defvar names-font-lock
   '(("^:autoload\\_>" 0 'font-lock-warning-face prepend)
@@ -152,12 +152,17 @@ If KILL is non-nil, kill the temp buffer afterwards."
            (kill-buffer b))))))
 
 (defun names--top-of-namespace ()
-  ""
-  (progn
-    (beginning-of-defun)
-    (ignore-errors
-      (backward-up-list)
-      (names--looking-at-namespace))))
+  "Move to the top of current namespace, and return non-nil.
+If not inside a namespace, return nil and don't move point."
+  (let ((top (save-excursion
+               (beginning-of-defun)
+               (ignore-errors
+                 (backward-up-list))
+               (when (names--looking-at-namespace)
+                 (point)))))
+    (when top
+      (goto-char top)
+      t)))
 
 (defun names-eval-defun (edebug-it)
   "Identical to `eval-defun', except it works for forms inside namespaces.
@@ -204,6 +209,15 @@ Argument EVAL-LAST-SEXP-ARG-INTERNAL is the same as 
`eval-print-last-sexp'."
 
 ;; (pp (symbol-function 'names--preceding-sexp-original) (current-buffer))
 
+(defun names-pprint ()
+  "Pretty-print an expansion of the namespace around point."
+  (interactive)
+  (save-excursion
+    (when (names--top-of-namespace)
+      (let ((ns (cdr (read (current-buffer)))))
+        (pp-macroexpand-expression
+         (macroexpand (cons 'names-print ns)))))))
+
 
 ;;; Find stuff
 (require 'find-func nil t)
diff --git a/names.el b/names.el
index 4cd23a1..1695a21 100644
--- a/names.el
+++ b/names.el
@@ -5,7 +5,7 @@
 ;; Author: Artur Malabarba <bruce.connor.am@gmail.com>
 ;; Maintainer: Artur Malabarba <bruce.connor.am@gmail.com>
 ;; URL: http://github.com/Bruce-Connor/names
-;; Version: 20150125.9
+;; Version: 20150617.0
 ;; Package-Requires: ((emacs "24.1") (cl-lib "0.5"))
 ;; Keywords: extensions lisp
 ;; Prefix: names
@@ -120,7 +120,7 @@ it will set PROP."
 
 ;;; ---------------------------------------------------------------
 ;;; Variables
-(defconst names-version "20150125.9" "Version of the names.el package.")
+(defconst names-version "20150617.0" "Version of the names.el package.")
 
 (defvar names--name nil
   "Name of the current namespace inside the `define-namespace' macro.")
@@ -182,6 +182,22 @@ Is only non-nil if the :group keyword is passed to 
`define-namespace'.")
   "The version number given by :version.
 Used to define a constant and a command.")
 
+(defvar names--functionlike-macros nil
+  "Function-like macros, even if their debug-spec says otherwise.
+When expanding the namespace, these macros will be treated
+exactly like functions. This means that their contents will be
+namespaced like regular function arguments.
+
+To add macros to this list, pass the :functionlike-macros keyword
+to your namespace along with a list of macro names (as unquoted
+symbols).
+Example:
+
+    (define-namespace foo-
+    :functionlike-macros (-> ->> thread-first thread-last)
+    ;; Rest of code
+    )")
+
 (defconst names--keyword-list
   `((:group
      1 ,(lambda (x)
@@ -240,6 +256,12 @@ needed by the :version and :group keywords.")
                 (format "\\`%s" (regexp-quote val)))))
      "Change the value of the `names--protection' variable.")
 
+    (:functionlike-macros
+     1
+     ,(lambda (x) (setq names--functionlike-macros
+                   (append x names--functionlike-macros)))
+     "A list of values to be appended to `names--functionlike-macros'.")
+
     (:no-let-vars
      0 nil
      "Indicates variables assigned in let-bind are NOT candidates for 
namespacing.")
@@ -407,6 +429,7 @@ See `define-namespace' for more information."
               (names--remove-namespace-from-list
                (names--filter-if-bound byte-compile-macro-environment (lambda 
(x) (not (names--compat-macrop x))))
                (names--filter-if-bound byte-compile-function-environment 
(lambda (x) (not (names--compat-macrop x))))))
+             (names--functionlike-macros names--functionlike-macros)
              names--keywords names--local-vars key-and-args
              names--version names--package names--group-parent)
         ;; Read keywords
@@ -742,7 +765,6 @@ returns nil."
            (and (names--keyword :global)
                 (boundp (names--prepend sbl))))))
 
-;;; This is calling edebug even on `when' and `unless'
 (defun names--args-of-function-or-macro (function args macro)
   "Namespace FUNCTION's arguments ARGS, with special treatment if MACRO is 
non-nil."
   (if macro
@@ -750,7 +772,8 @@ returns nil."
             (names--verbose (eq function 'push)))
         (names--message "Edebug-spec of `%s' is %s" function it)
         ;; Macros where we evaluate all arguments are like functions.
-        (if (equal it t)
+        (if (or (equal it t)
+                (memq function names--functionlike-macros))
             (names--args-of-function-or-macro function args nil)
           ;; Macros where nothing is evaluated we can just return.
           (if (equal it 0)
diff --git a/tests/names-tests.el b/tests/names-tests.el
index 741934b..6cf3d4d 100644
--- a/tests/names-tests.el
+++ b/tests/names-tests.el
@@ -277,3 +277,16 @@
    (defcustom a-hi 1 "hi" :type 'boolean :group 'names-tests :package-version 
'(names-tests . ""))
    (defcustom a-ok 1 "hi" :type 'boolean :package-version '(names-tests . "") 
:group 'a)))
 
+(names-deftest functionlike-macros
+  ""
+  (:functionlike-macros
+   (thread-first ->>)
+   (defvar var nil)
+   (defun fun (x &optional y) (concat x y))
+   (thread-first (fun "some string" var) (fun var) fun)
+   (->> "some string" (fun var) fun))
+  ((defvar a-var nil)
+   (defun a-fun (x &optional y) (concat x y))
+   (thread-first (a-fun "some string" a-var) (a-fun a-var) fun)
+   (->> "some string" (a-fun a-var) fun)))
+



reply via email to

[Prev in Thread] Current Thread [Next in Thread]