From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.io/gmane.comp.tex.context/27267 Path: news.gmane.org!not-for-mail From: Sanjoy Mahajan Newsgroups: gmane.comp.tex.context Subject: emacs lisp for context in AucTeX Date: Wed, 19 Apr 2006 21:44:16 -0400 Message-ID: Reply-To: mailing list for ConTeXt users NNTP-Posting-Host: main.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Trace: sea.gmane.org 1145497489 8985 80.91.229.2 (20 Apr 2006 01:44:49 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Thu, 20 Apr 2006 01:44:49 +0000 (UTC) Original-X-From: ntg-context-bounces@ntg.nl Thu Apr 20 03:44:42 2006 Return-path: Envelope-to: gctc-ntg-context-518@m.gmane.org Original-Received: from ronja.vet.uu.nl ([131.211.172.88] helo=ronja.ntg.nl) by ciao.gmane.org with esmtp (Exim 4.43) id 1FWODj-0002kV-5v for gctc-ntg-context-518@m.gmane.org; Thu, 20 Apr 2006 03:44:35 +0200 Original-Received: from localhost (localhost [127.0.0.1]) by ronja.ntg.nl (Postfix) with ESMTP id 8FA1412775; Thu, 20 Apr 2006 03:44:34 +0200 (CEST) Original-Received: from ronja.ntg.nl ([127.0.0.1]) by localhost (smtp.ntg.nl [127.0.0.1]) (amavisd-new, port 10024) with LMTP id 19196-06; Thu, 20 Apr 2006 03:44:29 +0200 (CEST) Original-Received: from ronja.vet.uu.nl (localhost [127.0.0.1]) by ronja.ntg.nl (Postfix) with ESMTP id 71B9A1276C; Thu, 20 Apr 2006 03:44:29 +0200 (CEST) Original-Received: from localhost (localhost [127.0.0.1]) by ronja.ntg.nl (Postfix) with ESMTP id 9E5F31276C for ; Thu, 20 Apr 2006 03:44:27 +0200 (CEST) Original-Received: from ronja.ntg.nl ([127.0.0.1]) by localhost (smtp.ntg.nl [127.0.0.1]) (amavisd-new, port 10024) with LMTP id 19278-04 for ; Thu, 20 Apr 2006 03:44:24 +0200 (CEST) Original-Received: from smtpauth07.mail.atl.earthlink.net (smtpauth07.mail.atl.earthlink.net [209.86.89.67]) by ronja.ntg.nl (Postfix) with SMTP id 0264E126F8 for ; Thu, 20 Apr 2006 03:44:23 +0200 (CEST) Original-Received: from [24.41.6.91] (helo=approximate.corpus.cam.ac.uk) by smtpauth07.mail.atl.earthlink.net with asmtp (TLSv1:AES256-SHA:256) (Exim 4.34) id 1FWODW-0001AC-Bg; Wed, 19 Apr 2006 21:44:22 -0400 Original-Received: from sanjoy by approximate.corpus.cam.ac.uk with local (Exim 4.60) (envelope-from ) id 1FWODQ-0003tn-Jo; Wed, 19 Apr 2006 21:44:16 -0400 Original-To: ntg-context@ntg.nl X-Mailer: MH-E 7.93; nmh 1.1; GNU Emacs 21.4.1 X-ELNK-Trace: dcd19350f30646cc26f3bd1b5f75c9f474bf435c0eb9d4789de090ecffd5a975949cc1068cab90ed3024c067555c2132350badd9bab72f9c350badd9bab72f9c X-Originating-IP: 24.41.6.91 X-Virus-Scanned: amavisd-new at ntg.nl X-BeenThere: ntg-context@ntg.nl X-Mailman-Version: 2.1.7 Precedence: list List-Id: mailing list for ConTeXt users List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: ntg-context-bounces@ntg.nl Errors-To: ntg-context-bounces@ntg.nl X-Virus-Scanned: amavisd-new at ntg.nl Xref: news.gmane.org gmane.comp.tex.context:27267 Archived-At: I wrote the following emacs lisp for my .emacs and pass it on (no copyright) in case others find it useful. I used variants for a decade with the old tex mode, but just rewrote it for auctex and for context's display math syntax (which deprecates $$...$$). The purpose: 1. If you type {, [, or (, the appropriate right delimiter is inserted for you and the insertion point is placed between them. So I never get unbalanced XYZ errors. If you think it's a misfeature, delete the last three local-set-key's in the TeX-mode-hook. 2. Same feature if you type $ for inline math: you get $$. The old tex-mode code would do the same on typing a second $, so you'd get $$$$ for tex's display math. The code below will also do that, except in context mode. 3. In context mode, typing the second $ will gobble up preceding whitespace and then insert \placeformula\startformula \stopformula The whitespace-deletion code in start-context-math is a bit pathetic and any improvements are welcome. I couldn't get re-search-backward to work for me because it wouldn't match greedily (so it would gobble up one space but not all of them, for example), so instead it just looks one character at a time and deletes what it should. -Sanjoy (defun insert-balanced (left right) "Make a left, right delmiter pair and be poised to type inside of them." (interactive) (insert left) (save-excursion (insert right))) (defun start-context-math () (interactive) (let* ((start (max (point-min) (- (point) 1))) (stop (min (point-max) (+ (point) 1)))) ; if in the middle of a $$, turn inline math into context display math (if (equal "$$" (buffer-substring-no-properties start stop)) (progn (delete-region start stop) ;get rid of the $$ ; delete preceding spaces, if any (while (and (< (point-min) (point)) (equal (buffer-substring-no-properties (- (point) 1) (point)) " ")) (backward-delete-char 1)) ; delete a preceding newline, if any (if (equal (buffer-substring-no-properties (- (point) 1) (point)) "\n") (backward-delete-char 1)) ; place (insert "\n\\placeformula\\startformula\n") (save-excursion (insert "\n\\stopformula"))) ; else: just doing inline math (insert-balanced ?\$ ?\$)))) (add-hook 'ConTeXt-mode-hook '(lambda () (local-set-key "$" 'start-context-math))) (add-hook 'TeX-mode-hook '(lambda () (local-set-key "$" '(lambda () (interactive) (insert-balanced ?\$ ?\$))) (local-set-key "{" '(lambda () (interactive) (insert-balanced ?\{ ?\}))) (local-set-key "[" '(lambda () (interactive) (insert-balanced ?\[ ?\]))) (local-set-key "(" '(lambda () (interactive) (insert-balanced ?\( ?\))))))