From 1132d2cb2bac59ae41c409756688e49965d2aa53 Mon Sep 17 00:00:00 2001 From: Yuan Fu Date: Mon, 9 Jun 2025 22:12:49 -0700 Subject: [PATCH] Extract out prog--text-at-point-p from prog-fill-reindent-defun * lisp/progmodes/prog-mode.el (prog--text-at-point-p): New function. (prog-fill-reindent-defun): Use new function. (cherry picked from commit 32bc6914e5fe696e51f03a035f7ca73c98dbb1a5) --- lisp/progmodes/prog-mode.el | 48 ++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/lisp/progmodes/prog-mode.el b/lisp/progmodes/prog-mode.el index eee9cd3bce2..09775a55ab3 100644 --- a/lisp/progmodes/prog-mode.el +++ b/lisp/progmodes/prog-mode.el @@ -143,6 +143,31 @@ instead." (end (progn (forward-sexp 1) (point)))) (indent-region start end nil)))) +(defun prog--text-at-point-p () + "Return non-nil if point is in text (comment or string)." + ;; FIXME: For some reason, the comment-start syntax regexp doesn't + ;; work for me. But I kept it around to be safe, and in the hope + ;; that it can cover cases where comment-start-skip is unset. + (or (nth 8 (syntax-ppss)) + ;; If point is at the beginning of a comment delimiter, + ;; syntax-ppss doesn't consider point as being inside a + ;; comment. + (save-excursion + (beginning-of-line) + (and comment-start-skip + ;; FIXME: This doesn't work for the case where there + ;; are two matches of comment-start-skip, and the + ;; first one is, say, inside a string. We need to + ;; call re-search-forward repeatedly until either + ;; reached EOL or (nth 4 (syntax-ppss)) returns + ;; non-nil. + (re-search-forward comment-start-skip (pos-eol) t) + (nth 8 (syntax-ppss)))) + (save-excursion + (beginning-of-line) + (and (re-search-forward "\\s-*\\s<" (line-end-position) t) + (nth 8 (syntax-ppss)))))) + (defun prog-fill-reindent-defun (&optional argument) "Refill or reindent the paragraph or defun that contains point. @@ -153,28 +178,7 @@ Otherwise, reindent the function definition that contains point or follows point." (interactive "P") (save-excursion - ;; FIXME: For some reason, the comment-start syntax regexp doesn't - ;; work for me. But I kept it around to be safe, and in the hope - ;; that it can cover cases where comment-start-skip is unset. - (if (or (nth 8 (syntax-ppss)) - ;; If point is at the beginning of a comment delimiter, - ;; syntax-ppss doesn't consider point as being inside a - ;; comment. - (save-excursion - (beginning-of-line) - (and comment-start-skip - ;; FIXME: This doesn't work for the case where there - ;; are two matches of comment-start-skip, and the - ;; first one is, say, inside a string. We need to - ;; call re-search-forward repeatedly until either - ;; reached EOL or (nth 4 (syntax-ppss)) returns - ;; non-nil. - (re-search-forward comment-start-skip (pos-eol) t) - (nth 8 (syntax-ppss)))) - (save-excursion - (beginning-of-line) - (and (re-search-forward "\\s-*\\s<" (line-end-position) t) - (nth 8 (syntax-ppss))))) + (if (prog--text-at-point-p) (fill-paragraph argument (region-active-p)) (beginning-of-defun) (let ((start (point))) -- 2.39.5