mirror of
https://github.com/org-roam/org-roam
synced 2025-09-28 16:50:58 -05:00
Add org-roam--insert-title
This commit is contained in:
51
org-roam.el
51
org-roam.el
@@ -37,6 +37,16 @@ Valid values are
|
|||||||
(const right))
|
(const right))
|
||||||
:group 'org-roam)
|
:group 'org-roam)
|
||||||
|
|
||||||
|
(defcustom org-roam-link-representation 'id
|
||||||
|
"The value used to represent an org-roam link.
|
||||||
|
|
||||||
|
Valid values are
|
||||||
|
* file,
|
||||||
|
* title."
|
||||||
|
:type '(choice (const id)
|
||||||
|
(const title))
|
||||||
|
:group 'org-roam)
|
||||||
|
|
||||||
(defcustom org-roam-buffer-width 0.33 "Width of `org-roam' buffer."
|
(defcustom org-roam-buffer-width 0.33 "Width of `org-roam' buffer."
|
||||||
:type 'number
|
:type 'number
|
||||||
:group 'org-roam)
|
:group 'org-roam)
|
||||||
@@ -90,6 +100,16 @@ If called interactively, then PARENTS is non-nil."
|
|||||||
(f-child-of-p (file-truename (buffer-file-name (current-buffer)))
|
(f-child-of-p (file-truename (buffer-file-name (current-buffer)))
|
||||||
org-roam-directory)))
|
org-roam-directory)))
|
||||||
|
|
||||||
|
(defun org-roam--get-title (file)
|
||||||
|
"Return title of `FILE'.
|
||||||
|
|
||||||
|
It first tries the cache. If the cache does not contain the file,
|
||||||
|
it will return the title by loading the file."
|
||||||
|
(if-let ((titles-cache (plist-get org-roam-cache :titles)))
|
||||||
|
(or (gethash file titles-cache)
|
||||||
|
(org-roam--extract-file-title file))
|
||||||
|
(org-roam--extract-file-title file)))
|
||||||
|
|
||||||
(defun org-roam--find-files (dir)
|
(defun org-roam--find-files (dir)
|
||||||
"Return all org-roam files in `DIR'."
|
"Return all org-roam files in `DIR'."
|
||||||
(if (file-exists-p dir)
|
(if (file-exists-p dir)
|
||||||
@@ -132,12 +152,35 @@ If `ABSOLUTE', return the absolute file-path. Else, return the relative file-pat
|
|||||||
(file-truename org-roam-directory))))
|
(file-truename org-roam-directory))))
|
||||||
|
|
||||||
;;; Inserting org-roam links
|
;;; Inserting org-roam links
|
||||||
(defun org-roam-insert (id)
|
(defun org-roam-insert ()
|
||||||
|
"Insert an org-roam link."
|
||||||
|
(interactive)
|
||||||
|
(pcase org-roam-link-representation
|
||||||
|
('id (org-roam--insert-id))
|
||||||
|
('title (org-roam--insert-title))))
|
||||||
|
|
||||||
|
(defun org-roam--insert-title ()
|
||||||
"Find `ID', and insert a relative org link to it at point."
|
"Find `ID', and insert a relative org link to it at point."
|
||||||
(interactive (list (completing-read "File: "
|
(let* ((completions (mapcar (lambda (file)
|
||||||
(mapcar #'org-roam--get-id
|
(list (or (org-roam--get-title file)
|
||||||
(org-roam--find-all-files)))))
|
(org-roam--get-id file))
|
||||||
|
(org-roam--get-id file)))
|
||||||
|
(org-roam--find-all-files)))
|
||||||
|
(title (completing-read "File: " completions))
|
||||||
|
(id (cadr (assoc title completions))))
|
||||||
|
(unless id
|
||||||
|
(setq id (read-string "Enter new file id: ")))
|
||||||
(let ((file-path (org-roam--get-file-path id)))
|
(let ((file-path (org-roam--get-file-path id)))
|
||||||
|
(unless (file-exists-p file-path)
|
||||||
|
(make-empty-file file-path))
|
||||||
|
(insert (format "[[%s][%s]]"
|
||||||
|
(concat "file:" file-path)
|
||||||
|
title)))))
|
||||||
|
|
||||||
|
(defun org-roam--insert-id ()
|
||||||
|
"Find `ID', and insert a relative org link to it at point."
|
||||||
|
(let* ((id (completing-read "File: " (mapcar #'org-roam--get-id (org-roam--find-all-files))))
|
||||||
|
(file-path (org-roam--get-file-path id)))
|
||||||
(unless (file-exists-p file-path)
|
(unless (file-exists-p file-path)
|
||||||
(make-empty-file file-path))
|
(make-empty-file file-path))
|
||||||
(insert (format "[[%s][%s]]"
|
(insert (format "[[%s][%s]]"
|
||||||
|
Reference in New Issue
Block a user