page.el 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. ;;; page.el --- page motion commands for Emacs
  2. ;; Copyright (C) 1985, 2001-2017 Free Software Foundation, Inc.
  3. ;; Maintainer: emacs-devel@gnu.org
  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. (unless (bobp)
  42. (forward-char -1)
  43. (if (re-search-backward page-delimiter nil t)
  44. ;; We found one--move to the end of it.
  45. (goto-char (match-end 0))
  46. ;; We found nothing--go to beg of buffer.
  47. (goto-char (point-min))))
  48. (setq count (1+ count))))
  49. (defun backward-page (&optional count)
  50. "Move backward to page boundary. With arg, repeat, or go fwd if negative.
  51. A page boundary is any line whose beginning matches the regexp
  52. `page-delimiter'."
  53. (interactive "p")
  54. (or count (setq count 1))
  55. (forward-page (- count)))
  56. (defun mark-page (&optional arg)
  57. "Put mark at end of page, point at beginning.
  58. A numeric arg specifies to move forward or backward by that many pages,
  59. thus marking a page other than the one point was originally in."
  60. (interactive "P")
  61. (setq arg (if arg (prefix-numeric-value arg) 0))
  62. (if (> arg 0)
  63. (forward-page arg)
  64. (if (< arg 0)
  65. (forward-page (1- arg))))
  66. (forward-page)
  67. (push-mark nil t t)
  68. (forward-page -1))
  69. (defun narrow-to-page (&optional arg)
  70. "Make text outside current page invisible.
  71. A numeric arg specifies to move forward or backward by that many pages,
  72. thus showing a page other than the one point was originally in."
  73. (interactive "P")
  74. (setq arg (if arg (prefix-numeric-value arg) 0))
  75. (save-excursion
  76. (widen)
  77. (if (> arg 0)
  78. (forward-page arg)
  79. (if (< arg 0)
  80. (let ((adjust 0)
  81. (opoint (point)))
  82. ;; If we are not now at the beginning of a page,
  83. ;; move back one extra time, to get to the start of this page.
  84. (save-excursion
  85. (beginning-of-line)
  86. (or (and (looking-at page-delimiter)
  87. (eq (match-end 0) opoint))
  88. (setq adjust 1)))
  89. (forward-page (- arg adjust)))))
  90. ;; Find the end of the page.
  91. (set-match-data nil)
  92. (forward-page)
  93. ;; If we stopped due to end of buffer, stay there.
  94. ;; If we stopped after a page delimiter, put end of restriction
  95. ;; at the beginning of that line.
  96. ;; Before checking the match that was found,
  97. ;; verify that forward-page actually set the match data.
  98. (if (and (match-beginning 0)
  99. (save-excursion
  100. (goto-char (match-beginning 0)) ; was (beginning-of-line)
  101. (looking-at page-delimiter)))
  102. (goto-char (match-beginning 0))) ; was (beginning-of-line)
  103. (narrow-to-region (point)
  104. (progn
  105. ;; Find the top of the page.
  106. (forward-page -1)
  107. ;; If we found beginning of buffer, stay there.
  108. ;; If extra text follows page delimiter on same line,
  109. ;; include it.
  110. ;; Otherwise, show text starting with following line.
  111. (if (and (eolp) (not (bobp)))
  112. (forward-line 1))
  113. (point)))))
  114. (put 'narrow-to-page 'disabled t)
  115. (defun count-lines-page ()
  116. "Report number of lines on current page, and how many are before or after point."
  117. (interactive)
  118. (save-excursion
  119. (let ((opoint (point)) beg end
  120. total before after)
  121. (forward-page)
  122. (beginning-of-line)
  123. (or (looking-at page-delimiter)
  124. (end-of-line))
  125. (setq end (point))
  126. (backward-page)
  127. (setq beg (point))
  128. (setq total (count-lines beg end)
  129. before (count-lines beg opoint)
  130. after (count-lines opoint end))
  131. (message "Page has %d lines (%d + %d)" total before after))))
  132. (defun what-page ()
  133. "Print page and line number of point."
  134. (interactive)
  135. (save-restriction
  136. (widen)
  137. (save-excursion
  138. (let ((count 1)
  139. (opoint (point)))
  140. (goto-char (point-min))
  141. (while (re-search-forward page-delimiter opoint t)
  142. (if (= (match-beginning 0) (match-end 0))
  143. (forward-char 1))
  144. (setq count (1+ count)))
  145. (message "Page %d, line %d" count (line-number-at-pos opoint))))))
  146. ;;; Place `provide' at end of file.
  147. (provide 'page)
  148. ;;; page.el ends here