; Author: Peter Gacs, Sept 30, 1997 ; ;The macros below allow to write latex source files much more conveniently. ;There are two improvements over similar macros I have seen. First, when, ;say, M-space is pushed, you are put between $ $, but the second $ is not ;seen since it is banished temporarily to the next line below. Same with ;(), {}, _{}, etc. Second, you can use the same keystroke M-/ to get any ;such nested pair of things to close up and to position you after them. ; Thus, M-space 2 M-^ n in the text below at the position ; "takes time to do" ; produces ; "takes time $2^{n ;} ; ;$ to do" ; After M-/M-/, you will have ; "takes time $2^{n}$ to do". ; The following hack was needed a while ago to turn on LaTeX Fill in every ; TeX file (maybe it is still). ; The problem is that latex-mode calls tex-mode-hook ; again. To avoid infinite recursion the value of tex-mode-hook ; must be turned off for the time of the calling of latex-mode. (set-variable 'my-tex-mode-hook '((lambda () (setq tex-mode-hook nil) (latex-mode) (setq tex-mode-hook my-tex-mode-hook) (define-key tex-mode-map " " 'tex-math-escape); M-space (define-key tex-mode-map "Þ" 'tex-superscript); M-^ (define-key tex-mode-map "ß" 'tex-subscript); M-_ ; (define-key tex-mode-map "É" 'tex-italic-escape); M-i ; (define-key tex-mode-map "ô" 'tex-typewriter-escape) ;M-t (define-key tex-mode-map "Å" 'latex-environment); M-E (auto-fill-mode 1) ))) (set-variable 'tex-mode-hook my-tex-mode-hook) (load "tex-mode" t t) ; I always need tex mode. (set-variable 'TeX-directory ".") (defun tex-pop () "Gets you out of the last $$,(),{},\{\},[],||, etc. created by the corresponding TeX macros. Works not only in TeX mode. This is a poor man's up-list ." (interactive)(delete-char 1)(end-of-line)(delete-char 1) ) (defun latex-environment (Env) "Creates a LaTeX environment." (interactive "*sEnvironment: ") (let ((indentation (current-column))) (insert "\\begin{" Env "}") (save-excursion (insert "\n")(indent-to indentation) (insert "\\end{" Env "}\n")(indent-to indentation) ))) (defun tex-math-escape () "Print two $'s and put the cursor between them, with some linebreaks." (interactive) (insert-string "$$")(open-line 1) (backward-char)(open-line 1) ) (defun latex-math-escape () "Print \(\) and put the cursor between them, with some linebreaks." (interactive) (insert-string "\\(\\)")(open-line 1) (backward-char 2)(open-line 1) ) (defun tex-italic-escape () "Print {\it } and put the cursor between them, with some linebreaks." (interactive) (insert-string "{\\it }") (open-line 1)(backward-char)(open-line 1) ) (defun tex-typewriter-escape () "Print {\tt } and put the cursor between them, with some linebreaks." (interactive) (insert-string "{\\tt }") (open-line 1)(backward-char)(open-line 1) ) (defun tex-insert-braces () "Make a pair of (possibly escaped) braces, with some linebreaks, and be poised to type inside of them." (interactive) (let (Escaped) (if (and (> (point) 1 ) (string-equal (buffer-substring (- (point) 1) (point)) "\\") ) (setq Escaped 1) (setq Escaped 0)) (insert ?\{) (save-excursion (newline 1) (if (= Escaped 1) (insert ?\\ )) (insert ?})(newline 1) ))) (defun my-insert-parentheses () "Make a pair of (possibly escaped) parentheses and be poised to type inside of them." (interactive) (let (Escaped) (if (and (> (point) 1 ) (string-equal (buffer-substring (- (point) 1) (point)) "\\") ) (setq Escaped 1) (setq Escaped 0)) (insert ?\() (save-excursion (newline 1) (if (= Escaped 1) (insert ?\\ )) (insert ?\))(newline 1) ))) (defun insert-square-brackets () "Make a pair of (possibly escaped) square brackets and be poised to type inside of them, with some linebreaks." (interactive) (let (Escaped) (if (and (> (point) 1) (string-equal (buffer-substring (- (point) 1) (point)) "\\") ) (setq Escaped 1) (setq Escaped 0) ) (insert ?\[) (if (= Escaped 1) (progn (newline 1)(insert " ") (save-excursion (newline 1) (insert " \\\]") )) (save-excursion (newline 1)(insert ?\])(newline 1) )))) (defun insert-bars () "Make a pair of vertical bars and be poised to type inside of them." (interactive) (insert ?|) (save-excursion (newline 1)(insert ?|)(newline 1) )) (defun tex-subscript () "Write underscore with accompanying braces." (interactive) (insert ?_) (tex-insert-braces) ) (defun tex-superscript () "Write caret with accompanying braces." (interactive) (insert ?^) (tex-insert-braces) ) (defun tex-quote-off () "Turns off tex-insert-quote." (interactive) (define-key tex-mode-map "\"" 'self-insert-command) ) (defun tex-quote-on () "Turns on tex-insert-quote." (interactive) (define-key tex-mode-map "\"" 'tex-insert-quote) ) (defun find-label-def () (interactive) (let (x) (re-search-forward "}") (setq x (point)) (re-search-backward "{") ; (bookmark-set "l") (tags-search (concat "\\label" (buffer-substring (point) x))))) (defun find-other-refs () (interactive) (let (x) (re-search-forward "}") (setq x (point)) (re-search-backward "{") ; (bookmark-set "l") (tags-search (buffer-substring (point) x)))) (define-key ctl-x-map "c" 'comment-region) ; (define-key ctl-x-map " " 'find-label-def) (define-key ctl-x-map "p" 'mh-rmail) ;; (define-key ctl-x-map "t" 'find-other-refs) (define-key esc-map "(" 'my-insert-parentheses) ; official is different (define-key esc-map "[" 'insert-square-brackets) (define-key esc-map "{" 'tex-insert-braces) (define-key esc-map "/" 'tex-pop) (define-key esc-map "|" 'insert-bars)