From mboxrd@z Thu Jan 1 00:00:00 1970 From: Andrew Burgess Subject: Re: [PATCH] org-capture-place-item better alignment for new lists. Date: Tue, 7 Oct 2014 16:59:23 +0100 Message-ID: <20141007155923.GD3627@embecosm.com> References: <20141007105611.GC3627@embecosm.com> <87sij02dnu.fsf@nicolasgoaziou.fr> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:33909) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XbXAk-0004nO-G2 for emacs-orgmode@gnu.org; Tue, 07 Oct 2014 11:59:40 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1XbXAd-0005CT-CX for emacs-orgmode@gnu.org; Tue, 07 Oct 2014 11:59:34 -0400 Received: from mail-wi0-f180.google.com ([209.85.212.180]:32796) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XbXAd-0005Br-4D for emacs-orgmode@gnu.org; Tue, 07 Oct 2014 11:59:27 -0400 Received: by mail-wi0-f180.google.com with SMTP id em10so8442014wid.1 for ; Tue, 07 Oct 2014 08:59:25 -0700 (PDT) Received: from localhost (cust64-dsl91-135-5.idnet.net. [91.135.5.64]) by mx.google.com with ESMTPSA id hu3sm21247722wjb.17.2014.10.07.08.59.24 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 07 Oct 2014 08:59:25 -0700 (PDT) Content-Disposition: inline In-Reply-To: <87sij02dnu.fsf@nicolasgoaziou.fr> List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: emacs-orgmode@gnu.org * Nicolas Goaziou [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