all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Rasmus <rasmus@gmx.us>
To: emacs-orgmode@gnu.org
Subject: Re: [patch] better(?) indention for cdlatex-environment
Date: Thu, 19 Feb 2015 01:22:29 +0100	[thread overview]
Message-ID: <87r3tmsqbe.fsf@gmx.us> (raw)
In-Reply-To: <87mw4cm4sq.fsf@nicolasgoaziou.fr> (Nicolas Goaziou's message of "Wed, 18 Feb 2015 01:39:17 +0100")

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

Hi,

Nicolas Goaziou <mail@nicolasgoaziou.fr> 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?

OK, the missing key is org-indent-line-to.  It works better in this case
and I can use end.

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

OK it now does this.

—Rasmus

-- 
Dung makes an excellent fertilizer

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-org.el-Change-indention-for-cdlatex-environments.patch --]
[-- Type: text/x-diff, Size: 3119 bytes --]

From a0dca50b09baede2e7c63ec6b9bedb976b3a3b96 Mon Sep 17 00:00:00 2001
From: rasmus <rasmus@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 | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 54 insertions(+), 5 deletions(-)

diff --git a/lisp/org.el b/lisp/org.el
index 4f047b2..e15e622 100755
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -18645,12 +18645,61 @@ 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."
+  "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 environment item)
-  (let ((element (org-element-at-point)))
-    (org-indent-region (org-element-property :begin element)
-		       (org-element-property :end element))))
+  ;; 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))
+	 (inserted (progn
+		     (ignore-errors (cdlatex-environment environment item))
+		     (< beg end)))
+	 ;; Figure out how many lines to move forward after the
+	 ;; environment has been inserted.
+	 (lines (when inserted
+		  (save-excursion
+		    (- (cl-loop while (< beg (point))
+				with x = 0
+				do (forward-line -1)
+				(cl-incf x)
+				finally return x)
+		       (if (progn (goto-char beg)
+				  (and (progn (skip-chars-forward " \t") (eolp))
+				       (progn (skip-chars-backward " \t") (bolp))))
+			   1 0)))))
+	 (env (org-trim (delete-and-extract-region beg end))))
+    (when inserted
+      ;; 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)
+	  (save-excursion
+	    (goto-char beg)
+	    (while (< (point) end)
+	      (unless (eolp) (org-indent-line-to ind))
+	      (forward-line))))
+	(goto-char beg)
+	(forward-line lines)
+	(org-indent-line-to ind)))
+    (set-marker beg nil)
+    (set-marker end nil)))
 
 \f
 ;;;; LaTeX fragments
-- 
2.3.0


  parent reply	other threads:[~2015-02-19  0:22 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-02-10 11:28 [patch] better(?) indention for cdlatex-environment Rasmus
2015-02-10 12:27 ` Rasmus
2015-02-10 22:35 ` Nicolas Goaziou
2015-02-11 11:26   ` Rasmus
2015-02-11 21:39     ` Nicolas Goaziou
2015-02-11 23:40       ` Rasmus
2015-02-13 22:10         ` Nicolas Goaziou
2015-02-13 23:13           ` Rasmus
2015-02-14  0:29             ` Rasmus
2015-02-14 21:20               ` Nicolas Goaziou
2015-02-15  0:08                 ` Rasmus
2015-02-15  9:52                   ` Nicolas Goaziou
2015-02-17  0:41                     ` Rasmus
2015-02-17  8:51                       ` Nicolas Goaziou
2015-02-17 21:19                         ` Rasmus
2015-02-18  0:39                           ` Nicolas Goaziou
2015-02-18  1:06                             ` Rasmus
2015-02-19  0:22                             ` Rasmus [this message]
2015-02-19 23:11                               ` Rasmus
2015-02-19 23:23                                 ` Nicolas Goaziou
2015-02-19 23:32                                   ` Rasmus
2015-02-20  0:13                                     ` Nicolas Goaziou
2015-02-20  0:38                                       ` Rasmus
2015-02-20 10:16                                         ` Nicolas Goaziou
2015-02-20 10:35                                           ` Rasmus
2015-02-20 10:40                                             ` Nicolas Goaziou
2015-02-20 10:43                                               ` Rasmus
2015-02-20 10:49                                                 ` Nicolas Goaziou
2015-02-20 10:54                                                   ` Rasmus
2015-02-20 11:17                                                     ` Nicolas Goaziou
2015-02-20 10:50                                                 ` Rasmus
2015-02-14  1:10             ` Nicolas Goaziou

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

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

  git send-email \
    --in-reply-to=87r3tmsqbe.fsf@gmx.us \
    --to=rasmus@gmx.us \
    --cc=emacs-orgmode@gnu.org \
    /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 external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.