Subject: Re: bug#32281: shr.el align support patch

From: Bad Blue Bull <[email-masked]>
Date: Tue, 07 Aug 2018 03:51:52 +0300

OKay, I've trimmed the source and reverted default value of shr-use-fonts to t.

06.08.2018, 18:14, "Eli Zaretskii" <[email-masked]>:

The first line of the doc string should not be longer than 78
characters, and should be a complete sentence.

I don't know what do you mean of "complete sentence", docs say it must start with a capital letter and end with a period.

First sentence now fills 2 lines I know it's not good for apropos, I can edit it again if needed.

Why did you decide to use u+2028 and u+2029 for these purposes? Emacs
doesn't yet support these characters as Unicode intended, so using
them might have unexpected effects, and might produce different effect
if we start supporting them in the future.

I need to use a character to mark places where lines must be split (specified by <br> tags and end of list items), also a character to mark end of a paragraph to be filled (a mark can be used for this purpose but docs warn against it). These chars will be removed when a paragraph gets filled, I don't see them cause any trouble in the future and those values can easily be altered to diffirent ones if it happens.

--- shr-old.el	2018-07-21 03:37:08.299506430 +0300
+++ shr.el	2018-08-07 03:14:00.677563202 +0300
@@ -292,6 +292,52 @@
     (when shr-warning
       (message "%s" shr-warning))))
 
+(defun shr-fill-paragraph-with-breaks (&optional justify bre-del bre-regexp)
+  "Fill paragraph at or after point, breaking lines at positions
+matching regexp argument BRE-REGEXP.
+
+If JUSTIFY is non-nil justify as well.
+
+Delete matched text if BRE-DEL is non nil.
+
+BRE-REGEXP must be a regexp that determines positions where to
+break lines, default value is \\x2028."
+  (unless bre-regexp (setq bre-regexp "\x2028"))
+  (setq bre-regexp (concat bre-regexp "\\|\x2029"))
+  (save-excursion
+   (let ((line-begin) (paragraph-end))
+	 (forward-paragraph)
+	 (insert "\x2029") ; use \x2029 (unicode paragraph separator) to mark end
+					   ; of a paragraph
+	 (backward-paragraph)
+	 (setq line-begin (point))
+	 (while (not paragraph-end)
+			(re-search-forward bre-regexp)
+			(if (equal (match-string 0) "\x2029") (setq paragraph-end t))
+										; end of paragraph reached
+			(if (or bre-del paragraph-end) (replace-match ""))
+			(unless (equal (char-after) ?\n) (insert "\n"))
+										; I don't know why but two adjacent \n
+										; leave an empty line after fill
+			(fill-region-as-paragraph line-begin (- (point) 1)
+			justify)
+			(setq line-begin (point)))
+	 (delete-char -1))))
+
+(defun shr-fill-paragraph (dom)
+  "Fill paragraph"
+  (when (not shr-use-fonts) ;fill-paragraph is useful only with monospace
+							;fonts
+ 	(shr-fill-paragraph-with-breaks
+	 ((lambda (x)
+		(cond
+ 		  ((equal x "right") 'right)
+ 		  ((equal x "center") 'center)
+ 		  ((equal x "left") 'left)))
+ 	  (cdr (assq 'align (dom-attributes dom)))) ; justify
+	 parameter t ; bre-del t
+	 )))
+
 (defun shr--remove-blank-lines-at-the-end (start end)
   (save-restriction
     (save-excursion
@@ -1191,6 +1237,7 @@
 (defun shr-heading (dom &rest types)
   (shr-ensure-paragraph)
   (apply #'shr-fontize-dom dom types)
+  (shr-fill-paragraph dom)
   (shr-ensure-paragraph))
 
 (defun shr-urlify (start url &optional title)
@@ -1354,6 +1401,7 @@
 (defun shr-tag-p (dom)
   (shr-ensure-paragraph)
   (shr-generic dom)
+  (shr-fill-paragraph dom)
   (shr-ensure-paragraph))
 
 (defun shr-tag-div (dom)
@@ -1742,12 +1790,14 @@
   ;; paragraph.
   (unless (bolp)
     (insert "\n"))
+  (shr-fill-paragraph dom)
   (shr-ensure-paragraph))
 
 (defun shr-tag-ol (dom)
   (shr-ensure-paragraph)
   (let ((shr-list-mode 1))
     (shr-generic dom))
+  (shr-fill-paragraph dom)
   (shr-ensure-paragraph))
 
 (defun shr-tag-li (dom)
@@ -1770,6 +1820,7 @@
 	(put-text-property start (1+ start) 'shr-prefix-length (length bullet))
 	(shr-generic dom))))
   (unless (bolp)
+	(unless shr-use-fonts (insert "\x2028")) ; insert a line separator
     (insert "\n")))
 
 (defun shr-mark-fill (start)
@@ -1785,7 +1836,8 @@
 	     (or (not (bolp))
 		 (and (> (- (point) 2) (point-min))
 		      (not (= (char-after (- (point) 2)) ?\n)))))
-    (insert "\n"))
+	(unless shr-use-fonts (insert "\x2028")) ; insert a line separator
+	(insert "\n"))
   (shr-generic dom))
 
 (defun shr-tag-span (dom)