page.el 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. ;;; page.el --- page motion commands for Emacs
  2. ;; Copyright (C) 1985, 2001-2012 Free Software Foundation, Inc.
  3. ;; Maintainer: FSF
  4. ;; Keywords: wp convenience
  5. ;; Package: emacs
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  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. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; This code provides the page-oriented movement and selection commands
  19. ;; documented in the Emacs manual.
  20. ;;; Code:
  21. (defun forward-page (&optional count)
  22. "Move forward to page boundary. With arg, repeat, or go back if negative.
  23. A page boundary is any line whose beginning matches the regexp
  24. `page-delimiter'."
  25. (interactive "p")
  26. (or count (setq count 1))
  27. (while (and (> count 0) (not (eobp)))
  28. ;; In case the page-delimiter matches the null string,
  29. ;; don't find a match without moving.
  30. (if (bolp) (forward-char 1))
  31. (if (re-search-forward page-delimiter nil t)
  32. nil
  33. (goto-char (point-max)))
  34. (setq count (1- count)))
  35. (while (and (< count 0) (not (bobp)))
  36. ;; In case the page-delimiter matches the null string,
  37. ;; don't find a match without moving.
  38. (and (save-excursion (re-search-backward page-delimiter nil t))
  39. (= (match-end 0) (point))
  40. (goto-char (match-beginning 0)))
  41. (forward-char -1)
  42. (if (re-search-backward page-delimiter nil t)
  43. ;; We found one--move to the end of it.
  44. (goto-char (match-end 0))
  45. ;; We found nothing--go to beg of buffer.
  46. (goto-char (point-min)))
  47. (setq count (1+ count))))
  48. (defun backward-page (&optional count)
  49. "Move backward to page boundary. With arg, repeat, or go fwd if negative.
  50. A page boundary is any line whose beginning matches the regexp
  51. `page-delimiter'."
  52. (interactive "p")
  53. (or count (setq count 1))
  54. (forward-page (- count)))
  55. (defun mark-page (&optional arg)
  56. "Put mark at end of page, point at beginning.
  57. A numeric arg specifies to move forward or backward by that many pages,
  58. thus marking a page other than the one point was originally in."
  59. (interactive "P")
  60. (setq arg (if arg (prefix-numeric-value arg) 0))
  61. (if (> arg 0)
  62. (forward-page arg)
  63. (if (< arg 0)
  64. (forward-page (1- arg))))
  65. (forward-page)
  66. (push-mark nil t t)
  67. (forward-page -1))
  68. (defun narrow-to-page (&optional arg)
  69. "Make text outside current page invisible.
  70. A numeric arg specifies to move forward or backward by that many pages,
  71. thus showing a page other than the one point was originally in."
  72. (interactive "P")
  73. (setq arg (if arg (prefix-numeric-value arg) 0))
  74. (save-excursion
  75. (widen)
  76. (if (> arg 0)
  77. (forward-page arg)
  78. (if (< arg 0)
  79. (let ((adjust 0)
  80. (opoint (point)))
  81. ;; If we are not now at the beginning of a page,
  82. ;; move back one extra time, to get to the start of this page.
  83. (save-excursion
  84. (beginning-of-line)
  85. (or (and (looking-at page-delimiter)
  86. (eq (match-end 0) opoint))
  87. (setq adjust 1)))
  88. (forward-page (- arg adjust)))))
  89. ;; Find the end of the page.
  90. (set-match-data nil)
  91. (forward-page)
  92. ;; If we stopped due to end of buffer, stay there.
  93. ;; If we stopped after a page delimiter, put end of restriction
  94. ;; at the beginning of that line.
  95. ;; Before checking the match that was found,
  96. ;; verify that forward-page actually set the match data.
  97. (if (and (match-beginning 0)
  98. (save-excursion
  99. (goto-char (match-beginning 0)) ; was (beginning-of-line)
  100. (looking-at page-delimiter)))
  101. (goto-char (match-beginning 0))) ; was (beginning-of-line)
  102. (narrow-to-region (point)
  103. (progn
  104. ;; Find the top of the page.
  105. (forward-page -1)
  106. ;; If we found beginning of buffer, stay there.
  107. ;; If extra text follows page delimiter on same line,
  108. ;; include it.
  109. ;; Otherwise, show text starting with following line.
  110. (if (and (eolp) (not (bobp)))
  111. (forward-line 1))
  112. (point)))))
  113. (put 'narrow-to-page 'disabled t)
  114. (defun count-lines-page ()
  115. "Report number of lines on current page, and how many are before or after point."
  116. (interactive)
  117. (save-excursion
  118. (let ((opoint (point)) beg end
  119. total before after)
  120. (forward-page)
  121. (beginning-of-line)
  122. (or (looking-at page-delimiter)
  123. (end-of-line))
  124. (setq end (point))
  125. (backward-page)
  126. (setq beg (point))
  127. (setq total (count-lines beg end)
  128. before (count-lines beg opoint)
  129. after (count-lines opoint end))
  130. (message "Page has %d lines (%d + %d)" total before after))))
  131. (defun what-page ()
  132. "Print page and line number of point."
  133. (interactive)
  134. (save-restriction
  135. (widen)
  136. (save-excursion
  137. (let ((count 1)
  138. (opoint (point)))
  139. (goto-char (point-min))
  140. (while (re-search-forward page-delimiter opoint t)
  141. (if (= (match-beginning 0) (match-end 0))
  142. (forward-char 1))
  143. (setq count (1+ count)))
  144. (message "Page %d, line %d" count (line-number-at-pos opoint))))))
  145. ;;; Place `provide' at end of file.
  146. (provide 'page)
  147. ;;; page.el ends here