emacs-elpa-diffs
[Top][All Lists]
Advanced

[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
 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]