* dynamic variables and advice
@ 2022-03-03 6:40 Madhu
2022-03-03 15:03 ` Stefan Monnier via Users list for the GNU Emacs text editor
0 siblings, 1 reply; 3+ messages in thread
From: Madhu @ 2022-03-03 6:40 UTC (permalink / raw)
To: help-gnu-emacs@gnu.org
I was trying to use the add-function facility to make dired and friends
prompt only for directories. the naive approach with a lot of printf
debugging -
#+begin_src elisp
(defvar read-file-name--directories-p nil
"dynamic variable which should be let-bound in an :around advice
and should be used in a :filter-args advice.")
(defun read-file-name--maybe-filter-directories (args)
"filter-args advice for read-file-name."
(cl-destructuring-bind (prompt &optional dir default-filename
mustmatch initial predicate)
args
(let ((ret (list prompt dir default-filename mustmatch initial
(or predicate
(and read-file-name--maybe-filter-directories-p
#'file-directory-p)))))
(message "rfn-mfd: rfnmfd=%s this-command=%s ret=%s"
read-file-name--maybe-filter-directories-p this-command ret)
ret)))
(advice-add 'read-file-name
:filter-args 'read-file-name--maybe-filter-directories)
(defun rfnmfd-yes (oldfun &rest args)
"an around advice function which sets rfn-mfd-p to t"
(let ((read-file-name--mabe-filter-directories-p t))
(message "rfnmfd-yes: begin")
(prog1 (apply oldfun args)
(message "rfnmfd-yes: done"))))
(advice-add 'dired-read-dir-and-switches :around 'rfnmfd-yes)
#+end_src
Now when I call M-x dired the around advice for
dired-read-dir-and-switches runs, and it binds
read-file-name--directories-p to t and eventually goes on to call
read-file-name where the filter-args advice kicks in.
but in filter-args advice my dynamic variable is bound to nil and not
to t as I'd expect.
How is my expectation wrong or what is actually happening?
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: dynamic variables and advice
2022-03-03 6:40 dynamic variables and advice Madhu
@ 2022-03-03 15:03 ` Stefan Monnier via Users list for the GNU Emacs text editor
2022-03-04 2:48 ` Madhu
0 siblings, 1 reply; 3+ messages in thread
From: Stefan Monnier via Users list for the GNU Emacs text editor @ 2022-03-03 15:03 UTC (permalink / raw)
To: help-gnu-emacs
> I was trying to use the add-function facility to make dired and friends
> prompt only for directories. the naive approach with a lot of printf
> debugging -
>
> #+begin_src elisp
> (defvar read-file-name--directories-p nil
> "dynamic variable which should be let-bound in an :around advice
> and should be used in a :filter-args advice.")
>
> (defun read-file-name--maybe-filter-directories (args)
> "filter-args advice for read-file-name."
> (cl-destructuring-bind (prompt &optional dir default-filename
> mustmatch initial predicate)
> args
> (let ((ret (list prompt dir default-filename mustmatch initial
> (or predicate
> (and read-file-name--maybe-filter-directories-p
> #'file-directory-p)))))
> (message "rfn-mfd: rfnmfd=%s this-command=%s ret=%s"
> read-file-name--maybe-filter-directories-p this-command ret)
> ret)))
>
> (advice-add 'read-file-name
> :filter-args 'read-file-name--maybe-filter-directories)
>
> (defun rfnmfd-yes (oldfun &rest args)
> "an around advice function which sets rfn-mfd-p to t"
> (let ((read-file-name--mabe-filter-directories-p t))
> (message "rfnmfd-yes: begin")
> (prog1 (apply oldfun args)
> (message "rfnmfd-yes: done"))))
>
> (advice-add 'dired-read-dir-and-switches :around 'rfnmfd-yes)
> #+end_src
>
> Now when I call M-x dired the around advice for
> dired-read-dir-and-switches runs, and it binds
> read-file-name--directories-p to t and eventually goes on to call
> read-file-name where the filter-args advice kicks in.
>
> but in filter-args advice my dynamic variable is bound to nil and not
> to t as I'd expect.
>
> How is my expectation wrong or what is actually happening?
A typo, "maybe"?
[ Pun intended. ]
Stefan
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2022-03-04 2:48 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-03-03 6:40 dynamic variables and advice Madhu
2022-03-03 15:03 ` Stefan Monnier via Users list for the GNU Emacs text editor
2022-03-04 2:48 ` Madhu
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).