(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
This commit is contained in:
Jethro Kuan
2020-02-17 13:50:40 +08:00
committed by GitHub
parent 58590a0e9d
commit ce305af319
3 changed files with 43 additions and 35 deletions

View File

@ -6,7 +6,10 @@
* [#87][gh-87], [#90][gh-90] Support encrypted Org files (by [@chip2n](https://github.com/chip2n/)) * [#87][gh-87], [#90][gh-90] Support encrypted Org files (by [@chip2n](https://github.com/chip2n/))
### Bugfixes ### 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) ## 0.1.1 (2020-02-15)

View File

@ -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 `org-roam-find-file`). Hence, having any unique file name is a decent
option, and the default workflow uses the timestamp as the filename. option, and the default workflow uses the timestamp as the filename.
The format of the filename is specified by the string The format of the filename is controlled by the function
`org-roam-file-format`, which defaults to `"%Y%m%d%H%M%S"`. To see `org-roam-file-name-function`, which defaults to a format like
valid specifications, see the help (`C-h f`) for `format-time-string`. `YYYYMMDDHHMMSS_title_here.org`. You may choose to define your own
function to change this.
There are several reasons for keeping filenames meaningful. For If you wish to be prompted to change the file name on creation, set
example, one may wish to publish the Org files, and some publishing `org-roam-filename-noconfirm` to `nil`:
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`:
```emacs-lisp ```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 It is then the user's responsibility to ensure that the file names are
for a filename. It is then the user's responsibility to ensure that unique.
the file names are unique.
### Autopopulating Titles ### Autopopulating Titles

View File

@ -68,18 +68,23 @@ Valid values are
(const right)) (const right))
:group 'org-roam) :group 'org-roam)
(defcustom org-roam-file-format "%Y%m%d%H%M%S" (defcustom org-roam-file-name-function #'org-roam--file-name-timestamp-title
"The timestamp format to use filenames." "The function used to generate filenames.
:type 'string
:group 'org-roam) 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" (defcustom org-roam-link-title-format "%s"
"The format string used when inserting org-roam links that use their title." "The format string used when inserting org-roam links that use their title."
:type 'string :type 'string
:group 'org-roam) :group 'org-roam)
(defcustom org-roam-use-timestamp-as-filename t (defcustom org-roam-filename-noconfirm t
"Whether to use timestamp as a file name. If not true, prompt for a file name each time." "Whether to prompt for confirmation of fil name for new files.
If nil, always ask for filename."
:type 'boolean :type 'boolean
:group 'org-roam) :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 (s-join "_" s)))
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 ;;; Creating org-roam files
(defun org-roam--populate-title (file &optional title) (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)) (org-roam--make-file file-path))
(find-file file-path))) (find-file file-path)))
(defun org-roam--get-new-id (&optional title) (defun org-roam--get-new-id (title)
"Return a new ID, generated from the current time. "Return a new ID, given the note TITLE."
(let* ((proposed-slug (funcall org-roam-file-name-function title))
Optionally pass it the title, for a smart file name." (new-slug (if org-roam-filename-noconfirm
(if org-roam-use-timestamp-as-filename proposed-slug
(format-time-string org-roam-file-format (current-time)) (read-string "Enter ID (without extension): "
(let* ((slug (read-string "Enter ID (without extension): " proposed-slug)))
(if title (file-path (org-roam--make-new-file-path new-slug t)))
(org-roam--title-to-slug title) (if (file-exists-p file-path)
""))) (user-error "There's already a file at %s")
(file-path (org-roam--make-new-file-path slug t))) new-slug)))
(if (file-exists-p file-path)
(user-error "There's already a file at %s")
slug))))
(defun org-roam-new-file () (defun org-roam-new-file ()
"Quickly create a new file, using the current timestamp." "Quickly create a new file, using the current timestamp."
(interactive) (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 ;;; Inserting org-roam links
(defun org-roam-insert () (defun org-roam-insert ()