Add a function to create cloze from word under cursor (#27)

* anki-editor-cloze-region: Accept hint as an argument.

To allow the function to be called non-interactively accept 'hint' as function's
argument.

* Split anki-editor-cloze into interactive and non-interactive parts.

* Add anki-editor-cloze-dwim function.

Create a cloze from currently selected region, if no region selected, use word
at point.
This commit is contained in:
Aleksei Fedotov 2018-09-05 15:21:15 +02:00 committed by Lei Tan
parent b533c9899c
commit 44624cd391

View file

@ -658,14 +658,26 @@ same as how it is used by `M-RET'(org-insert-heading)."
note-type
fields)))
(defun anki-editor-cloze-region (&optional arg)
(defun anki-editor-cloze-region (&optional arg hint)
"Cloze region with number ARG."
(interactive "p")
(interactive "p\nsHint (optional): ")
(unless (region-active-p) (error "No active region"))
(let ((region (buffer-substring (region-beginning) (region-end)))
(hint (read-from-minibuffer "Hint (optional): ")))
(anki-editor-cloze (region-beginning) (region-end)))
(defun anki-editor-cloze-dwim (&optional arg hint)
"Cloze current active region or a word the under the cursor"
(interactive "p\nsHint (optional): ")
(cond
((region-active-p) (anki-editor-cloze (region-beginning) (region-end)))
((thing-at-point 'word) (let ((bounds (bounds-of-thing-at-point 'word)))
(anki-editor-cloze (car bounds) (cdr bounds) arg hint)))
(t (error "Nothing to create cloze from"))))
(defun anki-editor-cloze (begin end arg hint)
"Cloze region from BEGIN to END with number ARG."
(let ((region (buffer-substring begin end)))
(save-excursion
(delete-region (region-beginning) (region-end))
(delete-region begin end)
(insert (with-output-to-string
(princ (format "{{c%d::%s" (or arg 1) region))
(unless (string-blank-p hint) (princ (format "::%s" hint)))