                       (pair (and syntax-entry
                                  (cdr syntax-entry))))
                  (cond ((eq class (car (string-to-syntax "(")))
+                        ;; syntax classes "opening parens" and "close parens"
                         (define-key map (string char) 'autopair-insert-opening)
                         (define-key map (string pair) 'autopair-skip-close-maybe))
-                       ((or (eq class (car (string-to-syntax "\"")))
-                            (eq class (car (string-to-syntax "$"))))
-                        (define-key map (string char) 'autopair-insert-or-skip-quote))))))
+                       ((eq class (car (string-to-syntax "\"")))
+                        ;; syntax class "string quote
+                        (define-key map (string char) 'autopair-insert-or-skip-quote))
+                       ((eq class (car (string-to-syntax "$")))
+                        ;; syntax class "paired-delimiter" 
+                        ;;
+                        ;; Apropos this class, see Issues 18, 25 and
+                        ;; elisp info node "35.2.1 Table of Syntax
+                        ;; Classes". The fact that it supresses
+                        ;; syntatic properties in the delimited region
+                        ;; dictates that deciding to autopair/autoskip
+                        ;; can't really be as clean as the string
+                        ;; delimiter.
+                        ;;
+                        ;; Apparently, only `TeX-mode' uses this, so
+                        ;; the best is to bind this to
+                        ;; `autopair-insert-or-skip-paired-delimiter'
+                        ;; which defers any decision making to
+                        ;; mode-specific post-command handler
+                        ;; functions.
+                        ;;
+                        (define-key map (string char) 'autopair-insert-or-skip-paired-delimiter))))))
            ;; read `autopair-extra-pairs'
            (dolist (pairs-list (remove-if-not #'listp autopair-extra-pairs))
              (dolist (pair pairs-list)


(defun autopair-newline ()
  (interactive)
  (let ((pair (autopair-find-pair (char-before))))
    (when (eq (char-after) pair)
      (setq autopair-action (list 'newline pair (point))))
    (autopair-fallback (kbd "RET"))))
(put 'autopair-newline 'function-documentation
     '(concat "Possibly insert two newlines and place point after the first, indented.\n\n"
              (autopair-document-bindings (kbd "RET"))))

(defun autopair-skip-p ()
  (interactive)
  (let* ((syntax-triplet (autopair-syntax-ppss))
         (syntax-info (first syntax-triplet))
         (orig-point (point)))
    (cond ((eq autopair-skip-criteria 'help-balance)
           (save-excursion
             (let ((pos-pair (autopair-up-list syntax-info last-input-event)))
               ;; if `autopair-up-list' returned something valid, we
               ;; probably want to skip but only if on of the following is true.
               ;;

        (blink-matching-delay autopair-blink-delay))
    (blink-matching-open)))

(defun autopair-blink (&optional pos)
  (when autopair-blink
    (if pos
        (save-excursion
          (goto-char pos)
          (sit-for autopair-blink-delay))
      (sit-for autopair-blink-delay))))

(defun autopair-default-handle-action (action pair pos-before)
  ;;(message "action is %s" action)
  (condition-case err
      (cond (;; automatically insert closing delimiter
             (and (eq 'opening action)
                  (not (eq pair (char-before))))
             (insert pair)
             (autopair-blink)
             (backward-char 1))
            (;; automatically insert closing quote delimiter
             (eq 'insert-quote action)
             (insert pair)
             (autopair-blink)
             (backward-char 1))
            (;; automatically skip oper closer quote delimiter
             (and (eq 'skip-quote action)
                  (eq pair (char-after (point))))
             (delete-char 1)
             (autopair-blink-matching-open))
            (;; skip over newly-inserted-but-existing closing delimiter
             ;; (normal case)
             (eq 'closing action)
             (let ((skipped 0))
               (when autopair-skip-whitespace
                 (setq skipped (save-excursion (skip-chars-forward "\s\n\t"))))
               (when (eq autopair-inserted (char-after (+ (point) skipped)))
                 (backward-delete-char 1)
                 (unless (zerop skipped) (autopair-blink (+ (point) skipped)))
                 (if (eq autopair-skip-whitespace 'chomp)
                     (delete-char skipped)
                   (forward-char skipped))
                 (forward-char))
                 (autopair-blink-matching-open)))
            (;; autodelete closing delimiter
             (and (eq 'backspace action)
                  (eq pair (char-after (point))))
             (delete-char 1))
            (;; opens an extra line after point, then indents
             (and (eq 'newline action)
                  (eq pair (char-after (point))))
             (save-excursion
               (newline-and-indent))
             (indent-according-to-mode)
             (when (or (and (boundp 'global-hl-line-mode)
                            global-hl-line-mode)
                       (and (boundp 'hl-line-mode)
                            hl-line-mode))
               (hl-line-unhighlight) (hl-line-highlight))))
    (error
     (message "[autopair] Ignored error in `autopair-default-handle-action'"))))

(defun autopair-default-handle-wrap-action (action pair pos-before region-before)
  "Default handler for the wrapping action in `autopair-wrap'"
  (condition-case err
      (when (eq 'wrap action)
        (let ((delete-active-region nil))
          (cond
           ((eq 'opening (first autopair-action))
            (goto-char (1+ (cdr region-before)))
            (insert pair)
            (autopair-blink)
            (goto-char (1+ (car region-before))))
           (;; wraps
            (eq 'closing (first autopair-action))
            (delete-backward-char 1)
            (insert pair)
            (goto-char (1+ (cdr region-before)))
            (insert autopair-inserted))
           ((eq 'insert-quote (first autopair-action))
            (goto-char (1+ (cdr region-before)))
            (insert pair)
            (autopair-blink))
           (t
            (delete-backward-char 1)
            (goto-char (cdr region-before))
            (insert autopair-inserted)))
          (setq autopair-action nil)))
    (error
     (message "[autopair] Ignored error in `autopair-default-handle-wrap-action'"))))


;; example python triple quote helper
