1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
;; Quickly navigate between your denote journal files without manually having to
;; select the file or buffer. If you have a lot of journal files like me, you
;; will want to take care of the empty ones. See the second attached file to
;; deal with this.
;;; utils
(defun files (dir &optional regex)
(seq-remove
#'file-directory-p
(directory-files dir t (or regex directory-files-no-dot-files-regexp) t)))
(defun clamp (beg end val)
(max beg (min end val)))
;;; Cycle Entries
(defun zyd/cycle-entry (direction)
"Visit the next or previous journal entry. DIRECTION is a function,
either `1+' or `1-'"
(let ((visiting-entry (buffer-file-name (current-buffer)))
(journal-entries (sort (files (denote-journal-directory)))))
(find-file
(elt journal-entries
(clamp 0
(1- (length journal-entries))
(funcall direction (seq-position journal-entries visiting-entry)))))))
(defun zyd/next-entry ()
(interactive)
(zyd/cycle-entry #'1+))
(defun zyd/previous-entry ()
(interactive)
(zyd/cycle-entry #'1-))
(define-key org-mode-map (kbd "C-<") #'zyd/previous-entry)
(define-key org-mode-map (kbd "C->") #'zyd/next-entry)
;; Test it out from a denote journal file.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
;; If you find the cycle functionality above useful you will probably want to
;; remove any empty journals you have lingering around. By empty we mean all
;; journals that don't have anything written beyond the front matter. Lets trash
;; those.
;;; utils
;; Extracted from `denote--file-with-temp-buffer'
(defmacro with-temp-file-buffer (file &rest body)
"If FILE exists, insert its contents in a temp buffer and call BODY."
(declare (indent 1))
`(cl-flet ((get-buffer-or-file (file)
(let* ((buffer (get-file-buffer file))
(file-exists (file-exists-p file))
(buffer-modified (buffer-modified-p buffer)))
(cond
((or (and file-exists
buffer
(not buffer-modified)
(not (eq buffer-modified 'autosaved)))
(and file-exists (not buffer)))
(cons #'insert-file-contents file))
(buffer
(cons #'insert-buffer buffer))))))
(when-let* ((file-and-function (get-buffer-or-file ,file)))
(with-temp-buffer
(funcall (car file-and-function) (cdr file-and-function))
(goto-char (point-min))
,@body))))
(defun files (dir &optional regex)
(seq-remove
#'file-directory-p
(directory-files dir t (or regex directory-files-no-dot-files-regexp) t)))
;;; Empty Journals
(defun zyd/denote-retrieve-file-contents (file)
"Retrieve the contents of the file after the front matter."
(or (with-temp-file-buffer file
(re-search-forward "^\s*$" nil t 1)
(forward-line 1)
(buffer-substring-no-properties (point) (point-max)))
(error "Something went wrong trying to access this file: %s" file)))
(defun zyd/denote-empty-p (file)
"Return t if note is empty beyond its front-matter."
(when (denote-file-has-denoted-filename-p file)
(string-empty-p (string-clean-whitespace (zyd/denote-retrieve-file-contents file)))))
(defun zyd/empty-journals (dir)
(seq-filter #'zyd/denote-empty-p (files dir)))
(defun zyd/denote-trash-empty-journals (&optional dry-run)
(interactive "P")
(cl-flet ((msg (journals)
(let ((inhibit-message t))
(message (concat "\n" (make-string 30 ? ) " Journals Trashed"))
(message (concat (make-string 80 ?=) "\n"))
(mapcar #'message journals)
(message (concat (make-string 80 ?=) "\n")))))
(let ((empty-journals (zyd/empty-journals denote-journal-directory)))
(cond
(dry-run
(msg empty-journals)
(message "[Dry run] Journals trash: %d" (length empty-journals)))
(t
(dolist (journal empty-journals (msg empty-journals))
(move-file-to-trash journal))
(message "Journals trashed: %d" (length empty-journals)))))))
;; Run with C-u to do a dry run. Check the *Messages* buffer for a listing of
;; empty files.
(define-key org-mode-map (kbd "C-c n e") #'zyd/denote-trash-empty-journals)