Nicolas Goaziou <m...@nicolasgoaziou.fr> writes:

> You can get real indentation by indenting a new line first. What about
> the following?
>
>     (org-return-indent)

Indeed that this the trick.  The attached patch seems to work nicely and
takes care of the corner cases I could think of.

I now get the following (desirable) behavior

 - key :: foo | bar
          baz
# insert latex-environment with cdlatex

 - key :: foo | bar
          \begin{ENV}
          whatever
          \end{ENV}
          baz

Here's another case
p1
 - item | item
p2
# insert latex-environment with cdlatex
p1
 - item  item
   \begin{equation}
   \label{eq:9}
   
   \end{equation}

And more...

Behavior is better now.  To improve further I'd need to change cdlatex.el
and last time I tried it was ignored...

—Rasmus

-- 
I feel emotional landscapes they puzzle me
>From a19cfa0f3eb22c4b3a3e3bd313626fb90a9affce Mon Sep 17 00:00:00 2001
From: rasmus <ras...@gmx.us>
Date: Tue, 10 Feb 2015 12:02:59 +0100
Subject: [PATCH] org.el: Change indention for cdlatex environments

* org.el (org-cdlatex-environment-indent): Use different indent
  algorithm based on content above the new latex-environment.
---
 lisp/org.el | 38 ++++++++++++++++++++++++++++++++++----
 1 file changed, 34 insertions(+), 4 deletions(-)

diff --git a/lisp/org.el b/lisp/org.el
index 64b546f..11fd3de 100755
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -18647,10 +18647,40 @@ Revert to the normal definition outside of these fragments."
 (defun org-cdlatex-environment-indent (&optional environment item)
   "Execute `cdlatex-environment' and indent the inserted environment."
   (interactive)
-  (cdlatex-environment environment item)
-  (let ((element (org-element-at-point)))
-    (org-indent-region (org-element-property :begin element)
-		       (org-element-property :end element))))
+  ;; TODO: Cleanup if quit.  Unfortunately `cdlatex-environment'
+  ;; always return nil.
+  (let* (;; Insert environment on next line unless at beginning of line.
+	 (eol
+	  (unless (<= (point)
+		      (save-excursion (beginning-of-line)
+				      (org-skip-whitespace)
+				      (point)))
+	    (end-of-line) t))
+	 ;; Get correct indention for next line.
+	  (ind (if eol (save-excursion
+			 (org-return-indent)
+			 (prog1 (org-get-indentation)
+			   (unless (or (eobp) (looking-at "[^ \t]"))
+			     (kill-whole-line))))
+		 (org-get-indentation))))
+    (cdlatex-environment environment item)
+    ;; Indent new latex-environment to correct indention.
+    (unless (zerop ind)
+      (let* ((element (org-element-at-point))
+	     (beg (org-element-property :begin element))
+	     (end (copy-marker
+		   (save-excursion
+		     (goto-char (org-element-property :end element))
+		     (skip-chars-backward " \t\n\r")
+		     (point)))))
+	(save-excursion
+	  (goto-char beg)
+	  (beginning-of-line)
+	  (while (<= (point) end)
+	    (org-indent-to-column ind)
+	    (forward-line 1)))
+	(set-marker end nil))
+      (forward-char ind))))
 
 
 ;;;; LaTeX fragments
-- 
2.3.0

Reply via email to