emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH 1/2] org-capture: Allow entry template to start without headi


From: Ihor Radchenko
Subject: Re: [PATCH 1/2] org-capture: Allow entry template to start without heading
Date: Sun, 14 Apr 2024 13:41:12 +0000

Rens Oliemans <hallo@rensoliemans.nl> writes:

> * 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/
> ---
> First iteration of these patches, please let me know if anything can be 
> improved, either
> about the code or patches themselves (I am not used to sending patches via 
> email).

Thanks!
I have improved your patches a little, fixing the regular expression
used to match headings ("^*" is not accurate, you need
org-outline-regexp-bol), and adding another test case.
See the attached.

Before I install the patches, may I know if you have FSF copyright
assignment? See https://orgmode.org/worg/org-contribute.html#copyright

>From 36639ac711f099b49900d886ad28d29abc1b29ed Mon Sep 17 00:00:00 2001
Message-ID: 
<36639ac711f099b49900d886ad28d29abc1b29ed.1713102029.git.yantar92@posteo.net>
From: Rens Oliemans <hallo@rensoliemans.nl>
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

>From af0b56f3338b8700bd6096e4963c95436b1a14b0 Mon Sep 17 00:00:00 2001
Message-ID: 
<af0b56f3338b8700bd6096e4963c95436b1a14b0.1713102029.git.yantar92@posteo.net>
In-Reply-To: 
<36639ac711f099b49900d886ad28d29abc1b29ed.1713102029.git.yantar92@posteo.net>
References: 
<36639ac711f099b49900d886ad28d29abc1b29ed.1713102029.git.yantar92@posteo.net>
From: Rens Oliemans <hallo@rensoliemans.nl>
Date: Sun, 14 Apr 2024 13:24:51 +0200
Subject: [PATCH v2 2/2] org-capture: Allow table-line entry to start without |

* lisp/org-capture.el (org-capture-place-table-line): Prepend
table-line begin ('|') if the template does not start with it.

* testing/lisp/test-org-capture.el (test-org-capture/table-line):
Verify that a template gets prepended with a '|' if it does not start
with it.
---
 lisp/org-capture.el              | 15 ++++++++-------
 testing/lisp/test-org-capture.el | 10 ++++++++++
 2 files changed, 18 insertions(+), 7 deletions(-)

diff --git a/lisp/org-capture.el b/lisp/org-capture.el
index a95a38162..205d09da8 100644
--- a/lisp/org-capture.el
+++ b/lisp/org-capture.el
@@ -1356,13 +1356,14 @@ (defun org-capture-place-item ()
 (defun org-capture-place-table-line ()
   "Place the template as a table line."
   (require 'org-table)
-  (let ((text
-        (pcase (org-trim (org-capture-get :template))
-          ((pred (string-match-p org-table-border-regexp))
-           "| %?Bad template |")
-          (text (concat text "\n"))))
-       (table-line-pos (org-capture-get :table-line-pos))
-       beg end)
+  (let* ((template (org-trim (org-capture-get :template)))
+         (text
+         (pcase template
+           ((pred (string-match-p org-table-border-regexp))
+            (concat "| " template))
+           (text (concat text "\n"))))
+        (table-line-pos (org-capture-get :table-line-pos))
+        beg end)
     (cond
      ((org-capture-get :exact-position)
       (org-with-point-at (org-capture-get :exact-position)
diff --git a/testing/lisp/test-org-capture.el b/testing/lisp/test-org-capture.el
index 4e9139c40..f97d08bce 100644
--- a/testing/lisp/test-org-capture.el
+++ b/testing/lisp/test-org-capture.el
@@ -613,6 +613,16 @@ (ert-deftest test-org-capture/table-line ()
                       "| 2 |" :immediate-finish t))))
              (org-capture nil "t"))
            (buffer-string))))
+  ;; Prepend | when the template does not start with it
+  (should
+   (equal "| 1 |\n| 2 |\n"
+          (org-test-with-temp-text-in-file "| 1 |\n"
+            (let* ((file (buffer-file-name))
+                   (org-capture-templates
+                    `(("t" "Table" table-line (file ,file)
+                       "2 |" :immediate-finish t))))
+              (org-capture nil "t")
+              (buffer-string)))))
   ;; When `:prepend' is nil, add the row at the end of the table.
   (should
    (equal "| a |\n| x |\n"
-- 
2.44.0

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>

reply via email to

[Prev in Thread] Current Thread [Next in Thread]