all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Stefan Monnier <monnier@iro.umontreal.ca>
To: emacs-devel@gnu.org
Subject: Re: Should mode commands be idempotent?
Date: Wed, 20 Sep 2017 18:25:07 -0400	[thread overview]
Message-ID: <jwvzi9pxb6m.fsf-monnier+gmane.emacs.devel@gnu.org> (raw)
In-Reply-To: 00b0c4e4-a02f-8c9f-ef52-7ad5a65798d1@gmail.com

>> 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)




  parent reply	other threads:[~2017-09-20 22:25 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-09-19 19:58 Should mode commands be idempotent? Philipp Stephani
2017-09-19 22:10 ` Clément Pit-Claudel
2017-09-19 22:29   ` Stefan Monnier
2017-09-20  7:24     ` Clément Pit-Claudel
2017-09-20 14:52       ` John Wiegley
2017-09-20 22:30         ` Stefan Monnier
2017-09-20 23:05           ` Drew Adams
2017-10-08 15:09         ` Philipp Stephani
2017-09-20 22:25       ` Stefan Monnier [this message]
2017-09-23  8:16         ` Clément Pit-Claudel
2017-09-23 14:17           ` Stefan Monnier
2017-09-23 20:37             ` Stefan Monnier
2017-09-19 22:58 ` Drew Adams
2017-09-20  3:10   ` Stefan Monnier
2017-09-20 17:52     ` Drew Adams
2017-09-21  1:11       ` Stefan Monnier
2017-09-21  5:22         ` Drew Adams
2017-09-21 18:28           ` Richard Stallman
     [not found]         ` <<9f11a3c6-b113-4bf6-9dab-f894b2ad77b5@default>
     [not found]           ` <<E1dv6D1-0006Jr-Fl@fencepost.gnu.org>
2017-09-22 15:54             ` Drew Adams
2017-09-23  0:39               ` Richard Stallman
2017-09-23  8:05               ` Clément Pit-Claudel
2017-09-24 17:26                 ` Drew Adams
2017-09-25  7:03                   ` Clément Pit-Claudel
2017-09-25 13:57                     ` Drew Adams
2017-09-26  0:36                       ` Stefan Monnier
2017-09-26  3:30                         ` John Wiegley
2017-09-26 17:55                           ` Drew Adams
2017-09-26 18:01                             ` John Wiegley
2017-09-26 18:50                               ` Drew Adams
2017-09-26 18:54                                 ` John Wiegley
2017-10-08 15:28                                   ` Philipp Stephani
2017-10-08 19:04                                     ` Stefan Monnier
2017-10-10  2:10                                       ` John Wiegley
2017-12-21 20:49                                       ` Philipp Stephani
2017-12-22  2:20                                         ` Stefan Monnier
2017-10-10 23:15                                   ` Herring, Davis
     [not found]               ` <<E1dvYUB-0007na-Mx@fencepost.gnu.org>
2017-09-24 17:26                 ` Drew Adams
2017-09-25 22:06                   ` Richard Stallman
2017-09-19 23:50 ` John Wiegley
2017-09-20  3:09   ` Stefan Monnier
2017-09-20 13:01 ` Richard Stallman

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=jwvzi9pxb6m.fsf-monnier+gmane.emacs.devel@gnu.org \
    --to=monnier@iro.umontreal.ca \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.