[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/auctex f2bac9d 11/43: Improve environment insertion (bu
From: |
Tassilo Horn |
Subject: |
[elpa] externals/auctex f2bac9d 11/43: Improve environment insertion (bug#35284) |
Date: |
Sat, 11 Apr 2020 15:05:19 -0400 (EDT) |
branch: externals/auctex
commit f2bac9dcde12d939a5c1267fdba02c2a869bd406
Author: Ikumi Keita <address@hidden>
Commit: Ikumi Keita <address@hidden>
Improve environment insertion (bug#35284)
* latex.el (LaTeX-insert-environment): Place the point and the mark at
appropriate place.
* tests/latex/latex-test.el (LaTeX-insert-environment-with-active-region):
New test.
---
latex.el | 88 +++++++++++++++----
tests/latex/latex-test.el | 218 +++++++++++++++++++++++++++++++++++++++++++++-
2 files changed, 286 insertions(+), 20 deletions(-)
diff --git a/latex.el b/latex.el
index aaa4a1f..f57fcfb 100644
--- a/latex.el
+++ b/latex.el
@@ -668,41 +668,91 @@ environment just inserted, the buffer position just before
(concat "^\\([ \t]*" TeX-comment-start-regexp "+\\)+[ \t]*"))
(setq prefix (match-string 0))))
;; What to do with the line containing point.
- (cond ((save-excursion (beginning-of-line)
+ ;; - Open a new empty line for later insertion of "\begin{foo}" and
+ ;; put the point there.
+ ;; - If there were at first any non-whitespace texts between the
+ ;; point and EOL, send them into their new own line with possible
+ ;; comment prefix.
+ (cond (;; When the entire line consists of whitespaces except
+ ;; possible prefix...
+ (save-excursion (beginning-of-line)
(looking-at (concat prefix "[ \t]*$")))
+ ;; ...make the line empty and put the point there.
(delete-region (match-beginning 0) (match-end 0)))
- ((TeX-looking-at-backward (concat "^" prefix "[ \t]*")
+ (;; When there are only whitespaces except possible prefix
+ ;; between the point and BOL (including the case the point
+ ;; is at BOL)...
+ (TeX-looking-at-backward (if prefix
+ (concat "^\\(" prefix "\\)?[ \t]*")
+ "^[ \t]*")
(line-beginning-position))
+ ;; ...in this case, we have non-whitespace texts between
+ ;; the point and EOL, so send the entire line into a new
+ ;; next line and put the point on the empty line just
+ ;; created.
(beginning-of-line)
(newline)
(beginning-of-line 0))
- ((bolp)
+ (;; In all other cases...
+ t
+ ;; ...insert a new empty line after deleting all
+ ;; whitespaces around the point, put the point there...
(delete-horizontal-space)
- (newline)
- (beginning-of-line 0))
- (t
- (delete-horizontal-space)
- (newline 2)
- (when prefix (insert prefix))
- (beginning-of-line 0)))
+ (if (eolp)
+ (newline)
+ ;; ...and if there were at first any non-whitespace texts
+ ;; between (the original position of) the point and EOL,
+ ;; send them into a new next line with possible comment
+ ;; prefix.
+ (newline 2)
+ (when prefix (insert prefix))
+ (beginning-of-line 0))))
;; What to do with the line containing mark.
+ ;; If there is active region...
(when active-mark
+ ;; - Open a new empty line for later insertion of "\end{foo}"
+ ;; and put the mark there.
+ ;; - If there were at first any non-whitespace texts between the
+ ;; mark and EOL, pass them over the empty line and put them on
+ ;; their own line with possible comment prefix.
(save-excursion
(goto-char (mark))
- (cond ((save-excursion (beginning-of-line)
- (or (looking-at (concat prefix "[ \t]*$"))
- (looking-at "[ \t]*$")))
+ (cond (;; When the entire line consists of whitespaces except
+ ;; possible prefix...
+ (save-excursion (beginning-of-line)
+ (looking-at
+ (if prefix
+ (concat "\\(" prefix "\\)?[ \t]*$")
+ "[ \t]*$")))
+ ;; ...make the line empty and put the mark there.
(delete-region (match-beginning 0) (match-end 0)))
- ((TeX-looking-at-backward (concat "^" prefix "[ \t]*")
+ (;; When there are only whitespaces except possible prefix
+ ;; between the mark and BOL (including the case the mark
+ ;; is at BOL)...
+ (TeX-looking-at-backward (if prefix
+ (concat "^\\(" prefix "\\)?[ \t]*")
+ "^[ \t]*")
(line-beginning-position))
+ ;; ...in this case, we have non-whitespace texts
+ ;; between the mark and EOL, so send the entire line
+ ;; into a new next line and put the mark on the empty
+ ;; line just created.
(beginning-of-line)
- (newline)
- (beginning-of-line 0))
- (t
+ (set-mark (point))
+ (newline))
+ (;; In all other cases...
+ t
+ ;; ...make a new empty line after deleting all
+ ;; whitespaces around the mark, put the mark there...
(delete-horizontal-space)
(insert-before-markers "\n")
- (newline)
- (when prefix (insert prefix))))))
+ ;; ...and if there were at first any non-whitespace
+ ;; texts between (the original position of) the mark
+ ;; and EOL, send them into a new next line with
+ ;; possible comment prefix.
+ (unless (eolp)
+ (newline)
+ (when prefix (insert prefix)))))))
;; Now insert the environment.
(when prefix (insert prefix))
(setq env-start (point))
diff --git a/tests/latex/latex-test.el b/tests/latex/latex-test.el
index b198eae..c8ecff7 100644
--- a/tests/latex/latex-test.el
+++ b/tests/latex/latex-test.el
@@ -1,6 +1,6 @@
;;; latex-test.el --- tests for LaTeX mode
-;; Copyright (C) 2014--2018 Free Software Foundation, Inc.
+;; Copyright (C) 2014--2019 Free Software Foundation, Inc.
;; This file is part of AUCTeX.
@@ -320,4 +320,220 @@ backend=biber % here is a comment
(should (not TeX-PDF-mode))
(should (not (member "psfrag" TeX-active-styles))))))
+(ert-deftest LaTeX-insert-environment-with-active-region ()
+ "Check environment is inserted correctly with active region."
+ ;; The former codes of `LaTeX-insert-environment' had problems about
+ ;; the management of the point and the mark, which sometimes
+ ;; resulted in additional empty line, spurious insertion of comment
+ ;; prefix, or both.
+ (with-temp-buffer
+ (let ((transient-mark-mode t)
+ (LaTeX-insert-into-comments t))
+ (latex-mode)
+ (auto-fill-mode 1)
+
+ ;; test 1: for bug#35284
+ ;; test 1-1
+ (insert "\\begin{document}
+% This is a comment
+\\def\\foo#1{foo}
+% another comment
+\\end{document}
+")
+ (set-mark (line-beginning-position 0)) ; just before \end{document}.
+ (goto-char (point-min))
+ (beginning-of-line 2) ; just before the first "%".
+ (LaTeX-insert-environment "verbatim")
+ (should (string=
+ (buffer-string)
+ "\\begin{document}
+% \\begin{verbatim}
+% This is a comment
+\\def\\foo#1{foo}
+% another comment
+% \\end{verbatim}
+\\end{document}
+"))
+
+ ;; test 1-2
+ (erase-buffer)
+ (insert "\\begin{document}
+% This is a comment
+\\def\\foo#1{foo}
+% another comment
+\\end{document}
+")
+ (set-mark (line-end-position -1)) ; just after "another comment"
+ (goto-char (point-min))
+ (beginning-of-line 2) ; just before the first "%".
+ (LaTeX-insert-environment "verbatim")
+ (should (string=
+ (buffer-string)
+ "\\begin{document}
+% \\begin{verbatim}
+% This is a comment
+\\def\\foo#1{foo}
+% another comment
+% \\end{verbatim}
+\\end{document}
+"))
+
+ (setq LaTeX-insert-into-comments nil)
+
+ ;; test 1-3
+ (erase-buffer)
+ (insert "\\begin{document}
+% This is a comment
+\\def\\foo#1{foo}
+% another comment
+\\end{document}
+")
+ (set-mark (line-beginning-position 0)) ; just before \end{document}
+ (goto-char (point-min))
+ (beginning-of-line 2) ; just before the first "%".
+ (LaTeX-insert-environment "verbatim")
+ (should (string=
+ (buffer-string)
+ "\\begin{document}
+\\begin{verbatim}
+% This is a comment
+\\def\\foo#1{foo}
+% another comment
+\\end{verbatim}
+\\end{document}
+"))
+ ;; test 2: for
+ ;; https://lists.gnu.org/archive/html/auctex/2019-11/msg00009.html
+
+ (setq LaTeX-insert-into-comments t)
+
+ ;; test 2-1
+ (erase-buffer)
+ (insert "abc def ghi")
+ (set-mark 5) ; just before "def"
+ (goto-char 8) ; just after "def"
+ (LaTeX-insert-environment "center")
+ (should (string=
+ (buffer-string)
+ "abc
+\\begin{center}
+ def
+\\end{center}
+ghi"))
+
+ ;; test 2-2
+ (erase-buffer)
+ (insert "abc
+def
+ghi")
+ (beginning-of-line 0) ; just before "def"
+ (set-mark (line-end-position)) ; just after "def"
+ (LaTeX-insert-environment "center")
+ (should (string=
+ (buffer-string)
+ "abc
+\\begin{center}
+ def
+\\end{center}
+ghi"))
+
+ ;; test 2-3
+ (erase-buffer)
+ (insert "\\begin{quote}
+ % abc
+ % def
+ % ghi
+\\end{quote}
+")
+ (set-mark (line-beginning-position 0)) ; just before \end{quote}
+ (goto-char (point-min))
+ (beginning-of-line 2) ; just before the first "%"
+ (LaTeX-insert-environment "center")
+ (should (string=
+ (buffer-string)
+ "\\begin{quote}
+ % \\begin{center}
+ % abc
+ % def
+ % ghi
+ % \\end{center}
+\\end{quote}
+"))
+
+ ;; test 2-4
+ (erase-buffer)
+ (insert "\\begin{quote}
+ %\s
+ % abc
+ % def
+ % ghi
+ %\s
+\\end{quote}
+")
+ (set-mark (line-end-position -1)) ; just after the last "% "
+ (goto-char (point-min))
+ (beginning-of-line 2) ; just before the first " %"
+ (LaTeX-insert-environment "center")
+ (should (string=
+ (buffer-string)
+ "\\begin{quote}
+ % \\begin{center}
+ % abc
+ % def
+ % ghi
+ % \\end{center}
+\\end{quote}
+"))
+
+ ;; test 2-5
+ (erase-buffer)
+ (insert "\\begin{quote}
+ %\s
+ % abc
+ % def
+ % ghi
+ %\s
+\\end{quote}
+")
+ (set-mark (1- (line-end-position -1))) ; just after the last "%"
+ (goto-char (point-min))
+ (beginning-of-line 2)
+ (forward-char 2) ; just before the first "%"
+ (LaTeX-insert-environment "center")
+ (should (string=
+ (buffer-string)
+ "\\begin{quote}
+ % \\begin{center}
+ % abc
+ % def
+ % ghi
+ % \\end{center}
+\\end{quote}
+"))
+
+ (setq LaTeX-insert-into-comments nil)
+
+ ;; test 2-6
+ (erase-buffer)
+ (insert "\\begin{quote}
+ % abc
+ % def
+ % ghi
+\\end{quote}
+")
+ (set-mark (line-beginning-position 0)) ; just before \end{quote}
+ (goto-char (point-min))
+ (beginning-of-line 2) ; just before the first " %"
+ (LaTeX-insert-environment "center")
+ (should (string=
+ (buffer-string)
+ "\\begin{quote}
+ \\begin{center}
+ % abc
+ % def
+ % ghi
+ \\end{center}
+\\end{quote}
+")))))
+
;;; latex-test.el ends here
- [elpa] externals/auctex 1d72a5c 21/43: Remove compatibility code for older emacsen, (continued)
- [elpa] externals/auctex 1d72a5c 21/43: Remove compatibility code for older emacsen, Tassilo Horn, 2020/04/11
- [elpa] externals/auctex 3424aae 15/43: Update style/fbox.el to package version 0.04, Tassilo Horn, 2020/04/11
- [elpa] externals/auctex c1e0419 23/43: Adjust style/fontaxes.el to LaTeX kernel, Tassilo Horn, 2020/04/11
- [elpa] externals/auctex 0bc0ff5 29/43: Add fontification support \textnormal macro, Tassilo Horn, 2020/04/11
- [elpa] externals/auctex 07efee4 31/43: Add new style/ltxguide.el, Tassilo Horn, 2020/04/11
- [elpa] externals/auctex 0d6271f 33/43: Add new style/overpic.el, Tassilo Horn, 2020/04/11
- [elpa] externals/auctex 54cb9c0 37/43: Load caption style before asking for package options, Tassilo Horn, 2020/04/11
- [elpa] externals/auctex aa85918 24/43: Add new style/revtex4-2.el, Tassilo Horn, 2020/04/11
- [elpa] externals/auctex dab40b7 38/43: Load caption style when asking for bicaption package options, Tassilo Horn, 2020/04/11
- [elpa] externals/auctex fb1fdc4 41/43: Fix y-or-n-p query prompts, Tassilo Horn, 2020/04/11
- [elpa] externals/auctex f2bac9d 11/43: Improve environment insertion (bug#35284),
Tassilo Horn <=
- [elpa] externals/auctex e35f85a 06/43: Update style/caption.el to package version 3.4a, Tassilo Horn, 2020/04/11
- [elpa] externals/auctex 2ceda21 09/43: Use pdf rather than dvi for preview package document, Tassilo Horn, 2020/04/11
- [elpa] externals/auctex 4c1bb12 10/43: ; * tex-style.el (LaTeX-exam-reftex-quick-id-key): Fix docstring., Tassilo Horn, 2020/04/11
- [elpa] externals/auctex d308a1e 14/43: Fix handling of LaTeX font declaration macros, Tassilo Horn, 2020/04/11
- [elpa] externals/auctex 68247ff 18/43: Delete obsolete comment, Tassilo Horn, 2020/04/11
- [elpa] externals/auctex 8979812 20/43: Don't use obsolete variable, Tassilo Horn, 2020/04/11
- [elpa] externals/auctex 7565d7c 19/43: Remove compatibility code for older emacsen, Tassilo Horn, 2020/04/11
- [elpa] externals/auctex 88edbf8 22/43: Improve support for extended NFSS macros, Tassilo Horn, 2020/04/11
- [elpa] externals/auctex 520585a 25/43: ; * style/revtex4-2.el: Silence the compiler., Tassilo Horn, 2020/04/11
- [elpa] externals/auctex 1d2e61d 26/43: Add new font macros in the related menus, Tassilo Horn, 2020/04/11