aboutsummaryrefslogtreecommitdiffstats
path: root/lisp
diff options
context:
space:
mode:
authorPhilipp Stephani2017-06-28 23:47:57 +0200
committerPhilipp Stephani2017-07-02 17:48:23 +0200
commit34d4720f833bb382b28d9faecf82d34db1eb4494 (patch)
treef149e3b03da94c7db458610007e82b33ac735018 /lisp
parentd90b98a2a52abf67b84aa12df282b0defec8505b (diff)
downloademacs-34d4720f833bb382b28d9faecf82d34db1eb4494.tar.gz
emacs-34d4720f833bb382b28d9faecf82d34db1eb4494.zip
Electric quotes: Improve support for Markdown mode (Bug#24709)
Introduce a new user option 'electric-quote-context-sensitive'. If non-nil, have ' insert an opening quote if sensible. Also introduce a new variable 'electric-quote-code-faces'. Major modes such as 'markdown-mode' can add faces to this list to treat text as inline code and disable electric quoting. * lisp/electric.el (electric-quote-context-sensitive): New user option. (electric-quote-code-faces): New variable. (electric-quote-post-self-insert-function): Treat ' as ` if desired and applicable; disable electric quoting for given faces. * test/lisp/electric-tests.el (electric-quote-opening-single) (electric-quote-closing-single, electric-quote-opening-double) (electric-quote-closing-double) (electric-quote-context-sensitive-backtick) (electric-quote-context-sensitive-bob-single) (electric-quote-context-sensitive-bob-double) (electric-quote-context-sensitive-bol-single) (electric-quote-context-sensitive-bol-double) (electric-quote-context-sensitive-after-space-single) (electric-quote-context-sensitive-after-space-double) (electric-quote-context-sensitive-after-letter-single) (electric-quote-context-sensitive-after-letter-double) (electric-quote-context-sensitive-after-paren-single) (electric-quote-context-sensitive-after-paren-double) (electric-quote-markdown-in-text) (electric-quote-markdown-in-code): New unit tests.
Diffstat (limited to 'lisp')
-rw-r--r--lisp/electric.el66
1 files changed, 47 insertions, 19 deletions
diff --git a/lisp/electric.el b/lisp/electric.el
index 4078ef8193e..1564df5949c 100644
--- a/lisp/electric.el
+++ b/lisp/electric.el
@@ -443,11 +443,24 @@ quote, left double quote, and right double quote, respectively."
443 :version "25.1" 443 :version "25.1"
444 :type 'boolean :safe 'booleanp :group 'electricity) 444 :type 'boolean :safe 'booleanp :group 'electricity)
445 445
446(defcustom electric-quote-context-sensitive nil
447 "Non-nil means to replace \\=' with an electric quote depending on context.
448If `electric-quote-context-sensitive' is non-nil, Emacs replaces
449\\=' and \\='\\=' with an opening quote after a line break,
450whitespace, opening parenthesis, or quote and leaves \\=` alone."
451 :version "26.1"
452 :type 'boolean :safe #'booleanp :group 'electricity)
453
454(defvar electric-quote-code-faces ()
455 "List of faces to treat as inline code in `text-mode'.")
456
446(defun electric-quote-post-self-insert-function () 457(defun electric-quote-post-self-insert-function ()
447 "Function that `electric-quote-mode' adds to `post-self-insert-hook'. 458 "Function that `electric-quote-mode' adds to `post-self-insert-hook'.
448This requotes when a quoting key is typed." 459This requotes when a quoting key is typed."
449 (when (and electric-quote-mode 460 (when (and electric-quote-mode
450 (memq last-command-event '(?\' ?\`))) 461 (or (eq last-command-event ?\')
462 (and (not electric-quote-context-sensitive)
463 (eq last-command-event ?\`))))
451 (let ((start 464 (let ((start
452 (if (and comment-start comment-use-syntax) 465 (if (and comment-start comment-use-syntax)
453 (when (or electric-quote-comment electric-quote-string) 466 (when (or electric-quote-comment electric-quote-string)
@@ -462,30 +475,45 @@ This requotes when a quoting key is typed."
462 (syntax-ppss (1- (point))))))))) 475 (syntax-ppss (1- (point)))))))))
463 (and electric-quote-paragraph 476 (and electric-quote-paragraph
464 (derived-mode-p 'text-mode) 477 (derived-mode-p 'text-mode)
478 ;; FIXME: There should be a ‘cl-disjoint’ function.
479 (null (cl-intersection (face-at-point nil 'multiple)
480 electric-quote-code-faces
481 :test #'eq))
482 ;; FIXME: Why is the next form there? It’s never
483 ;; nil.
465 (or (eq last-command-event ?\`) 484 (or (eq last-command-event ?\`)
466 (save-excursion (backward-paragraph) (point))))))) 485 (save-excursion (backward-paragraph) (point)))))))
467 (pcase electric-quote-chars 486 (pcase electric-quote-chars
468 (`(,q< ,q> ,q<< ,q>>) 487 (`(,q< ,q> ,q<< ,q>>)
469 (when start 488 (when start
470 (save-excursion 489 (save-excursion
471 (if (eq last-command-event ?\`) 490 (let ((backtick ?\`))
472 (cond ((search-backward (string q< ?`) (- (point) 2) t) 491 (if (or (eq last-command-event ?\`)
473 (replace-match (string q<<)) 492 (and electric-quote-context-sensitive
474 (when (and electric-pair-mode 493 (save-excursion
475 (eq (cdr-safe 494 (backward-char)
476 (assq q< electric-pair-text-pairs)) 495 (or (bobp) (bolp)
477 (char-after))) 496 (memq (char-before) (list q< q<<))
478 (delete-char 1)) 497 (memq (char-syntax (char-before))
479 (setq last-command-event q<<)) 498 '(?\s ?\())))
480 ((search-backward "`" (1- (point)) t) 499 (setq backtick ?\')))
481 (replace-match (string q<)) 500 (cond ((search-backward (string q< backtick) (- (point) 2) t)
482 (setq last-command-event q<))) 501 (replace-match (string q<<))
483 (cond ((search-backward (string q> ?') (- (point) 2) t) 502 (when (and electric-pair-mode
484 (replace-match (string q>>)) 503 (eq (cdr-safe
485 (setq last-command-event q>>)) 504 (assq q< electric-pair-text-pairs))
486 ((search-backward "'" (1- (point)) t) 505 (char-after)))
487 (replace-match (string q>)) 506 (delete-char 1))
488 (setq last-command-event q>))))))))))) 507 (setq last-command-event q<<))
508 ((search-backward (string backtick) (1- (point)) t)
509 (replace-match (string q<))
510 (setq last-command-event q<)))
511 (cond ((search-backward (string q> ?') (- (point) 2) t)
512 (replace-match (string q>>))
513 (setq last-command-event q>>))
514 ((search-backward "'" (1- (point)) t)
515 (replace-match (string q>))
516 (setq last-command-event q>))))))))))))
489 517
490(put 'electric-quote-post-self-insert-function 'priority 10) 518(put 'electric-quote-post-self-insert-function 'priority 10)
491 519