(feat): Protect region targeted by org-roam-insert
(#974)
Co-authored-by: Jethro Kuan <jethrokuan95@gmail.com>
This commit is contained in:
parent
f2c1500beb
commit
6345d0c22e
4 changed files with 113 additions and 56 deletions
10
CHANGELOG.md
10
CHANGELOG.md
|
@ -1,5 +1,15 @@
|
||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
|
## 1.2.2 (TBD)
|
||||||
|
|
||||||
|
### Breaking Changes
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
-[#974](https://github.com/org-roam/org-roam/pull/974) Protect region targeted by `org-roam-insert`
|
||||||
|
|
||||||
|
### Bugfixes
|
||||||
|
|
||||||
## 1.2.1 (27-07-2020)
|
## 1.2.1 (27-07-2020)
|
||||||
|
|
||||||
This release consisted of a big deal of refactoring and bug fixes. Notably, we fixed several catastrophic failures on db builds with bad setups (#854), and modularized tag and title extractions.
|
This release consisted of a big deal of refactoring and bug fixes. Notably, we fixed several catastrophic failures on db builds with bad setups (#854), and modularized tag and title extractions.
|
||||||
|
|
|
@ -31,6 +31,7 @@
|
||||||
;;; Code:
|
;;; Code:
|
||||||
;;;; Library Requires
|
;;;; Library Requires
|
||||||
(require 'org-capture)
|
(require 'org-capture)
|
||||||
|
(require 'org-roam-macs)
|
||||||
(require 'dash)
|
(require 'dash)
|
||||||
(require 's)
|
(require 's)
|
||||||
(require 'cl-lib)
|
(require 'cl-lib)
|
||||||
|
@ -320,8 +321,14 @@ the capture)."
|
||||||
|
|
||||||
(defun org-roam-capture--finalize ()
|
(defun org-roam-capture--finalize ()
|
||||||
"Finalize the `org-roam-capture' process."
|
"Finalize the `org-roam-capture' process."
|
||||||
|
(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
|
(unless org-note-abort
|
||||||
(pcase (org-roam-capture--get :finalize)
|
(pcase finalize
|
||||||
('find-file
|
('find-file
|
||||||
(when-let ((file-path (org-roam-capture--get :file-path)))
|
(when-let ((file-path (org-roam-capture--get :file-path)))
|
||||||
(org-roam--find-file file-path)
|
(org-roam--find-file file-path)
|
||||||
|
@ -330,7 +337,7 @@ the capture)."
|
||||||
(when-let* ((mkr (org-roam-capture--get :insert-at))
|
(when-let* ((mkr (org-roam-capture--get :insert-at))
|
||||||
(buf (marker-buffer mkr)))
|
(buf (marker-buffer mkr)))
|
||||||
(with-current-buffer buf
|
(with-current-buffer buf
|
||||||
(when-let ((region (org-roam-capture--get :region))) ;; Remove previously selected text.
|
(when region
|
||||||
(delete-region (car region) (cdr region)))
|
(delete-region (car region) (cdr region)))
|
||||||
(let ((path (org-roam-capture--get :file-path))
|
(let ((path (org-roam-capture--get :file-path))
|
||||||
(desc (org-roam-capture--get :link-description)))
|
(desc (org-roam-capture--get :link-description)))
|
||||||
|
@ -338,8 +345,11 @@ the capture)."
|
||||||
(insert (org-roam--format-link path desc))
|
(insert (org-roam--format-link path desc))
|
||||||
(org-with-point-at mkr
|
(org-with-point-at mkr
|
||||||
(insert (org-roam--format-link path desc))))))))))
|
(insert (org-roam--format-link path desc))))))))))
|
||||||
|
(when region
|
||||||
|
(set-marker beg nil)
|
||||||
|
(set-marker end nil))
|
||||||
(org-roam-capture--save-file-maybe)
|
(org-roam-capture--save-file-maybe)
|
||||||
(remove-hook 'org-capture-after-finalize-hook #'org-roam-capture--finalize))
|
(remove-hook 'org-capture-after-finalize-hook #'org-roam-capture--finalize)))
|
||||||
|
|
||||||
(defun org-roam-capture--install-finalize ()
|
(defun org-roam-capture--install-finalize ()
|
||||||
"Install `org-roam-capture--finalize' if the capture is an Org-roam capture."
|
"Install `org-roam-capture--finalize' if the capture is an Org-roam capture."
|
||||||
|
|
|
@ -77,6 +77,28 @@ to look.
|
||||||
(s-replace "\\" "\\\\")
|
(s-replace "\\" "\\\\")
|
||||||
(s-replace "\"" "\\\"")))
|
(s-replace "\"" "\\\"")))
|
||||||
|
|
||||||
|
;;; Shielding regions
|
||||||
|
(defun org-roam-shield-region (beg end)
|
||||||
|
"Shield REGION against modifications.
|
||||||
|
REGION must be a cons-cell containing the marker to the region
|
||||||
|
beginning and maximum values."
|
||||||
|
(when (and beg end)
|
||||||
|
(add-text-properties beg end
|
||||||
|
'(font-lock-face org-roam-link-shielded
|
||||||
|
read-only t)
|
||||||
|
(marker-buffer beg))
|
||||||
|
(cons beg end)))
|
||||||
|
|
||||||
|
(defun org-roam-unshield-region (beg end)
|
||||||
|
"Unshield the shielded REGION."
|
||||||
|
(when (and beg end)
|
||||||
|
(let ((inhibit-read-only t))
|
||||||
|
(remove-text-properties beg end
|
||||||
|
'(font-lock-face org-roam-link-shielded
|
||||||
|
read-only t)
|
||||||
|
(marker-buffer beg)))
|
||||||
|
(cons beg end)))
|
||||||
|
|
||||||
(provide 'org-roam-macs)
|
(provide 'org-roam-macs)
|
||||||
|
|
||||||
;;; org-roam-macs.el ends here
|
;;; org-roam-macs.el ends here
|
||||||
|
|
37
org-roam.el
37
org-roam.el
|
@ -960,6 +960,13 @@ Return nil if the file does not exist."
|
||||||
This face is used for links without a destination."
|
This face is used for links without a destination."
|
||||||
:group 'org-roam-faces)
|
:group 'org-roam-faces)
|
||||||
|
|
||||||
|
(defface org-roam-link-shielded
|
||||||
|
'((t :inherit (warning org-link)))
|
||||||
|
"Face for Org-roam links that are shielded.
|
||||||
|
This face is used on the region target by `org-roam-insertion'
|
||||||
|
during an `org-roam-capture'."
|
||||||
|
:group 'org-roam-faces)
|
||||||
|
|
||||||
;;;; org-roam-backlinks-mode
|
;;;; org-roam-backlinks-mode
|
||||||
(define-minor-mode org-roam-backlinks-mode
|
(define-minor-mode org-roam-backlinks-mode
|
||||||
"Minor mode for the `org-roam-buffer'.
|
"Minor mode for the `org-roam-buffer'.
|
||||||
|
@ -1420,11 +1427,16 @@ If DESCRIPTION is provided, use this as the link label. See
|
||||||
`org-roam--get-title-path-completions' for details."
|
`org-roam--get-title-path-completions' for details."
|
||||||
(interactive "P")
|
(interactive "P")
|
||||||
(unless org-roam-mode (org-roam-mode))
|
(unless org-roam-mode (org-roam-mode))
|
||||||
(let* ((region (and (region-active-p)
|
;; Deactivate the mark on quit since `atomic-change-group' prevents it
|
||||||
;; following may lose active region, so save it
|
(unwind-protect
|
||||||
(cons (region-beginning) (region-end))))
|
;; Group functions together to avoid inconsistent state on quit
|
||||||
(region-text (when region
|
(atomic-change-group
|
||||||
(buffer-substring-no-properties (car region) (cdr region))))
|
(let* (region-text
|
||||||
|
beg end
|
||||||
|
(_ (when (region-active-p)
|
||||||
|
(setq beg (set-marker (make-marker) (region-beginning)))
|
||||||
|
(setq end (set-marker (make-marker) (region-end)))
|
||||||
|
(setq region-text (buffer-substring-no-properties beg end))))
|
||||||
(completions (--> (or completions
|
(completions (--> (or completions
|
||||||
(org-roam--get-title-path-completions))
|
(org-roam--get-title-path-completions))
|
||||||
(if filter-fn
|
(if filter-fn
|
||||||
|
@ -1440,22 +1452,25 @@ If DESCRIPTION is provided, use this as the link label. See
|
||||||
(link-description (org-roam--format-link-title (if lowercase
|
(link-description (org-roam--format-link-title (if lowercase
|
||||||
(downcase description)
|
(downcase description)
|
||||||
description))))
|
description))))
|
||||||
(if (and target-file-path
|
(cond ((and target-file-path
|
||||||
(file-exists-p target-file-path))
|
(file-exists-p target-file-path))
|
||||||
(progn
|
(when region-text
|
||||||
(when region ;; Remove previously selected text.
|
(delete-region beg end)
|
||||||
(delete-region (car region) (cdr region)))
|
(set-marker beg nil)
|
||||||
|
(set-marker end nil))
|
||||||
(insert (org-roam--format-link target-file-path link-description)))
|
(insert (org-roam--format-link target-file-path link-description)))
|
||||||
|
(t
|
||||||
(let ((org-roam-capture--info `((title . ,title-with-tags)
|
(let ((org-roam-capture--info `((title . ,title-with-tags)
|
||||||
(slug . ,(funcall org-roam-title-to-slug-function title-with-tags))))
|
(slug . ,(funcall org-roam-title-to-slug-function title-with-tags))))
|
||||||
(org-roam-capture--context 'title))
|
(org-roam-capture--context 'title))
|
||||||
(setq org-roam-capture-additional-template-props (list :region region
|
(setq org-roam-capture-additional-template-props (list :region (org-roam-shield-region beg end)
|
||||||
:insert-at (point-marker)
|
:insert-at (point-marker)
|
||||||
:link-description link-description
|
:link-description link-description
|
||||||
:finalize 'insert-link))
|
:finalize 'insert-link))
|
||||||
(org-roam--with-template-error 'org-roam-capture-templates
|
(org-roam--with-template-error 'org-roam-capture-templates
|
||||||
(org-roam-capture--capture))))
|
(org-roam-capture--capture)))))
|
||||||
res))
|
res))
|
||||||
|
(deactivate-mark)))
|
||||||
|
|
||||||
;;;###autoload
|
;;;###autoload
|
||||||
(defun org-roam-insert-immediate (arg &rest args)
|
(defun org-roam-insert-immediate (arg &rest args)
|
||||||
|
|
Loading…
Reference in a new issue