emacs-diffs
[Top][All Lists]
Advanced

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

master 88a04ea985: Tweak how `M-q' in emacs-lisp-mode works


From: Lars Ingebrigtsen
Subject: master 88a04ea985: Tweak how `M-q' in emacs-lisp-mode works
Date: Tue, 12 Apr 2022 23:07:54 -0400 (EDT)

branch: master
commit 88a04ea985180d1fd619c4a6540fb117a1d59d9e
Author: Lars Ingebrigtsen <larsi@gnus.org>
Commit: Lars Ingebrigtsen <larsi@gnus.org>

    Tweak how `M-q' in emacs-lisp-mode works
    
    * lisp/emacs-lisp/lisp-mode.el (lisp-fill-paragraph): Only fill as
    strings inside strings (bug#31656).
    (lisp--fill-line-simple): New function to do simple sexp-based
    filling.
---
 lisp/emacs-lisp/lisp-mode.el | 62 +++++++++++++++++++++++++++++---------------
 1 file changed, 41 insertions(+), 21 deletions(-)

diff --git a/lisp/emacs-lisp/lisp-mode.el b/lisp/emacs-lisp/lisp-mode.el
index 7df40e36f8..e7c3a4b64f 100644
--- a/lisp/emacs-lisp/lisp-mode.el
+++ b/lisp/emacs-lisp/lisp-mode.el
@@ -1436,29 +1436,49 @@ and initial semicolons."
                                   (derived-mode-p 'emacs-lisp-mode))
                              emacs-lisp-docstring-fill-column
                            fill-column)))
-        (save-restriction
+        (let ((ppss (syntax-ppss))
+              (start (point)))
           (save-excursion
-          (let ((ppss (syntax-ppss))
-                (start (point)))
-            ;; If we're in a string, then narrow (roughly) to that
-            ;; string before filling.  This avoids filling Lisp
-            ;; statements that follow the string.
-            (when (ppss-string-terminator ppss)
-              (goto-char (ppss-comment-or-string-start ppss))
-              (beginning-of-line)
-              ;; The string may be unterminated -- in that case, don't
-              ;; narrow.
-              (when (ignore-errors
-                      (progn
-                        (forward-sexp 1)
-                        t))
-                (narrow-to-region (ppss-comment-or-string-start ppss)
-                                  (point))))
-            ;; Move back to where we were.
+            (save-restriction
+              ;; If we're not inside a string, then do very basic
+              ;; filling.  This avoids corrupting embedded strings in
+              ;; code.
+              (if (not (ppss-comment-or-string-start ppss))
+                  (lisp--fill-line-simple)
+                ;; If we're in a string, then narrow (roughly) to that
+                ;; string before filling.  This avoids filling Lisp
+                ;; statements that follow the string.
+                (when (ppss-string-terminator ppss)
+                  (goto-char (ppss-comment-or-string-start ppss))
+                  ;; The string may be unterminated -- in that case, don't
+                  ;; narrow.
+                  (when (ignore-errors
+                          (progn
+                            (forward-sexp 1)
+                            t))
+                    (narrow-to-region (ppss-comment-or-string-start ppss)
+                                      (point))))
+                ;; Move back to where we were.
+                (goto-char start)
+               (fill-paragraph justify)))))))
+  ;; Never return nil.
+  t)
+
+(defun lisp--fill-line-simple ()
+  (narrow-to-region (line-beginning-position) (line-end-position))
+  (goto-char (point-min))
+  (while (and (not (eobp))
+              (re-search-forward "\\_>" nil t))
+    (when (> (current-column) fill-column)
+      (let ((start (point)))
+        (backward-sexp)
+        (if (looking-back "[[(]" (point-min))
             (goto-char start)
-           (fill-paragraph justify)))))
-      ;; Never return nil.
-      t))
+          (skip-chars-backward " \t")
+          (insert "\n")
+          (forward-sexp))))
+    (unless (eobp)
+      (forward-char 1))))
 
 (defun indent-code-rigidly (start end arg &optional nochange-regexp)
   "Indent all lines of code, starting in the region, sideways by ARG columns.



reply via email to

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