* Make ‘electric-quote-mode’ not merge single quotes
@ 2022-08-02 10:42 Andrea Greselin
2022-08-03 12:50 ` Andrea Greselin
0 siblings, 1 reply; 2+ messages in thread
From: Andrea Greselin @ 2022-08-02 10:42 UTC (permalink / raw)
To: emacs-devel
[-- Attachment #1: Type: text/plain, Size: 4726 bytes --]
Hello everyone,
I stumbled a couple of times across a behaviour by
‘electric-quote-mode’ where it replaces two consecutive single
electric quotes with a single double quote. To see what I mean open
‘emacs -Q’, enable ‘electric-quote-mode’ and ‘text-mode’, then hit `
or ' twice. I find it more annoying than helpful so I changed
‘electric-quote-post-self-insert-function’ to make it check an option
that can disable this behaviour. It involves the two lines marked with
‘<--’ arrows in the code below.
Do you think it can be included in Emacs?
Best of wishes,
Andrea
Code:
(defcustom electric-quote-replace-consecutive t
"Non-nil means to replace two consecutive electric single quotes
with the corresponding (opening or closing) double quote."
:type 'boolean :safe #'booleanp :group 'electricity)
(defun electric-quote-post-self-insert-function ()
"Function that `electric-quote-mode' adds to `post-self-insert-hook'.
This requotes when a quoting key is typed."
(when (and electric-quote-mode
(or (eq last-command-event ?\')
(and (not electric-quote-context-sensitive)
(eq last-command-event ?\`))
(and electric-quote-replace-double
(eq last-command-event ?\")))
(not (run-hook-with-args-until-success
'electric-quote-inhibit-functions))
(if (derived-mode-p 'text-mode)
electric-quote-paragraph
(and comment-start comment-use-syntax
(or electric-quote-comment electric-quote-string)
(let* ((syntax (syntax-ppss))
(beg (nth 8 syntax)))
(and beg
(or (and electric-quote-comment (nth 4
syntax))
(and electric-quote-string (nth 3
syntax)))
;; Do not requote a quote that starts or ends
;; a comment or string.
(eq beg (nth 8 (save-excursion
(syntax-ppss (1-
(point)))))))))))
(pcase electric-quote-chars
(`(,q< ,q> ,q<< ,q>>)
(save-excursion
(let ((backtick ?\`))
(if (or (eq last-command-event ?\`)
(and (or electric-quote-context-sensitive ;; <--
Changed here
(and electric-quote-replace-double
(eq last-command-event ?\")))
(save-excursion
(backward-char)
(skip-syntax-backward "\\")
(or (bobp) (bolp)
(memq (char-before) (list q< q<<))
(memq (char-syntax (char-before))
'(?\s ?\())))
(setq backtick ?\')))
(cond ((and electric-quote-replace-consecutive ;; <-----
and here
(search-backward (string q< backtick) (-
(point) 2) t))
(replace-match (string q<<))
(when (and electric-pair-mode
(eq (cdr-safe
(assq q<
electric-pair-text-pairs))
(char-after)))
(delete-char 1))
(setq last-command-event q<<))
((search-backward (string backtick) (1- (point)) t)
(replace-match (string q<))
(setq last-command-event q<))
((search-backward "\"" (1- (point)) t)
(replace-match (string q<<))
(setq last-command-event q<<)))
(cond ((and electric-quote-replace-consecutive
(search-backward (string q> ?') (- (point) 2)
t))
(replace-match (string q>>))
(setq last-command-event q>>))
((search-backward "'" (1- (point)) t)
(replace-match (string q>))
(setq last-command-event q>))
((search-backward "\"" (1- (point)) t)
(replace-match (string q>>))
(setq last-command-event q>>))))))))))
[-- Attachment #2: Type: text/html, Size: 5983 bytes --]
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: Make ‘electric-quote-mode’ not merge single quotes
2022-08-02 10:42 Make ‘electric-quote-mode’ not merge single quotes Andrea Greselin
@ 2022-08-03 12:50 ` Andrea Greselin
0 siblings, 0 replies; 2+ messages in thread
From: Andrea Greselin @ 2022-08-03 12:50 UTC (permalink / raw)
To: emacs-devel
[-- Attachment #1: Type: text/plain, Size: 1928 bytes --]
Here's a patch that implements the changes described in my previous
message. It defaults to the current behaviour.
Andrea
--- electric--v28.1.el 2022-08-02 14:41:09.578483586 +0200
+++ electric--modified.el 2022-08-02 14:47:17.712875776 +0200
@@ -534,6 +534,11 @@
:version "26.1"
:type 'boolean :safe #'booleanp :group 'electricity)
+(defcustom electric-quote-replace-consecutive t
+ "Non-nil means to replace two consecutive electric single quotes
+with the corresponding (opening or closing) double quote."
+ :type 'boolean :safe #'booleanp :group 'electricity)
+
(defvar electric-quote-inhibit-functions ()
"List of functions that should inhibit electric quoting.
When the variable `electric-quote-mode' is non-nil, Emacs will
@@ -586,7 +591,8 @@
(memq (char-syntax (char-before))
'(?\s ?\())))
(setq backtick ?\')))
- (cond ((search-backward (string q< backtick) (- (point)
2) t)
+ (cond ((and electric-quote-replace-consecutive
+ (search-backward (string q< backtick) (-
(point) 2) t))
(replace-match (string q<<))
(when (and electric-pair-mode
(eq (cdr-safe
@@ -600,7 +606,8 @@
((search-backward "\"" (1- (point)) t)
(replace-match (string q<<))
(setq last-command-event q<<)))
- (cond ((search-backward (string q> ?') (- (point) 2) t)
+ (cond ((and electric-quote-replace-consecutive
+ (search-backward (string q> ?') (- (point) 2)
t))
(replace-match (string q>>))
(setq last-command-event q>>))
((search-backward "'" (1- (point)) t)
[-- Attachment #2: Type: text/html, Size: 2522 bytes --]
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2022-08-03 12:50 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-02 10:42 Make ‘electric-quote-mode’ not merge single quotes Andrea Greselin
2022-08-03 12:50 ` Andrea Greselin
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).