emacs-elpa-diffs
[Top][All Lists]
Advanced

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

[elpa] externals/valign bdb56ce 032/198: Sync all org mode alignment sta


From: Stefan Monnier
Subject: [elpa] externals/valign bdb56ce 032/198: Sync all org mode alignment state automatically
Date: Tue, 1 Dec 2020 18:19:11 -0500 (EST)

branch: externals/valign
commit bdb56cee1a2d7c0741d1db4a8e5c435042f565f8
Author: Yuan Fu <casouri@gmail.com>
Commit: Yuan Fu <casouri@gmail.com>

    Sync all org mode alignment state automatically
    
    Now when you enable or disable avalign-mode, you don’t need to revert
    existing Org buffers, they are aligned or unaligned automatically.
    
    * valign.el (valign-window-buffer-change-hook, valign-reset-buffer):
    New functions.
    (valign-mode): Add a hook window-buffer-change-functions.  Reset all
    Org buffers when disabling valign-mode.
---
 valign.el | 46 ++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 42 insertions(+), 4 deletions(-)

diff --git a/valign.el b/valign.el
index 652c116..28b9bc2 100644
--- a/valign.el
+++ b/valign.el
@@ -470,6 +470,33 @@ for the former, and 'multi-column for the latter."
   "Valign hook, realign table between BEG and END."
   (valign-initial-alignment beg end t))
 
+(defun valign-window-buffer-change-hook (frame)
+  "Makes sure all visible buffers in FRAME are aligned."
+  (dolist (window (window-list frame 'no-minibuf))
+    (with-current-buffer (window-buffer window)
+      (when (and (derived-mode-p 'org-mode)
+                 valign-mode
+                 (text-property-any (point-min) (point-max)
+                                    'valign-init nil))
+        (valign-initial-alignment (point-min) (point-max))))))
+
+(defun valign-reset-buffer ()
+  "Remove alignment in the buffer."
+  ;; TODO Use the new Emacs 27 function.
+  ;; Remove text properties
+  (with-silent-modifications
+    (let ((p (point-min)) (pp (point-min)) display)
+      (while (< p (point-max))
+        (setq display (plist-get (text-properties-at p) 'display))
+        (setq p (next-single-char-property-change p 'display))
+        (when (and (consp display)
+                   (eq (car display) 'space))
+          (put-text-property pp p 'display nil))))
+    ;; Remove overlays.
+    (dolist (ov (overlays-in (point-min) (point-max)))
+      (when (overlay-get ov 'valign)
+        (delete-overlay ov)))))
+
 (define-minor-mode valign-mode
   "Visually align Org tables."
   :global t
@@ -478,7 +505,10 @@ for the former, and 'multi-column for the latter."
   :lighter valign-lighter
   (if (and valign-mode window-system)
       (progn
-        (add-hook 'org-mode-hook #'valign--org-mode-hook)
+        (if (boundp 'window-buffer-change-functions)
+            (add-hook 'window-buffer-change-functions
+                      #'valign-window-buffer-change-hook)
+          (add-hook 'org-mode-hook #'valign--org-mode-hook))
         (add-hook 'org-agenda-finalize-hook #'valign--force-align-buffer)
         (advice-add 'org-toggle-inline-images
                     :after #'valign--force-align-buffer)
@@ -487,15 +517,23 @@ for the former, and 'multi-column for the latter."
         (advice-add 'visible-mode :after #'valign--force-align-buffer)
         (advice-add 'org-table-next-field :after #'valign-table)
         (advice-add 'org-table-previous-field :after #'valign-table)
-        (advice-add 'org-flag-region :after #'valign--org-flag-region-advice))
-    (remove-hook 'org-mode-hook #'valign--org-mode-hook)
+        (advice-add 'org-flag-region :after #'valign--org-flag-region-advice)
+        (valign-initial-alignment (point-min) (point-max)))
+    (if (boundp 'window-buffer-change-functions)
+        (remove-hook 'window-buffer-change-functions
+                     #'valign-window-buffer-change-hook)
+      (remove-hook 'org-mode-hook #'valign--org-mode-hook))
     (remove-hook 'org-agenda-finalize-hook #'valign--force-align-buffer)
     (advice-remove 'org-toggle-inline-images #'valign--force-align-buffer)
     (advice-remove 'org-restart-font-lock #'valign--force-align-buffer)
     (advice-remove 'visible-mode #'valign--force-align-buffer)
     (advice-remove 'org-table-next-field #'valign-table)
     (advice-remove 'org-table-previous-field #'valign-table)
-    (advice-remove 'org-flag-region #'valign--org-flag-region-advice)))
+    (advice-remove 'org-flag-region #'valign--org-flag-region-advice)
+    (dolist (buf (buffer-list))
+      (with-current-buffer buf
+        (when (derived-mode-p 'org-mode)
+          (valign-reset-buffer))))))
 
 (provide 'valign)
 



reply via email to

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