]> code.delx.au - gnu-emacs/blobdiff - lisp/elec-pair.el
Wrap around error in coreutil's ls
[gnu-emacs] / lisp / elec-pair.el
index 73eabdf51c86ad8cd2e0f241afb6c9dca46cf039..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>
 
@@ -166,36 +166,6 @@ return value is considered instead."
 quotes or comments.  If lookup fails here, `electric-pair-text-pairs' will
 be considered.")
 
-(defun electric-pair-backward-delete-char (n &optional killflag untabify)
-  "Delete characters backward, and maybe also two adjacent paired delimiters.
-
-Remaining behavior is given by `backward-delete-char' or, if UNTABIFY is
-non-nil, `backward-delete-char-untabify'."
-  (interactive "*p\nP")
-  (let* ((prev (char-before))
-         (next (char-after))
-         (syntax-info (and prev
-                           (electric-pair-syntax-info prev)))
-         (syntax (car syntax-info))
-         (pair (cadr syntax-info)))
-    (when (and next pair
-               (if (functionp electric-pair-delete-adjacent-pairs)
-                   (funcall electric-pair-delete-adjacent-pairs)
-                 electric-pair-delete-adjacent-pairs)
-               (memq syntax '(?\( ?\" ?\$))
-               (eq pair next))
-      (delete-char 1 killflag))
-    (if untabify
-        (backward-delete-char-untabify n killflag)
-        (backward-delete-char n killflag))))
-
-(defun electric-pair-backward-delete-char-untabify (n &optional killflag)
-  "Delete characters backward, and maybe also two adjacent paired delimiters.
-
-Remaining behavior is given by `backward-delete-char-untabify'."
-  (interactive "*p\nP")
-  (electric-pair-backward-delete-char n killflag t))
-
 (defun electric-pair-conservative-inhibit (char)
   (or
    ;; I find it more often preferable not to pair when the
@@ -256,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
@@ -273,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
@@ -373,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
@@ -503,10 +472,13 @@ happened."
                            (funcall electric-pair-skip-self last-command-event)
                          electric-pair-skip-self))
                    (save-excursion
-                     (when (setq skip-whitespace-info
-                                 (if (functionp electric-pair-skip-whitespace)
-                                     (funcall electric-pair-skip-whitespace)
-                                   electric-pair-skip-whitespace))
+                     (when (and (not (and unconditional
+                                          (eq syntax ?\")))
+                                (setq skip-whitespace-info
+                                      (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))
                      (eq (char-after) last-command-event))))
          ;; This is too late: rather than insert&delete we'd want to only
@@ -527,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)
@@ -546,14 +518,34 @@ happened."
        (memq (car (electric-pair-syntax-info last-command-event))
              '(?\( ?\) ?\" ?\$))))
 
+(defun electric-pair-delete-pair (arg &optional killp)
+  "When between adjacent paired delimiters, delete both of them.
+ARG and KILLP are passed directly to
+`backward-delete-char-untabify', which see."
+  (interactive "*p\nP")
+  (delete-char 1)
+  (backward-delete-char-untabify arg killp))
+
 (defvar electric-pair-mode-map
   (let ((map (make-sparse-keymap)))
-    (define-key map [remap backward-delete-char-untabify]
-      'electric-pair-backward-delete-char-untabify)
-    (define-key map [remap backward-delete-char]
-      'electric-pair-backward-delete-char)
-    (define-key map [remap delete-backward-char]
-      'electric-pair-backward-delete-char)
+    (define-key map "\177"
+      `(menu-item
+        "" electric-pair-delete-pair
+        :filter
+        ,(lambda (cmd)
+           (let* ((prev (char-before))
+                  (next (char-after))
+                  (syntax-info (and prev
+                                    (electric-pair-syntax-info prev)))
+                  (syntax (car syntax-info))
+                  (pair (cadr syntax-info)))
+             (and next pair
+                  (memq syntax '(?\( ?\" ?\$))
+                  (eq pair next)
+                  (if (functionp electric-pair-delete-adjacent-pairs)
+                      (funcall electric-pair-delete-adjacent-pairs)
+                    electric-pair-delete-adjacent-pairs)
+                  cmd)))))
     map)
   "Keymap used by `electric-pair-mode'.")
 
@@ -566,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
@@ -580,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