6

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.

  • how can I "see" (echo, message, trace, etc) what a key combination does, like "C-h k" but for repeated key sequences, like C-SPC C-SPC ? Here is what the manual says of the latter (emphasis mine)

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.

yPhil
  • 8,049
  • 4
  • 57
  • 83
  • 1
    Some people might be offended by your use of the word 'buggered' where careful users of English would have written 'bugged'. Me, I was highly amused at the thought of the hunnish practices of the mark-ring. – High Performance Mark Jul 27 '12 at 14:40
  • Ooh.. Point taken. Never realized the difference, thanks! Gosh, now I think of all the time I used this in corporate emails thinking it was a cute and refreshing expression... Brrr – yPhil Jul 27 '12 at 14:47

4 Answers4

3

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

Nicolas Dudebout
  • 9,172
  • 2
  • 34
  • 43
  • You seem to be right..! So if I were able to "C-h k RET C-SPC C-SPC" is that what emacs would say? How can I be sure? How come emacs does not echo the key combo like it does for, say "save-some-buffers" (if you enter that manually you get a nice "You can run the command `save-some-buffers' with C-x s")? And finally the essence of my question : How can I issue a single "push-mark" (if this is indeed what C-SPC C-SPC does) before whatever C-u C-SPC does? – yPhil Jul 27 '12 at 13:58
  • @PhilippeCM IIRC, C-SPC C-SPC literally just calls the function twice in quick succession, the results of that are from side-effects. That is, I'm fairly certain you could hit `C-SPC`, wait a bit and do it again with the same effect. – jdd Jul 27 '12 at 14:12
  • 2
    What I did is open the help for the function `set-mark-command`. I then looked in the definition of the function to see which part of the function was playing with the mark. As @jeremiahd said, `C-SPC C-SPC` is not a keybinding. You are calling twice the same keybinding. If you look at the definition of the function you will see that what happens. The first time you call it it sets the mark at point. The second time it sees that mark and point coincide and therefore it deactivates it. – Nicolas Dudebout Jul 27 '12 at 17:56
1

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)
yPhil
  • 8,049
  • 4
  • 57
  • 83
  • Now if only I could make it to go forward in the mark-ring... http://stackoverflow.com/questions/3393834/how-to-move-forward-and-backward-in-emacs-mark-ring offers no working solution, BTW – yPhil Jul 27 '12 at 14:36
  • The link quoted does offer a working solution if you read it carefully. – event_jr Jul 28 '12 at 09:49
  • even_jr, this "solution" does not address the *problem* at hand : eval the two advices in the link, then visit a buffer, then move the point to a reference location in the file using only arrow keys. Then use the advices to cycle the mark ring. You will *never* go back to the first location. My point. – yPhil Dec 23 '13 at 13:58
1

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.

event_jr
  • 17,467
  • 4
  • 47
  • 62
0

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.

jdd
  • 4,301
  • 1
  • 27
  • 28