yPhil
yPhil

Reputation: 8357

emacs: How to use the mark-ring?

When I do a C-u C-SPC, emacs takes me to "where I was before". Subsequent C-u C-SPC presses go back up previous places. That is damn great, and I use it a lot.

But something always bugged me : The only mark missing from the mark-ring is where-I-invoked-this-in-the-1st-place! It's like leaving bread crumbs behind you, and then go "whoops, I may be lost, imma go back and check", then going back without leaving a bread crumb where you are right now!

I tried advising the function, but I can't for the life of me programmatically simulate a C-SPC C-SPC.

C-SPC runs the command set-mark-command, which is an interactive compiled Lisp function in `simple.el'.

It is bound to C-@, C-SPC.

(set-mark-command ARG)

Set the mark where point is, or jump to the mark. Setting the mark also alters the region, which is the text between point and mark; this is the closest equivalent in Emacs to what some editors call the "selection".

With no prefix argument, set the mark at point, and push the old mark position on local mark ring. Also push the old mark on global mark ring, if the previous mark was set in another buffer.

But when I try to use it (non-interactively) "With no prefix argument" in order to "set the mark at point" I get a debugger error "wrong-number-of-arguments"..? (I realize the difference between an argument and a prefix argument).

I would be okay even with a philosophical, non-practical answer. I just want to understand what the idea is here.

Upvotes: 6

Views: 1942

Answers (4)

event_jr
event_jr

Reputation: 17707

Ok, I bundled up two relevant answers from this question into a small package here.

It allows you to use C-- C-SPC to move forward in mark-ring.

Upvotes: 1

yPhil
yPhil

Reputation: 8357

OK, here is what I did

(defun px-push-mark-once-and-back ()
    "Mark current point (`push-mark') and `set-mark-command' (C-u C-SPC) away."
    (interactive)
    (let ((current-prefix-arg '(4))) ; C-u
        (if (not (eq last-command 'px-push-mark-once-and-back))
                (progn
                    (push-mark)
                    (call-interactively 'set-mark-command))
            (call-interactively 'set-mark-command))))

(global-set-key (kbd "<s-left>") 'px-push-mark-once-and-back)

Upvotes: 1

jdd
jdd

Reputation: 4336

You probably shouldn't be using set-mark or set-mark-command non-interactively. Within elisp, just save point (or whatever location) in a variable with a good name.

Also, C-h i m emacs m mark.

I'm no emacs guru wrt mark movement, I barely use it. I know I've seen the behavior you want before though.

Upvotes: 0

Nicolas Dudebout
Nicolas Dudebout

Reputation: 9262

(push-mark) seems to be doing what you want.

Upvotes: 3

Related Questions