>From b3467c27c31ca2034f67f56438cfc8576c8f38b2 Mon Sep 17 00:00:00 2001 From: Paul Eggert
Date: Sun, 9 Oct 2016 11:29:50 -0700 Subject: [PATCH 2/2] electric-quote-chars fixups MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * lisp/electric.el (electric-quote-chars): Check types and safety more carefully. (electric--insertable-p): Donât bother with string argument, as that is no longer used. Donât call âstringâ unless needed. (electric-quote-post-self-insert-function): Use more-mnemonic locals. Omit no-longer-necessary runtime error diagnostic. --- lisp/electric.el | 64 +++++++++++++++++++++++++++++--------------------------- 1 file changed, 33 insertions(+), 31 deletions(-) diff --git a/lisp/electric.el b/lisp/electric.el index 5c7be35..b15e8ab 100644 --- a/lisp/electric.el +++ b/lisp/electric.el @@ -427,24 +427,27 @@ electric-quote-string (defcustom electric-quote-chars '(?â ?â ?â ?â) "Curved quote characters for `electric-quote-mode'. -The items correspond to the left single quote, the right single -quote, the left double quote, and the right double quote, respectively." - :version "25.1" - :type 'list :safe 'listp :group 'electricity) +This list's members correspond to left single quote, right single +quote, left double quote, and right double quote, respectively." + :version "26.1" + :type '(list character character character character) + :safe #'(lambda (x) + (pcase x + (`(,(pred characterp) ,(pred characterp) + ,(pred characterp) ,(pred characterp)) + t))) + :group 'electricity) (defcustom electric-quote-paragraph t "Non-nil means to use electric quoting in text paragraphs." :version "25.1" :type 'boolean :safe 'booleanp :group 'electricity) -(defun electric--insertable-p (string-or-char) - (let ((str (if (characterp string-or-char) - (string string-or-char) - string-or-char))) - (or (not buffer-file-coding-system) - (eq (coding-system-base buffer-file-coding-system) 'undecided) - (not (unencodable-char-position nil nil buffer-file-coding-system - nil str))))) +(defun electric--insertable-p (char) + (or (not buffer-file-coding-system) + (eq (coding-system-base buffer-file-coding-system) 'undecided) + (not (unencodable-char-position nil nil buffer-file-coding-system + nil (string char))))) (defun electric-quote-post-self-insert-function () "Function that `electric-quote-mode' adds to `post-self-insert-hook'. @@ -468,32 +471,31 @@ electric-quote-post-self-insert-function (or (eq last-command-event ?\`) (save-excursion (backward-paragraph) (point))))))) (pcase electric-quote-chars - (`(,q1 ,q2 ,q3 ,q4) + (`(,l ,r ,ll ,rr) (when start (save-excursion (if (eq last-command-event ?\`) - (cond ((and (electric--insertable-p q3) - (search-backward (string q1 ?`) (- (point) 2) t)) - (replace-match (string q3)) + (cond ((and (electric--insertable-p ll) + (search-backward (string l ?`) (- (point) 2) t)) + (replace-match (string ll)) (when (and electric-pair-mode (eq (cdr-safe - (assq q1 electric-pair-text-pairs)) + (assq l electric-pair-text-pairs)) (char-after))) (delete-char 1)) - (setq last-command-event q3)) - ((and (electric--insertable-p q1) + (setq last-command-event ll)) + ((and (electric--insertable-p l) (search-backward "`" (1- (point)) t)) - (replace-match (string q1)) - (setq last-command-event q1))) - (cond ((and (electric--insertable-p q4) - (search-backward (string q2 ?') (- (point) 2) t)) - (replace-match (string q4)) - (setq last-command-event q4)) - ((and (electric--insertable-p q2) + (replace-match (string l)) + (setq last-command-event l))) + (cond ((and (electric--insertable-p rr) + (search-backward (string r ?') (- (point) 2) t)) + (replace-match (string rr)) + (setq last-command-event rr)) + ((and (electric--insertable-p r) (search-backward "'" (1- (point)) t)) - (replace-match (string q2)) - (setq last-command-event q2))))))) - (_ (error "âelectric-quote-charsâ must contain exactly 4 characters.")))))) + (replace-match (string r)) + (setq last-command-event r))))))))))) (put 'electric-quote-post-self-insert-function 'priority 10) @@ -510,8 +512,8 @@ electric-quote-mode `electric-quote-comment', `electric-quote-string', and `electric-quote-paragraph'. -Customize `electric-quote-chars' in order to use quote chars -other than the ones listed here. +Customize `electric-quote-chars' to use characters other than the +ones listed here. This is a global minor mode. To toggle the mode in a single buffer, use `electric-quote-local-mode'." -- 2.7.4