[Top][All Lists]

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

Re: [AUCTeX-devel] save-excursion and Emacs 25

From: Nicolas Richard
Subject: Re: [AUCTeX-devel] save-excursion and Emacs 25
Date: Mon, 25 Jan 2016 10:57:40 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/25.0.50 (gnu/linux)

Hi Mosè,

> Did you notice some glitches when using AUCTeX due to this change?

There is (at least) one occurrence (which I mentionned on emacs-devel :

It happens in LaTeX-fill-environment :

(defun LaTeX-fill-environment (justify)
  "Fill and indent current environment as LaTeX text."
  (interactive "*P")
    (LaTeX-mark-environment) ;; <= this activates the mark
    (re-search-forward "{\\([^}]+\\)}")
    (LaTeX-fill-region (region-beginning) (region-end) justify
                       (concat " environment " (TeX-match-buffer 1))))

The mark ends up being active when it was not.

I believe this is wrong use of the mark, and we should instead have a
`LaTeX-bounds-of-environement function, which could be used both by
LaTeX-mark-environment and LaTeX-fill-environment.

Here's what I have in mind (the same should be done for other similar
commands such as LaTeX-fill-section vs LaTeX-mark-section) :

--8<---------------cut here---------------start------------->8---
diff --git a/latex.el b/latex.el
index 22aab5e..d2aac48 100644
--- a/latex.el
+++ b/latex.el
@@ -4238,6 +4238,22 @@ (defun LaTeX-find-matching-begin ()
     (or (= level 0)
        (error "Can't locate beginning of current environment"))))
+(defun LaTeX-bounds-of-environment (&optional count)
+  "Return the bounds of current environment.
+If COUNT is given, work on the respective number of enclosing
+environments.  The command will not work properly if there are
+unbalanced begin-end pairs in comments and verbatim environments.
+Return value is a cons (BEG END)."
+  (setq count (if count (abs count) 1))
+  (let (beg end)
+    (save-excursion
+      (dotimes (c count) (LaTeX-find-matching-end))
+      (setq end (point)))
+    (save-excursion
+      (dotimes (c count) (LaTeX-find-matching-begin))
+      (setq beg (point)))
+    (cons beg end)))
 (defun LaTeX-mark-environment (&optional count)
   "Set mark to end of current environment and point to the matching begin.
 If prefix argument COUNT is given, mark the respective number of
@@ -4245,28 +4261,22 @@ (defun LaTeX-mark-environment (&optional count)
 there are unbalanced begin-end pairs in comments and verbatim
   (interactive "p")
-  (setq count (if count (abs count) 1))
-  (let ((cur (point)) beg end)
-    ;; Only change point and mark after beginning and end were found.
-    ;; Point should not end up in the middle of nowhere if the search fails.
-    (save-excursion
-      (dotimes (c count) (LaTeX-find-matching-end))
-      (setq end (line-beginning-position 2))
-      (goto-char cur)
-      (dotimes (c count) (LaTeX-find-matching-begin))
-      (setq beg (point)))
-    (push-mark end)
-    (goto-char beg)
+  (let ((bounds (LaTeX-bounds-of-environment count)))
+    (push-mark (save-excursion
+                 (goto-char (cdr bounds))
+                 (line-beginning-position 2)))
+    (goto-char (car bounds))
 (defun LaTeX-fill-environment (justify)
   "Fill and indent current environment as LaTeX text."
   (interactive "*P")
-    (LaTeX-mark-environment)
-    (re-search-forward "{\\([^}]+\\)}")
-    (LaTeX-fill-region (region-beginning) (region-end) justify
-                      (concat " environment " (TeX-match-buffer 1)))))
+    (let ((bounds (LaTeX-bounds-of-environment )))
+      (goto-char (car bounds))
+      (re-search-forward "{\\([^}]+\\)}")
+      (LaTeX-fill-region (point) (cdr bounds) justify
+                         (concat " environment " (TeX-match-buffer 1))))))
 (defun LaTeX-fill-section (justify)
   "Fill and indent current logical section as LaTeX text."
--8<---------------cut here---------------end--------------->8---

If we're going through such a rewrite, I'd like to mention that I have a
local patch where I remove the use of (line-beginning-position 2) in
LaTeX-mark-environment. I don't really know why it's there and it
usually gets in my way (some people end multiple environments on the
same line, and I have to work with their code). I suggest using
(skip-chars-forward "\n") instead (but I didn't include that in the above
patch because it's a different kind of change)


reply via email to

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