From f24ea04020888b90ddf00bac784253ce3e37a174 Mon Sep 17 00:00:00 2001 From: louie Date: Thu, 17 May 2018 23:34:40 +0800 Subject: [PATCH] Add a helper function to migrate old notes --- fix-notes-0.3.el | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 fix-notes-0.3.el diff --git a/fix-notes-0.3.el b/fix-notes-0.3.el new file mode 100644 index 0000000..587656b --- /dev/null +++ b/fix-notes-0.3.el @@ -0,0 +1,45 @@ +(require 'seq) +(require 'anki-editor) + +(defconst anki-editor-fix-prop-tags "ANKI_TAGS") +(defconst anki-editor-fix-prop-deck "ANKI_DECK") +(defconst anki-editor-fix-tag-note "note") +(defconst anki-editor-fix-tag-deck "deck") + + +(defun anki-editor-fix-notes (&optional list-of-files) + "Fix notes in LIST-OF-FILES, or in current buffer if it's nil. +`anki-editor' has be to upgraded to `0.3' before using this +command. It should be noted that only letters, numbers, `_' and +`@' are allowed in Org tags, and existing tags of headings will +be considered as Anki tags now." + + (interactive) + (when (yes-or-no-p "It's strongly recommended that you backup or version-control your old note files before running this command. Are you sure to continue ?") + (org-map-entries + (lambda () + (message "Fixing in buffer \"%s\" at %d..." (buffer-name) (point)) + (let* ((local-tags (org-get-local-tags)) + (old-anki-tags (org-entry-get-multivalued-property nil anki-editor-fix-prop-tags)) + (new-tags (cl-set-difference (append old-anki-tags (org-get-local-tags)) + (list anki-editor-fix-tag-deck anki-editor-fix-tag-note) + :test #'string=))) + + (unless (seq-every-p #'anki-editor-is-valid-org-tag new-tags) + (error "Fixing failed in buffer \"%s\" at point %d: only letters, numbers, `_', `@', `#' and `%%' are allowed in an Org tag" + (buffer-name) + (point))) + (org-set-tags-to new-tags) + (beginning-of-line) + (org-fix-tags-on-the-fly) + (org-entry-delete nil anki-editor-fix-prop-tags) + (cond + ((member anki-editor-fix-tag-deck local-tags) + (anki-editor-fix-deck))))) + nil + list-of-files) + (message "Fixing... DONE!"))) + +(defun anki-editor-fix-deck () + (interactive) + (org-set-property anki-editor-fix-prop-deck (nth 4 (org-heading-components))))