entered into RCS
[emacs.git] / lisp / textmodes / page-ext.el
bloba351dcb5e3d042cbf46276eb0a51864e86565613
1 ;;; page-ext.el --- page handling commands
3 ;;; Copyright (C) 1990 Free Software Foundation
5 ;; Author: Robert J. Chassell <bob@gnu.ai.mit.edu>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to
21 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23 ;;; Commentary:
25 ;;; You may use these commands to handle an address list or other
26 ;;; small data base.
28 ;;; Change Log:
30 ;;; Change Log ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
31 ;;;
32 ;;; Version 0.043
33 ;;; 24 May 1990 - When the cursor is at the end of the pages directory
34 ;;; buffer (which is empty), a `C-c C-c' (pages-directory-goto)
35 ;;; command now takes you to the end of the buffer.
36 ;;;
37 ;;; Version 0.042
38 ;;; 16 May 1990 - Since people often handle address and other files
39 ;;; differently, variable `pages-directory-for-addresses-narrowing-p'
40 ;;; now specifies whether `pages-directory-goto' should narrow
41 ;;; addresses buffer to entry to which it goes.
42 ;;; `pages-directory-buffer-narrowing-p' continues to control
43 ;;; narrowing of pages buffer.
44 ;;;
45 ;;; `add-new-page' documentation string now explains
46 ;;; that the value of the inserted page-delimiter is a `^L'.
47 ;;;
48 ;;; `pages-directory-previous-regexp' definition reworded.
49 ;;;
50 ;;; Removed unneeded defvar for `pages-directory-buffer'.
51 ;;;
52 ;;; Version 0.041
53 ;;; 14 May 1990 - `pages-last-search' bound to nil initially.
54 ;;; Remove unnecessary lines from `search-pages' definition.
55 ;;;
56 ;;; Version 0.04
57 ;;; 18 Mar 1990 - `pages-directory' creates a directory for only the
58 ;;; accessible portion of the buffer; it does not automatically widen
59 ;;; the buffer.
60 ;;;
61 ;;; However, `pages-directory-for-addresses' does widen the addresses'
62 ;;; buffer before constructing the addresses' directory.
63 ;;;
64 ;;; Version 0.032
65 ;;; 20 Feb 1990 - `pages-directory-for-addresses' no longer copies
66 ;;; first line of addresses directory to kill-ring
67 ;;;
68 ;;; Remove `(kill-all-local-variables)' line from
69 ;;; `pages-directory-address-mode' so Emacs will not be told to forget
70 ;;; the name of the file containing the addresses!
71 ;;;
72 ;;; Version 0.031
73 ;;; 15 Feb 1990 - `pages-directory-goto' no longer erroneously selects
74 ;;; the entry on the following line when the cursor is at the end of
75 ;;; the line, but selects the entry on which the cursor rests.
76 ;;;
77 ;;; `pages-directory-address-mode' now sets local variables and enables
78 ;;; `describe-mode' to describe Addresses Directory mode.
79 ;;;
80 ;;; `pages-directory-for-addresses' now sets the buffer-modifed flag
81 ;;; for the Addresses Directory to nil.
82 ;;;
83 ;;; The documentation string for both `pages-directory-mode' and
84 ;;; `pages-directory-address-mode' now provide a lookup for the
85 ;;; `pages-directory-goto' keybinding.
86 ;;;
87 ;;; Version 0.03
88 ;;; 10 Feb 1990 - Incorporated a specialized extension of the
89 ;;; `pages-directory' command called `pages-directory-for-addresses'
90 ;;; and bound it to ctl-x-ctl-p-map "d" for integration with other
91 ;;; page functions. This function finds a file, creates a directory
92 ;;; for it using the `pages-directory' command, and displays the
93 ;;; directory. It is primarily for lists of addresses and the like.
94 ;;;
95 ;;; The difference between this and the `pages-directory' command is
96 ;;; that the `pages-directory-for-addresses' command presumes a
97 ;;; default addresses file (although you may optionally specify a file
98 ;;; name) and it switches you to the directory for the file, but the
99 ;;; `pages-directory' command creates a directory for the current
100 ;;; buffer, and pops to the directory in another window.
102 ;;; `pages-directory' now places the cursor over the header line of
103 ;;; the page in which point was located in the pages buffer.
105 ;;; New `set-page-delimiter' command sets the buffer local value of
106 ;;; the page-delimiter variable. With prefix arg, resets function to
107 ;;; original value. (Quicker to use than `edit-options'.)
109 ;;; Version 0.02
110 ;;; 9 Feb 1990 - `pages-directory' now displays the
111 ;;; first line that contains a non-blank character that follows the
112 ;;; `page-delimiter'; this may be the rest of the line that contains
113 ;;; the `page-delimiter' or a line following. (In most instances, the
114 ;;; line containing a non-blank character is a line of text.)
115 ;;; Modification includes changes to `pages-copy-header-and-position'.
117 ;;; Each directory created by `pages-directory' now possesses a name
118 ;;; derived on the name of the pages buffer. Consequently, you may
119 ;;; create several different directories, one for each pages buffer.
121 ;;; `sort-pages-in-region' no longers requires the text to start on
122 ;;; the line immediately following the line containing the
123 ;;; page-delimiter.
125 ;;; `pages-directory-goto' no longer narrows to the page
126 ;;; automatically. Instead, if you wish it to narrow to the page, set
127 ;;; variable pages-directory-buffer-narrowing-p to a non-nil value.
128 ;;; Default is nil; this is an experiment to see whether it is useful
129 ;;; to see the surrounding context.
131 ;;; Version 0.011
132 ;;; 2 Feb 1990 - `add-new-page': removed extraneous space.
134 ;;; Version 0.01
135 ;;; 28 Jan 1990 - Initial definitions.
136 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
138 ;;; Code:
140 ;;;; Summary
142 ; The current page commands are:
144 ; forward-page C-x ]
145 ; backward-page C-x [
146 ; narrow-to-page C-x p
147 ; count-lines-page C-x l
148 ; mark-page C-x C-p (change this to C-x C-p C-m)
149 ; sort-pages not bound
150 ; what-page not bound
152 ; The new page handling commands all use `C-x C-p' as a prefix. This
153 ; means that the key binding for `mark-page' must be changed.
154 ; Otherwise, no other changes are made to the current commands or
155 ; their bindings.
157 ; New page handling commands:
159 ; next-page C-x C-p C-n
160 ; previous-page C-x C-p C-p
161 ; search-pages C-x C-p C-s
162 ; add-new-page C-x C-p C-a
163 ; sort-pages-buffer C-x C-p s
164 ; set-page-delimiter C-x C-p C-l
165 ; pages-directory C-x C-p C-d
166 ; pages-directory-for-addresses C-x C-p d
167 ; goto-page C-c C-c
170 ;;;; Using the page commands
172 ; The page commands are helpful in several different contexts. For
173 ; example, programmers often divide source files into sections using the
174 ; `page-delimiter'; you can use the `pages-directory' command to list
175 ; the sections.
177 ; You may change the buffer local value of the `page-delimiter' with
178 ; the `set-page-delimiter' command. This command is bound to `C-x C-p
179 ; C-l' The command prompts you for a new value for the page-delimiter.
180 ; Called with a prefix-arg, the command resets the value of the
181 ; page-delimiter to its original value.
184 ;;;; Handling an address list or small data base
186 ; You may use the page commands to handle an address list or other
187 ; small data base. Put each address or entry on its own page. The
188 ; first line of text in each page is a `header line' and is listed by
189 ; the `pages-directory' or `pages-directory-for-addresses' command.
191 ; Specifically:
193 ; 1. Begin each entry with a `page-delimiter' (which is, by default,
194 ; `^L' at the beginning of the line).
196 ; 2. The first line of text in each entry is the `heading line'; it
197 ; will appear in the pages-directory-buffer which is constructed
198 ; using the `C-x C-p C-d' (pages-directory) command or the `C-x
199 ; C-p d' (pages-directory-for-addresses) command.
201 ; The heading line may be on the same line as the page-delimiter
202 ; or it may follow after. It is the first non-blank line on the
203 ; page. Conventionally, the heading line is placed on the line
204 ; immediately following the line containing page-delimiter.
206 ; 3. Follow the heading line with the body of the entry. The body
207 ; extends up to the next `page-delimiter'. The body may be of any
208 ; length. It is conventional to place a blank line after the last
209 ; line of the body.
211 ; For example, a file might look like this:
213 ; FSF
214 ; Free Software Foundation
215 ; 675 Massachusetts Avenue
216 ; Cambridge, MA 02139 USA
217 ; (617) 876-3296
218 ; gnu@prep.ai.mit.edu
220 ; \f
221 ; House Subcommittee on Intellectual Property,
222 ; U.S. House of Representatives,
223 ; Washington, DC 20515
225 ; Congressional committee concerned with permitting or preventing
226 ; monopolistic restictions on the use of software technology
228 ; \f
229 ; George Lakoff
230 ; ``Women, Fire, and Dangerous Things:
231 ; What Categories Reveal about the Mind''
232 ; 1987, Univ. of Chicago Press
234 ; About philosophy, Whorfian effects, and linguistics.
236 ; \f
237 ; OBI (On line text collection.)
238 ; Open Book Initiative
239 ; c/o Software Tool & Die
240 ; 1330 Beacon St, Brookline, MA 02146 USA
241 ; (617) 739-0202
242 ; obi@world.std.com
244 ; In this example, the heading lines are:
246 ; FSF
247 ; House Subcommittee on Intellectual Property
248 ; George Lakoff
249 ; OBI (On line text collection.)
251 ; The `C-x C-p s' (sort-pages-buffer) command sorts the entries in the
252 ; buffer alphabetically.
254 ; You may use any of the page commands, including the `next-page',
255 ; `previous-page', `add-new-page', `mark-page', and `search-pages'
256 ; commands.
258 ; You may use either the `C-x C-p d' (pages-directory-for-addresses)
259 ; or the `C-x C-p C-d' (pages-directory) command to construct and
260 ; dislay a directory of all the heading lines.
262 ; In the directory, you may position the cursor over a heading line
263 ; and type `C-c C-c' to go to the entry to which it refers in the
264 ; pages buffer.
266 ; When used in conjunction with the `pages-directory-for-addresses'
267 ; command, the `C-c C-c' (pages-directory-goto) command narrows to the
268 ; entry to which it goes. But, when used in conjunction with the
269 ; `pages-directory' command, the `C-c C-c' (pages-directory-goto)
270 ; command does not narrow to the entry, but widens the buffer so you
271 ; can see the context surrounding the entry.
273 ; If you wish, you may create several different directories,
274 ; one for each different buffer.
276 ;; `pages-directory-for-addresses' in detail
278 ; The `pages-directory-for-addresses' assumes a default addresses
279 ; file. You do not need to specify the addresses file but merely type
280 ; `C-x C-p d' from any buffer. The command finds the file, constructs
281 ; a directory for it, and switches you to the directory. If you call
282 ; the command with a prefix arg, `C-u C-x C-p d', it prompts you for a
283 ; file name.
285 ;; `pages-directory' in detail
287 ; Call the `pages-directory' from the buffer for which you want a
288 ; directory created; it creates a directory for the buffer and pops
289 ; you to the directory.
291 ; The `pages-directory' command has several options:
293 ; Called with a prefix arg, `C-u C-x C-p C-d', the `pages-directory'
294 ; prompts you for a regular expression and only lists only those
295 ; header lines that are part of pages that contain matches to the
296 ; regexp. In the example above, `C-u C-x C-p C-d 617 RET' would
297 ; match the telephone area code of the first and fourth entries, so
298 ; only the header lines of those two entries would appear in the
299 ; pages-directory-buffer.
301 ; Called with a numeric argument, the `pages-directory' command
302 ; lists the number of lines in each page. This is helpful when you
303 ; are printing hardcopy.
305 ; Called with a negative numeric argument, the `pages-directory'
306 ; command lists the lengths of pages whose contents match a regexp.
309 ;;;; Key bindings for page handling functions
311 (global-unset-key "\C-x\C-p")
313 (defvar ctl-x-ctl-p-map (make-sparse-keymap)
314 "Keymap for subcommands of C-x C-p, which are for page handling.")
316 (define-key ctl-x-map "\C-p" 'ctl-x-ctl-p-prefix)
317 (fset 'ctl-x-ctl-p-prefix ctl-x-ctl-p-map)
319 (define-key ctl-x-ctl-p-map "\C-n" 'next-page)
320 (define-key ctl-x-ctl-p-map "\C-p" 'previous-page)
321 (define-key ctl-x-ctl-p-map "\C-a" 'add-new-page)
322 (define-key ctl-x-ctl-p-map "\C-m" 'mark-page)
323 (define-key ctl-x-ctl-p-map "\C-s" 'search-pages)
324 (define-key ctl-x-ctl-p-map "s" 'sort-pages-buffer)
325 (define-key ctl-x-ctl-p-map "\C-l" 'set-page-delimiter)
326 (define-key ctl-x-ctl-p-map "\C-d" 'pages-directory)
327 (define-key ctl-x-ctl-p-map "d" 'pages-directory-for-addresses)
330 ;;;; Page movement function definitions
332 (defun next-page (&optional count)
333 "Move to the next page bounded by the `page-delimiter' variable.
334 With arg (prefix if interactive), move that many pages."
335 (interactive "p")
336 (or count (setq count 1))
337 (widen)
338 ;; Cannot use forward-page because of problems at page boundaries.
339 (while (and (> count 0) (not (eobp)))
340 (if (re-search-forward page-delimiter nil t)
342 (goto-char (point-max)))
343 (setq count (1- count)))
344 (while (and (< count 0) (not (bobp)))
345 (if (re-search-backward page-delimiter nil t)
346 (goto-char (match-beginning 0))
347 (goto-char (point-min)))
348 (setq count (1+ count)))
349 (narrow-to-page)
350 (goto-char (point-min))
351 (recenter 0))
353 (defun previous-page (&optional count)
354 "Move to the previous page bounded by the `page-delimiter' variable.
355 With arg (prefix if interactive), move that many pages."
356 (interactive "p")
357 (or count (setq count 1))
358 (next-page (- count)))
361 ;;;; Adding and searching pages
363 (defun add-new-page (header-line)
364 "Insert new page at point; prompt for header line.
365 Page begins with a `^L' as the page-delimiter.
366 Point is left in the body of page."
367 (interactive "sHeader line: ")
368 (widen)
369 (insert (format "\n\f\n%s\n\n" header-line))
370 ;; don't renarrow; stay unnarrowed to see context
371 (forward-line -1))
373 (defvar pages-last-search nil
374 "Value of last regexp searched for. Initially, nil.")
376 (defun search-pages (regexp)
377 "Search for REGEXP, starting from point, and narrow to page it is in."
378 (interactive (list
379 (read-string
380 (format "Search for `%s' (end with RET): "
381 (or pages-last-search "regexp")))))
382 (if (equal regexp "")
383 (setq regexp pages-last-search)
384 (setq pages-last-search regexp))
385 (widen)
386 (re-search-forward regexp)
387 (narrow-to-page))
390 ;;;; Sorting pages
392 (autoload 'sort-subr "sort" "Primary function for sorting." t nil)
394 (defun sort-pages-in-region (reverse beg end)
395 "Sort pages in region alphabetically. Prefix arg means reverse order.
397 Called from a program, there are three arguments:
398 REVERSE (non-nil means reverse order), BEG and END (region to sort)."
400 ;;; This sort function handles ends of pages differently than
401 ;;; `sort-pages' and works better with lists of addresses and similar
402 ;;; files.
404 (interactive "P\nr")
405 (save-restriction
406 (narrow-to-region beg end)
407 (goto-char (point-min))
408 ;;; `sort-subr' takes three arguments
409 (sort-subr reverse
411 ;; NEXTRECFUN is called with point at the end of the
412 ;; previous record. It moves point to the start of the
413 ;; next record.
414 (function (lambda ()
415 (re-search-forward page-delimiter nil t)
416 (skip-chars-forward " \t\n")
419 ;; ENDRECFUN is is called with point within the record.
420 ;; It should move point to the end of the record.
421 (function (lambda ()
422 (if (re-search-forward
423 page-delimiter
426 (goto-char (match-beginning 0))
427 (goto-char (point-max))))))))
429 (defun sort-pages-buffer (&optional reverse)
430 "Sort pages alphabetically in buffer. Prefix arg means reverse order.
431 \(Non-nil arg if not interactive.\)"
433 (interactive "P")
434 (or reverse (setq reverse nil))
435 (widen)
436 (let ((beginning (point-min))
437 (end (point-max)))
438 (sort-pages-in-region reverse beginning end)))
441 ;;;; Pages directory ancillary definitions
443 (defvar pages-directory-buffer-narrowing-p nil
444 "*If non-nil, `pages-directory-goto' narrows pages buffer to entry.")
446 (defvar pages-directory-previous-regexp nil
447 "Value of previous regexp used by `pages-directory'.
448 \(This regular expression may be used to select only those pages that
449 contain matches to the regexp.\)")
451 (defvar pages-buffer nil
452 "The buffer for which the pages-directory function creates the directory.")
454 (defvar pages-directory-prefix "*Directory for:"
455 "Prefix of name of temporary buffer for pages-directory.")
457 (defvar pages-pos-list nil
458 "List containing the positions of the pages in the pages-buffer.")
460 (defvar pages-directory-map nil
461 "Keymap for the pages-directory-buffer.")
463 (if pages-directory-map
465 (setq pages-directory-map (make-sparse-keymap))
466 (define-key pages-directory-map "\C-c\C-c"
467 'pages-directory-goto))
469 (defun set-page-delimiter (regexp reset-p)
470 "Set buffer local value of page-delimiter to REGEXP.
471 Called interactively with a prefix argument, reset `page-delimiter' to
472 its original value.
474 In a program, non-nil second arg causes first arg to be ignored and
475 resets the page-delimiter to the original value."
477 (interactive
478 (if current-prefix-arg
479 (list original-page-delimiter nil)
480 (list (read-string "Set page-delimiter to regexp: " page-delimiter)
481 nil)))
482 (make-local-variable 'original-page-delimiter)
483 (make-local-variable 'page-delimiter)
484 (setq original-page-delimiter
485 (or original-page-delimiter page-delimiter))
486 (if (not reset-p)
487 (setq page-delimiter regexp)
488 (setq page-delimiter original-page-delimiter))
489 (if (interactive-p)
490 (message "The value of `page-delimiter' is now: %s" page-delimiter)))
493 ;;;; Pages directory main definitions
495 (defun pages-directory
496 (pages-list-all-headers-p count-lines-p &optional regexp)
497 "Display a directory of the page headers in a temporary buffer.
498 A header is the first non-blank line after the page-delimiter.
499 \\[pages-directory-mode]
500 You may move point to one of the lines in the temporary buffer,
501 then use \\<pages-directory-goto> to go to the same line in the pages buffer.
503 In interactive use:
505 1. With no prefix arg, display all headers.
507 2. With prefix arg, display the headers of only those pages that
508 contain matches to a regular expression for which you are
509 prompted.
511 3. With numeric prefix arg, for every page, print the number of
512 lines within each page.
514 4. With negative numeric prefix arg, for only those pages that
515 match a regular expression, print the number of lines within
516 each page.
518 When called from a program, non-nil first arg means list all headers;
519 non-nil second arg means print numbers of lines in each page; if first
520 arg is nil, optional third arg is regular expression.
522 If the buffer is narrowed, the `pages-directory' command creates a
523 directory for only the accessible portion of the buffer."
525 (interactive
526 (cond ((not current-prefix-arg)
527 (list t nil nil))
528 ((listp current-prefix-arg)
529 (list nil
531 (read-string
532 (format "Select according to `%s' (end with RET): "
533 (or pages-directory-previous-regexp "regexp")))))
534 ((> (prefix-numeric-value current-prefix-arg) 0)
535 (list t t nil))
536 ((< (prefix-numeric-value current-prefix-arg) 0)
537 (list nil
539 (read-string
540 (format "Select according to `%s' (end with RET): "
541 (or pages-directory-previous-regexp "regexp")))))))
543 (if (equal regexp "")
544 (setq regexp pages-directory-previous-regexp)
545 (setq pages-directory-previous-regexp regexp))
547 (if (interactive-p)
548 (message "Creating directory for: %s "
549 (buffer-name)))
551 (let ((buffer (current-buffer))
552 (pages-directory-buffer
553 (concat pages-directory-prefix " " (buffer-name) " "))
554 (linenum 1)
555 (pages-buffer-original-position (point))
556 (pages-buffer-original-page 0))
558 ;; `with-output-to-temp-buffer' binds the value of the variable
559 ;; `standard-output' to the buffer named as its first argument,
560 ;; but does not switch to that buffer.
561 (with-output-to-temp-buffer pages-directory-buffer
562 (save-excursion
563 (set-buffer standard-output)
564 (pages-directory-mode)
565 (insert
566 "==== Pages Directory: use `C-c C-c' to go to page under cursor. ====" ?\n)
567 (setq pages-buffer buffer)
568 (setq pages-pos-list nil))
570 (if pages-list-all-headers-p
572 ;; 1. If no prefix argument, list all headers
573 (save-excursion
574 (goto-char (point-min))
576 ;; (a) Point is at beginning of buffer; but the first
577 ;; page may not begin with a page-delimiter
578 (save-restriction
579 ;; If page delimiter is at beginning of buffer, skip it
580 (if (and (save-excursion
581 (re-search-forward page-delimiter nil t))
582 (= 1 (match-beginning 0)))
583 (goto-char (match-end 0)))
584 (narrow-to-page)
585 (pages-copy-header-and-position count-lines-p))
587 ;; (b) Search within pages buffer for next page-delimiter
588 (while (re-search-forward page-delimiter nil t)
589 (pages-copy-header-and-position count-lines-p)))
591 ;; 2. Else list headers whose pages match regexp.
592 (save-excursion
593 ;; REMOVED save-restriction AND widen FROM HERE
594 (goto-char (point-min))
596 ;; (a) Handle first page
597 (save-restriction
598 (narrow-to-page)
599 ;; search for selection regexp
600 (if (save-excursion (re-search-forward regexp nil t))
601 (pages-copy-header-and-position count-lines-p)))
603 ;; (b) Search for next page-delimiter
604 (while (re-search-forward page-delimiter nil t)
605 (save-restriction
606 (narrow-to-page)
607 ;; search for selection regexp
608 (if (save-excursion (re-search-forward regexp nil t))
609 (pages-copy-header-and-position count-lines-p)
610 )))))
612 (set-buffer standard-output)
613 ;; Put positions in increasing order to go with buffer.
614 (setq pages-pos-list (nreverse pages-pos-list))
615 (if (interactive-p)
616 (message "%d matching lines in: %s"
617 (length pages-pos-list) (buffer-name buffer))))
618 (pop-to-buffer pages-directory-buffer)
619 (sit-for 0) ; otherwise forward-line fails if N > window height.
620 (forward-line (if (= 0 pages-buffer-original-page)
622 pages-buffer-original-page))))
624 (defun pages-copy-header-and-position (count-lines-p)
625 "Copy page header and its position to the Pages Directory.
626 Only arg non-nil, count lines in page and insert before header.
627 Used by `pages-directory' function."
629 (let (position line-count)
631 (if count-lines-p
632 (save-excursion
633 (save-restriction
634 (narrow-to-page)
635 (setq line-count (count-lines (point-min) (point-max))))))
637 ;; Keep track of page for later cursor positioning
638 (if (<= (point) pages-buffer-original-position)
639 (setq pages-buffer-original-page
640 (1+ pages-buffer-original-page)))
642 (save-excursion
643 ;; go to first non-blank char after the page-delimiter
644 (skip-chars-forward " \t\n")
645 ;; set the marker here; this the place to which the
646 ;; `pages-directory-goto' command will go
647 (setq position (make-marker))
648 (set-marker position (point))
649 (let ((start (point))
650 (end (save-excursion (end-of-line) (point))))
651 ;; change to directory buffer
652 (set-buffer standard-output)
653 ;; record page position
654 (setq pages-pos-list (cons position pages-pos-list))
655 ;; insert page header
656 (insert-buffer-substring buffer start end))
658 (if count-lines-p
659 (save-excursion
660 (beginning-of-line)
661 (insert (format "%3d: " line-count))))
663 (terpri))
664 (forward-line 1)))
666 (defun pages-directory-mode ()
667 "Mode for handling the pages-directory buffer.
669 Move point to one of the lines in this buffer, then use \\[pages-directory-goto] to go
670 to the same line in the pages buffer."
672 (kill-all-local-variables)
673 (use-local-map pages-directory-map)
674 (setq major-mode 'pages-directory-mode)
675 (setq mode-name "Pages-Directory")
676 (make-local-variable 'pages-buffer)
677 (make-local-variable 'pages-pos-list)
678 (make-local-variable 'pages-directory-buffer-narrowing-p))
680 (defun pages-directory-goto ()
681 "Go to the corresponding line in the pages buffer."
683 ;;; This function is mostly a copy of `occur-mode-goto-occurrence'
685 (interactive)
686 (if (or (not pages-buffer)
687 (not (buffer-name pages-buffer)))
688 (progn
689 (setq pages-buffer nil
690 pages-pos-list nil)
691 (error "Buffer in which pages were found is deleted.")))
692 (beginning-of-line)
693 (let* ((pages-number (1- (count-lines (point-min) (point))))
694 (pos (nth pages-number pages-pos-list))
695 (end-of-directory-p (eobp))
696 (narrowing-p pages-directory-buffer-narrowing-p))
697 (pop-to-buffer pages-buffer)
698 (widen)
699 (if end-of-directory-p
700 (goto-char (point-max))
701 (goto-char (marker-position pos)))
702 (if narrowing-p (narrow-to-page))))
705 ;;;; The `pages-directory-for-addresses' function and ancillary code
707 (defvar pages-addresses-file-name "~/addresses"
708 "*Standard name for file of addresses. Entries separated by `page-delimiter'.
709 Used by `pages-directory-for-addresses' function.")
711 (defvar pages-directory-for-addresses-narrowing-p t
712 "*If non-nil, `pages-directory-goto' narrows addresses buffer to entry.")
714 (defun pages-directory-for-addresses (&optional filename)
715 "Find addresses file and display its directory.
716 By default, create and display directory of `pages-addresses-file-name'.
717 Optional argument is FILENAME. In interactive use, with prefix
718 argument, prompt for file name and provide completion.
720 Move point to one of the lines in the displayed directory,
721 then use C-c C-c to go to the same line in the addresses buffer."
723 (interactive
724 (list (if current-prefix-arg
725 (read-file-name "Filename: " pages-addresses-file-name))))
727 (if (interactive-p)
728 (message "Creating directory for: %s "
729 (or filename pages-addresses-file-name)))
730 (if (file-exists-p (or filename pages-addresses-file-name))
731 (progn
732 (set-buffer
733 (find-file-noselect
734 (expand-file-name
735 (or filename pages-addresses-file-name))))
736 (widen)
737 (pages-directory t nil nil)
738 (pages-directory-address-mode)
739 (setq pages-directory-buffer-narrowing-p
740 pages-directory-for-addresses-narrowing-p)
741 (delete-other-windows)
742 (save-excursion
743 (goto-char (point-min))
744 (delete-region (point) (save-excursion (end-of-line) (point)))
745 (insert
746 "=== Address List Directory: use `C-c C-c' to go to page under cursor. ===")
747 (set-buffer-modified-p nil)
749 (error "No addresses file found!")))
751 (defun pages-directory-address-mode ()
752 "Mode for handling the Addresses Directory buffer.
754 Move point to one of the lines in this buffer, then use C-c C-c to go
755 to the same line in the pages buffer."
757 (use-local-map pages-directory-map)
758 (setq major-mode 'pages-directory-address-mode)
759 (setq mode-name "Addresses Directory")
760 (make-local-variable 'pages-buffer)
761 (make-local-variable 'pages-pos-list)
762 (make-local-variable 'pages-directory-buffer-narrowing-p))
764 ;;; page-ext.el ends here