]> git.eshelyaron.com Git - emacs.git/commitdiff
dolist: support non-list spec
authorEshel Yaron <me@eshelyaron.com>
Tue, 25 Feb 2025 20:15:18 +0000 (21:15 +0100)
committerEshel Yaron <me@eshelyaron.com>
Tue, 25 Feb 2025 20:15:18 +0000 (21:15 +0100)
lisp/subr.el

index 1627bfdbb6b86b8deb9f4425dec4fcfc04e0a4b9..87b4cbf1e8caf9dd066c5e57e7bd2402ab2a4da9 100644 (file)
@@ -398,19 +398,26 @@ in compilation warnings about unused variables.
 
 \(fn (VAR COUNT [RESULT]) BODY...)"
   (declare (indent 1) (debug dolist))
-  (let ((var (nth 0 spec))
-        (end (nth 1 spec))
-        (upper-bound (make-symbol "upper-bound"))
+  (let ((upper-bound (make-symbol "upper-bound"))
         (counter (make-symbol "counter")))
-    `(let ((,upper-bound ,end)
-           (,counter 0))
-       (while (< ,counter ,upper-bound)
-         (let ((,var ,counter))
-           ,@body)
-         (setq ,counter (1+ ,counter)))
-       ,@(if (cddr spec)
-             ;; FIXME: This let often leads to "unused var" warnings.
-             `((let ((,var ,counter)) ,@(cddr spec)))))))
+    (if (consp spec)
+        (let ((var (nth 0 spec))
+              (end (nth 1 spec)))
+          `(let ((,upper-bound ,end)
+                 (,counter 0))
+             (while (< ,counter ,upper-bound)
+               (let ((,var ,counter))
+                 ,@body)
+               (setq ,counter (1+ ,counter)))
+             ,@(if (cddr spec)
+                   ;; FIXME: This let often leads to "unused var" warnings.
+                   `((let ((,var ,counter)) ,@(cddr spec))))))
+      ;; `spec' is a number.
+      `(let ((,upper-bound ,spec)
+             (,counter 0))
+         (while (< ,counter ,upper-bound)
+           ,@body
+           (setq ,counter (1+ ,counter)))))))
 
 (defmacro declare (&rest specs)
   "Do not evaluate any arguments, and return nil.