Add a command to insert notes

This commit is contained in:
louie 2018-01-06 00:09:31 +08:00
parent 9922313a9a
commit cd25a6a437

View file

@ -53,18 +53,46 @@
(defun anki-editor-insert-deck () (defun anki-editor-insert-deck ()
(interactive) (interactive)
(message "Fetching...") (message "Fetching decks...")
(anki-editor--anki-connect-invoke (anki-editor--anki-connect-invoke
"deckNames" 5 nil "deckNames" 5 nil
(lambda (result) (lambda (result)
(setq result (append (sort result #'string-lessp) nil)) (setq result (append (sort result #'string-lessp) nil))
(insert (completing-read "Choose a deck: " result)) (insert (completing-read "Choose a deck: " result))
(org-set-tags-to anki-editor-deck-tag) (anki-editor--set-tags-fix anki-editor-deck-tag))))
(org-fix-tags-on-the-fly))))
(defun anki-editor-insert-note ()
(interactive)
(message "Fetching note types...")
(anki-editor--anki-connect-invoke
"modelNames" 5 nil
(lambda (note-types)
(let (note-type note-heading)
(setq note-types (append (sort note-types #'string-lessp) nil)
note-type (completing-read "Choose a note type: " note-types))
(message "Fetching note fields...")
(anki-editor--anki-connect-invoke
"modelFieldNames" 5 `((modelName . ,note-type))
(lambda (fields)
(setq fields (append (reverse fields) nil)
note-heading (read-from-minibuffer "Enter the heading: " "Item"))
(org-insert-subheading nil)
(insert note-heading)
(anki-editor--set-tags-fix anki-editor-note-tag)
(org-set-property (substring (symbol-name anki-editor-note-type-prop) 1) note-type)
(org-next-visible-heading 1)
(end-of-line 0)
(dolist (field fields)
(save-excursion
(org-insert-subheading nil)
(insert field)))
(org-next-visible-heading 1)
(end-of-line)
(newline-and-indent)))))))
(setq anki-editor--key-map `((,(kbd "C-c a s") . ,#'anki-editor-submit) (setq anki-editor--key-map `((,(kbd "C-c a s") . ,#'anki-editor-submit)
(,(kbd "C-c a d") . ,#'anki-editor-insert-deck))) (,(kbd "C-c a d") . ,#'anki-editor-insert-deck)
(,(kbd "C-c a n") . ,#'anki-editor-insert-note)))
(defun anki-editor-setup-default-keybindings () (defun anki-editor-setup-default-keybindings ()
(interactive) (interactive)
@ -190,6 +218,10 @@
parent parent
(anki-editor--find-ancestor parent test))))) (anki-editor--find-ancestor parent test)))))
(defun anki-editor--set-tags-fix (tags)
(org-set-tags-to tags)
(org-fix-tags-on-the-fly))
;; anki-connect ;; anki-connect
(defun anki-editor--anki-connect-invoke (action version &optional params success) (defun anki-editor--anki-connect-invoke (action version &optional params success)