From 36639ac711f099b49900d886ad28d29abc1b29ed Mon Sep 17 00:00:00 2001 Message-ID: <36639ac711f099b49900d886ad28d29abc1b29ed.1713102029.git.yantar92@posteo.net> From: Rens Oliemans Date: Sun, 14 Apr 2024 13:24:49 +0200 Subject: [PATCH v2 1/2] org-capture: Allow entry template to start without heading * lisp/org-capture.el (org-capture-place-entry): Prepend heading to template if the template does not yet start with a heading. * testing/lisp/test-org-capture.el (test-org-capture/entry): Add two tests: no error is raised when org-capture is called with a template that does not start with a heading; and org-capture should error with a template with a lower heading after a higher heading. Link: https://list.orgmode.org/877chnc0lr.fsf@localhost/ --- lisp/org-capture.el | 2 ++ testing/lisp/test-org-capture.el | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/lisp/org-capture.el b/lisp/org-capture.el index da14f45c0..a95a38162 100644 --- a/lisp/org-capture.el +++ b/lisp/org-capture.el @@ -1198,6 +1198,8 @@ (defun org-capture-place-entry () (exact-position (org-capture-get :exact-position)) (insert-here? (org-capture-get :insert-here)) (level 1)) + (unless (string-match org-outline-regexp-bol template) + (setq template (concat "* " template))) (org-capture-verify-tree template) (when exact-position (goto-char exact-position)) (cond diff --git a/testing/lisp/test-org-capture.el b/testing/lisp/test-org-capture.el index 0ed44c6af..4e9139c40 100644 --- a/testing/lisp/test-org-capture.el +++ b/testing/lisp/test-org-capture.el @@ -244,6 +244,30 @@ (ert-deftest test-org-capture/entry () :immediate-finish t)))) (org-capture nil "t") (buffer-string)))) + ;; Do not raise an error on templates that do not start with a heading. + (should + (org-test-with-temp-text-in-file "" + (let* ((file (buffer-file-name)) + (org-capture-templates + `(("t" "Test" entry (file ,file) "Foo" + :immediate-finish t)))) + (org-capture nil "t")))) + (should + (org-test-with-temp-text-in-file "" + (let* ((file (buffer-file-name)) + (org-capture-templates + `(("t" "Test" entry (file ,file) "*bold*" + :immediate-finish t)))) + (org-capture nil "t")))) + ;; Raise an error on templates with a lower level heading after a + ;; higher level one. + (should-error + (org-test-with-temp-text-in-file "" + (let* ((file (buffer-file-name)) + (org-capture-templates + `(("t" "Test" entry (file ,file) "** X\n* Y" + :immediate-finish t)))) + (org-capture nil "t")))) ;; With a 0 prefix argument, ignore surrounding lists. (should (equal "Foo\n* X\nBar\n" -- 2.44.0