[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: dynamic variables and advice
From: |
Stefan Monnier |
Subject: |
Re: dynamic variables and advice |
Date: |
Thu, 03 Mar 2022 10:03:22 -0500 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/29.0.50 (gnu/linux) |
> 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