aboutsummaryrefslogtreecommitdiff
path: root/emacs/.emacs.d/lisp
diff options
context:
space:
mode:
authorYuchen Pei <id@ypei.org>2023-09-08 23:41:31 +1000
committerYuchen Pei <id@ypei.org>2023-09-08 23:41:31 +1000
commite229072e3d97738bc2ed50d12d8f2927549f44b8 (patch)
tree4b16255d8c4869f4c2ffa57e7c6c8ef068e5e17f /emacs/.emacs.d/lisp
parent6ec968e2cadca7e4df4aba8960e758aa6c7d00b0 (diff)
[emacs] Fix indenting in org src block invoking org-edit-src-exit
Diffstat (limited to 'emacs/.emacs.d/lisp')
-rw-r--r--emacs/.emacs.d/lisp/my/my-org.el26
1 files changed, 17 insertions, 9 deletions
diff --git a/emacs/.emacs.d/lisp/my/my-org.el b/emacs/.emacs.d/lisp/my/my-org.el
index 03b34ff..38eca7b 100644
--- a/emacs/.emacs.d/lisp/my/my-org.el
+++ b/emacs/.emacs.d/lisp/my/my-org.el
@@ -75,9 +75,14 @@ With a prefix, insert inactive dates.
(when (eq major-mode 'mhtml-mode)
(browse-url-of-buffer)))
+(defvar my-org-edit-src-was-live-window nil
+ "Set to non-nil by `my-org-edit-src-before-exit' if the edit src
+buffer was a live window.")
+
(defun my-org-edit-src-before-exit ()
"A :before advice for org-edit-src-exit."
- (delete-trailing-whitespace))
+ (delete-trailing-whitespace)
+ (setq my-org-edit-src-was-live-window (get-buffer-window (current-buffer))))
(defun my-org-element-block-p (element)
"Returns t if ELEMENT is an org block."
@@ -87,17 +92,20 @@ With a prefix, insert inactive dates.
special-block src-block verse-block)))
(defun my-org-edit-src-after-exit ()
- "An :after advice for org-edit-src-exit.
+ "An :after advice for `org-edit-src-exit'.
If the block is empty, remove it.
Otherwise deactivate mark and move point to after the block."
- (deactivate-mark)
- (let ((element (org-element-at-point)))
- (cl-assert (my-org-element-block-p element))
- (if (string-empty-p (org-element-property :value element))
- (delete-region (org-element-property :begin element)
- (org-element-property :end element))
- (goto-char (org-element-property :end element)))))
+ ;; Only proceed if buffer is in a live window, needed to avoid
+ ;; indenting triggering this function
+ (when my-org-edit-src-was-live-window
+ (deactivate-mark)
+ (let ((element (org-element-at-point)))
+ (cl-assert (my-org-element-block-p element))
+ (if (string-empty-p (org-element-property :value element))
+ (delete-region (org-element-property :begin element)
+ (org-element-property :end element))
+ (goto-char (org-element-property :end element))))))
;; navigation
(defun my-org-jump-to-last-visible-child ()