(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
This commit is contained in:
Herbert Jones
2020-02-21 00:44:43 -06:00
committed by GitHub
parent cb029c4ce8
commit 22f596d275

View File

@ -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 ()