[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/valign 9554e4b 123/198: Clean up valign-table-1
From: |
Stefan Monnier |
Subject: |
[elpa] externals/valign 9554e4b 123/198: Clean up valign-table-1 |
Date: |
Tue, 1 Dec 2020 18:19:31 -0500 (EST) |
branch: externals/valign
commit 9554e4b41c8b06a43883d65e16aca9851d943fa2
Author: Yuan Fu <casouri@gmail.com>
Commit: Yuan Fu <casouri@gmail.com>
Clean up valign-table-1
* valign.el (valign-table-1): Move initialization of 'end',
'column-width-list', 'column-alignment-list' up. Move
row-initialization up, under 'valign--do-row'. Move separator check
up, under 'valign--do-row'. Rename 'pos' to 'column-start'.
---
valign.el | 104 +++++++++++++++++++++++++++-----------------------------------
1 file changed, 46 insertions(+), 58 deletions(-)
diff --git a/valign.el b/valign.el
index 8200fbc..28a83a2 100644
--- a/valign.el
+++ b/valign.el
@@ -482,42 +482,41 @@ You need to restart valign mode for this setting to take
effect."
(defun valign-table-1 ()
"Visually align the table at point."
- (let (end column-width-list column-idx pos
- column-alignment-list right-bar-pos row-idx
- (ssw (save-excursion
- (search-forward " ")
- (valign--pixel-width-from-to
- (match-beginning 0) (match-end 0))))
- (bar-width (save-excursion
- (search-forward "|")
- (valign--pixel-width-from-to
- (match-beginning 0) (match-end 0)))))
+ (valign--beginning-of-table)
+ (let* ((space-width (save-excursion
+ (search-forward " ")
+ (valign--pixel-width-from-to
+ (match-beginning 0) (match-end 0))))
+ (bar-width (save-excursion
+ (search-forward "|")
+ (valign--pixel-width-from-to
+ (match-beginning 0) (match-end 0))))
+ (end (save-excursion (valign--end-of-table) (point)))
+ (column-width-list (valign--calculate-cell-width end))
+ (column-alignment-list (valign--calculate-alignment
+ (valign--guess-table-type) end))
+ row-idx column-idx column-start)
(ignore row-idx)
- ;; ‘separator-row-point-list’ marks the point for each
- ;; separator-row, so we can later come back and align them.
- ;; ‘rev-list’ is the reverse list of right positions of each
- ;; separator row cell. ‘at-sep-row’ t means we are at
- ;; a separator row.
- (valign--end-of-table)
- (setq end (point))
- (valign--beginning-of-table)
(valign--clean-text-property (point) end)
- (setq column-width-list
- (valign--calculate-cell-width end)
- column-alignment-list
- (valign--calculate-alignment
- (valign--guess-table-type) end))
- ;; Iterate each cell and apply tab stops.
+
+ ;; Start alignment.
(valign--do-row row-idx end
- (valign--do-column column-idx
- (save-excursion
- ;; Check there is a right bar.
- (when (save-excursion
- (search-forward "|" (line-end-position) t)
- (setq right-bar-pos (match-beginning 0)))
- ;; Can’t put this in the save-excursion, donno why.
- ;; Render the right bar of each cell.
- (valign--maybe-render-bar right-bar-pos)
+ (search-forward "|" (line-end-position))
+ (if (valign--separator-p)
+ (valign--align-separator-row
+ (valign--guess-table-type)
+ valign-separator-row-style
+ column-width-list)
+
+ ;; Not separator row, align each cell. Initialize some
+ ;; numbers when we are at a new line. ‘column-start’ is the
+ ;; pixel position of the current point, i.e., after the left
+ ;; bar.
+ (setq column-start (valign--pixel-width-from-to
+ (line-beginning-position) (point)))
+ (valign--maybe-render-bar (1- (point)))
+ (valign--do-column column-idx
+ (save-excursion
;; We are after the left bar (“|”).
;; Start aligning this cell.
;; Pixel width of the column
@@ -526,14 +525,6 @@ You need to restart valign mode for this setting to take
effect."
;; Pixel width of the cell.
(cell-width (valign--cell-width))
tab-width tab-start tab-end)
- ;; Initialize some numbers when we are at a new
- ;; line. ‘pos’ is the pixel position of the
- ;; current point, i.e., after the left bar.
- (when (eq column-idx 0)
- ;; Render the first bar of the line.
- (valign--maybe-render-bar (1- (point)))
- (setq pos (valign--pixel-width-from-to
- (line-beginning-position) (point))))
;; Align cell.
(cond ((eq cell-width 0)
;; 1) Empty cell.
@@ -541,7 +532,8 @@ You need to restart valign mode for this setting to take
effect."
(skip-chars-forward " ")
(if (< (- (point) tab-start) 2)
(valign--put-text-property
- tab-start (point) (+ pos col-width ssw))
+ tab-start (point)
+ (+ column-start col-width space-width))
;; When possible, we try to add two tabs
;; and the point can appear in the middle
;; of the cell, instead of on the very
@@ -549,37 +541,33 @@ You need to restart valign mode for this setting to take
effect."
(valign--put-text-property
tab-start
(1+ tab-start)
- (+ pos (/ col-width 2) ssw))
+ (+ column-start (/ col-width 2) space-width))
(valign--put-text-property
(1+ tab-start) (point)
- (+ pos col-width ssw))))
- ;; 2) Separator row. We don’t align the separator
- ;; row yet, but will come back to it.
- ((valign--separator-p)
- (valign--align-separator-row
- (valign--guess-table-type)
- valign-separator-row-style
- column-width-list))
- ;; 3) Normal cell.
+ (+ column-start col-width space-width))))
+ ;; 2) Normal cell.
(t (pcase alignment
- ;; 3.1) Align a left-aligned cell.
+ ;; 2.1) Align a left-aligned cell.
('left (search-forward "|" nil t)
(backward-char)
(setq tab-end (point))
(skip-chars-backward " ")
(valign--put-text-property
(point) tab-end
- (+ pos col-width ssw)))
- ;; 3.2) Align a right-aligned cell.
+ (+ column-start col-width space-width)))
+ ;; 2.2) Align a right-aligned cell.
('right (setq tab-width
(- col-width cell-width))
(setq tab-start (point))
(skip-chars-forward " ")
(valign--put-text-property
tab-start (point)
- (+ pos tab-width))))))
- ;; Update ‘pos’ for the next cell.
- (setq pos (+ pos col-width bar-width ssw)))))))))
+ (+ column-start tab-width))))))
+ ;; Update ‘column-start’ for the next cell.
+ (setq column-start (+ column-start
+ col-width
+ bar-width
+ space-width)))))))))
;;; Mode intergration
- [elpa] externals/valign 8b6ce8c 084/198: * valign.el (valign--cell-width): Minor change., (continued)
- [elpa] externals/valign 8b6ce8c 084/198: * valign.el (valign--cell-width): Minor change., Stefan Monnier, 2020/12/01
- [elpa] externals/valign dcf6bd8 088/198: Add force align, Stefan Monnier, 2020/12/01
- [elpa] externals/valign 53be5d7 089/198: Add fancy bar, Stefan Monnier, 2020/12/01
- [elpa] externals/valign 98eae8d 090/198: * valign.el (valign-table): Remove setter for custom option., Stefan Monnier, 2020/12/01
- [elpa] externals/valign 159ae0f 102/198: Fix typo, Stefan Monnier, 2020/12/01
- [elpa] externals/valign 1a614d2 101/198: Add missing with-silent-modifications form, Stefan Monnier, 2020/12/01
- [elpa] externals/valign ba7a72b 110/198: * valign.el (valign-table): Tuck in some long lines., Stefan Monnier, 2020/12/01
- [elpa] externals/valign ea71f38 126/198: * valign.el (valign-fancy-bar): Fix typo., Stefan Monnier, 2020/12/01
- [elpa] externals/valign cb95dc1 119/198: Separate valign--do-table, Stefan Monnier, 2020/12/01
- [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 <=
- [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, 2020/12/01
- [elpa] externals/valign 11f02f5 193/198: Allow non-table lines at the end of the table, Stefan Monnier, 2020/12/01