ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
From: Sanjoy Mahajan <sanjoy@mrao.cam.ac.uk>
Subject: emacs lisp for context in AucTeX
Date: Wed, 19 Apr 2006 21:44:16 -0400	[thread overview]
Message-ID: <E1FWODQ-0003tn-Jo@approximate.corpus.cam.ac.uk> (raw)

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 $<insertion point
   here>$.  The old tex-mode code would do the same on typing a second $,
   so you'd get $$<insertion point here>$$ 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
<place insertion point here>
\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 ?\( ?\))))))

             reply	other threads:[~2006-04-20  1:44 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-04-20  1:44 Sanjoy Mahajan [this message]
2006-04-20 12:23 ` Johan Sandblom
2006-04-20 15:15   ` Sanjoy Mahajan
2006-04-20 15:45     ` Johan Sandblom
2006-04-20 15:48       ` Sanjoy Mahajan
2006-04-20 16:04         ` Johan Sandblom

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=E1FWODQ-0003tn-Jo@approximate.corpus.cam.ac.uk \
    --to=sanjoy@mrao.cam.ac.uk \
    --cc=ntg-context@ntg.nl \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).