[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/valign 9e1d2f4 152/198: Fix the problem where cells get
From: |
Stefan Monnier |
Subject: |
[elpa] externals/valign 9e1d2f4 152/198: Fix the problem where cells get too much padding |
Date: |
Tue, 1 Dec 2020 18:19:37 -0500 (EST) |
branch: externals/valign
commit 9e1d2f4fb3d17b2bee7ba4a6a0cf1cd22b5cfc57
Author: Yuan Fu <casouri@gmail.com>
Commit: Yuan Fu <casouri@gmail.com>
Fix the problem where cells get too much padding
valign-table -> vailgn-table-maybe
force align table -> valign-table
* valign.el (valign--cell-empty-p): New function.
(valign--cell-width): Return 0 for empty cells.
(valign--do-row, valign--do-column): Add edebug declare.
(valign-table): Rename to 'valign-table-maybe', add 'force' argument.
(valign-table-maybe): New function.
(valign--tab-advice): Don't need to modify text properties now.
(valign-region): Use 'valign-table-maybe'.
(valign-reset-buffer): Remove obsolete code.
---
valign.el | 52 ++++++++++++++++++++++++++++++++++++----------------
1 file changed, 36 insertions(+), 16 deletions(-)
diff --git a/valign.el b/valign.el
index d23b523..4e5774b 100644
--- a/valign.el
+++ b/valign.el
@@ -113,6 +113,13 @@ right bar."
(1+ content-end-strict))
cell-end)))))
+(defun valign--cell-empty-p ()
+ "Return non-nil if cell is empty.
+Assumes point is after the left bar (“|”)."
+ (save-excursion
+ (and (skip-chars-forward " ")
+ (looking-at "|"))))
+
(defun valign--cell-width ()
"Return the pixel width of the cell at point.
Assumes point is after the left bar (“|”).
@@ -126,8 +133,18 @@ Return nil if not in a cell."
;; EMPTY := <SPACE>+
;; NON-EMPTY := <SPACE>+<NON-SPACE>+<SPACE>+
;; DELIM := | or +
- (pcase-let ((`(,_a ,beg ,end ,_b) (valign--cell-content-config)))
- (valign--pixel-width-from-to beg end)))
+ ;;
+ ;; Sometimes, because of Org's table alignment, empty cell is longer
+ ;; than non-empty cell. This usually happens with CJK text, because
+ ;; CJK characters are shorter than 2x ASCII character but Org treats
+ ;; CJK characters as 2 ASCII characters when aligning. And if you
+ ;; have 16 CJK char in one cell, Org uses 32 ASCII spaces for the
+ ;; empty cell, which is longer than 16 CJK chars. So better regard
+ ;; empty cell as 0-width rather than measuring it's white spaces.
+ (if (valign--cell-empty-p)
+ 0
+ (pcase-let ((`(,_a ,beg ,end ,_b) (valign--cell-content-config)))
+ (valign--pixel-width-from-to beg end))))
;; We used to use a custom functions that calculates the pixel text
;; width that doesn’t require a live window. However that function
@@ -175,7 +192,8 @@ cell. We don’t distinguish between left and center
aligned."
At each row, stop at the beginning of the line. Start from point
and stop at LIMIT. ROW-IDX-SYM is bound to each row’s
index (0-based)."
- (declare (indent 2))
+ (declare (debug (sexp form &rest form))
+ (indent 2))
`(progn
(setq ,row-idx-sym 0)
(while (<= (point) ,limit)
@@ -189,7 +207,8 @@ index (0-based)."
Start from point and stop at the end of the line. Stop after the
cell bar (“|”) in each iteration.
COLUMN-IDX-SYM is bound to the index of the column (0-based)."
- (declare (indent 1))
+ (declare (debug (sexp &rest form))
+ (indent 1))
`(progn
(setq ,column-idx-sym 0)
(beginning-of-line)
@@ -474,13 +493,19 @@ You need to restart valign mode for this setting to take
effect."
(defun valign-table ()
"Visually align the table at point."
(interactive)
+ (valign-table-maybe t))
+
+(defun valign-table-maybe (&optional force)
+ "Visually align the table at point.
+If FORCE non-nil, force align."
(condition-case err
(save-excursion
- (when (and (display-graphic-p)
- (valign--at-table-p)
- (progn (valign--beginning-of-table)
- (text-property-any
- (point) (1+ (point)) 'valign-init nil)))
+ (when (or force
+ (and (display-graphic-p)
+ (valign--at-table-p)
+ (progn (valign--beginning-of-table)
+ (text-property-any
+ (point) (1+ (point)) 'valign-init nil))))
(valign-table-1)))
((valign-bad-cell search-failed error)
(valign--clean-text-property
@@ -580,7 +605,7 @@ Force align if FORCE non-nil."
(while (and (search-forward "|" nil t)
(< (point) end))
(condition-case err
- (valign-table)
+ (valign-table-maybe)
(error (message "Error when aligning table: %s"
(error-message-string err))))
(valign--end-of-table)))))
@@ -613,17 +638,12 @@ FLAG is the same as in ‘org-flag-region’."
(when-let ((on-table (valign--at-table-p))
(beg (progn (valign--beginning-of-table) (point)))
(end (progn (valign--end-of-table) (point))))
- (with-silent-modifications
- (put-text-property beg end 'valign-init nil)
- (valign-table))))))
+ (valign-table)))))
(defun valign-reset-buffer ()
"Remove alignment in the buffer."
- ;; TODO Use the new Emacs 27 function.
- ;; Remove text properties
(with-silent-modifications
(valign--clean-text-property (point-min) (point-max))
- (put-text-property (point-min) (point-max) 'font-lock-face nil)
(jit-lock-refontify)))
(defun valign-remove-advice ()
- [elpa] externals/valign 237293b 140/198: Change signal-catch to if-else, (continued)
- [elpa] externals/valign 237293b 140/198: Change signal-catch to if-else, Stefan Monnier, 2020/12/01
- [elpa] externals/valign 9554e4b 123/198: Clean up valign-table-1, Stefan Monnier, 2020/12/01
- [elpa] externals/valign a8d93c5 145/198: Fix markdown table alignment, Stefan Monnier, 2020/12/01
- [elpa] externals/valign a68750a 114/198: * valign.el (valign-table): Add missing docstring., Stefan Monnier, 2020/12/01
- [elpa] externals/valign 70b3004 141/198: * valign.el (valign-table-1): Minor change., Stefan Monnier, 2020/12/01
- [elpa] externals/valign 59dfb21 115/198: * valign.el (valign--fancy-bar-cursor-fn): Thinner bar when cursor is bar., Stefan Monnier, 2020/12/01
- [elpa] externals/valign 07585a7 142/198: Change advice, Stefan Monnier, 2020/12/01
- [elpa] externals/valign 9033d3b 128/198: Replace text property with overlay, Stefan Monnier, 2020/12/01
- [elpa] externals/valign efa9b3c 179/198: Refactor code that calculates column width, Stefan Monnier, 2020/12/01
- [elpa] externals/valign 27998c6 187/198: Update documentation, Stefan Monnier, 2020/12/01
- [elpa] externals/valign 9e1d2f4 152/198: Fix the problem where cells get too much padding,
Stefan Monnier <=
- [elpa] externals/valign 11f02f5 193/198: Allow non-table lines at the end of the table, Stefan Monnier, 2020/12/01
- [elpa] externals/valign fcb62c4 161/198: * valign.el (valign--glyph-width-of): Fix docstring., Stefan Monnier, 2020/12/01
- [elpa] externals/valign 70c5c91 147/198: * valign.el: Remove obsolete number regexp., Stefan Monnier, 2020/12/01
- [elpa] externals/valign 65859b3 177/198: Refactor out valign--cell, Stefan Monnier, 2020/12/01
- [elpa] externals/valign a0e40c5 197/198: Fix alignment in Markdown mode, Stefan Monnier, 2020/12/01
- [elpa] externals/valign 5b0c557 107/198: * README.org (Customization): Update., Stefan Monnier, 2020/12/01
- [elpa] externals/valign d37ce14 185/198: Add column padding, Stefan Monnier, 2020/12/01
- [elpa] externals/valign 58f5dc8 190/198: * valign.el: Update keywords., Stefan Monnier, 2020/12/01
- [elpa] externals/valign 666e7d7 175/198: * valign.el (valign--end-of-table): Fix infinite loop., Stefan Monnier, 2020/12/01
- [elpa] externals/valign 6a4a91b 159/198: Add a case for empty cells, Stefan Monnier, 2020/12/01