Skip to content

Instantly share code, notes, and snippets.

@jamescherti
Created October 23, 2024 16:59
Show Gist options
  • Save jamescherti/8921b341b628599ca8c7ff7f173a435a to your computer and use it in GitHub Desktop.
Save jamescherti/8921b341b628599ca8c7ff7f173a435a to your computer and use it in GitHub Desktop.
Emacs: Save point as a marker, window start/hscroll, restoring them after BODY.
;;; save-marker-and-window.el --- Emacs: Save point as a marker, window start/hscroll, restoring them after BODY. -*- lexical-binding: t; -*-
;; Gits URL: https://gist.github.com/jamescherti/8921b341b628599ca8c7ff7f173a435a
;; Usage: (my-save-marker-and-window (progn (your-code argument)))
;; License: MIT
;; Author: James Cherti
(defmacro my-save-marker-and-window (&rest body)
"Save point as a marker, window start/hscroll, restoring them after BODY."
`(let ((point-marker (copy-marker (point)))
(window-start-marker (copy-marker (window-start)))
(window-hscroll (window-hscroll)))
(unwind-protect
(progn ,@body)
(goto-char point-marker)
(set-window-start (selected-window) window-start-marker)
(set-window-hscroll (selected-window) window-hscroll))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment