emacselispconfiguration-fileskey-bindingskeymaps

How to pop local and global marks with a common keybinding in Emacs


Is there any way to combine local and global marks together? what I am trying to do is just to have a common keybinding to pop-global-mark and set-mark-command. Something like; "try to pop the local mark, if there are no remaining ones, then try pop the global mark".

This is needed to move back around the code, and not thinking if i should press C-u C-SPC or C-x C-SPC, depending if the jump between functions was inside or outside the same file.


Solution

  • First of all, you cannot really use set-mark-command in code because it checks this-command and last-command so it will not work as intended.

    However, examining its code, I came up with (untested!):

    (defun pop-local-or-global-mark ()
      "Pop to local mark if it exists or to the global mark if it does not."
      (interactive)
      (if (mark t)
          (pop-to-mark-command)
          (pop-global-mark)))