Merge from trunk and resolve conflicts.
[bpt/emacs.git] / lisp / textmodes / page-ext.el
CommitLineData
98d87352 1;;; page-ext.el --- extended page handling commands
0d2d639a 2
ab422c4d
PE
3;; Copyright (C) 1990-1991, 1993-1994, 2001-2013 Free Software
4;; Foundation, Inc.
98d87352 5
76ced338
GM
6;; Author: Robert J. Chassell <bob@gnu.org>
7;; (according to ack.texi)
6228c05b 8;; Keywords: wp data
9750e079 9
3bab998b
ER
10;; This file is part of GNU Emacs.
11
1fecc8fe 12;; GNU Emacs is free software: you can redistribute it and/or modify
3bab998b 13;; it under the terms of the GNU General Public License as published by
1fecc8fe
GM
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
3bab998b
ER
16
17;; GNU Emacs is distributed in the hope that it will be useful,
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
21
22;; You should have received a copy of the GNU General Public License
1fecc8fe 23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
b578f267
EN
24
25;;; Commentary:
26
27;; You may use these commands to handle an address list or other
28;; small data base.
3bab998b 29
0d2d639a 30\f
98d87352 31;;; Summary
0d2d639a 32
b578f267
EN
33;; The current page commands are:
34
35;; forward-page C-x ]
36;; backward-page C-x [
37;; narrow-to-page C-x p
38;; count-lines-page C-x l
39;; mark-page C-x C-p (change this to C-x C-p C-m)
40;; sort-pages not bound
41;; what-page not bound
42
43;; The new page handling commands all use `C-x C-p' as a prefix. This
44;; means that the key binding for `mark-page' must be changed.
45;; Otherwise, no other changes are made to the current commands or
46;; their bindings.
47
48;; New page handling commands:
49
50;; next-page C-x C-p C-n
51;; previous-page C-x C-p C-p
52;; search-pages C-x C-p C-s
53;; add-new-page C-x C-p C-a
db95369b 54;; sort-pages-buffer C-x C-p s
b578f267 55;; set-page-delimiter C-x C-p C-l
db95369b 56;; pages-directory C-x C-p C-d
b578f267
EN
57;; pages-directory-for-addresses C-x C-p d
58;; pages-directory-goto C-c C-c
0d2d639a
JB
59
60\f
98d87352 61;;; Using the page commands
db95369b 62
b578f267
EN
63;; The page commands are helpful in several different contexts. For
64;; example, programmers often divide source files into sections using the
65;; `page-delimiter'; you can use the `pages-directory' command to list
66;; the sections.
67
68;; You may change the buffer local value of the `page-delimiter' with
69;; the `set-page-delimiter' command. This command is bound to `C-x C-p
70;; C-l' The command prompts you for a new value for the page-delimiter.
71;; Called with a prefix-arg, the command resets the value of the
72;; page-delimiter to its original value.
73
74;; You may set several user options:
75;;
76;; The `pages-directory-buffer-narrowing-p' variable causes the
77;; `pages-directory-goto' command to narrow to the destination page.
78;;
79;; The `pages-directory-for-adding-page-narrowing-p' variable, causes the
80;; `add-new-page' command to narrow to the new entry.
81;;
82;; The `pages-directory-for-adding-new-page-before-current-page-p' variable
83;; causes the `add-new-page' command to insert a new page before current
84;; page.
85;;
86;; These variables are true by default.
87;;
88;; Additional, addresses-related user options are described in the next page
89;; of this file.
98d87352 90
0d2d639a 91\f
98d87352 92;;; Handling an address list or small data base
0d2d639a 93
b578f267
EN
94;; You may use the page commands to handle an address list or other
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
97;; the `pages-directory' or `pages-directory-for-addresses' command.
98
99;; Specifically:
db95369b 100;;
b578f267
EN
101;; 1. Begin each entry with a `page-delimiter' (which is, by default,
102;; `^L' at the beginning of the line).
db95369b 103;;
b578f267
EN
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
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.
db95369b 108;;
b578f267
EN
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
111;; page. Conventionally, the heading line is placed on the line
112;; immediately following the line containing page-delimiter.
113;;
114;; 3. Follow the heading line with the body of the entry. The body
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
117;; line of the body.
118
119;; For example, a file might look like this:
db95369b 120;;
b578f267
EN
121;; FSF
122;; Free Software Foundation
086add15
LK
123;; 51 Franklin Street, Fifth Floor
124;; Boston, MA 02110-1301 USA.
29bd7a8f 125;; (617) 542-5942
5762abec 126;; gnu@gnu.org
db95369b 127;;
b578f267
EN
128;; \f
129;; House Subcommittee on Intellectual Property,
130;; U.S. House of Representatives,
131;; Washington, DC 20515
db95369b 132;;
b578f267
EN
133;; Congressional committee concerned with permitting or preventing
134;; monopolistic restrictions on the use of software technology.
db95369b 135;;
b578f267
EN
136;; \f
137;; George Lakoff
138;; ``Women, Fire, and Dangerous Things:
139;; What Categories Reveal about the Mind''
140;; 1987, Univ. of Chicago Press
db95369b 141;;
b578f267 142;; About philosophy, Whorfian effects, and linguistics.
db95369b
JB
143;;
144;; \f
b578f267
EN
145;; OBI (On line text collection.)
146;; Open Book Initiative
147;; c/o Software Tool & Die
148;; 1330 Beacon St, Brookline, MA 02146 USA
db95369b 149;; (617) 739-0202
b578f267
EN
150;; obi@world.std.com
151
152;; In this example, the heading lines are:
153;;
154;; FSF
155;; House Subcommittee on Intellectual Property
156;; George Lakoff
157;; OBI (On line text collection.)
158
159;; The `C-x C-p s' (sort-pages-buffer) command sorts the entries in the
160;; buffer alphabetically.
161
162;; You may use any of the page commands, including the `next-page',
163;; `previous-page', `add-new-page', `mark-page', and `search-pages'
164;; commands.
165
166;; You may use either the `C-x C-p d' (pages-directory-for-addresses)
167;; or the `C-x C-p C-d' (pages-directory) command to construct and
168;; display a directory of all the heading lines.
169
170;; In the directory, you may position the cursor over a heading line
171;; and type `C-c C-c' (pages-directory-goto) to go to the entry to
172;; which it refers in the pages buffer.
173
174;; You can type `C-c C-p C-a' (add-new-page) to add a new entry in the
175;; pages buffer or address file. This is the same command you use to
176;; add a new entry when you are in the pages buffer or address file.
177
178;; If you wish, you may create several different directories,
179;; one for each different buffer.
0d2d639a
JB
180
181;; `pages-directory-for-addresses' in detail
182
b578f267
EN
183;; The `pages-directory-for-addresses' assumes a default addresses
184;; file. You do not need to specify the addresses file but merely type
185;; `C-x C-p d' from any buffer. The command finds the file, constructs
186;; a directory for it, and switches you to the directory. If you call
187;; the command with a prefix arg, `C-u C-x C-p d', it prompts you for a
188;; file name.
0d2d639a 189
b578f267 190;; You may customize the addresses commands:
98d87352 191
b578f267
EN
192;; The `pages-addresses-file-name' variable determines the name of
193;; the addresses file; by default it is "~/addresses".
98d87352 194
b578f267
EN
195;; The `pages-directory-for-addresses-goto-narrowing-p' variable
196;; determines whether `pages-directory-goto' narrows the addresses
197;; buffer to the entry, which it does by default.
98d87352 198
b578f267
EN
199;; The `pages-directory-for-addresses-buffer-keep-windows-p' variable
200;; determines whether `pages-directory-for-addresses' deletes other
201;; windows to show as many lines as possible on the screen or works
202;; in the usual Emacs manner and keeps other windows. Default is to
203;; keep other windows.
98d87352 204
b578f267
EN
205;; The `pages-directory-for-adding-addresses-narrowing-p' variable
206;; determines whether `pages-directory-for-addresses' narrows the
207;; addresses buffer to a new entry when you are adding that entry.
208;; Default is to narrow to new entry, which means you see a blank
209;; screen before you write the new entry.
98d87352 210
0d2d639a
JB
211;; `pages-directory' in detail
212
b578f267
EN
213;; Call the `pages-directory' command from the buffer for which you
214;; want a directory created; it creates a directory for the buffer and
215;; pops you to the directory.
216
217;; The `pages-directory' command has several options:
218
219;; Called with a prefix arg, `C-u C-x C-p C-d', the `pages-directory'
220;; prompts you for a regular expression and only lists only those
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
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
225;; pages-directory-buffer.
db95369b 226;;
b578f267
EN
227;; Called with a numeric argument, the `pages-directory' command
228;; lists the number of lines in each page. This is helpful when you
db95369b 229;; are printing hardcopy.
b578f267
EN
230
231;; Called with a negative numeric argument, the `pages-directory'
232;; command lists the lengths of pages whose contents match a regexp.
0d2d639a 233
98d87352
RS
234;;; Code:
235
236\f
237;;; Customarily customizable variable definitions
238
00ed33e7
RS
239(defgroup pages nil
240 "Extended page-handling commands."
241 :group 'extensions)
98d87352 242
98d87352 243
00ed33e7 244(defcustom pages-directory-buffer-narrowing-p t
1fc7dabf 245 "If non-nil, `pages-directory-goto' narrows pages buffer to entry."
00ed33e7
RS
246 :type 'boolean
247 :group 'pages)
248
249(defcustom pages-directory-for-adding-page-narrowing-p t
1fc7dabf 250 "If non-nil, `add-new-page' narrows page buffer to new entry."
00ed33e7
RS
251 :type 'boolean
252 :group 'pages)
253
254(defcustom pages-directory-for-adding-new-page-before-current-page-p t
1fc7dabf 255 "If non-nil, `add-new-page' inserts new page before current page."
00ed33e7
RS
256 :type 'boolean
257 :group 'pages)
98d87352
RS
258
259\f
260;;; Addresses related variables
261
00ed33e7 262(defcustom pages-addresses-file-name "~/addresses"
1fc7dabf 263 "Standard name for file of addresses. Entries separated by page-delimiter.
00ed33e7
RS
264Used by `pages-directory-for-addresses' function."
265 :type 'file
266 :group 'pages)
267
268(defcustom pages-directory-for-addresses-goto-narrowing-p t
1fc7dabf 269 "If non-nil, `pages-directory-goto' narrows addresses buffer to entry."
00ed33e7
RS
270 :type 'boolean
271 :group 'pages)
272
273(defcustom pages-directory-for-addresses-buffer-keep-windows-p t
1fc7dabf 274 "If nil, `pages-directory-for-addresses' deletes other windows."
00ed33e7
RS
275 :type 'boolean
276 :group 'pages)
277
278(defcustom pages-directory-for-adding-addresses-narrowing-p t
1fc7dabf 279 "If non-nil, `add-new-page' narrows addresses buffer to new entry."
00ed33e7
RS
280 :type 'boolean
281 :group 'pages)
98d87352 282
0d2d639a 283\f
98d87352 284;;; Key bindings for page handling functions
0d2d639a
JB
285
286(global-unset-key "\C-x\C-p")
287
288(defvar ctl-x-ctl-p-map (make-sparse-keymap)
289 "Keymap for subcommands of C-x C-p, which are for page handling.")
290
291(define-key ctl-x-map "\C-p" 'ctl-x-ctl-p-prefix)
292(fset 'ctl-x-ctl-p-prefix ctl-x-ctl-p-map)
293
294(define-key ctl-x-ctl-p-map "\C-n" 'next-page)
295(define-key ctl-x-ctl-p-map "\C-p" 'previous-page)
296(define-key ctl-x-ctl-p-map "\C-a" 'add-new-page)
297(define-key ctl-x-ctl-p-map "\C-m" 'mark-page)
298(define-key ctl-x-ctl-p-map "\C-s" 'search-pages)
299(define-key ctl-x-ctl-p-map "s" 'sort-pages-buffer)
300(define-key ctl-x-ctl-p-map "\C-l" 'set-page-delimiter)
301(define-key ctl-x-ctl-p-map "\C-d" 'pages-directory)
302(define-key ctl-x-ctl-p-map "d" 'pages-directory-for-addresses)
303
304\f
98d87352 305;;; Page movement function definitions
0d2d639a
JB
306
307(defun next-page (&optional count)
308 "Move to the next page bounded by the `page-delimiter' variable.
309With arg (prefix if interactive), move that many pages."
310 (interactive "p")
311 (or count (setq count 1))
312 (widen)
313 ;; Cannot use forward-page because of problems at page boundaries.
314 (while (and (> count 0) (not (eobp)))
315 (if (re-search-forward page-delimiter nil t)
316 nil
317 (goto-char (point-max)))
318 (setq count (1- count)))
817d6535
JB
319 ;; If COUNT is negative, we want to go back -COUNT + 1 page boundaries.
320 ;; The first page boundary we reach is the top of the current page,
321 ;; which doesn't count.
322 (while (and (< count 1) (not (bobp)))
0d2d639a
JB
323 (if (re-search-backward page-delimiter nil t)
324 (goto-char (match-beginning 0))
325 (goto-char (point-min)))
326 (setq count (1+ count)))
327 (narrow-to-page)
328 (goto-char (point-min))
329 (recenter 0))
330
331(defun previous-page (&optional count)
332 "Move to the previous page bounded by the `page-delimiter' variable.
333With arg (prefix if interactive), move that many pages."
334 (interactive "p")
335 (or count (setq count 1))
336 (next-page (- count)))
337
338\f
98d87352 339;;; Adding and searching pages
0d2d639a
JB
340
341(defun add-new-page (header-line)
98d87352
RS
342 "Insert new page. Prompt for header line.
343
344If point is in the pages directory buffer, insert the new page in the
345buffer associated with the directory.
346
347Insert the new page just before current page if
348 pages-directory-for-adding-new-page-before-current-page-p variable
349is non-nil. Else insert at exact location of point.
350
351Narrow to new page if
352 pages-directory-for-adding-page-narrowing-p variable
353is non-nil.
354
db95369b 355Page begins with a `^L' as the default page-delimiter.
98d87352 356Use \\[set-page-delimiter] to change the page-delimiter.
0d2d639a
JB
357Point is left in the body of page."
358 (interactive "sHeader line: ")
359 (widen)
98d87352 360 ;; If in pages directory buffer
db95369b 361 (if (eq major-mode 'pages-directory-mode)
98d87352
RS
362 (progn
363 ;; Add new page before or after current page?
364 (if pages-directory-for-adding-new-page-before-current-page-p
365 (pages-directory-goto)
366 (pages-directory-goto)
367 (forward-page)
368 (or (eobp) (forward-line -1)))))
369 (widen)
370 ;; Move point before current delimiter if desired.
371 (and pages-directory-for-adding-new-page-before-current-page-p
372 (if (re-search-backward page-delimiter nil t)
373 (goto-char (match-beginning 0))
374 ;; If going to beginning of file, insert a page-delimiter
375 ;; before current first page.
376 (goto-char (point-min))
377 (insert
378 (format "%s\n"
379 ;; Remove leading `^' from page-delimiter string
380 (if (eq '^ (car (read-from-string page-delimiter)))
db95369b 381 (substring page-delimiter 1))))
98d87352
RS
382 (goto-char (point-min))))
383 ;; Insert page delimiter at beginning of line.
384 (if (not (looking-at "^.")) (forward-line 1))
db95369b 385 (insert (format "%s\n%s\n\n\n"
98d87352
RS
386 (if (eq '^ (car (read-from-string page-delimiter)))
387 (substring page-delimiter 1))
388 header-line))
389 (forward-line -1)
390 (and pages-directory-for-adding-page-narrowing-p (narrow-to-page)))
0d2d639a
JB
391
392(defvar pages-last-search nil
393 "Value of last regexp searched for. Initially, nil.")
394
395(defun search-pages (regexp)
396 "Search for REGEXP, starting from point, and narrow to page it is in."
397 (interactive (list
398 (read-string
399 (format "Search for `%s' (end with RET): "
400 (or pages-last-search "regexp")))))
401 (if (equal regexp "")
402 (setq regexp pages-last-search)
403 (setq pages-last-search regexp))
404 (widen)
405 (re-search-forward regexp)
406 (narrow-to-page))
407
408\f
98d87352 409;;; Sorting pages
0d2d639a
JB
410
411(autoload 'sort-subr "sort" "Primary function for sorting." t nil)
412
413(defun sort-pages-in-region (reverse beg end)
db95369b 414 "Sort pages in region alphabetically. Prefix arg means reverse order.
0d2d639a
JB
415
416Called from a program, there are three arguments:
417REVERSE (non-nil means reverse order), BEG and END (region to sort)."
418
419;;; This sort function handles ends of pages differently than
420;;; `sort-pages' and works better with lists of addresses and similar
421;;; files.
422
423 (interactive "P\nr")
424 (save-restriction
425 (narrow-to-region beg end)
426 (goto-char (point-min))
427 ;;; `sort-subr' takes three arguments
428 (sort-subr reverse
429
430 ;; NEXTRECFUN is called with point at the end of the
431 ;; previous record. It moves point to the start of the
432 ;; next record.
433 (function (lambda ()
434 (re-search-forward page-delimiter nil t)
435 (skip-chars-forward " \t\n")
436 ))
437
58179cce 438 ;; ENDRECFUN is called with point within the record.
0d2d639a
JB
439 ;; It should move point to the end of the record.
440 (function (lambda ()
441 (if (re-search-forward
442 page-delimiter
443 nil
444 t)
445 (goto-char (match-beginning 0))
446 (goto-char (point-max))))))))
447
448(defun sort-pages-buffer (&optional reverse)
db95369b 449 "Sort pages alphabetically in buffer. Prefix arg means reverse order.
0d2d639a
JB
450\(Non-nil arg if not interactive.\)"
451
452 (interactive "P")
453 (or reverse (setq reverse nil))
454 (widen)
455 (let ((beginning (point-min))
456 (end (point-max)))
457 (sort-pages-in-region reverse beginning end)))
458
459\f
98d87352 460;;; Pages directory ancillary definitions
0d2d639a
JB
461
462(defvar pages-directory-previous-regexp nil
463 "Value of previous regexp used by `pages-directory'.
464\(This regular expression may be used to select only those pages that
465contain matches to the regexp.\)")
466
467(defvar pages-buffer nil
468 "The buffer for which the pages-directory function creates the directory.")
469
470(defvar pages-directory-prefix "*Directory for:"
471 "Prefix of name of temporary buffer for pages-directory.")
472
473(defvar pages-pos-list nil
474 "List containing the positions of the pages in the pages-buffer.")
475
2600f4e4
RS
476(defvar pages-target-buffer)
477
764e5002
SM
478(defvar pages-directory-mode-map
479 (let ((map (make-sparse-keymap)))
480 (define-key map "\C-c\C-c" 'pages-directory-goto)
481 (define-key map "\C-c\C-p\C-a" 'add-new-page)
482 (define-key map [mouse-2] 'pages-directory-goto-with-mouse)
483 map)
0d2d639a 484 "Keymap for the pages-directory-buffer.")
764e5002 485(defvaralias 'pages-directory-map 'pages-directory-mode-map)
98d87352 486
98526426 487(defvar original-page-delimiter "^\f"
98d87352 488 "Default page delimiter.")
0d2d639a
JB
489
490(defun set-page-delimiter (regexp reset-p)
491 "Set buffer local value of page-delimiter to REGEXP.
492Called interactively with a prefix argument, reset `page-delimiter' to
493its original value.
494
495In a program, non-nil second arg causes first arg to be ignored and
496resets the page-delimiter to the original value."
db95369b 497
0d2d639a
JB
498 (interactive
499 (if current-prefix-arg
98526426 500 (list original-page-delimiter "^\f")
0d2d639a
JB
501 (list (read-string "Set page-delimiter to regexp: " page-delimiter)
502 nil)))
503 (make-local-variable 'original-page-delimiter)
504 (make-local-variable 'page-delimiter)
505 (setq original-page-delimiter
506 (or original-page-delimiter page-delimiter))
507 (if (not reset-p)
508 (setq page-delimiter regexp)
509 (setq page-delimiter original-page-delimiter))
32226619 510 (if (called-interactively-p 'interactive)
0d2d639a
JB
511 (message "The value of `page-delimiter' is now: %s" page-delimiter)))
512
513\f
98d87352 514;;; Pages directory main definitions
0d2d639a
JB
515
516(defun pages-directory
517 (pages-list-all-headers-p count-lines-p &optional regexp)
518 "Display a directory of the page headers in a temporary buffer.
519A header is the first non-blank line after the page-delimiter.
520\\[pages-directory-mode]
521You may move point to one of the lines in the temporary buffer,
522then use \\<pages-directory-goto> to go to the same line in the pages buffer.
523
524In interactive use:
525
526 1. With no prefix arg, display all headers.
527
528 2. With prefix arg, display the headers of only those pages that
529 contain matches to a regular expression for which you are
530 prompted.
531
532 3. With numeric prefix arg, for every page, print the number of
533 lines within each page.
534
535 4. With negative numeric prefix arg, for only those pages that
536 match a regular expression, print the number of lines within
537 each page.
538
539When called from a program, non-nil first arg means list all headers;
540non-nil second arg means print numbers of lines in each page; if first
541arg is nil, optional third arg is regular expression.
542
543If the buffer is narrowed, the `pages-directory' command creates a
544directory for only the accessible portion of the buffer."
db95369b 545
0d2d639a
JB
546 (interactive
547 (cond ((not current-prefix-arg)
548 (list t nil nil))
db95369b 549 ((listp current-prefix-arg)
0d2d639a
JB
550 (list nil
551 nil
552 (read-string
553 (format "Select according to `%s' (end with RET): "
554 (or pages-directory-previous-regexp "regexp")))))
555 ((> (prefix-numeric-value current-prefix-arg) 0)
556 (list t t nil))
db95369b 557 ((< (prefix-numeric-value current-prefix-arg) 0)
0d2d639a
JB
558 (list nil
559 t
560 (read-string
561 (format "Select according to `%s' (end with RET): "
562 (or pages-directory-previous-regexp "regexp")))))))
db95369b 563
0d2d639a
JB
564 (if (equal regexp "")
565 (setq regexp pages-directory-previous-regexp)
566 (setq pages-directory-previous-regexp regexp))
db95369b 567
32226619 568 (if (called-interactively-p 'interactive)
0d2d639a
JB
569 (message "Creating directory for: %s "
570 (buffer-name)))
db95369b 571
2600f4e4 572 (let ((pages-target-buffer (current-buffer))
0d2d639a 573 (pages-directory-buffer
e6945ec7 574 (concat pages-directory-prefix " " (buffer-name)))
0d2d639a
JB
575 (linenum 1)
576 (pages-buffer-original-position (point))
577 (pages-buffer-original-page 0))
db95369b 578
0d2d639a
JB
579 ;; `with-output-to-temp-buffer' binds the value of the variable
580 ;; `standard-output' to the buffer named as its first argument,
581 ;; but does not switch to that buffer.
582 (with-output-to-temp-buffer pages-directory-buffer
9a529312 583 (with-current-buffer standard-output
0d2d639a
JB
584 (pages-directory-mode)
585 (insert
586 "==== Pages Directory: use `C-c C-c' to go to page under cursor. ====" ?\n)
2600f4e4 587 (setq pages-buffer pages-target-buffer)
0d2d639a 588 (setq pages-pos-list nil))
db95369b 589
0d2d639a 590 (if pages-list-all-headers-p
db95369b 591
0d2d639a
JB
592 ;; 1. If no prefix argument, list all headers
593 (save-excursion
594 (goto-char (point-min))
db95369b 595
0d2d639a
JB
596 ;; (a) Point is at beginning of buffer; but the first
597 ;; page may not begin with a page-delimiter
598 (save-restriction
599 ;; If page delimiter is at beginning of buffer, skip it
600 (if (and (save-excursion
601 (re-search-forward page-delimiter nil t))
602 (= 1 (match-beginning 0)))
603 (goto-char (match-end 0)))
604 (narrow-to-page)
605 (pages-copy-header-and-position count-lines-p))
db95369b 606
0d2d639a
JB
607 ;; (b) Search within pages buffer for next page-delimiter
608 (while (re-search-forward page-delimiter nil t)
609 (pages-copy-header-and-position count-lines-p)))
db95369b 610
0d2d639a
JB
611 ;; 2. Else list headers whose pages match regexp.
612 (save-excursion
613 ;; REMOVED save-restriction AND widen FROM HERE
614 (goto-char (point-min))
db95369b 615
0d2d639a
JB
616 ;; (a) Handle first page
617 (save-restriction
618 (narrow-to-page)
619 ;; search for selection regexp
620 (if (save-excursion (re-search-forward regexp nil t))
621 (pages-copy-header-and-position count-lines-p)))
db95369b 622
0d2d639a
JB
623 ;; (b) Search for next page-delimiter
624 (while (re-search-forward page-delimiter nil t)
625 (save-restriction
626 (narrow-to-page)
627 ;; search for selection regexp
628 (if (save-excursion (re-search-forward regexp nil t))
629 (pages-copy-header-and-position count-lines-p)
630 )))))
db95369b 631
0d2d639a
JB
632 (set-buffer standard-output)
633 ;; Put positions in increasing order to go with buffer.
634 (setq pages-pos-list (nreverse pages-pos-list))
32226619 635 (if (called-interactively-p 'interactive)
0d2d639a 636 (message "%d matching lines in: %s"
2600f4e4 637 (length pages-pos-list) (buffer-name pages-target-buffer))))
0d2d639a
JB
638 (pop-to-buffer pages-directory-buffer)
639 (sit-for 0) ; otherwise forward-line fails if N > window height.
640 (forward-line (if (= 0 pages-buffer-original-page)
641 1
642 pages-buffer-original-page))))
0d56ae89
GM
643
644(defvar pages-buffer-original-position)
645(defvar pages-buffer-original-page)
646(defvar pages-buffer-original-page)
0d2d639a
JB
647
648(defun pages-copy-header-and-position (count-lines-p)
649 "Copy page header and its position to the Pages Directory.
650Only arg non-nil, count lines in page and insert before header.
651Used by `pages-directory' function."
db95369b 652
0d2d639a
JB
653 (let (position line-count)
654
655 (if count-lines-p
656 (save-excursion
657 (save-restriction
658 (narrow-to-page)
659 (setq line-count (count-lines (point-min) (point-max))))))
660
661 ;; Keep track of page for later cursor positioning
662 (if (<= (point) pages-buffer-original-position)
663 (setq pages-buffer-original-page
664 (1+ pages-buffer-original-page)))
db95369b 665
0d2d639a
JB
666 (save-excursion
667 ;; go to first non-blank char after the page-delimiter
db95369b 668 (skip-chars-forward " \t\n")
0d2d639a 669 ;; set the marker here; this the place to which the
db95369b 670 ;; `pages-directory-goto' command will go
0d2d639a
JB
671 (setq position (make-marker))
672 (set-marker position (point))
673 (let ((start (point))
5ed619e0 674 (end (line-end-position))
a2b327b6 675 inserted-at)
0d2d639a
JB
676 ;; change to directory buffer
677 (set-buffer standard-output)
db95369b 678 ;; record page position
0d2d639a
JB
679 (setq pages-pos-list (cons position pages-pos-list))
680 ;; insert page header
a2b327b6 681 (setq inserted-at (point))
2600f4e4 682 (insert-buffer-substring pages-target-buffer start end)
db95369b 683 (add-text-properties inserted-at (point)
dc44c48c
EZ
684 '(mouse-face highlight
685 help-echo "mouse-2: go to this page"))
cac7e5a8 686 (put-text-property inserted-at (point) 'rear-nonsticky 'highlight))
db95369b 687
0d2d639a
JB
688 (if count-lines-p
689 (save-excursion
690 (beginning-of-line)
691 (insert (format "%3d: " line-count))))
db95369b 692
0d2d639a 693 (terpri))
e6945ec7 694 (end-of-line 1)))
0d2d639a 695
f8da45ad 696(defun pages-directory-mode ()
0d2d639a
JB
697 "Mode for handling the pages-directory buffer.
698
699Move point to one of the lines in this buffer, then use \\[pages-directory-goto] to go
700to the same line in the pages buffer."
f8da45ad
RS
701
702 (kill-all-local-variables)
764e5002 703 (use-local-map pages-directory-mode-map)
f8da45ad
RS
704 (setq major-mode 'pages-directory-mode)
705 (setq mode-name "Pages-Directory")
0d2d639a
JB
706 (make-local-variable 'pages-buffer)
707 (make-local-variable 'pages-pos-list)
764e5002
SM
708 (make-local-variable 'pages-directory-buffer-narrowing-p)
709 (run-mode-hooks 'pages-directory-mode-hook))
0d2d639a
JB
710
711(defun pages-directory-goto ()
712 "Go to the corresponding line in the pages buffer."
713
714;;; This function is mostly a copy of `occur-mode-goto-occurrence'
715
716 (interactive)
717 (if (or (not pages-buffer)
718 (not (buffer-name pages-buffer)))
719 (progn
720 (setq pages-buffer nil
721 pages-pos-list nil)
e8af40ee 722 (error "Buffer in which pages were found is deleted")))
0d2d639a
JB
723 (beginning-of-line)
724 (let* ((pages-number (1- (count-lines (point-min) (point))))
725 (pos (nth pages-number pages-pos-list))
726 (end-of-directory-p (eobp))
727 (narrowing-p pages-directory-buffer-narrowing-p))
db95369b 728 (pop-to-buffer pages-buffer)
0d2d639a
JB
729 (widen)
730 (if end-of-directory-p
731 (goto-char (point-max))
732 (goto-char (marker-position pos)))
733 (if narrowing-p (narrow-to-page))))
734
a2b327b6
RS
735(defun pages-directory-goto-with-mouse (event)
736 "Go to the corresponding line under the mouse pointer in the pages buffer."
737 (interactive "e")
9a529312 738 (with-current-buffer (window-buffer (posn-window (event-end event)))
a2b327b6
RS
739 (save-excursion
740 (goto-char (posn-point (event-end event)))
741 (pages-directory-goto))))
0d2d639a 742\f
98d87352 743;;; The `pages-directory-for-addresses' function and ancillary code
0d2d639a
JB
744
745(defun pages-directory-for-addresses (&optional filename)
746 "Find addresses file and display its directory.
747By default, create and display directory of `pages-addresses-file-name'.
748Optional argument is FILENAME. In interactive use, with prefix
749argument, prompt for file name and provide completion.
750
751Move point to one of the lines in the displayed directory,
98d87352
RS
752then use \\[pages-directory-goto] to go to the same line
753in the addresses buffer.
754
755If pages-directory-for-addresses-goto-narrowing-p is non-nil,
756`pages-directory-goto' narrows addresses buffer to entry.
757
758If pages-directory-for-addresses-buffer-keep-windows-p is nil,
db95369b 759this command deletes other windows when it displays the addresses
98d87352 760directory."
0d2d639a
JB
761
762 (interactive
763 (list (if current-prefix-arg
764 (read-file-name "Filename: " pages-addresses-file-name))))
765
32226619 766 (if (called-interactively-p 'interactive)
0d2d639a
JB
767 (message "Creating directory for: %s "
768 (or filename pages-addresses-file-name)))
769 (if (file-exists-p (or filename pages-addresses-file-name))
770 (progn
771 (set-buffer
772 (find-file-noselect
773 (expand-file-name
774 (or filename pages-addresses-file-name))))
775 (widen)
776 (pages-directory t nil nil)
73aee91d
RC
777 ;; by RJC, 2006 Jun 11: including this causes failure; it results in
778 ;; the message "Buffer in which pages were found is deleted"
779 ;; (pages-directory-address-mode)
98d87352
RS
780 (setq pages-directory-buffer-narrowing-p
781 pages-directory-for-addresses-goto-narrowing-p)
782 (or pages-directory-for-addresses-buffer-keep-windows-p
783 (delete-other-windows))
0d2d639a
JB
784 (save-excursion
785 (goto-char (point-min))
5ed619e0 786 (delete-region (point) (line-end-position))
0d2d639a
JB
787 (insert
788 "=== Address List Directory: use `C-c C-c' to go to page under cursor. ===")
789 (set-buffer-modified-p nil)
790 ))
791 (error "No addresses file found!")))
792
764e5002
SM
793(define-derived-mode pages-directory-address-mode pages-directory-mode
794 "Addresses Directory"
0d2d639a
JB
795 "Mode for handling the Addresses Directory buffer.
796
98d87352
RS
797Move point to one of the lines in this buffer,
798then use \\[pages-directory-goto] to go
0d2d639a 799to the same line in the pages buffer."
764e5002 800 :syntax-table nil)
0d2d639a 801
98d87352 802(provide 'page-ext)
ab5796a9 803
e8af40ee 804;;; page-ext.el ends here