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

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

[elpa] externals/valign 1d915f7 024/198: Merge pull request #1 from tuma


From: Stefan Monnier
Subject: [elpa] externals/valign 1d915f7 024/198: Merge pull request #1 from tumashu/master
Date: Tue, 1 Dec 2020 18:19:09 -0500 (EST)

branch: externals/valign
commit 1d915f7f1dda9795cde3ed4bc4378edc991206f8
Merge: f4235e7 eea08cc
Author: Yuan Fu <casouri@gmail.com>
Commit: GitHub <noreply@github.com>

    Merge pull request #1 from tumashu/master
    
    Add valign-mode.
---
 valign.el | 69 ++++++++++++++++++++++++++++++++++++++++++++-------------------
 1 file changed, 48 insertions(+), 21 deletions(-)

diff --git a/valign.el b/valign.el
index fbde10b..50d446c 100644
--- a/valign.el
+++ b/valign.el
@@ -1,6 +1,10 @@
 ;;; valign.el --- Visually align tables      -*- lexical-binding: t; -*-
 
 ;; Author: Yuan Fu <casouri@gmail.com>
+;; URL: https://github.com/casouri/valign
+;; Version: 0.1.0
+;; Keywords: convenience
+;; Package-Requires: ((emacs "26.0"))
 
 ;;; This file is NOT part of GNU Emacs
 
@@ -8,15 +12,15 @@
 ;;
 ;; This package provides visual alignment for Org tables on GUI Emacs.
 ;; It can properly align tables containing variable-pitch font, CJK
-;; characters and images. In the meantime, the text-based alignment
+;; characters and images.  In the meantime, the text-based alignment
 ;; generated by Org mode is left untouched.
 ;;
-;; To use this package, load it and run M-x valign-setup RET. And any
-;; Org tables in Org mode should be automatically aligned. If you want
+;; To use this package, load it and run M-x valign-setup RET.  And any
+;; Org tables in Org mode should be automatically aligned.  If you want
 ;; to align a table manually, run M-x valign-table RET on a Org table.
 ;;
 ;; Valign provides two styles of separator, |-----|-----|, and
-;; |           |. Customize ‘valign-separator-row-style’ to set a
+;; |           |.  Customize ‘valign-separator-row-style’ to set a
 ;; style.
 
 ;;; Code:
@@ -25,6 +29,11 @@
 (require 'cl-lib)
 (require 'pcase)
 
+(defcustom valign-lighter " valign"
+  "The lighter string used by function `valign-mode'."
+  :group 'valign
+  :type 'string)
+
 ;;; Backstage
 
 (define-error 'valign-bad-cell "Valign encountered a invalid table cell")
@@ -77,7 +86,7 @@ Return nil if not in a cell."
 
 (defun valign--glyph-width-at-point (&optional point)
   "Return the pixel width of the glyph at POINT.
-The buffer has to be visible. If point is at an image, this
+The buffer has to be visible.  If point is at an image, this
 function doens’t return the image’s width, but the underlining
 character’s glyph width."
   (let* ((p (or point (point))))
@@ -88,7 +97,7 @@ character’s glyph width."
 
 (defun valign--pixel-width-from-to (from to)
   "Return the width of the glyphs from FROM (inclusive) to TO (exclusive).
-The buffer has to be visible. FROM has to be less than TO. Unlike
+The buffer has to be visible.  FROM has to be less than TO.  Unlike
 ‘valign--glyph-width-at-point’, this function can properly
 calculate images pixel width."
   (let ((width 0))
@@ -202,7 +211,7 @@ Start from point, stop at LIMIT."
 
 (defun valign--beginning-of-table ()
   "Go backward to the beginning of the table at point.
-Assumes point is on a table. Return nil if failed, point
+Assumes point is on a table.  Return nil if failed, point
 otherwise."
   (beginning-of-line)
   (if (not (eq (char-after) ?|))
@@ -215,7 +224,7 @@ otherwise."
 
 (defun valign--end-of-table ()
   "Go forward to the end of the table at point.
-Assumes point is on a table. Return nil if failed, point
+Assumes point is on a table.  Return nil if failed, point
 otherwise."
   (beginning-of-line)
   (if (not (eq (char-after) ?|))
@@ -280,7 +289,7 @@ Force align if FORCE non-nil."
 
 (defun valign--align-separator-row (total-width)
   "Align the separator row (|---+---|) as “|        |”.
-Assumes the point is after the left bar (“|”). TOTAL-WIDTH is the
+Assumes the point is after the left bar (“|”).  TOTAL-WIDTH is the
 pixel width counting from the left of the left bar to the left of
 the right bar."
   (let ((p (point)))
@@ -324,7 +333,7 @@ the position for the right bar (“|”)."
 (defcustom valign-separator-row-style 'multi-column
   "The style of the separator row of a table.
 Valign can render it as “|          |”
-or as “|-----|-----|”. Set this option to 'single-column
+or as “|-----|-----|”.  Set this option to 'single-column
 for the former, and 'multi-column for the latter."
   :type 'symbol
   :group 'valign)
@@ -437,23 +446,41 @@ for the former, and 'multi-column for the latter."
                    (valign--separator-row-add-overlay
                     p (1- (point))
                     (or (nth col-idx (reverse rev-list)) 0)))))))))
-    
+
     (valign-bad-cell (message (error-message-string err)))
     (valign-werid-alignment (message (error-message-string err)))))
 
+(defun valign-org-mode-hook ()
+  "Valign hook function use by `org-mode'."
+  (add-hook 'jit-lock-functions
+            #'valign-initial-alignment 90 t))
+
+(defun valign-org-agenda-finalize-hook ()
+  "Valign hook function use by `org-agenda-finalize-hook'."
+  (valign-initial-alignment
+   (point-min) (point-max) t))
+
+(define-minor-mode valign-mode
+  "valign minor mode."
+  :global t
+  :require 'valign
+  :group 'valign
+  :lighter valign-lighter
+  (if (and valign-mode window-system)
+      (progn
+        (add-hook 'org-mode-hook #'valign-org-mode-hook)
+        (add-hook 'org-agenda-finalize-hook #'valign-org-agenda-finalize-hook)
+        (advice-add 'org-table-next-field :after #'valign-table)
+        (advice-add 'org-table-previous-field :after #'valign-table))
+    (remove-hook 'org-mode-hook #'valign-org-mode-hook)
+    (remove-hook 'org-agenda-finalize-hook #'valign-org-agenda-finalize-hook)
+    (advice-remove 'org-table-next-field #'valign-table)
+    (advice-remove 'org-table-previous-field #'valign-table)))
+
 (defun valign-setup ()
   "Enable visual table alignment."
   (interactive)
-  (when window-system
-    (add-hook 'org-mode-hook
-              (lambda ()
-                (add-hook 'jit-lock-functions
-                          #'valign-initial-alignment 90 t)))
-    (advice-add #'org-table-next-field :after #'valign-table)
-    (advice-add #'org-table-previous-field :after #'valign-table)
-    (add-hook 'org-agenda-finalize-hook
-              (lambda () (valign-initial-alignment
-                          (point-min) (point-max) t)))))
+  (valign-mode 1))
 
 (provide 'valign)
 



reply via email to

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