]> git.eshelyaron.com Git - emacs.git/commitdiff
Make cl-gensym obsolete in favor of built-in gensym
authorStefan Kangas <stefankangas@gmail.com>
Mon, 24 Feb 2025 23:12:00 +0000 (00:12 +0100)
committerEshel Yaron <me@eshelyaron.com>
Wed, 26 Feb 2025 09:37:42 +0000 (10:37 +0100)
* lisp/emacs-lisp/cl-macs.el (cl-gensym): Declare function
obsolete in favor of gensym, added in Emacs 26.1.  The only reason
for its existence is that it allows an integer argument, but
that's not really useful, so it's better to remove this complexity.
Ref: https://lists.gnu.org/r/emacs-devel/2017-09/msg00313.html
* doc/misc/cl.texi (Symbols, Creating Symbols, Efficiency Concerns)
(Obsolete Setf Customization): Don't document above obsolete function.
* lisp/emacs-lisp/cl-macs.el (cl--parse-loop-clause):
* lisp/emacs-lisp/edebug.el (edebug-make-form-wrapper):
* lisp/obsolete/cl.el (cl--function-convert, lexical-let):
* lisp/obsolete/thumbs.el (thumbs-temp-file):
* lisp/progmodes/eglot.el (eglot--lambda)
(eglot--when-live-buffer, eglot--when-buffer-window)
(eglot--collecting-xrefs, eglot--glob-parse):
* lisp/progmodes/flymake.el (flymake--run-backend):
* test/lisp/emacs-lisp/package-tests.el (with-package-test):
* test/lisp/progmodes/eglot-tests.el (eglot--guessing-contact):
* test/lisp/progmodes/elisp-mode-tests.el
(elisp-shorthand-read-buffer, elisp-shorthand-read-from-string): Prefer
plain gensym to cl-gensym in files that can depend on Emacs 26.1.
* lisp/jsonrpc.el (jsonrpc-lambda, jsonrpc-request): Prefer gensym to
cl-gensym only when defined, as this file supports Emacs 25.1
* test/lisp/emacs-lisp/cl-macs-tests.el (cl-lib-test-gensym): Simplify
test as 'should' no longer uses cl-gensym.

(cherry picked from commit 60b071e224136207f7fa24983037522e637e7efa)

doc/misc/cl.texi
lisp/emacs-lisp/cl-macs.el
lisp/emacs-lisp/edebug.el
lisp/jsonrpc.el
lisp/obsolete/cl.el
lisp/obsolete/thumbs.el
lisp/progmodes/eglot.el
lisp/progmodes/flymake.el
test/lisp/emacs-lisp/cl-macs-tests.el
test/lisp/progmodes/eglot-tests.el
test/lisp/progmodes/elisp-mode-tests.el

index b4c1f29f47f82b2f0a9431674f5697d077f0710f..df67478a5aac7d71c63f5be00d6f791eeac86f00 100644 (file)
@@ -2804,7 +2804,7 @@ missing from Emacs Lisp.
 
 @menu
 * Property Lists::       @code{cl-get}, @code{cl-remprop}, @code{cl-getf}, @code{cl-remf}.
-* Creating Symbols::     @code{cl-gensym}, @code{cl-gentemp}.
+* Creating Symbols::     @code{cl-gentemp}.
 @end menu
 
 @node Property Lists
@@ -2892,30 +2892,10 @@ out the property and value cells.
 @section Creating Symbols
 @cindex gensym
 
-@noindent
-These functions create unique symbols, typically for use as
-temporary variables.
-
-@defun cl-gensym &optional x
-This function creates a new, uninterned symbol (using @code{make-symbol})
-with a unique name.  (The name of an uninterned symbol is relevant
-only if the symbol is printed.)  By default, the name is generated
-from an increasing sequence of numbers, @samp{G1000}, @samp{G1001},
-@samp{G1002}, etc.  If the optional argument @var{x} is a string, that
-string is used as a prefix instead of @samp{G}.  Uninterned symbols
-are used in macro expansions for temporary variables, to ensure that
-their names will not conflict with ``real'' variables in the user's
-code.
-
-(Internally, the variable @code{cl--gensym-counter} holds the counter
-used to generate names.  It is initialized with zero and incremented
-after each use.)
-@end defun
-
 @defun cl-gentemp &optional x
-This function is like @code{cl-gensym}, except that it produces a new
-@emph{interned} symbol.  If the symbol that is generated already
-exists, the function keeps incrementing the counter and trying
+This function is like the built-in @code{gensym}, except that it
+produces a new @emph{interned} symbol.  If the symbol that is generated
+already exists, the function keeps incrementing the counter and trying
 again until a new symbol is generated.
 @end defun
 
@@ -4419,18 +4399,18 @@ an expansion similar to:
 @example
 (cl-block nil
      (let* ((x 0)
-            (G1004 nil))
+            (g1004 nil))
        (while (< x 10)
-         (setq G1004 (cons x G1004))
+         (setq g1004 (cons x g1004))
          (setq x (+ x 1)))
-       (nreverse G1004)))
+       (nreverse g1004)))
 @end example
 
 @noindent
 will be inserted into the buffer.  (The @code{cl-block} macro is
 expanded differently in the interpreter and compiler, so
 @code{cl-prettyexpand} just leaves it alone.  The temporary
-variable @code{G1004} was created by @code{cl-gensym}.)
+variable @code{g1004} was created by @code{gensym}.)
 
 If the optional argument @var{full} is true, then @emph{all}
 macros are expanded, including @code{cl-block}, @code{cl-eval-when},
@@ -5157,7 +5137,7 @@ temporary variables.  In the setf-methods generated by
 @code{defsetf}, the second return value is simply the list of
 arguments in the place form, and the first return value is a
 list of a corresponding number of temporary variables generated
-by @code{cl-gensym}.
+by @code{gensym}.
 @end defmac
 
 @node GNU Free Documentation License
index dc67e55f371998b514c788d01c1cd616883487af..a1c34fa01e7af254026275df80fb669f721cb03e 100644 (file)
@@ -166,6 +166,7 @@ whether X is known at compile time, macroexpand it completely in
 (defun cl-gensym (&optional prefix)
   "Generate a new uninterned symbol.
 The name is made by appending a number to PREFIX, default \"G\"."
+  (declare (obsolete gensym "31.1"))
   (let ((pfix (if (stringp prefix) prefix "G"))
        (num (if (integerp prefix) prefix
               (prog1 cl--gensym-counter
@@ -1277,10 +1278,10 @@ For more details, see Info node `(cl)Loop Facility'.
       (let ((loop-for-bindings nil) (loop-for-sets nil) (loop-for-steps nil)
            (ands nil))
        (while
-           ;; Use `cl-gensym' rather than `make-symbol'.  It's important that
+            ;; Use `gensym' rather than `make-symbol'.  It's important that
            ;; (not (eq (symbol-name var1) (symbol-name var2))) because
            ;; these vars get added to the macro-environment.
-           (let ((var (or (pop cl--loop-args) (cl-gensym "--cl-var--"))))
+            (let ((var (or (pop cl--loop-args) (gensym "--cl-var--"))))
              (setq word (pop cl--loop-args))
              (if (eq word 'being) (setq word (pop cl--loop-args)))
              (if (memq word '(the each)) (setq word (pop cl--loop-args)))
index 3033e8378f57b7a328f41b2b5496413d9b64c146..aea7b45973694f9a7c6c181860a4c5a339d3706a 100644 (file)
@@ -1369,7 +1369,7 @@ infinite loops when the code/environment contains a circular object.")
 
       ;; Set the name here if it was not set by edebug-make-enter-wrapper.
       (setq edebug-def-name
-           (or edebug-def-name edebug-old-def-name (cl-gensym "edebug-anon")))
+            (or edebug-def-name edebug-old-def-name (gensym "edebug-anon")))
 
       ;; Add this def as a dependent of containing def.  Buggy.
       '(if (and edebug-containing-def-name
index 669ceba30ef8f5510d3ee65f5ade3223bf3c065d..4b8a6cec921c0aea7510104f77d4c59e8c40ccc2 100644 (file)
@@ -204,7 +204,8 @@ JSONRPC message."
 ;;;
 (cl-defmacro jsonrpc-lambda (cl-lambda-list &body body)
   (declare (indent 1) (debug (sexp &rest form)))
-  (let ((e (cl-gensym "jsonrpc-lambda-elem")))
+  (let ((e (funcall (if (fboundp 'gensym) 'gensym 'cl-gensym)
+                    "jsonrpc-lambda-elem")))
     `(lambda (,e) (apply (cl-function (lambda ,cl-lambda-list ,@body)) ,e))))
 
 (defun jsonrpc-events-buffer (connection)
@@ -405,7 +406,9 @@ remote endpoint (normal or error) are ignored and the function exits
 returning CANCEL-ON-INPUT-RETVAL.  If CANCEL-ON-INPUT is a function, it
 is invoked with one argument, an integer identifying the canceled
 request as specified in the JSONRPC 2.0 spec."
-  (let* ((tag (cl-gensym "jsonrpc-request-catch-tag")) id-and-timer
+  (let* ((tag (funcall (if (fboundp 'gensym) 'gensym 'cl-gensym)
+                       "jsonrpc-request-catch-tag"))
+         id-and-timer
          canceled
          (throw-on-input nil)
          (retval
index 5fbfbb7899ed53c6ff8c4e9e12fb5464154d86cb..8a7e54a981c13b638852d806af5b2de2a9414734 100644 (file)
@@ -325,7 +325,7 @@ The two cases that are handled are:
                         (cddr f))))
       (if (and cl-closure-vars
                (cl--expr-contains-any body cl-closure-vars))
-          (let* ((new (mapcar #'cl-gensym cl-closure-vars))
+          (let* ((new (mapcar #'gensym cl-closure-vars))
                  (sub (cl-pairlis cl-closure-vars new)) (decls nil))
             (while (or (stringp (car body))
                        (eq (car-safe (car body)) 'interactive))
@@ -372,7 +372,7 @@ lexical closures as in Common Lisp.
           (cons (cons 'function #'cl--function-convert)
                  macroexpand-all-environment))))
     (if (not (get (car (last cl-closure-vars)) 'used))
-        ;; Turn (let ((foo (cl-gensym)))
+        ;; Turn (let ((foo (gensym)))
         ;;        (set foo <val>) ...(symbol-value foo)...)
         ;; into (let ((foo <val>)) ...(symbol-value 'foo)...).
         ;; This is good because it's more efficient but it only works with
index a4f28ce97c54a67043b627d74a95c2062a64d065..ac8f9c5fad6acaaabbadcac7a68116c5d11136d5 100644 (file)
@@ -60,7 +60,7 @@
 ;;; Code:
 
 (require 'dired)
-(require 'cl-lib)                      ; for cl-gensym
+(require 'cl-lib)
 
 ;; CUSTOMIZATIONS
 
@@ -176,7 +176,7 @@ this value can let another user see some of your images."
   (format "%s%s-%s.jpg"
           (thumbs-temp-dir)
           thumbs-temp-prefix
-          (cl-gensym "T")))
+          (gensym "T")))
 
 (defun thumbs-thumbsdir ()
   "Return the current thumbnails directory (from `thumbs-thumbsdir').
index f812b4c925dec5dd9ce768703acf82b9567002cf..df54c9bbf8eda302cbfc0b506103292eb5d3188e 100644 (file)
@@ -879,7 +879,7 @@ Honor `eglot-strict-mode'."
   "Function of args CL-LAMBDA-LIST for processing INTERFACE objects.
 Honor `eglot-strict-mode'."
   (declare (indent 1) (debug (sexp &rest form)))
-  (let ((e (cl-gensym "jsonrpc-lambda-elem")))
+  (let ((e (gensym "jsonrpc-lambda-elem")))
     `(lambda (,e) (cl-block nil (eglot--dbind ,cl-lambda-list ,e ,@body)))))
 
 (cl-defmacro eglot--dcase (obj &rest clauses)
@@ -925,12 +925,12 @@ treated as in `eglot--dbind'."
 
 (cl-defmacro eglot--when-live-buffer (buf &rest body)
   "Check BUF live, then do BODY in it." (declare (indent 1) (debug t))
-  (let ((b (cl-gensym)))
+  (let ((b (gensym)))
     `(let ((,b ,buf)) (if (buffer-live-p ,b) (with-current-buffer ,b ,@body)))))
 
 (cl-defmacro eglot--when-buffer-window (buf &body body)
   "Check BUF showing somewhere, then do BODY in it." (declare (indent 1) (debug t))
-  (let ((b (cl-gensym)))
+  (let ((b (gensym)))
     `(let ((,b ,buf))
        ;;notice the exception when testing with `ert'
        (when (or (get-buffer-window ,b) (ert-running-test))
@@ -3176,7 +3176,7 @@ may be called multiple times (respecting the protocol of
 (cl-defmacro eglot--collecting-xrefs ((collector) &rest body)
   "Sort and handle xrefs collected with COLLECTOR in BODY."
   (declare (indent 1) (debug (sexp &rest form)))
-  (let ((collected (cl-gensym "collected")))
+  (let ((collected (gensym "collected")))
     `(unwind-protect
          (let (,collected)
            (cl-flet ((,collector (xref) (push xref ,collected)))
@@ -4289,7 +4289,7 @@ at point.  With prefix argument, prompt for ACTION-KIND."
      collect (cl-loop
               for (_token regexp emitter) in grammar
               thereis (and (re-search-forward (concat "\\=" regexp) nil t)
-                           (list (cl-gensym "state-") emitter (match-string 0)))
+                           (list (gensym "state-") emitter (match-string 0)))
               finally (error "Glob '%s' invalid at %s" (buffer-string) (point))))))
 
 (cl-defun eglot--glob-fsm (states &key (exit 'eobp) noerror)
index fb751085583f3bfeec74af5d2b7e1304543cfe1b..150658f4972b149b04772d97bccc9337ad596095 100644 (file)
@@ -1444,7 +1444,7 @@ If it is running also stop it."
 ARGS is a keyword-value plist passed to the backend along
 with a report function."
   (flymake-log :debug "Running backend %s" backend)
-  (let ((run-token (cl-gensym "backend-token")))
+  (let ((run-token (gensym "backend-token")))
     (flymake--with-backend-state backend state
       (setf (flymake--state-running state) run-token
             (flymake--state-disabled state) nil
index a3118c9b5566631c9ef1749164a5abebab0c6de5..64ccdd2d1cedc2f2e7ecca31ee59b8d1138426ec 100644 (file)
@@ -961,21 +961,14 @@ See Bug#57915."
                          :b 1)))
 
 (ert-deftest cl-lib-test-gensym ()
-  ;; Since the expansion of `should' calls `cl-gensym' and thus has a
-  ;; side-effect on `cl--gensym-counter', we have to make sure all
-  ;; macros in our test body are expanded before we rebind
-  ;; `cl--gensym-counter' and run the body.  Otherwise, the test would
-  ;; fail if run interpreted.
-  (let ((body (byte-compile
-               '(lambda ()
-                  (should (equal (symbol-name (cl-gensym)) "G0"))
-                  (should (equal (symbol-name (cl-gensym)) "G1"))
-                  (should (equal (symbol-name (cl-gensym)) "G2"))
-                  (should (equal (symbol-name (cl-gensym "foo")) "foo3"))
-                  (should (equal (symbol-name (cl-gensym "bar")) "bar4"))
-                  (should (equal cl--gensym-counter 5))))))
+  (with-suppressed-warnings ((obsolete cl-gensym))
     (let ((cl--gensym-counter 0))
-      (funcall body))))
+      (should (equal (symbol-name (cl-gensym)) "G0"))
+      (should (equal (symbol-name (cl-gensym)) "G1"))
+      (should (equal (symbol-name (cl-gensym)) "G2"))
+      (should (equal (symbol-name (cl-gensym "foo")) "foo3"))
+      (should (equal (symbol-name (cl-gensym "bar")) "bar4"))
+      (should (equal cl--gensym-counter 5)))))
 
 (ert-deftest cl-the ()
   (should (eql (cl-the integer 42) 42))
index eec076da7238fb52878f46f8e0a76da9aed6bf99..2b6e09a4d166e366336b0d65124af920f516a0a4 100644 (file)
@@ -1236,7 +1236,7 @@ GUESSED-MAJOR-MODES-SYM are bound to the useful return values of
 `eglot--guess-contact'.  Unless the server program evaluates to
 \"a-missing-executable.exe\", this macro will assume it exists."
   (declare (indent 1) (debug t))
-  (let ((i-sym (cl-gensym)))
+  (let ((i-sym (gensym)))
     `(dolist (,i-sym '(nil t))
        (let ((,interactive-sym ,i-sym)
              (buffer-file-name "_")
index c4a2847285b7f440b84cb6455ef8c8578f3f2138..06edcdba703d5f9180aac9a20d618b62b99bb227 100644 (file)
@@ -1043,7 +1043,7 @@ evaluation of BODY."
 
 \f
 (ert-deftest elisp-shorthand-read-buffer ()
-  (let* ((gsym (downcase (symbol-name (cl-gensym "sh-"))))
+  (let* ((gsym (downcase (symbol-name (gensym "sh-"))))
          (shorthand-sname (format "s-%s" gsym))
          (expected (intern (format "shorthand-longhand-%s" gsym))))
     (cl-assert (not (intern-soft shorthand-sname)))
@@ -1057,7 +1057,7 @@ evaluation of BODY."
     (should (not (intern-soft shorthand-sname)))))
 
 (ert-deftest elisp-shorthand-read-from-string ()
-  (let* ((gsym (downcase (symbol-name (cl-gensym "sh-"))))
+  (let* ((gsym (downcase (symbol-name (gensym "sh-"))))
          (shorthand-sname (format "s-%s" gsym))
          (expected (intern (format "shorthand-longhand-%s" gsym))))
     (cl-assert (not (intern-soft shorthand-sname)))