Commit | Line | Data |
---|---|---|
a2535589 JA |
1 | ;; Page motion commands for emacs. |
2 | ;; Copyright (C) 1985 Free Software Foundation, Inc. | |
3 | ||
4 | ;; This file is part of GNU Emacs. | |
5 | ||
6 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
7 | ;; it under the terms of the GNU General Public License as published by | |
8 | ;; the Free Software Foundation; either version 1, or (at your option) | |
9 | ;; any later version. | |
10 | ||
11 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
12 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | ;; GNU General Public License for more details. | |
15 | ||
16 | ;; You should have received a copy of the GNU General Public License | |
17 | ;; along with GNU Emacs; see the file COPYING. If not, write to | |
18 | ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
19 | ||
20 | ||
21 | (defun forward-page (&optional count) | |
22 | "Move forward to page boundary. With arg, repeat, or go back if negative. | |
fa8a7944 JB |
23 | A page boundary is any line whose beginning matches the regexp |
24 | `page-delimiter'." | |
a2535589 JA |
25 | (interactive "p") |
26 | (or count (setq count 1)) | |
27 | (while (and (> count 0) (not (eobp))) | |
28 | (if (re-search-forward page-delimiter nil t) | |
29 | nil | |
30 | (goto-char (point-max))) | |
31 | (setq count (1- count))) | |
32 | (while (and (< count 0) (not (bobp))) | |
33 | (forward-char -1) | |
34 | (if (re-search-backward page-delimiter nil t) | |
35 | (goto-char (match-end 0)) | |
36 | (goto-char (point-min))) | |
37 | (setq count (1+ count)))) | |
38 | ||
39 | (defun backward-page (&optional count) | |
40 | "Move backward to page boundary. With arg, repeat, or go fwd if negative. | |
fa8a7944 JB |
41 | A page boundary is any line whose beginning matches the regexp |
42 | `page-delimiter'." | |
a2535589 JA |
43 | (interactive "p") |
44 | (or count (setq count 1)) | |
45 | (forward-page (- count))) | |
46 | ||
47 | (defun mark-page (&optional arg) | |
48 | "Put mark at end of page, point at beginning. | |
49 | A numeric arg specifies to move forward or backward by that many pages, | |
50 | thus marking a page other than the one point was originally in." | |
51 | (interactive "P") | |
52 | (setq arg (if arg (prefix-numeric-value arg) 0)) | |
53 | (if (> arg 0) | |
54 | (forward-page arg) | |
55 | (if (< arg 0) | |
56 | (forward-page (1- arg)))) | |
57 | (forward-page) | |
58 | (push-mark nil t) | |
59 | (forward-page -1)) | |
60 | ||
61 | (defun narrow-to-page (&optional arg) | |
62 | "Make text outside current page invisible. | |
63 | A numeric arg specifies to move forward or backward by that many pages, | |
64 | thus showing a page other than the one point was originally in." | |
65 | (interactive "P") | |
66 | (setq arg (if arg (prefix-numeric-value arg) 0)) | |
67 | (save-excursion | |
68 | (widen) | |
69 | (if (> arg 0) | |
70 | (forward-page arg) | |
71 | (if (< arg 0) | |
72 | (forward-page (1- arg)))) | |
73 | ;; Find the end of the page. | |
74 | (forward-page) | |
75 | ;; If we stopped due to end of buffer, stay there. | |
76 | ;; If we stopped after a page delimiter, put end of restriction | |
77 | ;; at the beginning of that line. | |
78 | (if (save-excursion (beginning-of-line) | |
79 | (looking-at page-delimiter)) | |
80 | (beginning-of-line)) | |
81 | (narrow-to-region (point) | |
82 | (progn | |
83 | ;; Find the top of the page. | |
84 | (forward-page -1) | |
85 | ;; If we found beginning of buffer, stay there. | |
86 | ;; If extra text follows page delimiter on same line, | |
87 | ;; include it. | |
88 | ;; Otherwise, show text starting with following line. | |
89 | (if (and (eolp) (not (bobp))) | |
90 | (forward-line 1)) | |
91 | (point))))) | |
92 | ||
93 | (defun count-lines-page () | |
94 | "Report number of lines on current page, and how many are before or after point." | |
95 | (interactive) | |
96 | (save-excursion | |
97 | (let ((opoint (point)) beg end | |
98 | total before after) | |
99 | (forward-page) | |
100 | (beginning-of-line) | |
101 | (or (looking-at page-delimiter) | |
102 | (end-of-line)) | |
103 | (setq end (point)) | |
104 | (backward-page) | |
105 | (setq beg (point)) | |
106 | (setq total (count-lines beg end) | |
107 | before (count-lines beg opoint) | |
108 | after (count-lines opoint end)) | |
109 | (message "Page has %d lines (%d + %d)" total before after)))) | |
110 | ||
111 | (defun what-page () | |
112 | "Print page and line number of point." | |
113 | (interactive) | |
114 | (save-restriction | |
115 | (widen) | |
116 | (save-excursion | |
117 | (beginning-of-line) | |
118 | (let ((count 1) | |
119 | (opoint (point))) | |
120 | (goto-char 1) | |
121 | (while (re-search-forward page-delimiter opoint t) | |
122 | (setq count (1+ count))) | |
123 | (message "Page %d, line %d" | |
124 | count | |
125 | (1+ (count-lines (point) opoint))))))) |