emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Bug: org-edit-special indents inline latex [9.5 (nil @ /home/david/.emacs.d/.local/straight/build-27.2/org-mode/)]
@ 2021-04-28 13:37 Dávid Jakab
  2021-08-28 13:11 ` Ihor Radchenko
  0 siblings, 1 reply; 8+ messages in thread
From: Dávid Jakab @ 2021-04-28 13:37 UTC (permalink / raw)
  To: emacs-orgmode

Remember to cover the basics, that is, what you expected to happen and
what in fact did happen.  You don't know how to make a good report?  See

      https://orgmode.org/manual/Feedback.html#Feedback

Your bug report will be posted to the Org mailing list.
------------------------------------------------------------------------


Emacs  : GNU Emacs 27.2 (build 1, x86_64-pc-linux-gnu, GTK+ Version 
3.24.27, cairo version 1.17.4)
  of 2021-03-26
Package: Org mode version 9.5 (nil @ 
/home/david/.emacs.d/.local/straight/build-27.2/org-mode/)


When using org-edit-special to edit inline latex, i.e., equations 
between \( and \), in an org-mode buffer, a number of
spaces may get inserted before \( after the latex editing minibuffer is 
closed. It seems like the number of extra spaces
defaults to the indentation of the line in which the inline math is 
located. This behavior makes complete sense for code
blocks and latex environments, but I can't see a use case in which it 
should be applied to inline math. It looks like
the option to edit inline math with org-edit-special was added only 
recently, so this could be a bug that noone
noticed yet.
I looked at the code that does things and it seems the variable that 
controls the indentation of the parts edited in the
minibuffer is a buffer-local variable org-src--preserve-indentation. 
Setting a default value to it has no effect because
it gets overwritten internally in the function org-src--edit-element. 
There is a variable with a similar name
org-src-preserve-indentation that is supposed to set by the user, but it 
has no effect specifically in the case of latex
fragments because org-src--edit-element contains the code:
   (let* (...
          (preserve-ind
           (and (memq type '(example-block src-block))
            (or (org-element-property :preserve-indent datum)
                org-src-preserve-indentation))) ...)
  ...

(setq org-src--preserve-indentation preserve-ind)

So the user is only allowed to turn off org-edit-special messing with 
the indentation if he is an an example-block or
src-block. I hacked in a fix modifying org-src--edit-element by 
replacing (memq type '(example-block src-block)) with
(memq type '(example-block src-block latex-fragment)) and setting 
org-src-preserve-indentation. This seems to have
solved the issue, but I believe the function shouldn't even be trying to 
indent inline math in the first place.



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Bug: org-edit-special indents inline latex [9.5 (nil @ /home/david/.emacs.d/.local/straight/build-27.2/org-mode/)]
  2021-04-28 13:37 Bug: org-edit-special indents inline latex [9.5 (nil @ /home/david/.emacs.d/.local/straight/build-27.2/org-mode/)] Dávid Jakab
@ 2021-08-28 13:11 ` Ihor Radchenko
  2021-08-31 15:13   ` [PATCH] " Sébastien Miquel
  0 siblings, 1 reply; 8+ messages in thread
From: Ihor Radchenko @ 2021-08-28 13:11 UTC (permalink / raw)
  To: Dávid Jakab; +Cc: emacs-orgmode

Dávid Jakab <djakab314@gmail.com> writes:

> When using org-edit-special to edit inline latex, i.e., equations 
> between \( and \), in an org-mode buffer, a number of
> spaces may get inserted before \( after the latex editing minibuffer is 
> closed.

Confirmed

Recipe:

1. Insert the following into a new Org mode buffer (note the indentation):

           \(x^{2}+3=\frac{2}{1}\)

2. Move point to the equation
3. M-x org-edit-special <RET>
4. M-x org-edit-src-exit <RET>
5. Observe the equation indented twice more

Best,
Ihor


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH] Re: Bug: org-edit-special indents inline latex [9.5 (nil @ /home/david/.emacs.d/.local/straight/build-27.2/org-mode/)]
  2021-08-28 13:11 ` Ihor Radchenko
@ 2021-08-31 15:13   ` Sébastien Miquel
  2021-09-27 11:01     ` Bastien
  0 siblings, 1 reply; 8+ messages in thread
From: Sébastien Miquel @ 2021-08-31 15:13 UTC (permalink / raw)
  To: Ihor Radchenko, Dávid Jakab; +Cc: emacs-orgmode

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

Hi,

Ihor Radchenko writes:
> Dávid Jakab <djakab314@gmail.com> writes:
>> When using org-edit-special to edit inline latex, i.e., equations
>> between \( and \), in an org-mode buffer, a number of
>> spaces may get inserted before \( after the latex editing minibuffer is
>> closed.
The attached patch fixes this as well as a couple other issues with special
editing of latex fragments :
  + the coordinates computation was wrong, so point position inside fragment
    isn't preserved when calling ~org-edit-special~ from an inline fragment.
  + common leading indentation wasn't getting trimmed when editing a 
multiline
    fragment inside an org list such as
    $$abc
    def$$

Thanks for reporting and confirming.

Regards,

-- 
Sébastien Miquel


[-- Attachment #2: 0001-org-src.el-Fix-special-editing-of-LaTeX-fragments.patch --]
[-- Type: text/x-patch, Size: 4840 bytes --]

From 5c3254d42e3d359021d41dae9a0549244e6fddff Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Miquel?= <sebastien.miquel@posteo.eu>
Date: Mon, 30 Aug 2021 23:18:41 +0200
Subject: [PATCH] org-src.el: Fix special editing of LaTeX fragments

* lisp/org-macs.el (org-do-remove-indentation): Add optional argument
to skip the first line.
* lisp/org-src.el (org-src--coordinates): Fix coordinates for inline
LaTeX fragments.
(org-src--contents-for-write-back): Do not indent first line for LaTeX
fragments.
(org-src--edit-element): Compute block-indentation according to parent
for LaTeX fragments.  Skip first line when removing common indentation
for LaTeX fragments.
---
 lisp/org-macs.el |  9 ++++++---
 lisp/org-src.el  | 18 ++++++++++++++----
 2 files changed, 20 insertions(+), 7 deletions(-)

diff --git a/lisp/org-macs.el b/lisp/org-macs.el
index 77458db96..5c90c92f6 100644
--- a/lisp/org-macs.el
+++ b/lisp/org-macs.el
@@ -326,17 +326,19 @@ it for output."
 \f
 ;;; Indentation
 
-(defun org-do-remove-indentation (&optional n)
+(defun org-do-remove-indentation (&optional n skip-fl)
   "Remove the maximum common indentation from the buffer.
 When optional argument N is a positive integer, remove exactly
-that much characters from indentation, if possible.  Return nil
-if it fails."
+that much characters from indentation, if possible.  When
+optional argument SKIP-FL is non-nil, skip the first
+line.  Return nil if it fails."
   (catch :exit
     (goto-char (point-min))
     ;; Find maximum common indentation, if not specified.
     (let ((n (or n
 		 (let ((min-ind (point-max)))
 		   (save-excursion
+                     (when skip-fl (forward-line))
 		     (while (re-search-forward "^[ \t]*\\S-" nil t)
 		       (let ((ind (current-indentation)))
 			 (if (zerop ind) (throw :exit nil)
@@ -344,6 +346,7 @@ if it fails."
 		   min-ind))))
       (if (zerop n) (throw :exit nil)
 	;; Remove exactly N indentation, but give up if not possible.
+        (when skip-fl (forward-line))
 	(while (not (eobp))
 	  (let ((ind (progn (skip-chars-forward " \t") (current-column))))
 	    (cond ((eolp) (delete-region (line-beginning-position) (point)))
diff --git a/lisp/org-src.el b/lisp/org-src.el
index 4698c6dd2..2e72b1755 100644
--- a/lisp/org-src.el
+++ b/lisp/org-src.el
@@ -324,7 +324,8 @@ a cons cell (LINE . COLUMN) or symbol `end'.  See also
   (if (>= pos end) 'end
     (org-with-wide-buffer
      (goto-char (max beg pos))
-     (cons (count-lines beg (line-beginning-position))
+     (cons (count-lines (save-excursion (goto-char beg) (line-beginning-position))
+                        (line-beginning-position))
 	   ;; Column is relative to the end of line to avoid problems of
 	   ;; comma escaping or colons appended in front of the line.
 	   (- (point) (min end (line-end-position)))))))
@@ -442,6 +443,7 @@ Assume point is in the corresponding edit buffer."
 		  org-src--content-indentation
 		0))))
 	(use-tabs? (and (> org-src--tab-width 0) t))
+        (preserve-fl (eq org-src--source-type 'latex-fragment))
 	(source-tab-width org-src--tab-width)
 	(contents (org-with-wide-buffer (buffer-string)))
 	(write-back org-src--allow-write-back))
@@ -456,7 +458,8 @@ Assume point is in the corresponding edit buffer."
       ;; Add INDENTATION-OFFSET to every line in buffer,
       ;; unless indentation is meant to be preserved.
       (when (> indentation-offset 0)
-	(while (not (eobp))
+	(when preserve-fl (forward-line))
+        (while (not (eobp))
 	  (skip-chars-forward " \t")
 	  (let ((i (current-column)))
 	    (delete-region (line-beginning-position) (point))
@@ -504,7 +507,13 @@ Leave point in edit buffer."
 	     (source-tab-width (if indent-tabs-mode tab-width 0))
 	     (type (org-element-type datum))
 	     (block-ind (org-with-point-at (org-element-property :begin datum)
-			  (current-indentation)))
+                          (cond
+                           ((save-excursion (skip-chars-backward " \t") (bolp))
+			    (current-indentation))
+                           ((org-element-property :parent datum)
+                            (org--get-expected-indentation
+                             (org-element-property :parent datum) nil))
+                           (t (current-indentation)))))
 	     (content-ind org-edit-src-content-indentation)
 	     (preserve-ind
 	      (and (memq type '(example-block src-block))
@@ -529,7 +538,8 @@ Leave point in edit buffer."
 	(insert contents)
 	(remove-text-properties (point-min) (point-max)
 				'(display nil invisible nil intangible nil))
-	(unless preserve-ind (org-do-remove-indentation))
+	(let ((lf (eq type 'latex-fragment)))
+          (unless preserve-ind (org-do-remove-indentation (and lf block-ind) lf)))
 	(set-buffer-modified-p nil)
 	(setq buffer-file-name nil)
 	;; Initialize buffer.
-- 
2.33.0


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] Re: Bug: org-edit-special indents inline latex [9.5 (nil @ /home/david/.emacs.d/.local/straight/build-27.2/org-mode/)]
  2021-08-31 15:13   ` [PATCH] " Sébastien Miquel
@ 2021-09-27 11:01     ` Bastien
  2021-09-29 21:01       ` Dávid Jakab
  2021-09-30 12:07       ` Sébastien Miquel
  0 siblings, 2 replies; 8+ messages in thread
From: Bastien @ 2021-09-27 11:01 UTC (permalink / raw)
  To: Sébastien Miquel; +Cc: emacs-orgmode, Ihor Radchenko, Dávid Jakab

Hi David and Sébastien,

Sébastien Miquel <sebastien.miquel@posteo.eu> writes:

> Thanks for reporting and confirming.

David, Did you have time to look at Sébastien's patch?

Sébastien, have you been able to test this patch heavily and is it
still needed, or has it been made somehow irrelevant?  (I see it does
apply well on the bugfix branch, but not on main.)

Thanks for any feedback,

-- 
 Bastien


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] Re: Bug: org-edit-special indents inline latex [9.5 (nil @ /home/david/.emacs.d/.local/straight/build-27.2/org-mode/)]
  2021-09-27 11:01     ` Bastien
@ 2021-09-29 21:01       ` Dávid Jakab
  2021-09-30 12:07       ` Sébastien Miquel
  1 sibling, 0 replies; 8+ messages in thread
From: Dávid Jakab @ 2021-09-29 21:01 UTC (permalink / raw)
  To: Bastien, Sébastien Miquel; +Cc: emacs-orgmode, Ihor Radchenko

Hi!

I briefly tried the patch today, and it seemed to solve the issue on my 
end, but I'm afraid a deeper analysis of the changes is beyond what I 
can do. As for it's relevance, when I didn't apply Sebastien's patch and 
disabled my temporary workaround fix I described in the original post, 
the problem it was still very much there.

Best,

David

On 9/27/21 13:01, Bastien wrote:
> Hi David and Sébastien,
>
> Sébastien Miquel <sebastien.miquel@posteo.eu> writes:
>
>> Thanks for reporting and confirming.
> David, Did you have time to look at Sébastien's patch?
>
> Sébastien, have you been able to test this patch heavily and is it
> still needed, or has it been made somehow irrelevant?  (I see it does
> apply well on the bugfix branch, but not on main.)
>
> Thanks for any feedback,
>


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] Re: Bug: org-edit-special indents inline latex [9.5 (nil @ /home/david/.emacs.d/.local/straight/build-27.2/org-mode/)]
  2021-09-27 11:01     ` Bastien
  2021-09-29 21:01       ` Dávid Jakab
@ 2021-09-30 12:07       ` Sébastien Miquel
  2021-09-30 13:50         ` Ihor Radchenko
  2021-09-30 15:42         ` Bastien
  1 sibling, 2 replies; 8+ messages in thread
From: Sébastien Miquel @ 2021-09-30 12:07 UTC (permalink / raw)
  To: Bastien; +Cc: emacs-orgmode

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

Hi Bastien,

Bastien writes:
>
> Sébastien, have you been able to test this patch heavily and is it
> still needed, or has it been made somehow irrelevant?  (I see it does
> apply well on the bugfix branch, but not on main.)
>
> Thanks for any feedback,
>
I've rebased the patch on main. It is still relevant (Ihor and I have 
provided
reproducers earlier in the thread).

I had done some testing, and some more recently. The patch only affects
latex-fragments and org entities to be edited which do not need to start 
at a
beginning of line (latex-fragments, inline src blocks, footnote 
definitions).

Regards,

-- 
Sébastien Miquel

[-- Attachment #2: 0001-org-src.el-Fix-special-editing-of-LaTeX-fragments.patch --]
[-- Type: text/x-patch, Size: 4970 bytes --]

From b80124aa6edbd3b6992817dd8c37253705c82ae3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Miquel?= <sebastien.miquel@posteo.eu>
Date: Mon, 30 Aug 2021 23:18:41 +0200
Subject: [PATCH] org-src.el: Fix special editing of LaTeX fragments

* lisp/org-macs.el (org-do-remove-indentation): Add optional argument
to skip the first line.
* lisp/org-src.el (org-src--coordinates): Fix coordinates for inline
LaTeX fragments.
(org-src--contents-for-write-back): Do not indent first line for LaTeX
fragments.
(org-src--edit-element): Compute block-indentation according to parent
for LaTeX fragments.  Skip first line when removing common indentation
for LaTeX fragments.
---
 lisp/org-macs.el |  9 ++++++---
 lisp/org-src.el  | 18 ++++++++++++++----
 2 files changed, 20 insertions(+), 7 deletions(-)

diff --git a/lisp/org-macs.el b/lisp/org-macs.el
index bf1340b0a..1ef89a04d 100644
--- a/lisp/org-macs.el
+++ b/lisp/org-macs.el
@@ -326,17 +326,19 @@ it for output."
 \f
 ;;; Indentation

-(defun org-do-remove-indentation (&optional n)
+(defun org-do-remove-indentation (&optional n skip-fl)
   "Remove the maximum common indentation from the buffer.
 When optional argument N is a positive integer, remove exactly
-that much characters from indentation, if possible.  Return nil
-if it fails."
+that much characters from indentation, if possible.  When
+optional argument SKIP-FL is non-nil, skip the first
+line.  Return nil if it fails."
   (catch :exit
     (goto-char (point-min))
     ;; Find maximum common indentation, if not specified.
     (let ((n (or n
 		 (let ((min-ind (point-max)))
 		   (save-excursion
+                     (when skip-fl (forward-line))
 		     (while (re-search-forward "^[ \t]*\\S-" nil t)
 		       (let ((ind (current-indentation)))
 			 (if (zerop ind) (throw :exit nil)
@@ -344,6 +346,7 @@ if it fails."
 		   min-ind))))
       (if (zerop n) (throw :exit nil)
 	;; Remove exactly N indentation, but give up if not possible.
+        (when skip-fl (forward-line))
 	(while (not (eobp))
 	  (let ((ind (progn (skip-chars-forward " \t") (current-column))))
 	    (cond ((eolp) (delete-region (line-beginning-position) (point)))
diff --git a/lisp/org-src.el b/lisp/org-src.el
index 3b25fad60..d78f46186 100644
--- a/lisp/org-src.el
+++ b/lisp/org-src.el
@@ -327,7 +327,8 @@ a cons cell (LINE . COLUMN) or symbol `end'.  See also
   (if (>= pos end) 'end
     (org-with-wide-buffer
      (goto-char (max beg pos))
-     (cons (count-lines beg (line-beginning-position))
+     (cons (count-lines (save-excursion (goto-char beg) (line-beginning-position))
+                        (line-beginning-position))
 	   ;; Column is relative to the end of line to avoid problems of
 	   ;; comma escaping or colons appended in front of the line.
 	   (- (point) (min end (line-end-position)))))))
@@ -445,6 +446,7 @@ Assume point is in the corresponding edit buffer."
 		  org-src--content-indentation
 		0))))
 	(use-tabs? (and (> org-src--tab-width 0) t))
+        (preserve-fl (eq org-src--source-type 'latex-fragment))
 	(source-tab-width org-src--tab-width)
 	(contents (org-with-wide-buffer
                    (let ((eol (line-end-position)))
@@ -466,7 +468,8 @@ Assume point is in the corresponding edit buffer."
       ;; Add INDENTATION-OFFSET to every line in buffer,
       ;; unless indentation is meant to be preserved.
       (when (> indentation-offset 0)
-	(while (not (eobp))
+	(when preserve-fl (forward-line))
+        (while (not (eobp))
 	  (skip-chars-forward " \t")
           (when (or (not (eolp))                               ; not a blank line
                     (and (eq (point) (marker-position marker)) ; current line
@@ -518,7 +521,13 @@ Leave point in edit buffer."
 	     (source-tab-width (if indent-tabs-mode tab-width 0))
 	     (type (org-element-type datum))
 	     (block-ind (org-with-point-at (org-element-property :begin datum)
-			  (current-indentation)))
+                          (cond
+                           ((save-excursion (skip-chars-backward " \t") (bolp))
+			    (current-indentation))
+                           ((org-element-property :parent datum)
+                            (org--get-expected-indentation
+                             (org-element-property :parent datum) nil))
+                           (t (current-indentation)))))
 	     (content-ind org-edit-src-content-indentation)
              (blank-line (save-excursion (beginning-of-line)
                                          (looking-at-p "^[[:space:]]*$")))
@@ -548,7 +557,8 @@ Leave point in edit buffer."
 	(insert contents)
 	(remove-text-properties (point-min) (point-max)
 				'(display nil invisible nil intangible nil))
-	(unless preserve-ind (org-do-remove-indentation))
+	(let ((lf (eq type 'latex-fragment)))
+          (unless preserve-ind (org-do-remove-indentation (and lf block-ind) lf)))
 	(set-buffer-modified-p nil)
 	(setq buffer-file-name nil)
 	;; Initialize buffer.
-- 
2.33.0

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] Re: Bug: org-edit-special indents inline latex [9.5 (nil @ /home/david/.emacs.d/.local/straight/build-27.2/org-mode/)]
  2021-09-30 12:07       ` Sébastien Miquel
@ 2021-09-30 13:50         ` Ihor Radchenko
  2021-09-30 15:42         ` Bastien
  1 sibling, 0 replies; 8+ messages in thread
From: Ihor Radchenko @ 2021-09-30 13:50 UTC (permalink / raw)
  To: sebastien.miquel; +Cc: Bastien, emacs-orgmode

Sébastien Miquel <sebastien.miquel@posteo.eu> writes:

> I had done some testing, and some more recently. The patch only affects
> latex-fragments and org entities to be edited which do not need to start 
> at a
> beginning of line (latex-fragments, inline src blocks, footnote 
> definitions).

Also looks fine for me.

Best,
Ihor


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] Re: Bug: org-edit-special indents inline latex [9.5 (nil @ /home/david/.emacs.d/.local/straight/build-27.2/org-mode/)]
  2021-09-30 12:07       ` Sébastien Miquel
  2021-09-30 13:50         ` Ihor Radchenko
@ 2021-09-30 15:42         ` Bastien
  1 sibling, 0 replies; 8+ messages in thread
From: Bastien @ 2021-09-30 15:42 UTC (permalink / raw)
  To: Sébastien Miquel; +Cc: emacs-orgmode

Hi Sébastien,

Sébastien Miquel <sebastien.miquel@posteo.eu> writes:

> I've rebased the patch on main. It is still relevant (Ihor and I have
> provided
> reproducers earlier in the thread).

Applied in bugfix, thanks!

-- 
 Bastien


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2021-09-30 16:19 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-28 13:37 Bug: org-edit-special indents inline latex [9.5 (nil @ /home/david/.emacs.d/.local/straight/build-27.2/org-mode/)] Dávid Jakab
2021-08-28 13:11 ` Ihor Radchenko
2021-08-31 15:13   ` [PATCH] " Sébastien Miquel
2021-09-27 11:01     ` Bastien
2021-09-29 21:01       ` Dávid Jakab
2021-09-30 12:07       ` Sébastien Miquel
2021-09-30 13:50         ` Ihor Radchenko
2021-09-30 15:42         ` Bastien

Code repositories for project(s) associated with this inbox:

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).