]> git.eshelyaron.com Git - emacs.git/commitdiff
Extract out prog--text-at-point-p from prog-fill-reindent-defun
authorYuan Fu <casouri@gmail.com>
Tue, 10 Jun 2025 05:12:49 +0000 (22:12 -0700)
committerEshel Yaron <me@eshelyaron.com>
Wed, 18 Jun 2025 08:02:01 +0000 (10:02 +0200)
* 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

index eee9cd3bce260e6ff630605fdb84bb51c17f1227..09775a55ab3044a2cda50e9d26a7d14c13d471af 100644 (file)
@@ -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)))