;;; Miscelaneous additions to auctex (version 1.01) ;;; Copyright (C) 2001, 2002 Stefan D. Bruda ;;; ;;; 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 1, 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, write to the Free Software ;;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. ;;; ;;; ;;; Commentary: ;; ;; I find that following additions to auctex dramatically improved my ;; productivity, so that I wrote some pieces of code implementing ;; them. The new key bindings are as follows: ;; ;; - C-arrow-down and C-arrow-up moves point to the next or previous ;; sectioning command (chapter or [sub]*section), respectively. ;; ;; - "$" inserts itself if region is not active, and surrounds the region ;; by "$" characters otherwise. ;; ;; - "^" and "_" insert themselves, followed by "{}". Point is positioned ;; between the braces. If region is active, the region is placed between ;; the braces. ;; ;; - as soon as you type a period immediately following a sequence of two ;; periods, the whole sequence is changed to "\ldots". ;; ;; - Backspace is electric: in addition to its normal functionality, it ;; deletes special formatting, as follows: Pressing it at the end of ;; "\ldots" converts it to "..."; after a LaTeX-like quote (`` or ''), ;; changes it to a double quote character ("); if you want to insert just ;; the character "_" or "^", press the corresponding key (which also ;; inserts the braces for a sub/superscript environment) and then press ;; backspace. ;; ;; - C-` moves point after the next closing brace. ;; ;; - C-c C-v inserts a \verb environment with ! as delimiter (the ;; selection if any is included in the environment). ;; ;; To use these features, place the file where emacs can see it, and ;; evaluate (or place in your init file, normally .emacs) the ;; expressions ;; ;; (load "latex-bindings") ;; (add-hook 'LaTeX-mode-hook 'my-latex-bindings) ;; ;; If you improve/change this file, I would appreciate if you send ;; me a copy. Not only I will be glad to use it, but, unless you tell ;; me not to, I will post your changes for others to use and praise ;; you. ;-) ;; Misc math mode (defun latex-surround-by (empty beg end back-over-end) "The main function implementing electric special characters ($, ^,_, etc.; see for example `latex-subscript'). If region is active, insert BEG before it and END after. Otherwise, insert EMPTY and place point BACK-OVER-END characters before the end of EMPTY. If `zmacs-regions' is nil, always behave as if region is *not* active." (if (and zmacs-regions (region-active-p)) (let ((text (buffer-substring (mark) (point)))) (delete-region (mark) (point)) (insert beg) (insert text) (insert end)) (progn (insert empty) (backward-char back-over-end)))) (defun latex-mathematicize () "Electric \"$\". If region is not active, inserts a \"$\" character. Otherwise, surrounds the region by \"$\" characters. Basically, just a call to `latex-surround-by' (which see) with appropriate arguments." (interactive) (latex-surround-by "$" "$" "$" 0)) (defun latex-subscript () "Electric subscript. If region is not active, inserts \"_{}\" and places cursor between braces. Otherwise, inserts \"_{\" before region and \"}\" after. Basically, just a call to `latex-surround-by' with appropriate arguments." (interactive) (latex-surround-by "_{}" "_{" "}" 1)) (defun latex-superscript () "Electric superscript. If region is not active, inserts \"^{}\" and places cursor between braces. Otherwise, inserts \"^{\" before region and \"}\" after. Basically, just a call to `latex-surround-by' with appropriate arguments." (interactive) (latex-surround-by "^{}" "^{" "}" 1)) (defun latex-verb () "Electric \"\\verb\" (just as, e.g., `latex-superscript') with \"!\" as delimiter." (interactive) (latex-surround-by "\\verb!!" "\\verb!" "!" 1)) ;; Here is an example for handling other things similar to _, ^, or ;; $. (the functionality of latex-emphasize below is already handled by ;; C-c C-f C-e...) ;; ;(defun latex-emphasize () ; (interactive) ; (latex-surround-by "\\emph{}" "\\emph{" "}")) (defun latex-slides-hide () (interactive) (latex-surround-by "\\hide{}" "\\hide{" "}" 1)) ;; Auto ldots... (defun latex-ldots () "Electric \"...\". Typing a period after a sequence of two periods converts the whole sequence to the LaTeX command \\ldots." (interactive) (cond ((and (boundp 'pending-delete-mode) pending-delete-mode zmacs-regions (region-active-p)) (delete-region (point) (mark)) (insert ".")) ((and (>= (- (point) 2) (point-min)) (equal ".." (buffer-substring (- (point) 2) (point)))) (delete-region (- (point) 2) (point)) (insert "\\ldots")) (t (insert ".")))) ;; LaTeX sectioning search. (defun latex-next-section () "Moves cursor to the nearest sectioning command below point." (interactive) (unless (search-forward-regexp "\\(\\\\\\(sub\\)*section\\)\\|\\(\\\\chapter\\)" nil t) (error "No more sectioning commands below."))) (defun latex-previous-section () "Moves cursor to the nearest sectioning command above point." (interactive) (unless (search-backward-regexp "\\(\\\\\\(sub\\)*section\\)\\|\\(\\\\chapter\\)" nil t) (error "No more sectioning commands above."))) ;; Making backspace electric: (defun latex-smart-delete (arg &optional killflag) "Electric backward delete. Works just like `delete-backward-char', except that - \"\\ldots-!-\" is converted to \"...-!-\" - \"``-!-\" and \"''-!-\" are converted to \"\"-!-\" - \"_{-!-}\" is converted to \"_-!-\" - \"^{-!-}\" is converted to \"^-!-\" where \"-!-\" specifies the position of the point." (interactive "p") (let* ((safe-ldots (>= (- (point) 6) (point-min))) (safe-quote (>= (- (point) 2) (point-min))) (safe-sub/super (and safe-quote (<= (+ (point) 1) (point-max))))) (cond ((and (region-active-p) zmacs-regions (boundp 'pending-delete-mode) pending-delete-mode) (delete-region (point) (mark))) ; if in pending-delete mode and if region is ; active, just delete the region; otherwise... ((and safe-ldots (equal (buffer-substring (- (point) 6) (point)) "\\ldots")) (delete-region (- (point) 6) (point)) (insert "...")) ; \ldots to ... ((and safe-quote (or (equal (buffer-substring (- (point) 2) (point)) "``") (equal (buffer-substring (- (point) 2) (point)) "''"))) (delete-region (- (point) 2) (point)) (insert "\"")) ; LaTeX quote to character ((and safe-sub/super (or (equal (buffer-substring (- (point) 2) (+ (point) 1)) "_{}") (equal (buffer-substring (- (point) 2) (+ (point) 1)) "^{}"))) (delete-region (- (point) 1) (+ (point) 1))) ; sub/superscript to character (t (delete-backward-char arg killflag) ; just delete the damn thing! )))) ;; The actual bindings: (defun my-latex-bindings () "Create new bindings in LaTeX mode. See `latex-mathematicize', `latex-ldots', `latex-subscript', `latex-superscript', `latex-smart-delete', `latex-next-section', `latex-previous-section'." ;(define-key LaTeX-mode-map [(control $)] 'latex-mathematicize) (define-key LaTeX-mode-map [$] 'latex-mathematicize) (define-key LaTeX-mode-map [\.] 'latex-ldots) (define-key LaTeX-mode-map [_] 'latex-subscript) (define-key LaTeX-mode-map [^] 'latex-superscript) (define-key LaTeX-mode-map [f10] 'latex-slides-hide) (define-key LaTeX-mode-map [(control c) (control v)] 'latex-verb) (define-key LaTeX-mode-map [backspace] 'latex-smart-delete) (define-key LaTeX-mode-map [(control \`)] (lambda () "Moves point after the next closing brace." (interactive) (if (equal (buffer-substring (point) (+ (point) 1)) "}") (forward-char 1) (search-forward "}" nil t)))) (define-key LaTeX-mode-map [(control down)] 'latex-next-section) (define-key LaTeX-mode-map [(control up)] 'latex-previous-section) ) (defun latex-romanian-diacritics () (interactive) (define-key LaTeX-mode-map [(control c) (control a)] (lambda () (interactive) (insert "\\^{a}"))) (define-key LaTeX-mode-map [(control c) (control q)] (lambda () (interactive) (insert "\\u{a}"))) (define-key LaTeX-mode-map [(control c) (control i)] (lambda () (interactive) (insert "\\^{\\i}"))) (define-key LaTeX-mode-map [(control c) (control s)] (lambda () (interactive) (insert "\\c{s}"))) (define-key LaTeX-mode-map [(control c) (control t)] (lambda () (interactive) (insert "\\c{t}")))) ;; latex-bindings.el ends here.