ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* .emacs tricks for indenting TeX code
@ 2002-02-18  4:11 Ed L Cashin
  2002-02-18  4:56 ` Ed L Cashin
  0 siblings, 1 reply; 2+ messages in thread
From: Ed L Cashin @ 2002-02-18  4:11 UTC (permalink / raw)


This is probably only interesting to emacs users who, like me, have
never liked the way indentation works in emacs' tex-mode.  I want it
to work like this:

  * if I hit tab on a blank line, I want to indent, even if I'm right
    below a blank line -- that's why I hit the tab key!

  * if I hit tab under an indented line, I want to indent to the
    beginning of the previous line.

  * using four-space tab stops so tex looks good

... and I can always use M-i to indent further that the previous
line. 

Here's some emacs lisp code that does all that (try it on
non-essential files first -- I'm no lisp guru).  It's based on stuff
in indent.el, and I haven't really optimized it for style.  ;)

(defun indent-tab-stop-or-relative (&optional unindented-ok)
"indent even under blank line.  indent to beginning of last line if
non-blank"
  (interactive "P")
  (if (and abbrev-mode
	   (eq (char-syntax (preceding-char)) ?w))
      (expand-abbrev))
  (let ((start-column (current-column))
	(indent nil))
    (save-excursion
      (beginning-of-line)
      ;; if preceding line is not blank
      (if (not (re-search-backward "^[ \t]*\n" nil t))
	  (progn
	    (forward-line -1)
	    (move-to-column start-column)
	    ;; Is start-column inside a tab on this line?
	    (if (> (current-column) start-column)
		(backward-char 1))
	    (if (looking-at "[ \t]")
		(progn
		  (skip-chars-forward " \t")
		  (setq indent (current-column)))))))
    (let ((opoint (point-marker)))
      (delete-horizontal-space)
      (if indent
	  (indent-to indent)
	(tab-to-tab-stop))
      (if (> opoint (point))
	  (goto-char opoint))
      (move-marker opoint nil))))

; (setq tex-mode-hook nil)
(add-hook
 'tex-mode-hook
 '(lambda ()
    "my own tex stuff.  enough misery!!!"
    ; (let ((k (make-sparse-keymap))
    (make-local-variable 'tab-stop-list)
    (setq tab-stop-list			; four-space stops
	  '(4 8 12 16 20 24 28 32 36 40 44 48 52 56 60
	      64 68 72 76 80 84 88 92 96 100 104 108 112
	      116 120 124 128 132 136 140 144 148 152 156
	      160 164 168 172 176 180 184 188 192 196 200
	      204 208 212 216 220 224 228 232 236 240))
    (make-local-variable 'indent-line-function)
    (setq indent-line-function 'indent-tab-stop-or-relative)
    (local-set-key "\C-j" 'newline-and-indent)
    (local-set-key "\t" 'indent-tab-stop-or-relative)))

-- 
--Ed L Cashin            |   PGP public key:
  ecashin@uga.edu        |   http://noserose.net/e/pgp/


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: .emacs tricks for indenting TeX code
  2002-02-18  4:11 .emacs tricks for indenting TeX code Ed L Cashin
@ 2002-02-18  4:56 ` Ed L Cashin
  0 siblings, 0 replies; 2+ messages in thread
From: Ed L Cashin @ 2002-02-18  4:56 UTC (permalink / raw)


Ed L Cashin <ecashin@uga.edu> writes:

...
> Here's some emacs lisp code that does all that (try it on
> non-essential files first -- I'm no lisp guru).  It's based on stuff
> in indent.el, and I haven't really optimized it for style.  ;)

When I added a context-specific feature to indent after the opening
square bracket on a preceding line, I noticed that the original was
overly complex and a bit wrong.  This is the last unsolicited version
I'll post to the list.  Any other changes will go on the web
somewhere. 

;;;; my indention function
(defun indent-tab-stop-or-relative (&optional unindented-ok)
"indent even under blank line.  indent to beginning of last line if
non-blank"
  (interactive "P")
  (if (and abbrev-mode
	   (eq (char-syntax (preceding-char)) ?w))
      (expand-abbrev))
  (let ((indent nil))
    (save-excursion
      (beginning-of-line)
      ;; if we're not on the first line
      (if (save-excursion (re-search-backward "\n" nil t))
	  (progn
	    (forward-line -1)
	    (if (looking-at "[[ \t]")
		(progn
		  (skip-chars-forward " \t[")
		  (setq indent (current-column)))))))
    (let ((opoint (point-marker)))
      (delete-horizontal-space)
      (if indent
	  (indent-to indent)
	(tab-to-tab-stop))
      (if (> opoint (point))
	  (goto-char opoint))
      (move-marker opoint nil))))

... this part was OK:

> ; (setq tex-mode-hook nil)
> (add-hook
>  'tex-mode-hook
>  '(lambda ()
>     "my own tex stuff.  enough misery!!!"
>     ; (let ((k (make-sparse-keymap))
>     (make-local-variable 'tab-stop-list)
>     (setq tab-stop-list			; four-space stops
> 	  '(4 8 12 16 20 24 28 32 36 40 44 48 52 56 60
> 	      64 68 72 76 80 84 88 92 96 100 104 108 112
> 	      116 120 124 128 132 136 140 144 148 152 156
> 	      160 164 168 172 176 180 184 188 192 196 200
> 	      204 208 212 216 220 224 228 232 236 240))
>     (make-local-variable 'indent-line-function)
>     (setq indent-line-function 'indent-tab-stop-or-relative)
>     (local-set-key "\C-j" 'newline-and-indent)
>     (local-set-key "\t" 'indent-tab-stop-or-relative)))

-- 
--Ed L Cashin            |   PGP public key:
  ecashin@uga.edu        |   http://noserose.net/e/pgp/


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2002-02-18  4:56 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-02-18  4:11 .emacs tricks for indenting TeX code Ed L Cashin
2002-02-18  4:56 ` Ed L Cashin

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).