emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Should mode commands be idempotent?


From: Stefan Monnier
Subject: Re: Should mode commands be idempotent?
Date: Wed, 20 Sep 2017 18:25:07 -0400
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/26.0.50 (gnu/linux)

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




reply via email to

[Prev in Thread] Current Thread [Next in Thread]