0

I use this function to reopen a file killed by mistake.

(defun undo-kill-buffer ()
    (interactive)
    (let ((active-files (loop for buf in (buffer-list)
                              when (buffer-file-name buf) collect it)))
      (loop for file in recentf-list
            unless (member file active-files) return (find-file file))))
  (global-set-key (kbd "C-x K") 'undo-kill-buffer)

The problem is that I would like to reopen the file at the right place (save-place-mode) only in this case. If I open a file with another function (find-file, find-alternate-file, find-file-at-point, ...) I don't want to remember the last position.

I have not found a solution to activate save-place-mode in some cases only. Is it possible to write a find-file-with-saveplace function without modify find-file ?

Thanks for any help.

djangoliv
  • 3,169
  • 16
  • 31

1 Answers1

1

Take a look at the source for save-place--setup-hooks, since you want a subset of this behavior. I haven't tested this out, but I believe you want:

  • (add-hook 'kill-buffer-hook #'save-place-to-alist) so that the place is saved when a buffer is killed.

  • Modify your undo command to call save-place-find-file-hook after it calls find-file, with the restored buffer as the current buffer.

Update: The save-place-to-alist function checks whether save-place is set, so you probably need your own function here too:

(defun always-save-place-to-alist ()
  (let ((save-place t))
    (save-place-to-alist)))

(add-hook 'kill-buffer-hook #'always-save-place-to-alist)

And the hook that restores the position also enables save-place, so you probably want this too:

(defun restore-save-place ()
  (save-place-find-file-hook)
  (setq save-place nil))
glucas
  • 20,175
  • 1
  • 51
  • 83
  • Thanks for your reply but it doesn't works. Despite the hook, the place is not recorded after killing buffers. – djangoliv Feb 02 '17 at 12:55