home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / lucid / lemacs-19.6 / lisp / packages / page-ext.el < prev    next >
Encoding:
Text File  |  1992-06-29  |  27.1 KB  |  748 lines

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