Skip to content

Instantly share code, notes, and snippets.

@jgrahamc
Created March 11, 2013 21:29
Show Gist options
  • Save jgrahamc/5137943 to your computer and use it in GitHub Desktop.
Save jgrahamc/5137943 to your computer and use it in GitHub Desktop.
A system to save the location (line number) of every file edited with Emacs and let the user jump there with C-l
(setq last-location-file (concat my-emacs-d "/last-location"))
(defun load-last-location-file ()
(if (file-exists-p last-location-file)
(read (with-temp-buffer
(insert-file-contents last-location-file)
(buffer-string)))
(make-hash-table :test 'equal)))
(setq last-location-table (load-last-location-file))
(defun save-last-location ()
(puthash (buffer-file-name) (1+ (count-lines 1 (point))) last-location-table))
(add-hook 'kill-buffer-hook 'save-last-location)
(add-hook 'after-save-hook 'save-last-location)
(defun save-all-locations ()
(progn
(save-current-buffer
(mapc '(lambda (buffer)
(set-buffer buffer)
(save-last-location))
(buffer-list)))
(with-temp-file last-location-file (insert (prin1-to-string last-location-table)))))
(add-hook 'kill-emacs-hook 'save-all-locations)
(defun get-last-location (f)
(gethash f last-location-table 1))
(defun jump-to-last-location ()
(interactive)
(goto-char (point-min)) (forward-line (1- (get-last-location (buffer-file-name)))))
(global-set-key (kbd "C-l") 'jump-to-last-location)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment