Skip to content

Instantly share code, notes, and snippets.

@nxg
Last active December 21, 2015 17:39
Show Gist options
  • Save nxg/6341779 to your computer and use it in GitHub Desktop.
Save nxg/6341779 to your computer and use it in GitHub Desktop.
Emacs lisp code to provide a stack of buffer names and point locations. I find this useful because I often need to do something elsewhere in the same or another buffer, and then return to what I was doing – perhaps I need to run around in the file to find a function definition or something. Then I want to be taken right back to where I was befor…
;;; point-stack.el
;; This file is free software.
;; Copyright Greg Novak (novak@ucolick.org) December 2004
;; Copyright Norman Gray (norman@astro.gla.ac.uk) January 2013
;; Released under the GPL, available at http://www.gnu.org
;;
;; Provide a stack of buffer names and point locations
;;
;; I find this useful because I often need to do something elsewhere
;; in a file and then return to what I was doing. Ie, I need to run
;; around in the file to find a function definition or something.
;; Then I want to be taken right back to where I was before the
;; "context switch"
;;
;; The following are possible key definitions for the .emacs file.
;;
;; (require 'point-stack)
;; (global-set-key "\C-cm" 'point-stack-push) ; mark location
;; (global-set-key "\C-cb" 'point-stack-pop) ; go back
;; (global-set-key "\C-cx" 'point-stack-swap) ; eXchange point and top of stack
;; (global-set-key "\C-cp" 'point-stack-show) ; print stack
;;
;; Modified 2012-10-08 by Norman, to remove cadar, undefined in Cocoa Emacs 24
;; Modified 2013-01-26 by Norman, to add point-stack swap and point-stack-show
;;
;; Available as a github gist: https://gist.github.com/nxg/6341779
(defvar point-stack% nil) ;the data structure
(defun point-stack-push ()
(interactive)
(point-stack-push* (cons (current-buffer) (point)))
(point-stack-show))
(defun point-stack-push* (pos)
"Push the given position onto the top of the point-stack%"
(setq point-stack% (cons pos point-stack%))
(length point-stack%))
(defun point-stack-pop ()
"Pop a position from the top of the stack, and go there"
(interactive)
(let ((top (point-stack-pop*)))
(if top
(progn (point-stack-goto* top)
(point-stack-show))
(message "Stack empty"))))
(defun point-stack-pop* ()
"Pop a position from the top of the stack and return it"
(and (not (null point-stack%))
(let ((top (car point-stack%)))
(setq point-stack% (cdr point-stack%))
top)))
(defun point-stack-goto* (pos)
"Go to the given position"
(switch-to-buffer (car pos))
(goto-char (cdr pos)))
(defun point-stack-swap ()
"Swap the current location with the location on the top of the point-stack"
(interactive)
(if (null point-stack%)
(message "Point stack is empty")
(let ((old-top (point-stack-pop*)))
(point-stack-push)
(point-stack-goto* old-top)
(point-stack-show))))
(defun point-stack-show ()
"Show the point-stack in the message line"
(interactive)
(message "point-stack: %s"
(if (null point-stack%)
"<empty>"
(mapconcat (lambda (s) (buffer-name (car s)))
point-stack%
" -> "))))
(provide 'point-stack)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment