bastibe / org-journal

A simple org-mode based journaling mode
BSD 3-Clause "New" or "Revised" License
1.24k stars 123 forks source link

Replace setq with `(setf (alist-get` for capture templates in the README.org. #343

Open ghost opened 3 years ago

ghost commented 3 years ago

Describe the bug The default example suggests "seqting" org-capture-templates, which would overwrite it. I suggest replacing it with setf, which would add the template if it is not there, replace the entry if it as in:

That is, I have the following configuration snippet in my init.el.

(cl-labels
 ((org-journal-find-location ()
                             ;; Open today's journal, but specify a non-nil prefix argument in order to
                             ;; inhibit inserting the heading; org-capture will insert the heading.
                             (org-journal-new-entry t)
                             (unless (eq org-journal-file-type 'daily)
                               (org-narrow-to-subtree))
                             (goto-char (point-max))))
 (use-package org-journal
              :ensure t
              :demand t
              :after org
              :init
              ;; Change default prefix key; needs to be set before loading org-journal
              (setq org-journal-prefix-key "C-c j ")
              :config
              (setq org-journal-dir "~/Personal_Planner/Journal/"
                 org-journal-file-type 'monthly
                 org-journal-date-format "%F %A, %d %B %Y"
                 org-journal-time-format "%T %Z(%:::z)"
                 org-journal-enable-encryption nil)
              (setf (alist-get "j" org-capture-templates nil nil #'string-equal)
                 `("Journal entry"
                       plain
                       (function ,#'org-journal-find-location)
                       "** %(format-time-string org-journal-time-format)%^{Title}\n%i%?"
                       :jump-to-captured t :immediate-finish t)))
)

cl-labels hides the function from general visibility, and setf is a "generalised set", which would be clever enough to replace an association if it exists, and add one if it does not exist.

bastibe commented 3 years ago

Sounds good to me. Would you like to contribute the change as a pull request?

ghost commented 3 years ago

Pull request available: https://github.com/bastibe/org-journal/pull/344 I think it can be merged in.