page-ext.el 29 KB

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