From 425375e8bf44d9c7751aec93633c643d5e64b4ae Mon Sep 17 00:00:00 2001 From: Michael Wolf Date: Tue, 1 Dec 2020 18:06:48 -0600 Subject: [PATCH 1/7] Add a function named zetteldeft-upsert ref https://github.com/EFLS/zetteldeft/issues/94 --- zetteldeft.el | 16 ++++++++++++++++ zetteldeft.org | 20 ++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/zetteldeft.el b/zetteldeft.el index b218b08..f5bcd32 100644 --- a/zetteldeft.el +++ b/zetteldeft.el @@ -343,6 +343,22 @@ change to insert state." (save-buffer) (when (featurep 'evil) (evil-insert-state)))) +;;;###autoload +(defun zetteldeft-upsert (str) + (interactive (list (read-string "Note title: "))) + (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 :initial-input str :preselect 1))) + (progn (cond ((equal choice try-again) + (let* ((new-str (read-string "Note title: " str))) + (mw-zetteldeft-upsert new-str))) + ((equal choice create-new-note) + (zetteldeft-new-file str)) + (t (zetteldeft-find-file choice)))))) + ;;;###autoload (defun zetteldeft-new-file-and-link (str) "Create a new note and insert a link to it. diff --git a/zetteldeft.org b/zetteldeft.org index c3adc59..bb04081 100644 --- a/zetteldeft.org +++ b/zetteldeft.org @@ -925,6 +925,26 @@ 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 +;;;###autoload +(defun zetteldeft-upsert (str) + (interactive (list (read-string "Note title: "))) + (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 :initial-input str :preselect 1))) + (progn (cond ((equal choice try-again) + (let* ((new-str (read-string "Note title: " str))) + (mw-zetteldeft-upsert new-str))) + ((equal choice create-new-note) + (zetteldeft-new-file str)) + (t (zetteldeft-find-file choice)))))) +#+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. From 7d9be444da6b1128bdb45060c508f8b8dd4a481d Mon Sep 17 00:00:00 2001 From: Michael Wolf Date: Wed, 2 Dec 2020 19:26:24 -0600 Subject: [PATCH 2/7] Refactor function zetteldeft-upsert - Create zetteldeft--upsert that prompts the user for a choice among several - Create zetteldeft--list-upsert-choices that creates the list of possible choices. --- zetteldeft.el | 30 ++++++++++++++++++++---------- zetteldeft.org | 30 ++++++++++++++++++++---------- 2 files changed, 40 insertions(+), 20 deletions(-) diff --git a/zetteldeft.el b/zetteldeft.el index f5bcd32..87a1873 100644 --- a/zetteldeft.el +++ b/zetteldeft.el @@ -343,21 +343,31 @@ change to insert state." (save-buffer) (when (featurep 'evil) (evil-insert-state)))) -;;;###autoload -(defun zetteldeft-upsert (str) - (interactive (list (read-string "Note title: "))) +(defun zetteldeft--list-upsert-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 :initial-input str :preselect 1))) + (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 (ivy-read "Choose: " all-choices :initial-input str :preselect 1))) + (list try-again create-new-note choice))) + +(defun zetteldeft--upsert (str find-file-func new-file-func) + (let* ((choices (zetteldeft--list-upsert-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))) (mw-zetteldeft-upsert new-str))) ((equal choice create-new-note) - (zetteldeft-new-file str)) - (t (zetteldeft-find-file choice)))))) + (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)) ;;;###autoload (defun zetteldeft-new-file-and-link (str) diff --git a/zetteldeft.org b/zetteldeft.org index bb04081..c88e73d 100644 --- a/zetteldeft.org +++ b/zetteldeft.org @@ -928,21 +928,31 @@ 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 -;;;###autoload -(defun zetteldeft-upsert (str) - (interactive (list (read-string "Note title: "))) +(defun zetteldeft--list-upsert-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 :initial-input str :preselect 1))) + (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 (ivy-read "Choose: " all-choices :initial-input str :preselect 1))) + (list try-again create-new-note choice))) + +(defun zetteldeft--upsert (str find-file-func new-file-func) + (let* ((choices (zetteldeft--list-upsert-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))) (mw-zetteldeft-upsert new-str))) ((equal choice create-new-note) - (zetteldeft-new-file str)) - (t (zetteldeft-find-file choice)))))) + (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)) #+END_SRC **** =zetteldeft-new-file-and-link= inserts generated id From 6f5926ed33e9478d15f22fb840c45956c6461399 Mon Sep 17 00:00:00 2001 From: Michael Wolf Date: Wed, 2 Dec 2020 19:30:22 -0600 Subject: [PATCH 3/7] Rename function --- zetteldeft.el | 4 ++-- zetteldeft.org | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/zetteldeft.el b/zetteldeft.el index 87a1873..a425afc 100644 --- a/zetteldeft.el +++ b/zetteldeft.el @@ -343,7 +343,7 @@ change to insert state." (save-buffer) (when (featurep 'evil) (evil-insert-state)))) -(defun zetteldeft--list-upsert-choices (str) +(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 @@ -353,7 +353,7 @@ change to insert state." (list try-again create-new-note choice))) (defun zetteldeft--upsert (str find-file-func new-file-func) - (let* ((choices (zetteldeft--list-upsert-choices str)) + (let* ((choices (zetteldeft--upsert-list-choices str)) (try-again (first choices)) (create-new-note (second choices)) (choice (third choices))) diff --git a/zetteldeft.org b/zetteldeft.org index c88e73d..8e20703 100644 --- a/zetteldeft.org +++ b/zetteldeft.org @@ -928,7 +928,7 @@ 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--list-upsert-choices (str) +(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 @@ -938,7 +938,7 @@ Note that the file is only actually created when =save-buffer= is called. (list try-again create-new-note choice))) (defun zetteldeft--upsert (str find-file-func new-file-func) - (let* ((choices (zetteldeft--list-upsert-choices str)) + (let* ((choices (zetteldeft--upsert-list-choices str)) (try-again (first choices)) (create-new-note (second choices)) (choice (third choices))) From 3ad16eb2562ef33441afe1355ae21973187e26f3 Mon Sep 17 00:00:00 2001 From: Michael Wolf Date: Wed, 2 Dec 2020 21:38:38 -0600 Subject: [PATCH 4/7] Add zetteldeft-upsert-and-link and zetteldeft-upsert-and-backlink --- zetteldeft.el | 31 ++++++++++++++++++++++++++++--- zetteldeft.org | 31 ++++++++++++++++++++++++++++--- 2 files changed, 56 insertions(+), 6 deletions(-) diff --git a/zetteldeft.el b/zetteldeft.el index a425afc..acf93f2 100644 --- a/zetteldeft.el +++ b/zetteldeft.el @@ -352,14 +352,14 @@ change to insert state." (choice (ivy-read "Choose: " all-choices :initial-input str :preselect 1))) (list try-again create-new-note choice))) -(defun zetteldeft--upsert (str find-file-func new-file-func) +(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))) - (mw-zetteldeft-upsert new-str))) + (funcall calling-func new-str))) ((equal choice create-new-note) (funcall new-file-func str)) (t (funcall find-file-func choice)))))) @@ -367,7 +367,32 @@ change to insert state." ;;;###autoload (defun zetteldeft-upsert (str) (interactive (list (read-string "Note title: "))) - (zetteldeft--upsert str #'zetteldeft-find-file #'zetteldeft-new-file)) + (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) diff --git a/zetteldeft.org b/zetteldeft.org index 8e20703..ec3550e 100644 --- a/zetteldeft.org +++ b/zetteldeft.org @@ -937,14 +937,14 @@ Note that the file is only actually created when =save-buffer= is called. (choice (ivy-read "Choose: " all-choices :initial-input str :preselect 1))) (list try-again create-new-note choice))) -(defun zetteldeft--upsert (str find-file-func new-file-func) +(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))) - (mw-zetteldeft-upsert new-str))) + (funcall calling-func new-str))) ((equal choice create-new-note) (funcall new-file-func str)) (t (funcall find-file-func choice)))))) @@ -952,7 +952,32 @@ Note that the file is only actually created when =save-buffer= is called. ;;;###autoload (defun zetteldeft-upsert (str) (interactive (list (read-string "Note title: "))) - (zetteldeft--upsert str #'zetteldeft-find-file #'zetteldeft-new-file)) + (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 From 12f924d4944f3454e63738cd89125c7c91b561a9 Mon Sep 17 00:00:00 2001 From: Michael Wolf Date: Wed, 2 Dec 2020 21:44:22 -0600 Subject: [PATCH 5/7] Replace ivy-read with completing-read --- zetteldeft.el | 2 +- zetteldeft.org | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/zetteldeft.el b/zetteldeft.el index acf93f2..7bf4e1f 100644 --- a/zetteldeft.el +++ b/zetteldeft.el @@ -349,7 +349,7 @@ change to insert state." (all-choices (flatten-list (list create-new-note try-again (deft-find-all-files-no-prefix)))) - (choice (ivy-read "Choose: " all-choices :initial-input str :preselect 1))) + (choice (completing-read "Choose: " all-choices :initial-input str :preselect 1))) (list try-again create-new-note choice))) (defun zetteldeft--upsert (str find-file-func new-file-func calling-func) diff --git a/zetteldeft.org b/zetteldeft.org index ec3550e..727a597 100644 --- a/zetteldeft.org +++ b/zetteldeft.org @@ -934,7 +934,7 @@ Note that the file is only actually created when =save-buffer= is called. (all-choices (flatten-list (list create-new-note try-again (deft-find-all-files-no-prefix)))) - (choice (ivy-read "Choose: " all-choices :initial-input str :preselect 1))) + (choice (completing-read "Choose: " all-choices :initial-input str :preselect 1))) (list try-again create-new-note choice))) (defun zetteldeft--upsert (str find-file-func new-file-func calling-func) From f4be25acdbe934e8048d1b8db82d6390cf6d4fbd Mon Sep 17 00:00:00 2001 From: Michael Wolf Date: Mon, 18 Oct 2021 18:12:27 -0500 Subject: [PATCH 6/7] Require cl We use first, second, and third because they're cleaner than the alternatives in my opinion. --- zetteldeft.el | 2 ++ zetteldeft.org | 2 ++ 2 files changed, 4 insertions(+) diff --git a/zetteldeft.el b/zetteldeft.el index 7bf4e1f..3d911cb 100644 --- a/zetteldeft.el +++ b/zetteldeft.el @@ -48,6 +48,8 @@ (require 'seq) +(require 'cl) + (declare-function avy-jump "avy") (unless (fboundp 'avy-jump) (display-warning 'zetteldeft diff --git a/zetteldeft.org b/zetteldeft.org index 727a597..e25e713 100644 --- a/zetteldeft.org +++ b/zetteldeft.org @@ -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=. From 348678197b48eeff5e7a013a3863d28875abd955 Mon Sep 17 00:00:00 2001 From: Michael Wolf Date: Tue, 19 Oct 2021 18:04:24 -0500 Subject: [PATCH 7/7] Remove unrecognized args from completing-read --- zetteldeft.el | 2 +- zetteldeft.org | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/zetteldeft.el b/zetteldeft.el index 3d911cb..f64618c 100644 --- a/zetteldeft.el +++ b/zetteldeft.el @@ -351,7 +351,7 @@ change to insert state." (all-choices (flatten-list (list create-new-note try-again (deft-find-all-files-no-prefix)))) - (choice (completing-read "Choose: " all-choices :initial-input str :preselect 1))) + (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) diff --git a/zetteldeft.org b/zetteldeft.org index e25e713..b8f75d9 100644 --- a/zetteldeft.org +++ b/zetteldeft.org @@ -936,7 +936,7 @@ Note that the file is only actually created when =save-buffer= is called. (all-choices (flatten-list (list create-new-note try-again (deft-find-all-files-no-prefix)))) - (choice (completing-read "Choose: " all-choices :initial-input str :preselect 1))) + (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)