From 5bc5de2fc6f7783b0cd71c5945755fc98431aa60 Mon Sep 17 00:00:00 2001 From: Maxim Cournoyer Date: Tue, 31 Dec 2024 10:18:30 +0900 Subject: [PATCH 3/3] wip: work on solution --- lisp/emacs-lisp/lisp-mode.el | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/lisp/emacs-lisp/lisp-mode.el b/lisp/emacs-lisp/lisp-mode.el index d0c32d238bc..6c6d88f73a5 100644 --- a/lisp/emacs-lisp/lisp-mode.el +++ b/lisp/emacs-lisp/lisp-mode.el @@ -1,6 +1,6 @@ ;;; lisp-mode.el --- Lisp mode, and its idiosyncratic commands -*- lexical-binding:t -*- -;; Copyright (C) 1985-1986, 1999-2024 Free Software Foundation, Inc. +;; Copyright (C) 1985-1986, 1999-2025 Free Software Foundation, Inc. ;; Maintainer: emacs-devel@gnu.org ;; Keywords: lisp, languages @@ -1480,30 +1480,34 @@ lisp-fill-paragraph (derived-mode-p 'emacs-lisp-mode)) emacs-lisp-docstring-fill-column fill-column))) - (let ((ppss (syntax-ppss)) - (start (point)) - ;; Avoid recursion if we're being called directly with - ;; `M-x lisp-fill-paragraph' in an `emacs-lisp-mode' buffer. - (fill-paragraph-function t)) + (let* ((ppss (syntax-ppss)) + (start (point)) + ;; Avoid recursion if we're being called directly with + ;; `M-x lisp-fill-paragraph' in an `emacs-lisp-mode' buffer. + (fill-paragraph-function t) + (string-start (ppss-comment-or-string-start ppss)) + (indent (save-excursion + (goto-char string-start) + (current-column)))) (save-excursion (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)) + (if (not string-start) (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)) + (goto-char string-start) ;; The string may be unterminated -- in that case, don't ;; narrow. (when (ignore-errors (progn (forward-sexp 1) t)) - (narrow-to-region (1+ (ppss-comment-or-string-start ppss)) + (narrow-to-region (1+ string-start) (1- (point))))) ;; Move back to where we were. (goto-char start) @@ -1516,7 +1520,16 @@ lisp-fill-paragraph (goto-char (point-min)) (forward-line 1) (narrow-to-region (point) (point-max)))) - (fill-paragraph justify))))))) + ;; Insert spaces to reproduce the same leading indent + ;; for the string inside the narrowed region, to avoid + ;; bug#56197. + (save-excursion + (goto-char (point-min)) + (insert (make-string indent ?\s))) + (fill-paragraph justify) + (save-excursion ;clean up + (goto-char (point-min)) + (delete-char indent)))))))) ;; Never return nil. t) -- 2.46.0