Skip to content

Commit 297d3d2

Browse files
committed
* lisp/subr.el (dlet): New macro
* lisp/calendar/calendar.el (calendar-dlet*): Use it.
1 parent a98c8f5 commit 297d3d2

File tree

3 files changed

+34
-11
lines changed

3 files changed

+34
-11
lines changed

etc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,8 @@ This is no longer supported, and setting this variable has no effect.
216216

217217
* Lisp Changes in Emacs 28.1
218218

219+
** New macro 'dlet' to dynamically bind variables
220+
219221
** The variable 'force-new-style-backquotes' has been removed.
220222
This removes the final remaining trace of old-style backquotes.
221223

lisp/calendar/calendar.el

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,13 @@
136136
;; - whatever is passed to diary-remind
137137

138138
(defmacro calendar-dlet* (binders &rest body)
139-
"Like `let*' but using dynamic scoping."
139+
"Like `dlet' but without warnings about non-prefixed var names."
140140
(declare (indent 1) (debug let))
141-
`(progn
142-
(with-no-warnings ;Silence "lacks a prefix" warnings!
143-
,@(mapcar (lambda (binder)
144-
`(defvar ,(if (consp binder) (car binder) binder)))
145-
binders))
146-
(let* ,binders ,@body)))
141+
(let ((vars (mapcar (lambda (binder)
142+
(if (consp binder) (car binder) binder))
143+
binders)))
144+
`(with-suppressed-warnings ((lexical ,@vars))
145+
(dlet ,binders ,@body))))
147146

148147
;; Avoid recursive load of calendar when loading cal-menu. Yuck.
149148
(provide 'calendar)

lisp/subr.el

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1777,6 +1777,21 @@ all symbols are bound before any of the VALUEFORMs are evalled."
17771777
,@(mapcar (lambda (binder) `(setq ,@binder)) binders)
17781778
,@body))
17791779

1780+
(defmacro dlet (binders &rest body)
1781+
"Like `let*' but using dynamic scoping."
1782+
(declare (indent 1) (debug let))
1783+
;; (defvar FOO) only affects the current scope, but in order for
1784+
;; this not to affect code after the `let*' we need to create a new scope,
1785+
;; which is what the surrounding `let' is for.
1786+
;; FIXME: (let () ...) currently doesn't actually create a new scope,
1787+
;; which is why we use (let (_) ...).
1788+
`(let (_)
1789+
,@(mapcar (lambda (binder)
1790+
`(defvar ,(if (consp binder) (car binder) binder)))
1791+
binders)
1792+
(let* ,binders ,@body)))
1793+
1794+
17801795
(defmacro with-wrapper-hook (hook args &rest body)
17811796
"Run BODY, using wrapper functions from HOOK with additional ARGS.
17821797
HOOK is an abnormal hook. Each hook function in HOOK \"wraps\"
@@ -2972,13 +2987,14 @@ This finishes the change group by reverting all of its changes."
29722987
;; the body of `atomic-change-group' all changes can be undone.
29732988
(widen)
29742989
(let ((old-car (car-safe elt))
2975-
(old-cdr (cdr-safe elt)))
2990+
(old-cdr (cdr-safe elt))
2991+
(start-pul pending-undo-list))
29762992
(unwind-protect
29772993
(progn
29782994
;; Temporarily truncate the undo log at ELT.
29792995
(when (consp elt)
29802996
(setcar elt nil) (setcdr elt nil))
2981-
(unless (eq last-command 'undo) (undo-start))
2997+
(setq pending-undo-list buffer-undo-list)
29822998
;; Make sure there's no confusion.
29832999
(when (and (consp elt) (not (eq elt (last pending-undo-list))))
29843000
(error "Undoing to some unrelated state"))
@@ -2991,7 +3007,13 @@ This finishes the change group by reverting all of its changes."
29913007
;; Reset the modified cons cell ELT to its original content.
29923008
(when (consp elt)
29933009
(setcar elt old-car)
2994-
(setcdr elt old-cdr))))))))
3010+
(setcdr elt old-cdr)))
3011+
;; Let's not break a sequence of undos just because we
3012+
;; tried to make a change and then undid it: preserve
3013+
;; the original `pending-undo-list' if it's still valid.
3014+
(if (eq (undo--last-change-was-undo-p buffer-undo-list)
3015+
start-pul)
3016+
(setq pending-undo-list start-pul)))))))
29953017

29963018
;;;; Display-related functions.
29973019

@@ -3970,7 +3992,7 @@ the function `undo--wrap-and-run-primitive-undo'."
39703992
(let (;; (inhibit-modification-hooks t)
39713993
(before-change-functions
39723994
;; Ugly Hack: if the body uses syntax-ppss/syntax-propertize
3973-
;; (e.g. via a regexp-search or sexp-movement trigerring
3995+
;; (e.g. via a regexp-search or sexp-movement triggering
39743996
;; on-the-fly syntax-propertize), make sure that this gets
39753997
;; properly refreshed after subsequent changes.
39763998
(if (memq #'syntax-ppss-flush-cache before-change-functions)

0 commit comments

Comments
 (0)