emacs-orgmode
[Top][All Lists]
Advanced

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

Re: [O] [patch] better(?) indention for cdlatex-environment


From: Rasmus
Subject: Re: [O] [patch] better(?) indention for cdlatex-environment
Date: Wed, 18 Feb 2015 02:06:01 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/25.0.50 (gnu/linux)

Nicolas Goaziou <address@hidden> writes:

> Rasmus <address@hidden> writes:
>
>> Perhaps there are clever ways to figure it out.  I say there are too many
>> dynamics and "fixes" in the code to get cdlatex-environment to work
>> already.  Just consider this example where | is cursor
>>
>>    - foo | bar
>>
>> Midway through, when ENV is reinserted, but before indentation the
>> end-marker will be *after* bar which is a line after \end{ENV}...
>
> This is exactly what we want: indent (non empty) lines starting in
> [BEG ; END[. Or am I missing something?

Maybe I'm missing something.

Bar is already indented through ord-return-indent.  So it would get double
indented.  I could use "normal" return or "\n", but then I think I would
not get a good metric for indentation.


>> Anyway it reminded me that I missed "re-implementing" one feature of
>> cdlatex, namely moving the cursor to the right place. I refind this
>> place do it by inserting a funny string and replacing it. A poor man's
>> marker, I guess...
>
> Another option: when ENV is inserted the first time, store (e.g., in N)
> how many (forward-line -1) are needed to go back to BEG. At the end of
> the process, move to BEG then (forward-line n). I assume point is always
> left on an empty lines. If it is not the case, you also need to store
> current column, relatively to end of line.

Cdlatex inserts a "random" amount of newlines which I remove with trim
(depending mostly on bolp).  Then I insert 0 or 1 newlines based on
context.  It can probably be done, but I don't know if it's as robust.

> BTW, You didn't update the patch.

Ups.  The old new patch is attached.

-- 
Together we'll stand, divided we'll fall
>From 0a639d79f67a2aceadf6edbb334a4e6d9d16c88e Mon Sep 17 00:00:00 2001
From: rasmus <address@hidden>
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 | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 50 insertions(+), 6 deletions(-)

diff --git a/lisp/org.el b/lisp/org.el
index 4f047b2..8ea8b28 100755
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -18645,13 +18645,57 @@ Revert to the normal definition outside of these 
fragments."
       (call-interactively (key-binding (vector last-input-event))))))
 
 (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))))
+  "Execute `cdlatex-environment' and indent the inserted environment.
+
+ENVIRONMENT and ITEM are passed to `cdlatex-environment'.
 
+The inserted environment is indented to current indentation
+unless point is at the beginning of the line, in which the
+environment remains unintended."
+  (interactive)
+  ;; cdlatex-environment always return nil.  Therefore, capture output
+  ;; first and determine if an environment was selected.
+  (let* ((beg (point-marker))
+        (end (copy-marker (point) t))
+        (pointstr "*?*")
+        (env (progn (ignore-errors (cdlatex-environment environment item))
+                    (when (> end beg) (insert pointstr))
+                    (org-trim (delete-and-extract-region beg end)))))
+    (when (org-string-nw-p env)
+      ;; Get indentation of next line unless at column 0.
+      (let ((ind (if (bolp) 0
+                  (save-excursion
+                    (org-return-indent)
+                    (prog1 (org-get-indentation)
+                      (when (progn (skip-chars-forward " \t") (eolp))
+                        (delete-region beg (point)))))))
+           (bol (progn (skip-chars-backward " \t") (bolp))))
+       ;; Insert a newline before environment unless at column zero
+       ;; to "escape" the current line.  Insert a newline if
+       ;; something is one the same line as \end{ENVIRONMENT}.
+       (insert (concat (unless bol "\n")
+                       env
+                       (when (and (skip-chars-forward " \t") (not (eolp)))
+                         "\n")))
+       (unless (zerop ind)
+         (let* ((elm (org-element-at-point))
+                (elm-beg (org-element-property :begin elm))
+                (elm-end (copy-marker
+                          (save-excursion
+                            (goto-char (org-element-property :end elm))
+                            (skip-chars-backward " \t\n\r")
+                            (point)))))
+           (save-excursion
+             (goto-char elm-beg)
+             (while (< (point) elm-end)
+               (unless (eolp) (org-indent-to-column ind))
+               (forward-line)))
+           (set-marker elm-end nil))))
+      (goto-char beg)
+      (search-forward pointstr)
+      (replace-match ""))
+    (set-marker beg nil)
+    (set-marker end nil)))
 
 ;;;; LaTeX fragments
 
-- 
2.3.0


reply via email to

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