Skip to content

Instantly share code, notes, and snippets.

@knollet
Last active March 20, 2024 17:29
Show Gist options
  • Save knollet/851c13ed1354f7a288e9a94e7dee64fc to your computer and use it in GitHub Desktop.
Save knollet/851c13ed1354f7a288e9a94e7dee64fc to your computer and use it in GitHub Desktop.
(defun mo-paging--what-page ()
"Print page and line number of point."
(interactive)
(save-restriction
(widen)
(save-excursion
(let ((count 1)
(opoint (point)))
(goto-char (point-min))
(while (re-search-forward page-delimiter opoint t)
(if (= (match-beginning 0) (match-end 0))
(forward-char 1))
(setq count (1+ count)))
count))))
(defun mo-paging--forward-page ()
(interactive)
(widen)
(forward-page)
(narrow-to-page))
(defun mo-paging--backwards-page ()
(interactive)
(widen)
(let ((p (mo-paging--what-page)))
(backward-page)
(when (= (mo-paging--what-page) p)
(backward-page)))
(narrow-to-page))
(defun mo-paging--goto-page (n)
(interactive "nPage Number: ")
(widen)
(goto-char 0)
(dotimes (x (- n 1) ())
(forward-page))
(narrow-to-page))
(define-minor-mode mo-paging-mode
"narrow to pages and let you leaf through them by the usual keys
C-x [ - (backwards-page)
and
C-x ] - (forwards-page)"
:lighter (:eval (format " [%s]" (mo-paging--what-page)))
:after-hook (if mo-paging-mode
(progn
(setf header-line-format
"Forward: C-x ] | Backward: C-x [ | Goto: C-x g")
(goto-char 0)
(narrow-to-page))
(setf header-line-format nil)
(widen))
:keymap (list (cons (kbd "C-x ]") 'mo-paging--forward-page)
(cons (kbd "C-x [") 'mo-paging--backwards-page)
(cons (kbd "C-x g") 'mo-paging--goto-page)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment