[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/valign e32d191 079/198: Valign-table refactor
From: |
Stefan Monnier |
Subject: |
[elpa] externals/valign e32d191 079/198: Valign-table refactor |
Date: |
Tue, 1 Dec 2020 18:19:21 -0500 (EST) |
branch: externals/valign
commit e32d1913a7f1f9d2cd2dda12edd3800c261a5d5c
Author: Yuan Fu <casouri@gmail.com>
Commit: Yuan Fu <casouri@gmail.com>
Valign-table refactor
Now we can align irregular tables. For example:
|----+-----| |-------|
| a | b | --> | mmm |
| ye | ho | | woome |
|----+-----| |-------|
This is considered as one table, just with weird empty separator rows.
Before:
if separator
xxx
else
(init)
if empty
xxx
else
pcase alignment
| left xxx
| right xxx
(update info)
After:
(init)
cond
| empty xxx
| separator xxx
| normal pcase
| left xxx
| right xxx
(update info)
* valign.el (valign-table): Refactor.
---
valign.el | 152 ++++++++++++++++++++++++++++++++------------------------------
1 file changed, 79 insertions(+), 73 deletions(-)
diff --git a/valign.el b/valign.el
index e72089a..7caa06d 100644
--- a/valign.el
+++ b/valign.el
@@ -514,12 +514,13 @@ for the former, and 'multi-column for the latter."
(save-excursion
(let (end column-width-list column-idx pos ssw bar-width
separator-row-point-list rev-list right-point
- column-alignment-list info)
+ column-alignment-list info at-sep-row)
;; ‘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. ‘right-point’ marks point before the
- ;; right bar for each cell.
+ ;; separator row cell. ‘right-point’ marks point before the
+ ;; right bar for each cell. ‘at-sep-row’ t means we are at
+ ;; a separator row.
(if (not (valign--end-of-table))
(signal 'valign-not-on-table nil))
(setq end (point))
@@ -531,76 +532,81 @@ for the former, and 'multi-column for the latter."
(valign-table-info-column-alignment-list info))
;; Iterate each cell and apply tab stops.
(valign--do-table column-idx end
- ;; We don’t align the separator row yet, but will come
- ;; back to it.
- (if (valign--sperator-p)
- (when (eq column-idx 0)
- (push (point) separator-row-point-list))
- (save-excursion
- ;; Check there is a right bar.
- (when (save-excursion
- (setq right-point (search-forward "|" nil t)))
- ;; We are after the left bar (“|”).
- ;; Start aligning this cell.
- (let* ((col-width (or (nth column-idx column-width-list)
- 0)) ;; Pixel width of the column
- ;; Pixel width of the cell.
- (cell-width (valign--cell-width))
- ;; single-space-width
- (ssw (or ssw (valign--glyph-width-at-point)))
- (bar-width (or bar-width
- (valign--glyph-width-at-point
- (1- (point)))))
- 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.
- (if (eq column-idx 0)
- (setq pos (valign--pixel-width-from-to
- (line-beginning-position) (point))
- rev-list nil))
- ;; Clean up old tabs (i.e., stuff used for padding).
- (valign--clean-text-property (point) (1- right-point))
- ;; Align an empty cell.
- (if (eq cell-width 0)
- (progn
- (setq tab-start (point))
- (valign--skip-space-forward)
- (if (< (- (point) tab-start) 2)
- (valign--put-text-property
- tab-start (point) (+ pos col-width ssw))
- ;; When possible, we try to add two tabs
- ;; and the point can appear in the middle
- ;; of the cell, instead of on the very
- ;; left or very right.
- (valign--put-text-property
- tab-start
- (1+ tab-start)
- (+ pos (/ col-width 2) ssw))
- (valign--put-text-property
- (1+ tab-start) (point)
- (+ pos col-width ssw))))
- ;; Align a left-aligned cell.
- (pcase (valign--cell-alignment
- (valign--guess-table-type)
- (nth column-idx column-alignment-list))
- ('left (search-forward "|" nil t)
- (backward-char)
- (setq tab-end (point))
- (valign--skip-space-backward)
- (valign--put-text-property
- (point) tab-end
- (+ pos col-width ssw)))
- ;; Align a right-aligned cell.
- ('right (setq tab-width
- (- col-width cell-width))
- (setq tab-start (point))
- (valign--skip-space-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))
+ (save-excursion
+ ;; Check there is a right bar.
+ (when (save-excursion
+ (setq right-point
+ (search-forward "|" (line-end-position) t)))
+ ;; We are after the left bar (“|”).
+ ;; Start aligning this cell.
+ ;; Pixel width of the column
+ (let* ((col-width (nth column-idx column-width-list))
+ ;; Pixel width of the cell.
+ (cell-width (valign--cell-width))
+ tab-width tab-start tab-end)
+ ;; single-space-width
+ (unless ssw (setq ssw (valign--glyph-width-at-point)))
+ (unless bar-width (setq bar-width
+ (valign--glyph-width-at-point
+ (1- (point)))))
+ ;; 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)
+ (when (valign--separator-p)
+ (push (point) separator-row-point-list))
+ (unless (valign--separator-p)
+ (setq rev-list nil))
+ (setq at-sep-row (if (valign--separator-p) t nil))
+ (setq pos (valign--pixel-width-from-to
+ (line-beginning-position) (point))))
+ ;; Clean up old tabs (i.e., stuff used for padding).
+ (valign--clean-text-property (point) (1- right-point))
+ ;; Align cell.
+ (cond ((eq cell-width 0)
+ ;; 1) Empty cell.
+ (setq tab-start (point))
+ (valign--skip-space-forward)
+ (if (< (- (point) tab-start) 2)
+ (valign--put-text-property
+ tab-start (point) (+ pos col-width ssw))
+ ;; When possible, we try to add two tabs
+ ;; and the point can appear in the middle
+ ;; of the cell, instead of on the very
+ ;; left or very right.
+ (valign--put-text-property
+ tab-start
+ (1+ tab-start)
+ (+ pos (/ col-width 2) ssw))
+ (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) nil)
+ ;; 3) Normal cell.
+ (t (pcase (valign--cell-alignment
+ (valign--guess-table-type)
+ (nth column-idx column-alignment-list))
+ ;; 3.1) Align a left-aligned cell.
+ ('left (search-forward "|" nil t)
+ (backward-char)
+ (setq tab-end (point))
+ (valign--skip-space-backward)
+ (valign--put-text-property
+ (point) tab-end
+ (+ pos col-width ssw)))
+ ;; 3.2) Align a right-aligned cell.
+ ('right (setq tab-width
+ (- col-width cell-width))
+ (setq tab-start (point))
+ (valign--skip-space-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))
+ (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)
- [elpa] externals/valign 4005231 165/198: Fix inconsistency after enabling org-indent, (continued)
- [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
- [elpa] externals/valign 99300eb 167/198: Add support for table.el tables, Stefan Monnier, 2020/12/01
- [elpa] externals/valign 7b581e0 072/198: Remove valign-werid-alignment error, Stefan Monnier, 2020/12/01
- [elpa] externals/valign f6d60d2 078/198: Merge two implementations for valign--align-separator-row, Stefan Monnier, 2020/12/01
- [elpa] externals/valign e9975b7 083/198: Use new text size calculation, Stefan Monnier, 2020/12/01
- [elpa] externals/valign e32d191 079/198: Valign-table refactor,
Stefan Monnier <=
- [elpa] externals/valign 21a397a 092/198: Update README, Stefan Monnier, 2020/12/01
- [elpa] externals/valign 4ecc990 093/198: * valign.el (valign--table-face): Fix markdown-mode., Stefan Monnier, 2020/12/01
- [elpa] externals/valign c4ddfb3 095/198: Fix org-flag-region advice, Stefan Monnier, 2020/12/01
- [elpa] externals/valign 7529ddd 111/198: Revert "* valign.el (valign-table): Tuck in some long lines.", Stefan Monnier, 2020/12/01
- [elpa] externals/valign 97beef8 127/198: Remove valign-table-quiet, Stefan Monnier, 2020/12/01
- [elpa] externals/valign 1412fef 133/198: * valign.el (valign-table): Suppress search errors., Stefan Monnier, 2020/12/01
- [elpa] externals/valign fc591d8 129/198: * valign.el (valign-table-1): Clear overlay before measuring table., Stefan Monnier, 2020/12/01
- [elpa] externals/valign c5eb220 108/198: Remove valign--face-attribute, Stefan Monnier, 2020/12/01
- [elpa] externals/valign ed8dd50 118/198: Remove obsolete functions, Stefan Monnier, 2020/12/01
- [elpa] externals/valign ced2f6a 132/198: * valign.el (valign-table-1): Fix search term., Stefan Monnier, 2020/12/01