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