From ce305af3193bbcb0d5af3b4c4bbd0ffc815f9da2 Mon Sep 17 00:00:00 2001 From: Jethro Kuan Date: Mon, 17 Feb 2020 13:50:40 +0800 Subject: [PATCH] (breaking): change org-roam-file-format to a function (#103) This allows for more flexible naming of files. Now filename defaults to yyyymmddhhmmss_title_here.org. Also, remove `org-use-timestamp-as-filename`, and change it to `org-roam-filename-noconfirm` to better describe what it is doing.s --- CHANGELOG.md | 5 ++++- doc/configuration.md | 22 ++++++++----------- org-roam.el | 51 ++++++++++++++++++++++++++------------------ 3 files changed, 43 insertions(+), 35 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e4b4a4b..b9a9e5a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,10 @@ * [#87][gh-87], [#90][gh-90] Support encrypted Org files (by [@chip2n](https://github.com/chip2n/)) ### Bugfixes -* [#86][gh-86] Fix org-roam--parse-content incorrect `:to` computation for nested files +* [#86][gh-86] Fix `org-roam--parse-content` incorrect `:to` computation for nested files + +### Breaking Changes +* [#103][gh-103] Change `org-roam-file-format` to a function: `org-roam-file-name-function` to allow more flexible file name customizaton. Also changes `org-roam-use-timestamp-as-filename` to `org-roam-filename-noconfirm` to better describe what it does. ## 0.1.1 (2020-02-15) diff --git a/doc/configuration.md b/doc/configuration.md index ebdb4fe..b9c8ae3 100644 --- a/doc/configuration.md +++ b/doc/configuration.md @@ -66,24 +66,20 @@ Org files in all of its main commands (`org-roam-insert`, `org-roam-find-file`). Hence, having any unique file name is a decent option, and the default workflow uses the timestamp as the filename. -The format of the filename is specified by the string -`org-roam-file-format`, which defaults to `"%Y%m%d%H%M%S"`. To see -valid specifications, see the help (`C-h f`) for `format-time-string`. +The format of the filename is controlled by the function +`org-roam-file-name-function`, which defaults to a format like +`YYYYMMDDHHMMSS_title_here.org`. You may choose to define your own +function to change this. -There are several reasons for keeping filenames meaningful. For -example, one may wish to publish the Org files, and some publishing -methods such as Org-publish use the file names as slugs for the URLs. - -If you wish to maintain manual control of filenames, set -`org-roam-use-timestamp-as-filename` to `nil`: +If you wish to be prompted to change the file name on creation, set +`org-roam-filename-noconfirm` to `nil`: ```emacs-lisp -(setq org-roam-use-timestamp-as-filename nil) +(setq org-roam-filename-noconfirm nil) ``` -When this setting is turned off, the user is instead manually prompted -for a filename. It is then the user's responsibility to ensure that -the file names are unique. +It is then the user's responsibility to ensure that the file names are +unique. ### Autopopulating Titles diff --git a/org-roam.el b/org-roam.el index 0856823..f739da3 100644 --- a/org-roam.el +++ b/org-roam.el @@ -68,18 +68,23 @@ Valid values are (const right)) :group 'org-roam) -(defcustom org-roam-file-format "%Y%m%d%H%M%S" - "The timestamp format to use filenames." - :type 'string - :group 'org-roam) +(defcustom org-roam-file-name-function #'org-roam--file-name-timestamp-title + "The function used to generate filenames. + +The function takes as parameter `TITLE', a string the user inputs." + :group 'org-roam + :type '(choice (const :tag "Default" org-roam--file-name-timestamp-title) + (function :tag "Personalized function"))) (defcustom org-roam-link-title-format "%s" "The format string used when inserting org-roam links that use their title." :type 'string :group 'org-roam) -(defcustom org-roam-use-timestamp-as-filename t - "Whether to use timestamp as a file name. If not true, prompt for a file name each time." +(defcustom org-roam-filename-noconfirm t + "Whether to prompt for confirmation of fil name for new files. + +If nil, always ask for filename." :type 'boolean :group 'org-roam) @@ -202,6 +207,13 @@ If `ABSOLUTE', return an absolute file-path. Else, return a relative file-path." (s (s-join "_" s))) s)) +(defun org-roam--file-name-timestamp-title (title) + "Return a file name (without extension) for new files. + +It uses TITLE and the current timestamp to form a unique title." + (let ((timestamp (format-time-string "%Y%m%d%H%M%S" (current-time))) + (slug (org-roam--title-to-slug title))) + (format "%s_%s" timestamp slug))) ;;; Creating org-roam files (defun org-roam--populate-title (file &optional title) @@ -238,25 +250,22 @@ If not provided, derive the title from the file name." (org-roam--make-file file-path)) (find-file file-path))) -(defun org-roam--get-new-id (&optional title) - "Return a new ID, generated from the current time. - -Optionally pass it the title, for a smart file name." - (if org-roam-use-timestamp-as-filename - (format-time-string org-roam-file-format (current-time)) - (let* ((slug (read-string "Enter ID (without extension): " - (if title - (org-roam--title-to-slug title) - ""))) - (file-path (org-roam--make-new-file-path slug t))) - (if (file-exists-p file-path) - (user-error "There's already a file at %s") - slug)))) +(defun org-roam--get-new-id (title) + "Return a new ID, given the note TITLE." + (let* ((proposed-slug (funcall org-roam-file-name-function title)) + (new-slug (if org-roam-filename-noconfirm + proposed-slug + (read-string "Enter ID (without extension): " + proposed-slug))) + (file-path (org-roam--make-new-file-path new-slug t))) + (if (file-exists-p file-path) + (user-error "There's already a file at %s") + new-slug))) (defun org-roam-new-file () "Quickly create a new file, using the current timestamp." (interactive) - (org-roam--new-file-named (org-roam--get-new-id))) + (org-roam--new-file-named (format-time-string "%Y%m%d%H%M%S" (current-time)))) ;;; Inserting org-roam links (defun org-roam-insert ()