Subject: bug#11531: 24.1.50; electric-pair-mode needs to detect apostrophes in strings or comments

From: Simon Law <[email-masked]>
Date: Mon, 21 May 2012 00:33:47 -0400

Reproduction steps:

$ emacs -Q
M-x js-mode
M-x electric-pair-mode
Type: // Simon's comment
Press RET
Type "Simon's string"
Press RET

Expected result:

// Simon's comment
"Simon's string"

Actual result:

// Simon's comment'
"Simon's string'"

Notes:

Inside a comment or string, in a programming mode, we can try to detect
apostophes. An apostrophe is likely if you've inserted a single-quote
that immediately follows a word character.

Here is my workaround:

(defadvice electric-pair-post-self-insert-function (around
                                                    electric-pair-apostrophe
                                                    activate)
  "Fix electric-pair to handle apostrophes inside strings or comments"
  (let* ((quick-syntax-info (syntax-ppss))
         (inside-string (fourth quick-syntax-info))
         (inside-comment (fifth quick-syntax-info)))
    (if (not (eq last-command-event ?\'))
        ;; Only deal with single-quote characters
        ad-do-it
      (let* ((char-before-insert (char-before (1- (point))))
             (char-before-syntax (and char-before-insert
                                     (char-syntax char-before-insert))))
        ;; We want the character before the self-insert-command
        (if (and (or inside-string inside-comment)
                 (eq char-before-syntax ?w))
            ;; Single-quotes inside a string or comment, and immediately
            ;; following a word, are actually apostrophes and should not
            ;; be paired.
            t
          ad-do-it)))))

In GNU Emacs 24.1.50.1 (x86_64-pc-linux-gnu, GTK+ Version 3.2.0)
 of 2012-05-10 on charichuelo, modified by Debian
 (emacs-snapshot package, version 2:20120510-1~ppa1~oneiric1)
Windowing system distributor `The X.Org Foundation', version 11.0.11004000
Configured using:
 `configure '--build' 'x86_64-linux-gnu' '--host' 'x86_64-linux-gnu'
 '--prefix=/usr' '--sharedstatedir=/var/lib' '--libexecdir=/usr/lib'
 '--localstatedir=/var' '--infodir=/usr/share/info'
 '--mandir=/usr/share/man' '--with-pop=yes'
 '--enable-locallisppath=/etc/emacs-snapshot:/etc/emacs:/usr/local/share/emacs/24.1.50/site-lisp:/usr/local/share/emacs/site-lisp:/usr/share/emacs/24.1.50/site-lisp:/usr/share/emacs/site-lisp'
 '--without-compress-info' '--with-crt-dir=/usr/lib/x86_64-linux-gnu/'
 '--with-x=yes' '--with-x-toolkit=gtk3' '--with-imagemagick=yes'
 'build_alias=x86_64-linux-gnu' 'host_alias=x86_64-linux-gnu'
 'CFLAGS=-DDEBIAN -DSITELOAD_PURESIZE_EXTRA=5000 -g -O2' 'LDFLAGS=-g
 -Wl,--as-needed -znocombreloc' 'CPPFLAGS=''

Important settings:
  value of $LC_ALL: nil
  value of $LC_COLLATE: en_CA.UTF-8
  value of $LC_CTYPE: en_CA.UTF-8
  value of $LC_MESSAGES: en_CA.UTF-8
  value of $LC_MONETARY: nil
  value of $LC_NUMERIC: nil
  value of $LC_TIME: nil
  value of $LANG: en_CA.UTF-8
  value of $XMODIFIERS: nil
  locale-coding-system: utf-8-unix
  default enable-multibyte-characters: t

Major mode: Javascript

Minor modes in effect:
  electric-pair-mode: t
  tooltip-mode: t
  mouse-wheel-mode: t
  tool-bar-mode: t
  menu-bar-mode: t
  file-name-shadow-mode: t
  global-font-lock-mode: t
  font-lock-mode: t
  blink-cursor-mode: t
  auto-composition-mode: t
  auto-encryption-mode: t
  auto-compression-mode: t
  line-number-mode: t
  transient-mark-mode: t

Recent input:
M-x j s - m o d e <return> M-x e l e c t r i c - p 
a i r - m o d e <return> / / SPC S i m o n ' s SPC 
c o m m e n t C-e <return> " S i m o n ' s SPC s t 
r i n g C-n M-x r e p o r t - e m a c s - b u g
<return>

Recent messages:
For information about GNU Emacs and the GNU system, type C-h C-a.
Electric-Pair mode enabled
byte-code: End of buffer

Load-path shadows:
/usr/share/emacs/24.1.50/site-lisp/debian-startup hides /usr/share/emacs/site-lisp/debian-startup

Features:
(shadow sort gnus-util mail-extr emacsbug message format-spec rfc822 mml
mml-sec mm-decode mm-bodies mm-encode mail-parse rfc2231 mailabbrev
gmm-utils mailheader sendmail rfc2047 rfc2045 ietf-drums mm-util
mail-prsvr mail-utils cus-start cus-load electric js byte-opt bytecomp
byte-compile cconv macroexp json imenu thingatpt cc-mode cc-fonts
easymenu cc-guess cc-menus cc-cmds cc-styles cc-align cc-engine cc-vars
cc-defs time-date tooltip ediff-hook vc-hooks lisp-float-type mwheel
x-win x-dnd tool-bar dnd fontset image regexp-opt fringe tabulated-list
newcomment lisp-mode register page menu-bar rfn-eshadow timer select
scroll-bar mouse jit-lock font-lock syntax facemenu font-core frame cham
georgian utf-8-lang misc-lang vietnamese tibetan thai tai-viet lao
korean japanese hebrew greek romanian slovak czech european ethiopic
indian cyrillic chinese case-table epa-hook jka-cmpr-hook help simple
abbrev minibuffer loaddefs button faces cus-face files text-properties
overlay sha1 md5 base64 format env code-pages mule custom widget
hashtable-print-readable backquote make-network-process dbusbind
dynamic-setting system-font-setting font-render-setting move-toolbar gtk
x-toolkit x multi-tty emacs)