(narrow-to-page): Exclude _entire_ multi-line delimiter from the region
[emacs.git] / lisp / textmodes / page.el
blob3ec1eca1fb7b33188b8534ef6796529b3e9acf26
1 ;;; page.el --- page motion commands for Emacs
3 ;; Copyright (C) 1985, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
5 ;; Maintainer: FSF
6 ;; Keywords: wp convenience
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
25 ;;; Commentary:
27 ;; This code provides the page-oriented movement and selection commands
28 ;; documented in the Emacs manual.
30 ;;; Code:
32 (defun forward-page (&optional count)
33 "Move forward to page boundary. With arg, repeat, or go back if negative.
34 A page boundary is any line whose beginning matches the regexp
35 `page-delimiter'."
36 (interactive "p")
37 (or count (setq count 1))
38 (while (and (> count 0) (not (eobp)))
39 ;; In case the page-delimiter matches the null string,
40 ;; don't find a match without moving.
41 (if (bolp) (forward-char 1))
42 (if (re-search-forward page-delimiter nil t)
43 nil
44 (goto-char (point-max)))
45 (setq count (1- count)))
46 (while (and (< count 0) (not (bobp)))
47 ;; In case the page-delimiter matches the null string,
48 ;; don't find a match without moving.
49 (and (save-excursion (re-search-backward page-delimiter nil t))
50 (= (match-end 0) (point))
51 (goto-char (match-beginning 0)))
52 (forward-char -1)
53 (if (re-search-backward page-delimiter nil t)
54 ;; We found one--move to the end of it.
55 (goto-char (match-end 0))
56 ;; We found nothing--go to beg of buffer.
57 (goto-char (point-min)))
58 (setq count (1+ count))))
60 (defun backward-page (&optional count)
61 "Move backward to page boundary. With arg, repeat, or go fwd if negative.
62 A page boundary is any line whose beginning matches the regexp
63 `page-delimiter'."
64 (interactive "p")
65 (or count (setq count 1))
66 (forward-page (- count)))
68 (defun mark-page (&optional arg)
69 "Put mark at end of page, point at beginning.
70 A numeric arg specifies to move forward or backward by that many pages,
71 thus marking a page other than the one point was originally in."
72 (interactive "P")
73 (setq arg (if arg (prefix-numeric-value arg) 0))
74 (if (> arg 0)
75 (forward-page arg)
76 (if (< arg 0)
77 (forward-page (1- arg))))
78 (forward-page)
79 (push-mark nil t t)
80 (forward-page -1))
82 (defun narrow-to-page (&optional arg)
83 "Make text outside current page invisible.
84 A numeric arg specifies to move forward or backward by that many pages,
85 thus showing a page other than the one point was originally in."
86 (interactive "P")
87 (setq arg (if arg (prefix-numeric-value arg) 0))
88 (save-excursion
89 (widen)
90 (if (> arg 0)
91 (forward-page arg)
92 (if (< arg 0)
93 (let ((adjust 0)
94 (opoint (point)))
95 ;; If we are not now at the beginning of a page,
96 ;; move back one extra time, to get to the start of this page.
97 (save-excursion
98 (beginning-of-line)
99 (or (and (looking-at page-delimiter)
100 (eq (match-end 0) opoint))
101 (setq adjust 1)))
102 (forward-page (- arg adjust)))))
103 ;; Find the end of the page.
104 (set-match-data nil)
105 (forward-page)
106 ;; If we stopped due to end of buffer, stay there.
107 ;; If we stopped after a page delimiter, put end of restriction
108 ;; at the beginning of that line.
109 ;; Before checking the match that was found,
110 ;; verify that forward-page actually set the match data.
111 (if (and (match-beginning 0)
112 (save-excursion
113 (goto-char (match-beginning 0)) ; was (beginning-of-line)
114 (looking-at page-delimiter)))
115 (goto-char (match-beginning 0))) ; was (beginning-of-line)
116 (narrow-to-region (point)
117 (progn
118 ;; Find the top of the page.
119 (forward-page -1)
120 ;; If we found beginning of buffer, stay there.
121 ;; If extra text follows page delimiter on same line,
122 ;; include it.
123 ;; Otherwise, show text starting with following line.
124 (if (and (eolp) (not (bobp)))
125 (forward-line 1))
126 (point)))))
127 (put 'narrow-to-page 'disabled t)
129 (defun count-lines-page ()
130 "Report number of lines on current page, and how many are before or after point."
131 (interactive)
132 (save-excursion
133 (let ((opoint (point)) beg end
134 total before after)
135 (forward-page)
136 (beginning-of-line)
137 (or (looking-at page-delimiter)
138 (end-of-line))
139 (setq end (point))
140 (backward-page)
141 (setq beg (point))
142 (setq total (count-lines beg end)
143 before (count-lines beg opoint)
144 after (count-lines opoint end))
145 (message "Page has %d lines (%d + %d)" total before after))))
147 (defun what-page ()
148 "Print page and line number of point."
149 (interactive)
150 (save-restriction
151 (widen)
152 (save-excursion
153 (beginning-of-line)
154 (let ((count 1)
155 (opoint (point)))
156 (goto-char 1)
157 (while (re-search-forward page-delimiter opoint t)
158 (setq count (1+ count)))
159 (message "Page %d, line %d"
160 count
161 (1+ (count-lines (point) opoint)))))))
163 ;;; Place `provide' at end of file.
164 (provide 'page)
166 ;;; arch-tag: e8d7a0bd-8655-4b6e-b852-f2ee25316a1d
167 ;;; page.el ends here