emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [PATCH] org-capture-place-item better alignment for new lists.
@ 2014-10-07 10:56 Andrew Burgess
  2014-10-07 13:49 ` Nicolas Goaziou
  0 siblings, 1 reply; 4+ messages in thread
From: Andrew Burgess @ 2014-10-07 10:56 UTC (permalink / raw)
  To: emacs-orgmode

Creating an entry in org-capture-templates of type item adds entries
into a list, however, if the list is empty then the first list item
will be indented to depth 0 (so hard on the left) like this:

* Top Level
** Second Level
- item #1
- item #2
- item #3

I prefer to indent content, including lists, to a level matching the
parent, so something like this:

* Top Level
** Second Level
   - item #1
   - item #2
   - item #3

The patch below changes org-capture-place-item so that, when starting
a new list, the items are indented as in the second example above.

The existing behaviour is maintained when adding additional items to a
list, that is the indentation of new items will match the indentation
on items already in the list.

All feedback welcome,

Thanks,
Andrew


	lisp/org-capture.el (org-capture-place-item): When starting a
	new list use (1+ org-current-level) rather than just 0 for the
	indentation. This ensures new lists are indented under their
	parent element.
---
 lisp/org-capture.el | 19 +++++++------------
 1 file changed, 7 insertions(+), 12 deletions(-)

diff --git a/lisp/org-capture.el b/lisp/org-capture.el
index 9e33d25..150ba1e 100644
--- a/lisp/org-capture.el
+++ b/lisp/org-capture.el
@@ -1064,7 +1064,7 @@ may have been stored before."
   "Place the template as a new plain list item."
   (let* ((txt (org-capture-get :template))
 	 (target-entry-p (org-capture-get :target-entry-p))
-	 (ind 0)
+	 (ind (1+ (org-current-level)))
 	 beg end)
     (if (org-capture-get :exact-position)
 	(goto-char (org-capture-get :exact-position))
@@ -1078,18 +1078,13 @@ may have been stored before."
       (if (org-capture-get :prepend)
 	  (progn
 	    (goto-char beg)
-	    (if (org-list-search-forward (org-item-beginning-re) end t)
-		(progn
-		  (goto-char (match-beginning 0))
-		  (setq ind (org-get-indentation)))
-	      (goto-char end)
-	      (setq ind 0)))
+	    (when (org-list-search-forward (org-item-beginning-re) end t)
+	      (goto-char (match-beginning 0))
+	      (setq ind (org-get-indentation))))
 	(goto-char end)
-	(if (org-list-search-backward (org-item-beginning-re) beg t)
-	    (progn
-	      (setq ind (org-get-indentation))
-	      (org-end-of-item))
-	  (setq ind 0))))
+	(when (org-list-search-backward (org-item-beginning-re) beg t)
+	  (setq ind (org-get-indentation))
+	  (org-end-of-item))))
     ;; Remove common indentation
     (setq txt (org-remove-indentation txt))
     ;; Make sure this is indeed an item
-- 
1.9.3

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] org-capture-place-item better alignment for new lists.
  2014-10-07 10:56 [PATCH] org-capture-place-item better alignment for new lists Andrew Burgess
@ 2014-10-07 13:49 ` Nicolas Goaziou
  2014-10-07 15:59   ` Andrew Burgess
  0 siblings, 1 reply; 4+ messages in thread
From: Nicolas Goaziou @ 2014-10-07 13:49 UTC (permalink / raw)
  To: Andrew Burgess; +Cc: emacs-orgmode

Hello,

Andrew Burgess <andrew.burgess@embecosm.com> writes:

> Creating an entry in org-capture-templates of type item adds entries
> into a list, however, if the list is empty then the first list item
> will be indented to depth 0 (so hard on the left) like this:
>
> * Top Level
> ** Second Level
> - item #1
> - item #2
> - item #3
>
> I prefer to indent content, including lists, to a level matching the
> parent, so something like this:
>
> * Top Level
> ** Second Level
>    - item #1
>    - item #2
>    - item #3
>
> The patch below changes org-capture-place-item so that, when starting
> a new list, the items are indented as in the second example above.

Thanks for the patch.

However, this behaviour is wrong when `org-adapt-indentation' is nil.

It is better to use `org-indent-line' than hard-coding

  (1+ (org-current-level)).


Regards,

-- 
Nicolas Goaziou

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] org-capture-place-item better alignment for new lists.
  2014-10-07 13:49 ` Nicolas Goaziou
@ 2014-10-07 15:59   ` Andrew Burgess
  2014-10-14 20:29     ` Nicolas Goaziou
  0 siblings, 1 reply; 4+ messages in thread
From: Andrew Burgess @ 2014-10-07 15:59 UTC (permalink / raw)
  To: emacs-orgmode

* Nicolas Goaziou <mail@nicolasgoaziou.fr> [2014-10-07 15:49:09 +0200]:

>
> Thanks for the patch.
>
> However, this behaviour is wrong when `org-adapt-indentation' is nil.
>
> It is better to use `org-indent-line' than hard-coding
>
>   (1+ (org-current-level)).
>

Thanks for taking a look at the patch, and thanks for the feedback. I
did suspect I'd have missed something.

I've revised the patch (below) so that I now use org-indent-line to
establish the best indentation if we're starting a new list, this has
resulted in slightly more churn, but hopefully not too much.

Thanks,
Andrew

---

Creating an entry in org-capture-templates of type item adds entries
into a list, however, currently, if the list is empty then the first
list item will always be indented to depth 0 (so hard on the left),
which looks like this:

* Top Level
** Second Level
- item #1
- item #2
- item #3

This is fine if org-adapt-indentation is nil, however, with the
default value of t lists should be indented more like this:

* Top Level
** Second Level
   - item #1
   - item #2
   - item #3

The patch below changes org-capture-place-item so that, when starting
a new list, the items are indented as above.

Care is taken to preserve two features of the existing behaviour,
first, when adding to an existing list, new items are indented to match
the items already in the list. And secondly, when there is some
introductory text before the list new items are inserted after the
text, like this:

* Top Level
** Second Level
   This is some introductory text:
   - item #1
   - item #2
   - item #3

	lisp/org-capture.el (org-capture-place-item): When starting a
	new list use org-indent-line to establish the correct
	indentation rather than just using 0.
---
 lisp/org-capture.el | 35 ++++++++++++++++++++---------------
 1 file changed, 20 insertions(+), 15 deletions(-)

diff --git a/lisp/org-capture.el b/lisp/org-capture.el
index 9e33d25..094ff55 100644
--- a/lisp/org-capture.el
+++ b/lisp/org-capture.el
@@ -1075,21 +1075,19 @@ may have been stored before."
        (t
 	(setq beg (1+ (point-at-eol))
 	      end (save-excursion (outline-next-heading) (point)))))
+      (setq ind nil)
       (if (org-capture-get :prepend)
 	  (progn
 	    (goto-char beg)
-	    (if (org-list-search-forward (org-item-beginning-re) end t)
-		(progn
-		  (goto-char (match-beginning 0))
-		  (setq ind (org-get-indentation)))
-	      (goto-char end)
-	      (setq ind 0)))
+	    (when (org-list-search-forward (org-item-beginning-re) end t)
+	      (goto-char (match-beginning 0))
+	      (setq ind (org-get-indentation))))
 	(goto-char end)
-	(if (org-list-search-backward (org-item-beginning-re) beg t)
-	    (progn
-	      (setq ind (org-get-indentation))
-	      (org-end-of-item))
-	  (setq ind 0))))
+	(when (org-list-search-backward (org-item-beginning-re) beg t)
+	  (setq ind (org-get-indentation))
+	  (org-end-of-item)))
+      (unless ind
+	(goto-char end)))
     ;; Remove common indentation
     (setq txt (org-remove-indentation txt))
     ;; Make sure this is indeed an item
@@ -1097,17 +1095,24 @@ may have been stored before."
       (setq txt (concat "- "
 			(mapconcat 'identity (split-string txt "\n")
 				   "\n  "))))
+    ;; Prepare surrounding empty lines
+    (org-capture-empty-lines-before)
+    (setq beg (point))
+    (unless (eolp)
+      (insert "\n")
+      (previous-line))
+    (unless ind
+      (org-indent-line)
+      (setq ind (org-get-indentation))
+      (delete-region beg (point)))
     ;; Set the correct indentation, depending on context
     (setq ind (make-string ind ?\ ))
     (setq txt (concat ind
 		      (mapconcat 'identity (split-string txt "\n")
 				 (concat "\n" ind))
 		      "\n"))
-    ;; Insert, with surrounding empty lines
-    (org-capture-empty-lines-before)
-    (setq beg (point))
+    ;; Insert
     (insert txt)
-    (or (bolp) (insert "\n"))
     (org-capture-empty-lines-after 1)
     (org-capture-position-for-last-stored beg)
     (forward-char 1)
-- 
1.9.3

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] org-capture-place-item better alignment for new lists.
  2014-10-07 15:59   ` Andrew Burgess
@ 2014-10-14 20:29     ` Nicolas Goaziou
  0 siblings, 0 replies; 4+ messages in thread
From: Nicolas Goaziou @ 2014-10-14 20:29 UTC (permalink / raw)
  To: Andrew Burgess; +Cc: emacs-orgmode

Hello,

Andrew Burgess <andrew.burgess@embecosm.com> writes:

> I've revised the patch (below) so that I now use org-indent-line to
> establish the best indentation if we're starting a new list, this has
> resulted in slightly more churn, but hopefully not too much.

Thanks. I applied it.

Note that you needed to add "TINYCHANGE" at the end of your patch. Since
you are close to the loc limit, you will need to sign FSF papers for
another one.


Regards,

-- 
Nicolas Goaziou

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2014-10-14 20:28 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-10-07 10:56 [PATCH] org-capture-place-item better alignment for new lists Andrew Burgess
2014-10-07 13:49 ` Nicolas Goaziou
2014-10-07 15:59   ` Andrew Burgess
2014-10-14 20:29     ` Nicolas Goaziou

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).