]> code.delx.au - gnu-emacs/blobdiff - lisp/elec-pair.el
Merge branch 'master' of git.savannah.gnu.org:/srv/git/emacs
[gnu-emacs] / lisp / elec-pair.el
index c16c1141800a8c7929c65c8af0490dce123c7b51..116292027cd614f74f84d03e9ac2564c4424935d 100644 (file)
@@ -1,6 +1,6 @@
 ;;; elec-pair.el --- Automatic parenthesis pairing  -*- lexical-binding:t -*-
 
-;; Copyright (C) 2013-2014 Free Software Foundation, Inc.
+;; Copyright (C) 2013-2016 Free Software Foundation, Inc.
 
 ;; Author: João Távora <joaotavora@gmail.com>
 
@@ -226,10 +226,9 @@ WHERE is a list defaulting to '(string comment) and indicates
 when to fallback to `parse-partial-sexp'."
   (let* ((pos (or pos (point)))
          (where (or where '(string comment)))
-         (quick-ppss (syntax-ppss))
-         (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)))
+         (quick-ppss (syntax-ppss pos))
+         (in-string (and (nth 3 quick-ppss) (memq 'string where)))
+         (in-comment (and (nth 4 quick-ppss) (memq 'comment where)))
          (s-or-c-start (cond (in-string
                               (1+ (nth 8 quick-ppss)))
                              (in-comment
@@ -243,7 +242,7 @@ when to fallback to `parse-partial-sexp'."
       ;; HACK! cc-mode apparently has some `syntax-ppss' bugs
       (if (memq major-mode '(c-mode c++ mode))
           (parse-partial-sexp (point-min) pos)
-        quick-ppss-at-pos))))
+        quick-ppss))))
 
 ;; Balancing means controlling pairing and skipping of parentheses
 ;; so that, if possible, the buffer ends up at least as balanced as
@@ -343,7 +342,7 @@ If point is not enclosed by any lists, return ((t) . (t))."
     (cons innermost outermost)))
 
 (defvar electric-pair-string-bound-function 'point-max
-  "Next buffer position where strings are syntatically unexpected.
+  "Next buffer position where strings are syntactically 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
@@ -476,7 +475,8 @@ happened."
                      (when (and (not (and unconditional
                                           (eq syntax ?\")))
                                 (setq skip-whitespace-info
-                                      (if (functionp electric-pair-skip-whitespace)
+                                      (if (and (not (eq electric-pair-skip-whitespace 'chomp))
+                                               (functionp electric-pair-skip-whitespace))
                                           (funcall electric-pair-skip-whitespace)
                                         electric-pair-skip-whitespace)))
                        (electric-pair--skip-whitespace))
@@ -499,7 +499,7 @@ happened."
                   (not (funcall electric-pair-inhibit-predicate
                                 last-command-event))))
          (save-excursion (electric-pair--insert pair)))))
-      (t
+      (_
        (when (and (if (functionp electric-pair-open-newline-between-pairs)
                       (funcall electric-pair-open-newline-between-pairs)
                     electric-pair-open-newline-between-pairs)
@@ -558,7 +558,8 @@ the mode if ARG is omitted or nil.
 
 Electric Pair mode is a global minor mode.  When enabled, typing
 an open parenthesis automatically inserts the corresponding
-closing parenthesis.  (Likewise for brackets, etc.)."
+closing parenthesis.  (Likewise for brackets, etc.). To toggle
+the mode in a single buffer, use `electric-pair-local-mode'."
   :global t :group 'electricity
   (if electric-pair-mode
       (progn
@@ -572,6 +573,19 @@ closing parenthesis.  (Likewise for brackets, etc.)."
     (remove-hook 'self-insert-uses-region-functions
                  #'electric-pair-will-use-region)))
 
+;;;###autoload
+(define-minor-mode electric-pair-local-mode
+  "Toggle `electric-pair-mode' only in this buffer."
+  :variable (buffer-local-value 'electric-pair-mode (current-buffer))
+  (cond
+   ((eq electric-pair-mode (default-value 'electric-pair-mode))
+    (kill-local-variable 'electric-pair-mode))
+   ((not (default-value 'electric-pair-mode))
+    ;; Locally enabled, but globally disabled.
+    (electric-pair-mode 1)               ; Setup the hooks.
+    (setq-default electric-pair-mode nil) ; But keep it globally disabled.
+    )))
+
 (provide 'elec-pair)
 
 ;;; elec-pair.el ends here