From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Jonas Bernoulli Newsgroups: gmane.emacs.bugs Subject: bug#61176: post-command-hook is not run if minibuffer input is aborted Date: Mon, 30 Jan 2023 16:07:30 +0100 Message-ID: <87y1pk2h8t.fsf@bernoul.li> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="29533"; mail-complaints-to="usenet@ciao.gmane.io" To: 61176@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Mon Jan 30 16:08:17 2023 Return-path: Envelope-to: geb-bug-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 1pMVlZ-0007WN-3C for geb-bug-gnu-emacs@m.gmane-mx.org; Mon, 30 Jan 2023 16:08:17 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pMVlL-0006bE-JY; Mon, 30 Jan 2023 10:08:04 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pMVlK-0006b3-GP for bug-gnu-emacs@gnu.org; Mon, 30 Jan 2023 10:08:02 -0500 Original-Received: from debbugs.gnu.org ([209.51.188.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pMVlK-0003N1-84 for bug-gnu-emacs@gnu.org; Mon, 30 Jan 2023 10:08:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1pMVlK-0006gG-2g for bug-gnu-emacs@gnu.org; Mon, 30 Jan 2023 10:08:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Jonas Bernoulli Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Mon, 30 Jan 2023 15:08:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 61176 X-GNU-PR-Package: emacs X-Debbugs-Original-To: bug-gnu-emacs@gnu.org Original-Received: via spool by submit@debbugs.gnu.org id=B.167509126725646 (code B ref -1); Mon, 30 Jan 2023 15:08:01 +0000 Original-Received: (at submit) by debbugs.gnu.org; 30 Jan 2023 15:07:47 +0000 Original-Received: from localhost ([127.0.0.1]:50082 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pMVl4-0006fa-Li for submit@debbugs.gnu.org; Mon, 30 Jan 2023 10:07:47 -0500 Original-Received: from lists.gnu.org ([209.51.188.17]:47028) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pMVl3-0006fS-1h for submit@debbugs.gnu.org; Mon, 30 Jan 2023 10:07:45 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pMVl1-0006W5-Em for bug-gnu-emacs@gnu.org; Mon, 30 Jan 2023 10:07:43 -0500 Original-Received: from mail.hostpark.net ([212.243.197.30]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pMVkv-0003LV-0Q for bug-gnu-emacs@gnu.org; Mon, 30 Jan 2023 10:07:38 -0500 Original-Received: from localhost (localhost [127.0.0.1]) by mail.hostpark.net (Postfix) with ESMTP id C5F611625E for ; Mon, 30 Jan 2023 16:07:32 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=bernoul.li; h= content-transfer-encoding:content-type:content-type:mime-version :message-id:date:date:subject:subject:from:from:received :received; s=sel2011a; t=1675091252; bh=xm8mU2aFUjHiR8tVFx7pZjAx 7BqTANmqH7awNFrRWdc=; b=RnchppOXBEDK6h5Wpik69AKGNVJqWcrfOhNUNsMW opOD3jFfjEJZFvoZ+6KPQDQ4E1MxebI8rJIqllDln8f6oUwp5lSL09UudDQEZOW3 vWh0onZBgeeWfstqd7ylLgXVQnhKBL6iQclg0zs6VEV9egJiZz+MOdt124ClG5nn 6oI= X-Virus-Scanned: by Hostpark/NetZone Mailprotection at hostpark.net Original-Received: from mail.hostpark.net ([127.0.0.1]) by localhost (mail1.hostpark.net [127.0.0.1]) (amavisd-new, port 10224) with ESMTP id UTqG6mhsipxH for ; Mon, 30 Jan 2023 16:07:32 +0100 (CET) Original-Received: from customer (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mail.hostpark.net (Postfix) with ESMTPSA id 8442F1625C for ; Mon, 30 Jan 2023 16:07:32 +0100 (CET) Received-SPF: none client-ip=212.243.197.30; envelope-from=jonas@bernoul.li; helo=mail.hostpark.net X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_NONE=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:254442 Archived-At: > -- Variable: post-command-hook > This normal hook is run by the editor command loop after it > executes each command (including commands terminated prematurely by > quitting or by errors). At that time, =E2=80=98this-command=E2=80=99= refers to the > command that just ran, and =E2=80=98last-command=E2=80=99 refers to t= he command > before that. > > This hook is also run when Emacs first enters the command loop (at > which point =E2=80=98this-command=E2=80=99 and =E2=80=98last-command= =E2=80=99 are both =E2=80=98nil=E2=80=99). - post-command-hook is run even if a command is "terminated prematurely by quitting or by errors". This is very useful when it is crucial that some cleanup is run even when something goes wrong. - When a command uses the minibuffer, then post-command-hook is additionally run when the minibuffer is setup, with this-command being the command that uses the minibuffer. This happens before minibuffer-setup-hook is run. This is surprising, because undocumented, but easy to detect, because at this time this-command-keys-vector returns an empty vector. Never- the-less this should be documented (instead of being "fixed"; I depend on this behavior, and so might others). - However, when the command reads from the minibuffer and the user aborts that, then post-command-hook is NOT run a second time AFTER the command. This is extremely inconvenient. IMO, the fact that this hook is documented to run even if the command "terminated prematurely by quitting or by errors", implies that the hook is run even if the quitting is done intentionally by the user. This could be fixed simply by running post-command-hook in this case as well. If that is considered a dangerous change in behavior, then maybe a very similar hook --say unwind-command-hook-- could be added. Cheers, Jonas Oh -- here's some code that can be used to observe this behavior: (keymap-global-set "" '-command) (keymap-global-set "" '-prepare) (keymap-global-set "" '-cleanup) (defun -prepare () (interactive) (add-hook 'post-command-hook '-post) (add-hook 'minibuffer-setup-hook '-setup) (add-hook 'minibuffer-exit-hook '-exit)) (defun -cleanup () (interactive) (remove-hook 'post-command-hook '-post) (remove-hook 'minibuffer-setup-hook '-setup) (remove-hook 'minibuffer-exit-hook '-exit)) (defun -post () (message ";; -post (%-10s %s)" (this-command-keys-vector) this-command= )) (defun -setup () (message ";; -setup (%-10s %s)" (this-command-keys-vector) this-command= )) (defun -exit () (message ";; -exit (%-10s %s)" (this-command-keys-vector) this-command= )) (defun -command () (interactive) (message ";; -command")) ;; -command ;; -post ([f1] -command) (defun -command () (interactive) (message ";; -command") (error "error in command")) ;; -command ;; -command: error in command ;; -post ([] -command) (defun -command () (interactive (error "error in interactive")) (message ";; -command")) ;; call-interactively: error in interactive ;; -post ([] -command) (defun -command (arg) (interactive (list (read-string ": "))) (message ";; -command")) ;; -setup ([f1] -command) ;; -post ([] -command) ;; -post ([97] self-insert-command) ;; -exit ([return] exit-minibuffer) ;; -command ;; -post ([f1] -command) ;; -setup ([f1] -command) ;; -post ([] -command) ;; -exit ([7] abort-minibuffers) ;; Quit ;; -post ([] abort-minibuffers) (defun -command () (interactive) (message ";; -command") (read-string "-command: ")) ;; -setup ([f1] -command) ;; -post ([] -command) ;; -post ([97] self-insert-command) ;; -exit ([return] exit-minibuffer) ;; -post ([f1] exit-minibuffer) ;; -setup ([f1] -command) ;; -post ([] -command) ;; -exit ([return] exit-minibuffer) ;; -post ([f1] exit-minibuffer) (defun -command (arg) (interactive (list (read-from-minibuffer ": " nil (let ((map (make-sparse-keymap))) (set-keymap-parent map minibuffer-local-map) (define-key map "a" (lambda () (interactive) (error "error in minibuffer"))) map)))) (message ";; -command")) ;; -setup ([f1] -command) ;; -post ([] -command) ;; (lambda nil (interactive) (error error in minibuffer)): error in minibuf= fer ;; -post ([] (lambda nil (interactive) (error error in minibuffe= r))) ;; -exit ([return] exit-minibuffer) ;; -command ;; -post ([f1] -command)