louietan / anki-editor

Emacs minor mode for making Anki cards with Org
699 stars 87 forks source link

Match org headings escaped with a `,` (needed within org source blocks) #100

Open tommyphillips opened 2 years ago

tommyphillips commented 2 years ago

In an org file, I would like to 'hide' anki-editor code as much as possible. My strategy for doing so is to write the anki-editor within a source block as follows:

#+begin_src org
,*  Question Name
:PROPERTIES:
:ANKI_NOTE_TYPE: Cloze
:ANKI_DECK: Programming
:ANKI_NOTE_ID: 1653402332712
:END:

,** Text

{{c1::Solution::Question}}

#+end_src

The problem is that it is necessary to escape the heading *s using ,* Otherwise, the main org document will interpret headings within the block as part of the main document. But anki-editor does not recognise this ,* syntax. I have had a look at the source code, but I am no expert. It seems that probably only a small tweak is required to enable anki-editor to recognise the ,*.

If anyone could help, I would appreciate it. In case anyone suggests tangling the block to another file, and then pushing that file to anki, this is not the ideal solution, because the source block will not receive the ANKI_NOTE_ID.

tommyphillips commented 2 years ago

Workaround found: I have put this in my .emacs and it enables me to push the notes to anki while keeping the ,* sytax:

(defun tap/push-escaped-source-block-to-anki ()
  (interactive)

  (defun tap/select-source-block ()
    (search-backward "#\+begin_src")
    (next-line)
    (set-mark-command nil)
    (search-forward "#\+end_src")
    (previous-line)
    (end-of-line)
    )
  (anki-editor-mode 1)
  (tap/select-source-block)
  (let ((p1 (region-beginning))
    (p2 (region-end)))
    (save-restriction (narrow-to-region p1 p2)
              (goto-char (point-min))
              (while (re-search-forward "^," nil t) (replace-match "" nil t))
              (anki-editor-push-notes)
              (goto-char (point-min))
              (while (re-search-forward "^\*" nil t) (replace-match ",\*" nil t))
              )))