mirror of
https://github.com/org-roam/org-roam
synced 2025-08-01 12:17:21 -05:00
(feat): Protect region targeted by org-roam-insert
(#974)
Co-authored-by: Jethro Kuan <jethrokuan95@gmail.com>
This commit is contained in:
@ -31,6 +31,7 @@
|
||||
;;; Code:
|
||||
;;;; Library Requires
|
||||
(require 'org-capture)
|
||||
(require 'org-roam-macs)
|
||||
(require 'dash)
|
||||
(require 's)
|
||||
(require 'cl-lib)
|
||||
@ -320,26 +321,35 @@ the capture)."
|
||||
|
||||
(defun org-roam-capture--finalize ()
|
||||
"Finalize the `org-roam-capture' process."
|
||||
(unless org-note-abort
|
||||
(pcase (org-roam-capture--get :finalize)
|
||||
('find-file
|
||||
(when-let ((file-path (org-roam-capture--get :file-path)))
|
||||
(org-roam--find-file file-path)
|
||||
(run-hooks 'org-roam-capture-after-find-file-hook)))
|
||||
('insert-link
|
||||
(when-let* ((mkr (org-roam-capture--get :insert-at))
|
||||
(buf (marker-buffer mkr)))
|
||||
(with-current-buffer buf
|
||||
(when-let ((region (org-roam-capture--get :region))) ;; Remove previously selected text.
|
||||
(delete-region (car region) (cdr region)))
|
||||
(let ((path (org-roam-capture--get :file-path))
|
||||
(desc (org-roam-capture--get :link-description)))
|
||||
(if (eq (point) (marker-position mkr))
|
||||
(insert (org-roam--format-link path desc))
|
||||
(org-with-point-at mkr
|
||||
(insert (org-roam--format-link path desc))))))))))
|
||||
(org-roam-capture--save-file-maybe)
|
||||
(remove-hook 'org-capture-after-finalize-hook #'org-roam-capture--finalize))
|
||||
(let* ((finalize (org-roam-capture--get :finalize))
|
||||
;; In case any regions were shielded before, unshield them
|
||||
(region (when-let ((region (org-roam-capture--get :region)))
|
||||
(org-roam-unshield-region (car region) (cdr region))))
|
||||
(beg (car region))
|
||||
(end (cdr region)))
|
||||
(unless org-note-abort
|
||||
(pcase finalize
|
||||
('find-file
|
||||
(when-let ((file-path (org-roam-capture--get :file-path)))
|
||||
(org-roam--find-file file-path)
|
||||
(run-hooks 'org-roam-capture-after-find-file-hook)))
|
||||
('insert-link
|
||||
(when-let* ((mkr (org-roam-capture--get :insert-at))
|
||||
(buf (marker-buffer mkr)))
|
||||
(with-current-buffer buf
|
||||
(when region
|
||||
(delete-region (car region) (cdr region)))
|
||||
(let ((path (org-roam-capture--get :file-path))
|
||||
(desc (org-roam-capture--get :link-description)))
|
||||
(if (eq (point) (marker-position mkr))
|
||||
(insert (org-roam--format-link path desc))
|
||||
(org-with-point-at mkr
|
||||
(insert (org-roam--format-link path desc))))))))))
|
||||
(when region
|
||||
(set-marker beg nil)
|
||||
(set-marker end nil))
|
||||
(org-roam-capture--save-file-maybe)
|
||||
(remove-hook 'org-capture-after-finalize-hook #'org-roam-capture--finalize)))
|
||||
|
||||
(defun org-roam-capture--install-finalize ()
|
||||
"Install `org-roam-capture--finalize' if the capture is an Org-roam capture."
|
||||
|
Reference in New Issue
Block a user