Skip to content

Commit b4f27b5

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

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

anki-editor.el

+31-1
Original file line numberDiff line numberDiff line change
@@ -647,14 +647,44 @@ 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+
""
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+
674+
675+
(defun leoc/build-fields ()
676+
""
677+
(interactive)
678+
(message "%S" (anki-editor--build-fields)))
679+
650680
(defun anki-editor--build-fields ()
651681
"Build a list of fields from subheadings of current heading.
652682
653683
Return a list of cons of (FIELD-NAME . FIELD-CONTENT)."
654684
(save-excursion
655685
(cl-loop with inhibit-message = t ; suppress echo message from `org-babel-exp-src-block'
656686
initially (unless (org-goto-first-child)
657-
(cl-return))
687+
(cl-return `(,(anki-editor--build-field-from-content-at-point "Back"))))
658688
for last-pt = (point)
659689
for element = (org-element-at-point)
660690
for heading = (substring-no-properties

0 commit comments

Comments
 (0)