page.el 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. ;; Page motion commands for emacs.
  2. ;; Copyright (C) 1985 Free Software Foundation, Inc.
  3. ;; This file is part of GNU Emacs.
  4. ;; GNU Emacs is distributed in the hope that it will be useful,
  5. ;; but WITHOUT ANY WARRANTY. No author or distributor
  6. ;; accepts responsibility to anyone for the consequences of using it
  7. ;; or for whether it serves any particular purpose or works at all,
  8. ;; unless he says so in writing. Refer to the GNU Emacs General Public
  9. ;; License for full details.
  10. ;; Everyone is granted permission to copy, modify and redistribute
  11. ;; GNU Emacs, but only under the conditions described in the
  12. ;; GNU Emacs General Public License. A copy of this license is
  13. ;; supposed to have been given to you along with GNU Emacs so you
  14. ;; can know your rights and responsibilities. It should be in a
  15. ;; file named COPYING. Among other things, the copyright notice
  16. ;; and this notice must be preserved on all copies.
  17. (defun forward-page (&optional count)
  18. "Move forward to page boundary. With arg, repeat, or go back if negative.
  19. A page boundary is any line whose beginning matches the regexp page-delimiter."
  20. (interactive "p")
  21. (or count (setq count 1))
  22. (while (and (> count 0) (not (eobp)))
  23. (if (re-search-forward page-delimiter nil t)
  24. nil
  25. (goto-char (point-max)))
  26. (setq count (1- count)))
  27. (while (and (< count 0) (not (bobp)))
  28. (forward-char -1)
  29. (if (re-search-backward page-delimiter nil t)
  30. (goto-char (match-end 0))
  31. (goto-char (point-min)))
  32. (setq count (1+ count))))
  33. (defun backward-page (&optional count)
  34. "Move backward to page boundary. With arg, repeat, or go fwd if negative.
  35. A page boundary is any line whose beginning matches the regexp page-delimiter."
  36. (interactive "p")
  37. (or count (setq count 1))
  38. (forward-page (- count)))
  39. (defun mark-page (&optional arg)
  40. "Put mark at end of page, point at beginning.
  41. A numeric arg specifies to move forward or backward by that many pages,
  42. thus marking a page other than the one point was originally in."
  43. (interactive "P")
  44. (setq arg (if arg (prefix-numeric-value arg) 0))
  45. (if (> arg 0)
  46. (forward-page arg)
  47. (if (< arg 0)
  48. (forward-page (1- arg))))
  49. (forward-page)
  50. (push-mark nil t)
  51. (forward-page -1))
  52. (defun narrow-to-page (&optional arg)
  53. "Make text outside current page invisible.
  54. A numeric arg specifies to move forward or backward by that many pages,
  55. thus showing a page other than the one point was originally in."
  56. (interactive "P")
  57. (setq arg (if arg (prefix-numeric-value arg) 0))
  58. (save-excursion
  59. (if (> arg 0)
  60. (forward-page arg)
  61. (if (< arg 0)
  62. (forward-page (1- arg))))
  63. (forward-page)
  64. (beginning-of-line)
  65. (narrow-to-region (point)
  66. (progn
  67. (forward-page -1)
  68. (point)))))
  69. (defun count-lines-page ()
  70. "Report number of lines on current page, and how many are before or after point."
  71. (interactive)
  72. (save-excursion
  73. (let ((opoint (point)) beg end
  74. total before after)
  75. (forward-page)
  76. (beginning-of-line)
  77. (or (looking-at page-delimiter)
  78. (end-of-line))
  79. (setq end (point))
  80. (backward-page)
  81. (setq beg (point))
  82. (setq total (count-lines beg end)
  83. before (count-lines beg opoint)
  84. after (count-lines opoint end))
  85. (message "Page has %d lines (%d + %d)" total before after))))
  86. (defun what-page ()
  87. "Print page and line number of point."
  88. (interactive)
  89. (save-restriction
  90. (widen)
  91. (save-excursion
  92. (let ((count 1)
  93. (opoint (point)))
  94. (goto-char 1)
  95. (while (re-search-forward page-delimiter opoint t)
  96. (setq count (1+ count)))
  97. (message "Page %d, line %d"
  98. count
  99. (1+ (count-lines (point) opoint)))))))