Skip to content

Commit 9dcc42a

Browse files
committed
Default content to "Back" field if no other child is found
1 parent ff3230f commit 9dcc42a

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

anki-editor.el

+25-1
Original file line numberDiff line numberDiff line change
@@ -647,14 +647,38 @@ Where the subtree is created depends on PREFIX."
647647
(values (and value (split-string value))))
648648
(mapcar #'org-entry-restore-space values)))
649649

650+
(defun anki-editor--build-field-from-content-at-point (name)
651+
"Build a field with NAME entry from the heading at point."
652+
(let* ((element (org-element-at-point))
653+
(format (anki-editor-entry-format))
654+
(begin (cl-loop for eoh = (org-element-property :contents-begin element)
655+
then (org-element-property :end subelem)
656+
for subelem = (progn
657+
(goto-char eoh)
658+
(org-element-context))
659+
while (memq (org-element-type subelem)
660+
'(drawer planning property-drawer))
661+
finally return (org-element-property :begin subelem)))
662+
(end (org-element-property :contents-end element))
663+
(raw (or (and begin
664+
end
665+
(buffer-substring-no-properties
666+
begin
667+
;; in case the buffer is narrowed,
668+
;; e.g. by `org-map-entries' when
669+
;; scope is `tree'
670+
(min (point-max) end)))
671+
"")))
672+
(cons name (anki-editor--export-string raw format))))
673+
650674
(defun anki-editor--build-fields ()
651675
"Build a list of fields from subheadings of current heading.
652676
653677
Return a list of cons of (FIELD-NAME . FIELD-CONTENT)."
654678
(save-excursion
655679
(cl-loop with inhibit-message = t ; suppress echo message from `org-babel-exp-src-block'
656680
initially (unless (org-goto-first-child)
657-
(cl-return))
681+
(cl-return `(,(anki-editor--build-field-from-content-at-point "Back"))))
658682
for last-pt = (point)
659683
for element = (org-element-at-point)
660684
for heading = (substring-no-properties

0 commit comments

Comments
 (0)