emacs lisp for context in AucTeX
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 ?\( ?\))))))
Thank you for this. I have written the following much more amateurish
emacs lisp code in order to make it easier to enter natural tables and
flowcharts. Perhaps someone may find it useful or can suggest
improvements. I also added some keybindings for them.
Johan
(defun context-insert-nattab (rows columns)
;; Johan Sandblom 060128
"Insert a TABLE skeleton"
(interactive "nNumber of rows: \nnNumber of columns: \n")
(newline)
(insert "\\bTABLE\n\\setupTABLE\[\]\n")
;; First a TABLE header
(insert "\\bTABLEhead\n\\bTR\\bTH \\eTH\n")
(let ((column 1))
(while (< column (- columns 1))
(insert " \\bTH \\eTH\n")
(setq column (1+ column))))
(insert " \\bTH \\eTH\\eTR\n\\eTABLEhead\n\\bTABLEbody\n")
;; The rows and columns
(let ((row 1))
(while (<= row rows)
(insert "\\bTR\\bTD \\eTD\n")
;; The let expression makes sure that each loop starts at the
;; right place
(let ((column 1))
(while (< column (- columns 1))
(insert " \\bTD \\eTD\n")
(setq column (1+ column)))
(insert " \\bTD \\eTD\\eTR\n")
(setq row (1+ row))))
(insert "\\eTABLEbody\n\\eTABLE\n")))
(defun context-insert-nattab-row (columns)
"Insert a row in a TABLE"
(interactive "nNumber of columns: \n")
(newline)
(insert "\\bTR\\bTD \\eTD\n")
(let ((column 1))
(while (< column (- columns 1))
(insert " \\bTD \\eTD\n")
(setq column (1+ column)))
(insert " \\bTD \\eTD\\eTR\n")))
(defun context-insert-nattab-column (&optional arg)
"Insert a column in a TABLE"
(interactive "P")
(insert "\\bTD \\eTD")
(indent-for-tab-command)
(newline)
(backward-char 5))
(defun context-insert-FLOW-cell (n)
;; Johan Sandblom 060128
"Insert a FLOWchart cell"
(interactive "nNumber of cells: \n")
(newline)
(let ((x 1))
(while (<= x n)
(insert "\\startFLOWcell\n")
(insert " \\name {}\n")
(insert " \\location {}\n")
(insert " \\shape {action}\n")
(insert " \\text {}\n")
(insert " \\connection[rl]{}\n")
(insert "\\stopFLOWcell\n")
(setq x (1+ x)))))
(add-hook 'ConTeXt-mode-hook
'(lambda ()
(local-set-key "\C-c\C-fc" 'context-insert-FLOW-cells)
(local-set-key "\C-cnr" 'context-insert-nattab-row)
(local-set-key "\C-cnc" 'context-insert-nattab-column)
(local-set-key "\C-cnn" 'context-insert-nattab)
(local-set-key "$" 'start-context-math)))
2006/4/20, Sanjoy Mahajan
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 ?\( ?\))))))
_______________________________________________ ntg-context mailing list ntg-context@ntg.nl http://www.ntg.nl/mailman/listinfo/ntg-context
-- Johan Sandblom N8, MRC, Karolinska sjh t +46851776108 17176 Stockholm m +46735521477 Sweden "What is wanted is not the will to believe, but the will to find out, which is the exact opposite" - Bertrand Russell
Great. Mojca suggested that I wikify my Emacs lisp. It's now at http://wiki.contextgarden.net/Text_editor. Following that method ('a method is a trick I use twice.' --Polya), I've put your elisp there too -- hope that's okay. I removed the (local-set-key "$" 'start-context-math) so that your code can stand alone (in case people want one or the other). I had to change a few things at the end (some maybe because I'm using GNU Emacs rather than XEmacs, not sure). Below is the diff and I've attached the resulting version. Most of the trouble was in the keybindings: (local-set-key "\C-c\C-fc" 'context-insert-FLOW-cells) doesn't work because C-c C-f is already defined as TeX-font, so it can't be a prefix as well. So I chose "\C-cnF". Also the function name needed an "s" at the end. These two: (local-set-key "\C-cnn" 'context-insert-nattab) (local-set-key "\C-cnr" 'context-insert-nattab-row) have a related problem, in that "\C-cn" is not a prefix yet, so "\C-cnn" and "\C-cnr" are not valid sequences. So I changed the first use of "\C-cn" as a prefix to use define-key(), which seems to make a prefix-map automatically as I eventually learnt by inspecting tex.el. So: (define-key (current-local-map) "\C-cnF" 'context-insert-FLOW-cells) Then the following local-set-key's work. As I say, this is on Emacs 21.4, and the tricks may be different on XEmacs. -Sanjoy --- a/tables.el 2006-04-20 10:46:16.000000000 -0400 +++ b/tables.el 2006-04-20 10:43:41.000000000 -0400 @@ -1,5 +1,5 @@ (defun context-insert-nattab (rows columns) - ;; Johan Sandblom 060128 + ;; Johan Sandblom 2006-01-28 "Insert a TABLE skeleton" (interactive "nNumber of rows: \nnNumber of columns: \n") (newline) @@ -44,8 +44,8 @@ (newline) (backward-char 5)) -(defun context-insert-FLOW-cell (n) - ;; Johan Sandblom 060128 +(defun context-insert-FLOW-cells (n) + ;; Johan Sandblom 2006-01-28 "Insert a FLOWchart cell" (interactive "nNumber of cells: \n") (newline) @@ -62,8 +62,7 @@ (add-hook 'ConTeXt-mode-hook '(lambda () - (local-set-key "\C-c\C-fc" 'context-insert-FLOW-cells) + (define-key (current-local-map) "\C-cnF" 'context-insert-FLOW-cells) (local-set-key "\C-cnr" 'context-insert-nattab-row) (local-set-key "\C-cnc" 'context-insert-nattab-column) - (local-set-key "\C-cnn" 'context-insert-nattab) - (local-set-key "$" 'start-context-math))) + (local-set-key "\C-cnn" 'context-insert-nattab))) (defun context-insert-nattab (rows columns) ;; Johan Sandblom 2006-01-28 "Insert a TABLE skeleton" (interactive "nNumber of rows: \nnNumber of columns: \n") (newline) (insert "\\bTABLE\n\\setupTABLE\[\]\n") ;; First a TABLE header (insert "\\bTABLEhead\n\\bTR\\bTH \\eTH\n") (let ((column 1)) (while (< column (- columns 1)) (insert " \\bTH \\eTH\n") (setq column (1+ column)))) (insert " \\bTH \\eTH\\eTR\n\\eTABLEhead\n\\bTABLEbody\n") ;; The rows and columns (let ((row 1)) (while (<= row rows) (insert "\\bTR\\bTD \\eTD\n") ;; The let expression makes sure that each loop starts at the ;; right place (let ((column 1)) (while (< column (- columns 1)) (insert " \\bTD \\eTD\n") (setq column (1+ column))) (insert " \\bTD \\eTD\\eTR\n") (setq row (1+ row)))) (insert "\\eTABLEbody\n\\eTABLE\n"))) (defun context-insert-nattab-row (columns) "Insert a row in a TABLE" (interactive "nNumber of columns: \n") (newline) (insert "\\bTR\\bTD \\eTD\n") (let ((column 1)) (while (< column (- columns 1)) (insert " \\bTD \\eTD\n") (setq column (1+ column))) (insert " \\bTD \\eTD\\eTR\n"))) (defun context-insert-nattab-column (&optional arg) "Insert a column in a TABLE" (interactive "P") (insert "\\bTD \\eTD") (indent-for-tab-command) (newline) (backward-char 5)) (defun context-insert-FLOW-cells (n) ;; Johan Sandblom 2006-01-28 "Insert a FLOWchart cell" (interactive "nNumber of cells: \n") (newline) (let ((x 1)) (while (<= x n) (insert "\\startFLOWcell\n") (insert " \\name {}\n") (insert " \\location {}\n") (insert " \\shape {action}\n") (insert " \\text {}\n") (insert " \\connection[rl]{}\n") (insert "\\stopFLOWcell\n") (setq x (1+ x))))) (add-hook 'ConTeXt-mode-hook '(lambda () (define-key (current-local-map) "\C-cnF" 'context-insert-FLOW-cells) (local-set-key "\C-cnr" 'context-insert-nattab-row) (local-set-key "\C-cnc" 'context-insert-nattab-column) (local-set-key "\C-cnn" 'context-insert-nattab)))
Strange that I did not run into these issues, I use GNU emacs as well,
version 22.0.50.1, so presumably a difference in the versions. C-c C-f
is not bound to TeX-font for me, which is odd. Sorry about the
cell/cells issue, I renamed the function just before sending it. Of
course it is ok to put it on the wiki (and thank you for doing that),
the code can be used and altered entirely according to anyone's taste.
Johan
2006/4/20, Sanjoy Mahajan
Great. Mojca suggested that I wikify my Emacs lisp. It's now at http://wiki.contextgarden.net/Text_editor. Following that method ('a method is a trick I use twice.' --Polya), I've put your elisp there too -- hope that's okay. I removed the
(local-set-key "$" 'start-context-math)
so that your code can stand alone (in case people want one or the other).
I had to change a few things at the end (some maybe because I'm using GNU Emacs rather than XEmacs, not sure). Below is the diff and I've attached the resulting version. Most of the trouble was in the keybindings:
(local-set-key "\C-c\C-fc" 'context-insert-FLOW-cells)
doesn't work because C-c C-f is already defined as TeX-font, so it can't be a prefix as well. So I chose "\C-cnF". Also the function name needed an "s" at the end. These two:
(local-set-key "\C-cnn" 'context-insert-nattab) (local-set-key "\C-cnr" 'context-insert-nattab-row)
have a related problem, in that "\C-cn" is not a prefix yet, so "\C-cnn" and "\C-cnr" are not valid sequences. So I changed the first use of "\C-cn" as a prefix to use define-key(), which seems to make a prefix-map automatically as I eventually learnt by inspecting tex.el. So:
(define-key (current-local-map) "\C-cnF" 'context-insert-FLOW-cells) Then the following local-set-key's work. As I say, this is on Emacs 21.4, and the tricks may be different on XEmacs.
-Sanjoy
--- a/tables.el 2006-04-20 10:46:16.000000000 -0400 +++ b/tables.el 2006-04-20 10:43:41.000000000 -0400 @@ -1,5 +1,5 @@ (defun context-insert-nattab (rows columns) - ;; Johan Sandblom 060128 + ;; Johan Sandblom 2006-01-28 "Insert a TABLE skeleton" (interactive "nNumber of rows: \nnNumber of columns: \n") (newline) @@ -44,8 +44,8 @@ (newline) (backward-char 5))
-(defun context-insert-FLOW-cell (n) - ;; Johan Sandblom 060128 +(defun context-insert-FLOW-cells (n) + ;; Johan Sandblom 2006-01-28 "Insert a FLOWchart cell" (interactive "nNumber of cells: \n") (newline) @@ -62,8 +62,7 @@
(add-hook 'ConTeXt-mode-hook '(lambda () - (local-set-key "\C-c\C-fc" 'context-insert-FLOW-cells) + (define-key (current-local-map) "\C-cnF" 'context-insert-FLOW-cells) (local-set-key "\C-cnr" 'context-insert-nattab-row) (local-set-key "\C-cnc" 'context-insert-nattab-column) - (local-set-key "\C-cnn" 'context-insert-nattab) - (local-set-key "$" 'start-context-math))) + (local-set-key "\C-cnn" 'context-insert-nattab)))
-- Johan Sandblom N8, MRC, Karolinska sjh t +46851776108 17176 Stockholm m +46735521477 Sweden "What is wanted is not the will to believe, but the will to find out, which is the exact opposite" - Bertrand Russell
Yes, I have 11.55. I will adopt your suggestion to avoid updating
problems. Still, strange though, seems to be an old feature. Do you
know a convenient way of finding where these bindings are defined?
Perhaps it is a matter of order of precedence differing between the
emacs versions.
Johan
2006/4/20, Sanjoy Mahajan
C-c C-f is not bound to TeX-font for me, which is odd.
Maybe an earlier AUCTeX? I have 11.82 (from Debian testing/unstable).
-Sanjoy
`Never underestimate the evil of which men of power are capable.' --Bertrand Russell, _War Crimes in Vietnam_, chapter 1.
-- Johan Sandblom N8, MRC, Karolinska sjh t +46851776108 17176 Stockholm m +46735521477 Sweden "What is wanted is not the will to believe, but the will to find out, which is the exact opposite" - Bertrand Russell
participants (2)
-
Johan Sandblom
-
Sanjoy Mahajan