From 5ee1cf198da259c913a944f6d520d66b5f33e0f4 Mon Sep 17 00:00:00 2001 From: Jethro Kuan Date: Wed, 12 Feb 2020 13:36:45 +0800 Subject: [PATCH] Refactor into org-roam--make-file --- org-roam.el | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/org-roam.el b/org-roam.el index 3a4560d..6403e81 100644 --- a/org-roam.el +++ b/org-roam.el @@ -199,16 +199,22 @@ If not provided, derive the title from the file name." "\n\n") nil file nil))) +(defun org-roam--make-file (file-path &optional title) + "Create an org-roam file at FILE-PATH, optionally setting the TITLE attribute." + (if (file-exists-p file-path) + (error (format "Aborting, file already exists at " file-path)) + (if org-roam-autopopulate-title + (org-roam--populate-title file-path title) + (make-empty-file file-path)))) + (defun org-roam--new-file-named (slug) "Create a new file named `SLUG'. `SLUG' is the short file name, without a path or a file extension." (interactive "sNew filename (without extension): ") - (let ((file (org-roam--get-file-path slug t))) - (unless (file-exists-p file) - (if org-roam-autopopulate-title - (org-roam--populate-title file) - (make-empty-file file))) - (find-file file))) + (let ((file-path (org-roam--get-file-path slug t))) + (unless (file-exists-p file-path) + (org-roam--make-file file-path)) + (find-file file-path))) (defun org-roam-new-file () "Quickly create a new file, using the current timestamp." @@ -234,9 +240,7 @@ If not provided, derive the title from the file name." (read-string "Enter new file id: ")))) (let ((file-path (org-roam--get-file-path id))) (unless (file-exists-p file-path) - (if org-roam-autopopulate-title - (org-roam--populate-title file-path title) - (make-empty-file file-path))) + (org-roam--make-file file-path title)) (insert (format "[[%s][%s]]" (concat "file:" file-path) (format org-roam-link-title-format title)))))) @@ -246,9 +250,7 @@ If not provided, derive the title from the file name." (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) - (if org-roam-autopopulate-title - (org-roam--populate-title file-path) - (make-empty-file file-path))) + (org-roam--make-file file-path)) (insert (format "[[%s][%s]]" (concat "file:" file-path) (format org-roam-link-id-format id))))) @@ -266,9 +268,7 @@ If not provided, derive the title from the file name." (let ((id (read-string "Enter new file id: "))) (setq file-path (org-roam--get-file-path id t)))) (unless (file-exists-p file-path) - (if org-roam-autopopulate-title - (org-roam--populate-title file-path title-or-id) - (make-empty-file file-path))) + (org-roam--make-file file-path title-or-id)) (find-file file-path))) ;;; Building the org-roam cache (asynchronously)