From 22f596d2754d9ded8627fbf645d20fb00d57403c Mon Sep 17 00:00:00 2001 From: Herbert Jones Date: Fri, 21 Feb 2020 00:44:43 -0600 Subject: [PATCH] (feature): update `org-roam-insert` behaviour on region-selection (#148) `org-roam-insert` now uses active region to pre-populate insert completion, and as the link description fixes #127 --- org-roam.el | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/org-roam.el b/org-roam.el index 17c4e7a..ef50977 100644 --- a/org-roam.el +++ b/org-roam.el @@ -300,11 +300,18 @@ If not provided, derive the title from the file name." If PREFIX, downcase the title before insertion." (interactive "P") - (let* ((completions (mapcar (lambda (file) + (let* ((region (and (region-active-p) + ;; following may lose active region, so save it + (cons (region-beginning) (region-end)))) + (region-text (when region + (buffer-substring-no-properties + (car region) (cdr region)))) + (completions (mapcar (lambda (file) (list (org-roam--get-title-or-slug file) file)) (org-roam--find-all-files))) - (title (completing-read "File: " completions)) + (title (completing-read "File: " completions nil nil region-text)) + (region-or-title (or region-text title)) (absolute-file-path (or (cadr (assoc title completions)) (org-roam--make-new-file-path (org-roam--get-new-id title) t))) (current-file-path (-> (or (buffer-base-buffer) @@ -314,12 +321,15 @@ If PREFIX, downcase the title before insertion." (file-name-directory)))) (unless (file-exists-p absolute-file-path) (org-roam--make-file absolute-file-path title)) + (when region ;; Remove previously selected text. + (goto-char (car region)) + (delete-char (- (cdr region) (car region)))) (insert (format "[[%s][%s]]" (concat "file:" (file-relative-name absolute-file-path current-file-path)) (format org-roam-link-title-format (if prefix - (downcase title) - title)))))) + (downcase region-or-title) + region-or-title)))))) ;;; Finding org-roam files (defun org-roam-find-file ()