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

2015-02-20 Thread Nicolas Goaziou
Rasmus  writes:

> What is the rational behind "Org 8.3 has Emacs 23 support; Org 8.4 does
> not".  I'm just trying to understand. . .

Easy: 8.3 ends with a 3, like Emacs 23...

Org 8.3 is close from being released. It started with Emacs 23 support,
a long time ago, so it would make little sense to drop this right now.
OTOH Emacs lisp evolves, and newer Org should logically follow its lead.


Regards,



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

2015-02-20 Thread Rasmus
Nicolas Goaziou  writes:

> Org 8.4 will drop support for Emacs 23, i.e., as soon as Org 8.3 is
> released, we can activate lexical binding on master.

What is the rational behind "Org 8.3 has Emacs 23 support; Org 8.4 does
not".  I'm just trying to understand. . .

—Rasmus

-- 
Vote for proprietary math!



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

2015-02-20 Thread Rasmus
Rasmus  writes:

>>> (define-obsolete-function-alias
>>> 'show-all 'outline-show-all "25.1")
>>
>> Ah right. That's the same idea. Functions without a namespace are being
>> renamed.
>
> My Debian Squeeze VM has 23.4.1.
 ^^
 Wheezy that is

Actually, Debian 7.8 has a Emacs 24.4 backport.  But I don't know how safe
backports are...?

—Rasmus

-- 
And when I’m finished thinking, I have to die a lot




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

2015-02-20 Thread Nicolas Goaziou
Rasmus  writes:

> So when can we target Emacs 24?  Jessie?  My Debian Squeeze VM has 23.4.1.
> CentOS 7 is already on Emacs 24.3.

Org 8.4 will drop support for Emacs 23, i.e., as soon as Org 8.3 is
released, we can activate lexical binding on master.


Regards,



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

2015-02-20 Thread Rasmus
Nicolas Goaziou  writes:

> Rasmus  writes:
>
>> The think that comes to mind is `show-all'.  In my Emacs:
>>
>> (define-obsolete-function-alias
>> 'show-all 'outline-show-all "25.1")
>
> Ah right. That's the same idea. Functions without a namespace are being
> renamed.

Good riddance.

So when can we target Emacs 24?  Jessie?  My Debian Squeeze VM has 23.4.1.
CentOS 7 is already on Emacs 24.3.

—Rasmus

-- 
However beautiful the theory, you should occasionally look at the evidence



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

2015-02-20 Thread Nicolas Goaziou
Rasmus  writes:

> The think that comes to mind is `show-all'.  In my Emacs:
>
> (define-obsolete-function-alias
> 'show-all 'outline-show-all "25.1")

Ah right. That's the same idea. Functions without a namespace are being
renamed.

Regards,



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

2015-02-20 Thread Rasmus
Nicolas Goaziou  writes:

> What do you mean by "outline prefixes"?

The think that comes to mind is `show-all'.  In my Emacs:

(define-obsolete-function-alias
'show-all 'outline-show-all "25.1")

—Rasmus

-- 
There are known knowns; there are things we know that we know



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

2015-02-20 Thread Nicolas Goaziou
Rasmus  writes:

> PS: Is that also why the outline prefixes are being added?  It complains
> about it when you compile org.

What do you mean by "outline prefixes"?


Regards,



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

2015-02-19 Thread Rasmus
Nicolas Goaziou  writes:

> Rasmus  writes:
>
>> E.g. org-latex--find-verb-separator or org-get-outline-path also uses
>> cl-loop (or at least loop which is an alias for cl-loop).
>
> `loop' is fine. `cl-loop' is not, at least in Org 8.3. The same goes for
> `incf' instead of `cl-incf'.

Okay.  I removed the prefixes.

—Rasmus

PS: Is that also why the outline prefixes are being added?  It complains
about it when you compile org.

-- 
May the Force be with you




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

2015-02-19 Thread Nicolas Goaziou
Rasmus  writes:

> E.g. org-latex--find-verb-separator or org-get-outline-path also uses
> cl-loop (or at least loop which is an alias for cl-loop).

`loop' is fine. `cl-loop' is not, at least in Org 8.3. The same goes for
`incf' instead of `cl-incf'.


Regards,



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

2015-02-19 Thread Rasmus
Nicolas Goaziou  writes:

> Rasmus  writes:
>
>> Oh, I pushed this by accident (damn).  Should I revert it?
>>
>> http://orgmode.org/cgit.cgi/org-mode.git/commit/?id=14a9510ce1b5535906ceb6a93238e132964fd45f
>
> No, but you cannot let cl-loop and cl-incf in the code, since we still
> support Emacs 23.

Color me confused.

E.g. org-latex--find-verb-separator or org-get-outline-path also uses
cl-loop (or at least loop which is an alias for cl-loop).

Is incf a recent function or why can you not use it?  Anyway, I don't
particularly mind (setq x (1+ x))...

—Rasmus

-- 
Together we'll stand, divided we'll fall



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

2015-02-19 Thread Nicolas Goaziou
Rasmus  writes:

> Oh, I pushed this by accident (damn).  Should I revert it?
>
> http://orgmode.org/cgit.cgi/org-mode.git/commit/?id=14a9510ce1b5535906ceb6a93238e132964fd45f

No, but you cannot let cl-loop and cl-incf in the code, since we still
support Emacs 23.

Regards,



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

2015-02-19 Thread Rasmus
Rasmus  writes:

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

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

Oh, I pushed this by accident (damn).  Should I revert it?

http://orgmode.org/cgit.cgi/org-mode.git/commit/?id=14a9510ce1b5535906ceb6a93238e132964fd45f

—Rasmus

-- 
With monopolies the cake is a lie!



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

2015-02-18 Thread Rasmus
Hi,

Nicolas Goaziou  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
>From a0dca50b09baede2e7c63ec6b9bedb976b3a3b96 Mon Sep 17 00:00:00 2001
From: rasmus 
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)))
 
 
  LaTeX fragments
-- 
2.3.0



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

2015-02-17 Thread Rasmus
Nicolas Goaziou  writes:

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



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

2015-02-17 Thread Nicolas Goaziou
Rasmus  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?

>> Also you shouldn't apply `org-indent-to-column' when line is empty.
>
> I don't see why not, but OK...

Because it introduces trailing white spaces, which can irritate some
users.

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

BTW, You didn't update the patch.

Regards,



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

2015-02-17 Thread Rasmus
Nicolas Goaziou  writes:

> I don't think you need `org-element-at-point' at all. You already have
> BEG and END markers available. I didn't test it, but this should be
> enough to indent the environment.

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}...  I think
playing the "deterministic" card is non-trivial to get right in all cases
and org-cdlatex-environment-indent is already longer than
cdlatex-environment.

> Also you shouldn't apply `org-indent-to-column' when line is empty.

I don't see why not, but OK...  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...

—Rasmus

-- 
I hear there's rumors on the, uh, Internets. . .
>From 7381526412dc6e36e2c5c1b4e92cb102dda8c965 Mon Sep 17 00:00:00 2001
From: rasmus 
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 | 52 +++-
 1 file changed, 47 insertions(+), 5 deletions(-)

diff --git a/lisp/org.el b/lisp/org.el
index 4f047b2..6de53f1 100755
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -18645,12 +18645,54 @@ 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))
+	 (env (org-trim
+	   (or (progn (ignore-errors (cdlatex-environment environment item))
+			  (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 (and (skip-chars-forward " \t") (eolp))
+			 (delete-region beg (point)))
+	(bol (and (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
+			(and (skip-chars-forward " \t") (not (eolp)) "\n")))
+	(unless (zerop ind)
+	  (let* ((element (org-element-at-point))
+		 (elm-beg (org-element-property :begin element))
+		 (elm-end (copy-marker
+			   (save-excursion
+			 (goto-char (org-element-property :end element))
+			 (skip-chars-backward " \t\n\r")
+			 (point)
+	(save-excursion
+	  (goto-char elm-beg)
+	  (beginning-of-line)
+	  (while (<= (point) elm-end)
+		(org-indent-to-column ind)
+		(forward-line)))
+	(set-marker elm-end nil)
+(set-marker beg nil)
+(set-marker end nil)))
 
 
  LaTeX fragments
-- 
2.3.0



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

2015-02-17 Thread Nicolas Goaziou
Rasmus  writes:

> +  ;; 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))
> +  (env (org-trim
> +(or (progn (ignore-errors (cdlatex-environment environment item))
> +   (delete-and-extract-region beg end))
> +""

The `or' is not necessary: `delete-and-extract-region' already returns
the empty string when markers don't move.

  (env (progn (ignore-errors (cdlatex-environment environment item))
  (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 (and (skip-chars-forward " \t") (eolp))
> +  (delete-region beg (point)))

Nitpick 

  (when (progn (skip-chars-forward " \") (eolp))
...)

since you're not really interested in the return value of
`skip-chars-forward' (which is always non-nil).

> + (bol (and (skip-chars-backward " \t") (bolp

Ditto.

> + ;; 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
> + (and (skip-chars-forward " \t") (not (eolp)) "\n")))

Ditto.

> + (unless (zerop ind)
> +   (let* ((element (org-element-at-point))
> +  (elm-beg (org-element-property :begin element))
> +  (elm-end (copy-marker
> +(save-excursion
> +  (goto-char (org-element-property :end element))
> +  (skip-chars-backward " \t\n\r")
> +  (point)
> + (save-excursion
> +   (goto-char elm-beg)
> +   (beginning-of-line)
> +   (while (<= (point) elm-end)
> + (org-indent-to-column ind)
> + (forward-line)))
> + (set-marker elm-end nil)

I don't think you need `org-element-at-point' at all. You already have
BEG and END markers available. I didn't test it, but this should be
enough to indent the environment.

Also you shouldn't apply `org-indent-to-column' when line is empty.


Regards,



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

2015-02-16 Thread Rasmus
Hi,

> Another idea: [...]

Good trick.  I used it in attached, which I think works well.

—Rasmus

-- 
With monopolies the cake is a lie!
>From 4ab1df88e5bf87d01594e280af7887cc6cd0d3ca Mon Sep 17 00:00:00 2001
From: rasmus 
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 | 52 +++-
 1 file changed, 47 insertions(+), 5 deletions(-)

diff --git a/lisp/org.el b/lisp/org.el
index 4f047b2..6de53f1 100755
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -18645,12 +18645,54 @@ 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))
+	 (env (org-trim
+	   (or (progn (ignore-errors (cdlatex-environment environment item))
+			  (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 (and (skip-chars-forward " \t") (eolp))
+			 (delete-region beg (point)))
+	(bol (and (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
+			(and (skip-chars-forward " \t") (not (eolp)) "\n")))
+	(unless (zerop ind)
+	  (let* ((element (org-element-at-point))
+		 (elm-beg (org-element-property :begin element))
+		 (elm-end (copy-marker
+			   (save-excursion
+			 (goto-char (org-element-property :end element))
+			 (skip-chars-backward " \t\n\r")
+			 (point)
+	(save-excursion
+	  (goto-char elm-beg)
+	  (beginning-of-line)
+	  (while (<= (point) elm-end)
+		(org-indent-to-column ind)
+		(forward-line)))
+	(set-marker elm-end nil)
+(set-marker beg nil)
+(set-marker end nil)))
 
 
  LaTeX fragments
-- 
2.3.0



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

2015-02-15 Thread Nicolas Goaziou
Rasmus  writes:

> It's a very good idea!  On the top of my head there's two issues.
>
>  1. cdlatex-environment doesn't work with buffers, only files...¹ I think
> it doesn't even work with (with-temp-file · ⋯) without saving the file
> first.  Try:
>
>  (require 'cdlatex)
>  (require 'reftex)
>  (with-temp-buffer (cdlatex-environment "equation"))
>
> 2. if run from a temporary buffer, the refcounter would always be one.
> Perhaps there would be a way to update it afterwards.

I see. Another idea:

  (let ((beg (point-marker))
(end (copy-marker (point) t)))
(cdlatex-environment "equation")
(prog1 (delete-and-extract-region beg end)
  (set-marker beg nil)
  (set-marker end nil)))

If it is not the empty string, indent it and insert it again...


Regards,



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

2015-02-14 Thread Rasmus
Nicolas Goaziou  writes:

> Another idea: insert the environment in a temp buffer. Check for buffer
> emptiness. If there is something, insert it with appropriate
> indentation.

It's a very good idea!  On the top of my head there's two issues.

 1. cdlatex-environment doesn't work with buffers, only files...¹ I think
it doesn't even work with (with-temp-file · ⋯) without saving the file
first.  Try:

 (require 'cdlatex)
 (require 'reftex)
 (with-temp-buffer (cdlatex-environment "equation"))

2. if run from a temporary buffer, the refcounter would always be one.
Perhaps there would be a way to update it afterwards.

—Rasmus

Footnotes: 
¹   https://github.com/cdominik/cdlatex/issues/3

-- 
Sådan en god dansk lagereddike kan man slet ikke bruge mere



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

2015-02-14 Thread Nicolas Goaziou
Rasmus  writes:

> This patch applies indentation unless at BOL in which case it stays at
> BOL.  The rest is basically just to work with cdlatex and not insert too
> many blank lines.  It's still quicky, but these quirks seem to be cdlatex
> quirks.
>
> I wonder, are there any commands to merge two elements buffer-undo-list
> into one?  'Cause ATM it takes two undo-presses to undo an environment
> insert via this command.

Another idea: insert the environment in a temp buffer. Check for buffer
emptiness. If there is something, insert it with appropriate
indentation.

> +  (let ((non-blank-eolp
> +  (save-excursion
> +(and (not (save-excursion
> +(skip-chars-backward " \t")
> +(bolp)))
> + (progn (skip-chars-forward " \t") (eolp)
> + (ind (if (bolp) 0
> +(save-excursion
> +  (unless (and (bolp)
> +   (save-excursion
> + (skip-chars-forward " \t")
> + (eolp)))

(bolp) is always nil, and so it (and (bolp) ...) so you can skip the
unless test and write (org-return-indent)

> +(org-return-indent))
> +  (org-get-indentation)
> +;; Skip forward to next bol to avoid extra newline from
> +;; cdlatex-environment.
> +(when non-blank-eolp (forward-line 1) (beginning-of-line))

(forward-line 1), which is (forward-line) always put point at the
beginning of line, excepted at eob. As a consequence,
(beginning-of-line) should be removed here.

Regards,



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

2015-02-13 Thread Nicolas Goaziou
Rasmus  writes:

> I insert a newline before I even call cdlatex.

If point moved _after calling cdlatex_ you know it worked. Otherwise,
you just remove the newline.

> I fail to see the relevant of your example.  I want to know why:
>
>-i1
>  X -i2
>
> => C-j 
>
>-i1
>
>   -i2
>
> I.e. i2 becomes a child of i1.

This was a bug, which is now fixed. Thanks.


Regards,



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

2015-02-13 Thread Rasmus
Rasmus  writes:

> Hi,
>
> Nicolas Goaziou  writes:
>
>>> I don't feel strongly about it.  Anyway, I like this better.  Cdlatex is,
>>> um, "opinionated" about is insertion of newlines.
>>
>> I still think it is better to split line. Your behaviour just requires
>> a C-e before calling the function.
>
> I agree that the "simpler" approach is better.  That's what I mean by the
> "this" above.

This patch applies indentation unless at BOL in which case it stays at
BOL.  The rest is basically just to work with cdlatex and not insert too
many blank lines.  It's still quicky, but these quirks seem to be cdlatex
quirks.

I wonder, are there any commands to merge two elements buffer-undo-list
into one?  'Cause ATM it takes two undo-presses to undo an environment
insert via this command.

—Rasmus

-- 
El Rey ha muerto. ¡Larga vida al Rey!
>From 85e6e454412c20b13d10e3d2b15dd3f9cb7e3a6b Mon Sep 17 00:00:00 2001
From: rasmus 
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 | 45 -
 1 file changed, 40 insertions(+), 5 deletions(-)

diff --git a/lisp/org.el b/lisp/org.el
index 59b245a..eaa9084 100755
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -18645,12 +18645,47 @@ 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.
+
+The inserted environment is indented to current indentation
+unless point is at the beginning of the line in which case no
+indentation occurs."
   (interactive)
-  (cdlatex-environment environment item)
-  (let ((element (org-element-at-point)))
-(org-indent-region (org-element-property :begin element)
-		   (org-element-property :end element
+  (let ((non-blank-eolp
+	 (save-excursion
+	   (and (not (save-excursion
+		   (skip-chars-backward " \t")
+		   (bolp)))
+		(progn (skip-chars-forward " \t") (eolp)
+	(ind (if (bolp) 0
+	   (save-excursion
+		 (unless (and (bolp)
+			  (save-excursion
+(skip-chars-forward " \t")
+(eolp)))
+		   (org-return-indent))
+		 (org-get-indentation)
+;; Skip forward to next bol to avoid extra newline from
+;; cdlatex-environment.
+(when non-blank-eolp (forward-line 1) (beginning-of-line))
+(cdlatex-environment environment item)
+;; Indent new latex-environment.
+(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



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

2015-02-13 Thread Rasmus
Hi,

Nicolas Goaziou  writes:

>> I don't feel strongly about it.  Anyway, I like this better.  Cdlatex is,
>> um, "opinionated" about is insertion of newlines.
>
> I still think it is better to split line. Your behaviour just requires
> a C-e before calling the function.

I agree that the "simpler" approach is better.  That's what I mean by the
"this" above.

>> cdlatex-environment always return nil.  I would have to analyze if
>> something got inserted "manually".  IOW, I don't have the name of the
>> environment, and cdlatex-environment returns nil if I press C-g and if I
>> select and environment.  I don't know how to distinguish the cases.
>
> If point moved, some text was inserted.

I insert a newline before I even call cdlatex.

>> The attached patch works "as expected" at all locations marked with "|",
>> but not the one marked with "/" and "\", which lead to the next question.
>> | - i1 | i2 |
>> / - i3 |
>> \
> `org-get-indentation' returns the column of the first non-blank
> character on the line. This has nothing to do with the point.

I think the environment should not indent if point is a bol, but I can
check for that.

> Well, if you have (X being the point)
>
>   - i1
>   - i2
>   X
>
> indenting like should give
>
>   - i1
>   - i2
> X

I fail to see the relevant of your example.  I want to know why:

   -i1
 X -i2

=> C-j 

   -i1

  -i2

I.e. i2 becomes a child of i1.

—Rasmus

-- 
May the Force be with you




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

2015-02-13 Thread Nicolas Goaziou
Rasmus  writes:

> Nicolas Goaziou  writes:
>
>> I don't see how it is desirable. The logical behaviour is to split the
>> line, unless, of course, docstring clearly specifies this.
>
> I don't feel strongly about it.  Anyway, I like this better.  Cdlatex is,
> um, "opinionated" about is insertion of newlines.

I still think it is better to split line. Your behaviour just requires
a C-e before calling the function.

This is also simpler to implement, which is non negligible.

> cdlatex-environment always return nil.  I would have to analyze if
> something got inserted "manually".  IOW, I don't have the name of the
> environment, and cdlatex-environment returns nil if I press C-g and if I
> select and environment.  I don't know how to distinguish the cases.

If point moved, some text was inserted.

> The attached patch works "as expected" at all locations marked with "|",
> but not the one marked with "/" and "\", which lead to the next question.
> | - i1 | i2 |
> / - i3 |
> \
>
> I expect indentation at all points not at bol.
>
> At "\" (org-get-indentation) returns 2 even though I'm at bol.  Why?

`org-get-indentation' returns the column of the first non-blank
character on the line. This has nothing to do with the point.

> Regarding "/".  In the following i2 is indented meaning that
> (org-get-indentation) becomes 2.  Is that a feature?
>
> (with-temp-buffer
>   (org-mode)
>   (insert "\n- i1\n- i2")
>   (beginning-of-line)
>   (org-return-indent)
>   (buffer-string))

Well, if you have (X being the point)

  - i1
  - i2
  X

indenting like should give

  - i1
  - i2
X

so that is indeed correct.


Regards,

-- 
Nicolas Goaziou



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

2015-02-11 Thread Rasmus
Hi,

Thanks for the comments!

Nicolas Goaziou  writes:

> I don't see how it is desirable. The logical behaviour is to split the
> line, unless, of course, docstring clearly specifies this.

I don't feel strongly about it.  Anyway, I like this better.  Cdlatex is,
um, "opinionated" about is insertion of newlines.

>> +  ;; TODO: Cleanup if quit.  Unfortunately `cdlatex-environment'
>> +  ;; always return nil.
>
> What do you want to clean up? In what situations? Can't `unwind-protect'
> help you?

cdlatex-environment always return nil.  I would have to analyze if
something got inserted "manually".  IOW, I don't have the name of the
environment, and cdlatex-environment returns nil if I press C-g and if I
select and environment.  I don't know how to distinguish the cases.

> Anyway, why bother?

Newlines is very hard to get right with cdlatex.  Unintended newlines is a
bug.

The attached patch works "as expected" at all locations marked with "|",
but not the one marked with "/" and "\", which lead to the next question.
| - i1 | i2 |
/ - i3 |
\

I expect indentation at all points not at bol.

At "\" (org-get-indentation) returns 2 even though I'm at bol.  Why?

Regarding "/".  In the following i2 is indented meaning that
(org-get-indentation) becomes 2.  Is that a feature?

(with-temp-buffer
  (org-mode)
  (insert "\n- i1\n- i2")
  (beginning-of-line)
  (org-return-indent)
  (buffer-string))

—Rasmus

-- 
to err is human. To screw up 10⁶ times per second, you need a computer
>From c43d7bb49a047b91a88327ce016b17383697376d Mon Sep 17 00:00:00 2001
From: rasmus 
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..682d27a 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
+  (let ((non-blank-eolp
+	 (save-excursion
+	   (and (not (save-excursion
+		   (skip-chars-backward " \t")
+		   (bolp)))
+		(progn (skip-chars-forward " \t") (eolp)
+	(ind (save-excursion
+	   (unless (and (bolp)
+			(save-excursion
+			  (skip-chars-forward " \t")
+			  (eolp)))
+		 (org-return-indent))
+	   (org-get-indentation
+;; Skip forward to next bol to avoid extra newline from
+;; cdlatex-environment.
+(when non-blank-eolp (forward-line 1) (beginning-of-line))
+(cdlatex-environment environment item)
+;; Indent new latex-environment.
+(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



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

2015-02-11 Thread Nicolas Goaziou
Rasmus  writes:

> 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

I don't see how it is desirable. The logical behaviour is to split the
line, unless, of course, docstring clearly specifies this.

AFAICT `cdlatex-environment' splits the line. Is there a particular
reason for `org-cdlatex-environment-indent' to proceed differently?

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

Ditto.

> +  ;; TODO: Cleanup if quit.  Unfortunately `cdlatex-environment'
> +  ;; always return nil.

What do you want to clean up? In what situations? Can't `unwind-protect'
help you?

> +  (let* (;; Insert environment on next line unless at beginning of line.

Comments starting with ";;" need to be on a line on their own.

> +  (eol
> +   (unless (<= (point)
> +   (save-excursion (beginning-of-line)
> +   (org-skip-whitespace)
> +   (point)))

Nitpick:

  (unless (save-excursion (skip-chars-backward " \t") (bolp))
...)

> + (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]"))

Nitpick: (org-looking-at-p "\\S-")

> +  (kill-whole-line

There's no need to pollute the kill ring.

  (delete-region (line-beginning-position) (line-end-position))

Anyway, why bother?


Regards,



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

2015-02-11 Thread Rasmus
Nicolas Goaziou  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 
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



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

2015-02-10 Thread Nicolas Goaziou
Hello,

Rasmus  writes:

> Cdlatex environment inserted via org-cdlatex-environment-indent are pretty
> bad at getting the right indention.  Consider:
>
> - concept :: a long description of concept |
>
> Where | is cursor.  When I call org-cdlatex-environment-indent, I expect 
>
> - concept :: a long description of concept
>   \begin{equation}
>   |
>   \end{equation}
>
> But I get 
>
> - concept :: a long description of concept
> \begin{equation}
> |
> \end{equation}
>
> This is because it determines the indention after the element is inserted
> at column zero.  So the correct indention /is/ column zero but I wanted it
> to be part of the description.  IOW I want Org to use the correct
> indention of when the time when I call the command.
>
> Note that I can still get an environment at column zero by issuing the
> command here:
>
> - concept :: a long description of concept
> |
>
> This patch just fixes this for org-cdlatex-indent-environment only, but
> maybe it's more correct to fix it in org-indent-region?

`org-indent-region' is right here, so I don't see what should be fixed
there.

You can get real indentation by indenting a new line first. What about
the following?

  (defun org-cdlatex-environment-indent (&optional environment item)
(org-return-indent)
(beginning-of-line)
(let ((ind (org-get-indentation)))
  (cdlatex-environment environment item)
  (save-excursion (forward-line -1) (org-indent-to-column ind))
  (save-excursion (forward-line 1) (org-indent-to-column ind))
  (org-indent-to-column ind)))


Regards,

-- 
Nicolas Goaziou



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

2015-02-10 Thread Rasmus
Rasmus  writes:

> Hi,
>
> Cdlatex environment inserted via org-cdlatex-environment-indent are pretty
> bad at getting the right indention.  Consider:
>
> - concept :: a long description of concept |
>
> Where | is cursor.  When I call org-cdlatex-environment-indent, I expect 
>
> - concept :: a long description of concept
>   \begin{equation}
>   |
>   \end{equation}

Actually, this doesn't work with a one-line description since
(org-get-indent) is then zero there.  I think a more complicated solution
is necessary.  Maybe org-indent-region could take an optional argument of
previous indention, though that still doesn't solve the one-line
description case...

—Rasmus

-- 
One thing that is clear: it's all down hill from here