Subject: bug#56335: 29.0.50; [PATCH] Add more breakpoint chars support to longlines-mode

From: Manuel Giraud <[email-masked]>
Date: Fri, 01 Jul 2022 15:33:20 +0200

Manuel Giraud <[email-masked]> writes:

[...]

> I'd like to prepare another patch afterward to be able to find
> breakpoint past fill-column.  For example, the file from the
> conversation fails to break after some lines because it contains
> *really* long words.

I missed a typo in my previous patch. Here is the new one. It also fixes
the issue of the file with really long words (I also add it as
attachment to ease testing).

>From 317d4973ca49e7458724a0c8d8bdf57a65a028cc Mon Sep 17 00:00:00 2001
From: Manuel Giraud <[email-masked]>
Date: Fri, 1 Jul 2022 12:02:42 +0200
Subject: [PATCH] Add more separators to longlines-mode.

* lisp/obsolete/longlines.el (longlines-breakpoint-chars): New
custom to have multiple breakpoint chars.
(longlines-set-breakpoint): Add a target-column parameter and use
`longlines-breakpoint-chars'.
(longlines-find-break-backward, longlines-find-break-foreward):
Use `longlines-breakpoint-chars'.
(longlines-wrap-line): Do not insert space upon merging, just
remove the soft newline. Fix "space before tab" in indent.
(longlines-merge-lines-p): Use the new target-column parameter to
find out if the next line could be merged with the current one.
(longlines-encode-region): Do not replace a soft newline with a
space, just remove it.
* etc/NEWS: New user option 'longlines-breakpoint-chars'
---
 etc/NEWS                   |  5 ++
 lisp/obsolete/longlines.el | 99 ++++++++++++++++++--------------------
 2 files changed, 52 insertions(+), 52 deletions(-)

diff --git a/etc/NEWS b/etc/NEWS
index e757435ff9..1457f35c50 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -419,6 +419,11 @@ including those typed in response to passwords prompt (this was the
 previous behavior).  The default is nil, which inhibits recording of
 passwords.
 
++++
+** New user option 'longlines-breakpoint-chars'.
+This is a string containing chars that could be used as breakpoint in
+longlines mode.
+
 +++
 ** New function 'command-query'.
 This function makes its argument command prompt the user for
diff --git a/lisp/obsolete/longlines.el b/lisp/obsolete/longlines.el
index 731f47794c..5b67d4c9ac 100644
--- a/lisp/obsolete/longlines.el
+++ b/lisp/obsolete/longlines.el
@@ -73,6 +73,10 @@ longlines-show-effect
 This is used when `longlines-show-hard-newlines' is on."
   :type 'string)
 
+(defcustom longlines-breakpoint-chars " ;,|"
+  "A bag of separator chars for longlines."
+  :type 'string)
+
 ;;; Internal variables
 
 (defvar longlines-wrap-beg nil)
@@ -273,11 +277,8 @@ longlines-wrap-line
   "If the current line needs to be wrapped, wrap it and return nil.
 If wrapping is performed, point remains on the line.  If the line does
 not need to be wrapped, move point to the next line and return t."
-  (if (longlines-set-breakpoint)
+  (if (longlines-set-breakpoint fill-column)
       (progn (insert-before-markers-and-inherit ?\n)
-	     (backward-char 1)
-             (delete-char -1)
-	     (forward-char 1)
              nil)
     (if (longlines-merge-lines-p)
         (progn (end-of-line)
@@ -286,58 +287,59 @@ longlines-wrap-line
      ;; replace these two newlines by a single space.  Unfortunately,
      ;; this breaks the conservation of (spaces + newlines), so we
      ;; have to fiddle with longlines-wrap-point.
-	       (if (or (prog1 (bolp) (forward-char 1)) (eolp))
-		   (progn
-		     (delete-char -1)
-		     (if (> longlines-wrap-point (point))
-			 (setq longlines-wrap-point
-			       (1- longlines-wrap-point))))
-		 (insert-before-markers-and-inherit ?\s)
-		 (backward-char 1)
-		 (delete-char -1)
-		 (forward-char 1))
+               (if (or (prog1 (bolp) (forward-char 1)) (eolp))
+	           (progn
+	             (delete-char -1)
+	             (if (> longlines-wrap-point (point))
+		         (setq longlines-wrap-point
+		               (1- longlines-wrap-point))))
+	         (delete-char -1))
                nil)
       (forward-line 1)
       t)))
 
-(defun longlines-set-breakpoint ()
+(defun longlines-set-breakpoint (target-column)
   "Place point where we should break the current line, and return t.
 If the line should not be broken, return nil; point remains on the
 line."
-  (move-to-column fill-column)
-  (if (and (re-search-forward "[^ ]" (line-end-position) 1)
-           (> (current-column) fill-column))
-      ;; This line is too long.  Can we break it?
-      (or (longlines-find-break-backward)
-          (progn (move-to-column fill-column)
-                 (longlines-find-break-forward)))))
+  (move-to-column target-column)
+  (let ((non-breakpoint-re (format "[^%s]" longlines-breakpoint-chars)))
+    (if (and (re-search-forward non-breakpoint-re (line-end-position) t 1)
+             (> (current-column) target-column))
+        ;; This line is too long.  Can we break it?
+        (or (longlines-find-break-backward)
+            (progn (move-to-column target-column)
+                   (longlines-find-break-forward))))))
 
 (defun longlines-find-break-backward ()
   "Move point backward to the first available breakpoint and return t.
 If no breakpoint is found, return nil."
-  (and (search-backward " " (line-beginning-position) 1)
-       (save-excursion
-         (skip-chars-backward " " (line-beginning-position))
-         (null (bolp)))
-       (progn (forward-char 1)
-              (if (and fill-nobreak-predicate
-                       (run-hook-with-args-until-success
-                        'fill-nobreak-predicate))
-                  (progn (skip-chars-backward " " (line-beginning-position))
-                         (longlines-find-break-backward))
-                t))))
+  (let ((breakpoint-re (format "[%s]" longlines-breakpoint-chars)))
+    (and (re-search-backward breakpoint-re (line-beginning-position) t 1)
+         (save-excursion
+           (skip-chars-backward longlines-breakpoint-chars (line-beginning-position))
+           (null (bolp)))
+         (progn (forward-char 1)
+                (if (and fill-nobreak-predicate
+                         (run-hook-with-args-until-success
+                          'fill-nobreak-predicate))
+                    (progn (skip-chars-backward longlines-breakpoint-chars
+                                                (line-beginning-position))
+                           (longlines-find-break-backward))
+                  t)))))
 
 (defun longlines-find-break-forward ()
   "Move point forward to the first available breakpoint and return t.
 If no break point is found, return nil."
-  (and (search-forward " " (line-end-position) 1)
-       (progn (skip-chars-forward " " (line-end-position))
-              (null (eolp)))
-       (if (and fill-nobreak-predicate
-                (run-hook-with-args-until-success
-                 'fill-nobreak-predicate))
-           (longlines-find-break-forward)
-         t)))
+  (let ((breakpoint-re (format "[%s]" longlines-breakpoint-chars)))
+    (and (re-search-forward breakpoint-re (line-end-position) t 1)
+         (progn (skip-chars-forward longlines-breakpoint-chars (line-end-position))
+                (null (eolp)))
+         (if (and fill-nobreak-predicate
+                  (run-hook-with-args-until-success
+                   'fill-nobreak-predicate))
+             (longlines-find-break-forward)
+           t))))
 
 (defun longlines-merge-lines-p ()
   "Return t if part of the next line can fit onto the current line.
@@ -348,12 +350,7 @@ longlines-merge-lines-p
          (null (get-text-property (point) 'hard))
          (let ((space (- fill-column (current-column))))
            (forward-line 1)
-           (if (eq (char-after) ? )
-               t ; We can always merge some spaces
-             (<= (if (search-forward " " (line-end-position) 1)
-                     (current-column)
-                   (1+ (current-column)))
-                 space))))))
+           (longlines-set-breakpoint (max 0 (1- space)))))))
 
 (defun longlines-decode-region (&optional beg end)
   "Turn all newlines between BEG and END into hard newlines.
@@ -372,7 +369,7 @@ longlines-decode-buffer
   (longlines-decode-region (point-min) (point-max)))
 
 (defun longlines-encode-region (beg end &optional _buffer)
-  "Replace each soft newline between BEG and END with exactly one space.
+  "Remove each soft newline between BEG and END.
 Hard newlines are left intact.  The optional argument BUFFER exists for
 compatibility with `format-alist', and is ignored."
   (save-excursion
@@ -382,10 +379,8 @@ longlines-encode-region
       (while (search-forward "\n" reg-max t)
 	(let ((pos (match-beginning 0)))
 	  (unless (get-text-property pos 'hard)
-	    (goto-char (1+ pos))
-	    (insert-and-inherit " ")
-	    (delete-region pos (1+ pos))
-            (remove-text-properties pos (1+ pos) '(hard nil)))))
+            (remove-text-properties pos (1+ pos) '(hard nil))
+            (delete-region pos (1+ pos)))))
       (set-buffer-modified-p mod)
       end)))
 
-- 
2.36.1

-- 
Manuel Giraud