Consider electric-pair-mode in tex-mode.

Fixes: debbugs:19356

* lisp/textmodes/tex-mode.el (tex-insert-quote): Consider and respect
`electric-pair-mode'.

* test/automated/electric-tests.el (autowrapping-7): New test for
tex-mode's autowrapping.
(electric-pair-test-for): Call the actual key-binding
interactively.
This commit is contained in:
Joao Tavora 2014-12-14 11:22:46 +00:00
parent 7b945728d3
commit bb57c94d5f
4 changed files with 60 additions and 11 deletions

View file

@ -1,3 +1,8 @@
2014-12-14 João Távora <joaotavora@gmail.com>
* textmodes/tex-mode.el (tex-insert-quote): Consider and respect
`electric-pair-mode' (bug#19356).
2014-12-12 Michael Albinus <michael.albinus@gmx.de>
* simple.el (password-word-equivalents): Add "passcode", used for

View file

@ -1277,18 +1277,48 @@ Inserts the value of `tex-open-quote' (normally ``) or `tex-close-quote'
\(normally '') depending on the context. With prefix argument, always
inserts \" characters."
(interactive "*P")
;; Discover if we'll be inserting normal double quotes.
;;
(if (or arg (memq (char-syntax (preceding-char)) '(?/ ?\\))
(eq (get-text-property (point) 'face) 'tex-verbatim)
(save-excursion
(backward-char (length tex-open-quote))
(when (or (looking-at (regexp-quote tex-open-quote))
(looking-at (regexp-quote tex-close-quote)))
(delete-char (length tex-open-quote))
t)))
(eq (get-text-property (point) 'face) 'tex-verbatim)
;; Discover if a preceding occurance of `tex-open-quote'
;; should be morphed to a normal double quote.
;;
(and (>= (point) (+ (point-min) (length tex-open-quote)))
(save-excursion
(backward-char (length tex-open-quote))
(when (or (looking-at (regexp-quote tex-open-quote))
(looking-at (regexp-quote tex-close-quote)))
(delete-char (length tex-open-quote))
(when (looking-at (regexp-quote tex-close-quote))
(delete-char (length tex-close-quote)))
t))))
;; Insert the normal quote (eventually letting
;; `electric-pair-mode' do its thing).
;;
(self-insert-command (prefix-numeric-value arg))
(insert (if (or (memq (char-syntax (preceding-char)) '(?\( ?> ?\s))
(memq (preceding-char) '(?~)))
tex-open-quote tex-close-quote))))
;; We'll be inserting fancy TeX quotes, but consider and imitate
;; `electric-pair-mode''s two behaviours: pair-insertion and
;; region wrapping.
;;
(if (and electric-pair-mode (use-region-p))
(let* ((saved (point-marker)))
(goto-char (mark))
(insert (if (> saved (mark)) tex-open-quote tex-close-quote))
(goto-char saved)
(insert (if (> saved (mark)) tex-close-quote tex-open-quote)))
(if (or (memq (char-syntax (preceding-char)) '(?\( ?> ?\s))
(memq (preceding-char) '(?~)))
(if electric-pair-mode
(if (looking-at (regexp-quote tex-close-quote))
(forward-char (length tex-close-quote))
(insert tex-open-quote)
(insert tex-close-quote)
(backward-char (length tex-close-quote)))
(insert tex-open-quote))
(if (looking-at (regexp-quote tex-close-quote))
(forward-char (length tex-close-quote))
(insert tex-close-quote))))))
(defun tex-validate-buffer ()
"Check current buffer for paragraphs containing mismatched braces or $s.

View file

@ -1,3 +1,8 @@
2014-12-14 João Távora <joaotavora@gmail.com>
* automated/electric-tests.el (autowrapping-7): Tests for
tex-mode.
2014-12-13 Glenn Morris <rgm@gnu.org>
* automated/flymake/warnpred/test.pl: Tweak format, since the

View file

@ -60,7 +60,7 @@
(cl-progv
(mapcar #'car bindings)
(mapcar #'cdr bindings)
(self-insert-command 1))))
(call-interactively (key-binding `[,last-command-event])))))
(should (equal (buffer-substring-no-properties (point-min) (point-max))
expected-string))
(should (equal (point)
@ -575,5 +575,14 @@ baz\"\""
(skip-chars-backward "\"")
(mark-sexp -1)))
(define-electric-pair-test autowrapping-7
"foo" "\"" :expected-string "``foo''" :expected-point 8
:modes '(tex-mode)
:fixture-fn #'(lambda ()
(electric-pair-mode 1)
(goto-char (point-max))
(skip-chars-backward "\"")
(mark-sexp -1)))
(provide 'electric-tests)
;;; electric-tests.el ends here