Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a function named zetteldeft-upsert #95

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions zetteldeft.el
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@

(require 'seq)

(require 'cl)

(declare-function avy-jump "avy")
(unless (fboundp 'avy-jump)
(display-warning 'zetteldeft
Expand Down Expand Up @@ -343,6 +345,57 @@ change to insert state."
(save-buffer)
(when (featurep 'evil) (evil-insert-state))))

(defun zetteldeft--upsert-list-choices (str)
(let* ((try-again (format "Edit '%s' and try again" str))
(create-new-note (format "Create a new note with the title '%s'" str))
(all-choices (flatten-list (list create-new-note
try-again
(deft-find-all-files-no-prefix))))
(choice (completing-read "Choose: " all-choices)))
(list try-again create-new-note choice)))

(defun zetteldeft--upsert (str find-file-func new-file-func calling-func)
(let* ((choices (zetteldeft--upsert-list-choices str))
(try-again (first choices))
(create-new-note (second choices))
(choice (third choices)))
(progn (cond ((equal choice try-again)
(let* ((new-str (read-string "Note title: " str)))
(funcall calling-func new-str)))
((equal choice create-new-note)
(funcall new-file-func str))
(t (funcall find-file-func choice))))))

;;;###autoload
(defun zetteldeft-upsert (str)
(interactive (list (read-string "Note title: ")))
(zetteldeft--upsert str #'zetteldeft-find-file #'zetteldeft-new-file #'zetteldeft-upsert))

(defun zetteldeft--upsert-insert-link (f)
(let* ((id (zetteldeft--lift-id f)))
(insert
(concat zetteldeft-link-indicator
(zetteldeft--lift-id f)
zetteldeft-link-suffix))))

;;;###autoload
(defun zetteldeft-upsert-and-link (str)
(interactive (list (read-string "Note title: ")))
(zetteldeft--upsert
str
#'zetteldeft--upsert-insert-link
#'zetteldeft-new-file-and-link
#'zetteldeft-upsert-and-link))

;;;###autoload
(defun zetteldeft-upsert-and-backlink (str)
(interactive (list (read-string "Note title: ")))
(zetteldeft--upsert
str
#'zetteldeft--upsert-insert-link
#'zetteldeft-new-file-and-backlink
#'zetteldeft-upsert-and-backlink))

;;;###autoload
(defun zetteldeft-new-file-and-link (str)
"Create a new note and insert a link to it.
Expand Down
57 changes: 57 additions & 0 deletions zetteldeft.org
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,8 @@ Some declaration.
(require 'ace-window)

(require 'seq)

(require 'cl)
#+END_SRC

Since February 2019, the =avy= API changed and =avy--generic-jump= is replaced by =avy-jump=.
Expand Down Expand Up @@ -925,6 +927,61 @@ To bypass =deft-auto-populate-title-maybe= and prevent having two lines with tit

Note that the file is only actually created when =save-buffer= is called.

**** =zetteldeft-upsert= creates a new note or opens one that already exists

#+BEGIN_SRC emacs-lisp
(defun zetteldeft--upsert-list-choices (str)
(let* ((try-again (format "Edit '%s' and try again" str))
(create-new-note (format "Create a new note with the title '%s'" str))
(all-choices (flatten-list (list create-new-note
try-again
(deft-find-all-files-no-prefix))))
(choice (completing-read "Choose: " all-choices)))
(list try-again create-new-note choice)))

(defun zetteldeft--upsert (str find-file-func new-file-func calling-func)
(let* ((choices (zetteldeft--upsert-list-choices str))
(try-again (first choices))
(create-new-note (second choices))
(choice (third choices)))
(progn (cond ((equal choice try-again)
(let* ((new-str (read-string "Note title: " str)))
(funcall calling-func new-str)))
((equal choice create-new-note)
(funcall new-file-func str))
(t (funcall find-file-func choice))))))

;;;###autoload
(defun zetteldeft-upsert (str)
(interactive (list (read-string "Note title: ")))
(zetteldeft--upsert str #'zetteldeft-find-file #'zetteldeft-new-file #'zetteldeft-upsert))

(defun zetteldeft--upsert-insert-link (f)
(let* ((id (zetteldeft--lift-id f)))
(insert
(concat zetteldeft-link-indicator
(zetteldeft--lift-id f)
zetteldeft-link-suffix))))

;;;###autoload
(defun zetteldeft-upsert-and-link (str)
(interactive (list (read-string "Note title: ")))
(zetteldeft--upsert
str
#'zetteldeft--upsert-insert-link
#'zetteldeft-new-file-and-link
#'zetteldeft-upsert-and-link))

;;;###autoload
(defun zetteldeft-upsert-and-backlink (str)
(interactive (list (read-string "Note title: ")))
(zetteldeft--upsert
str
#'zetteldeft--upsert-insert-link
#'zetteldeft-new-file-and-backlink
#'zetteldeft-upsert-and-backlink))
#+END_SRC

**** =zetteldeft-new-file-and-link= inserts generated id

Similar to the previous function, but also insert a link to the newly created note.
Expand Down