Journaling cleanup

This commit is contained in:
Howard Abrams 2024-08-10 21:59:03 -07:00
parent a9ce3c1d27
commit 4e11a22457
2 changed files with 83 additions and 95 deletions

View file

@ -28,37 +28,29 @@ A literate programming configuration file for extending the Journaling capabilit
Using the [[https://github.com/bastibe/org-journal][org-journal]] project to easily create /daily/ journal entries: Using the [[https://github.com/bastibe/org-journal][org-journal]] project to easily create /daily/ journal entries:
#+begin_src emacs-lisp #+begin_src emacs-lisp
(use-package org-journal (use-package org-journal
:init :after org
(setq org-journal-dir "~/journal" :config
org-journal-date-format " " (setq org-journal-dir "~/journal"
org-journal-time-format "" org-journal-date-format " "
org-journal-file-type 'daily org-journal-time-format ""
org-journal-file-format "%Y%m%d") org-journal-file-type 'daily
:config org-journal-file-format "%Y%m%d")
#+end_src
Notice that the rest of this file's contents is /contained/ in this =config= section!
And let's put a /leader key/ sequence for it (Doom-specific): (ha-leader "f j" '("journal" . org-journal-new-entry))
#+begin_src emacs-lisp
(ha-leader "f j" '("journal" . org-journal-new-entry))
#+end_src
In normal Org file, I like large headers, but in my Journal, where each task is a header, I want them smaller: ;; In normal Org file, I like large headers, but in my Journal,
;; where each task is a header, I want them smaller:
#+begin_src emacs-lisp (add-hook 'org-journal-mode-hook
(add-hook 'org-journal-mode-hook
(lambda () (lambda ()
(set-face-attribute 'org-level-1 nil :height 1.2) (set-face-attribute 'org-level-1 nil :height 1.2)
(set-face-attribute 'org-level-2 nil :height 1.1) (set-face-attribute 'org-level-2 nil :height 1.1)
(set-face-attribute 'org-level-3 nil :height 1.0))) (set-face-attribute 'org-level-3 nil :height 1.0)))
#+end_src
But new files could use /my formatting/ (which is different than the options available in the project): ;; But new files could use /my formatting/ (which is different
;; than the options available in the project):
#+begin_src emacs-lisp (ha-auto-insert-file (rx "journal/" (zero-or-more any) (= 8 digit)) "journal"))
(ha-auto-insert-file (rx "journal/" (zero-or-more any) (= 8 digit)) "journal")
#+end_src #+end_src
This depends on the following [[file:~/.doom.d/snippets/org-journal-mode/__journal][snippet/template file]]: This depends on the following [[file:~/.doom.d/snippets/org-journal-mode/__journal][snippet/template file]]:
@ -75,114 +67,110 @@ Note that when I create a new journal entry, I want a title that should insert a
Since the Journal's filename represents a date, I should be able to get the "date" associated with a file. Since the Journal's filename represents a date, I should be able to get the "date" associated with a file.
#+begin_src emacs-lisp #+begin_src emacs-lisp
(defun ha-journal-file-date (&optional datename) (defun ha-journal-file-date (&optional datename)
"Returns a Lisp date-timestamp based on the format of the current filename, "Returns a Lisp date-timestamp based on the format of the current filename,
or DATENAME if given." or DATENAME if given."
(unless datename (unless datename
(setq datename (buffer-file-name))) (setq datename (buffer-file-name)))
(let* ((datename-parser (rx (group (= 4 digit)) (let* ((datename-parser (rx (group (= 4 digit))
(group (= 2 digit)) (group (= 2 digit))
(group (= 2 digit)))) (group (= 2 digit))))
(parsed-datename (string-match datename-parser datename)) (parsed-datename (string-match datename-parser datename))
(day (string-to-number (match-string 3 datename))) (day (string-to-number (match-string 3 datename)))
(month (string-to-number (match-string 2 datename))) (month (string-to-number (match-string 2 datename)))
(year(string-to-number (match-string 1 datename)))) (year(string-to-number (match-string 1 datename))))
(encode-time 0 0 0 day month year))) (encode-time 0 0 0 day month year)))
#+end_src #+end_src
Using the "date" associated with a file, we can create our standard timestamp: Using the "date" associated with a file, we can create our standard timestamp:
#+begin_src emacs-lisp #+begin_src emacs-lisp
(defun ha-journal-file-datestamp (&optional datename) (defun ha-journal-file-datestamp (&optional datename)
"Return a string of the buffer's date (based on the file's name)." "Return a string of the buffer's date (based on the file's name)."
(format-time-string "%e %b %Y (%A)" (ha-journal-file-date datename))) (format-time-string "%e %b %Y (%A)" (ha-journal-file-date datename)))
#+end_src #+end_src
Close the =use-package= call:
#+begin_src emacs-lisp
)
#+end_src
* Journal Capture * Journal Capture
Capturing a task (that when uncompleted, would then spillover to following days) could go to the daily journal entry. This requires a special function that opens today's journal, but specifies a non-nil prefix argument in order to inhibit inserting the heading, as =org-capture= will insert the heading. Capturing a task (that when uncompleted, would then spillover to following days) could go to the daily journal entry. This requires a special function that opens today's journal, but specifies a non-nil prefix argument in order to inhibit inserting the heading, as =org-capture= will insert the heading.
#+begin_src emacs-lisp #+begin_src emacs-lisp
(defun org-journal-find-location () (defun org-journal-find-location ()
(org-journal-new-entry t) (org-journal-new-entry t)
(org-narrow-to-subtree) (org-narrow-to-subtree)
(goto-char (point-max))) (goto-char (point-max)))
(defvar org-capture-templates (list)) (defvar org-capture-templates (list))
(add-to-list 'org-capture-templates (add-to-list 'org-capture-templates
'("j" "Journal Task/Entry" plain '("j" "Journal Task/Entry" plain
(function org-journal-find-location) (function org-journal-find-location)
"* %?\n\n %i\n\n From: %a" "* %?\n\n %i\n\n From: %a"
:empty-lines 1 :jump-to-captured t :immediate-finish t)) :empty-lines 1 :jump-to-captured t :immediate-finish t))
#+end_src #+end_src
* Next and Previous File * Next and Previous File
Sometimes it is obvious what is the /next file/ based on the one I'm currently reading. For instance, in my journal entries, the filename is a number that can be incremented. Same with presentation files... Sometimes it is obvious what is the /next file/ based on the one I'm currently reading. For instance, in my journal entries, the filename is a number that can be incremented. Same with presentation files...
#+begin_src emacs-lisp #+begin_src emacs-lisp
(defun split-string-with-number (string) (defun split-string-with-number (string)
"Returns a list of three components of the string, the first is "Returns a list of three components of the string, the first is
the text prior to any numbers, the second is the embedded number, the text prior to any numbers, the second is the embedded number,
and the third is the rest of the text in the string." and the third is the rest of the text in the string."
(let* ((start (string-match "[0-9]+" string)) (let* ((start (string-match "[0-9]+" string))
(end (string-match "[^0-9]+" string start))) (end (string-match "[^0-9]+" string start)))
(if start (if start
(list (substring string 0 start) (list (substring string 0 start)
(substring string start end) (substring string start end)
(if end (substring string end) ""))))) (if end (substring string end) "")))))
#+end_src #+end_src
Which means that the following defines this function: Which means that the following defines this function:
#+begin_src emacs-lisp :tangle no #+begin_src emacs-lisp :tangle no
(ert-deftest split-string-with-number-test () (ert-deftest split-string-default-separatorsg-with-number-test ()
(should (equal (split-string-with-number "abc42xyz") '("abc" "42" "xyz"))) (should (equal (split-string-with-number "abc42xyz") '("abc" "42" "xyz")))
(should (equal (split-string-with-number "42xyz") '("" "42" "xyz"))) (should (equal (split-string-with-number "42xyz") '("" "42" "xyz")))
(should (equal (split-string-with-number "abc42") '("abc" "42" ""))) (should (equal (split-string-with-number "abc42") '("abc" "42" "")))
(should (equal (split-string-with-number "20140424") '("" "20140424" ""))) (should (equal (split-string-with-number "20140424") '("" "20140424" "")))
(should (null (split-string-with-number "abcxyz")))) (should (null (split-string-with-number "abcxyz"))))
#+end_src #+end_src
Given this splitter function, we create a function that takes some sort of operator and return a new filename based on the conversion that happens: Given this splitter function, we create a function that takes some sort of operator and return a new filename based on the conversion that happens:
#+begin_src emacs-lisp #+begin_src emacs-lisp
(defun find-file-number-change (f) (defun find-file-number-change (f)
(let* ((filename (buffer-file-name)) (let* ((filename (buffer-file-name))
(parts (split-string-with-number (parts (split-string-with-number
(file-name-base filename))) (file-name-base filename)))
(new-name (number-to-string (new-name (number-to-string
(funcall f (string-to-number (nth 1 parts)))))) (funcall f (string-to-number (nth 1 parts))))))
(concat (file-name-directory filename) (concat (file-name-directory filename)
(nth 0 parts) (nth 0 parts)
new-name new-name
(nth 2 parts)))) (nth 2 parts))))
#+end_src #+end_src
And this allows us to create two simple functions that can load the "next" and "previous" files: And this allows us to create two simple functions that can load the "next" and "previous" files:
#+begin_src emacs-lisp #+begin_src emacs-lisp
(defun find-file-increment () (defun find-file-increment ()
"Takes the current buffer, and loads the file that is 'one "Takes the current buffer, and loads the file that is 'one
more' than the file contained in the current buffer. This more' than the file contained in the current buffer. This
requires that the current file contain a number that can be requires that the current file contain a number that can be
incremented." incremented."
(interactive) (interactive)
(find-file (find-file-number-change '1+))) (find-file (find-file-number-change '1+)))
#+end_src #+end_src
#+begin_src emacs-lisp #+begin_src emacs-lisp
(defun find-file-decrement () (defun find-file-decrement ()
"Takes the current buffer, and loads the file that is 'one "Takes the current buffer, and loads the file that is 'one
less' than the file contained in the current buffer. This less' than the file contained in the current buffer. This
requires that the current file contain a number that can be requires that the current file contain a number that can be
decremented." decremented."
(interactive) (interactive)
(find-file (find-file-number-change '1-))) (find-file (find-file-number-change '1-)))
#+end_src #+end_src
* Technical Artifacts :noexport: * Technical Artifacts :noexport:
Let's =provide= a name so we can =require= this file. Let's =provide= a name so we can =require= this file.
#+begin_src emacs-lisp :exports none #+begin_src emacs-lisp :exports none

View file

@ -1,3 +1,3 @@
#+TITLE: Journal Entry- `(ha-journal-file-datestamp)` #+title: Journal Entry- `(ha-journal-file-datestamp)`
$0 $0