comparison lisp/textmodes/page-ext.el @ 49599:5ade352e8d1c

Trailing whitespace deleted.
author Juanma Barranquero <lekktu@gmail.com>
date Tue, 04 Feb 2003 13:30:45 +0000
parents 28ae6b9b086e
children 77c73732b535
comparison
equal deleted inserted replaced
49598:0d8b17d428b5 49599:5ade352e8d1c
49 49
50 ;; next-page C-x C-p C-n 50 ;; next-page C-x C-p C-n
51 ;; previous-page C-x C-p C-p 51 ;; previous-page C-x C-p C-p
52 ;; search-pages C-x C-p C-s 52 ;; search-pages C-x C-p C-s
53 ;; add-new-page C-x C-p C-a 53 ;; add-new-page C-x C-p C-a
54 ;; sort-pages-buffer C-x C-p s 54 ;; sort-pages-buffer C-x C-p s
55 ;; set-page-delimiter C-x C-p C-l 55 ;; set-page-delimiter C-x C-p C-l
56 ;; pages-directory C-x C-p C-d 56 ;; pages-directory C-x C-p C-d
57 ;; pages-directory-for-addresses C-x C-p d 57 ;; pages-directory-for-addresses C-x C-p d
58 ;; pages-directory-goto C-c C-c 58 ;; pages-directory-goto C-c C-c
59 59
60 60
61 ;;; Using the page commands 61 ;;; Using the page commands
62 62
63 ;; The page commands are helpful in several different contexts. For 63 ;; The page commands are helpful in several different contexts. For
64 ;; example, programmers often divide source files into sections using the 64 ;; example, programmers often divide source files into sections using the
65 ;; `page-delimiter'; you can use the `pages-directory' command to list 65 ;; `page-delimiter'; you can use the `pages-directory' command to list
66 ;; the sections. 66 ;; the sections.
67 67
95 ;; small data base. Put each address or entry on its own page. The 95 ;; small data base. Put each address or entry on its own page. The
96 ;; first line of text in each page is a `header line' and is listed by 96 ;; first line of text in each page is a `header line' and is listed by
97 ;; the `pages-directory' or `pages-directory-for-addresses' command. 97 ;; the `pages-directory' or `pages-directory-for-addresses' command.
98 98
99 ;; Specifically: 99 ;; Specifically:
100 ;; 100 ;;
101 ;; 1. Begin each entry with a `page-delimiter' (which is, by default, 101 ;; 1. Begin each entry with a `page-delimiter' (which is, by default,
102 ;; `^L' at the beginning of the line). 102 ;; `^L' at the beginning of the line).
103 ;; 103 ;;
104 ;; 2. The first line of text in each entry is the `heading line'; it 104 ;; 2. The first line of text in each entry is the `heading line'; it
105 ;; will appear in the pages-directory-buffer which is constructed 105 ;; will appear in the pages-directory-buffer which is constructed
106 ;; using the `C-x C-p C-d' (pages-directory) command or the `C-x 106 ;; using the `C-x C-p C-d' (pages-directory) command or the `C-x
107 ;; C-p d' (pages-directory-for-addresses) command. 107 ;; C-p d' (pages-directory-for-addresses) command.
108 ;; 108 ;;
109 ;; The heading line may be on the same line as the page-delimiter 109 ;; The heading line may be on the same line as the page-delimiter
110 ;; or it may follow after. It is the first non-blank line on the 110 ;; or it may follow after. It is the first non-blank line on the
111 ;; page. Conventionally, the heading line is placed on the line 111 ;; page. Conventionally, the heading line is placed on the line
112 ;; immediately following the line containing page-delimiter. 112 ;; immediately following the line containing page-delimiter.
113 ;; 113 ;;
115 ;; extends up to the next `page-delimiter'. The body may be of any 115 ;; extends up to the next `page-delimiter'. The body may be of any
116 ;; length. It is conventional to place a blank line after the last 116 ;; length. It is conventional to place a blank line after the last
117 ;; line of the body. 117 ;; line of the body.
118 118
119 ;; For example, a file might look like this: 119 ;; For example, a file might look like this:
120 ;; 120 ;;
121 ;; FSF 121 ;; FSF
122 ;; Free Software Foundation 122 ;; Free Software Foundation
123 ;; 59 Temple Place - Suite 330 123 ;; 59 Temple Place - Suite 330
124 ;; Boston, MA 02111-1307 USA. 124 ;; Boston, MA 02111-1307 USA.
125 ;; (617) 542-5942 125 ;; (617) 542-5942
126 ;; gnu@gnu.org 126 ;; gnu@gnu.org
127 ;; 127 ;;
128 ;; 128 ;;
129 ;; House Subcommittee on Intellectual Property, 129 ;; House Subcommittee on Intellectual Property,
130 ;; U.S. House of Representatives, 130 ;; U.S. House of Representatives,
131 ;; Washington, DC 20515 131 ;; Washington, DC 20515
132 ;; 132 ;;
133 ;; Congressional committee concerned with permitting or preventing 133 ;; Congressional committee concerned with permitting or preventing
134 ;; monopolistic restrictions on the use of software technology. 134 ;; monopolistic restrictions on the use of software technology.
135 ;; 135 ;;
136 ;; 136 ;;
137 ;; George Lakoff 137 ;; George Lakoff
138 ;; ``Women, Fire, and Dangerous Things: 138 ;; ``Women, Fire, and Dangerous Things:
139 ;; What Categories Reveal about the Mind'' 139 ;; What Categories Reveal about the Mind''
140 ;; 1987, Univ. of Chicago Press 140 ;; 1987, Univ. of Chicago Press
141 ;; 141 ;;
142 ;; About philosophy, Whorfian effects, and linguistics. 142 ;; About philosophy, Whorfian effects, and linguistics.
143 ;; 143 ;;
144 ;; 144 ;;
145 ;; OBI (On line text collection.) 145 ;; OBI (On line text collection.)
146 ;; Open Book Initiative 146 ;; Open Book Initiative
147 ;; c/o Software Tool & Die 147 ;; c/o Software Tool & Die
148 ;; 1330 Beacon St, Brookline, MA 02146 USA 148 ;; 1330 Beacon St, Brookline, MA 02146 USA
149 ;; (617) 739-0202 149 ;; (617) 739-0202
150 ;; obi@world.std.com 150 ;; obi@world.std.com
151 151
152 ;; In this example, the heading lines are: 152 ;; In this example, the heading lines are:
153 ;; 153 ;;
154 ;; FSF 154 ;; FSF
221 ;; header lines that are part of pages that contain matches to the 221 ;; header lines that are part of pages that contain matches to the
222 ;; regexp. In the example above, `C-u C-x C-p C-d 617 RET' would 222 ;; regexp. In the example above, `C-u C-x C-p C-d 617 RET' would
223 ;; match the telephone area code of the first and fourth entries, so 223 ;; match the telephone area code of the first and fourth entries, so
224 ;; only the header lines of those two entries would appear in the 224 ;; only the header lines of those two entries would appear in the
225 ;; pages-directory-buffer. 225 ;; pages-directory-buffer.
226 ;; 226 ;;
227 ;; Called with a numeric argument, the `pages-directory' command 227 ;; Called with a numeric argument, the `pages-directory' command
228 ;; lists the number of lines in each page. This is helpful when you 228 ;; lists the number of lines in each page. This is helpful when you
229 ;; are printing hardcopy. 229 ;; are printing hardcopy.
230 230
231 ;; Called with a negative numeric argument, the `pages-directory' 231 ;; Called with a negative numeric argument, the `pages-directory'
232 ;; command lists the lengths of pages whose contents match a regexp. 232 ;; command lists the lengths of pages whose contents match a regexp.
233 233
234 ;;; Code: 234 ;;; Code:
350 350
351 Narrow to new page if 351 Narrow to new page if
352 pages-directory-for-adding-page-narrowing-p variable 352 pages-directory-for-adding-page-narrowing-p variable
353 is non-nil. 353 is non-nil.
354 354
355 Page begins with a `^L' as the default page-delimiter. 355 Page begins with a `^L' as the default page-delimiter.
356 Use \\[set-page-delimiter] to change the page-delimiter. 356 Use \\[set-page-delimiter] to change the page-delimiter.
357 Point is left in the body of page." 357 Point is left in the body of page."
358 (interactive "sHeader line: ") 358 (interactive "sHeader line: ")
359 (widen) 359 (widen)
360 ;; If in pages directory buffer 360 ;; If in pages directory buffer
361 (if (eq major-mode 'pages-directory-mode) 361 (if (eq major-mode 'pages-directory-mode)
362 (progn 362 (progn
363 ;; Add new page before or after current page? 363 ;; Add new page before or after current page?
364 (if pages-directory-for-adding-new-page-before-current-page-p 364 (if pages-directory-for-adding-new-page-before-current-page-p
365 (pages-directory-goto) 365 (pages-directory-goto)
366 (pages-directory-goto) 366 (pages-directory-goto)
376 (goto-char (point-min)) 376 (goto-char (point-min))
377 (insert 377 (insert
378 (format "%s\n" 378 (format "%s\n"
379 ;; Remove leading `^' from page-delimiter string 379 ;; Remove leading `^' from page-delimiter string
380 (if (eq '^ (car (read-from-string page-delimiter))) 380 (if (eq '^ (car (read-from-string page-delimiter)))
381 (substring page-delimiter 1)))) 381 (substring page-delimiter 1))))
382 (goto-char (point-min)))) 382 (goto-char (point-min))))
383 ;; Insert page delimiter at beginning of line. 383 ;; Insert page delimiter at beginning of line.
384 (if (not (looking-at "^.")) (forward-line 1)) 384 (if (not (looking-at "^.")) (forward-line 1))
385 (insert (format "%s\n%s\n\n\n" 385 (insert (format "%s\n%s\n\n\n"
386 (if (eq '^ (car (read-from-string page-delimiter))) 386 (if (eq '^ (car (read-from-string page-delimiter)))
387 (substring page-delimiter 1)) 387 (substring page-delimiter 1))
388 header-line)) 388 header-line))
389 (forward-line -1) 389 (forward-line -1)
390 (and pages-directory-for-adding-page-narrowing-p (narrow-to-page))) 390 (and pages-directory-for-adding-page-narrowing-p (narrow-to-page)))
409 ;;; Sorting pages 409 ;;; Sorting pages
410 410
411 (autoload 'sort-subr "sort" "Primary function for sorting." t nil) 411 (autoload 'sort-subr "sort" "Primary function for sorting." t nil)
412 412
413 (defun sort-pages-in-region (reverse beg end) 413 (defun sort-pages-in-region (reverse beg end)
414 "Sort pages in region alphabetically. Prefix arg means reverse order. 414 "Sort pages in region alphabetically. Prefix arg means reverse order.
415 415
416 Called from a program, there are three arguments: 416 Called from a program, there are three arguments:
417 REVERSE (non-nil means reverse order), BEG and END (region to sort)." 417 REVERSE (non-nil means reverse order), BEG and END (region to sort)."
418 418
419 ;;; This sort function handles ends of pages differently than 419 ;;; This sort function handles ends of pages differently than
444 t) 444 t)
445 (goto-char (match-beginning 0)) 445 (goto-char (match-beginning 0))
446 (goto-char (point-max)))))))) 446 (goto-char (point-max))))))))
447 447
448 (defun sort-pages-buffer (&optional reverse) 448 (defun sort-pages-buffer (&optional reverse)
449 "Sort pages alphabetically in buffer. Prefix arg means reverse order. 449 "Sort pages alphabetically in buffer. Prefix arg means reverse order.
450 \(Non-nil arg if not interactive.\)" 450 \(Non-nil arg if not interactive.\)"
451 451
452 (interactive "P") 452 (interactive "P")
453 (or reverse (setq reverse nil)) 453 (or reverse (setq reverse nil))
454 (widen) 454 (widen)
494 Called interactively with a prefix argument, reset `page-delimiter' to 494 Called interactively with a prefix argument, reset `page-delimiter' to
495 its original value. 495 its original value.
496 496
497 In a program, non-nil second arg causes first arg to be ignored and 497 In a program, non-nil second arg causes first arg to be ignored and
498 resets the page-delimiter to the original value." 498 resets the page-delimiter to the original value."
499 499
500 (interactive 500 (interactive
501 (if current-prefix-arg 501 (if current-prefix-arg
502 (list original-page-delimiter "^\f") 502 (list original-page-delimiter "^\f")
503 (list (read-string "Set page-delimiter to regexp: " page-delimiter) 503 (list (read-string "Set page-delimiter to regexp: " page-delimiter)
504 nil))) 504 nil)))
542 non-nil second arg means print numbers of lines in each page; if first 542 non-nil second arg means print numbers of lines in each page; if first
543 arg is nil, optional third arg is regular expression. 543 arg is nil, optional third arg is regular expression.
544 544
545 If the buffer is narrowed, the `pages-directory' command creates a 545 If the buffer is narrowed, the `pages-directory' command creates a
546 directory for only the accessible portion of the buffer." 546 directory for only the accessible portion of the buffer."
547 547
548 (interactive 548 (interactive
549 (cond ((not current-prefix-arg) 549 (cond ((not current-prefix-arg)
550 (list t nil nil)) 550 (list t nil nil))
551 ((listp current-prefix-arg) 551 ((listp current-prefix-arg)
552 (list nil 552 (list nil
553 nil 553 nil
554 (read-string 554 (read-string
555 (format "Select according to `%s' (end with RET): " 555 (format "Select according to `%s' (end with RET): "
556 (or pages-directory-previous-regexp "regexp"))))) 556 (or pages-directory-previous-regexp "regexp")))))
557 ((> (prefix-numeric-value current-prefix-arg) 0) 557 ((> (prefix-numeric-value current-prefix-arg) 0)
558 (list t t nil)) 558 (list t t nil))
559 ((< (prefix-numeric-value current-prefix-arg) 0) 559 ((< (prefix-numeric-value current-prefix-arg) 0)
560 (list nil 560 (list nil
561 t 561 t
562 (read-string 562 (read-string
563 (format "Select according to `%s' (end with RET): " 563 (format "Select according to `%s' (end with RET): "
564 (or pages-directory-previous-regexp "regexp"))))))) 564 (or pages-directory-previous-regexp "regexp")))))))
565 565
566 (if (equal regexp "") 566 (if (equal regexp "")
567 (setq regexp pages-directory-previous-regexp) 567 (setq regexp pages-directory-previous-regexp)
568 (setq pages-directory-previous-regexp regexp)) 568 (setq pages-directory-previous-regexp regexp))
569 569
570 (if (interactive-p) 570 (if (interactive-p)
571 (message "Creating directory for: %s " 571 (message "Creating directory for: %s "
572 (buffer-name))) 572 (buffer-name)))
573 573
574 (let ((pages-target-buffer (current-buffer)) 574 (let ((pages-target-buffer (current-buffer))
575 (pages-directory-buffer 575 (pages-directory-buffer
576 (concat pages-directory-prefix " " (buffer-name))) 576 (concat pages-directory-prefix " " (buffer-name)))
577 (linenum 1) 577 (linenum 1)
578 (pages-buffer-original-position (point)) 578 (pages-buffer-original-position (point))
579 (pages-buffer-original-page 0)) 579 (pages-buffer-original-page 0))
580 580
581 ;; `with-output-to-temp-buffer' binds the value of the variable 581 ;; `with-output-to-temp-buffer' binds the value of the variable
582 ;; `standard-output' to the buffer named as its first argument, 582 ;; `standard-output' to the buffer named as its first argument,
583 ;; but does not switch to that buffer. 583 ;; but does not switch to that buffer.
584 (with-output-to-temp-buffer pages-directory-buffer 584 (with-output-to-temp-buffer pages-directory-buffer
585 (save-excursion 585 (save-excursion
587 (pages-directory-mode) 587 (pages-directory-mode)
588 (insert 588 (insert
589 "==== Pages Directory: use `C-c C-c' to go to page under cursor. ====" ?\n) 589 "==== Pages Directory: use `C-c C-c' to go to page under cursor. ====" ?\n)
590 (setq pages-buffer pages-target-buffer) 590 (setq pages-buffer pages-target-buffer)
591 (setq pages-pos-list nil)) 591 (setq pages-pos-list nil))
592 592
593 (if pages-list-all-headers-p 593 (if pages-list-all-headers-p
594 594
595 ;; 1. If no prefix argument, list all headers 595 ;; 1. If no prefix argument, list all headers
596 (save-excursion 596 (save-excursion
597 (goto-char (point-min)) 597 (goto-char (point-min))
598 598
599 ;; (a) Point is at beginning of buffer; but the first 599 ;; (a) Point is at beginning of buffer; but the first
600 ;; page may not begin with a page-delimiter 600 ;; page may not begin with a page-delimiter
601 (save-restriction 601 (save-restriction
602 ;; If page delimiter is at beginning of buffer, skip it 602 ;; If page delimiter is at beginning of buffer, skip it
603 (if (and (save-excursion 603 (if (and (save-excursion
604 (re-search-forward page-delimiter nil t)) 604 (re-search-forward page-delimiter nil t))
605 (= 1 (match-beginning 0))) 605 (= 1 (match-beginning 0)))
606 (goto-char (match-end 0))) 606 (goto-char (match-end 0)))
607 (narrow-to-page) 607 (narrow-to-page)
608 (pages-copy-header-and-position count-lines-p)) 608 (pages-copy-header-and-position count-lines-p))
609 609
610 ;; (b) Search within pages buffer for next page-delimiter 610 ;; (b) Search within pages buffer for next page-delimiter
611 (while (re-search-forward page-delimiter nil t) 611 (while (re-search-forward page-delimiter nil t)
612 (pages-copy-header-and-position count-lines-p))) 612 (pages-copy-header-and-position count-lines-p)))
613 613
614 ;; 2. Else list headers whose pages match regexp. 614 ;; 2. Else list headers whose pages match regexp.
615 (save-excursion 615 (save-excursion
616 ;; REMOVED save-restriction AND widen FROM HERE 616 ;; REMOVED save-restriction AND widen FROM HERE
617 (goto-char (point-min)) 617 (goto-char (point-min))
618 618
619 ;; (a) Handle first page 619 ;; (a) Handle first page
620 (save-restriction 620 (save-restriction
621 (narrow-to-page) 621 (narrow-to-page)
622 ;; search for selection regexp 622 ;; search for selection regexp
623 (if (save-excursion (re-search-forward regexp nil t)) 623 (if (save-excursion (re-search-forward regexp nil t))
624 (pages-copy-header-and-position count-lines-p))) 624 (pages-copy-header-and-position count-lines-p)))
625 625
626 ;; (b) Search for next page-delimiter 626 ;; (b) Search for next page-delimiter
627 (while (re-search-forward page-delimiter nil t) 627 (while (re-search-forward page-delimiter nil t)
628 (save-restriction 628 (save-restriction
629 (narrow-to-page) 629 (narrow-to-page)
630 ;; search for selection regexp 630 ;; search for selection regexp
631 (if (save-excursion (re-search-forward regexp nil t)) 631 (if (save-excursion (re-search-forward regexp nil t))
632 (pages-copy-header-and-position count-lines-p) 632 (pages-copy-header-and-position count-lines-p)
633 ))))) 633 )))))
634 634
635 (set-buffer standard-output) 635 (set-buffer standard-output)
636 ;; Put positions in increasing order to go with buffer. 636 ;; Put positions in increasing order to go with buffer.
637 (setq pages-pos-list (nreverse pages-pos-list)) 637 (setq pages-pos-list (nreverse pages-pos-list))
638 (if (interactive-p) 638 (if (interactive-p)
639 (message "%d matching lines in: %s" 639 (message "%d matching lines in: %s"
650 650
651 (defun pages-copy-header-and-position (count-lines-p) 651 (defun pages-copy-header-and-position (count-lines-p)
652 "Copy page header and its position to the Pages Directory. 652 "Copy page header and its position to the Pages Directory.
653 Only arg non-nil, count lines in page and insert before header. 653 Only arg non-nil, count lines in page and insert before header.
654 Used by `pages-directory' function." 654 Used by `pages-directory' function."
655 655
656 (let (position line-count) 656 (let (position line-count)
657 657
658 (if count-lines-p 658 (if count-lines-p
659 (save-excursion 659 (save-excursion
660 (save-restriction 660 (save-restriction
663 663
664 ;; Keep track of page for later cursor positioning 664 ;; Keep track of page for later cursor positioning
665 (if (<= (point) pages-buffer-original-position) 665 (if (<= (point) pages-buffer-original-position)
666 (setq pages-buffer-original-page 666 (setq pages-buffer-original-page
667 (1+ pages-buffer-original-page))) 667 (1+ pages-buffer-original-page)))
668 668
669 (save-excursion 669 (save-excursion
670 ;; go to first non-blank char after the page-delimiter 670 ;; go to first non-blank char after the page-delimiter
671 (skip-chars-forward " \t\n") 671 (skip-chars-forward " \t\n")
672 ;; set the marker here; this the place to which the 672 ;; set the marker here; this the place to which the
673 ;; `pages-directory-goto' command will go 673 ;; `pages-directory-goto' command will go
674 (setq position (make-marker)) 674 (setq position (make-marker))
675 (set-marker position (point)) 675 (set-marker position (point))
676 (let ((start (point)) 676 (let ((start (point))
677 (end (save-excursion (end-of-line) (point))) 677 (end (save-excursion (end-of-line) (point)))
678 inserted-at) 678 inserted-at)
679 ;; change to directory buffer 679 ;; change to directory buffer
680 (set-buffer standard-output) 680 (set-buffer standard-output)
681 ;; record page position 681 ;; record page position
682 (setq pages-pos-list (cons position pages-pos-list)) 682 (setq pages-pos-list (cons position pages-pos-list))
683 ;; insert page header 683 ;; insert page header
684 (setq inserted-at (point)) 684 (setq inserted-at (point))
685 (insert-buffer-substring pages-target-buffer start end) 685 (insert-buffer-substring pages-target-buffer start end)
686 (add-text-properties inserted-at (point) 686 (add-text-properties inserted-at (point)
687 '(mouse-face highlight 687 '(mouse-face highlight
688 help-echo "mouse-2: go to this page")) 688 help-echo "mouse-2: go to this page"))
689 (put-text-property inserted-at (point) 'rear-nonsticky 'highlight)) 689 (put-text-property inserted-at (point) 'rear-nonsticky 'highlight))
690 690
691 (if count-lines-p 691 (if count-lines-p
692 (save-excursion 692 (save-excursion
693 (beginning-of-line) 693 (beginning-of-line)
694 (insert (format "%3d: " line-count)))) 694 (insert (format "%3d: " line-count))))
695 695
696 (terpri)) 696 (terpri))
697 (end-of-line 1))) 697 (end-of-line 1)))
698 698
699 (defun pages-directory-mode () 699 (defun pages-directory-mode ()
700 "Mode for handling the pages-directory buffer. 700 "Mode for handling the pages-directory buffer.
725 (beginning-of-line) 725 (beginning-of-line)
726 (let* ((pages-number (1- (count-lines (point-min) (point)))) 726 (let* ((pages-number (1- (count-lines (point-min) (point))))
727 (pos (nth pages-number pages-pos-list)) 727 (pos (nth pages-number pages-pos-list))
728 (end-of-directory-p (eobp)) 728 (end-of-directory-p (eobp))
729 (narrowing-p pages-directory-buffer-narrowing-p)) 729 (narrowing-p pages-directory-buffer-narrowing-p))
730 (pop-to-buffer pages-buffer) 730 (pop-to-buffer pages-buffer)
731 (widen) 731 (widen)
732 (if end-of-directory-p 732 (if end-of-directory-p
733 (goto-char (point-max)) 733 (goto-char (point-max))
734 (goto-char (marker-position pos))) 734 (goto-char (marker-position pos)))
735 (if narrowing-p (narrow-to-page)))) 735 (if narrowing-p (narrow-to-page))))
757 757
758 If pages-directory-for-addresses-goto-narrowing-p is non-nil, 758 If pages-directory-for-addresses-goto-narrowing-p is non-nil,
759 `pages-directory-goto' narrows addresses buffer to entry. 759 `pages-directory-goto' narrows addresses buffer to entry.
760 760
761 If pages-directory-for-addresses-buffer-keep-windows-p is nil, 761 If pages-directory-for-addresses-buffer-keep-windows-p is nil,
762 this command deletes other windows when it displays the addresses 762 this command deletes other windows when it displays the addresses
763 directory." 763 directory."
764 764
765 (interactive 765 (interactive
766 (list (if current-prefix-arg 766 (list (if current-prefix-arg
767 (read-file-name "Filename: " pages-addresses-file-name)))) 767 (read-file-name "Filename: " pages-addresses-file-name))))