From 8777839a4fe5da6c9a780eac946e1a8a892d4f22 Mon Sep 17 00:00:00 2001 Message-Id: <8777839a4fe5da6c9a780eac946e1a8a892d4f22.1664788728.git.yantar92@gmail.com> From: Ihor Radchenko Date: Mon, 3 Oct 2022 17:03:15 +0800 Subject: [PATCH] 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 Fixes: https://orgmode.org/list/201b44de-1f97-1b23-1767-970ee00f259c@posteo.eu --- lisp/org.el | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/lisp/org.el b/lisp/org.el index 036384a04..23cf4198e 100644 --- a/lisp/org.el +++ b/lisp/org.el @@ -19135,11 +19135,18 @@ (defun org-fill-element (&optional justify) ;; 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 -- 2.35.1