PascalVKooten
PascalVKooten

Reputation: 21481

Highlight mark-ring locations in Emacs

Would anyone know how to get the past locations of the mark-ring to highlight when pressing C-u C-Space, and to remove whenever something other than C-u C-Space is pressed?

Maybe with only a history of let's say 5 not to have a full screen of highlights.

Upvotes: 3

Views: 678

Answers (3)

Ian Kelling
Ian Kelling

Reputation: 10051

Some elisp to do what you asked https://www.emacswiki.org/emacs/VisibleMark

Also google shows someone added to that code on their own site http://retroj.net/visible-mark I am currently trying that out to highlight the last 3 marks. Added visible-mark.el to my load path. Added this to my init file

(require 'visible-mark)
(defface visible-mark-face1
  '((((type tty) (class mono))
     (:inverse-video t))
    (t (:background "gold4"))) "")
(defface visible-mark-face2
  '((((type tty) (class mono)))
    (t (:background "DarkOrange4"))) "")
(defface visible-mark-face3
  '((((type tty) (class mono)))
    (t (:background "red4"))) "")
(setq visible-mark-faces (quote (visible-mark-face1 visible-mark-face2 visible-mark-face3)))

; highlight the last 3 marks
(setq visible-mark-max 3)
; globally activate visible-mark-mode
(global-visible-mark-mode)

Upvotes: 1

user4815162342
user4815162342

Reputation: 155630

To highlight the locations in the mark ring:

(defun highlight-ring-marks ()
  (let ((markstr (char-to-string 8595)))
    (put-text-property 0 1 'face '((:background "yellow") (:foreground "black"))
                       markstr)
    (mapc (lambda (m)
            (let ((ovl (make-overlay m m)))
              (overlay-put ovl 'after-string markstr)
              (overlay-put ovl 'mark-ring-pointer t)))
          mark-ring)))

To dehighlight them:

(defun dehighlight-ring-marks ()
  (mapc (lambda (ovl)
          (and (overlay-get ovl 'mark-ring-pointer)
               (delete-overlay ovl)))
        (overlays-in (point-min) (point-max))))

With those in place, all that remains is to connect the highlight code to C-u C-SPC and add the dehighlight code to pre-command-hook, so that the indicators are removed when the user presses a key:

(require 'cl)   ; for lexical-let

(defun set-mark-and-highlight (pos)
  (interactive "P")
  (set-mark-command pos)
  (when pos
    (highlight-ring-marks))
  ;; call dehighlight-ring-marks, but only once
  (lexical-let (hookfn)
    (setq hookfn (lambda ()
                   (dehighlight-ring-marks)
                   (remove-hook 'pre-command-hook hookfn)))
    (add-hook 'pre-command-hook hookfn)))

Finally, bind the new command to C-SPC:

(global-set-key (kbd "C-SPC") 'set-mark-and-highlight)

Upvotes: 2

Thomas
Thomas

Reputation: 17422

I've started writing a minor mode to do that. You can get it at GitHub:

https://github.com/kleiba/visual-mark-ring-mode

However, please be advised that this is a very preliminary 0.1 version - it might not work at all for you.

(P.S.: If anyone is willing to help me out in improving this minor-mode, I'd be grateful for any contribution.)

Upvotes: 4

Related Questions