Improve editing of ANKI_TAGS

This commit is contained in:
louie 2018-09-24 02:41:15 +08:00
parent 44624cd391
commit bcaa89bf36

View file

@ -72,6 +72,7 @@
(defconst anki-editor-prop-note-id "ANKI_NOTE_ID") (defconst anki-editor-prop-note-id "ANKI_NOTE_ID")
(defconst anki-editor-prop-deck "ANKI_DECK") (defconst anki-editor-prop-deck "ANKI_DECK")
(defconst anki-editor-prop-tags "ANKI_TAGS") (defconst anki-editor-prop-tags "ANKI_TAGS")
(defconst anki-editor-prop-tags-plus (concat anki-editor-prop-tags "+"))
(defconst anki-editor-prop-failure-reason "ANKI_FAILURE_REASON") (defconst anki-editor-prop-failure-reason "ANKI_FAILURE_REASON")
(defconst anki-editor-buffer-html-output "*AnkiEditor HTML Output*") (defconst anki-editor-buffer-html-output "*AnkiEditor HTML Output*")
(defconst anki-editor-org-tag-regexp "^\\([[:alnum:]_@#%]+\\)+$") (defconst anki-editor-org-tag-regexp "^\\([[:alnum:]_@#%]+\\)+$")
@ -441,7 +442,7 @@ Where the subtree is created depends on PREFIX."
(pcase property (pcase property
((pred (string= anki-editor-prop-deck)) (anki-editor-deck-names)) ((pred (string= anki-editor-prop-deck)) (anki-editor-deck-names))
((pred (string= anki-editor-prop-note-type)) (anki-editor-note-types)) ((pred (string= anki-editor-prop-note-type)) (anki-editor-note-types))
((pred (string= anki-editor-prop-tags)) (anki-editor-all-tags)) ((pred (string-match-p (format "%s\\+?" anki-editor-prop-tags))) (anki-editor-all-tags))
(_ nil))) (_ nil)))
(defun anki-editor-is-valid-org-tag (tag) (defun anki-editor-is-valid-org-tag (tag)
@ -497,13 +498,19 @@ Where the subtree is created depends on PREFIX."
(fields . ,fields)))) (fields . ,fields))))
(defun anki-editor--get-tags () (defun anki-editor--get-tags ()
(let ((tags (org-entry-get-multivalued-property (let ((tags (anki-editor--entry-get-multivalued-property-with-inheritance
nil nil
anki-editor-prop-tags))) anki-editor-prop-tags)))
(if anki-editor-org-tags-as-anki-tags (if anki-editor-org-tags-as-anki-tags
(append tags (org-get-tags-at)) (append tags (org-get-tags-at))
tags))) tags)))
(defun anki-editor--entry-get-multivalued-property-with-inheritance (pom property)
"Return a list of values in a multivalued property with inheritance."
(let* ((value (org-entry-get pom property t))
(values (and value (split-string value))))
(mapcar #'org-entry-restore-space values)))
(defun anki-editor--build-fields () (defun anki-editor--build-fields ()
"Build a list of fields from subheadings of current heading, each element of which is a cons cell, the car of which is field name and the cdr of which is field content." "Build a list of fields from subheadings of current heading, each element of which is a cons cell, the car of which is field name and the cdr of which is field content."
(save-excursion (save-excursion
@ -549,6 +556,23 @@ Where the subtree is created depends on PREFIX."
(defvar-local anki-editor--anki-tags-cache nil) (defvar-local anki-editor--anki-tags-cache nil)
(defun anki-editor--concat-multivalued-property-value (prop value)
(let ((old-values (org-entry-get-multivalued-property nil prop)))
(unless (string-suffix-p prop "+")
(setq old-values (-difference old-values (org-entry-get-multivalued-property nil (concat prop "+")))))
(mapconcat #'org-entry-protect-space
(append old-values (list value))
" ")))
(setq org-properties-postprocess-alist
(append org-properties-postprocess-alist
(list (cons anki-editor-prop-tags
(lambda (value)
(anki-editor--concat-multivalued-property-value anki-editor-prop-tags value)))
(cons anki-editor-prop-tags-plus
(lambda (value)
(anki-editor--concat-multivalued-property-value anki-editor-prop-tags-plus value))))))
;;;###autoload ;;;###autoload
(define-minor-mode anki-editor-mode (define-minor-mode anki-editor-mode
"anki-eidtor-mode" "anki-eidtor-mode"