Commits

Mike Steder  committed 49e3473

Adding escreen, yasnippets, autopair (with working triple quoting).

  • Participants
  • Parent commits 506dd12

Comments (0)

Files changed (419)

 (add-to-list 'load-path "~/etc/emacs.d")
 (add-to-list 'load-path "~/etc/emacs.d/color-theme-6.6.0")
 (add-to-list 'load-path "~/etc/emacs.d/CsharpToolsForEmacs")
+(add-to-list 'load-path "~/etc/emacs.d/sellout-emacs-color-theme-solarized")
 
 ;; Emacs config menus will write customizations
 ;; to this file:
 (load "misc.el")
 ;(load "menu.el")
 (load "remote.el")
+(load "snippets.el")
+(load "escreen.el")
+(escreen-install)
 
 (load "csharp-mode.el")
 
 ;(require 'sr-speedbar)
 
 ; Autopair: pair's braces automatically (like textmate)
-;(require 'autopair)
-;(autopair-global-mode)
+(load "msautopair.el")
 
 ; terminal stuff:
 (load "msterm.el")

File emacs.d/autopair.el

 ;;; autopair.el --- Automagically pair braces and quotes like TextMate
 
-;; Copyright (C) 2009 Joao Tavora
+;; Copyright (C) 2009,2010 Joao Tavora
 
 ;; Author: Joao Tavora <joaotavora [at] gmail.com>
 ;; Keywords: convenience, emulations
 ;; X-URL: http://autopair.googlecode.com
 ;; URL: http://autopair.googlecode.com
 ;; EmacsWiki: AutoPairs
-;; Version: 0.3
+;; Version: 0.4
 ;; Revision: $Rev$ ($LastChangedDate$)
 
 ;; This program is free software; you can redistribute it and/or modify
 ;; If you find the paren-blinking annoying, turn `autopair-blink' to
 ;; nil.
 ;;
+;; For lisp-programming you might also like `autopair-skip-whitespace'.
+;;
 ;; For further customization have a look at `autopair-dont-pair',
-;; `autopair-handle-action-fns' and `autopair-extra-pair'.
+;; `autopair-handle-action-fns' and `autopair-extra-pairs'.
 ;;
 ;; `autopair-dont-pair' lets you define special cases of characters
 ;; you don't want paired.  Its default value skips pairing
 ;;                     (list #'autopair-default-handle-action
 ;;                           #'autopair-python-triple-quote-action))))
 ;;
+;; It's also useful to deal with latex's mode use of the "paired
+;; delimiter" syntax class.
+;;
+;; (add-hook 'latex-mode-hook
+;;           #'(lambda ()
+;;               (set (make-local-variable 'autopair-handle-action-fns)
+;;                    (list #'autopair-default-handle-action
+;;                          #'autopair-latex-mode-paired-delimiter-action))))
+;;
 ;; `autopair-extra-pairs' lets you define extra pairing and skipping
 ;; behaviour for pairs not programmed into the syntax table. Watch
 ;; out, this is work-in-progress, a little unstable and does not help
 ;;; Bugs:
 ;;
 ;; * Quote pairing/skipping inside comments is not perfect...
-;; * Autopair is perfectly compatible with `delete-selection-mode'. To
-;;   do so, add the following to your .emacs.
-;; 
-;;   (put 'autopair-insert-opening 'delete-selection t)
-;;   (put 'autopair-skip-close-maybe 'delete-selection t)
-;;   (put 'autopair-insert-or-skip-quote 'delete-selection t)
-;;   (put 'autopair-extra-insert-opening 'delete-selection t)
-;;   (put 'autopair-extra-skip-close-maybe 'delete-selection t)
-;;   (put 'autopair-backspace 'delete-selection 'supersede)
-;;   (put 'autopair-newline 'delete-selection t)
 ;;
-;;  you will, however, lose the `autopair-autowrap'
-;;  behaviour. Autopair is compatible with `cua-mode' though, which
-;;  also provides the same behaviour.
+;; * See the last section on monkey-patching for the `defadvice'
+;;   tricks used to make `autopair-autowrap' work with `cua-mode' and
+;;   `delete-selection-mode'.
 ;;
 ;;; Credit:
 ;;
 
 ;; variables
 (defvar autopair-pair-criteria 'help-balance
-  "If non-nil, be more criterious when pairing opening brackets.")
+  "How to decide whether to pair opening brackets or quotes.
+
+Set this to 'always to always pair, or 'help-balance to be more
+criterious when pairing.")
 
 (defvar autopair-skip-criteria 'help-balance
-  "If non-nil, be more criterious when skipping closing brackets.")
+  "How to decide whether to skip closing brackets or quotes.
+
+Set this to 'always to always skip, or 'help-balance to be more
+criterious when skipping.")
 
 (defvar autopair-emulation-alist nil
   "A dinamic keymap for autopair set mostly from the current
 
 
 Note that this does *not* work for single characters,
-e.x. characters you want to behave as quotes.  To have quote-like
-behaviour consider using something else, for example:
+e.x. characters you want to behave as quotes.  See the
+docs/source comments for more details.")
 
-(add-hook 'latex-mode-hook
-          #'(lambda ()
-              (modify-syntax-entry ?$ \"\\\"\")))
-
-")
 (make-variable-buffer-local 'autopair-extra-pairs)
 
 (defvar autopair-dont-pair `(:string (?') :comment  (?'))
 three elements are (ACTION PAIR POS-BEFORE).
 
 ACTION is one of `opening', `insert-quote', `skip-quote',
-`backspace' or `newline'. PAIR is the pair of `last-input-event'
-delimiter. POS-BEFORE is value of point before action command
-took place .")
+`backspace', `newline' or `paired-delimiter'. PAIR is the pair of
+the `autopair-inserted' character, if applicable. POS-BEFORE is
+value of point before action command took place .")
 
 
 (defvar autopair-wrap-action nil
 to specify special behaviour. To also run the default behaviour,
 be sure to include `autopair-default-handle-action' in the
 list, or call it from your handlers.")
+(make-variable-buffer-local 'autopair-handle-action-fns)
 
 (defvar autopair-handle-wrap-action-fns '()
   "Autopair wrap handlers to run *instead* of the default handler.
 variable to specify special behaviour. To also run the default
 behaviour, be sure to include `autopair-default-handle-wrap-action' in
 the list, or call it in your handlers.")
+(make-variable-buffer-local 'autopair-handle-wrap-action-fns)
+
+(defvar autopair-inserted nil
+  "Delimiter inserted by last interactive autopair command.
+
+This is calculated with `autopair-calculate-inserted', which see.")
+
+(defun autopair-calculate-inserted ()
+  "Attempts to guess the delimiter the current command is inserting.
+
+For now, simply returns `last-command-event'"
+  last-command-event)
 
 ;; minor mode and global mode
 ;;
 (define-globalized-minor-mode autopair-global-mode autopair-mode autopair-on)
 
-(defun autopair-on () (unless autopair-dont-activate (autopair-mode 1)))
+(defun autopair-on () (unless (or buffer-read-only autopair-dont-activate) (autopair-mode 1)))
 
 (define-minor-mode autopair-mode
   "Automagically pair braces and quotes like in TextMate."
            (define-key map (kbd "<backspace>") 'autopair-backspace)
            (define-key map [backspace] 'autopair-backspace)
            (define-key map (kbd "DEL") 'autopair-backspace)
+           (define-key map [return] 'autopair-newline)
            (define-key map (kbd "RET") 'autopair-newline)
            (dotimes (char 256) ;; only searches the first 256 chars,
-                               ;; TODO: is this enough/toomuch/stupid?
+             ;; TODO: is this enough/toomuch/stupid?
              (unless (member char
                              (getf autopair-dont-pair :never))
                (let* ((syntax-entry (aref (syntax-table) char))
                       (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)
          (setq autopair-action nil)
          (setq autopair-wrap-action nil)
          (add-hook 'emulation-mode-map-alists 'autopair-emulation-alist 'append)
-         (add-hook 'post-command-hook 'autopair-post-command-handler 'append 'local))
+         (add-hook 'post-command-hook 'autopair-post-command-handler nil 'local))
         (t
          (setq autopair-emulation-alist nil)
          (remove-hook 'emulation-mode-map-alists 'autopair-emulation-alist)
                  :code
                  quick-syntax-info)))))
 
-(defun autopair-find-pair (delim)
-  (when delim
+(defun autopair-find-pair (delim &optional closing)
+  (when (and delim
+             (integerp delim))
     (let ((syntax-entry (aref (syntax-table) delim)))
       (cond ((eq (syntax-class syntax-entry) (car (string-to-syntax "(")))
              (cdr syntax-entry))
-            ((eq (syntax-class syntax-entry) (car (string-to-syntax "\"")))
+            ((or (eq (syntax-class syntax-entry) (car (string-to-syntax "\"")))
+                 (eq (syntax-class syntax-entry) (car (string-to-syntax "$"))))
              delim)
-            ((eq (syntax-class syntax-entry) (car (string-to-syntax ")")))
+            ((and (not closing)
+                  (eq (syntax-class syntax-entry) (car (string-to-syntax ")"))))
              (cdr syntax-entry))
             (autopair-extra-pairs
              (some #'(lambda (pair-list)
                              pair-list))
                    (remove-if-not #'listp autopair-extra-pairs)))))))
 
-(defun autopair-set-wrapping-action ()
-  (when mark-active
+(defun autopair-calculate-wrap-action ()
+  (when (and transient-mark-mode mark-active)
+    (when (> (point) (mark))
+      (exchange-point-and-mark))
     (save-excursion
       (let* ((region-before (cons (region-beginning)
                                   (region-end)))
              (point-before (point))
              (start-syntax (syntax-ppss (car region-before)))
              (end-syntax   (syntax-ppss (cdr region-before))))
-        (when (and (eq (nth 0 start-syntax) (nth 0 end-syntax))
-                   (eq (nth 3 start-syntax) (nth 3 end-syntax)))
-          (setq autopair-wrap-action (list 'wrap (or (second autopair-action)
-                                                     (autopair-find-pair last-input-event))
-                                           point-before
-                                           region-before)))))))
+        (when (or (not (eq autopair-autowrap 'help-balance))
+                  (and (eq (nth 0 start-syntax) (nth 0 end-syntax))
+                       (eq (nth 3 start-syntax) (nth 3 end-syntax))))
+          (list 'wrap (or (second autopair-action)
+                          (autopair-find-pair autopair-inserted))
+                point-before
+                region-before))))))
+
+(defun autopair-original-binding ()
+  (or (key-binding `[,autopair-inserted])
+      (key-binding (this-single-command-keys))
+      (key-binding fallback-keys)))
 
 (defun autopair-fallback (&optional fallback-keys)
   (let* ((autopair-emulation-alist nil)
          (beyond-cua (let ((cua--keymap-alist nil))
-                       (or (key-binding (this-single-command-keys))
-                           (key-binding fallback-keys))))
-         (beyond-autopair (or (key-binding (this-single-command-keys))
-                              (key-binding fallback-keys))))
+                       (autopair-original-binding)))
+         (beyond-autopair (autopair-original-binding)))
     (when autopair-autowrap
-      (autopair-set-wrapping-action))
+      (setq autopair-wrap-action (autopair-calculate-wrap-action)))
     
     (setq this-original-command beyond-cua)
     ;; defer to "paredit-mode" if that is installed and running
           (blink-matching-paren (not autopair-action)))
       (call-interactively beyond-autopair))))
 
-(defvar autopair-autowrap nil
+(defvar autopair-autowrap 'help-balance
   "If non-nil autopair attempts to wrap the selected region.
 
-This is also done in an optimistic \"try-to-balance\" fashion.")
+This is also done in an optimistic \"try-to-balance\" fashion.
+Set this to to 'help-balance to be more criterious when wrapping.")
+
+(defvar autopair-skip-whitespace nil
+  "If non-nil also skip over whitespace when skipping closing delimiters.
+
+If set to 'chomp, this will be most useful in lisp-like languages where you want
+lots of )))))....")
 
 (defvar autopair-blink (if (boundp 'blink-matching-paren)
                            blink-matching-paren
 (defun autopair-exception-p (where-sym exception-where-sym blacklist &optional fn)
   (and (or (eq exception-where-sym :everywhere)
            (eq exception-where-sym where-sym))
-       (member last-input-event
+       (member autopair-inserted
                (if fn
                    (mapcar fn (getf blacklist exception-where-sym))
                  (getf blacklist exception-where-sym)))))
       (let ((howmany (car syntax-info))
             (retval (cons (point)
                           (point))))
-        (while (and (/= howmany 0)
+        (while (and (> howmany 0)
                     (condition-case err
                         (progn
                           (scan-sexps (point) (- (point-max)))
 ;; 
 (defun autopair-insert-or-skip-quote ()
   (interactive)
+  (setq autopair-inserted (autopair-calculate-inserted))
   (let* ((syntax-triplet (autopair-syntax-ppss))
          (syntax-info (first syntax-triplet))
          (where-sym (second syntax-triplet))
     (cond (;; decides whether to skip the quote...
            ;;
            (and (not escaped-p)
-                (eq last-input-event (char-after (point)))
+                (eq autopair-inserted (char-after (point)))
                 (or
                  ;; ... if we're already inside a string and the
                  ;; string starts with the character just inserted,
                  ;; or it's a generically terminated string
                  (and inside-string
                       (or (eq inside-string t)
-                          (eq last-input-event inside-string)))
+                          (eq autopair-inserted inside-string)))
                  ;; ... if we're in a comment and ending a string
                  ;; (the inside-string criteria does not work
                  ;; here...)
                  (and (eq where-sym :comment)
                       (condition-case nil
-                          (eq last-input-event (char-after (scan-sexps (1+ (point)) -1)))
+                          (eq autopair-inserted (char-after (scan-sexps (1+ (point)) -1)))
                         (error nil)))))
-           (setq autopair-action (list 'skip-quote last-input-event (point))))
+           (setq autopair-action (list 'skip-quote autopair-inserted (point))))
           (;; decides whether to pair, i.e do *not* pair the quote if...
            ;;
            (not
              ;; ... comment-disable or string-disable are true here.
              ;; The latter is only useful if we're in a string
              ;; terminated by a character other than
-             ;; `last-input-event'.
+             ;; `autopair-inserted'.
              (some #'(lambda (sym)
                        (autopair-exception-p where-sym sym autopair-dont-pair))
                    '(:comment :string))))
-           (setq autopair-action (list 'insert-quote last-input-event (point)))))
+           (setq autopair-action (list 'insert-quote autopair-inserted (point)))))
     (autopair-fallback)))
 
-  (put 'autopair-insert-or-skip-quote 'function-documentation
+(put 'autopair-insert-or-skip-quote 'function-documentation
      '(concat "Insert or possibly skip over a quoting character.\n\n"
               (autopair-document-bindings)))
 
 (defun autopair-in-unterminated-string-p (autopair-triplet)
-  (and (eq last-input-event (fourth (third autopair-triplet)))
+  (and (eq autopair-inserted (fourth (third autopair-triplet)))
        (condition-case nil (progn (scan-sexps (ninth (third autopair-triplet)) 1) nil) (error t))))     
 
 
 (defun autopair-insert-opening ()
   (interactive)
+  (setq autopair-inserted (autopair-calculate-inserted))
   (when (autopair-pair-p)
-    (setq autopair-action (list 'opening (autopair-find-pair last-input-event) (point))))
+    (setq autopair-action (list 'opening (autopair-find-pair autopair-inserted) (point))))
   (autopair-fallback))
 (put 'autopair-insert-opening 'function-documentation
      '(concat "Insert opening delimiter and possibly automatically close it.\n\n"
 
 (defun autopair-skip-close-maybe ()
   (interactive)
+  (setq autopair-inserted (autopair-calculate-inserted))
   (when (autopair-skip-p)
-    (setq autopair-action (list 'closing (autopair-find-pair last-input-event) (point))))
+    (setq autopair-action (list 'closing (autopair-find-pair autopair-inserted) (point))))
   (autopair-fallback))
 (put 'autopair-skip-close-maybe 'function-documentation
      '(concat "Insert or possibly skip over a closing delimiter.\n\n"
-               (autopair-document-bindings)))
+              (autopair-document-bindings)))
 
 (defun autopair-backspace ()
   (interactive)
-    (when (char-before)
-      (setq autopair-action (list 'backspace (autopair-find-pair (char-before)) (point))))
+  (setq autopair-inserted (autopair-calculate-inserted))
+  (when (char-before)
+    (setq autopair-action (list 'backspace (autopair-find-pair (char-before) 'closing) (point))))
   (autopair-fallback (kbd "DEL")))
 (put 'autopair-backspace 'function-documentation
      '(concat "Possibly delete a pair of paired delimiters.\n\n"
 
 (defun autopair-newline ()
   (interactive)
+  (setq autopair-inserted (autopair-calculate-inserted))
   (let ((pair (autopair-find-pair (char-before))))
-    (when (eq (char-after) pair)
+    (when (and pair
+               (eq (char-syntax pair) ?\))
+               (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"
+     '(concat "Do a smart newline when right between parenthesis.\n
+In other words, insert an extra newline along with the one inserted normally
+by this command. Then 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)))
+             (let ((pos-pair (autopair-up-list syntax-info autopair-inserted)))
                ;; if `autopair-up-list' returned something valid, we
                ;; probably want to skip but only if on of the following is true.
                ;;
                     (or (eq (car pos-pair) (cdr pos-pair))
                         (< orig-point (cdr pos-pair))
                         (eq (char-after (car pos-pair))
-                            (autopair-find-pair last-input-event)))))))
+                            (autopair-find-pair autopair-inserted)))))))
           ((eq autopair-skip-criteria 'need-opening)
            (save-excursion
              (condition-case err
                      (save-excursion
                        (let ((pos-pair (autopair-up-list syntax-info))
                              (prev-point (point-max))
-                             (expected-closing (autopair-find-pair last-input-event)))
+                             (expected-closing (autopair-find-pair autopair-inserted)))
                          (condition-case err
                              (progn
                                (while (not (eq prev-point (point)))
                                    ;; autopair if we're in a mixed parens situation,
                                    ;; i.e. the last list jumped over was started by
                                    ;; the paren we're trying to match
-                                   ;; (`last-input-event') and ended by a different
+                                   ;; (`autopair-inserted') and ended by a different
                                    ;; parens, or the closing paren we stopped at is
                                    ;; also different from the expected. The second
                                    ;; `scan-lists' places point at the closing of the
                                    (condition-case err
                                        (prog1
                                            (eq (char-after (scan-lists (point) -1 0))
-                                               last-input-event)
+                                               autopair-inserted)
                                          (goto-char (scan-lists (point) -1 -1)))
                                      (error t))
                                    
 `autopair-wrap-action'. "
   (when (and autopair-wrap-action
              (notany #'null autopair-wrap-action))
-    (condition-case err
-        (if autopair-handle-wrap-action-fns
+    
+    (if autopair-handle-wrap-action-fns
+        (condition-case err
             (mapc #'(lambda (fn)
                       (apply fn autopair-wrap-action))
                   autopair-handle-wrap-action-fns)
-          (apply #'autopair-default-handle-wrap-action autopair-wrap-action))
-      (error (progn
-               (message "[autopair] error running `autopair-handle-wrap-action-fns', switching autopair off")
-               (autopair-mode -1))))
+          (error (progn
+                   (message "[autopair] error running custom `autopair-handle-wrap-action-fns', switching autopair off")
+                   (autopair-mode -1))))
+      (apply #'autopair-default-handle-wrap-action autopair-wrap-action))
     (setq autopair-wrap-action nil))
   
   (when (and autopair-action
              (notany #'null autopair-action))
-    (condition-case err
-        (if autopair-handle-action-fns
+    (if autopair-handle-action-fns
+        (condition-case err
             (mapc #'(lambda (fn)
                       (funcall fn (first autopair-action) (second autopair-action) (third autopair-action)))
                   autopair-handle-action-fns)
-          (apply #'autopair-default-handle-action autopair-action))
-      (error (progn
-               (message "[autopair] error running `autopair-handle-action-fns', switching autopair off")
-               (autopair-mode -1))))
+          (error (progn
+                   (message "[autopair] error running custom `autopair-handle-action-fns', switching autopair off")
+                   (autopair-mode -1))))
+      (apply #'autopair-default-handle-action autopair-action))
     (setq autopair-action nil)))
 
 (defun autopair-blink-matching-open ()
         (blink-matching-delay autopair-blink-delay))
     (blink-matching-open)))
 
-(defun autopair-blink ()
+(defun autopair-blink (&optional pos)
   (when autopair-blink
-    (sit-for autopair-blink-delay)))
+    (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)
-  (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)
-         (and (eq 'closing action)
-              (eq last-input-event (char-after (point))))
-         (delete-char 1)
-         (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)))))
+  (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'"
-  (when (eq 'wrap action)
-    (let ((reverse-selected (= (car region-before) pos-before)))
-      (cond
-       ((eq 'opening (first autopair-action))
-        ;; (message "wrap-opening!")
-        (cond (reverse-selected
-               (goto-char (1+ (cdr region-before)))
-               (insert pair)
-               (goto-char (1+ (car region-before))))
-              (t
-               (delete-backward-char 1)
-               (insert pair)
-               (goto-char (car region-before))
-               (insert last-input-event)))
-        (setq autopair-action nil) )
-       (;; wraps
-        (eq 'closing (first autopair-action))
-        ;; (message "wrap-closing!")
-        (cond (reverse-selected
-               (delete-backward-char 1)
-               (insert pair)
-               (goto-char (1+ (cdr region-before)))
-               (insert last-input-event))
-              (t
-               (goto-char (car region-before))
-               (insert pair)
-               (goto-char (+ 2 (cdr region-before)))))
-        (setq autopair-action nil))
-       ((eq 'insert-quote (first autopair-action))
-        (cond (reverse-selected
-               (goto-char (1+ (cdr region-before)))
-               (insert pair))
-              (t
-               (goto-char (car region-before))
-               (insert last-input-event)))
-        (setq autopair-action nil))
-       (reverse-selected
-        (delete-backward-char 1)
-        (goto-char (cdr region-before))
-        (insert last-input-event))))))
+  (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
         (t
          t)))
 
-;; Commands, predicates and tests for the autopair-extra* feature
+;; example latex paired-delimiter helper 
+;;
+(defun autopair-latex-mode-paired-delimiter-action (action pair pos-before)
+  "Pair or skip latex's \"paired delimiter\" syntax in math mode. Added AucText support, thanks Massimo Lauria"
+  (when (eq action 'paired-delimiter)
+    (when (eq (char-before) pair)
+      (if (and (or
+                (eq (get-text-property pos-before 'face) 'tex-math)
+                (eq (get-text-property (- pos-before 1) 'face) 'font-latex-math-face)
+                (member 'font-latex-math-face (get-text-property (- pos-before 1) 'face)))
+               (eq (char-after) pair))
+          (cond ((and (eq (char-after) pair)
+                      (eq (char-after (1+ (point))) pair))
+                 ;; double skip
+                 (delete-char 1)
+                 (forward-char))
+                ((eq (char-before pos-before) pair)
+                 ;; doube insert
+                 (insert pair)
+                 (backward-char))
+                (t
+                 ;; simple skip
+                 (delete-char 1)))
+        (insert pair)
+        (backward-char)))))
+
+;; Commands and predicates for the autopair-extra* feature 
 ;;
 
 (defun autopair-extra-insert-opening ()
   (interactive)
+  (setq autopair-inserted (autopair-calculate-inserted))
   (when (autopair-extra-pair-p)
-    (setq autopair-action (list 'opening (autopair-find-pair last-input-event) (point))))
+    (setq autopair-action (list 'opening (autopair-find-pair autopair-inserted) (point))))
   (autopair-fallback))
 (put 'autopair-extra-insert-opening 'function-documentation
      '(concat "Insert (an extra) opening delimiter and possibly automatically close it.\n\n"
 
 (defun autopair-extra-skip-close-maybe ()
   (interactive)
+  (setq autopair-inserted (autopair-calculate-inserted))
   (when (autopair-extra-skip-p)
-    (setq autopair-action (list 'closing last-input-event (point))))
+    (setq autopair-action (list 'closing autopair-inserted (point))))
   (autopair-fallback))
 (put 'autopair-extra-skip-close-maybe 'function-documentation
      '(concat "Insert or possibly skip over a (and extra) closing delimiter.\n\n"
          (syntax-info (first syntax-triplet))
          (where-sym (second syntax-triplet))
          (orig-point (point)))
-    (and (eq (char-after (point)) last-input-event)
+    (and (eq (char-after (point)) autopair-inserted)
          (some #'(lambda (sym)
                    (autopair-exception-p where-sym sym autopair-extra-pairs #'cdr))
                '(:comment :string :code :everywhere))
                (backward-sexp (point-max))
              (error
               (goto-char (third err))))
-           (search-forward (make-string 1 (autopair-find-pair last-input-event))
+           (search-forward (make-string 1 (autopair-find-pair autopair-inserted))
                            orig-point
                            'noerror)))))
 
+;; Commands and tex-mode specific handler functions for the "paired
+;; delimiter" syntax class.
+;; 
+(defun autopair-insert-or-skip-paired-delimiter ()
+  " insert or skip a character paired delimiter"
+  (interactive)
+  (setq autopair-inserted (autopair-calculate-inserted))
+  (setq autopair-action (list 'paired-delimiter autopair-inserted (point)))
+  (autopair-fallback))
+
+(put 'autopair-insert-or-skip-paired-delimiter 'function-documentation
+     '(concat "Insert or possibly skip over a character with a syntax-class of \"paired delimiter\"."
+              (autopair-document-bindings)))
+
+
+
+;; monkey-patching: Compatibility with delete-selection-mode and cua-mode
+;;
+;; Ideally one would be able to use functions as the value of the
+;; 'delete-selection properties of the autopair commands. The function
+;; would return non-nil when no wrapping should/could be performed.
+;;
+;; Until then use some `defadvice' i.e. monkey-patching, which relies
+;; on these features' implementation details.
+;;
+(put 'autopair-insert-opening 'delete-selection t)
+(put 'autopair-skip-close-maybe 'delete-selection t)
+(put 'autopair-insert-or-skip-quote 'delete-selection t)
+(put 'autopair-extra-insert-opening 'delete-selection t)
+(put 'autopair-extra-skip-close-maybe 'delete-selection t)
+(put 'autopair-backspace 'delete-selection 'supersede)
+(put 'autopair-newline 'delete-selection t)
+
+(defun autopair-should-autowrap ()
+  (let ((name (symbol-name this-command)))
+    (and autopair-mode
+         (not (eq this-command 'autopair-backspace))
+         (string-match "^autopair" (symbol-name this-command))
+         (autopair-calculate-wrap-action))))
+
+(defadvice cua--pre-command-handler-1 (around autopair-override activate)
+  "Don't actually do anything if autopair is about to autowrap. "
+  (unless (autopair-should-autowrap) ad-do-it))
+
+(defadvice delete-selection-pre-hook (around autopair-override activate)
+  "Don't actually do anything if autopair is about to autowrap. "
+  (unless (autopair-should-autowrap) ad-do-it))
+
+
 (provide 'autopair)
 ;;; autopair.el ends here
 ;;

File emacs.d/custom.el

   ;; If you edit it by hand, you could mess it up, so be careful.
   ;; Your init file should contain only one such instance.
   ;; If there is more than one, they won't work right.
- '(column-number-mode t)
- '(display-time-mode t)
- '(show-paren-mode t)
- '(size-indication-mode t))
+ )
 
 (custom-set-faces
   ;; custom-set-faces was added by Custom.
   ;; If you edit it by hand, you could mess it up, so be careful.
   ;; Your init file should contain only one such instance.
   ;; If there is more than one, they won't work right.
- '(default ((t (:inherit nil :stipple nil :background "black" :foreground "#eeeeec" :inverse-video nil :box nil :strike-through nil :overline nil :underline nil :slant normal :weight normal :height 98 :width normal :foundry "unknown" :family "Inconsolata-dz")))))
+ '(default ((t (:inherit nil :stipple nil :background "black" :foreground "#eeeeec" :inverse-video nil :box nil :strike-through nil :overline nil :underline nil :slant normal :weight normal :height 98 :width medium :foundry "outline" :family "Courier New")))))
 

File emacs.d/escreen.el

+;;; escreen.el --- emacs window session manager
+
+;;; Copyright (C) 1992, 94, 95, 97, 2001, 2005 Noah S. Friedman
+
+;;; Author: Noah Friedman <friedman@splode.com>
+;;; Maintainer: friedman@splode.com
+;;; Keywords: extensions
+;;; Created: 1992-03-23
+
+;;; $Id: escreen.el,v 1.18 2005/05/23 09:47:13 friedman Exp $
+
+;; This program is free software; you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation; either version 2, or (at your option)
+;; any later version.
+;;
+;; This program is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;; GNU General Public License for more details.
+;;
+;; You should have received a copy of the GNU General Public License
+;; along with this program; if not, you can either send email to this
+;; program's maintainer or write to: The Free Software Foundation,
+;; Inc.; 51 Franklin Street, Fifth Floor; Boston, MA 02110-1301, USA.
+
+;;; Commentary:
+
+;; To install, put this file in your load-path, byte-compile it, and add
+;; the following to your .emacs:
+;;
+;;   (load "escreen")
+;;   (escreen-install)
+
+;; If you are using Emacs 19, you may have trouble loading this program
+;; because of the customs syntax officially introduced in Emacs 20.  In
+;; that case, first load cust-stub.el, available from
+;;
+;;     http://www.splode.com/~friedman/software/emacs-lisp/
+;;
+;; Updates to escreen.el will also be made available on that page.
+
+;; Inspired by:
+;;   * wicos.el, written by Heikki Suopanki <suopanki@phoenix.oulu.fi>
+;;   * `screen', written by Oliver Laumann, Juergen Weigert,
+;;     and Michael Schroeder.
+
+;; Todo:
+;;   * make escreen-menu display list of screens for all frames
+;;   * ability to lock window configurations against change or deletion
+;;   * ability to renumber screens
+;;   * symbolic names for screens (a la wicos.el); partly started
+;;   * switching active screen from pull-down menu from menubar
+;;   * switching active screen from escreen menu
+;;   * persistance of screens across instances of emacs
+;;     [look at johnw's work on this; depends on additional non-standard
+;;     packages but perhaps those parts can be reimplemented inline.]
+
+;;; Code:
+
+;; Variable declarations -- can be set by user
+
+(defgroup escreen nil
+  "Window configuration management"
+  :group 'escreen
+  :group 'extensions)
+
+(defcustom escreen-max-screens 10
+  "*Maximum number of screens that may be created."
+  :type 'integer
+  :group 'escreen)
+
+(defcustom escreen-new-screen-default-buffer "*scratch*"
+  "*Default buffer to display in newly-created screens."
+  :type 'string
+  :group 'escreen)
+
+(defcustom escreen-restore-killed-buffers nil
+  "*If non-nil, automatically revisit files if they have been killed.
+That is, if a buffer was killed while in another screen session,
+recreate them, visiting whatever file they were visiting."
+  :type 'boolean
+  :group 'escreen)
+
+(defcustom escreen-preserve-buffer-list-order t
+  "*If non-nil, preserve buffer list for each screen when switching.
+When returning to a previously-saved screen, the buffer list order is
+restored.  Buffers which have been created since the saved screen was last
+visited, are put at the end of the list but the relative order is preserved.
+
+This buffer list order is returned by the function `buffer-list' and
+affects the behavior of `other-buffer', etc.
+
+In Emacs 20 and later, each frame has its own ordered buffer list.
+Switching screen sessions affects the selected frame's buffer list only."
+  :type 'boolean
+  :group 'escreen)
+
+(defcustom escreen-number-mode t
+  "*If non-nil, display current escreen number in mode line."
+  :type 'boolean
+  :group 'escreen)
+
+(defcustom escreen-install-number-mode-format t
+  "*If non-nil, install `escreen-mode-line-format' on `global-mode-string'.
+This is performed by `escreen-install'."
+  :type 'boolean
+  :group 'escreen)
+
+(defcustom escreen-goto-screen-before-hook nil
+  "*Hook to run in `escreen-goto-screen' before anything else."
+  :type 'hook
+  :group 'escreen)
+
+(defcustom escreen-goto-screen-hook nil
+  "*Hook to run after `escreen-goto-screen' completes.
+An example function that can make use of this hook is
+`escreen-enable-number-mode-if-more-than-one-screen'."
+  :type 'hook
+  :group 'escreen)
+
+(defcustom escreen-menu-mode-hook nil
+  "*Hook to run by `escreen-menu' after everything else."
+  :type 'hook
+  :group 'escreen)
+
+
+;; Keybindings
+
+(defcustom escreen-prefix-char "\C-\\"
+  "*Character prefixing escreen commands.
+If you wish to change this, you must also do
+
+   (global-set-key escreen-prefix-char 'escreen-prefix)
+
+to update the prefix in the global keymap."
+  :type 'string
+  :group 'escreen)
+
+(defvar escreen-map nil
+  "*Keymap for escreen commands.")
+(cond
+ ((null escreen-map)
+  (setq escreen-map (make-sparse-keymap))
+  (define-key escreen-map escreen-prefix-char 'escreen-goto-last-screen)
+  (define-key escreen-map "0"    'escreen-goto-screen-0)
+  (define-key escreen-map "1"    'escreen-goto-screen-1)
+  (define-key escreen-map "2"    'escreen-goto-screen-2)
+  (define-key escreen-map "3"    'escreen-goto-screen-3)
+  (define-key escreen-map "4"    'escreen-goto-screen-4)
+  (define-key escreen-map "5"    'escreen-goto-screen-5)
+  (define-key escreen-map "6"    'escreen-goto-screen-6)
+  (define-key escreen-map "7"    'escreen-goto-screen-7)
+  (define-key escreen-map "8"    'escreen-goto-screen-8)
+  (define-key escreen-map "9"    'escreen-goto-screen-9)
+  (define-key escreen-map "?"    'escreen-help)
+  (define-key escreen-map "\C-b" 'escreen-menu)
+  (define-key escreen-map "a"    'escreen-get-active-screen-numbers)
+  (define-key escreen-map "b"    'escreen-get-current-screen-number)
+  (define-key escreen-map "c"    'escreen-create-screen)
+  (define-key escreen-map "g"    'escreen-goto-screen)
+  (define-key escreen-map "k"    'escreen-kill-screen)
+  (define-key escreen-map "n"    'escreen-goto-next-screen)
+  (define-key escreen-map "p"    'escreen-goto-prev-screen)))
+
+(defalias 'escreen-prefix escreen-map)
+
+
+;;; Internal variables.  Do not set these yourself.
+
+;; This should not be modified by the user.  The information it provides is
+;; critical and the calling conventions are different than for
+;; escreen-map-data-format.  The order here is important too.
+;; Do not change this data structure without also changing the
+;; escreen-configuration-data-map-critical-* accessors.
+(defvar escreen-map-critical-data-format
+  (list 'current-buffer
+        (lambda () (buffer-name))
+        'buffer-file-name))
+
+;; If you want to add or change this list, it's best to set
+;; escreen-configuration-alist to nil and run escreen-install afterward.
+;; Otherwise, the new table will be used with old data and may cause errors.
+;;
+;; Note that resetting escreen in this way loses all but the current
+;; window configuration.
+(defvar escreen-map-data-format
+  '((escreen-map-save-window-start    . escreen-map-restore-window-start)
+    (mark-marker                      . escreen-map-restore-mark-marker)
+    (escreen-map-save-point           . escreen-map-restore-point)
+    (escreen-map-save-narrowed-region . escreen-map-restore-narrowed-region)
+    (escreen-map-save-truncate-lines  . escreen-map-restore-truncate-lines)
+    (escreen-map-save-mode-line-face  . escreen-map-restore-mode-line-face)
+    (escreen-map-save-menu-bar-mode   . escreen-map-restore-menu-bar-mode)
+    (buffer-list                      . escreen-map-restore-buffer-list)))
+
+;; Keeps track of escreen state (window config, buffers, etc.)
+;; The structure of each elt is
+;;
+;;  (screen-number screen-name
+;;                 #<window-configuration>
+;;                 (((critical-data-buffer-1) user-data-buffer-1 ...)
+;;                  ((critical-data-buffer-2) user-data-buffer-2 ...)
+;;                  ...)
+;;                 selected-window-number)
+;;
+(defvar escreen-configuration-alist nil)
+
+;; Current screen number.  Smallest possible screen number is 0.
+(defvar escreen-current-screen-number 0)
+
+;; Current screen number as a string.
+;; Smallest possible screen number is 0.
+(defvar escreen-current-screen-string
+  (int-to-string escreen-current-screen-number))
+
+;; Last-visited screen number.  Smallest possible screen number is 0.
+(defvar escreen-last-screen-number 0)
+
+;; Highest screen number currently in use.
+(defvar escreen-highest-screen-number-used 0)
+
+;; t or nil depending on if there is more than one screen
+;; This is only used by escreen-enable-number-mode-if-more-than-one-screen
+;; and escreen-mode-line-format.
+;; This defaults to t since initially there is only one screen on a frame.
+(defvar escreen-one-screen-p t)
+
+;; It's ok to change this, but it makes use of internal variables
+(defvar escreen-mode-line-format
+  '(escreen-number-mode
+    (escreen-one-screen-p "" ("S" escreen-current-screen-string " "))))
+
+(defvar escreen-frame-local-variables
+  '(escreen-configuration-alist
+    escreen-current-screen-number
+    escreen-current-screen-string
+    escreen-last-screen-number
+    escreen-highest-screen-number-used
+    escreen-one-screen-p))
+
+
+(defmacro escreen-save-frame-excursion (&rest body)
+  "Execute BODY, saving and restoring the selected frame."
+  (let ((orig-frame (make-symbol "orig-frame")))
+    `(let ((,orig-frame (selected-frame)))
+       (unwind-protect
+           (progn ,@body)
+         (and (frame-live-p ,orig-frame)
+              (select-frame ,orig-frame))))))
+
+(put 'escreen-save-frame-excursion 'lisp-indent-function 0)
+
+(defalias 'escreen-mapc (if (fboundp 'mapc) 'mapc 'mapcar))
+
+(defsubst escreen-map-frames (fn)
+  (escreen-save-frame-excursion
+    (escreen-mapc fn (frame-list))))
+
+
+;; Older versions of Emacs did not have window-pixel-edges
+;; Older versions of XEmacs did not have window-edges
+(defalias 'escreen-window-edges
+  (if (fboundp 'window-edges) 'window-edges 'window-pixel-edges))
+
+
+(defun escreen-install ()
+  (interactive)
+  (global-set-key escreen-prefix-char 'escreen-prefix)
+
+  ;; Install screen number on global-mode-string
+  (and escreen-install-number-mode-format
+       (let ((elt '("" escreen-mode-line-format)))
+         (or (member elt global-mode-string)
+             (setq global-mode-string
+                   (cons elt global-mode-string)))))
+
+  (cond ((fboundp 'make-variable-frame-local)
+         (escreen-mapc 'make-variable-frame-local
+                       escreen-frame-local-variables)
+
+         (add-hook 'after-make-frame-functions
+                   'escreen-initialize-frame-variables)))
+
+  (if escreen-number-mode
+      (escreen-number-mode 1))
+
+  ;; Initialize escreen-configuration-alist by placing current window
+  ;; config in it.
+  (if (fboundp 'make-variable-frame-local)
+      (escreen-map-frames 'escreen-initialize-frame-variables)
+    (escreen-save-current-screen-configuration)))
+
+(defun escreen-number-mode (&optional prefix)
+  "*Toggle escreen-number-mode (see variable docstring).
+If called with a positive prefix argument, always enable.
+If called with a negative prefix argument, always disable.
+If called with no prefix argument, toggle current state."
+  (interactive "P")
+  (setq escreen-number-mode
+        (cond ((null prefix)
+               (not escreen-number-mode))
+              (t
+               (>= (prefix-numeric-value prefix) 0)))))
+
+
+(defun escreen-create-screen ()
+  "Create a new screen and switch to it.
+New screen will display one window with the buffer specified by
+`escreen-new-screen-default-buffer'."
+  (interactive)
+  (let ((new-screen-number (escreen-first-unused-screen-number)))
+    (or new-screen-number
+        (error "escreen: No more screens (see \"escreen-max-screens\")"))
+
+    ;; Save window configuration before switching to a new one.
+    (escreen-save-current-screen-configuration)
+    (and (> new-screen-number escreen-highest-screen-number-used)
+         (setq escreen-highest-screen-number-used new-screen-number))
+    (setq escreen-last-screen-number escreen-current-screen-number)
+    (setq escreen-current-screen-number new-screen-number)
+    (setq escreen-current-screen-string (int-to-string new-screen-number))
+
+    ;; Don't reuse any of the previous screen's window objects; settings
+    ;; like window-dedicated-p, window display tables, etc. will just cause
+    ;; grief.
+    ;;
+    ;; Modify the frame so there is only one window; this insures that we
+    ;; have room to split to a second window.  Select new window, then
+    ;; delete the previous one.  We now start the new screen with a totally
+    ;; new window (the previous window is still saved in the window
+    ;; configuration, so its settings are not lost).
+    (delete-other-windows)
+    (select-window (split-window))
+    (delete-other-windows)
+
+    ;; create a new window and switch to that, then delete the other window.
+    ;; this is just
+    (switch-to-buffer escreen-new-screen-default-buffer)
+    ;; Save new window configuration so that it's in the alist.
+    (escreen-save-current-screen-configuration))
+  ;; We run this hook because, in a sense, we have gone to a new
+  ;; screen. but we don't actually call escreen-goto-screen because of the
+  ;; extra setup work here.
+  (run-hooks 'escreen-goto-screen-hook))
+
+(defun escreen-kill-screen (&optional number)
+  "Kill current screen, or screen given by optional argument NUMBER.
+No error occurs if the specified screen number doesn't exist.
+You cannot kill the last existing screen.
+Switch to previous screen if killing active one."
+  (interactive)
+  (let* ((screen-number (or number escreen-current-screen-number))
+         (killing-current-screen-p (eq escreen-current-screen-number
+                                       screen-number))
+         (screen-data (escreen-configuration-escreen screen-number))
+         previous-screen)
+    (cond (screen-data
+           (and killing-current-screen-p
+                (escreen-configuration-one-screen-p)
+                (error "escreen: only one screen, can't kill."))
+           ;; Don't bother looking for previous screen number unless killing
+           ;; current screen, because only then do we need to switch screens.
+           (and killing-current-screen-p
+                (setq previous-screen (escreen-get-prev-screen-number)))
+           (escreen-configuration-escreen-delete screen-data)
+           (and (eq screen-number escreen-highest-screen-number-used)
+                ;; We're killing the screen with the highest number.
+                ;; Look for the next highest number.
+                (setq escreen-highest-screen-number-used
+                      (car (sort (escreen-configuration-screen-numbers) '>))))
+           (and killing-current-screen-p
+                (escreen-goto-screen previous-screen 'dont-update-current))))))
+
+;; This is only called in versions of emacs which support frame-local
+;; variables; that's Emacs 20.3 and later.
+(defun escreen-initialize-frame-variables (&optional frame)
+  (escreen-save-frame-excursion
+    (select-frame frame)
+    (modify-frame-parameters frame
+      (mapcar (lambda (s)
+                (cons s (default-value s)))
+              escreen-frame-local-variables))
+    (setq escreen-configuration-alist nil)
+    (escreen-save-current-screen-configuration)))
+
+
+(defun escreen-goto-screen (number &optional dont-update-current)
+  "Switch to screen number N.
+Optional arg DONT-UPDATE-CURRENT means don't save the current screen
+configuration, though this isn't intended to be used interactively."
+  (interactive "NGo to escreen number: ")
+  (run-hooks 'escreen-goto-screen-before-hook)
+  (let ((screen-data (escreen-configuration-escreen number)))
+    (or screen-data
+        (error "escreen: %d: invalid screen number." number))
+    (or dont-update-current
+        (escreen-save-current-screen-configuration))
+    (escreen-restore-screen-map screen-data)
+    (setq escreen-current-screen-string (int-to-string number))
+    (or dont-update-current
+        (setq escreen-last-screen-number escreen-current-screen-number))
+    (setq escreen-current-screen-number number))
+  (run-hooks 'escreen-goto-screen-hook))
+
+(defun escreen-goto-last-screen ()
+  "Switch to the last visited screen."
+  (interactive)
+  (let ((n (if (= escreen-last-screen-number escreen-current-screen-number)
+               (escreen-get-next-screen-number escreen-last-screen-number)
+             escreen-last-screen-number)))
+    (setq escreen-last-screen-number escreen-current-screen-number)
+    (escreen-goto-screen n)))
+
+(defun escreen-goto-prev-screen (&optional n)
+  "Switch to the previous screen.
+This is the nearest lower-numbered existing screen from the current one,
+wrapping around list of screens if necessary.
+If prefix arg N given, jump to the Nth previous screen."
+  (interactive "p")
+  (if (< n 0)
+      (escreen-goto-prev-or-next-screen-internal (- n) 'next)
+    (escreen-goto-prev-or-next-screen-internal n 'prev)))
+
+(defun escreen-goto-next-screen (&optional n)
+  "Switch to the next screen.
+This is the nearest greater-numbered existing screen from the current one,
+wrapping around list of screens if necessary.
+If prefix arg N given, jump to the Nth next screen."
+  (interactive "p")
+  (if (< n 0)
+      (escreen-goto-prev-or-next-screen-internal (- n) 'prev)
+    (escreen-goto-prev-or-next-screen-internal n 'next)))
+
+(defun escreen-goto-prev-or-next-screen-internal (n prev-or-next)
+  (let ((total (length (escreen-get-active-screen-numbers)))
+        (func (if (eq prev-or-next 'next)
+                  'escreen-get-next-screen-number
+                'escreen-get-prev-screen-number))
+        (i 0)
+        (screen-number escreen-current-screen-number))
+    (and (> n total)
+         ;; Trim off excess amount so we do fewer iterations, since
+         ;; wrapping over the total number of screens even once is
+         ;; wasteful and slow.
+         (setq n (- n (* (/ n total) total))))
+    (while (< i n)
+      (setq screen-number (funcall func screen-number)
+            i (1+ i)))
+    (escreen-goto-screen screen-number)))
+
+(defun escreen-goto-screen-0 () (interactive) (escreen-goto-screen 0))
+(defun escreen-goto-screen-1 () (interactive) (escreen-goto-screen 1))
+(defun escreen-goto-screen-2 () (interactive) (escreen-goto-screen 2))
+(defun escreen-goto-screen-3 () (interactive) (escreen-goto-screen 3))
+(defun escreen-goto-screen-4 () (interactive) (escreen-goto-screen 4))
+(defun escreen-goto-screen-5 () (interactive) (escreen-goto-screen 5))
+(defun escreen-goto-screen-6 () (interactive) (escreen-goto-screen 6))
+(defun escreen-goto-screen-7 () (interactive) (escreen-goto-screen 7))
+(defun escreen-goto-screen-8 () (interactive) (escreen-goto-screen 8))
+(defun escreen-goto-screen-9 () (interactive) (escreen-goto-screen 9))
+
+
+(defun escreen-get-current-screen-number ()
+  "Returns the currently selected screen number.
+If called interactively, also print this result in the minibuffer."
+  (interactive)
+  (if (interactive-p)
+      (message "escreen: current screen is number %d"
+               escreen-current-screen-number)
+    escreen-current-screen-number))
+
+(defun escreen-get-active-screen-numbers ()
+  "Print a list of the active screen numbers in the echo area.
+Returns a list of numbers which represent screen numbers presently in use."
+  (interactive)
+  (let ((screen-list (sort (escreen-configuration-screen-numbers) '<)))
+    (if (interactive-p)
+        (message "escreen: active screens: %s"
+                 (mapconcat 'number-to-string screen-list " ")))
+    screen-list))
+
+(defun escreen-help ()
+  "Display a short summary of escreen commands."
+  (interactive)
+  (if (string-lessp emacs-version "19")
+      ;; emacs 18 couldn't list only bindings with a common prefix.
+      (describe-bindings)
+    ;; Emacs 19 can handle escreen-prefix-char (as a string) directly, but
+    ;; for XEmacs, it must be converted to a vector.
+    (describe-bindings (escreen-string-to-vector escreen-prefix-char))))
+
+(defun escreen-string-to-vector (s)
+  (let* ((l (length s))
+         (v (make-vector l nil))
+         (i 0))
+    (while (< i l)
+      (aset v i (aref s i))
+      (setq i (1+ i)))
+    v))
+
+
+;; Return the first unused number available for designation as a screen
+;; number, or nil if  escreen-max-screens  screens are already in use.
+(defun escreen-first-unused-screen-number ()
+  (let ((number 0))
+    (while (and (< number escreen-max-screens)
+                (escreen-configuration-escreen number))
+      (setq number (1+ number)))
+    (and (< number escreen-max-screens) number)))
+
+;; Save window configuration, buffer configuration, and current marks and
+;; point for each displayed buffer for the current screen.
+(defun escreen-save-current-screen-configuration ()
+  (let ((screen-data (escreen-screen-defined))
+        (new-alist-member nil))
+    (if screen-data
+        (setcdr (cdr screen-data) (escreen-save-screen-map))
+      (setq new-alist-member (cons escreen-current-screen-number
+                                   (cons nil (escreen-save-screen-map))))
+      (setq escreen-configuration-alist
+            (cons new-alist-member escreen-configuration-alist)))))
+
+;; Return attributes for screen N, or nil if it doesn't exist.
+(defun escreen-screen-defined (&optional n)
+  (escreen-configuration-escreen (or n escreen-current-screen-number)))
+
+;; Return nearest number less than current screen number that is
+;; an active screen, wrapping around end of screen list if necessary.
+(defun escreen-get-prev-screen-number (&optional current-screen-number)
+  (or current-screen-number
+      (setq current-screen-number escreen-current-screen-number))
+  (if (eq 0 escreen-highest-screen-number-used)
+      0
+    ;; Decrement/wrap current screen number
+    (setq current-screen-number (1- current-screen-number))
+    (and (< current-screen-number 0)
+         (setq current-screen-number escreen-highest-screen-number-used))
+    (while (not (assq current-screen-number escreen-configuration-alist))
+      ;; Decrement/wrap current screen number
+      (setq current-screen-number (1- current-screen-number))
+      (and (< current-screen-number 0)
+           (setq current-screen-number escreen-highest-screen-number-used)))
+    current-screen-number))
+
+;; Return nearest number greater than current screen number that is
+;; an active screen, wrapping around end of screen list if necessary.
+(defun escreen-get-next-screen-number (&optional current-screen-number)
+  (or current-screen-number
+      (setq current-screen-number escreen-current-screen-number))
+  (if (eq 0 escreen-highest-screen-number-used)
+      0
+    ;; Increment/wrap current screen number
+    (setq current-screen-number (1+ current-screen-number))
+    (and (> current-screen-number escreen-highest-screen-number-used)
+         (setq current-screen-number 0))
+    (while (not (assq current-screen-number escreen-configuration-alist))
+      ;; Increment/wrap current screen number
+      (setq current-screen-number (1+ current-screen-number))
+      (and (> current-screen-number escreen-highest-screen-number-used)
+           (setq current-screen-number 0)))
+    current-screen-number))
+
+
+;;; Primitive accessors for escreen-configuration-alist
+;;;
+;;; These could be made into macros or defsubsts, but it would make
+;;; debugging more difficult and they are not critical for speed.
+
+(defun escreen-configuration-escreen (number)
+  (assq number escreen-configuration-alist))
+
+(defun escreen-configuration-escreen-delete (data)
+  (setq escreen-configuration-alist
+        (delq (if (numberp data)
+                  (escreen-configuration-escreen data)
+                data)
+              escreen-configuration-alist)))
+
+(defun escreen-configuration-screen-numbers ()
+  (mapcar 'car escreen-configuration-alist))
+
+(defun escreen-configuration-one-screen-p ()
+  (>= 1 (length escreen-configuration-alist)))
+
+;; Sort the alist so that they are in order numerically.
+(defun escreen-configuration-alist-sort-by-number ()
+  (setq escreen-configuration-alist
+        (sort escreen-configuration-alist
+              (lambda (a b)
+                (< (car a) (car b))))))
+
+;;; map-data sub-accessors
+
+(defun escreen-configuration-screen-number (l)
+  (nth 0 l))
+
+(defun escreen-configuration-screen-name (l)
+  (nth 1 l))
+
+(defun escreen-configuration-window-data-configuration (l)
+  (nth 2 l))
+
+(defun escreen-configuration-data-map (l)
+  (nth 3 l))
+
+(defun escreen-configuration-selected-window-count (l)
+  (nth 4 l))
+
+;;; screen map data accessors
+
+(defun escreen-configuration-data-map-critical (data)
+  (car data))
+
+(defun escreen-configuration-data-map-user (data)
+  (cdr data))
+
+;;; critical map data accessors
+
+(defun escreen-configuration-data-map-critical-buffer (crit-map)
+  (nth 0 crit-map))
+
+(defun escreen-configuration-data-map-critical-buffer-name (crit-map)
+  (nth 1 crit-map))
+
+(defun escreen-configuration-data-map-critical-buffer-file-name (crit-map)
+  (nth 2 crit-map))
+
+
+(defun escreen-save-screen-map ()
+  (let ((config (current-window-configuration))
+        (win-data nil)
+        (sel-win-count 0)
+        (sel-window (selected-window))
+        (first-window (escreen-first-window))
+        (window nil))
+    (save-excursion
+      (save-window-excursion
+        (select-window first-window)
+        (while (not (eq window first-window))
+          (cond ((null sel-window))
+                ((eq (selected-window) sel-window)
+                 (setq sel-window nil))
+                (t
+                 (setq sel-win-count (1+ sel-win-count))))
+          (setq win-data
+                (cons (cons (escreen-save-critical-data)
+                            (escreen-save-user-data))
+                      win-data))
+          (setq window (select-window (next-window)))
+          (set-buffer (window-buffer (selected-window))))))
+    (list config (nreverse win-data) sel-win-count)))
+
+(defun escreen-restore-screen-map (map)
+  (let ((config (escreen-configuration-window-data-configuration map))
+        (map (escreen-configuration-data-map map))
+        (sel-win-number (escreen-configuration-selected-window-count map))
+        (win-count 0)
</