From 0928dd6465aa1e6bd689d6438ccd578d3d7c1a41 Mon Sep 17 00:00:00 2001 From: orgtre Date: Tue, 15 Nov 2022 23:59:46 +0100 Subject: [PATCH] Add `anki-editor-delete-note-at-point` It improves upon `anki-editor-delete-notes` when deleting note at point, allowing aribtrary position in the note when calling it, and also allowing deletion in Org. `anki-editor-delete-notes` is removed. --- anki-editor-ui.el | 2 +- anki-editor.el | 46 +++++++++++++++++++++++++++++++++------------- 2 files changed, 34 insertions(+), 14 deletions(-) diff --git a/anki-editor-ui.el b/anki-editor-ui.el index 3859d3f..8ca0b12 100644 --- a/anki-editor-ui.el +++ b/anki-editor-ui.el @@ -46,7 +46,7 @@ ["" ("I" " insert note" anki-editor-insert-note :level 1) ("T" " set note type" anki-editor-set-note-type :level 1) - ("d" " delete note" anki-editor-delete-notes :level 1)]] + ("d" " delete note" anki-editor-delete-note-at-point :level 1)]] [["Push" ("." " note at point " anki-editor-push-note-at-point :level 2) ("n" " new notes " anki-editor-push-new-notes :level 2) diff --git a/anki-editor.el b/anki-editor.el index b01ee07..fc4d080 100644 --- a/anki-editor.el +++ b/anki-editor.el @@ -1083,19 +1083,39 @@ matching non-empty `ANKI_FAILURE_REASON' properties." (anki-editor-push-notes scope (concat anki-editor-prop-failure-reason "<>\"\""))) -(defun anki-editor-delete-notes (noteids) - "Delete notes in NOTEIDS or the note at point." - (interactive (list (list (org-entry-get nil anki-editor-prop-note-id)))) - (when (or (not (called-interactively-p 'interactive)) - (yes-or-no-p - (format (concat "Do you really want to delete note %s? " - "This can't be undone.") - (nth 0 noteids)))) - (anki-editor-api-call-result 'deleteNotes - :notes noteids) - (org-entry-delete nil anki-editor-prop-note-id) - (when (called-interactively-p 'interactive) - (message "Deleted note %s" (nth 0 noteids))))) +(defun anki-editor-delete-note-at-point (&optional prefix) + "Delete the note at point from Anki. +With PREFIX also delete it from Org." + (interactive "P") + (save-excursion + (let (note-type note-id) + (while + (and (org-back-to-heading) + (not (setq note-type + (org-entry-get nil anki-editor-prop-note-type))) + (org-up-heading-safe))) + (when (not note-type) + (user-error "No note to delete found")) + (setq note-id (condition-case nil + (string-to-number + (org-entry-get nil anki-editor-prop-note-id)) + (error nil))) + (if (not note-id) + (if prefix + (message "Note at point is not in Anki (no note-id)") + (user-error "Note at point is not in Anki (no note-id)")) + (when (yes-or-no-p + (format (concat "Do you really want to delete note %s " + "from Anki?") + note-id)) + (anki-editor-api-call-result 'deleteNotes + :notes (list note-id)) + (org-entry-delete nil anki-editor-prop-note-id) + (message "Deleted note %s from Anki" note-id))) + (when prefix + (org-mark-subtree) + (kill-region nil nil t) + (message "Deleted note at point from Org"))))) (defun anki-editor-insert-note (&optional prefix note-type) "Insert a note interactively.