emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Emacs-diffs] shr-fontified f8d6b5c 5/7: (shr-expand-alignments): Get in


From: Lars Ingebrigtsen
Subject: [Emacs-diffs] shr-fontified f8d6b5c 5/7: (shr-expand-alignments): Get indentation of nested tables right.
Date: Sat, 07 Feb 2015 04:09:53 +0000

branch: shr-fontified
commit f8d6b5c1b608234f92535e05cb32cc646e3afdcf
Author: Lars Magne Ingebrigtsen <address@hidden>
Commit: Lars Magne Ingebrigtsen <address@hidden>

    (shr-expand-alignments): Get indentation of nested tables right.
---
 lisp/ChangeLog  |    1 +
 lisp/net/shr.el |   75 ++++++++++++++++++++++++++++++++++++------------------
 2 files changed, 51 insertions(+), 25 deletions(-)

diff --git a/lisp/ChangeLog b/lisp/ChangeLog
index 4645db9..dea164f 100644
--- a/lisp/ChangeLog
+++ b/lisp/ChangeLog
@@ -4,6 +4,7 @@
        does what it's supposed to.
        (shr-fold-line): Fix up implementation based on
        `window-text-pixel-size'.
+       (shr-expand-alignments): Get indentation of nested tables right.
 
 2015-02-05  Lars Ingebrigtsen  <address@hidden>
 
diff --git a/lisp/net/shr.el b/lisp/net/shr.el
index d11baf7..7cb3575 100644
--- a/lisp/net/shr.el
+++ b/lisp/net/shr.el
@@ -208,6 +208,7 @@ DOM should be a parse tree as generated by
        (shr-start nil)
        (shr-base nil)
        (shr-depth 0)
+       (shr-table-id 0)
        (shr-warning nil)
        (shr-font-cache (make-hash-table :test 'eq))
        (shr-fill-cache (make-hash-table :test 'equal))
@@ -1464,11 +1465,10 @@ The preference is a float determined from 
`shr-prefer-media-type'."
         ;; be smaller (if there's little text) or bigger (if there's
         ;; unbreakable text).
         (sketch (shr-make-table dom suggested-widths))
-        ;; Compute the "natural" width by setting each column to 500
+        ;; Compute the "natural" width by setting each column to 5000
         ;; characters and see how wide they really render.
         (natural (shr-make-table
-                  dom (make-vector (length columns)
-                                   (* 500 (shr-string-pixel-width "x")))))
+                  dom (make-vector (length columns) 5000)))
         (sketch-widths (shr-table-widths sketch natural suggested-widths)))
     ;; This probably won't work very well.
     (when (> (+ (loop for width across sketch-widths
@@ -1576,7 +1576,9 @@ The preference is a float determined from 
`shr-prefer-media-type'."
   (let* ((collapse (equal (cdr (assq 'border-collapse shr-stylesheet))
                          "collapse"))
         (shr-table-separator-length (if collapse 0 1))
-        (shr-table-vertical-line (if collapse "" shr-table-vertical-line)))
+        (shr-table-vertical-line (if collapse "" shr-table-vertical-line))
+        (start (point)))
+    (setq shr-table-id (1+ shr-table-id))
     (unless collapse
       (shr-insert-table-ruler widths))
     (dolist (row table)
@@ -1603,11 +1605,10 @@ The preference is a float determined from 
`shr-prefer-media-type'."
            (dolist (line lines)
              (end-of-line)
              (let ((start (point)))
-               (when (> align 1000)
-                 (debug (current-buffer)))
                (insert line
-                       (propertize " " 'display
-                                   `(space :align-to (,align)))
+                       (propertize " "
+                                   'display `(space :align-to (,align))
+                                   'shr-table-indent shr-table-id)
                        shr-table-vertical-line)
                (shr-colorize-region
                 start (1- (point)) (nth 4 column) (nth 5 column)))
@@ -1617,14 +1618,36 @@ The preference is a float determined from 
`shr-prefer-media-type'."
            (dotimes (i (- height (length lines)))
              (end-of-line)
              (let ((start (point)))
-               (insert (propertize " " 'display
-                                   `(space :align-to (,align)))
+               (insert (propertize " "
+                                   'display `(space :align-to (,align))
+                                   'shr-table-indent shr-table-id)
                        shr-table-vertical-line)
                (shr-colorize-region
                 start (1- (point)) (nth 4 column) (nth 5 column)))
              (forward-line 1)))))
       (unless collapse
-       (shr-insert-table-ruler widths)))))
+       (shr-insert-table-ruler widths)))
+    (if (equal (buffer-name) "*eww*")
+       (shr-expand-alignments start (point))
+      (unless (= start (point))
+       (put-text-property start (1+ start) 'shr-table-id shr-table-id)))))
+
+(defun shr-expand-alignments (start end)
+  (while (< (setq start (next-single-property-change
+                        start 'shr-table-id nil end))
+           end)
+    (goto-char start)
+    (let ((id (get-text-property (point) 'shr-table-id))
+         (base (shr-pixel-column)))
+      (save-excursion
+       (while (setq elem (text-property-any (point) end 'shr-table-indent id))
+         (goto-char elem)
+         (let ((align (get-text-property (point) 'display)))
+           (put-text-property (point) (1+ (point)) 'display
+                              `(space :align-to (,(+ (car (nth 2 align))
+                                                     base)))))
+         (forward-char 1))))
+    (setq start (1+ start))))
 
 (defun shr-insert-table-ruler (widths)
   (when shr-table-horizontal-line
@@ -1632,11 +1655,16 @@ The preference is a float determined from 
`shr-prefer-media-type'."
               (> shr-indentation 0))
       (shr-indent))
     (insert shr-table-corner)
-    (dotimes (i (length widths))
-      (insert (make-string (1+ (/ (aref widths i)
-                                 (shr-string-pixel-width "-")))
-                          shr-table-horizontal-line)
-             shr-table-corner))
+    (let ((total-width 0))
+      (dotimes (i (length widths))
+       (setq total-width (+ total-width (aref widths i) 20))
+       (insert (make-string (1+ (/ (aref widths i)
+                                   (shr-string-pixel-width "-")))
+                            shr-table-horizontal-line)
+               (propertize " "
+                           'display `(space :align-to (,total-width))
+                           'shr-table-indent shr-table-id)
+               shr-table-corner)))
     (insert "\n")))
 
 (defun shr-table-widths (table natural-table suggested-widths)
@@ -1757,6 +1785,11 @@ The preference is a float determined from 
`shr-prefer-media-type'."
          (push (nreverse tds) trs))))
     (nreverse trs)))
 
+(defun shr-pixel-buffer-width ()
+  (save-window-excursion
+    (set-window-buffer nil (current-buffer))
+    (car (window-text-pixel-size nil (point-min) (point-max)))))
+
 (defun shr-render-td (dom width fill)
   (with-temp-buffer
     (let ((bgcolor (dom-attr dom 'bgcolor))
@@ -1778,15 +1811,7 @@ The preference is a float determined from 
`shr-prefer-media-type'."
        (shr-descend dom))
       (let ((shr-internal-width width))
        (shr-fold-lines (point-min) (point-max))
-       ;; Compute the max width of all non-foldable lines.
-       (save-window-excursion
-         (set-window-buffer nil (current-buffer))
-         (goto-char (point-min))
-         (while (not (eobp))
-           (when (not (eolp))
-             (end-of-line)
-             (setq max-width (max max-width (shr-pixel-column))))
-           (forward-line 1))))
+       (setq max-width (shr-pixel-buffer-width)))
       (goto-char (point-max))
       ;; Delete padding at the bottom of the TDs.
       (delete-region



reply via email to

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