From: Joao Tavora Date: Sun, 14 Dec 2014 11:22:46 +0000 (+0000) Subject: Consider electric-pair-mode in tex-mode. X-Git-Tag: emacs-24.4.90~133 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=bb57c94;p=emacs.git 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. --- diff --git a/lisp/ChangeLog b/lisp/ChangeLog index 4be07b5921c..204283ea705 100644 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog @@ -1,3 +1,8 @@ +2014-12-14 João Távora + + * textmodes/tex-mode.el (tex-insert-quote): Consider and respect + `electric-pair-mode' (bug#19356). + 2014-12-12 Michael Albinus * simple.el (password-word-equivalents): Add "passcode", used for diff --git a/lisp/textmodes/tex-mode.el b/lisp/textmodes/tex-mode.el index 18843bcd15a..cb8f2ee4357 100644 --- a/lisp/textmodes/tex-mode.el +++ b/lisp/textmodes/tex-mode.el @@ -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. diff --git a/test/ChangeLog b/test/ChangeLog index 442e802a1bb..a117834cd34 100644 --- a/test/ChangeLog +++ b/test/ChangeLog @@ -1,3 +1,8 @@ +2014-12-14 João Távora + + * automated/electric-tests.el (autowrapping-7): Tests for + tex-mode. + 2014-12-13 Glenn Morris * automated/flymake/warnpred/test.pl: Tweak format, since the diff --git a/test/automated/electric-tests.el b/test/automated/electric-tests.el index b1908e6bb32..cd07213bf20 100644 --- a/test/automated/electric-tests.el +++ b/test/automated/electric-tests.el @@ -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