I have followed these instruction to create a stack of visited position across buffers:

(defvar jedi:goto-stack '())
(defun jedi:jump-to-definition ()
  (add-to-list 'jedi:goto-stack
               (list (buffer-name) (point)))
(defun jedi:jump-back ()
  (let ((p (pop jedi:goto-stack)))
    (if p (progn
            (switch-to-buffer (nth 0 p))
            (goto-char (nth 1 p))))))

This worked well in most case, except when you visited files with the same name under different directories.

Example, you have two files ~/a/test.py and ~/b/test.py. When you first visit ~/a/test.py, the emacs buffer name will be test.py. Now, I used jedi:jump-to-definition function above to go to ~/b/test.py.

As it intended to do, before switching to ~/b/test.py, it push current buffer name and position to stack jedi:goto-stack as (test.py (point)).

But emacs saw two files have the same name, they uniquify them, make their buffer name to test.py<a> and test.py<b>. Now, I can't use jedi:jump-back to go to previous buffer ~/a/test.py, because the stack only contains (test.py (point)).

Can I check when did emacs uniquify the buffer name, and push the new uniquify name to my stack instead of the old one?

  • 1
    The question is not clear. Please try to clarify it. Remove everything that is not strictly germane to what you are asking. Try to ask a specific, narrow question. Thx. – Drew Jul 3 '15 at 1:37

Rather than check when the name changes, you want to not use names at all. Instead, use the actual buffer objects. I.e. instead of (buffer-name) use (current-buffer).

While you're at it, you'll sometimes notice that you won't get back to the proper place in the buffer, because the buffer has been modified in the mean time and the saved (point) value now corresponds to "another place". So, instead of saving (point), you should save a marker, e.g. (point-marker). And once you do that, you'll notice you don't need to save (current-buffer) separately since the marker object actually remembers the buffer as well, so you can replace (list (buffer-name) (point)) with (point-marker), and then replace (nth 0 p) with (marker-buffer p) and (nth 1 p) with just p.

| improve this answer | |
  • That's what I expected, +1 for (point-marker). Thanks! – cuonglm Jul 3 '15 at 2:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.