From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Madhu Newsgroups: gmane.emacs.help Subject: dynamic variables and advice Date: Thu, 03 Mar 2022 12:10:42 +0530 (IST) Message-ID: <20220303.121042.104668854711893143.enometh@meer.net> Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="10383"; mail-complaints-to="usenet@ciao.gmane.io" To: "help-gnu-emacs@gnu.org" Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Thu Mar 03 15:24:02 2022 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nPmN7-0002Pl-VN for geh-help-gnu-emacs@m.gmane-mx.org; Thu, 03 Mar 2022 15:24:01 +0100 Original-Received: from localhost ([::1]:42246 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nPmN6-00055L-83 for geh-help-gnu-emacs@m.gmane-mx.org; Thu, 03 Mar 2022 09:24:00 -0500 Original-Received: from eggs.gnu.org ([209.51.188.92]:54290) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nPfIK-0004yV-KO for help-gnu-emacs@gnu.org; Thu, 03 Mar 2022 01:50:36 -0500 Original-Received: from smtp6.ctinetworks.com ([205.166.61.199]:37090) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nPfIJ-00060I-5n for help-gnu-emacs@gnu.org; Thu, 03 Mar 2022 01:50:36 -0500 Original-Received: from localhost (unknown [117.193.2.130]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: enometh@meer.net) by smtp6.ctinetworks.com (Postfix) with ESMTPSA id 9C942860A2 for ; Thu, 3 Mar 2022 01:40:50 -0500 (EST) X-Mailer: Mew version 6.8 on Emacs 29.0.50 X-ctinetworks-Information: Please contact the ISP for more information X-ctinetworks-MailScanner-ID: 9C942860A2.A8DF1 X-ctinetworks-VirusCheck: Found to be clean X-ctinetworks-Watermark: 1647153655.76444@az8o7KlmF/0s4zxFfewNOQ Received-SPF: pass client-ip=205.166.61.199; envelope-from=enometh@meer.net; helo=smtp6.ctinetworks.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-Mailman-Approved-At: Thu, 03 Mar 2022 09:23:15 -0500 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.io gmane.emacs.help:136337 Archived-At: 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?