Skip to content

Instantly share code, notes, and snippets.

Created September 17, 2009 23:07
Show Gist options
  • Save anonymous/188773 to your computer and use it in GitHub Desktop.
Save anonymous/188773 to your computer and use it in GitHub Desktop.
(require 'cl)
(require 'highlight-current-line)
(defface mini-map-face
'((t (:font "Consolas-2")))
"Face used in mini-map")
(defface mini-map-window-highlight-face
'((t (:background "#333333")))
"Face used in mini-map to highlight the visible window")
(defun mini-map-window ()
"Return a mini-map window for the current frame"
(or (some (lambda (w)
(if (window-parameter w 'mini-map-p) w))
(window-list))
(let ((w (split-window-horizontally 15))
(m (selected-window)))
(set-window-dedicated-p m 1)
(set-window-parameter m 'mini-map-p t)
(select-window w)
m)))
(defun mini-map-window-p (w)
"Test if a window is a mini-map"
(window-parameter w 'mini-map-p))
(define-minor-mode mini-map-mode
"Toggle mini-map-mode for this buffer"
nil " map" nil
(if mini-map-mode
(install-mini-map)
(remove-mini-map)))
(defun install-mini-map ()
(set (make-local-variable 'mini-map-buffer)
(clone-indirect-buffer
(format "%s <mini-map>" (buffer-name)) nil t))
(with-current-buffer mini-map-buffer
(buffer-face-mode t)
(buffer-face-set 'mini-map-face)
(highlight-current-line-minor-mode))
(let ((win-start (window-start))
(win-end (window-end))
(map-buf mini-map-buffer)
(map-win (mini-map-window)))
(with-selected-window map-win
(switch-to-buffer map-buf)
(set (make-local-variable 'mini-map-window-overlay)
(make-overlay win-start win-end))
(overlay-put mini-map-window-overlay
'face 'mini-map-window-highlight-face)))
(add-hook 'post-command-hook 'mini-map-update-position nil t)
(add-hook 'window-scroll-functions 'mini-map-update-scroll nil t))
(defun remove-mini-map ()
(remove-hook 'window-scroll-functions 'mini-map-update-scroll t)
(remove-hook 'post-command-hook 'mini-map-update-position t)
(with-current-buffer mini-map-buffer
(delete-overlay mini-map-window-overlay)
(set-buffer-modified-p nil)
(kill-buffer))
(setq mini-map-buffer nil))
(defun mini-map-update-position ()
"Update cursor position in mini-map"
(let ((p (point))
(b mini-map-buffer))
(with-selected-window (mini-map-window)
(if (not (eq (current-buffer) b))
(switch-to-buffer b))
(goto-char p)
(highlight-current-line-hook))))
(defun mini-map-update-scroll (window start)
"Update background highlighting in mini-map"
(with-current-buffer (window-buffer window)
(if mini-map-mode
(let* ((end (window-end window t)))
(if (buffer-live-p mini-map-buffer)
(with-current-buffer mini-map-buffer
(move-overlay mini-map-window-overlay start end)))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment