From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Stefan Monnier Newsgroups: gmane.emacs.devel Subject: Re: Should mode commands be idempotent? Date: Wed, 20 Sep 2017 18:25:07 -0400 Message-ID: References: <5358b04b-70cb-bbaf-1887-bd83613e9c2b@gmail.com> <00b0c4e4-a02f-8c9f-ef52-7ad5a65798d1@gmail.com> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: blaine.gmane.org 1505946326 24666 195.159.176.226 (20 Sep 2017 22:25:26 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Wed, 20 Sep 2017 22:25:26 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.0.50 (gnu/linux) To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Thu Sep 21 00:25:23 2017 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dunQp-0006D2-2C for ged-emacs-devel@m.gmane.org; Thu, 21 Sep 2017 00:25:23 +0200 Original-Received: from localhost ([::1]:50930 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dunQw-0000wL-A8 for ged-emacs-devel@m.gmane.org; Wed, 20 Sep 2017 18:25:30 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:57626) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dunQo-0000w9-2s for emacs-devel@gnu.org; Wed, 20 Sep 2017 18:25:23 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dunQk-0004C8-Tv for emacs-devel@gnu.org; Wed, 20 Sep 2017 18:25:22 -0400 Original-Received: from [195.159.176.226] (port=43359 helo=blaine.gmane.org) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dunQk-00046q-Mj for emacs-devel@gnu.org; Wed, 20 Sep 2017 18:25:18 -0400 Original-Received: from list by blaine.gmane.org with local (Exim 4.84_2) (envelope-from ) id 1dunQZ-0005MU-0V for emacs-devel@gnu.org; Thu, 21 Sep 2017 00:25:07 +0200 X-Injected-Via-Gmane: http://gmane.org/ Original-Lines: 52 Original-X-Complaints-To: usenet@blaine.gmane.org Cancel-Lock: sha1:jCXnrq1brO9xom7JtiXpxfxZSu0= X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 195.159.176.226 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.org gmane.emacs.devel:218609 Archived-At: >> It shouldn't be needed: the idempotence should emerge naturally from the >> way the code is written, rather than being the result of special tests >> to detect that particular situation. > I'm not too sure: take the example of visual-line-mode: how do you make that > idempotent without explicitly checking whether the mode has already > been activated? In this case I'd typically do something like the patch below, Stefan diff --git a/lisp/simple.el b/lisp/simple.el index a3d1cc3864..ed0a29bbcc 100644 --- a/lisp/simple.el +++ b/lisp/simple.el @@ -7053,22 +7053,21 @@ visual-line-mode :lighter " Wrap" (if visual-line-mode (progn - (set (make-local-variable 'visual-line--saved-state) nil) - ;; Save the local values of some variables, to be restored if - ;; visual-line-mode is turned off. - (dolist (var '(line-move-visual truncate-lines - truncate-partial-width-windows - word-wrap fringe-indicator-alist)) - (if (local-variable-p var) - (push (cons var (symbol-value var)) - visual-line--saved-state))) + (unless visual-line--saved-state + ;; Save the local values of some variables, to be restored if + ;; visual-line-mode is turned off. + (dolist (var '(line-move-visual truncate-lines + truncate-partial-width-windows + word-wrap fringe-indicator-alist)) + (if (local-variable-p var) + (push (cons var (symbol-value var)) + visual-line--saved-state)))) (set (make-local-variable 'line-move-visual) t) (set (make-local-variable 'truncate-partial-width-windows) nil) (setq truncate-lines nil - word-wrap t - fringe-indicator-alist - (cons (cons 'continuation visual-line-fringe-indicators) - fringe-indicator-alist))) + word-wrap t) + (add-to-list 'fringe-indicator-alist + (cons 'continuation visual-line-fringe-indicators))) (kill-local-variable 'line-move-visual) (kill-local-variable 'word-wrap) (kill-local-variable 'truncate-lines)