From 20d61f4c6384ac50772ed5776847560cf4876192 Mon Sep 17 00:00:00 2001 From: louie Date: Fri, 6 Jul 2018 21:10:03 +0800 Subject: [PATCH] Expose and document some functions --- README.org | 8 ++++++ anki-editor.el | 73 +++++++++++++++++++++++++++----------------------- 2 files changed, 47 insertions(+), 34 deletions(-) diff --git a/README.org b/README.org index 11db459..086a5e9 100644 --- a/README.org +++ b/README.org @@ -4,6 +4,7 @@ anki-editor -- Emacs minor mode for making Anki cards with Org /Since I'm not a native English speaker, feel free to correct me if there were any ambiguity or grammatical mistakes ;-)/ + * Installation *Requirements* @@ -61,6 +62,13 @@ there were any ambiguity or grammatical mistakes ;-)/ | =anki-editor-export-subtree-to-html= | Export the subtree at point to HTML. | | =anki-editor-convert-region-to-html= | Convert and replace region to HTML. | +** Functions + + | Name | Description | + |------------------------------+------------------------------------------------------------| + | anki-editor-map-note-entries | Simple wrapper that calls =org-map-entries=. | + | anki-editor-note-at-point | Construct an alist representing a note from current entry. | + * Limitations ** Tags between Anki and Org diff --git a/anki-editor.el b/anki-editor.el index c3b884d..66a7b35 100644 --- a/anki-editor.el +++ b/anki-editor.el @@ -311,9 +311,12 @@ The implementation is borrowed and simplified from ox-html." ;;; Core Functions -(defun anki-editor--process-note-heading () - "Process note heading at point." - (anki-editor--push-note (anki-editor-note-at-point))) +(defun anki-editor-map-note-entries (func &optional match scope &rest skip) + "Simple wrapper that calls `org-map-entries' with `&ANKI_NOTE_TYPE<>\"\"' appended to MATCH." + ;; disable property inheritance temporarily, or all subheadings of a + ;; note heading will be counted as note headings as well + (let ((org-use-property-inheritance nil)) + (org-map-entries func (concat match "&" anki-editor-prop-note-type "<>\"\"") scope skip))) (defun anki-editor--insert-note-skeleton (prefix deck heading note-type fields) "Insert a note subtree (skeleton) with HEADING, NOTE-TYPE and FIELDS. @@ -480,22 +483,27 @@ Do nothing when JUST-ALIGN is non-nil." (field-name (substring-no-properties (org-element-property :raw-value - field-heading)))) - (push (cons field-name - (or (org-export-string-as - (buffer-substring - (org-element-property :contents-begin field-heading) - ;; in case the buffer is narrowed, - ;; e.g. by `org-map-entries' when - ;; scope is `tree' - (min (point-max) (org-element-property :contents-end field-heading))) - anki-editor--ox-anki-html-backend t '(:with-toc nil)) + field-heading))) + (contents-begin (org-element-property :contents-begin field-heading)) + (contents-end (org-element-property :contents-end field-heading))) - ;; 8.2.10 version of - ;; `org-export-filter-apply-functions' - ;; returns nil for an input of empty string, - ;; which will cause AnkiConnect to fail - "")) + (push (cons field-name + (cond + ((and contents-begin contents-end) (or (org-export-string-as + (buffer-substring + contents-begin + ;; in case the buffer is narrowed, + ;; e.g. by `org-map-entries' when + ;; scope is `tree' + (min (point-max) contents-end)) + anki-editor--ox-anki-html-backend t '(:with-toc nil)) + + ;; 8.2.10 version of + ;; `org-export-filter-apply-functions' + ;; returns nil for an input of empty string, + ;; which will cause AnkiConnect to fail + "")) + (t ""))) fields) (org-forward-heading-same-level nil t)))) (reverse fields)))) @@ -558,26 +566,23 @@ of that heading." ((equal arg '(16)) 'file) ((equal arg '(64)) 'agenda) (t nil)))) - (setq match (concat match "&" anki-editor-prop-note-type "<>\"\"")) - ;; disable property inheritance temporarily, or all subheadings of a - ;; note heading will be counted as note headings as well - (let* ((org-use-property-inheritance nil) - (total (progn + (let* ((total (progn (message "Counting notes...") - (length (org-map-entries t match scope)))) + (length (anki-editor-map-note-entries t match scope)))) (acc 0) (failed 0)) - (org-map-entries (lambda () - (message "[%d/%d] Processing notes in buffer \"%s\", wait a moment..." - (cl-incf acc) total (buffer-name)) - (anki-editor--clear-failure-reason) - (condition-case err - (anki-editor--process-note-heading) - (error (cl-incf failed) - (anki-editor--set-failure-reason (error-message-string err))))) - match - scope) + (anki-editor-map-note-entries + (lambda () + (message "[%d/%d] Processing notes in buffer \"%s\", wait a moment..." + (cl-incf acc) total (buffer-name)) + (anki-editor--clear-failure-reason) + (condition-case err + (anki-editor--push-note (anki-editor-note-at-point)) + (error (cl-incf failed) + (anki-editor--set-failure-reason (error-message-string err))))) + match + scope) (message (if (= 0 failed) (format "Successfully pushed %d notes to Anki." acc)