aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEric S. Raymond1992-07-17 06:48:03 +0000
committerEric S. Raymond1992-07-17 06:48:03 +0000
commit72c0ae01d68e82fb8902db33afb668946527778a (patch)
tree4714b9403694580a0bfb452c33bd114d07dccc87
parentebb9e16f9893959a7a8036ed62b41e0667320874 (diff)
downloademacs-72c0ae01d68e82fb8902db33afb668946527778a.tar.gz
emacs-72c0ae01d68e82fb8902db33afb668946527778a.zip
Initial revision
-rw-r--r--lisp/mail/mail-extr.el1469
-rw-r--r--lisp/progmodes/make-mode.el1043
-rw-r--r--lisp/textmodes/sgml-mode.el266
-rw-r--r--lisp/textmodes/two-column.el646
4 files changed, 3424 insertions, 0 deletions
diff --git a/lisp/mail/mail-extr.el b/lisp/mail/mail-extr.el
new file mode 100644
index 00000000000..57ed623485f
--- /dev/null
+++ b/lisp/mail/mail-extr.el
@@ -0,0 +1,1469 @@
1;;; mail-extr.el --- extract full name and address from RFC 822 mail header.
2
3;; Author: Joe Wells <jbw@cs.bu.edu>
4;; Last-Modified: 7 Apr 1992
5;; Version: 1.0
6;; Adapted-By: ESR
7;; Keywords: mail
8
9;; Copyright (C) 1992 Free Software Foundation, Inc.
10
11;; This file is part of GNU Emacs.
12
13;; GNU Emacs is free software; you can redistribute it and/or modify
14;; it under the terms of the GNU General Public License as published by
15;; the Free Software Foundation; either version 1, or (at your option)
16;; any later version.
17
18;; GNU Emacs is distributed in the hope that it will be useful,
19;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21;; GNU General Public License for more details.
22
23;; You should have received a copy of the GNU General Public License
24;; along with GNU Emacs; see the file COPYING. If not, write to
25;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
26
27;;; Commentary:
28
29;; Here is `mail-extr', a package for extracting full names and canonical
30;; addresses from RFC 822 mail headers. It is intended to be hooked into
31;; other Emacs Lisp packages that deal with RFC 822 format messages, such as
32;; Gnews, GNUS, RMAIL, MH-E, BBDB, VM, Supercite, etc. Thus, this release is
33;; mainly for Emacs Lisp developers.
34
35;; There are two main benefits:
36
37;; 1. Higher probability of getting the correct full name for a human than
38;; any other package I know of. (On the other hand, it will cheerfully
39;; mangle non-human names/comments.)
40;; 2. Address part is put in a canonical form.
41
42;; The interface is not yet carved in stone; please give me suggestions.
43
44;; I have an extensive test-case collection of funny addresses if you want to
45;; work with the code. Developing this code requires frequent testing to
46;; make sure you're not breaking functionality. I'm not posting the
47;; test-cases because they take over 100K.
48
49;; If you find an address that mail-extr fails on, please send it to me along
50;; with what you think the correct results should be. I do not consider it a
51;; bug if mail-extr mangles a comment that does not correspond to a real
52;; human full name, although I would prefer that mail-extr would return the
53;; comment as-is.
54
55;; Features:
56
57;; * Full name handling:
58
59;; * knows where full names can be found in an address.
60;; * avoids using empty comments and quoted text.
61;; * extracts full names from mailbox names.
62;; * recognizes common formats for comments after a full name.
63;; * puts a period and a space after each initial.
64;; * understands & referring to the mailbox name capitalized.
65;; * strips name prefixes like "Prof.", etc..
66;; * understands what characters can occur in names (not just letters).
67;; * figures out middle initial from mailbox name.
68;; * removes funny nicknames.
69;; * keeps suffixes such as Jr., Sr., III, etc.
70;; * reorders "Last, First" type names.
71
72;; * Address handling:
73
74;; * parses rfc822 quoted text, comments, and domain literals.
75;; * parses rfc822 multi-line headers.
76;; * does something reasonable with rfc822 GROUP addresses.
77;; * handles many rfc822 noncompliant and garbage addresses.
78;; * canonicalizes addresses (after stripping comments/phrases outside <>).
79;; * converts ! addresses into .UUCP and %-style addresses.
80;; * converts rfc822 ROUTE addresses to %-style addresses.
81;; * truncates %-style addresses at leftmost fully qualified domain name.
82;; * handles local relative precedence of ! vs. % and @ (untested).
83
84;; It does almost no string creation. It primarily uses the built-in
85;; parsing routines with the appropriate syntax tables. This should
86;; result in greater speed.
87
88;; TODO:
89
90;; * handle all test cases. (This will take forever.)
91;; * software to pick the correct header to use (eg., "Senders-Name:").
92;; * multiple addresses in the "From:" header (almost all of the necessary
93;; code is there).
94;; * flag to not treat `,' as an address separator. (This is useful when
95;; there is a "From:" header but no "Sender:" header, because then there
96;; is only allowed to be one address.)
97;; * mailbox name does not necessarily contain full name.
98;; * fixing capitalization when it's all upper or lowercase. (Hard!)
99;; * some of the domain literal handling is missing. (But I've never even
100;; seen one of these in a mail address, so maybe no big deal.)
101;; * arrange to have syntax tables byte-compiled.
102;; * speed hacks.
103;; * delete unused variables.
104;; * arrange for testing with different relative precedences of ! vs. @
105;; and %.
106;; * put variant-method back into mail-extract-address-components.
107;; * insert documentation strings!
108;; * handle X.400-gatewayed addresses according to RFC 1148.
109
110;;; Change Log:
111;;
112;; Mon Apr 6 23:59:09 1992 Joe Wells (jbw at bigbird.bu.edu)
113;;
114;; * Cleaned up some more. Release version 1.0 to world.
115;;
116;; Sun Apr 5 19:39:08 1992 Joe Wells (jbw at bigbird.bu.edu)
117;;
118;; * Cleaned up full name extraction extensively.
119;;
120;; Sun Feb 2 14:45:24 1992 Joe Wells (jbw at bigbird.bu.edu)
121;;
122;; * Total rewrite. Integrated mail-canonicalize-address into
123;; mail-extract-address-components. Now handles GROUP addresses more
124;; or less correctly. Better handling of lots of different cases.
125;;
126;; Fri Jun 14 19:39:50 1991
127;; * Created.
128
129;;; Code:
130
131;; Variable definitions.
132
133(defvar mail-@-binds-tighter-than-! nil)
134
135;;----------------------------------------------------------------------
136;; what orderings are meaningful?????
137;;(defvar mail-operator-precedence-list '(?! ?% ?@))
138;; Right operand of a % or a @ must be a domain name, period. No other
139;; operators allowed. Left operand of a @ is an address relative to that
140;; site.
141
142;; Left operand of a ! must be a domain name. Right operand is an
143;; arbitrary address.
144;;----------------------------------------------------------------------
145
146(defconst mail-space-char 32)
147
148(defconst mail-whitespace " \t\n")
149
150;; Any character that can occur in a name in an RFC822 address.
151;; Yes, there are weird people with digits in their names.
152(defconst mail-all-letters "A-Za-z---{|}'~0-9`.")
153
154;; Any character that can occur in a name, not counting characters that
155;; separate parts of a multipart name.
156(defconst mail-all-letters-but-separators "A-Za-z{|}'~0-9`")
157
158;; Any character that can start a name
159(defconst mail-first-letters "A-Za-z")
160
161;; Any character that can end a name.
162(defconst mail-last-letters "A-Za-z`'.")
163
164;; Matches an initial not followed by both a period and a space.
165(defconst mail-bad-initials-pattern
166 (format "\\(\\([^%s]\\|\\`\\)[%s]\\)\\(\\.\\([^ ]\\)\\| \\|\\([^%s .]\\)\\|\\'\\)"
167 mail-all-letters mail-first-letters mail-all-letters))
168
169(defconst mail-non-name-chars (concat "^" mail-all-letters "."))
170
171(defconst mail-non-begin-name-chars (concat "^" mail-first-letters))
172
173(defconst mail-non-end-name-chars (concat "^" mail-last-letters))
174
175;; Matches periods used instead of spaces. Must not match the period
176;; following an initial.
177(defconst mail-bad-\.-pattern
178 (format "\\([%s][%s]\\)\\.+\\([%s]\\)"
179 mail-all-letters mail-last-letters mail-first-letters))
180
181;; Matches an embedded or leading nickname that should be removed.
182(defconst mail-nickname-pattern
183 (format "\\([ .]\\|\\`\\)[\"'`\[\(]\\([ .%s]+\\)[\]\"'\)] "
184 mail-all-letters))
185
186;; Matches a leading title that is not part of the name (does not
187;; contribute to uniquely identifying the person).
188(defconst mail-full-name-prefixes
189 '"\\` *\\(Prof\\|Dr\\|Mrs?\\|Rev\\|Rabbi\\|SysOp\\|LCDR\\)\\.? ")
190
191;; Matches the occurrence of a generational name suffix, and the last
192;; character of the preceding name.
193(defconst mail-full-name-suffix-pattern
194 (format
195 "\\(,? ?\\([JjSs]r\\.?\\|V?I+V?\\)\\)\\([^%s]\\([^%s]\\|\\'\\)\\|\\'\\)"
196 mail-all-letters mail-all-letters))
197
198(defconst mail-roman-numeral-pattern
199 "V?I+V?\\b")
200
201;; Matches a trailing uppercase (with other characters possible) acronym.
202;; Must not match a trailing uppercase last name or trailing initial
203(defconst mail-weird-acronym-pattern "\\([A-Z]+[-_/]\\|[A-Z][A-Z][A-Z]?\\b\\)")
204
205;; Matches a mixed-case or lowercase name (not an initial).
206(defconst mail-mixed-case-name-pattern
207 (format
208 "\\b\\([a-z][%s]*[%s]\\|[%s][%s]*[a-z][%s]*[%s]\\|[%s][%s]*[a-z]\\)"
209 mail-all-letters mail-last-letters
210 mail-first-letters mail-all-letters mail-all-letters mail-last-letters
211 mail-first-letters mail-all-letters))
212
213;; Matches a trailing alternative address.
214(defconst mail-alternative-address-pattern "[a-zA-Z.]+[!@][a-zA-Z.]")
215
216;; Matches a variety of trailing comments not including comma-delimited
217;; comments.
218(defconst mail-trailing-comment-start-pattern " [-{]\\|--\\|[+@#></\;]")
219
220;; Matches a name (not an initial).
221;; This doesn't force a word boundary at the end because sometimes a
222;; comment is separated by a `-' with no preceding space.
223(defconst mail-name-pattern
224 (format
225 "\\b[%s][%s]*[%s]"
226 mail-first-letters mail-all-letters mail-last-letters))
227
228(defconst mail-initial-pattern
229 (format "\\b[%s]\\([. ]\\|\\b\\)" mail-first-letters))
230
231;; Matches a single name before a comma.
232(defconst mail-last-name-first-pattern
233 (concat "\\`" mail-name-pattern ","))
234
235;; Matches telephone extensions.
236(defconst mail-telephone-extension-pattern
237 "\\(\\([Ee]xt\\|[Tt]el\\|[Xx]\\).?\\)? *\\+?[0-9][- 0-9]+")
238
239;; Matches ham radio call signs.
240(defconst mail-ham-call-sign-pattern
241 "\\b[A-Z]+[0-9][A-Z0-9]*")
242
243;; Matches normal single-part name
244(defconst mail-normal-name-pattern
245 (format
246 "\\b[%s][%s]+[%s]"
247 mail-first-letters mail-all-letters-but-separators mail-last-letters))
248
249;; Matches normal two names with missing middle initial
250(defconst mail-two-name-pattern
251 (concat "\\`\\(" mail-normal-name-pattern
252 "\\|" mail-initial-pattern
253 "\\) +\\(" mail-normal-name-pattern "\\)\\(,\\|\\'\\)"))
254
255(defvar address-syntax-table (make-syntax-table))
256(defvar address-comment-syntax-table (make-syntax-table))
257(defvar address-domain-literal-syntax-table (make-syntax-table))
258(defvar address-text-comment-syntax-table (make-syntax-table))
259(defvar address-text-syntax-table (make-syntax-table))
260(mapcar
261 (function
262 (lambda (pair)
263 (let ((syntax-table (symbol-value (car pair))))
264 (mapcar
265 (function
266 (lambda (item)
267 (if (eq 2 (length item))
268 (modify-syntax-entry (car item) (car (cdr item)) syntax-table)
269 (let ((char (car item))
270 (bound (car (cdr item)))
271 (syntax (car (cdr (cdr item)))))
272 (while (<= char bound)
273 (modify-syntax-entry char syntax syntax-table)
274 (setq char (1+ char)))))))
275 (cdr pair)))))
276 '((address-syntax-table
277 (0 31 "w") ;control characters
278 (32 " ") ;SPC
279 (?! ?~ "w") ;printable characters
280 (127 "w") ;DEL
281 (128 255 "w") ;high-bit-on characters
282 (?\t " ")
283 (?\r " ")
284 (?\n " ")
285 (?\( ".")
286 (?\) ".")
287 (?< ".")
288 (?> ".")
289 (?@ ".")
290 (?, ".")
291 (?\; ".")
292 (?: ".")
293 (?\\ "\\")
294 (?\" "\"")
295 (?. ".")
296 (?\[ ".")
297 (?\] ".")
298 ;; % and ! aren't RFC822 characters, but it is convenient to pretend
299 (?% ".")
300 (?! ".")
301 )
302 (address-comment-syntax-table
303 (0 255 "w")
304 (?\( "\(\)")
305 (?\) "\)\(")
306 (?\\ "\\"))
307 (address-domain-literal-syntax-table
308 (0 255 "w")
309 (?\[ "\(\]") ;??????
310 (?\] "\)\[") ;??????
311 (?\\ "\\"))
312 (address-text-comment-syntax-table
313 (0 255 "w")
314 (?\( "\(\)")
315 (?\) "\)\(")
316 (?\[ "\(\]")
317 (?\] "\)\[")
318 (?\{ "\(\}")
319 (?\} "\)\{")
320 (?\\ "\\")
321 (?\" "\"")
322 ;; (?\' "\)\`")
323 ;; (?\` "\(\'")
324 )
325 (address-text-syntax-table
326 (0 255 ".")
327 (?A ?Z "w")
328 (?a ?z "w")
329 (?- "w")
330 (?\} "w")
331 (?\{ "w")
332 (?| "w")
333 (?\' "w")
334 (?~ "w")
335 (?0 ?9 "w"))
336 ))
337
338
339;; Utility functions and macros.
340
341(defmacro undo-backslash-quoting (beg end)
342 (`(save-excursion
343 (save-restriction
344 (narrow-to-region (, beg) (, end))
345 (goto-char (point-min))
346 ;; undo \ quoting
347 (while (re-search-forward "\\\\\\(.\\)" nil t)
348 (replace-match "\\1")
349 ;; CHECK: does this leave point after the replacement?
350 )))))
351
352(defmacro mail-nuke-char-at (pos)
353 (` (save-excursion
354 (goto-char (, pos))
355 (delete-char 1)
356 (insert mail-space-char))))
357
358(defmacro mail-nuke-elements-outside-range (list-symbol beg-symbol end-symbol
359 &optional no-replace)
360 (` (progn
361 (setq temp (, list-symbol))
362 (while temp
363 (cond ((or (> (car temp) (, end-symbol))
364 (< (car temp) (, beg-symbol)))
365 (, (or no-replace
366 (` (mail-nuke-char-at (car temp)))))
367 (setcar temp nil)))
368 (setq temp (cdr temp)))
369 (setq (, list-symbol) (delq nil (, list-symbol))))))
370
371(defun mail-demarkerize (marker)
372 (and marker
373 (if (markerp marker)
374 (let ((temp (marker-position marker)))
375 (set-marker marker nil)
376 temp)
377 marker)))
378
379(defun mail-markerize (pos)
380 (and pos
381 (if (markerp pos)
382 pos
383 (copy-marker pos))))
384
385(defmacro mail-last-element (list)
386 "Return last element of LIST."
387 (` (let ((list (, list)))
388 (while (not (null (cdr list)))
389 (setq list (cdr list)))
390 (car list))))
391
392(defmacro safe-move-sexp (arg)
393 "Safely skip over one balanced sexp, if there is one. Return t if success."
394 (` (condition-case error
395 (progn
396 (goto-char (scan-sexps (point) (, arg)))
397 t)
398 (error
399 (if (string-equal (nth 1 error) "Unbalanced parentheses")
400 nil
401 (while t
402 (signal (car error) (cdr error))))))))
403
404
405;; The main function to grind addresses
406
407(defun mail-extract-address-components (address)
408 "Given an rfc 822 ADDRESS, extract full name and canonical address.
409Returns a list of the form (FULL-NAME CANONICAL-ADDRESS)."
410 (let ((canonicalization-buffer (get-buffer-create "*canonical address*"))
411 (extraction-buffer (get-buffer-create "*extract address components*"))
412 (foo 'bar)
413 char
414 multiple-addresses
415 <-pos >-pos @-pos :-pos ,-pos !-pos %-pos \;-pos
416 group-:-pos group-\;-pos route-addr-:-pos
417 record-pos-symbol
418 first-real-pos last-real-pos
419 phrase-beg phrase-end
420 comment-beg comment-end
421 quote-beg quote-end
422 atom-beg atom-end
423 mbox-beg mbox-end
424 \.-ends-name
425 temp
426 name-suffix
427 saved-point
428 fi mi li
429 saved-%-pos saved-!-pos saved-@-pos
430 domain-pos \.-pos insert-point)
431
432 (save-excursion
433 (set-buffer extraction-buffer)
434 (buffer-flush-undo extraction-buffer)
435 (set-syntax-table address-syntax-table)
436 (widen)
437 (erase-buffer)
438 (setq case-fold-search nil)
439
440 ;; Insert extra space at beginning to allow later replacement with <
441 ;; without having to move markers.
442 (insert mail-space-char address)
443
444 ;; stolen from rfc822.el
445 ;; Unfold multiple lines.
446 (goto-char (point-min))
447 (while (re-search-forward "\\([^\\]\\(\\\\\\\\\\)*\\)\n[ \t]" nil t)
448 (replace-match "\\1 " t))
449
450 ;; first pass grabs useful information about address
451 (goto-char (point-min))
452 (while (progn
453 (skip-chars-forward mail-whitespace)
454 (not (eobp)))
455 (setq char (char-after (point)))
456 (or first-real-pos
457 (if (not (eq char ?\())
458 (setq first-real-pos (point))))
459 (cond
460 ;; comment
461 ((eq char ?\()
462 (set-syntax-table address-comment-syntax-table)
463 ;; only record the first non-empty comment's position
464 (if (and (not comment-beg)
465 (save-excursion
466 (forward-char 1)
467 (skip-chars-forward mail-whitespace)
468 (not (eq ?\) (char-after (point))))))
469 (setq comment-beg (point)))
470 ;; TODO: don't record if unbalanced
471 (or (safe-move-sexp 1)
472 (forward-char 1))
473 (set-syntax-table address-syntax-table)
474 (if (and comment-beg
475 (not comment-end))
476 (setq comment-end (point))))
477 ;; quoted text
478 ((eq char ?\")
479 ;; only record the first non-empty quote's position
480 (if (and (not quote-beg)
481 (save-excursion
482 (forward-char 1)
483 (skip-chars-forward mail-whitespace)
484 (not (eq ?\" (char-after (point))))))
485 (setq quote-beg (point)))
486 ;; TODO: don't record if unbalanced
487 (or (safe-move-sexp 1)
488 (forward-char 1))
489 (if (and quote-beg
490 (not quote-end))
491 (setq quote-end (point))))
492 ;; domain literals
493 ((eq char ?\[)
494 (set-syntax-table address-domain-literal-syntax-table)
495 (or (safe-move-sexp 1)
496 (forward-char 1))
497 (set-syntax-table address-syntax-table))
498 ;; commas delimit addresses when outside < > pairs.
499 ((and (eq char ?,)
500 (or (null <-pos)
501 (and >-pos
502 ;; handle weird munged addresses
503 (> (mail-last-element <-pos) (car >-pos)))))
504 (setq multiple-addresses t)
505 (delete-char 1)
506 (narrow-to-region (point-min) (point)))
507 ;; record the position of various interesting chars, determine
508 ;; legality later.
509 ((setq record-pos-symbol
510 (cdr (assq char
511 '((?< . <-pos) (?> . >-pos) (?@ . @-pos)
512 (?: . :-pos) (?, . ,-pos) (?! . !-pos)
513 (?% . %-pos) (?\; . \;-pos)))))
514 (set record-pos-symbol
515 (cons (point) (symbol-value record-pos-symbol)))
516 (forward-char 1))
517 ((eq char ?.)
518 (forward-char 1))
519 ((memq char '(
520 ;; comment terminator illegal
521 ?\)
522 ;; domain literal terminator illegal
523 ?\]
524 ;; \ allowed only within quoted strings,
525 ;; domain literals, and comments
526 ?\\
527 ))
528 (mail-nuke-char-at (point))
529 (forward-char 1))
530 (t
531 (forward-word 1)))
532 (or (eq char ?\()
533 (setq last-real-pos (point))))
534
535 ;; Use only the leftmost <, if any. Replace all others with spaces.
536 (while (cdr <-pos)
537 (mail-nuke-char-at (car <-pos))
538 (setq <-pos (cdr <-pos)))
539
540 ;; Use only the rightmost >, if any. Replace all others with spaces.
541 (while (cdr >-pos)
542 (mail-nuke-char-at (nth 1 >-pos))
543 (setcdr >-pos (nthcdr 2 >-pos)))
544
545 ;; If multiple @s and a :, but no < and >, insert around buffer.
546 ;; This commonly happens on the UUCP "From " line. Ugh.
547 (cond ((and (> (length @-pos) 1)
548 :-pos ;TODO: check if between @s
549 (not <-pos))
550 (goto-char (point-min))
551 (delete-char 1)
552 (setq <-pos (list (point)))
553 (insert ?<)))
554
555 ;; If < but no >, insert > in rightmost possible position
556 (cond ((and <-pos
557 (null >-pos))
558 (goto-char (point-max))
559 (setq >-pos (list (point)))
560 (insert ?>)))
561
562 ;; If > but no <, replace > with space.
563 (cond ((and >-pos
564 (null <-pos))
565 (mail-nuke-char-at (car >-pos))
566 (setq >-pos nil)))
567
568 ;; Turn >-pos and <-pos into non-lists
569 (setq >-pos (car >-pos)
570 <-pos (car <-pos))
571
572 ;; Trim other punctuation lists of items outside < > pair to handle
573 ;; stupid MTAs.
574 (cond (<-pos ; don't need to check >-pos also
575 ;; handle bozo software that violates RFC 822 by sticking
576 ;; punctuation marks outside of a < > pair
577 (mail-nuke-elements-outside-range @-pos <-pos >-pos t)
578 ;; RFC 822 says nothing about these two outside < >, but
579 ;; remove those positions from the lists to make things
580 ;; easier.
581 (mail-nuke-elements-outside-range !-pos <-pos >-pos t)
582 (mail-nuke-elements-outside-range %-pos <-pos >-pos t)))
583
584 ;; Check for : that indicates GROUP list and for : part of
585 ;; ROUTE-ADDR spec.
586 ;; Can't possibly be more than two :. Nuke any extra.
587 (while :-pos
588 (setq temp (car :-pos)
589 :-pos (cdr :-pos))
590 (cond ((and <-pos >-pos
591 (> temp <-pos)
592 (< temp >-pos))
593 (if (or route-addr-:-pos
594 (< (length @-pos) 2)
595 (> temp (car @-pos))
596 (< temp (nth 1 @-pos)))
597 (mail-nuke-char-at temp)
598 (setq route-addr-:-pos temp)))
599 ((or (not <-pos)
600 (and <-pos
601 (< temp <-pos)))
602 (setq group-:-pos temp))))
603
604 ;; Nuke any ; that is in or to the left of a < > pair or to the left
605 ;; of a GROUP starting :. Also, there may only be one ;.
606 (while \;-pos
607 (setq temp (car \;-pos)
608 \;-pos (cdr \;-pos))
609 (cond ((and <-pos >-pos
610 (> temp <-pos)
611 (< temp >-pos))
612 (mail-nuke-char-at temp))
613 ((and (or (not group-:-pos)
614 (> temp group-:-pos))
615 (not group-\;-pos))
616 (setq group-\;-pos temp))))
617
618 ;; Handle junk like ";@host.company.dom" that sendmail adds.
619 ;; **** should I remember comment positions?
620 (and group-\;-pos
621 ;; this is fine for now
622 (mail-nuke-elements-outside-range !-pos group-:-pos group-\;-pos t)
623 (mail-nuke-elements-outside-range @-pos group-:-pos group-\;-pos t)
624 (mail-nuke-elements-outside-range %-pos group-:-pos group-\;-pos t)
625 (mail-nuke-elements-outside-range ,-pos group-:-pos group-\;-pos t)
626 (and last-real-pos
627 (> last-real-pos (1+ group-\;-pos))
628 (setq last-real-pos (1+ group-\;-pos)))
629 (and comment-end
630 (> comment-end group-\;-pos)
631 (setq comment-end nil
632 comment-beg nil))
633 (and quote-end
634 (> quote-end group-\;-pos)
635 (setq quote-end nil
636 quote-beg nil))
637 (narrow-to-region (point-min) group-\;-pos))
638
639 ;; Any commas must be between < and : of ROUTE-ADDR. Nuke any
640 ;; others.
641 ;; Hell, go ahead an nuke all of the commas.
642 ;; **** This will cause problems when we start handling commas in
643 ;; the PHRASE part .... no it won't ... yes it will ... ?????
644 (mail-nuke-elements-outside-range ,-pos 1 1)
645
646 ;; can only have multiple @s inside < >. The fact that some MTAs
647 ;; put de-bracketed ROUTE-ADDRs in the UUCP-style "From " line is
648 ;; handled above.
649
650 ;; Locate PHRASE part of ROUTE-ADDR.
651 (cond (<-pos
652 (goto-char <-pos)
653 (skip-chars-backward mail-whitespace)
654 (setq phrase-end (point))
655 (goto-char (or ;;group-:-pos
656 (point-min)))
657 (skip-chars-forward mail-whitespace)
658 (if (< (point) phrase-end)
659 (setq phrase-beg (point))
660 (setq phrase-end nil))))
661
662 ;; handle ROUTE-ADDRS with real ROUTEs.
663 ;; If there are multiple @s, then we assume ROUTE-ADDR syntax, and
664 ;; any % or ! must be semantically meaningless.
665 ;; TODO: do this processing into canonicalization buffer
666 (cond (route-addr-:-pos
667 (setq !-pos nil
668 %-pos nil
669 >-pos (copy-marker >-pos)
670 route-addr-:-pos (copy-marker route-addr-:-pos))
671 (goto-char >-pos)
672 (insert-before-markers ?X)
673 (goto-char (car @-pos))
674 (while (setq @-pos (cdr @-pos))
675 (delete-char 1)
676 (setq %-pos (cons (point-marker) %-pos))
677 (insert "%")
678 (goto-char (1- >-pos))
679 (save-excursion
680 (insert-buffer-substring extraction-buffer
681 (car @-pos) route-addr-:-pos)
682 (delete-region (car @-pos) route-addr-:-pos))
683 (or (cdr @-pos)
684 (setq saved-@-pos (list (point)))))
685 (setq @-pos saved-@-pos)
686 (goto-char >-pos)
687 (delete-char -1)
688 (mail-nuke-char-at route-addr-:-pos)
689 (mail-demarkerize route-addr-:-pos)
690 (setq route-addr-:-pos nil
691 >-pos (mail-demarkerize >-pos)
692 %-pos (mapcar 'mail-demarkerize %-pos))))
693
694 ;; de-listify @-pos
695 (setq @-pos (car @-pos))
696
697 ;; TODO: remove comments in the middle of an address
698
699 (set-buffer canonicalization-buffer)
700
701 (buffer-flush-undo canonicalization-buffer)
702 (set-syntax-table address-syntax-table)
703 (setq case-fold-search nil)
704
705 (widen)
706 (erase-buffer)
707 (insert-buffer-substring extraction-buffer)
708
709 (if <-pos
710 (narrow-to-region (progn
711 (goto-char (1+ <-pos))
712 (skip-chars-forward mail-whitespace)
713 (point))
714 >-pos)
715 ;; ****** Oh no! What if the address is completely empty!
716 (narrow-to-region first-real-pos last-real-pos))
717
718 (and @-pos %-pos
719 (mail-nuke-elements-outside-range %-pos (point-min) @-pos))
720 (and %-pos !-pos
721 (mail-nuke-elements-outside-range !-pos (point-min) (car %-pos)))
722 (and @-pos !-pos (not %-pos)
723 (mail-nuke-elements-outside-range !-pos (point-min) @-pos))
724
725 ;; Error condition:?? (and %-pos (not @-pos))
726
727 (cond (!-pos
728 ;; **** I don't understand this save-restriction and the
729 ;; narrow-to-region inside it. Why did I do that?
730 (save-restriction
731 (cond ((and @-pos
732 mail-@-binds-tighter-than-!)
733 (goto-char @-pos)
734 (setq %-pos (cons (point) %-pos)
735 @-pos nil)
736 (delete-char 1)
737 (insert "%")
738 (setq insert-point (point-max)))
739 (mail-@-binds-tighter-than-!
740 (setq insert-point (point-max)))
741 (%-pos
742 (setq insert-point (mail-last-element %-pos)
743 saved-%-pos (mapcar 'mail-markerize %-pos)
744 %-pos nil
745 @-pos (mail-markerize @-pos)))
746 (@-pos
747 (setq insert-point @-pos)
748 (setq @-pos (mail-markerize @-pos)))
749 (t
750 (setq insert-point (point-max))))
751 (narrow-to-region (point-min) insert-point)
752 (setq saved-!-pos (car !-pos))
753 (while !-pos
754 (goto-char (point-max))
755 (cond ((and (not @-pos)
756 (not (cdr !-pos)))
757 (setq @-pos (point))
758 (insert-before-markers "@ "))
759 (t
760 (setq %-pos (cons (point) %-pos))
761 (insert-before-markers "% ")))
762 (backward-char 1)
763 (insert-buffer-substring
764 (current-buffer)
765 (if (nth 1 !-pos)
766 (1+ (nth 1 !-pos))
767 (point-min))
768 (car !-pos))
769 (delete-char 1)
770 (or (save-excursion
771 (safe-move-sexp -1)
772 (skip-chars-backward mail-whitespace)
773 (eq ?. (preceding-char)))
774 (insert-before-markers
775 (if (save-excursion
776 (skip-chars-backward mail-whitespace)
777 (eq ?. (preceding-char)))
778 ""
779 ".")
780 "uucp"))
781 (setq !-pos (cdr !-pos))))
782 (and saved-%-pos
783 (setq %-pos (append (mapcar 'mail-demarkerize saved-%-pos)
784 %-pos)))
785 (setq @-pos (mail-demarkerize @-pos))
786 (narrow-to-region (1+ saved-!-pos) (point-max))))
787 (cond ((and %-pos
788 (not @-pos))
789 (goto-char (car %-pos))
790 (delete-char 1)
791 (setq @-pos (point))
792 (insert "@")
793 (setq %-pos (cdr %-pos))))
794 (setq %-pos (nreverse %-pos))
795 ;; RFC 1034 doesn't approve of this, oh well:
796 (downcase-region (or (car %-pos) @-pos (point-max)) (point-max))
797 (cond (%-pos ; implies @-pos valid
798 (setq temp %-pos)
799 (catch 'truncated
800 (while temp
801 (goto-char (or (nth 1 temp)
802 @-pos))
803 (skip-chars-backward mail-whitespace)
804 (save-excursion
805 (safe-move-sexp -1)
806 (setq domain-pos (point))
807 (skip-chars-backward mail-whitespace)
808 (setq \.-pos (eq ?. (preceding-char))))
809 (cond ((and \.-pos
810 (get
811 (intern
812 (buffer-substring domain-pos (point)))
813 'domain-name))
814 (narrow-to-region (point-min) (point))
815 (goto-char (car temp))
816 (delete-char 1)
817 (setq @-pos (point))
818 (setcdr temp nil)
819 (setq %-pos (delq @-pos %-pos))
820 (insert "@")
821 (throw 'truncated t)))
822 (setq temp (cdr temp))))))
823 (setq mbox-beg (point-min)
824 mbox-end (if %-pos (car %-pos)
825 (or @-pos
826 (point-max))))
827
828 ;; Done canonicalizing address.
829
830 (set-buffer extraction-buffer)
831
832 ;; Find the full name
833
834 (cond ((and phrase-beg
835 (eq quote-beg phrase-beg)
836 (<= quote-end phrase-end))
837 (narrow-to-region (1+ quote-beg) (1- quote-end))
838 (undo-backslash-quoting (point-min) (point-max)))
839 (phrase-beg
840 (narrow-to-region phrase-beg phrase-end))
841 (comment-beg
842 (narrow-to-region (1+ comment-beg) (1- comment-end))
843 (undo-backslash-quoting (point-min) (point-max)))
844 (t
845 ;; *** Work in canon buffer instead? No, can't. Hmm.
846 (delete-region (point-min) (point-max))
847 (insert-buffer-substring canonicalization-buffer
848 mbox-beg mbox-end)
849 (goto-char (point-min))
850 (setq \.-ends-name (search-forward "_" nil t))
851 (goto-char (point-min))
852 (while (progn
853 (skip-chars-forward mail-whitespace)
854 (not (eobp)))
855 (setq char (char-after (point)))
856 (cond
857 ((eq char ?\")
858 (setq quote-beg (point))
859 (or (safe-move-sexp 1)
860 ;; TODO: handle this error condition!!!!!
861 (forward-char 1))
862 ;; take into account deletions
863 (setq quote-end (- (point) 2))
864 (save-excursion
865 (backward-char 1)
866 (delete-char 1)
867 (goto-char quote-beg)
868 (delete-char 1))
869 (undo-backslash-quoting quote-beg quote-end)
870 (or (eq mail-space-char (char-after (point)))
871 (insert " "))
872 (setq \.-ends-name t))
873 ((eq char ?.)
874 (if (eq (char-after (1+ (point))) ?_)
875 (progn
876 (forward-char 1)
877 (delete-char 1)
878 (insert mail-space-char))
879 (if \.-ends-name
880 (narrow-to-region (point-min) (point))
881 (delete-char 1)
882 (insert " "))))
883 ((memq (char-syntax char) '(?. ?\\))
884 (delete-char 1)
885 (insert " "))
886 (t
887 (setq atom-beg (point))
888 (forward-word 1)
889 (setq atom-end (point))
890 (save-restriction
891 (narrow-to-region atom-beg atom-end)
892 (goto-char (point-min))
893 (while (re-search-forward "\\([^_]+\\)_" nil t)
894 (replace-match "\\1 "))
895 (goto-char (point-max))))))))
896
897 (set-syntax-table address-text-syntax-table)
898
899 (setq xxx (variant-method (buffer-string)))
900 (delete-region (point-min) (point-max))
901 (insert xxx)
902 (goto-char (point-min))
903
904;; ;; Compress whitespace
905;; (goto-char (point-min))
906;; (while (re-search-forward "[ \t\n]+" nil t)
907;; (replace-match " "))
908;;
909;; ;; Fix . used as space
910;; (goto-char (point-min))
911;; (while (re-search-forward mail-bad-\.-pattern nil t)
912;; (replace-match "\\1 \\2"))
913;;
914;; ;; Delete trailing parenthesized comment
915;; (goto-char (point-max))
916;; (skip-chars-backward mail-whitespace)
917;; (cond ((memq (char-after (1- (point))) '(?\) ?\} ?\]))
918;; (setq comment-end (point))
919;; (set-syntax-table address-text-comment-syntax-table)
920;; (or (safe-move-sexp -1)
921;; (backward-char 1))
922;; (set-syntax-table address-text-syntax-table)
923;; (setq comment-beg (point))
924;; (skip-chars-backward mail-whitespace)
925;; (if (bobp)
926;; (narrow-to-region (1+ comment-beg) (1- comment-end))
927;; (narrow-to-region (point-min) (point)))))
928;;
929;; ;; Find, save, and delete any name suffix
930;; ;; *** Broken!
931;; (goto-char (point-min))
932;; (cond ((re-search-forward mail-full-name-suffix-pattern nil t)
933;; (setq name-suffix (buffer-substring (match-beginning 3)
934;; (match-end 3)))
935;; (replace-match "\\1 \\4")))
936;;
937;; ;; Delete ALL CAPS words and after, if preceded by mixed-case or
938;; ;; lowercase words. Eg. XT-DEM.
939;; (goto-char (point-min))
940;; ;; ## This will lose on something like "SMITH MAX".
941;; ;; ## maybe it should be
942;; ;; ## " \\([A-Z]+[-_/][A-Z]+\\|[A-Z][A-Z][A-Z]\\)\\b.*[^A-Z \t]"
943;; ;; ## that is, three-letter-upper-case-word with non-upper-case
944;; ;; ## characters following it.
945;; (if (re-search-forward mail-mixed-case-name-pattern nil t)
946;; (if (re-search-forward mail-weird-acronym-pattern nil t)
947;; (narrow-to-region (point-min) (match-beginning 0))))
948;;
949;; ;; Delete trailing alternative address
950;; (goto-char (point-min))
951;; (if (re-search-forward mail-alternative-address-pattern nil t)
952;; (narrow-to-region (point-min) (match-beginning 0)))
953;;
954;; ;; Delete trailing comment
955;; (goto-char (point-min))
956;; (if (re-search-forward mail-trailing-comment-start-pattern nil t)
957;; (or (progn
958;; (goto-char (match-beginning 0))
959;; (skip-chars-backward mail-whitespace)
960;; (bobp))
961;; (narrow-to-region (point-min) (match-beginning 0))))
962;;
963;; ;; Delete trailing comma-separated comment
964;; (goto-char (point-min))
965;; ;; ## doesn't this break "Smith, John"? Yes.
966;; (re-search-forward mail-last-name-first-pattern nil t)
967;; (while (search-forward "," nil t)
968;; (or (save-excursion
969;; (backward-char 2)
970;; (looking-at mail-full-name-suffix-pattern))
971;; (narrow-to-region (point-min) (1- (point)))))
972;;
973;; ;; Delete telephone numbers and ham radio call signs
974;; (goto-char (point-min))
975;; (if (re-search-forward mail-telephone-extension-pattern nil t)
976;; (narrow-to-region (point-min) (match-beginning 0)))
977;; (goto-char (point-min))
978;; (if (re-search-forward mail-ham-call-sign-pattern nil t)
979;; (if (eq (match-beginning 0) (point-min))
980;; (narrow-to-region (match-end 0) (point-max))
981;; (narrow-to-region (point-min) (match-beginning 0))))
982;;
983;; ;; Delete trailing word followed immediately by .
984;; (goto-char (point-min))
985;; ;; ## what's this for? doesn't it mess up "Public, Harry Q."? No.
986;; (if (re-search-forward "\\b[A-Za-z][A-Za-z]+\\. *\\'" nil t)
987;; (narrow-to-region (point-min) (match-beginning 0)))
988;;
989;; ;; Handle & substitution
990;; ;; TODO: remember to disable middle initial guessing
991;; (goto-char (point-min))
992;; (cond ((re-search-forward "\\( \\|\\`\\)&\\( \\|\\'\\)" nil t)
993;; (goto-char (match-end 1))
994;; (delete-char 1)
995;; (capitalize-region
996;; (point)
997;; (progn
998;; (insert-buffer-substring canonicalization-buffer
999;; mbox-beg mbox-end)
1000;; (point)))))
1001;;
1002;; ;; Delete nickname
1003;; (goto-char (point-min))
1004;; (if (re-search-forward mail-nickname-pattern nil t)
1005;; (replace-match (if (eq (match-beginning 2) (1- (match-end 2)))
1006;; " \\2 "
1007;; " ")))
1008;;
1009;; ;; Fixup initials
1010;; (while (progn
1011;; (goto-char (point-min))
1012;; (re-search-forward mail-bad-initials-pattern nil t))
1013;; (replace-match
1014;; (if (match-beginning 4)
1015;; "\\1. \\4"
1016;; (if (match-beginning 5)
1017;; "\\1. \\5"
1018;; "\\1. "))))
1019;;
1020;; ;; Delete title
1021;; (goto-char (point-min))
1022;; (if (re-search-forward mail-full-name-prefixes nil t)
1023;; (narrow-to-region (point) (point-max)))
1024;;
1025;; ;; Delete trailing and preceding non-name characters
1026;; (goto-char (point-min))
1027;; (skip-chars-forward mail-non-begin-name-chars)
1028;; (narrow-to-region (point) (point-max))
1029;; (goto-char (point-max))
1030;; (skip-chars-backward mail-non-end-name-chars)
1031;; (narrow-to-region (point-min) (point))
1032
1033 ;; If name is "First Last" and userid is "F?L", then assume
1034 ;; the middle initial is the second letter in the userid.
1035 ;; initially by Jamie Zawinski <jwz@lucid.com>
1036 (cond ((and (eq 3 (- mbox-end mbox-beg))
1037 (progn
1038 (goto-char (point-min))
1039 (looking-at mail-two-name-pattern)))
1040 (setq fi (char-after (match-beginning 0))
1041 li (char-after (match-beginning 3)))
1042 (save-excursion
1043 (set-buffer canonicalization-buffer)
1044 ;; char-equal is ignoring case here, so no need to upcase
1045 ;; or downcase.
1046 (let ((case-fold-search t))
1047 (and (char-equal fi (char-after mbox-beg))
1048 (char-equal li (char-after (1- mbox-end)))
1049 (setq mi (char-after (1+ mbox-beg))))))
1050 (cond ((and mi
1051 ;; TODO: use better table than syntax table
1052 (eq ?w (char-syntax mi)))
1053 (goto-char (match-beginning 3))
1054 (insert (upcase mi) ". ")))))
1055
1056;; ;; Restore suffix
1057;; (cond (name-suffix
1058;; (goto-char (point-max))
1059;; (insert ", " name-suffix)
1060;; (backward-word 1)
1061;; (cond ((memq (following-char) '(?j ?J ?s ?S))
1062;; (capitalize-word 1)
1063;; (or (eq (following-char) ?.)
1064;; (insert ?.)))
1065;; (t
1066;; (upcase-word 1)))))
1067
1068 ;; Result
1069 (list (buffer-string)
1070 (progn
1071 (set-buffer canonicalization-buffer)
1072 (buffer-string)))
1073 )))
1074
1075;; TODO: put this back in the above function now that it's proven:
1076(defun variant-method (string)
1077 (let ((variant-buffer (get-buffer-create "*variant method buffer*"))
1078 (word-count 0)
1079 mixed-case-flag lower-case-flag upper-case-flag
1080 suffix-flag last-name-comma-flag
1081 comment-beg comment-end initial beg end
1082 )
1083 (save-excursion
1084 (set-buffer variant-buffer)
1085 (buffer-flush-undo variant-buffer)
1086 (set-syntax-table address-text-syntax-table)
1087 (widen)
1088 (erase-buffer)
1089 (setq case-fold-search nil)
1090
1091 (insert string)
1092
1093 ;; Fix . used as space
1094 (goto-char (point-min))
1095 (while (re-search-forward mail-bad-\.-pattern nil t)
1096 (replace-match "\\1 \\2"))
1097
1098 ;; Skip any initial garbage.
1099 (goto-char (point-min))
1100 (skip-chars-forward mail-non-begin-name-chars)
1101 (skip-chars-backward "& \"")
1102 (narrow-to-region (point) (point-max))
1103
1104 (catch 'stop
1105 (while t
1106 (skip-chars-forward mail-whitespace)
1107
1108 (cond
1109
1110 ;; Delete title
1111 ((and (eq word-count 0)
1112 (looking-at mail-full-name-prefixes))
1113 (goto-char (match-end 0))
1114 (narrow-to-region (point) (point-max)))
1115
1116 ;; Stop after name suffix
1117 ((and (>= word-count 2)
1118 (looking-at mail-full-name-suffix-pattern))
1119 (skip-chars-backward mail-whitespace)
1120 (setq suffix-flag (point))
1121 (if (eq ?, (following-char))
1122 (forward-char 1)
1123 (insert ?,))
1124 ;; Enforce at least one space after comma
1125 (or (eq mail-space-char (following-char))
1126 (insert mail-space-char))
1127 (skip-chars-forward mail-whitespace)
1128 (cond ((memq (following-char) '(?j ?J ?s ?S))
1129 (capitalize-word 1)
1130 (if (eq (following-char) ?.)
1131 (forward-char 1)
1132 (insert ?.)))
1133 (t
1134 (upcase-word 1)))
1135 (setq word-count (1+ word-count))
1136 (throw 'stop t))
1137
1138 ;; Handle SCA names
1139 ((looking-at "MKA \\(.+\\)") ; "Mundanely Known As"
1140 (setq word-count 0)
1141 (goto-char (match-beginning 1))
1142 (narrow-to-region (point) (point-max)))
1143
1144 ;; Various stopping points
1145 ((or
1146 ;; Stop before ALL CAPS acronyms, if preceded by mixed-case or
1147 ;; lowercase words. Eg. XT-DEM.
1148 (and (>= word-count 2)
1149 (or mixed-case-flag lower-case-flag)
1150 (looking-at mail-weird-acronym-pattern)
1151 (not (looking-at mail-roman-numeral-pattern)))
1152 ;; Stop before 4-or-more letter lowercase words preceded by
1153 ;; mixed case or uppercase words.
1154 (and (>= word-count 2)
1155 (or upper-case-flag mixed-case-flag)
1156 (looking-at "[a-z][a-z][a-z][a-z]+\\b"))
1157 ;; Stop before trailing alternative address
1158 (looking-at mail-alternative-address-pattern)
1159 ;; Stop before trailing comment not introduced by comma
1160 (looking-at mail-trailing-comment-start-pattern)
1161 ;; Stop before telephone numbers
1162 (looking-at mail-telephone-extension-pattern))
1163 (throw 'stop t))
1164
1165 ;; Check for initial last name followed by comma
1166 ((and (eq ?, (following-char))
1167 (eq word-count 1))
1168 (forward-char 1)
1169 (setq last-name-comma-flag t)
1170 (or (eq mail-space-char (following-char))
1171 (insert mail-space-char)))
1172
1173 ;; Stop before trailing comma-separated comment
1174 ((eq ?, (following-char))
1175 (throw 'stop t))
1176
1177 ;; Delete parenthesized/quoted comment/nickname
1178 ((memq (following-char) '(?\( ?\{ ?\[ ?\" ?\' ?\`))
1179 (setq comment-beg (point))
1180 (set-syntax-table address-text-comment-syntax-table)
1181 (cond ((memq (following-char) '(?\' ?\`))
1182 (if (eq ?\' (following-char))
1183 (forward-char 1))
1184 (or (search-forward "'" nil t)
1185 (delete-char 1)))
1186 (t
1187 (or (safe-move-sexp 1)
1188 (goto-char (point-max)))))
1189 (set-syntax-table address-text-syntax-table)
1190 (setq comment-end (point))
1191 (cond
1192 ;; Handle case of entire name being quoted
1193 ((and (eq word-count 0)
1194 (looking-at " *\\'")
1195 (>= (- comment-end comment-beg) 2))
1196 (narrow-to-region (1+ comment-beg) (1- comment-end))
1197 (goto-char (point-min)))
1198 (t
1199 ;; Handle case of quoted initial
1200 (if (and (or (= 3 (- comment-end comment-beg))
1201 (and (= 4 (- comment-end comment-beg))
1202 (eq ?. (char-after (+ 2 comment-beg)))))
1203 (not (looking-at " *\\'")))
1204 (setq initial (char-after (1+ comment-beg)))
1205 (setq initial nil))
1206 (delete-region comment-beg comment-end)
1207 (if initial
1208 (insert initial ". ")))))
1209
1210 ;; Delete ham radio call signs
1211 ((looking-at mail-ham-call-sign-pattern)
1212 (delete-region (match-beginning 0) (match-end 0)))
1213
1214 ;; Handle & substitution
1215 ;; TODO: remember to disable middle initial guessing
1216 ((and (or (bobp)
1217 (eq mail-space-char (preceding-char)))
1218 (looking-at "&\\( \\|\\'\\)"))
1219 (delete-char 1)
1220 (capitalize-region
1221 (point)
1222 (progn
1223 (insert-buffer-substring canonicalization-buffer
1224 mbox-beg mbox-end)
1225 (point))))
1226
1227 ;; Fixup initials
1228 ((looking-at mail-initial-pattern)
1229 (or (eq (following-char) (upcase (following-char)))
1230 (setq lower-case-flag t))
1231 (forward-char 1)
1232 (if (eq ?. (following-char))
1233 (forward-char 1)
1234 (insert ?.))
1235 (or (eq mail-space-char (following-char))
1236 (insert mail-space-char))
1237 (setq word-count (1+ word-count)))
1238
1239 ;; Regular name words
1240 ((looking-at mail-name-pattern)
1241 (setq beg (point))
1242 (setq end (match-end 0))
1243 (set (if (re-search-forward "[a-z]" end t)
1244 (if (progn
1245 (goto-char beg)
1246 (re-search-forward "[A-Z]" end t))
1247 'mixed-case-flag
1248 'lower-case-flag)
1249 'upper-case-flag) t)
1250 (goto-char end)
1251 (setq word-count (1+ word-count)))
1252
1253 (t
1254 (throw 'stop t)))))
1255
1256 (narrow-to-region (point-min) (point))
1257
1258 ;; Delete trailing word followed immediately by .
1259 (cond ((not suffix-flag)
1260 (goto-char (point-min))
1261 (if (re-search-forward "\\b[A-Za-z][A-Za-z]+\\. *\\'" nil t)
1262 (narrow-to-region (point-min) (match-beginning 0)))))
1263
1264 ;; If last name first put it at end (but before suffix)
1265 (cond (last-name-comma-flag
1266 (goto-char (point-min))
1267 (search-forward ",")
1268 (setq end (1- (point)))
1269 (goto-char (or suffix-flag (point-max)))
1270 (or (eq mail-space-char (preceding-char))
1271 (insert mail-space-char))
1272 (insert-buffer-substring (current-buffer) (point-min) end)
1273 (narrow-to-region (1+ end) (point-max))))
1274
1275 (goto-char (point-max))
1276 (skip-chars-backward mail-non-end-name-chars)
1277 (if (eq ?. (following-char))
1278 (forward-char 1))
1279 (narrow-to-region (point)
1280 (progn
1281 (goto-char (point-min))
1282 (skip-chars-forward mail-non-begin-name-chars)
1283 (point)))
1284
1285 ;; Compress whitespace
1286 (goto-char (point-min))
1287 (while (re-search-forward "[ \t\n]+" nil t)
1288 (replace-match " "))
1289
1290 (buffer-substring (point-min) (point-max))
1291
1292 )))
1293
1294;; The country names are just in there for show right now, and because
1295;; Jamie thought it would be neat. They aren't used yet.
1296
1297;; Keep in mind that the country abbreviations follow ISO-3166. There is
1298;; a U.S. FIPS that specifies a different set of two-letter country
1299;; abbreviations.
1300
1301;; TODO: put this in its own obarray, instead of cluttering up the main
1302;; symbol table with junk.
1303
1304(mapcar
1305 (function
1306 (lambda (x)
1307 (if (symbolp x)
1308 (put x 'domain-name t)
1309 (put (car x) 'domain-name (nth 1 x)))))
1310 '((ag "Antigua")
1311 (ar "Argentina") ; Argentine Republic
1312 arpa ; Advanced Projects Research Agency
1313 (at "Austria") ; The Republic of _
1314 (au "Australia")
1315 (bb "Barbados")
1316 (be "Belgium") ; The Kingdom of _
1317 (bg "Bulgaria")
1318 bitnet ; Because It's Time NET
1319 (bo "Bolivia") ; Republic of _
1320 (br "Brazil") ; The Federative Republic of _
1321 (bs "Bahamas")
1322 (bz "Belize")
1323 (ca "Canada")
1324 (ch "Switzerland") ; The Swiss Confederation
1325 (cl "Chile") ; The Republic of _
1326 (cn "China") ; The People's Republic of _
1327 (co "Columbia")
1328 com ; Commercial
1329 (cr "Costa Rica") ; The Republic of _
1330 (cs "Czechoslovakia")
1331 (de "Germany")
1332 (dk "Denmark")
1333 (dm "Dominica")
1334 (do "Dominican Republic") ; The _
1335 (ec "Ecuador") ; The Republic of _
1336 edu ; Educational
1337 (eg "Egypt") ; The Arab Republic of _
1338 (es "Spain") ; The Kingdom of _
1339 (fi "Finland") ; The Republic of _
1340 (fj "Fiji")
1341 (fr "France")
1342 gov ; Government (U.S.A.)
1343 (gr "Greece") ; The Hellenic Republic
1344 (hk "Hong Kong")
1345 (hu "Hungary") ; The Hungarian People's Republic (???)
1346 (ie "Ireland")
1347 (il "Israel") ; The State of _
1348 (in "India") ; The Republic of _
1349 int ; something British, don't know what
1350 (is "Iceland") ; The Republic of _
1351 (it "Italy") ; The Italian Republic
1352 (jm "Jamaica")
1353 (jp "Japan")
1354 (kn "St. Kitts and Nevis")
1355 (kr "South Korea")
1356 (lc "St. Lucia")
1357 (lk "Sri Lanka") ; The Democratic Socialist Republic of _
1358 mil ; Military (U.S.A.)
1359 (mx "Mexico") ; The United Mexican States
1360 (my "Malaysia") ; changed to Myanmar????
1361 (na "Namibia")
1362 nato ; North Atlantic Treaty Organization
1363 net ; Network
1364 (ni "Nicaragua") ; The Republic of _
1365 (nl "Netherlands") ; The Kingdom of the _
1366 (no "Norway") ; The Kingdom of _
1367 (nz "New Zealand")
1368 org ; Organization
1369 (pe "Peru")
1370 (pg "Papua New Guinea")
1371 (ph "Philippines") ; The Republic of the _
1372 (pl "Poland")
1373 (pr "Puerto Rico")
1374 (pt "Portugal") ; The Portugese Republic
1375 (py "Paraguay")
1376 (se "Sweden") ; The Kingdom of _
1377 (sg "Singapore") ; The Republic of _
1378 (sr "Suriname")
1379 (su "Soviet Union")
1380 (th "Thailand") ; The Kingdom of _
1381 (tn "Tunisia")
1382 (tr "Turkey") ; The Republic of _
1383 (tt "Trinidad and Tobago")
1384 (tw "Taiwan")
1385 (uk "United Kingdom") ; The _ of Great Britain
1386 unter-dom ; something German
1387 (us "U.S.A.") ; The United States of America
1388 uucp ; Unix to Unix CoPy
1389 (uy "Uruguay") ; The Eastern Republic of _
1390 (vc "St. Vincent and the Grenadines")
1391 (ve "Venezuela") ; The Republic of _
1392 (yu "Yugoslavia") ; The Socialist Federal Republic of _
1393 ;; Also said to be Zambia ...
1394 (za "South Africa") ; The Republic of _ (why not Zaire???)
1395 (zw "Zimbabwe") ; Republic of _
1396 ))
1397;; fipnet
1398
1399
1400;; Code for testing.
1401
1402(defun time-extract ()
1403 (let (times list)
1404 (setq times (cons (current-time-string) times)
1405 list problem-address-alist)
1406 (while list
1407 (mail-extract-address-components (car (car list)))
1408 (setq list (cdr list)))
1409 (setq times (cons (current-time-string) times))
1410 (nreverse times)))
1411
1412(defun test-extract (&optional starting-point)
1413 (interactive)
1414 (set-buffer (get-buffer-create "*Testing*"))
1415 (erase-buffer)
1416 (sit-for 0)
1417 (mapcar 'test-extract-internal
1418 (if starting-point
1419 (memq starting-point problem-address-alist)
1420 problem-address-alist)))
1421
1422(defvar failed-item)
1423(defun test-extract-internal (item)
1424 (setq failed-item item)
1425 (let* ((address (car item))
1426 (correct-name (nth 1 item))
1427 (correct-canon (nth 2 item))
1428 (result (mail-extract-address-components address))
1429 (name (car result))
1430 (canon (nth 1 result))
1431 (name-correct (or (null correct-name)
1432 (string-equal (downcase correct-name)
1433 (downcase name))))
1434 (canon-correct (or (null correct-canon)
1435 (string-equal correct-canon canon))))
1436 (cond ((not (and name-correct canon-correct))
1437 (pop-to-buffer "*Testing*")
1438 (select-window (get-buffer-window (current-buffer)))
1439 (goto-char (point-max))
1440 (insert "Address: " address "\n")
1441 (if (not name-correct)
1442 (insert " Correct Name: [" correct-name
1443 "]\; Result: [" name "]\n"))
1444 (if (not canon-correct)
1445 (insert " Correct Canon: [" correct-canon
1446 "]\; Result: [" canon "]\n"))
1447 (insert "\n")
1448 (sit-for 0))))
1449 (setq failed-item nil))
1450
1451(defun test-continue-extract ()
1452 (interactive)
1453 (test-extract failed-item))
1454
1455
1456;; Assorted junk.
1457
1458;; warsaw@nlm.nih.gov (A Bad Dude -- Barry Warsaw)
1459
1460;;'(from
1461;; reply-to
1462;; return-path
1463;; x-uucp-from
1464;; sender
1465;; resent-from
1466;; resent-sender
1467;; resent-reply-to)
1468
1469;;; mail-extr.el ends here
diff --git a/lisp/progmodes/make-mode.el b/lisp/progmodes/make-mode.el
new file mode 100644
index 00000000000..4f7d314773e
--- /dev/null
+++ b/lisp/progmodes/make-mode.el
@@ -0,0 +1,1043 @@
1;;; makefile.el --- makefile editing commands for Emacs
2
3;; Author: Thomas Neumann <tom@smart.bo.open.de>
4;; Adapted-By: ESR
5;; Keywords: unix tools
6
7;; $Id: makefile.el,v 1.7.1.17 1992/07/15 20:05:15 tom Exp tom $
8
9;; Copyright (C) 1992 Free Software Foundation, Inc.
10
11;; This file is part of GNU Emacs.
12
13;; GNU Emacs is free software; you can redistribute it and/or modify
14;; it under the terms of the GNU General Public License as published by
15;; the Free Software Foundation; either version 1, or (at your option)
16;; any later version.
17
18;; GNU Emacs is distributed in the hope that it will be useful,
19;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21;; GNU General Public License for more details.
22
23;; You should have received a copy of the GNU General Public License
24;; along with GNU Emacs; see the file COPYING. If not, write to
25;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
26
27;;; Code:
28
29(provide 'makefile)
30
31;;; ------------------------------------------------------------
32;;; Configureable stuff
33;;; ------------------------------------------------------------
34
35(defvar makefile-mode-name "makefile"
36 "The \"pretty name\" of makefile-mode, as it
37appears in the modeline.")
38
39(defvar makefile-browser-buffer-name "*Macros and Targets*"
40 "Name of the macro- and target browser buffer.")
41
42(defvar makefile-target-colon ":"
43 "The string that gets appended to all target names
44inserted by makefile-insert-target.
45\":\" or \"::\" are quite common values.")
46
47(defvar makefile-macro-assign " = "
48 "The string that gets appended to all macro names
49inserted by makefile-insert-macro.
50The normal value should be \" = \", since this is what
51standard make expects. However, newer makes such as dmake
52allow a larger variety of different macro assignments, so you
53might prefer to use \" += \" or \" := \" .")
54
55(defvar makefile-use-curly-braces-for-macros-p nil
56 "Set this variable to a non-nil value if you prefer curly braces
57in macro-references, so it looks like ${this}. A value of nil
58will cause makefile-mode to use parantheses, making macro references
59look like $(this) .")
60
61(defvar makefile-tab-after-target-colon t
62 "If you want a TAB (instead of a space) to be appended after the
63target colon, then set this to a non-nil value.")
64
65(defvar makefile-browser-leftmost-column 10
66 "Number of blanks to the left of the browser selection mark.")
67
68(defvar makefile-browser-cursor-column 10
69 "Column in which the cursor is positioned when it moves
70up or down in the browser.")
71
72(defvar makefile-browser-selected-mark "+ "
73 "String used to mark selected entries in the browser.")
74
75(defvar makefile-browser-unselected-mark " "
76 "String used to mark unselected entries in the browser.")
77
78(defvar makefile-browser-auto-advance-after-selection-p t
79 "If this variable is set to a non-nil value the cursor
80will automagically advance to the next line after an item
81has been selected in the browser.")
82
83(defvar makefile-find-file-autopickup-p t
84 "If this variable is set to a non-nil value then finding a file in
85a makefile-mode buffer will cause an automatic initial pickup of
86all macros and targets from the found file.")
87
88(defvar makefile-pickup-everything-picks-up-filenames-p nil
89 "If this variable is set to a non-nil value then
90makefile-pickup-everything also picks up filenames as targets
91(i.e. it calls makefile-find-filenames-as-targets), otherwise
92filenames are omitted.")
93
94(defvar makefile-cleanup-continuations-p t
95 "If this variable is set to a non-nil value then makefile-mode
96will assure that no line in the file ends with a backslash
97(the continuation character) followed by any whitespace.
98This is done by silently removing the trailing whitespace, leaving
99the backslash itself intact.
100IMPORTANT: Please note that enabling this option causes makefile-mode
101to MODIFY A FILE WITHOUT YOUR CONFIRMATION when \'it seems necessary\'.")
102
103(defvar makefile-browser-hook '()
104 "A function or list of functions to be called just before the
105browser is entered. This is executed in the makefile buffer, so
106you can for example run a makefile-pickup-everything automatically.")
107
108;;
109;; Special targets for DMake, Sun's make ...
110;;
111(defvar makefile-special-targets-list
112 '(("DEFAULT") ("DONE") ("ERROR") ("EXPORT")
113 ("FAILED") ("GROUPEPILOG") ("GROUPPROLOG") ("IGNORE")
114 ("IMPORT") ("INCLUDE") ("INCLUDEDIRS") ("INIT")
115 ("KEEP_STATE") ("MAKEFILES") ("MAKE_VERSION") ("NO_PARALLEL")
116 ("PARALLEL") ("PHONY") ("PRECIOUS") ("REMOVE")
117 ("SCCS_GET") ("SILENT") ("SOURCE") ("SUFFIXES")
118 ("WAIT") ("c.o") ("C.o") ("m.o")
119 ("el.elc") ("y.c") ("s.o"))
120 "List of special targets. You will be offered to complete
121on one of those in the minibuffer whenever you enter a \".\"
122at the beginning of a line in makefile-mode.")
123
124(defvar makefile-runtime-macros-list
125 '(("@") ("&") (">") ("<") ("*") ("^") ("?") ("%"))
126 "List of macros that are resolved by make at runtime.
127If you insert a macro reference using makefile-insert-macro-ref, the name
128of the macro is checked against this list. If it can be found its name will
129not be enclosed in { } or ( ).")
130
131(defconst makefile-dependency-regex
132 "^[^ \t#:]+\\([ \t]+[^ \t#:]+\\)*[ \t]*:\\($\\|\\([^=].*$\\)\\)"
133 "Regex used to find dependency lines in a makefile.")
134
135(defconst makefile-macroassign-regex
136 "^[^ \t][^:#=]*[\\*:\\+]?:?=.*$"
137 "Regex used to find macro assignment lines in a makefile.")
138
139(defconst makefile-ignored-files-in-pickup-regex
140 "\\(^\\..*\\)\\|\\(.*~$\\)\\|\\(.*,v$\\)"
141 "Regex for filenames that will NOT be included in the target list.")
142
143;;; ------------------------------------------------------------
144;;; The following configurable variables are used in the
145;;; up-to-date overview .
146;;; The standard configuration assumes that your `make' programm
147;;; can be run in question/query mode using the `-q' option, this
148;;; means that the command
149;;;
150;;; make -q foo
151;;;
152;;; should return an exit status of zero if the target `foo' is
153;;; up to date and a nonzero exit status otherwise.
154;;; Many makes can do this although the docs/manpages do not mention
155;;; it. Try it with your favourite one. GNU make and Dennis Vaduras
156;;; DMake have no problems.
157;;; Set the variable `makefile-brave-make' to the name of the
158;;; make utility that does this on your system.
159;;; To understand what this is all about see the function defintion
160;;; of `makefile-query-by-make-minus-q' .
161;;; ------------------------------------------------------------
162
163(defvar makefile-brave-make "gmake"
164 "A make that can handle the \'-q\' option.")
165
166(defvar makefile-query-one-target-method 'makefile-query-by-make-minus-q
167 "A function symbol [one that can be used as the first argument to
168funcall] that provides a function that must conform to the following
169interface:
170
171* As its first argument, it must accept the name of the target to
172 be checked, as a string.
173
174* As its second argument, it may accept the name of a makefile
175 as a string. Depending on what you\'re going to do you may
176 not need this.
177
178* It must return the integer value 0 (zero) if the given target
179 should be considered up-to-date in the context of the given
180 makefile, any nonzero integer value otherwise.")
181
182(defvar makefile-up-to-date-buffer-name "*Makefile Up-to-date overview*"
183 "Name of the Up-to-date overview buffer.")
184
185(defvar makefile-target-needs-rebuild-mark " .. NEEDS REBUILD"
186 "A string that is appended to the target name in the up-to-date
187overview if that target is considered to require a rebuild.")
188
189(defvar makefile-target-up-to-date-mark " .. is up to date"
190 "A string that is appenden to the target name in the up-to-date
191overview if that target is considered up-to-date.")
192
193;;; --- end of up-to-date-overview configuration ------------------
194
195
196(defvar makefile-mode-map nil
197 "The keymap that is used in makefile-mode.")
198(if makefile-mode-map
199 ()
200 (setq makefile-mode-map (make-sparse-keymap))
201 ;; set up the keymap
202 (define-key makefile-mode-map "$" 'makefile-insert-macro-ref)
203 (define-key makefile-mode-map "\C-c:" 'makefile-insert-target-ref)
204 (define-key makefile-mode-map ":" 'makefile-electric-colon)
205 (define-key makefile-mode-map "=" 'makefile-electric-equal)
206 (define-key makefile-mode-map "." 'makefile-electric-dot)
207 (define-key makefile-mode-map "\C-c\C-t" 'makefile-pickup-targets)
208 (define-key makefile-mode-map "\C-c\C-m" 'makefile-pickup-macros)
209 (define-key makefile-mode-map "\C-c\C-f" 'makefile-pickup-filenames-as-targets)
210 (define-key makefile-mode-map "\C-c\C-0" 'makefile-forget-everything)
211 (define-key makefile-mode-map "\C-c0" 'makefile-forget-everything)
212 (define-key makefile-mode-map "\C-c\C-b" 'makefile-switch-to-browser)
213 (define-key makefile-mode-map "\C-c\C-p" 'makefile-pickup-everything)
214 (define-key makefile-mode-map "\C-c\C-u" 'makefile-create-up-to-date-overview)
215 (define-key makefile-mode-map "\C-c\C-i" 'makefile-insert-gmake-function)
216 (define-key makefile-mode-map "\M-p" 'makefile-previous-dependency)
217 (define-key makefile-mode-map "\M-n" 'makefile-next-dependency))
218
219(defvar makefile-browser-map nil
220 "The keymap that is used in the macro- and target browser.")
221(if makefile-browser-map
222 ()
223 (setq makefile-browser-map (make-sparse-keymap))
224 (define-key makefile-browser-map "n" 'makefile-browser-next-line)
225 (define-key makefile-browser-map "\C-n" 'makefile-browser-next-line)
226 (define-key makefile-browser-map "p" 'makefile-browser-previous-line)
227 (define-key makefile-browser-map "\C-p" 'makefile-browser-previous-line)
228 (define-key makefile-browser-map " " 'makefile-browser-toggle)
229 (define-key makefile-browser-map "i" 'makefile-browser-insert-selection)
230 (define-key makefile-browser-map "I" 'makefile-browser-insert-selection-and-quit)
231 (define-key makefile-browser-map "\C-c\C-m" 'makefile-browser-insert-continuation)
232 (define-key makefile-browser-map "q" 'makefile-browser-quit)
233 ;; disable horizontal movement
234 (define-key makefile-browser-map "\C-b" 'undefined)
235 (define-key makefile-browser-map "\C-f" 'undefined))
236
237
238(defvar makefile-mode-syntax-table nil
239 "The syntax-table used in makefile mode.")
240(if makefile-mode-syntax-table
241 ()
242 (setq makefile-mode-syntax-table (make-syntax-table))
243 (modify-syntax-entry ?\( "() " makefile-mode-syntax-table)
244 (modify-syntax-entry ?\) ")( " makefile-mode-syntax-table)
245 (modify-syntax-entry ?\[ "(] " makefile-mode-syntax-table)
246 (modify-syntax-entry ?\] "([ " makefile-mode-syntax-table)
247 (modify-syntax-entry ?\{ "(} " makefile-mode-syntax-table)
248 (modify-syntax-entry ?\} "){ " makefile-mode-syntax-table)
249 (modify-syntax-entry ?# "< " makefile-mode-syntax-table)
250 (modify-syntax-entry ?\n "> " makefile-mode-syntax-table))
251
252
253;;; ------------------------------------------------------------
254;;; Internal variables.
255;;; You don't need to configure below this line.
256;;; ------------------------------------------------------------
257
258(defvar makefile-target-table nil
259 "Table of all targets that have been inserted in
260this Makefile buffer using makefile-insert-target or picked up
261using makefile-pickup-targets.")
262
263(defvar makefile-macro-table nil
264 "Table of all macros that have been iserted in
265this Makefile buffer using makefile-insert-macro or picked up
266using makefile-pickup-macros.")
267
268(defvar makefile-browser-client
269 "A buffer in makefile-mode that is currently using the browser.")
270
271(defvar makefile-browser-selection-vector nil)
272
273(defvar makefile-mode-hook '())
274
275(defconst makefile-gnumake-functions-alist
276
277 '(
278 ;; Text functions
279 ("subst" "From" "To" "In")
280 ("patsubst" "Pattern" "Replacement" "In")
281 ("strip" "Text")
282 ("findstring" "Find what" "In")
283 ("filter" "Pattern" "Text")
284 ("filter-out" "Pattern" "Text")
285 ("sort" "List")
286 ;; Filename functions
287 ("dir" "Names")
288 ("notdir" "Names")
289 ("suffix" "Names")
290 ("basename" "Names")
291 ("addsuffix" "Suffix" "Names")
292 ("join" "List 1" "List 2")
293 ("word" "Index" "Text")
294 ("words" "Text")
295 ("firstword" "Text")
296 ("wildcard" "Pattern")
297 ;; Misc functions
298 ("foreach" "Variable" "List" "Text")
299 ("origin" "Variable")
300 ("shell" "Command"))
301 "A list of GNU make 3.62 function names associated with
302the prompts for each function.
303This is used in the function makefile-insert-gmake-function .")
304
305
306;;; ------------------------------------------------------------
307;;; The mode function itself.
308;;; ------------------------------------------------------------
309
310(defun makefile-mode ()
311 "Major mode for editing Makefiles.
312Calling this function invokes the function(s) \"makefile-mode-hook\" before
313doing anything else.
314
315\\{makefile-mode-map}
316
317In the browser, use the following keys:
318
319\\{makefile-browser-map}
320
321makefile-mode can be configured by modifying the following
322variables:
323
324makefile-mode-name:
325 The \"pretty name\" of makefile-mode, as it
326 appears in the modeline.
327
328makefile-browser-buffer-name:
329 Name of the macro- and target browser buffer.
330
331makefile-target-colon:
332 The string that gets appended to all target names
333 inserted by makefile-insert-target.
334 \":\" or \"::\" are quite common values.
335
336makefile-macro-assign:
337 The string that gets appended to all macro names
338 inserted by makefile-insert-macro.
339 The normal value should be \" = \", since this is what
340 standard make expects. However, newer makes such as dmake
341 allow a larger variety of different macro assignments, so you
342 might prefer to use \" += \" or \" := \" .
343
344makefile-tab-after-target-colon:
345 If you want a TAB (instead of a space) to be appended after the
346 target colon, then set this to a non-nil value.
347
348makefile-browser-leftmost-column:
349 Number of blanks to the left of the browser selection mark.
350
351makefile-browser-cursor-column:
352 Column in which the cursor is positioned when it moves
353 up or down in the browser.
354
355makefile-browser-selected-mark:
356 String used to mark selected entries in the browser.
357
358makefile-browser-unselected-mark:
359 String used to mark unselected entries in the browser.
360
361makefile-browser-auto-advance-after-selection-p:
362 If this variable is set to a non-nil value the cursor
363 will automagically advance to the next line after an item
364 has been selected in the browser.
365
366makefile-find-file-autopickup-p:
367 If this variable is set to a non-nil value then finding a file in
368 a makefile-mode buffer will cause an automatic initial pickup of
369 all macros and targets from the found file.
370
371makefile-pickup-everything-picks-up-filenames-p:
372 If this variable is set to a non-nil value then
373 makefile-pickup-everything also picks up filenames as targets
374 (i.e. it calls makefile-find-filenames-as-targets), otherwise
375 filenames are omitted.
376
377makefile-cleanup-continuations-p:
378 If this variable is set to a non-nil value then makefile-mode
379 will assure that no line in the file ends with a backslash
380 (the continuation character) followed by any whitespace.
381 This is done by silently removing the trailing whitespace, leaving
382 the backslash itself intact.
383 IMPORTANT: Please note that enabling this option causes makefile-mode
384 to MODIFY A FILE WITHOUT YOUR CONFIRMATION when \'it seems necessary\'.
385
386makefile-browser-hook:
387 A function or list of functions to be called just before the
388 browser is entered. This is executed in the makefile buffer, so
389 you can for example run a makefile-pickup-everything automatically.
390
391makefile-special-targets-list:
392 List of special targets. You will be offered to complete
393 on one of those in the minibuffer whenever you enter a \".\"
394 at the beginning of a line in makefile-mode."
395 (interactive)
396 (kill-all-local-variables)
397 (if (not (memq 'makefile-find-file-autopickup find-file-hooks))
398 (setq find-file-hooks
399 (append find-file-hooks (list 'makefile-find-file-autopickup))))
400 (if (not (memq 'makefile-cleanup-continuations write-file-hooks))
401 (setq write-file-hooks
402 (append write-file-hooks (list 'makefile-cleanup-continuations))))
403 (make-variable-buffer-local 'makefile-target-table)
404 (make-variable-buffer-local 'makefile-macro-table)
405 (makefile-forget-all-macros)
406 (makefile-forget-all-targets)
407 (setq comment-start "#")
408 (setq comment-end "")
409 (setq comment-start-skip "#[ \t]*")
410 ;; become the current major mode
411 (setq major-mode 'makefile-mode)
412 (setq mode-name makefile-mode-name)
413 ;; activate keymap
414 (use-local-map makefile-mode-map)
415 (set-syntax-table makefile-mode-syntax-table)
416 (run-hooks 'makefile-mode-hook))
417
418
419(defun makefile-find-file-autopickup ()
420 (if (eq major-mode 'makefile-mode)
421 (if makefile-find-file-autopickup-p
422 (makefile-pickup-everything))))
423
424(defun makefile-next-dependency ()
425 "Move (point) to the beginning of the next dependency line
426below the current position of (point)."
427 (interactive)
428 (let ((here (point)))
429 (end-of-line)
430 (if (re-search-forward makefile-dependency-regex (point-max) t)
431 (progn (beginning-of-line) t) ; indicate success
432 (goto-char here) nil)))
433
434(defun makefile-previous-dependency ()
435 "Move (point) to the beginning of the next dependency line
436above the current position of (point)."
437 (interactive)
438 (let ((here (point)))
439 (beginning-of-line)
440 (if (re-search-backward makefile-dependency-regex (point-min) t)
441 (progn (beginning-of-line) t) ; indicate success
442 (goto-char here) nil)))
443
444
445(defun makefile-electric-dot ()
446 "At (bol), offer completion on makefile-special-targets-list.
447Anywhere else just insert a dot."
448 (interactive)
449 (if (bolp)
450 (makefile-insert-special-target)
451 (insert ".")))
452
453
454(defun makefile-insert-special-target ()
455 "Offer completion on makefile-special-targets-list and insert
456the result at (point)."
457 (interactive)
458 (let
459 ((special-target
460 (completing-read "Special target: "
461 makefile-special-targets-list nil nil nil)))
462 (if (zerop (length special-target))
463 ()
464 (insert (format ".%s:" special-target))
465 (makefile-forward-after-target-colon))))
466
467
468(defun makefile-electric-equal ()
469 "At (bol) do makefile-insert-macro. Anywhere else just
470self-insert."
471 (interactive)
472 (if (bolp)
473 (call-interactively 'makefile-insert-macro)
474 (insert "=")))
475
476(defun makefile-insert-macro (macro-name)
477 "Prepare definition of a new macro."
478 (interactive "sMacro Name: ")
479 (if (not (zerop (length macro-name)))
480 (progn
481 (beginning-of-line)
482 (insert (format "%s%s" macro-name makefile-macro-assign))
483 (makefile-remember-macro macro-name))))
484
485
486(defun makefile-insert-macro-ref (macro-name)
487 "Offer completion on a list of known macros, then
488insert complete macro-ref at (point) ."
489 (interactive
490 (list
491 (completing-read "Refer to macro: " makefile-macro-table nil nil nil)))
492 (if (not (zerop (length macro-name)))
493 (if (assoc macro-name makefile-runtime-macros-list)
494 (insert (format "$%s " macro-name))
495 (insert (makefile-format-macro-ref macro-name) " "))))
496
497
498(defun makefile-insert-target (target-name)
499 "Prepare definition of a new target (dependency line)."
500 (interactive "sTarget: ")
501 (if (not (zerop (length target-name)))
502 (progn
503 (beginning-of-line)
504 (insert (format "%s%s" target-name makefile-target-colon))
505 (makefile-forward-after-target-colon)
506 (end-of-line)
507 (makefile-remember-target target-name))))
508
509
510(defun makefile-insert-target-ref (target-name)
511 "Offer completion on a list of known targets, then
512insert complete target-ref at (point) ."
513 (interactive
514 (list
515 (completing-read "Refer to target: " makefile-target-table nil nil nil)))
516 (if (not (zerop (length target-name)))
517 (progn
518 (insert (format "%s " target-name)))))
519
520(defun makefile-electric-colon ()
521 "At (bol) defines a new target, anywhere else just self-insert ."
522 (interactive)
523 (if (bolp)
524 (call-interactively 'makefile-insert-target)
525 (insert ":")))
526
527
528;;; ------------------------------------------------------------
529;;; Extracting targets and macros from an existing makefile
530;;; ------------------------------------------------------------
531
532(defun makefile-pickup-targets ()
533 "Scan a buffer that contains a makefile for target definitions (dependencies)
534and add them to the list of known targets."
535 (interactive)
536 (save-excursion
537 (goto-char (point-min))
538 (while (re-search-forward makefile-dependency-regex (point-max) t)
539 (makefile-add-this-line-targets))))
540; (forward-line 1))))
541
542(defun makefile-add-this-line-targets ()
543 (save-excursion
544 (beginning-of-line)
545 (let ((done-with-line nil))
546 (while (not done-with-line)
547 (skip-chars-forward " \t")
548 (if (not (setq done-with-line (or (eolp)
549 (char-equal (char-after (point)) ?:))))
550 (progn
551 (let* ((start-of-target-name (point))
552 (target-name
553 (progn
554 (skip-chars-forward "^ \t:#")
555 (buffer-substring start-of-target-name (point)))))
556 (if (makefile-remember-target target-name)
557 (message "Picked up target \"%s\"" target-name)))))))))
558
559
560(defun makefile-pickup-macros ()
561 "Scan a buffer that contains a makefile for macro definitions
562and add them to the list of known macros."
563 (interactive)
564 (save-excursion
565 (goto-char (point-min))
566 (while (re-search-forward makefile-macroassign-regex (point-max) t)
567 (makefile-add-this-line-macro)
568 (forward-line 1))))
569
570(defun makefile-add-this-line-macro ()
571 (save-excursion
572 (beginning-of-line)
573 (skip-chars-forward " \t")
574 (if (not (eolp))
575 (let* ((start-of-macro-name (point))
576 (macro-name (progn
577 (skip-chars-forward "^ \t:#=*")
578 (buffer-substring start-of-macro-name (point)))))
579 (if (makefile-remember-macro macro-name)
580 (message "Picked up macro \"%s\"" macro-name))))))
581
582
583(defun makefile-pickup-everything ()
584 "Calls makefile-pickup-targets and makefile-pickup-macros.
585See their documentation for what they do."
586 (interactive)
587 (makefile-pickup-macros)
588 (makefile-pickup-targets)
589 (if makefile-pickup-everything-picks-up-filenames-p
590 (makefile-pickup-filenames-as-targets)))
591
592
593(defun makefile-pickup-filenames-as-targets ()
594 "Scan the current directory for filenames, check each filename
595against makefile-ignored-files-in-pickup-regex and add all qualifying
596names to the list of known targets."
597 (interactive)
598 (let* ((dir (file-name-directory (buffer-file-name)))
599 (raw-filename-list (if dir
600 (file-name-all-completions "" dir)
601 (file-name-all-completions "" ""))))
602 (mapcar '(lambda (name)
603 (if (and (not (file-directory-p name))
604 (not (string-match makefile-ignored-files-in-pickup-regex
605 name)))
606 (if (makefile-remember-target name)
607 (message "Picked up file \"%s\" as target" name))))
608 raw-filename-list)))
609
610;;; ------------------------------------------------------------
611;;; The browser window
612;;; ------------------------------------------------------------
613
614
615(defun makefile-browser-format-target-line (target selected)
616 (format
617 (concat (make-string makefile-browser-leftmost-column ?\ )
618 (if selected
619 makefile-browser-selected-mark
620 makefile-browser-unselected-mark)
621 "%s%s")
622 target makefile-target-colon))
623
624(defun makefile-browser-format-macro-line (macro selected)
625 (format
626 (concat (make-string makefile-browser-leftmost-column ?\ )
627 (if selected
628 makefile-browser-selected-mark
629 makefile-browser-unselected-mark)
630 (makefile-format-macro-ref macro))))
631
632(defun makefile-browser-fill (targets macros)
633 (setq buffer-read-only nil)
634 (goto-char (point-min))
635 (erase-buffer)
636 (mapconcat
637 (function
638 (lambda (item) (insert (makefile-browser-format-target-line (car item) nil) "\n")))
639 targets
640 "")
641 (mapconcat
642 (function
643 (lambda (item) (insert (makefile-browser-format-macro-line (car item) nil) "\n")))
644 macros
645 "")
646 (sort-lines nil (point-min) (point-max))
647 (goto-char (1- (point-max)))
648 (delete-char 1) ; remove unnecessary newline at eob
649 (goto-char (point-min))
650 (forward-char makefile-browser-cursor-column)
651 (setq buffer-read-only t))
652
653
654;;;
655;;; Moving up and down in the browser
656;;;
657
658(defun makefile-browser-next-line ()
659 "Move the browser selection cursor to the next line."
660 (interactive)
661 (if (not (makefile-last-line-p))
662 (progn
663 (forward-line 1)
664 (forward-char makefile-browser-cursor-column))))
665
666(defun makefile-browser-previous-line ()
667 "Move the browser selection cursor to the previous line."
668 (interactive)
669 (if (not (makefile-first-line-p))
670 (progn
671 (forward-line -1)
672 (forward-char makefile-browser-cursor-column))))
673
674;;;
675;;; Quitting the browser (returns to client buffer)
676;;;
677
678(defun makefile-browser-quit ()
679 "Leave the makefile-browser-buffer and return to the buffer
680from that it has been entered."
681 (interactive)
682 (let ((my-client makefile-browser-client))
683 (setq makefile-browser-client nil) ; we quitted, so NO client!
684 (set-buffer-modified-p nil)
685 (kill-buffer (current-buffer))
686 (pop-to-buffer my-client)))
687
688;;;
689;;; Toggle state of a browser item
690;;;
691
692(defun makefile-browser-toggle ()
693 "Toggle the selection state of the browser item at the cursor position."
694 (interactive)
695 (let ((this-line (count-lines (point-min) (point))))
696 (setq this-line (max 1 this-line))
697 (makefile-browser-toggle-state-for-line this-line)
698 (goto-line this-line)
699 (setq buffer-read-only nil)
700 (beginning-of-line)
701 (if (makefile-browser-on-macro-line-p)
702 (let ((macro-name (makefile-browser-this-line-macro-name)))
703 (kill-line)
704 (insert
705 (makefile-browser-format-macro-line
706 macro-name
707 (makefile-browser-get-state-for-line this-line))))
708 (let ((target-name (makefile-browser-this-line-target-name)))
709 (kill-line)
710 (insert
711 (makefile-browser-format-target-line
712 target-name
713 (makefile-browser-get-state-for-line this-line)))))
714 (setq buffer-read-only t)
715 (beginning-of-line)
716 (forward-char makefile-browser-cursor-column)
717 (if makefile-browser-auto-advance-after-selection-p
718 (makefile-browser-next-line))))
719
720;;;
721;;; Making insertions into the client buffer
722;;;
723
724(defun makefile-browser-insert-continuation ()
725 "In the browser\'s client buffer, go to (end-of-line), insert a \'\\\'
726character, insert a new blank line, go to that line and indent by one TAB.
727This is most useful in the process of creating continued lines when 'sending' large
728dependencies from the browser to the client buffer.
729(point) advances accordingly in the client buffer."
730 (interactive)
731 (save-excursion
732 (set-buffer makefile-browser-client)
733 (end-of-line)
734 (insert "\\\n\t")))
735
736(defun makefile-browser-insert-selection ()
737 "Insert all browser-selected targets and/or macros in the browser\'s
738client buffer.
739Insertion takes place at (point)."
740 (interactive)
741 (save-excursion
742 (goto-line 1)
743 (let ((current-line 1))
744 (while (not (eobp))
745 (if (makefile-browser-get-state-for-line current-line)
746 (makefile-browser-send-this-line-item))
747 (forward-line 1)
748 (setq current-line (1+ current-line))))))
749
750(defun makefile-browser-insert-selection-and-quit ()
751 (interactive)
752 (makefile-browser-insert-selection)
753 (makefile-browser-quit))
754
755(defun makefile-browser-send-this-line-item ()
756 (if (makefile-browser-on-macro-line-p)
757 (save-excursion
758 (let ((macro-name (makefile-browser-this-line-macro-name)))
759 (set-buffer makefile-browser-client)
760 (insert (makefile-format-macro-ref macro-name) " ")))
761 (save-excursion
762 (let ((target-name (makefile-browser-this-line-target-name)))
763 (set-buffer makefile-browser-client)
764 (insert target-name " ")))))
765
766
767(defun makefile-browser-start-interaction ()
768 (use-local-map makefile-browser-map)
769 (setq buffer-read-only t))
770
771
772(defun makefile-browse (targets macros)
773 (interactive)
774 (if (zerop (+ (length targets) (length macros)))
775 (progn
776 (beep)
777 (message "No macros or targets to browse! Consider running 'makefile-pickup-everything\'"))
778 (let ((browser-buffer (get-buffer-create makefile-browser-buffer-name)))
779 (pop-to-buffer browser-buffer)
780 (make-variable-buffer-local 'makefile-browser-selection-vector)
781 (makefile-browser-fill targets macros)
782 (setq makefile-browser-selection-vector
783 (make-vector (+ (length targets) (length macros)) nil))
784 (makefile-browser-start-interaction))))
785
786
787(defun makefile-switch-to-browser ()
788 (interactive)
789 (run-hooks 'makefile-browser-hook)
790 (setq makefile-browser-client (current-buffer))
791 (makefile-browse makefile-target-table makefile-macro-table))
792
793
794;;; ------------------------------------------------------------
795;;; Up-to-date overview buffer
796;;; ------------------------------------------------------------
797
798(defun makefile-create-up-to-date-overview ()
799 "Create a buffer containing an overview of the state of all
800known targets from the makefile that is currently being edited.
801Known targets are targets that are explicitly defined in that makefile;
802in other words, all targets that appear on the left hand side of a
803dependency in the makefile."
804 (interactive)
805 (if (y-or-n-p "Are you sure that the makefile being edited is consistent? ")
806 ;;
807 ;; The rest of this function operates on a temporary makefile, created by
808 ;; writing the current contents of the makefile buffer.
809 ;;
810 (let ((saved-target-table makefile-target-table)
811 (this-buffer (current-buffer))
812 (makefile-up-to-date-buffer
813 (get-buffer-create makefile-up-to-date-buffer-name))
814 (filename (makefile-save-temporary))
815 ;;
816 ;; Forget the target table because it may contain picked-up filenames
817 ;; that are not really targets in the current makefile.
818 ;; We don't want to query these, so get a new target-table with just the
819 ;; targets that can be found in the makefile buffer.
820 ;; The 'old' target table will be restored later.
821 ;;
822 (real-targets (progn
823 (makefile-forget-all-targets)
824 (makefile-pickup-targets)
825 makefile-target-table)))
826
827 (set-buffer makefile-up-to-date-buffer)
828 (setq buffer-read-only nil)
829 (erase-buffer)
830 (makefile-query-targets filename real-targets)
831 (if (zerop (buffer-size)) ; if it did not get us anything
832 (progn
833 (kill-buffer (current-buffer))
834 (message "No overview created!")))
835 (set-buffer this-buffer)
836 (setq makefile-target-table saved-target-table)
837 (if (get-buffer makefile-up-to-date-buffer-name)
838 (progn
839 (pop-to-buffer (get-buffer makefile-up-to-date-buffer-name))
840 (sort-lines nil (point-min) (point-max))
841 (setq buffer-read-only t))))))
842
843
844
845(defun makefile-save-temporary ()
846 "Create a temporary file from the current makefile buffer."
847 (let ((filename (makefile-generate-temporary-filename)))
848 (write-region (point-min) (point-max) filename nil 0)
849 filename)) ; return the filename
850
851(defun makefile-generate-temporary-filename ()
852 "Create a filename suitable for use in makefile-save-temporary.
853Be careful to allow brain-dead file systems (DOS, SYSV ...) to cope
854with the generated name !"
855 (let ((my-name (user-login-name))
856 (my-uid (int-to-string (user-uid))))
857 (concat "mktmp"
858 (if (> (length my-name) 3)
859 (substring my-name 0 3)
860 my-name)
861 "."
862 (if (> (length my-uid) 3)
863 (substring my-uid 0 3)
864 my-uid))))
865
866(defun makefile-query-targets (filename target-table)
867 "This function fills the up-to-date-overview-buffer.
868It checks each target in target-table using makefile-query-one-target-method
869and generates the overview, one line per target name."
870 (insert
871 (mapconcat '(lambda (item)
872 (let ((target-name (car item)))
873 (makefile-format-up-to-date-buffer-entry
874 (funcall makefile-query-one-target-method
875 target-name filename) target-name)))
876 target-table "\n"))
877 (goto-char (point-min))
878 (delete-file filename)) ; remove the tmpfile
879
880(defun makefile-query-by-make-minus-q (target &optional filename)
881 (not (zerop (call-process makefile-brave-make nil nil nil "-f" filename "-q" target))))
882
883(defun makefile-format-up-to-date-buffer-entry (needs-rebuild target)
884 (format "\t%s%s"
885 target
886 (if needs-rebuild
887 makefile-target-needs-rebuild-mark
888 makefile-target-up-to-date-mark)))
889
890
891;;; ------------------------------------------------------------
892;;; Continuation cleanup
893;;; ------------------------------------------------------------
894
895(defun makefile-cleanup-continuations ()
896 (if (eq major-mode 'makefile-mode)
897 (if (and makefile-cleanup-continuations-p
898 (not buffer-read-only))
899 (save-excursion
900 (goto-char (point-min))
901 (while (re-search-forward "\\\\[ \t]+$" (point-max) t)
902 (replace-match "\\" t t))))))
903
904;;; ------------------------------------------------------------
905;;; GNU make function support
906;;; ------------------------------------------------------------
907
908(defun makefile-insert-gmake-function ()
909 "This function is intended to help you using the numerous
910macro-like \'function calls\' of GNU make.
911It will ask you for the name of the function you wish to
912use (with completion), then, after you selected the function,
913it will prompt you for all required parameters.
914This function \'knows\' about the required parameters of every
915GNU make function and will use meaningfull prompts for the
916various args, making it much easier to take advantage of this
917powerfull GNU make feature."
918 (interactive)
919 (let* ((gm-function-name (completing-read
920 "Function: "
921 makefile-gnumake-functions-alist
922 nil t nil))
923 (gm-function-prompts
924 (cdr (assoc gm-function-name makefile-gnumake-functions-alist))))
925 (if (not (zerop (length gm-function-name)))
926 (insert (makefile-format-macro-ref
927 (concat gm-function-name " "
928 (makefile-prompt-for-gmake-funargs
929 gm-function-name gm-function-prompts)))
930 " "))))
931
932(defun makefile-prompt-for-gmake-funargs (function-name prompt-list)
933 (mapconcat
934 (function (lambda (one-prompt)
935 (read-string (format "[%s] %s: " function-name one-prompt) nil)))
936 prompt-list
937 ","))
938
939
940
941;;; ------------------------------------------------------------
942;;; Utility functions
943;;; ------------------------------------------------------------
944
945(defun makefile-forget-all-targets ()
946 "Clear the target-table for this buffer."
947 (interactive)
948 (setq makefile-target-table '()))
949
950(defun makefile-forget-all-macros ()
951 "Clear the macro-table for this buffer."
952 (interactive)
953 (setq makefile-macro-table '()))
954
955
956(defun makefile-forget-everything ()
957 "Clear the macro-table AND the target-table for this buffer."
958 (interactive)
959 (if (y-or-n-p "Really forget all macro- and target information ? ")
960 (progn
961 (makefile-forget-all-targets)
962 (makefile-forget-all-macros)
963 (if (get-buffer makefile-browser-buffer-name)
964 (kill-buffer makefile-browser-buffer-name))
965 (message "Cleared macro- and target tables."))))
966
967(defun makefile-remember-target (target-name)
968 "Remember a given target if it is not already remembered for this buffer."
969 (if (not (zerop (length target-name)))
970 (if (not (assoc target-name makefile-target-table))
971 (setq makefile-target-table
972 (cons (list target-name) makefile-target-table)))))
973
974(defun makefile-remember-macro (macro-name)
975 "Remember a given macro if it is not already remembered for this buffer."
976 (if (not (zerop (length macro-name)))
977 (if (not (assoc macro-name makefile-macro-table))
978 (setq makefile-macro-table
979 (cons (list macro-name) makefile-macro-table)))))
980
981(defun makefile-forward-after-target-colon ()
982"Move point forward after the terminating colon
983of a target has been inserted.
984This accts according to the value of makefile-tab-after-target-colon ."
985 (if makefile-tab-after-target-colon
986 (insert "\t")
987 (insert " ")))
988
989(defun makefile-browser-on-macro-line-p ()
990 "Determine if point is on a macro line in the browser."
991 (save-excursion
992 (beginning-of-line)
993 (re-search-forward "\\$[{(]" (makefile-end-of-line-point) t)))
994
995(defun makefile-browser-this-line-target-name ()
996 "Extract the target name from a line in the browser."
997 (save-excursion
998 (end-of-line)
999 (skip-chars-backward "^ \t")
1000 (buffer-substring (point) (1- (makefile-end-of-line-point)))))
1001
1002(defun makefile-browser-this-line-macro-name ()
1003 "Extract the macro name from a line in the browser."
1004 (save-excursion
1005 (beginning-of-line)
1006 (re-search-forward "\\$[{(]" (makefile-end-of-line-point) t)
1007 (let ((macro-start (point)))
1008 (skip-chars-forward "^})")
1009 (buffer-substring macro-start (point)))))
1010
1011(defun makefile-format-macro-ref (macro-name)
1012 "Format a macro reference according to the value of the
1013configuration variable makefile-use-curly-braces-for-macros-p ."
1014 (if makefile-use-curly-braces-for-macros-p
1015 (format "${%s}" macro-name)
1016 (format "$(%s)" macro-name)))
1017
1018(defun makefile-browser-get-state-for-line (n)
1019 (aref makefile-browser-selection-vector (1- n)))
1020
1021(defun makefile-browser-set-state-for-line (n to-state)
1022 (aset makefile-browser-selection-vector (1- n) to-state))
1023
1024(defun makefile-browser-toggle-state-for-line (n)
1025 (makefile-browser-set-state-for-line n (not (makefile-browser-get-state-for-line n))))
1026
1027(defun makefile-beginning-of-line-point ()
1028 (save-excursion
1029 (beginning-of-line)
1030 (point)))
1031
1032(defun makefile-end-of-line-point ()
1033 (save-excursion
1034 (end-of-line)
1035 (point)))
1036
1037(defun makefile-last-line-p ()
1038 (= (makefile-end-of-line-point) (point-max)))
1039
1040(defun makefile-first-line-p ()
1041 (= (makefile-beginning-of-line-point) (point-min)))
1042
1043;; makefile.el ends here
diff --git a/lisp/textmodes/sgml-mode.el b/lisp/textmodes/sgml-mode.el
new file mode 100644
index 00000000000..61437f14bb9
--- /dev/null
+++ b/lisp/textmodes/sgml-mode.el
@@ -0,0 +1,266 @@
1;;; sgml-mode.el --- SGML-editing mode
2
3;; Maintainer: FSF
4;; Last-Modified: 14 Jul 1992
5;; Adapted-By: ESR
6
7;; Copyright (C) 1992 Free Software Foundation, Inc.
8
9;; This file is part of GNU Emacs.
10
11;; GNU Emacs is free software; you can redistribute it and/or modify
12;; it under the terms of the GNU General Public License as published by
13;; the Free Software Foundation; either version 1, or (at your option)
14;; any later version.
15
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
22;; along with GNU Emacs; see the file COPYING. If not, write to
23;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24
25;;; Commentary:
26
27;; Some suggestions for your .emacs file:
28;;
29;; (autoload 'sgml-mode "sgml-mode" "SGML mode" t)
30;;
31;; (setq auto-mode-alist
32;; (append (list (cons "\\.sgm$" 'sgml-mode)
33;; (cons "\\.sgml$" 'sgml-mode)
34;; (cons "\\.dtd$" 'sgml-mode))
35;; auto-mode-alist))
36
37;;; Code:
38
39(provide 'sgml-mode)
40(require 'compile)
41
42;;; sgmls is a free SGML parser available from
43;;; ftp.uu.net:pub/text-processing/sgml
44;;; Its error messages can be parsed by next-error.
45;;; The -s option suppresses output.
46
47(defconst sgml-validate-command
48 "sgmls -s"
49 "*The command to validate an SGML document.
50The file name of current buffer file name will be appended to this,
51separated by a space.")
52
53(defvar sgml-saved-validate-command nil
54 "The command last used to validate in this buffer.")
55
56(defvar sgml-mode-map nil "Keymap for SGML mode")
57
58(if sgml-mode-map
59 ()
60 (setq sgml-mode-map (make-sparse-keymap))
61 (define-key sgml-mode-map ">" 'sgml-close-angle)
62 (define-key sgml-mode-map "/" 'sgml-slash)
63 (define-key sgml-mode-map "\C-c\C-v" 'sgml-validate))
64
65(defun sgml-mode ()
66 "Major mode for editing SGML.
67Makes > display the matching <. Makes / display matching /.
68Use \\[sgml-validate] to validate your document with an SGML parser."
69 (interactive)
70 (kill-all-local-variables)
71 (setq local-abbrev-table text-mode-abbrev-table)
72 (use-local-map sgml-mode-map)
73 (setq mode-name "SGML")
74 (setq major-mode 'sgml-mode)
75 (make-local-variable 'paragraph-start)
76 ;; A start or end tag by itself on a line separates a paragraph.
77 ;; This is desirable because SGML discards a newline that appears
78 ;; immediately after a start tag or immediately before an end tag.
79 (setq paragraph-start
80 "^[ \t\n]\\|\
81\\(</?\\([A-Za-z]\\([-.A-Za-z0-9= \t\n]\\|\"[^\"]*\"\\|'[^']*'\\)*\\)?>$\\)")
82 (make-local-variable 'paragraph-separate)
83 (setq paragraph-separate
84 "^[ \t\n]*$\\|\
85^</?\\([A-Za-z]\\([-.A-Za-z0-9= \t\n]\\|\"[^\"]*\"\\|'[^']*'\\)*\\)?>$")
86 (make-local-variable 'sgml-saved-validate-command)
87 (set-syntax-table text-mode-syntax-table)
88 (make-local-variable 'comment-start)
89 (setq comment-start "<!-- ")
90 (make-local-variable 'comment-end)
91 (setq comment-end " -->")
92 (make-local-variable 'comment-indent-hook)
93 (setq comment-indent-hook 'sgml-comment-indent)
94 (make-local-variable 'comment-start-skip)
95 ;; This will allow existing comments within declarations to be
96 ;; recognized.
97 (setq comment-start-skip "--[ \t]*")
98 (run-hooks 'text-mode-hook 'sgml-mode-hook))
99
100(defun sgml-comment-indent ()
101 (if (and (looking-at "--")
102 (not (and (eq (char-after (1- (point))) ?!)
103 (eq (char-after (- (point) 2)) ?<))))
104 (progn
105 (skip-chars-backward " \t")
106 (max comment-column (1+ (current-column))))
107 0))
108
109(defconst sgml-start-tag-regex
110 "<[A-Za-z]\\([-.A-Za-z0-9= \n\t]\\|\"[^\"]*\"\\|'[^']*'\\)*"
111 "Regular expression that matches a non-empty start tag.
112Any terminating > or / is not matched.")
113
114(defvar sgml-mode-markup-syntax-table nil
115 "Syntax table used for scanning SGML markup.")
116
117(if sgml-mode-markup-syntax-table
118 ()
119 (setq sgml-mode-markup-syntax-table (make-syntax-table))
120 (modify-syntax-entry ?< "(>" sgml-mode-markup-syntax-table)
121 (modify-syntax-entry ?> ")<" sgml-mode-markup-syntax-table)
122 (modify-syntax-entry ?- "_ 1234" sgml-mode-markup-syntax-table)
123 (modify-syntax-entry ?\' "\"" sgml-mode-markup-syntax-table))
124
125(defconst sgml-angle-distance 4000
126 "*If non-nil, is the maximum distance to search for matching <
127when > is inserted.")
128
129(defun sgml-close-angle (arg)
130 "Insert > and display matching <."
131 (interactive "p")
132 (insert-char ?> arg)
133 (if (> arg 0)
134 (let ((oldpos (point))
135 (blinkpos))
136 (save-excursion
137 (save-restriction
138 (if sgml-angle-distance
139 (narrow-to-region (max (point-min)
140 (- (point) sgml-angle-distance))
141 oldpos))
142 ;; See if it's the end of a marked section.
143 (and (> (- (point) (point-min)) 3)
144 (eq (char-after (- (point) 2)) ?\])
145 (eq (char-after (- (point) 3)) ?\])
146 (re-search-backward "<!\\[\\(-?[A-Za-z0-9. \t\n&;]\\|\
147--\\([^-]\\|-[^-]\\)*--\\)*\\["
148 (point-min)
149 t)
150 (let ((msspos (point)))
151 (if (and (search-forward "]]>" oldpos t)
152 (eq (point) oldpos))
153 (setq blinkpos msspos))))
154 ;; This handles cases where the > ends one of the following:
155 ;; markup declaration starting with <! (possibly including a
156 ;; declaration subset); start tag; end tag; SGML declaration.
157 (if blinkpos
158 ()
159 (goto-char oldpos)
160 (condition-case ()
161 (let ((oldtable (syntax-table))
162 (parse-sexp-ignore-comments t))
163 (unwind-protect
164 (progn
165 (set-syntax-table sgml-mode-markup-syntax-table)
166 (setq blinkpos (scan-sexps oldpos -1)))
167 (set-syntax-table oldtable)))
168 (error nil))
169 (and blinkpos
170 (goto-char blinkpos)
171 (or
172 ;; Check that it's a valid delimiter in context.
173 (not (looking-at
174 "<\\(\\?\\|/?[A-Za-z>]\\|!\\([[A-Za-z]\\|--\\)\\)"))
175 ;; Check that it's not a net-enabling start tag
176 ;; nor an unclosed start-tag.
177 (looking-at (concat sgml-start-tag-regex "[/<]"))
178 ;; Nor an unclosed end-tag.
179 (looking-at "</[A-Za-z][-.A-Za-z0-9]*[ \t]*<"))
180 (setq blinkpos nil)))
181 (if blinkpos
182 ()
183 ;; See if it's the end of a processing instruction.
184 (goto-char oldpos)
185 (if (search-backward "<?" (point-min) t)
186 (let ((pipos (point)))
187 (if (and (search-forward ">" oldpos t)
188 (eq (point) oldpos))
189 (setq blinkpos pipos))))))
190 (if blinkpos
191 (progn
192 (goto-char blinkpos)
193 (if (pos-visible-in-window-p)
194 (sit-for 1)
195 (message "Matches %s"
196 (buffer-substring blinkpos
197 (progn (end-of-line)
198 (point)))))))))))
199
200;;; I doubt that null end tags are used much for large elements,
201;;; so use a small distance here.
202(defconst sgml-slash-distance 1000
203 "*If non-nil, is the maximum distance to search for matching /
204when / is inserted.")
205
206(defun sgml-slash (arg)
207 "Insert / and display any previous matching /.
208Two /s are treated as matching if the first / ends a net-enabling
209start tag, and the second / is the corresponding null end tag."
210 (interactive "p")
211 (insert-char ?/ arg)
212 (if (> arg 0)
213 (let ((oldpos (point))
214 (blinkpos)
215 (level 0))
216 (save-excursion
217 (save-restriction
218 (if sgml-slash-distance
219 (narrow-to-region (max (point-min)
220 (- (point) sgml-slash-distance))
221 oldpos))
222 (if (and (re-search-backward sgml-start-tag-regex (point-min) t)
223 (eq (match-end 0) (1- oldpos)))
224 ()
225 (goto-char (1- oldpos))
226 (while (and (not blinkpos)
227 (search-backward "/" (point-min) t))
228 (let ((tagend (save-excursion
229 (if (re-search-backward sgml-start-tag-regex
230 (point-min) t)
231 (match-end 0)
232 nil))))
233 (if (eq tagend (point))
234 (if (eq level 0)
235 (setq blinkpos (point))
236 (setq level (1- level)))
237 (setq level (1+ level)))))))
238 (if blinkpos
239 (progn
240 (goto-char blinkpos)
241 (if (pos-visible-in-window-p)
242 (sit-for 1)
243 (message "Matches %s"
244 (buffer-substring (progn
245 (beginning-of-line)
246 (point))
247 (1+ blinkpos))))))))))
248
249(defun sgml-validate (command)
250 "Validate an SGML document.
251Runs COMMAND, a shell command, in a separate process asynchronously
252with output going to the buffer *compilation*.
253You can then use the command \\[next-error] to find the next error message
254and move to the line in the SGML document that caused it."
255 (interactive
256 (list (read-string "Validate command: "
257 (or sgml-saved-validate-command
258 (concat sgml-validate-command
259 " "
260 (let ((name (buffer-file-name)))
261 (and name
262 (file-name-nondirectory name))))))))
263 (setq sgml-saved-validate-command command)
264 (compile1 command "No more errors"))
265
266;;; sgml-mode.el ends here
diff --git a/lisp/textmodes/two-column.el b/lisp/textmodes/two-column.el
new file mode 100644
index 00000000000..a2ccdd3fd60
--- /dev/null
+++ b/lisp/textmodes/two-column.el
@@ -0,0 +1,646 @@
1;;; two-column.el --- minor mode for editing of two-column text
2
3;; Author: Daniel Pfeiffer <pfeiffer@cix.cict.fr>
4;; Last-Modified: 14 May 1991
5;; Adapted-By: ESR
6
7;; Copyright (C) 1992 Free Software Foundation, Inc.
8
9;; This file is part of GNU Emacs.
10
11;; GNU Emacs is free software; you can redistribute it and/or modify
12;; it under the terms of the GNU General Public License as published by
13;; the Free Software Foundation; either version 1, or (at your option)
14;; any later version.
15
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
22;; along with GNU Emacs; see the file COPYING. If not, write to
23;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24
25;;; Commentary:
26
27;; --8<---- two-column.el ----8<--------8<--------8<--------8<--------8<-------
28;; Esperanto: English:
29
30;; Minora modalo por samtempa dukolumna Minor mode for simultaneous
31;; tajpado two-column editing
32
33;; ^Ci dataro estas ero de GNU Emacs. This file is part of GNU Emacs.
34
35;; GNU Emacs estas disdonata en la GNU Emacs is distributed in the hope
36;; espero ke ^gi estos utila, sed SEN that it will be useful, but WITHOUT
37;; IA GARANTIO. Neniu a^utoro a^u ANY WARRANTY. No author or
38;; disdonanto akceptas respondecon al distributor accepts responsibility
39;; iu ajn por la sekvoj de ^gia uzado, to anyone for the consequences of
40;; a^u ^cu ^gi serveblas al iu celo, using it or for whether it serves
41;; a^u e^c entute funkcias, se li ni any particular purpose or works at
42;; estas skribinta tion. Vidu la GNU all, unless he says so in writing.
43;; Emacs ^Generala Publika Licenco por Refer to the GNU Emacs General
44;; plenaj detaloj. Public License for full details.
45
46;; ^Ciu rajtas kopii, modifi kaj ree Everyone is granted permission to
47;; disdoni GNU Emacs, sed nur sub la copy, modify and redistribute GNU
48;; condi^coj priskribitaj en la GNU Emacs, but only under the conditions
49;; Emacs ^Generala Publika Licenco. described in the GNU Emacs General
50;; Kopio de tiu licenso estas supozata Public License. A copy of this
51;; donita al vi kune kun GNU Emacs, por license is supposed to have been
52;; ke vi sciu viajn rajtojn kaj given to you along with GNU Emacs so
53;; respondecojn. ^Gi devus esti en you can know your rights and
54;; dataro nomata COPYING. Inter responsibilities. It should be in a
55;; alia^joj, la notico pri kopirajto file named COPYING. Among other
56;; kaj ^ci notico devas esti gardata things, the copyright notice and
57;; sur ^ciuj kopioj. this notice must be preserved on all
58;; copies.
59
60
61;; Tiu minora modalo ebligas al vi This minor mode allows you to
62;; tajpi sendepende en du apudaj independently edit two adjacent
63;; bufroj. Vi havas tri eblecojn por buffers. You have three ways to
64;; eki ^gin. ^Ciu donas al vi start it up. Each gives you a
65;; horizontale disigatan fenestron, horizontally split window similar to
66;; simila al fina apareco de via the final outcome of your text:
67;; teksto:
68
69;; C-x 6 2 asocias novan bufron nomatan associates a new buffer called
70;; same, sed kun 2C/ anta^u. the same, but with 2C/
71;; prepended.
72
73;; C-x 6 b asocias alian bufron. Vi povas associates another buffer.
74;; anka^u asocii dataron, se vi This can be used to associate a
75;; ^jus anta^ue faris C-x C-f. file if you just did C-x C-f.
76
77;; C-x 6 u disigas jam dukolumnan tekston unmerges a two-column text into
78;; en du bufroj ekde la nuna two buffers from the current
79;; linio, kaj je la nuna kolumno. line and at the current column.
80;; La anta^uaj signoj (ofte The preceding characters (often
81;; tabeligilo a^u |) estas la tab or |) are the column
82;; kolumna disiganto. Linioj kiuj separator. Lines that don't
83;; ne enhavas ilin ne estas have them won't be separated.
84;; disigitaj. Kiel la kvara kaj Like the fourth and fifth line
85;; la kvina linio se vi disigas if you unmerge this file from
86;; ^ci dataron ekde la unua angla the first english word.
87;; vorto.
88
89;; Je ^cia flanko estas bufro, kiu On each side is a buffer that knows
90;; konas la alian. Kun la ordonoj C-x about the other. With the commands
91;; 6 SPC, C-x 6 DEL kaj C-x 6 RET oni C-x 6 SPC, C-x 6 DEL and C-x 6 RET
92;; povas suben- a^u supreniri unu you can simultaneously scroll up or
93;; ekranon, kaj subeniri linion, down by a screenfull and by a line
94;; samtempe en la du bufroj. Al la alia in both buffers. Empty lines are
95;; bufro estas aldonataj linioj se added to the other buffer if
96;; necesas, por ke vi vidu la saman necessary, so that you see the same
97;; parton. Per C-x 6 C-l vi povas part. With C-x 6 C-l you can
98;; recentrigi la linion. Kiam vi nur recenter the line. When you only
99;; plu havas unu el la du bufroj have one of the two buffers onscreen
100;; surekrane vi revidos la alian per you can get the other back with C-x
101;; denove C-x 6 2. 6 2 once more.
102
103;; Se vi volas meti longajn liniojn If you include long lines, i.e which
104;; (ekz. programerojn) en la kunigotan will span both columns (eg. source
105;; tekston, ili devas esti en la code), they should be in what will
106;; estonte unua kolumno. La alia devas be the first column, with the
107;; havi malplenajn linion apud ili. associated buffer having empty lines
108;; next to them.
109
110;; Averto: en Emacs kiam vi ^san^gas la Attention: in Emacs when you change
111;; ma^joran modalon, la minoraj modaloj the major mode, the minor modes are
112;; estas anka^u elmemorigitaj. Tiu- also purged from memory. In that
113;; okaze vi devas religi la du bufrojn case you must reassociate the two
114;; per iu C-x 6-ordono, ekz. C-x 6 b. buffers with any C-x 6-command, e.g.
115;; C-x 6 b.
116
117;; Kiam vi estos kontenta de la When you have edited both buffers to
118;; rezulto, vi kunmetos la du kolumnojn your content, you merge them with
119;; per C-x 6 1. Se vi poste vidas C-x 6 1. If you then see a problem,
120;; problemon, vi neniigu la kunmeton you undo the merge with C-x u and
121;; per C-x u kaj plue modifu la du continue to edit the two buffers.
122;; bufrojn. Kiam vi ne plu volas tajpi When you no longer want to edit in
123;; dukolumne, vi eliru el la minora two columns, you turn off the minor
124;; modalo per C-x 6 k. mode with C-x 6 k.
125
126
127;; An^stata^u tri `autoload' kaj tri | Instead of three `autoload' and
128;; `global-set-key' vi povas uzi la | three `global-set-key' you can use
129;; jenon en via dataro ~/.emacs, por | the following in your file
130;; memstare ^car^gi la modalon: | ~/.emacs, to automatically load
131;; | the mode:
132
133;; (global-set-key "\C-x6"
134;; '(lambda () (interactive)
135;; (load-library "two-column")
136;; (call-interactively
137;; (cdr (assq (read-char) tc-mode-map)))))
138
139;; Se vi ^satus havi la dukolumnajn | If you'd like to have the
140;; ordonojn je funkciklavo <f2>, vi | two-column commands on function
141;; povas uzi la jenon en via dataro | key <f2>, you can use the
142;; ~/.emacs: | following in your file ~/.emacs:
143
144;; (define-key function-keymap "\^b"
145;; '(lambda () (interactive)
146;; (load-library "two-column")
147;; (define-key function-keymap "\^b" tc-mode-map)
148;; (call-interactively
149;; (cdr (assq (read-char) tc-mode-map)))))
150
151;; In addition to two-column editing of text, for example for writing a
152;; bilingual text side-by-side as shown below in the file's prolog, other
153;; interesting uses have been found for this minor mode:
154;;
155;;
156;; You can separate the columns with {+} C-x 6 u or <f2> u if you prefer
157;; any string that pleases you, by {+} handles these with a prefix argument
158;; setting tc-separator. For {+} that enables you to declare the
159;; example "{+} " if you like to {+} desired length of such a string.
160;; amuse yourself.
161;;
162;;
163;; keyword You can write any text corresponding to a
164;; given keyword in a filled paragraph next to
165;; it. Note that the width of the first column
166;; may be less than window-min-width in the
167;; result, but will be displayed at that width.
168;;
169;; another This is not a three- or multi-column mode.
170;; The example in the file's prolog required
171;; working on two columns and then treating the
172;; result as one column in order to add the
173;; third.
174;;
175;;
176;; Programmers might like the ability to split off the comment column of
177;; a file that looks like the following. The advantage is that with
178;; (setq fill-prefix "-- ") you can run M-q (fill-paragraph) on the
179;; comment. The problem is, code quickly gets rather wide, so you need
180;; to use a narrower comment column, which is less interesting, unless
181;; you have a 132-column screen. Code lines that reach beyond
182;; comment-column are no problem, except that you won't always see their
183;; end during editing.
184;;
185;; BEGIN -- This is just some meaningless
186;; FOR i IN 1..10 LOOP -- code in Ada, that runs foobar
187;; foobar( i ); -- once for each argument from one
188;; END LOOP; -- to ten, and then we're already
189;; END; -- through with it.
190;;
191;; Better yet, you can put the point before "This", type M-3 C-x 6 u
192;; which makes "-- " the separator between a no-comments Ada buffer, and
193;; a plain text comment buffer. When you put them back together, every
194;; non-empty line of the 2nd column will again be preceded by "-- ".
195;;
196;;
197;; The <f2> function key hack (which is one of the rare times when
198;; function keys are mnemonic) at the end of the file's prolog requires
199;; that the lisp/term/*.el for your terminal use the standard
200;; conventions. Too bad that some don't (at least not in version 18.55).
201;; The Sun one is hopelessly non-standard, and vt2[024]0 somehow forgot
202;; to define <f1> thru <f5>. (It defines <pf1> thru <pf4> instead, but
203;; that is not what we need on an X terminal.) If you want to use those,
204;; you'll need another hack something like:
205;;
206;; (if (string= (system-name) "cix")
207;; (progn
208;; (load-library "term/vt200.el")
209;; (define-key CSI-map "12~" (cons function-keymap ?\^b)))
210;; (global-unset-key "\e[")
211;; (define-key esc-map "[225z" (cons function-keymap ?\^b)))
212;;
213;; where "cix" is the non-sun machine I use. Actually I use the same X
214;; terminal to connect to both machines, and I want to keep my ~/.emacs
215;; identical on both. Bother, the two Emacses don't recognize the same
216;; keys and assign different sequences to those they do! I sure hope all
217;; this nonsense will stop with version 19 (or preferably soon) where I'd
218;; like to be able to say (define-key some-map '<f2> some-cmd), and see
219;; <f2> rather than some unintelligible ESC-sequence in command key
220;; sequences.
221
222;;; Code:
223
224;;;;; variable declarations ;;;;;
225
226(provide 'two-column)
227
228(defvar tc-prefix "\C-x6"
229 "Prefix tc-mode-map gets bound to.
230If you'd like to bind it to function key <f2>, see the prolog of the
231source file, lisp/two-column.el")
232
233(defvar tc-mode-map nil
234 "Keymap that contains all commands useful with two-column minor mode.
235This gets bound globally to `tc-prefix' since minor modes have
236no local keymap.")
237
238(if tc-mode-map
239 ()
240 (setq tc-mode-map (make-sparse-keymap))
241 (define-key tc-mode-map "1" 'tc-merge)
242 (define-key tc-mode-map "2" 'tc-split)
243 (define-key tc-mode-map "b" 'tc-associate-buffer)
244 (define-key tc-mode-map "k" 'tc-kill-association)
245 (define-key tc-mode-map "\C-l" 'tc-recenter)
246 (define-key tc-mode-map "o" 'tc-associated-buffer)
247 (define-key tc-mode-map "u" 'tc-unmerge)
248 (define-key tc-mode-map "{" 'shrink-window-horizontally)
249 (define-key tc-mode-map "}" 'enlarge-window-horizontally)
250 (define-key tc-mode-map " " 'tc-scroll-up)
251 (define-key tc-mode-map "\^?" 'tc-scroll-down)
252 (define-key tc-mode-map "\C-m" 'tc-scroll-line))
253
254(global-set-key tc-prefix tc-mode-map)
255
256
257;; markers seem to be the only buffer-id not affected by renaming
258;; a buffer. This nevertheless loses when a buffer is killed.
259(defvar tc-other nil
260 "Marker to the associated buffer, if non-nil.")
261(make-variable-buffer-local 'tc-other)
262
263
264(defvar tc-buffer-list ()
265 "An alist of markers to associated buffers. (Backs up `tc-other')")
266
267(setq minor-mode-alist (cons '(tc-other " 2C") minor-mode-alist))
268
269;; rearranged, so that the pertinent info will show in 40 columns
270(defvar tc-mode-line-format
271 '("-%*- %15b --" (-3 . "%p") "--%[(" mode-name
272 minor-mode-alist "%n" mode-line-process ")%]%-")
273 "*Value of mode-line-format for a buffer in two-column minor mode.")
274
275(defvar tc-separator ""
276 "*A string inserted between the two columns when merging.
277This gets set locally by \\[tc-unmerge].")
278
279(defvar tc-window-width 40
280 "*The width of the first column. (Must be at least `window-min-width')
281This value is local for every buffer that sets it.")
282(make-variable-buffer-local 'tc-window-width)
283
284(defvar tc-beyond-fill-column 4
285 "*Base for calculating `fill-column' for a buffer in two-column minor mode.
286The value of `fill-column' becomes `tc-window-width' for this buffer
287minus this value.")
288
289(defvar tc-mode-hook nil
290 "Function called, if non-nil, whenever turning on two-column minor mode.
291It can get called by \\[tc-split] (tc-split), \\[tc-unmerge] (tc-unmerge)
292and \\[tc-associate-buffer] (tc-associate-buffer), on both buffers.")
293
294;;;;; base functions ;;;;;
295
296;; the access method for the other buffer. this tries to remedy against
297;; lost local variables and lost buffers.
298(defun tc-other ()
299 (if (or tc-other
300 (setq tc-other
301 ; assoc with a different predicate, since we don't know
302 ; which marker points to this buffer
303 (let ((bl tc-buffer-list))
304 (while (and bl (not (eq (current-buffer)
305 (marker-buffer (car (car bl))))))
306 (setq bl (cdr bl)))
307 (cdr (car bl)))))
308 (or (prog1
309 (marker-buffer tc-other)
310 (setq mode-line-format tc-mode-line-format ))
311 ; The associated buffer somehow got killed.
312 (progn
313 ; The other variables may later be useful if the user
314 ; reestablishes the association.
315 (kill-local-variable 'tc-other)
316 (kill-local-variable 'mode-line-format)
317 nil))))
318
319(defun tc-split (&optional buffer)
320 "Split current window vertically for two-column editing.
321
322When called the first time, associates a buffer with the current
323buffer. Both buffers are put in two-column minor mode and
324tc-mode-hook gets called on both. These buffers remember
325about one another, even when renamed.
326
327When called again, restores the screen layout with the current buffer
328first and the associated buffer to it's right.
329
330If you include long lines, i.e which will span both columns (eg.
331source code), they should be in what will be the first column, with
332the associated buffer having empty lines next to them.
333
334You have the following commands at your disposal:
335
336\\[tc-split] Rearrange screen
337\\[tc-associate-buffer] Reassociate buffer after changing major mode
338\\[tc-scroll-up] Scroll both buffers up by a screenfull
339\\[tc-scroll-down] Scroll both buffers down by a screenful
340\\[tc-scroll-line] Scroll both buffers up by one or more lines
341\\[tc-recenter] Recenter and realign other buffer
342\\[shrink-window-horizontally], \\[enlarge-window-horizontally] Shrink, enlarge current column
343\\[tc-associated-buffer] Switch to associated buffer
344\\[tc-merge] Merge both buffers
345
346These keybindings can be customized in your ~/.emacs by `tc-prefix'
347and `tc-mode-map'.
348
349The appearance of the screen can be customized by the variables
350`tc-window-width', `tc-beyond-fill-column',
351`tc-mode-line-format' and `truncate-partial-width-windows'."
352
353 (interactive "P")
354 ; first go to full width, so that we can certainly split into
355 ; two windows
356 (if (< (window-width) (screen-width))
357 (enlarge-window 99999 t))
358 (split-window-horizontally
359 (max window-min-width (min tc-window-width
360 (- (screen-width) window-min-width))))
361 (if (tc-other)
362 (progn
363 (other-window 1)
364 (switch-to-buffer (tc-other))
365 (other-window -1)
366 ; align buffers if necessary
367 (tc-scroll-line 0))
368
369 ; set up minor mode linking two buffers
370 (setq fill-column (- tc-window-width
371 tc-beyond-fill-column)
372 mode-line-format tc-mode-line-format)
373 (run-hooks tc-mode-hook)
374 (let ((other (point-marker)))
375 (other-window 1)
376 (switch-to-buffer
377 (or buffer
378 (generate-new-buffer
379 (concat "2C/" (buffer-name)))))
380 (or buffer
381 (text-mode))
382 (setq fill-column (- tc-window-width
383 tc-beyond-fill-column)
384 mode-line-format tc-mode-line-format
385 tc-other other
386 other (point-marker))
387 (setq tc-buffer-list (cons (cons tc-other other)
388 tc-buffer-list))
389 (run-hooks tc-mode-hook)
390 (other-window -1)
391 (setq tc-buffer-list
392 (cons (cons other
393 (save-excursion
394 (set-buffer (tc-other))
395 tc-other))
396 tc-buffer-list))
397 (setq tc-other other))))
398
399(fset 'tc-mode 'tc-split)
400
401(defun tc-associate-buffer ()
402 "Associate another buffer with this one in two-column minor mode.
403Can also be used to associate a just previously visited file, by
404accepting the proposed default buffer.
405
406See \\[tc-split] and `lisp/two-column.el' for further details."
407 (interactive)
408 (let ((b1 (current-buffer))
409 (b2 (or (tc-other)
410 (read-buffer "Associate buffer: " (other-buffer)))))
411 (save-excursion
412 (setq tc-other nil)
413 (set-buffer b2)
414 (and (tc-other)
415 (not (eq b1 (tc-other)))
416 (error "Buffer already associated with buffer `%s'."
417 (buffer-name (tc-other))))
418 (setq b1 (and (assq 'tc-window-width (buffer-local-variables))
419 tc-window-width)))
420 ; if other buffer has a local width, adjust here too
421 (if b1 (setq tc-window-width (- (screen-width) b1)))
422 (tc-split b2)))
423
424(defun tc-unmerge (arg)
425 "Unmerge a two-column text into two buffers in two-column minor mode.
426The text is unmerged at the cursor's column which becomes the local
427value of tc-window-width. Only lines that have the ARG same
428preceding characters at that column get split. The ARG preceding
429characters without any leading whitespace become the local value for
430`tc-separator'. This way lines that continue across both
431columns remain untouched in the first buffer.
432
433This function can be used with a prototype line, to set up things as
434you like them. You write the first line of each column with the
435separator you like and then unmerge that line. E.g.:
436
437First column's text sSs Second columns text
438 \\___/\\
439 / \\
440 5 character Separator You type M-5 \\[tc-unmerge] with the point here
441
442See \\[tc-split] and `lisp/two-column.el' for further details."
443 (interactive "p")
444 (and (tc-other)
445 (if (y-or-n-p (concat "Overwrite associated buffer `"
446 (buffer-name (tc-other))
447 "'? "))
448 (save-excursion
449 (set-buffer (tc-other))
450 (erase-buffer))
451 (signal 'quit nil)))
452 (let ((point (point))
453 ; make next-line always come back to same column
454 (goal-column (current-column))
455 ; a counter for empty lines in other buffer
456 (n (1- (count-lines (point-min) (point))))
457 chars other)
458 (save-excursion
459 (backward-char arg)
460 (setq chars (buffer-substring (point) point))
461 (skip-chars-forward " \t" point)
462 (make-variable-buffer-local 'tc-separator)
463 (setq tc-separator (buffer-substring (point) point)
464 tc-window-width (current-column)))
465 (tc-split)
466 (setq other (tc-other))
467 ; now we're ready to actually unmerge
468 (save-excursion
469 (while (not (eobp))
470 (if (not (and (= (current-column) goal-column)
471 (string= chars
472 (buffer-substring (point)
473 (save-excursion
474 (backward-char arg)
475 (point))))))
476 (setq n (1+ n))
477 (setq point (point))
478 (backward-char arg)
479 (skip-chars-backward " \t")
480 (delete-region point (point))
481 (setq point (point))
482 (insert-char ?\n n)
483 (append-to-buffer other point (progn (end-of-line)
484 (if (eobp)
485 (point)
486 (1+ (point)))))
487 (delete-region point (point))
488 (setq n 0))
489 (next-line 1)))))
490
491(defun tc-kill-association ()
492 "Turn off two-column minor mode in current and associated buffer.
493If the associated buffer is unmodified and empty, it is killed."
494 (interactive)
495 (let ((buffer (current-buffer)))
496 (save-excursion
497 (and (tc-other)
498 (prog2
499 (setq tc-buffer-list
500 (delq (assq tc-other tc-buffer-list)
501 tc-buffer-list))
502 (set-buffer (tc-other))
503 (setq tc-buffer-list
504 (delq (assq tc-other tc-buffer-list)
505 tc-buffer-list)))
506 (or (not (tc-other))
507 (eq buffer (tc-other)))
508 (if (and (not (buffer-modified-p))
509 (eobp) (bobp))
510 (kill-buffer nil)
511 (kill-local-variable 'tc-other)
512 (kill-local-variable 'tc-window-width)
513 (kill-local-variable 'tc-separator)
514 (kill-local-variable 'mode-line-format)
515 (kill-local-variable 'fill-column))))
516 (kill-local-variable 'tc-other)
517 (kill-local-variable 'tc-window-width)
518 (kill-local-variable 'tc-separator)
519 (kill-local-variable 'mode-line-format)
520 (kill-local-variable 'fill-column)))
521
522
523;; this doesn't use yank-rectangle, so that the first column can
524;; contain long lines
525(defun tc-merge ()
526 "Merges the associated buffer with the current buffer.
527They get merged at the column, which is the value of
528`tc-window-width', i.e. usually at the vertical window
529separator. This separator gets replaced with white space. Beyond
530that the value of gets inserted on merged lines. The two columns are
531thus pasted side by side, in a single text. If the other buffer is
532not displayed to the left of this one, then this one becomes the left
533column.
534
535If you want `tc-separator' on empty lines in the second column,
536you should put just one space in them. In the final result, you can strip
537off trailing spaces with \\[beginning-of-buffer] \\[replace-regexp] [ SPC TAB ] + $ RET RET"
538
539 (interactive)
540 (or (tc-other)
541 (error "You must first set two-column minor mode."))
542 (and (> (car (window-edges)) 0) ; not touching left edge of screen
543 (eq (window-buffer (previous-window))
544 (tc-other))
545 (other-window -1))
546 (save-excursion
547 (let ((b1 (current-buffer))
548 (b2 (tc-other))
549 string)
550 (goto-char (point-min))
551 (set-buffer b2)
552 (goto-char (point-min))
553 (while (not (eobp))
554 (setq string (buffer-substring (point)
555 (progn (end-of-line) (point))))
556 (or (eobp)
557 (forward-char)) ; next line
558 (set-buffer b1)
559 (if (string= string "")
560 ()
561 (end-of-line)
562 (indent-to-column tc-window-width)
563 (insert tc-separator string))
564 (next-line 1) ; add one if necessary
565 (set-buffer b2))))
566 (if (< (window-width) (screen-width))
567 (enlarge-window 99999 t)))
568
569;;;;; utility functions ;;;;;
570
571(defun tc-associated-buffer ()
572 "Switch to associated buffer."
573 (interactive)
574 (or (tc-other)
575 (error "You must set two-column minor mode."))
576 (if (get-buffer-window (tc-other))
577 (select-window (get-buffer-window (tc-other)))
578 (switch-to-buffer (tc-other))))
579
580;; It would be desirable to intercept anything that causes the current
581;; window to scroll. Maybe a `scroll-hook'?
582(defun tc-scroll-line (arg)
583 "Scroll current window upward by ARG lines.
584The associated window gets scrolled to the same line."
585 (interactive "p")
586 (or (tc-other)
587 (error "You must set two-column minor mode."))
588 ; scroll-up has a bug on arg 0 at end of buffer
589 (or (zerop arg)
590 (scroll-up arg))
591 (setq arg (count-lines (point-min) (window-start)))
592 ; too bad that pre 18.57 Emacs makes save-window-excursion restore
593 ; the point. When it becomes extinct, we can simplify this.
594 (if (get-buffer-window (tc-other))
595 (let ((window (selected-window)))
596 (select-window (get-buffer-window (tc-other)))
597 (setq arg (- arg (count-lines (point-min) (window-start))))
598 ; make sure that other buffer has enough lines
599 (save-excursion
600 (goto-char (point-max))
601 (insert-char ?\n
602 (- arg (count-lines (window-start) (point-max)) -1)))
603 (or (zerop arg)
604 (scroll-up arg))
605 (select-window window))))
606
607(defun tc-scroll-up (arg)
608 "Scroll current window upward by ARG screens.
609The associated window gets scrolled to the same line."
610 (interactive "p")
611 (tc-scroll-line (* arg (- (window-height)
612 next-screen-context-lines 1))))
613
614(defun tc-scroll-down (arg)
615 "Scroll current window downward by ARG screens.
616The associated window gets scrolled to the same line."
617 (interactive "p")
618 (tc-scroll-line (* arg (- next-screen-context-lines
619 (window-height) -1))))
620
621(defun tc-recenter (arg)
622 "Center point in window. With ARG, put point on line ARG.
623This counts from bottom if ARG is negative. The associated window
624gets scrolled to the same line."
625 (interactive "P")
626 (setq arg (and arg (prefix-numeric-value arg)))
627 (tc-scroll-line (- (count-lines (window-start) (point))
628 (cond ((null arg) (/ (window-height) 2))
629 ((< arg 0) (+ (window-height) arg))
630 ( arg)))))
631
632(defun enlarge-window-horizontally (arg)
633 "Make current window ARG columns wider."
634 (interactive "p")
635 (enlarge-window arg t)
636 (and (tc-other)
637 (setq tc-window-width (+ tc-window-width arg))
638 (set-buffer (tc-other))
639 (setq tc-window-width (- tc-window-width arg))))
640
641(defun shrink-window-horizontally (arg)
642 "Make current window ARG columns narrower."
643 (interactive "p")
644 (enlarge-window-horizontally (- arg)))
645
646;;; two-column.el ends here