| 1 |
;; This file contains extensions to GNU-Emacs, to wit: |
| 2 |
; (1) some WEB-oriented functions that are also of general use |
| 3 |
; (2) changes to the GNU-distributed TeX mode |
| 4 |
; (3) definitions of simple WEB and CWEB modes |
| 5 |
|
| 6 |
; To use: Put this in your EMACS-lisp library and say (load-library "cweb") |
| 7 |
; in your .emacs init file. |
| 8 |
|
| 9 |
; Contributed by Don Knuth, July 1990 |
| 10 |
|
| 11 |
;; OK, here's part (1): some WEB-oriented functions whose main purpose is |
| 12 |
; to maintain a stack of module names that are "pending" as you are writing |
| 13 |
; a program. When you first think of a module that needs to be written later, |
| 14 |
; put it into the pending list (by typing CTL-Z instead of @> after the |
| 15 |
; name). Later you can say CTL-\ to retrieve a pending name (and if |
| 16 |
; you want to cycle through the pending names, ESC-y after CTL-\ will |
| 17 |
; do it, just as ESC-y works after a yank). |
| 18 |
; After you've said CTL-\, the current region is the name just removed from |
| 19 |
; the pending list. If you change your mind, you can put it back again by |
| 20 |
; saying ESC-\. If you had put it into the pending list by mistake, you |
| 21 |
; can get rid of it by using the normal CTL-W operation (kill-region). |
| 22 |
; The following code binds the new commands to CTL-Z, CTL-\, and ESC-\ |
| 23 |
; in all modes. You may prefer other bindings, of course. |
| 24 |
; CTL-Z is normally "suspend emacs", but it is best not used when emacs |
| 25 |
; has its own window as it usually does nowadays; if you need the |
| 26 |
; old CTL-Z, you might rather bind it to CTL-X CTL-Z. |
| 27 |
; CTL-\ is normally undefined. |
| 28 |
; ESC-\ is normally "delete space", but ESC-space DEL does that easily too. |
| 29 |
|
| 30 |
(defvar pending-list nil |
| 31 |
"List of strings (usually WEB module names) still pending.") |
| 32 |
|
| 33 |
(defun into-pending-list (beg end) |
| 34 |
"Copy region into pending-list." |
| 35 |
(interactive "r") |
| 36 |
(indicate-region) |
| 37 |
(setq pending-list (cons (buffer-substring beg end) pending-list))) |
| 38 |
|
| 39 |
(defun new-module-name-pending () |
| 40 |
"Insert @> to complete a module name, then put it into pending-list." |
| 41 |
(interactive) |
| 42 |
(insert "@>") |
| 43 |
(push-mark) |
| 44 |
(if (search-backward "@<" nil t) |
| 45 |
(progn |
| 46 |
(exchange-point-and-mark) |
| 47 |
(into-pending-list (point) (mark)) |
| 48 |
) |
| 49 |
(message "There's no @< to begin the module name!"))) |
| 50 |
(global-set-key "\C-z" 'new-module-name-pending) |
| 51 |
|
| 52 |
(defun pop-pending-list (arg) |
| 53 |
"Remove first element of pending-list and insert it as current region. |
| 54 |
With argument, put point at left; otherwise point will follow the insertion. |
| 55 |
Say \\[new-yank-pop] to replace this by another element of the list. |
| 56 |
Say \\[into-pending-list] to put it back in the list." |
| 57 |
(interactive "*P") |
| 58 |
(if (consp pending-list) |
| 59 |
(progn |
| 60 |
(push-mark (point)) |
| 61 |
(insert (car pending-list)) |
| 62 |
(setq pending-list (cdr pending-list)) |
| 63 |
(if arg |
| 64 |
(exchange-point-and-mark))) |
| 65 |
(message "Nothing is pending.") |
| 66 |
(setq this-command nil))) |
| 67 |
(global-set-key "\C-\\" 'pop-pending-list) |
| 68 |
(global-set-key "\M-\\" 'into-pending-list) |
| 69 |
|
| 70 |
(defun new-yank-pop (arg) |
| 71 |
"If previous command was \\[pop-pending-list], pop a different string; |
| 72 |
otherwise do an ordinary Meta-y." |
| 73 |
(interactive "*p") |
| 74 |
(if (eq last-command 'pop-pending-list) |
| 75 |
(let (xch) |
| 76 |
(setq xch (< (point) (mark))) |
| 77 |
(setq pending-list (append pending-list |
| 78 |
(list (buffer-substring (point) (mark))))) |
| 79 |
(delete-region (point) (mark)) |
| 80 |
(setq this-command 'pop-pending-list) |
| 81 |
(pop-pending-list xch)) |
| 82 |
(yank-pop arg))) |
| 83 |
(global-set-key "\M-y" 'new-yank-pop) |
| 84 |
|
| 85 |
(defun indicate-region () |
| 86 |
"Bounce cursor to mark and back again" |
| 87 |
(let ((point-save (point))) |
| 88 |
(unwind-protect |
| 89 |
(progn (goto-char (mark)) |
| 90 |
(sit-for 0 300)) ;; wait 300 milliseconds |
| 91 |
(goto-char point-save)))) |
| 92 |
|
| 93 |
; I prefer to change the standard copy-region command to the following, |
| 94 |
; which gives me visual feedback about what I've copied to the kill ring: |
| 95 |
(defun indicate-and-copy-region (beg end) |
| 96 |
"Indicate current region, then copy it to the kill ring." |
| 97 |
(interactive "r")(indicate-region)(copy-region-as-kill beg end)) |
| 98 |
(global-set-key "\M-w" 'indicate-and-copy-region) |
| 99 |
|
| 100 |
; Here's another convenient command, bound to the usually unused ESC-". |
| 101 |
(defun ditto (arg) |
| 102 |
"Copy ARG characters from the line above." |
| 103 |
(interactive "*p") |
| 104 |
(let (ch) |
| 105 |
(while (> arg 0) |
| 106 |
(setq temporary-goal-column (current-column)) |
| 107 |
(save-excursion |
| 108 |
(previous-line 1) |
| 109 |
(setq ch (following-char))) |
| 110 |
(insert ch) |
| 111 |
(setq arg (1- arg))))) |
| 112 |
(global-set-key "\M-\"" 'ditto) |
| 113 |
; If "ditto" suddenly fails to work, you have probably set goal-column |
| 114 |
; inadvertently. To unset it, say C-u C-x C-n. |
| 115 |
|
| 116 |
;; OK, here's part (2): Changes to TeX mode. |
| 117 |
; The WEB modes below are very much like TeX mode, but some improvements were |
| 118 |
; desirable in TeX mode: |
| 119 |
; I made newline act as it does in indented-text mode, since this |
| 120 |
; works nicely for both TeX and WEB (Pascal or C code). |
| 121 |
; I made RET check for unmatched delimiters if it ends a paragraph. |
| 122 |
; Otherwise TeX mode remains as it was before. |
| 123 |
|
| 124 |
(setq TeX-mode-map (make-sparse-keymap)) |
| 125 |
(define-key TeX-mode-map "\C-c\C-k" 'TeX-kill-job) |
| 126 |
(define-key TeX-mode-map "\C-c\C-l" 'TeX-recenter-output-buffer) |
| 127 |
(define-key TeX-mode-map "\C-c\C-q" 'TeX-show-print-queue) |
| 128 |
(define-key TeX-mode-map "\C-c\C-p" 'TeX-print) |
| 129 |
(define-key TeX-mode-map "\"" 'TeX-insert-quote) |
| 130 |
(define-key TeX-mode-map "\e}" 'up-list) |
| 131 |
(define-key TeX-mode-map "\e{" 'TeX-insert-braces) |
| 132 |
(define-key TeX-mode-map "\C-c\C-r" 'TeX-region) |
| 133 |
(define-key TeX-mode-map "\C-c\C-b" 'TeX-buffer) |
| 134 |
(define-key TeX-mode-map "\C-c\C-f" 'TeX-close-LaTeX-block) |
| 135 |
(define-key TeX-mode-map "\r" 'TeX-newline) |
| 136 |
(define-key TeX-mode-map "\t" 'indent-relative) |
| 137 |
(setq TeX-mode-hook '(lambda () |
| 138 |
(make-local-variable 'indent-line-function) |
| 139 |
(setq indent-line-function 'indent-relative-maybe))) |
| 140 |
|
| 141 |
(defun TeX-newline (arg) |
| 142 |
"If previous character is newline and no ARG, check for unbalanced braces |
| 143 |
and/or dollar signs in previous paragraph. If ARG is \\[universal-argument], |
| 144 |
do a single newline; otherwise do ordinary newline." |
| 145 |
(interactive "*P") |
| 146 |
(if (and (eq (preceding-char) ?\n) (not arg)) |
| 147 |
(TeX-check-paragraph) |
| 148 |
(if (listp arg) |
| 149 |
(newline) |
| 150 |
(newline arg)))) |
| 151 |
|
| 152 |
(defun TeX-check-paragraph () |
| 153 |
"Insert a newline following a newline, breaking a paragraph for TeX. |
| 154 |
Check for mismatched delimiters in paragraph being terminated." |
| 155 |
(interactive) |
| 156 |
(if (TeX-validate-paragraph |
| 157 |
(save-excursion |
| 158 |
(search-backward "\n\n" nil 'move) |
| 159 |
(point)) |
| 160 |
(point)) |
| 161 |
(insert ?\n) |
| 162 |
(insert ?\n) |
| 163 |
(error "Mismatched delimiters in that paragraph?"))) |
| 164 |
|
| 165 |
;; and now, part (3): WEB and CWEB modes. |
| 166 |
; These are like plain TeX mode except that the automatic conversion of |
| 167 |
; " to `` or '' is disabled. (Personally I never liked that feature anyway, |
| 168 |
; since it's easy to get used to typing `` and ''. In WEB modes, the |
| 169 |
; feature soon becomes intolerable, unless you never use string constants!) |
| 170 |
; Another thing distinguishing WEB mode from TeX is ESC-p and ESC-n, to |
| 171 |
; move to previous or next module. These keys are usually unbound, except |
| 172 |
; when processing email. |
| 173 |
|
| 174 |
(defun forward-module (arg) |
| 175 |
"Advance past next WEB module beginning; with ARG, repeat ARG times." |
| 176 |
(interactive "p") |
| 177 |
(move-to-module arg)) |
| 178 |
(defun backward-module (arg) |
| 179 |
"Advance to previous WEB module beginning; with ARG, repeat ARG times." |
| 180 |
(interactive "p") |
| 181 |
(move-to-module (- arg))) |
| 182 |
(defun move-to-module (arg) |
| 183 |
(while (> arg 0) |
| 184 |
(re-search-forward "@ \\|@\\*\\|@\n") |
| 185 |
(setq arg (1- arg))) |
| 186 |
(while (< arg 0) |
| 187 |
(re-search-backward "@ \\|@\\*\\|@\n") |
| 188 |
(setq arg (1+ arg)))) |
| 189 |
|
| 190 |
(defun web-mode () |
| 191 |
"Major mode like TeX mode plus \\[forward-module] and \\[backward-module] |
| 192 |
for relative module movement. The automatic \" feature is disabled." |
| 193 |
(interactive) |
| 194 |
(plain-tex-mode) |
| 195 |
(local-set-key "\M-n" 'forward-module) |
| 196 |
(local-set-key "\M-p" 'backward-module) |
| 197 |
(local-set-key "\"" 'self-insert-command) |
| 198 |
(setq mode-name "WEB") |
| 199 |
(setq major-mode 'web-mode) |
| 200 |
(run-hooks 'web-mode-hook)) |
| 201 |
(setq auto-mode-alist (cons '("\\.web$" . web-mode) auto-mode-alist)) |
| 202 |
|
| 203 |
(defun cweb-mode () |
| 204 |
"Major mode like TeX mode plus \\[forward-module] and \\[backward-module] |
| 205 |
for relative module movement. The automatic \" feature is disabled." |
| 206 |
(interactive) |
| 207 |
(plain-tex-mode) |
| 208 |
(local-set-key "\M-n" 'forward-module) |
| 209 |
(local-set-key "\M-p" 'backward-module) |
| 210 |
(local-set-key "\"" 'self-insert-command) |
| 211 |
(setq comment-start nil) |
| 212 |
(modify-syntax-entry ?% "@") |
| 213 |
(setq mode-name "CWEB") |
| 214 |
(setq major-mode 'cweb-mode) |
| 215 |
(setq tex-fontify-script nil) ;; needed in GNU Emacs version 22? |
| 216 |
(run-hooks 'cweb-mode-hook)) |
| 217 |
(setq auto-mode-alist (cons '("\\.w$" . cweb-mode) auto-mode-alist)) |
| 218 |
(setq auto-mode-alist (cons '("\\.ch$" . cweb-mode) auto-mode-alist)) |