emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Emacs-diffs] emacs-24 r116923: Improve on previous quote autopairing ch


From: João Távora
Subject: [Emacs-diffs] emacs-24 r116923: Improve on previous quote autopairing change
Date: Mon, 07 Apr 2014 07:30:48 +0000
User-agent: Bazaar (2.6b2)

------------------------------------------------------------
revno: 116923
revision-id: address@hidden
parent: address@hidden
committer: João Távora <address@hidden>
branch nick: emacs-24
timestamp: Mon 2014-04-07 08:29:50 +0100
message:
  Improve on previous quote autopairing change
  
  Backported from trunk r116940
  
  * lisp/elec-pair.el:
  (electric-pair--syntax-ppss): When inside comments parse from
  comment beginning.
  (electric-pair--balance-info): Fix typo in comment.
  (electric-pair--in-unterminated-string-p): Delete.
  (electric-pair--unbalanced-strings-p): New function.
  (electric-pair-string-bound-function): New var.
  (electric-pair-inhibit-if-helps-balance): Decide quote pairing
  according to `electric-pair--in-unterminated-string-p'
  
  * test/automated/electric-tests.el (define-electric-pair-test): Don't
  overtest..
  (inhibit-in-mismatched-string-inside-ruby-comments): New test.
  (inhibit-in-mismatched-string-inside-c-comments): New test.
modified:
  lisp/ChangeLog                 changelog-20091113204419-o5vbwnq5f7feedwu-1432
  lisp/elec-pair.el              elecpair.el-20131227124533-yaq8recs0j0ggt67-1
  test/ChangeLog                 changelog-20091113204419-o5vbwnq5f7feedwu-8588
  test/automated/electric-tests.el 
electrictests.el-20131226202454-s3cqbs9maop0w8qy-1
=== modified file 'lisp/ChangeLog'
--- a/lisp/ChangeLog    2014-04-07 07:24:03 +0000
+++ b/lisp/ChangeLog    2014-04-07 07:29:50 +0000
@@ -1,5 +1,17 @@
 2014-04-07  João Távora  <address@hidden>
 
+       * elec-pair.el:
+       (electric-pair--syntax-ppss): When inside comments parse from
+       comment beginning.
+       (electric-pair--balance-info): Fix typo in comment.
+       (electric-pair--in-unterminated-string-p): Delete.
+       (electric-pair--unbalanced-strings-p): New function.
+       (electric-pair-string-bound-function): New var.
+       (electric-pair-inhibit-if-helps-balance): Decide quote pairing
+       according to `electric-pair--in-unterminated-string-p'
+
+2014-04-07  João Távora  <address@hidden>
+
        * elec-pair.el (electric-pair-inhibit-if-helps-balance): Inhibit
        quote pairing if point-max is inside an unterminated string.
        (electric-pair--looking-at-unterminated-string-p):

=== modified file 'lisp/elec-pair.el'
--- a/lisp/elec-pair.el 2014-04-07 07:24:03 +0000
+++ b/lisp/elec-pair.el 2014-04-07 07:29:50 +0000
@@ -227,11 +227,19 @@
   (let* ((pos (or pos (point)))
          (where (or where '(string comment)))
          (quick-ppss (syntax-ppss))
-         (quick-ppss-at-pos (syntax-ppss pos)))
-    (if (or (and (nth 3 quick-ppss) (memq 'string where))
-            (and (nth 4 quick-ppss) (memq 'comment where)))
+         (quick-ppss-at-pos (syntax-ppss pos))
+         (in-string (and (nth 3 quick-ppss-at-pos) (memq 'string where)))
+         (in-comment (and (nth 4 quick-ppss-at-pos) (memq 'comment where)))
+         (s-or-c-start (cond (in-string
+                              (1+ (nth 8 quick-ppss)))
+                             (in-comment
+                              (goto-char (nth 8 quick-ppss))
+                              (forward-comment (- (point-max)))
+                              (skip-syntax-forward " >!")
+                              (point)))))
+    (if s-or-c-start
         (with-syntax-table electric-pair-text-syntax-table
-          (parse-partial-sexp (1+ (nth 8 quick-ppss)) pos))
+          (parse-partial-sexp s-or-c-start pos))
       ;; HACK! cc-mode apparently has some `syntax-ppss' bugs
       (if (memq major-mode '(c-mode c++ mode))
           (parse-partial-sexp (point-min) pos)
@@ -321,7 +329,7 @@
           (scan-error
            (cond ((or
                    ;; some error happened and it is not of the "ended
-                   ;; prematurely" kind"...
+                   ;; prematurely" kind...
                    (not (string-match "ends prematurely" (nth 1 err)))
                    ;; ... or we were in a comment and just came out of
                    ;; it.
@@ -334,18 +342,29 @@
                   (funcall ended-prematurely-fn)))))))
     (cons innermost outermost)))
 
-(defun electric-pair--in-unterminated-string-p (char)
-  "Return non-nil if inside unterminated string started by CHAR"
-  (let* ((ppss (syntax-ppss))
-         (relevant-ppss (if (nth 4 ppss) ; in comment
-                            (electric-pair--syntax-ppss)
-                          ppss))
+(defvar electric-pair-string-bound-function 'point-max
+  "Next buffer position where strings are syntatically unexpected.
+Value is a function called with no arguments and returning a
+buffer position. Major modes should set this variable
+buffer-locally if they experience slowness with
+`electric-pair-mode' when pairing quotes.")
+
+(defun electric-pair--unbalanced-strings-p (char)
+  "Return non-nil if there are unbalanced strings started by CHAR."
+  (let* ((selector-ppss (syntax-ppss))
+         (relevant-ppss (save-excursion
+                          (if (nth 4 selector-ppss) ; comment
+                              (electric-pair--syntax-ppss
+                               (progn
+                                 (goto-char (nth 8 selector-ppss))
+                                 (forward-comment (point-max))
+                                 (skip-syntax-backward " >!")
+                                 (point)))
+                            (syntax-ppss
+                             (funcall electric-pair-string-bound-function)))))
          (string-delim (nth 3 relevant-ppss)))
-    (and (or (eq t string-delim)
-             (eq char string-delim))
-         (condition-case nil (progn (scan-sexps (nth 8 relevant-ppss) 1)
-                                    nil)
-           (scan-error t)))))
+    (or (eq t string-delim)
+        (eq char string-delim))))
 
 (defun electric-pair--inside-string-p (char)
   "Return non-nil if point is inside a string started by CHAR.
@@ -378,9 +397,7 @@
                           (t
                            (eq (cdr outermost) pair)))))
                  ((eq syntax ?\")
-                  (save-excursion
-                    (goto-char (point-max))
-                    (electric-pair--in-unterminated-string-p char)))))
+                  (electric-pair--unbalanced-strings-p char))))
        (insert-char char)))))
 
 (defun electric-pair-skip-if-helps-balance (char)

=== modified file 'test/ChangeLog'
--- a/test/ChangeLog    2014-04-07 07:24:03 +0000
+++ b/test/ChangeLog    2014-04-07 07:29:50 +0000
@@ -1,5 +1,12 @@
 2014-04-07  João Távora  <address@hidden>
 
+       * automated/electric-tests.el (define-electric-pair-test): Don't
+       overtest..
+       (inhibit-in-mismatched-string-inside-ruby-comments): New test.
+       (inhibit-in-mismatched-string-inside-c-comments): New test.
+
+2014-04-07  João Távora  <address@hidden>
+
        * automated/electric-tests.el (inhibit-if-strings-mismatched):
        New test, change from `inhibit-only-of-next-is-mismatched'.
 

=== modified file 'test/automated/electric-tests.el'
--- a/test/automated/electric-tests.el  2014-04-07 07:24:03 +0000
+++ b/test/automated/electric-tests.el  2014-04-07 07:29:50 +0000
@@ -141,7 +141,7 @@
           expected-string
           expected-point
           bindings
-          (modes '(quote (emacs-lisp-mode ruby-mode c++-mode)))
+          (modes '(quote (ruby-mode c++-mode)))
           (test-in-comments t)
           (test-in-strings t)
           (test-in-code t)
@@ -303,6 +303,48 @@
   :bindings `((electric-pair-text-syntax-table
                . ,prog-mode-syntax-table)))
 
+(define-electric-pair-test inhibit-in-mismatched-string-inside-ruby-comments
+  "foo\"\"
+#
+#    \"bar\"
+#    \"   \"
+#    \"
+#
+baz\"\""
+  "\""
+  :modes '(ruby-mode)
+  :test-in-strings nil
+  :test-in-comments nil
+  :expected-point 19
+  :expected-string
+  "foo\"\"
+#
+#    \"bar\"\"
+#    \"   \"
+#    \"
+#
+baz\"\""
+  :fixture-fn #'(lambda () (goto-char (point-min)) (search-forward "bar")))
+
+(define-electric-pair-test inhibit-in-mismatched-string-inside-c-comments
+  "foo\"\"/*
+    \"bar\"
+    \"   \"
+    \"
+*/baz\"\""
+  "\""
+  :modes '(c-mode)
+  :test-in-strings nil
+  :test-in-comments nil
+  :expected-point 18
+  :expected-string
+  "foo\"\"/*
+    \"bar\"\"
+    \"   \"
+    \"
+*/baz\"\""
+  :fixture-fn #'(lambda () (goto-char (point-min)) (search-forward "bar")))
+
 
 ;;; More quotes, but now don't bind `electric-pair-text-syntax-table'
 ;;; to `prog-mode-syntax-table'. Use the defaults for


reply via email to

[Prev in Thread] Current Thread [Next in Thread]