[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/valign d132330 120/198: Align separator row like other
From: |
Stefan Monnier |
Subject: |
[elpa] externals/valign d132330 120/198: Align separator row like other rows |
Date: |
Tue, 1 Dec 2020 18:19:30 -0500 (EST) |
branch: externals/valign
commit d1323309805c199dc012e2f666c0080eff52c526
Author: Yuan Fu <casouri@gmail.com>
Commit: Yuan Fu <casouri@gmail.com>
Align separator row like other rows
Before: Collect positions and align separator rows at the end.
Now: Align it normally like other rows.
* valign.el (valign--align-separator-row): Don’t depend on pos-list
anymore, instead use column-width-list like other aligning code.
(valign-table-1): Change how ssw and bar-width are calculated (so they
match what 'valign--align-separator-row' gets). Move
'valign--align-separator-row' from the end to inline with other
aligning code.
---
valign.el | 79 +++++++++++++++++++++++++++++++++------------------------------
1 file changed, 42 insertions(+), 37 deletions(-)
diff --git a/valign.el b/valign.el
index 5df9e52..1ccc7f3 100644
--- a/valign.el
+++ b/valign.el
@@ -369,16 +369,19 @@ before event, ACTION is either 'entered or 'left."
(put-text-property p tab-end 'display nil)))))))
(cl-defmethod valign--align-separator-row
- (type (style (eql single-column)) pos-list)
+ (type (style (eql single-column)) column-width-list)
"Align the separator row (|---+---|) as “|---------|”.
Assumes the point is after the left bar (“|”). TYPE can be
either 'org-mode or 'markdown, it doesn’t make any difference.
-STYLE is 'single-column. POS-LIST is a list of each column’s
-right bar’s position."
+STYLE is 'single-column. COLUMN-WIDTH-LIST is returned from
+`valign--calculate-cell-width'."
(ignore type style)
- (let ((p (point))
- ;; Position of the right-most bar.
- (total-width (car (last pos-list))))
+ (let* ((p (point))
+ (column-count (length column-width-list))
+ (bar-width (valign--pixel-width-from-to (1- (point)) (point)))
+ ;; Position of the right-most bar.
+ (total-width (+ (apply #'+ column-width-list)
+ (* bar-width (1+ column-count)))))
(when (search-forward "|" nil t)
(valign--put-text-property p (1- (point)) total-width)
;; Render the right bar.
@@ -411,28 +414,27 @@ Assumes point is on the right bar or plus sign."
(valign--put-face-overlay '(:strike-through t) beg end))
(cl-defmethod valign--align-separator-row
- (type (style (eql multi-column)) pos-list)
+ (type (style (eql multi-column)) column-width-list)
"Align the separator row in multi column style.
TYPE can be 'org-mode or 'markdown-mode, STYLE is 'multi-column.
-POS-LIST is a list of positions for each column’s right bar."
+COLUMN-WIDTH-LIST is returned from `valign--calculate-cell-width'."
(ignore type style)
- (let ((p (point))
+ (let ((bar-width (valign--pixel-width-from-to (1- (point)) (point)))
+ (space-width (save-excursion
+ (search-forward " ")
+ (valign--pixel-width-from-to
+ (match-beginning 0) (match-end 0))))
+ (column-start (point))
(col-idx 0)
- (max-col (1- (length pos-list)))
- (seperator-p (valign--separator-p)))
- (while (and (< (point) (point-max))
- (<= col-idx max-col)
- (re-search-forward "[+|]" (line-end-position) t))
- ;; Separator rows that has empty cells (|----| |----|)
- ;; is also possible. `seperator-p' is t only for |----|.
- (if seperator-p
- (valign--separator-row-add-overlay
- p (1- (point)) (nth col-idx pos-list))
- (valign--put-text-property
- p (1- (point)) (nth col-idx pos-list)))
- (cl-incf col-idx)
- (setq p (point))
- (setq seperator-p (valign--separator-p)))))
+ (pos (valign--pixel-width-from-to
+ (line-beginning-position) (point))))
+ (while (re-search-forward "[+|]" (line-end-position) t)
+ (let ((column-width (nth col-idx column-width-list)))
+ (valign--separator-row-add-overlay
+ column-start (1- (point)) (+ pos column-width space-width))
+ (setq column-start (point)
+ pos (+ pos column-width bar-width space-width))
+ (cl-incf col-idx)))))
(defun valign--guess-table-type ()
"Return either 'org or 'markdown."
@@ -518,11 +520,16 @@ You need to restart valign mode for this setting to take
effect."
(cell-width (valign--cell-width))
tab-width tab-start tab-end)
;; single-space-width
- (unless ssw (setq ssw (valign--pixel-width-from-to
- (point) (1+ (point)))))
- (unless bar-width (setq bar-width
- (valign--pixel-width-from-to
- (1- (point)) (point))))
+ (unless ssw
+ (setq ssw (save-excursion
+ (search-forward " ")
+ (valign--pixel-width-from-to
+ (match-beginning 0) (match-end 0)))))
+ (unless bar-width
+ (setq bar-width (save-excursion
+ (search-forward "|")
+ (valign--pixel-width-from-to
+ (match-beginning 0) (match-end 0)))))
;; 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.
@@ -557,7 +564,11 @@ You need to restart valign mode for this setting to take
effect."
(+ pos col-width ssw))))
;; 2) Separator row. We don’t align the separator
;; row yet, but will come back to it.
- ((valign--separator-p) nil)
+ ((valign--separator-p)
+ (valign--align-separator-row
+ (valign--guess-table-type)
+ valign-separator-row-style
+ column-width-list))
;; 3) Normal cell.
(t (pcase alignment
;; 3.1) Align a left-aligned cell.
@@ -579,13 +590,7 @@ You need to restart valign mode for this setting to take
effect."
;; Update ‘pos’ for the next cell.
(setq pos (+ pos col-width bar-width ssw))
(unless at-sep-row
- (push (- pos bar-width) rev-list)))))))
- ;; After aligning all rows, align the separator row.
- (dolist (row-point separator-row-point-list)
- (goto-char row-point)
- (valign--align-separator-row (valign--guess-table-type)
- valign-separator-row-style
- (reverse rev-list)))))
+ (push (- pos bar-width) rev-list)))))))))
;;; Mode intergration
- [elpa] externals/valign a632117 122/198: * valign.el (valign--do-column): Go to beginning of line first., (continued)
- [elpa] externals/valign a632117 122/198: * valign.el (valign--do-column): Go to beginning of line first., Stefan Monnier, 2020/12/01
- [elpa] externals/valign e084c38 087/198: Change to local mode, Stefan Monnier, 2020/12/01
- [elpa] externals/valign 5c5a7a7 100/198: * README.org: Fix typo., Stefan Monnier, 2020/12/01
- [elpa] externals/valign 2fe8f52 109/198: * valign.el (valign-table): Move the check for GUI into condition-case form., Stefan Monnier, 2020/12/01
- [elpa] externals/valign 901112e 106/198: Update to 2.0.0, Stefan Monnier, 2020/12/01
- [elpa] externals/valign f9bfbcf 134/198: Made aligning lazy, Stefan Monnier, 2020/12/01
- [elpa] externals/valign 16cbfe1 117/198: * README.org: Add use-package snippet., Stefan Monnier, 2020/12/01
- [elpa] externals/valign 7244d66 139/198: * valign.el (valign--put-overlay): Make overlays non-sticky., Stefan Monnier, 2020/12/01
- [elpa] externals/valign 88dd625 130/198: Change align padding, Stefan Monnier, 2020/12/01
- [elpa] externals/valign 035c8e2 137/198: * valign.el (valign-mode): Alert user that valign-mode has no effect under non-graphical display., Stefan Monnier, 2020/12/01
- [elpa] externals/valign d132330 120/198: Align separator row like other rows,
Stefan Monnier <=
- [elpa] externals/valign 973ddd6 138/198: * valign.el: Fix typo., Stefan Monnier, 2020/12/01
- [elpa] externals/valign 6468741 168/198: Add better table.el table support, Stefan Monnier, 2020/12/01
- [elpa] externals/valign eea7454 173/198: * valign.el (cl-generic): Add require., Stefan Monnier, 2020/12/01
- [elpa] externals/valign 16e3313 174/198: * valign.el (Commentary): Update., Stefan Monnier, 2020/12/01
- [elpa] externals/valign 4005231 165/198: Fix inconsistency after enabling org-indent, Stefan Monnier, 2020/12/01
- [elpa] externals/valign 8c28c8c 151/198: * valign.el: Bump version to 2.3.0., Stefan Monnier, 2020/12/01
- [elpa] externals/valign e82e1f0 155/198: Re-align in jit-lock-functions, Stefan Monnier, 2020/12/01
- [elpa] externals/valign 2d7918b 195/198: Minor fixes, Stefan Monnier, 2020/12/01
- [elpa] externals/valign 831c2a7 176/198: * valign.el (valign--do-row): Fix infinite loop., Stefan Monnier, 2020/12/01
- [elpa] externals/valign 7315f4b 148/198: Add support for org-indent, Stefan Monnier, 2020/12/01