*** empty log message ***
[emacs.git] / lisp / textmodes / page.el
blob65cad14e6ed37ede19bbe209990cbcfc45fde37f
1 ;;; page.el --- page motion commands for emacs.
3 ;; Maintainer: FSF
4 ;; Last-Modified: 01 May 1992
6 ;; Copyright (C) 1985 Free Software Foundation, Inc.
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
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24 ;;; Code:
26 (defun forward-page (&optional count)
27 "Move forward to page boundary. With arg, repeat, or go back if negative.
28 A page boundary is any line whose beginning matches the regexp
29 `page-delimiter'."
30 (interactive "p")
31 (or count (setq count 1))
32 (while (and (> count 0) (not (eobp)))
33 (if (re-search-forward page-delimiter nil t)
34 nil
35 (goto-char (point-max)))
36 (setq count (1- count)))
37 (while (and (< count 0) (not (bobp)))
38 (forward-char -1)
39 (if (re-search-backward page-delimiter nil t)
40 (goto-char (match-end 0))
41 (goto-char (point-min)))
42 (setq count (1+ count))))
44 (defun backward-page (&optional count)
45 "Move backward to page boundary. With arg, repeat, or go fwd if negative.
46 A page boundary is any line whose beginning matches the regexp
47 `page-delimiter'."
48 (interactive "p")
49 (or count (setq count 1))
50 (forward-page (- count)))
52 (defun mark-page (&optional arg)
53 "Put mark at end of page, point at beginning.
54 A numeric arg specifies to move forward or backward by that many pages,
55 thus marking a page other than the one point was originally in."
56 (interactive "P")
57 (setq arg (if arg (prefix-numeric-value arg) 0))
58 (if (> arg 0)
59 (forward-page arg)
60 (if (< arg 0)
61 (forward-page (1- arg))))
62 (forward-page)
63 (push-mark nil t)
64 (forward-page -1))
66 (defun narrow-to-page (&optional arg)
67 "Make text outside current page invisible.
68 A numeric arg specifies to move forward or backward by that many pages,
69 thus showing a page other than the one point was originally in."
70 (interactive "P")
71 (setq arg (if arg (prefix-numeric-value arg) 0))
72 (save-excursion
73 (widen)
74 (if (> arg 0)
75 (forward-page arg)
76 (if (< arg 0)
77 (forward-page (1- arg))))
78 ;; Find the end of the page.
79 (forward-page)
80 ;; If we stopped due to end of buffer, stay there.
81 ;; If we stopped after a page delimiter, put end of restriction
82 ;; at the beginning of that line.
83 (if (save-excursion (beginning-of-line)
84 (looking-at page-delimiter))
85 (beginning-of-line))
86 (narrow-to-region (point)
87 (progn
88 ;; Find the top of the page.
89 (forward-page -1)
90 ;; If we found beginning of buffer, stay there.
91 ;; If extra text follows page delimiter on same line,
92 ;; include it.
93 ;; Otherwise, show text starting with following line.
94 (if (and (eolp) (not (bobp)))
95 (forward-line 1))
96 (point)))))
97 (put 'narrow-to-page 'disabled t)
99 (defun count-lines-page ()
100 "Report number of lines on current page, and how many are before or after point."
101 (interactive)
102 (save-excursion
103 (let ((opoint (point)) beg end
104 total before after)
105 (forward-page)
106 (beginning-of-line)
107 (or (looking-at page-delimiter)
108 (end-of-line))
109 (setq end (point))
110 (backward-page)
111 (setq beg (point))
112 (setq total (count-lines beg end)
113 before (count-lines beg opoint)
114 after (count-lines opoint end))
115 (message "Page has %d lines (%d + %d)" total before after))))
117 (defun what-page ()
118 "Print page and line number of point."
119 (interactive)
120 (save-restriction
121 (widen)
122 (save-excursion
123 (beginning-of-line)
124 (let ((count 1)
125 (opoint (point)))
126 (goto-char 1)
127 (while (re-search-forward page-delimiter opoint t)
128 (setq count (1+ count)))
129 (message "Page %d, line %d"
130 count
131 (1+ (count-lines (point) opoint)))))))
133 ;;; page.el ends here