[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/valign 88dd625 130/198: Change align padding
From: |
Stefan Monnier |
Subject: |
[elpa] externals/valign 88dd625 130/198: Change align padding |
Date: |
Tue, 1 Dec 2020 18:19:32 -0500 (EST) |
branch: externals/valign
commit 88dd625b2e8ca35c1a9ff5e7bd9855fdd9a3d548
Author: Yuan Fu <casouri@gmail.com>
Commit: Yuan Fu <casouri@gmail.com>
Change align padding
Now we try to leave a white space as padding on the either side of the
cell content, so a left-aligned cell would be:
| <space> <content> <space> <overlay> |
and a right-aligned cell would be:
| <overlay> <space> <content> <space> |
And for valign-table-1, the structure before is:
if (empty cell):
if (only one space):
fiddle around and add overlay
else
fiddle around and add overlay
else:
if (left align):
fiddle around and add overlay
else if (right align):
fiddle around and add overlay
The new structure is:
let cell-configuration in
if (emptry cell):
add overlay with cell-configuration
else:
if (left aligned):
add overlay with cell-configuration
else if (right aligned):
add overlay with cell-configuration
* valign.el (valign--cell-content-config): New function.
(valign--cell-width): Use 'valign--cell-content-config'.
(valign-table-1): Use 'valign--cell-content-config'.
---
valign.el | 133 +++++++++++++++++++++++++++++++++++++-------------------------
1 file changed, 79 insertions(+), 54 deletions(-)
diff --git a/valign.el b/valign.el
index d31cbea..37eeb7e 100644
--- a/valign.el
+++ b/valign.el
@@ -75,6 +75,58 @@ Doesn’t check if we are in a cell."
;; (buffer-substring p (1- (point))))
;; 'right 'left)
+(defun valign--cell-content-config ()
+ "Return (CELL-BEG CONTENT-BEG CONTENT-END CELL-END).
+CELL-BEG is after the left bar, CELL-END is before the right bar.
+CELL-CONTENT contains the actual non-white-space content,
+possibly with a single white space padding on the either side, if
+there are more than one white space on that side.
+
+If the cell is empty, CONTENT-BEG is
+
+ (min (CELL-BEG + 1) CELL-END)
+
+CONTENT-END is
+
+ (max (CELL-END - 1) CELL-BEG)
+
+Assumes point is after the left bar (“|”). Assumes there is a
+right bar."
+ (save-excursion
+ (let ((cell-beg (point))
+ (cell-end (save-excursion
+ (search-forward "|" (line-end-position))
+ (match-beginning 0)))
+ ;; `content-beg-strict' is the beginning of the content
+ ;; excluding any white space. Same for `content-end-strict'.
+ content-beg-strict content-end-strict)
+ (if (save-excursion (skip-chars-forward " ") (looking-at-p "|"))
+ ;; Empty cell.
+ (list cell-beg
+ (min (1+ cell-beg) cell-end)
+ (max (1- cell-end) cell-beg)
+ cell-end)
+ ;; Non-empty cell.
+ (skip-chars-forward " ")
+ (setq content-beg-strict (point))
+ (goto-char cell-end)
+ (skip-chars-backward " ")
+ (setq content-end-strict (point))
+ ;; Calculate delimiters. Basically, we try to preserve a white
+ ;; space on the either side of the content, i.e., include them
+ ;; in (BEG . END). Because if you are typing in a cell and
+ ;; type a space, you probably want valign to keep that space
+ ;; as cell content, rather than to consider it as part of the
+ ;; padding and add overlay over it.
+ (list cell-beg
+ (if (= (- content-beg-strict cell-beg) 1)
+ content-beg-strict
+ (1- content-beg-strict))
+ (if (= (- cell-end content-end-strict) 1)
+ content-end-strict
+ (1+ content-end-strict))
+ cell-end)))))
+
(defun valign--cell-width ()
"Return the pixel width of the cell at point.
Assumes point is after the left bar (“|”).
@@ -88,18 +140,8 @@ Return nil if not in a cell."
;; EMPTY := <SPACE>+
;; NON-EMPTY := <SPACE>+<NON-SPACE>+<SPACE>+
;; DELIM := | or +
- (let (start)
- (save-excursion
- (skip-chars-forward " ")
- (setq start (point))
- (if (looking-at-p "[|]")
- 0
- (if (not (search-forward "|" nil t))
- (signal 'valign-bad-cell nil)
- ;; We are at the right “|”
- (goto-char (match-beginning 0))
- (skip-chars-backward " ")
- (valign--pixel-width-from-to start (point)))))))
+ (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
@@ -463,17 +505,17 @@ You need to restart valign mode for this setting to take
effect."
row-idx column-idx column-start)
(ignore row-idx)
- ;; Start alignment.
+ ;; Align each row.
(valign--do-row row-idx table-end
(search-forward "|" (line-end-position))
(if (valign--separator-p)
+ ;; Separator row.
(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
+ ;; Not separator row, align each cell. ‘column-start’ is the
;; pixel position of the current point, i.e., after the left
;; bar.
(setq column-start (valign--pixel-width-from-to
@@ -487,50 +529,33 @@ You need to restart valign mode for this setting to take
effect."
(let* ((col-width (nth column-idx column-width-list))
(alignment (nth column-idx column-alignment-list))
;; Pixel width of the cell.
- (cell-width (valign--cell-width))
- tab-width tab-start tab-end)
+ (cell-width (valign--cell-width)))
;; Align cell.
(cl-labels ((valign--put-ov
(beg end xpos)
(valign--put-overlay beg end 'display
(valign--space xpos))))
- (cond ((eq cell-width 0)
- ;; 1) Empty cell.
- (setq tab-start (point))
- (skip-chars-forward " ")
- (if (< (- (point) tab-start) 2)
- (valign--put-ov
- 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
- ;; left or very right.
- (valign--put-ov
- tab-start
- (1+ tab-start)
- (+ column-start (/ col-width 2) space-width))
- (valign--put-ov
- (1+ tab-start) (point)
- (+ column-start col-width space-width))))
- ;; 2) Normal cell.
- (t (pcase alignment
- ;; 2.1) Align a left-aligned cell.
- ('left (search-forward "|" nil t)
- (backward-char)
- (setq tab-end (point))
- (skip-chars-backward " ")
- (valign--put-ov
- (point) tab-end
- (+ 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-ov
- tab-start (point)
- (+ column-start tab-width)))))))
+ (pcase-let ((`(,cell-beg
+ ,content-beg
+ ,content-end
+ ,cell-end)
+ (valign--cell-content-config)))
+ (if (= cell-beg content-beg)
+ ;; This cell has only one space.
+ (valign--put-ov
+ cell-beg cell-end
+ (+ column-start col-width space-width))
+ ;; A normal cell.
+ (pcase alignment
+ ;; Align a left-aligned cell.
+ ('left (valign--put-ov
+ content-end cell-end
+ (+ column-start col-width space-width)))
+ ;; Align a right-aligned cell.
+ ('right (valign--put-ov
+ cell-beg content-beg
+ (+ column-start
+ (- col-width cell-width))))))))
;; Update ‘column-start’ for the next cell.
(setq column-start (+ column-start
col-width
- [elpa] externals/valign 561650f 116/198: Change internal implementation, (continued)
- [elpa] externals/valign 561650f 116/198: Change internal implementation, Stefan Monnier, 2020/12/01
- [elpa] externals/valign 47641e3 124/198: * valign.el (valign-table-1): Rename 'end' to 'table-end'., Stefan Monnier, 2020/12/01
- [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 <=
- [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, 2020/12/01
- [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