Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
tomathan December 23, 2011
file 149 lines (126 sloc) 4.586 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
;;; autopair-latex.el --- Automatically pair dollar signs and \[,\] for math
;; mode in latex. Without this, math mode does not close on its own and the
;; coloring and formatting of the document gets messed up.

;; Copyright (C) 2011 Tom Guo <tomguo [at] cs.washington.edu>

;; 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 3 of the License, 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, see <http://www.gnu.org/licenses/>.

;;; Installation:
;;
;; Put (require 'autopair-latex) in your .emacs file

;;; Use:
;;
;; By default, 'C-c \' is bound to '\[','\]'. To initiate math mode, type
;; either '$' or 'C-c \', and emacs will automatically insert both the opening
;; and closing of math mode. Whereever you want math mode to end, type '$' or
;; 'C-c \', and emacs will insert a '$' or '\]' if needed, blink back where
;; math mode began, and move to the point right after end of math mode for you
;; to continue your document in text mode.

(defconst *blink-delay* 0.1) ;; used for matching

(defun count-dollar-signs ()
  "counts number of $'s before point, parity used to determine mode at point"
  (setq end-pos (point))
  (goto-char 1)
  (setq ct 0)
  (while (search-forward "$" end-pos t)
    (setq ct (1+ ct)))
  (goto-char end-pos)
  ct
)

(defun count-backslash-brackets ()
  "counts number of \[ and \] before point, parity used to determine mode at point"
  (setq end-pos (point))
  (goto-char 1)
  (setq ct 0)
  (while (search-forward "\\\[" end-pos t)
    (setq ct (1+ ct))
    )
  (goto-char 1)
  (while (search-forward "\\\]" end-pos t)
    (setq ct (1+ ct))
    )
  (goto-char end-pos)
  ct
)

(defun delete-backwards ()
  "delete both $'s when at second '$' of '$$'"
  "delete '\[\]' when at second '\'"
  (interactive)
  (setq following-char (following-char))
  (if (preceding-char)
      (setq preceding-char (preceding-char))
    (setq preceding-char 0))
  (if (char-after (1+ (point)))
      (setq following-char+1 (char-after (1+ (point))))
    (setq following-char+1 0))
  (if (char-before (1- (point)))
      (setq preceding-char-1 (char-before (1- (point))))
    (setq preceding-char-1 0))
  (when (and (char-equal ?$ following-char) (char-equal ?$ preceding-char) (oddp (count-dollar-signs)))
     (delete-char 1))

  (setq bracket-before (char-equal ?\[ preceding-char))
  (setq backslash-before (char-equal ?\\ preceding-char-1))
  (setq backslash-after (char-equal ?\\ following-char))
  (setq bracket-after (char-equal ?\] following-char+1))
  (when (and bracket-before backslash-before backslash-after bracket-after)
    (delete-char 2)
    (delete-backward-char 1))
  (delete-backward-char 1) ; the default action

)

(defun dollar-sign ()
  "auto pairs and matches $'s"
  (interactive)
  (setq is-even (evenp (count-dollar-signs)))
  (when is-even
(insert "$$")
(backward-char)
)

  (unless is-even
    (setq at-dollar (char-equal ?$ (following-char)))
    (unless at-dollar
      (insert "$")
      (backward-char)
      )
    (goto-char (search-backward "$"))
    (sit-for *blink-delay*)
    (forward-char)
    (search-forward "$")
    )
)

(defun backslash-bracket ()
  "auto pairs and matches \[ and \]"
  (interactive)
  (setq is-even (evenp (count-backslash-brackets)))
  (when is-even
    (insert "\\\[\\\]")
    (backward-char 2))

  (unless is-even
    (setq at-backslash (char-equal ?\\ (following-char)))
    (setq at-bracket (char-equal ?\] (char-after (1+ (point)))))
    
    (unless (and at-backslash at-bracket)
      (insert "\\\]")
      (backward-char 2))

    (goto-char (search-backward "\\\["))
    (sit-for *blink-delay*)
    (forward-char 2)
    (search-forward "\\\]")
    )
)

(add-hook 'TeX-mode-hook (lambda()
(local-set-key (kbd "$") 'dollar-sign)
(local-set-key (kbd "<backspace>") 'delete-backwards)
(local-set-key (kbd "C-c \\") 'backslash-bracket)
))

(add-hook 'LaTeX-mode-hook (lambda()
(local-set-key (kbd "$") 'dollar-sign)
(local-set-key (kbd "<backspace>") 'delete-backwards)
(local-set-key (kbd "C-c \\") 'backslash-bracket)
))

(provide 'autopair-latex)

Something went wrong with that request. Please try again.