emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: "Sébastien Miquel" <sebastien.miquel@posteo.eu>
To: "Ihor Radchenko" <yantar92@gmail.com>,
	"Dávid Jakab" <djakab314@gmail.com>
Cc: emacs-orgmode@gnu.org
Subject: [PATCH] Re: Bug: org-edit-special indents inline latex [9.5 (nil @ /home/david/.emacs.d/.local/straight/build-27.2/org-mode/)]
Date: Tue, 31 Aug 2021 15:13:02 +0000	[thread overview]
Message-ID: <3af691d1-cabd-27ce-94cd-89feb5a59665@posteo.eu> (raw)
In-Reply-To: <87bl5hu31c.fsf@localhost>

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


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 
    fragment inside an org list such as

Thanks for reporting and confirming.


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

  reply	other threads:[~2021-08-31 15:14 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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   ` Sébastien Miquel [this message]
2021-09-27 11:01     ` [PATCH] " 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
2022-07-04 14:48   ` Sébastien Miquel
2022-07-07  9:05     ` Ihor Radchenko
2022-07-04 12:35 ` Sébastien Miquel

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:

  List information: https://www.orgmode.org/

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

  git send-email \
    --in-reply-to=3af691d1-cabd-27ce-94cd-89feb5a59665@posteo.eu \
    --to=sebastien.miquel@posteo.eu \
    --cc=djakab314@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    --cc=yantar92@gmail.com \


* 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 public inbox


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