emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Ihor Radchenko <yantar92@posteo.net>
To: Rens Oliemans <hallo@rensoliemans.nl>
Cc: emacs-orgmode@gnu.org
Subject: Re: [PATCH 1/2] org-capture: Allow entry template to start without heading
Date: Sun, 14 Apr 2024 13:41:12 +0000	[thread overview]
Message-ID: <87y19gqcxz.fsf@localhost> (raw)
In-Reply-To: <87jzl0rxtq.fsf@rensoliemans.nl>

[-- Attachment #1: Type: text/plain, Size: 1042 bytes --]

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


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: v2-0001-org-capture-Allow-entry-template-to-start-without.patch --]
[-- Type: text/x-patch, Size: 2830 bytes --]

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


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #3: v2-0002-org-capture-Allow-table-line-entry-to-start-witho.patch --]
[-- Type: text/x-patch, Size: 2775 bytes --]

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


[-- Attachment #4: Type: text/plain, Size: 224 bytes --]


-- 
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	other threads:[~2024-04-14 13:41 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-27 10:47 "Bad template" when creating org-capture for table-line without '|' Rens Oliemans
2024-03-27 12:37 ` Ihor Radchenko
2024-04-14 11:24   ` [PATCH 1/2] org-capture: Allow entry template to start without heading Rens Oliemans
2024-04-14 13:41     ` Ihor Radchenko [this message]
2024-04-14 18:39       ` Rens Oliemans
2024-05-14  8:48         ` Ihor Radchenko
2024-05-14  9:49           ` Rens Oliemans
2024-05-27  7:54           ` Rens Oliemans
2024-05-27  8:29             ` Ihor Radchenko
2024-05-27  8:50               ` Bastien Guerry
2024-06-04 13:21       ` Ihor Radchenko
2024-06-05 10:25         ` Rens Oliemans
2024-04-14 11:24   ` [PATCH 2/2] org-capture: Allow table-line entry to start without | Rens Oliemans

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87y19gqcxz.fsf@localhost \
    --to=yantar92@posteo.net \
    --cc=emacs-orgmode@gnu.org \
    --cc=hallo@rensoliemans.nl \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).