[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/valign e91abf2 112/198: Extract out boilerplate from va
From: |
Stefan Monnier |
Subject: |
[elpa] externals/valign e91abf2 112/198: Extract out boilerplate from valign-table |
Date: |
Tue, 1 Dec 2020 18:19:28 -0500 (EST) |
branch: externals/valign
commit e91abf24633e5c9ea81cc08c470331f37e40fb4c
Author: Yuan Fu <casouri@gmail.com>
Commit: Yuan Fu <casouri@gmail.com>
Extract out boilerplate from valign-table
* valign.el (valign-table): Contains boilerplate.
(valign-table-1): New function. Contains working code.
---
valign.el | 219 +++++++++++++++++++++++++++++++-------------------------------
1 file changed, 111 insertions(+), 108 deletions(-)
diff --git a/valign.el b/valign.el
index 12fbad8..909aed6 100644
--- a/valign.el
+++ b/valign.el
@@ -506,119 +506,122 @@ You need to restart valign mode for this setting to
take effect."
:group 'valign)
(defun valign-table ()
- "Visually align the table at point."
(interactive)
(condition-case nil
(save-excursion
(if (not window-system)
(signal 'valign-not-gui nil))
- (let (end column-width-list column-idx pos ssw bar-width
- separator-row-point-list rev-list
- column-alignment-list info at-sep-row right-bar-pos)
- ;; ‘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.
- (if (not (valign--at-table-p))
- (signal 'valign-not-on-table nil))
- (valign--end-of-table)
- (setq end (point))
- (valign--beginning-of-table)
- (valign--clean-text-property (point) end)
- (setq info (valign--calculate-table-info end))
- (setq column-width-list
- (valign-table-info-column-width-list info)
- column-alignment-list
- (valign-table-info-column-alignment-list info))
- ;; Iterate each cell and apply tab stops.
- (valign--do-table column-idx end
- (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)
- ;; 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--pixel-width-from-to
- (point) (1+ (point)))))
- (unless bar-width (setq bar-width
- (valign--pixel-width-from-to
- (1- (point)) (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))
- ;; Render the first bar of the line.
- (valign--maybe-render-bar (1- (point)))
- (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))))
- ;; 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))
- (valign--put-text-property
- (point) (1+ (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)
- (goto-char row-point)
- (valign--align-separator-row (valign--guess-table-type)
- valign-separator-row-style
- (reverse rev-list)))))
-
- ((debug valign-bad-cell valign-not-gui valign-not-on-table) nil)))
+ (if (not (valign--at-table-p))
+ (signal 'valign-not-on-table nil))
+ (valign-table-1))
+ (valign-early-termination nil)
+ ((valign-bad-cell valign-not-gui valign-not-on-table) nil)))
+
+(defun valign-table-1 ()
+ "Visually align the table at point."
+ (let (end column-width-list column-idx pos ssw bar-width
+ separator-row-point-list rev-list
+ column-alignment-list info at-sep-row right-bar-pos)
+ ;; ‘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 info (valign--calculate-table-info end))
+ (setq column-width-list
+ (valign-table-info-column-width-list info)
+ column-alignment-list
+ (valign-table-info-column-alignment-list info))
+ ;; Iterate each cell and apply tab stops.
+ (valign--do-table column-idx end
+ (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)
+ ;; 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--pixel-width-from-to
+ (point) (1+ (point)))))
+ (unless bar-width (setq bar-width
+ (valign--pixel-width-from-to
+ (1- (point)) (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))
+ ;; Render the first bar of the line.
+ (valign--maybe-render-bar (1- (point)))
+ (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))))
+ ;; 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))
+ (valign--put-text-property
+ (point) (1+ (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)
+ (goto-char row-point)
+ (valign--align-separator-row (valign--guess-table-type)
+ valign-separator-row-style
+ (reverse rev-list)))))
;;; Mode intergration
- [elpa] externals/valign 4c514ce 169/198: Use text properties for table.el tables, (continued)
- [elpa] externals/valign 4c514ce 169/198: Use text properties for table.el tables, Stefan Monnier, 2020/12/01
- [elpa] externals/valign 8e598bf 170/198: ; * test.org: Add a test table., Stefan Monnier, 2020/12/01
- [elpa] externals/valign 316452b 163/198: Change fancy bar to use overlay, Stefan Monnier, 2020/12/01
- [elpa] externals/valign 06d9e1a 192/198: Prepare for ELPA, Stefan Monnier, 2020/12/01
- [elpa] externals/valign 5729733 178/198: * valign.el: Move user land code down below (re-layout)., Stefan Monnier, 2020/12/01
- [elpa] externals/valign b189102 194/198: ; * test.org: New test., Stefan Monnier, 2020/12/01
- [elpa] externals/valign 44c747b 136/198: * valign.el (valign-table): Clean up table if error occurs., Stefan Monnier, 2020/12/01
- [elpa] externals/valign 80cf682 121/198: Remove some internal variables, Stefan Monnier, 2020/12/01
- [elpa] externals/valign 9daeab8 071/198: * valign.el (valign-table-quite): Really ignore errors., Stefan Monnier, 2020/12/01
- [elpa] externals/valign 09e89aa 105/198: * valign.el (valign--at-table-p): Don’t align org blocks., Stefan Monnier, 2020/12/01
- [elpa] externals/valign e91abf2 112/198: Extract out boilerplate from valign-table,
Stefan Monnier <=
- [elpa] externals/valign d799e31 181/198: ; * valign.el (valign--cell-content-config): Fix typo., Stefan Monnier, 2020/12/01
- [elpa] externals/valign 9fc0c11 149/198: Use new function to calculate space and bar width, Stefan Monnier, 2020/12/01
- [elpa] externals/valign d18aea8 183/198: Improve error handling, Stefan Monnier, 2020/12/01
- [elpa] externals/valign 62766e7 184/198: Add custom group, Stefan Monnier, 2020/12/01
- [elpa] externals/valign bf68d39 171/198: Remove single-column style, Stefan Monnier, 2020/12/01
- [elpa] externals/valign ce1a53c 188/198: ; * test.org: Update., Stefan Monnier, 2020/12/01
- [elpa] externals/valign 82be45d 198/198: Bug fix, Stefan Monnier, 2020/12/01
- [elpa] externals/valign cf3a794 099/198: Fix: cursor disappears on bars when fancy-bar is t, Stefan Monnier, 2020/12/01
- [elpa] externals/valign 0e77a02 164/198: Add some commands that suppresses auto alignment, Stefan Monnier, 2020/12/01
- [elpa] externals/valign 7afe578 182/198: Improve documentation, Stefan Monnier, 2020/12/01