Explorar el Código

org-fill-element: Respect region selection when filling src-block

* lisp/org.el (org-fill-element): When region is not active, run
`fill-paragraph' at point inside src block.  When region is active and
within src block boundaries, run `fill-paragraph' preserving the
region.  When region is active and crosses src block boundaries, fill
the whole src block.

Reported-by: Fabio Natali <me@fabionatali.com>
Fixes: https://orgmode.org/list/201b44de-1f97-1b23-1767-970ee00f259c@posteo.eu
Ihor Radchenko hace 2 años
padre
commit
2c8bd0cc9b
Se han modificado 1 ficheros con 12 adiciones y 5 borrados
  1. 12 5
      lisp/org.el

+ 12 - 5
lisp/org.el

@@ -19138,11 +19138,18 @@ a footnote definition, try to fill the first paragraph within."
       ;; the buffer.  In that case, ignore filling.
       (cl-case (org-element-type element)
 	;; Use major mode filling function is source blocks.
-	(src-block (org-babel-do-in-edit-buffer
-                    (push-mark (point-min))
-                    (goto-char (point-max))
-                    (setq mark-active t)
-                    (funcall-interactively #'fill-paragraph justify 'region)))
+        (src-block
+         (let ((regionp (region-active-p)))
+           (org-babel-do-in-edit-buffer
+            ;; `org-babel-do-in-edit-buffer' will preserve region if it
+            ;; is within src block contents.  Otherwise, the region
+            ;; crosses src block boundaries.  We re-fill the whole src
+            ;; block in such scenario.
+            (when (and regionp (not (region-active-p)))
+              (push-mark (point-min))
+              (goto-char (point-max))
+              (setq mark-active t))
+            (funcall-interactively #'fill-paragraph justify 'region))))
 	;; Align Org tables, leave table.el tables as-is.
 	(table-row (org-table-align) t)
 	(table