-
Notifications
You must be signed in to change notification settings - Fork 8
/
tv-save-place.el
58 lines (48 loc) · 1.83 KB
/
tv-save-place.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
;;; tv-save-place.el --- Save places. -*- lexical-binding: t -*-
;;
;;; Commentary:
;; A simple replacement of saveplace.el
;;; Code:
(eval-when-compile (require 'cl-lib))
;; Places are saved and restored by psession!
(defvar tv-save-place-cache (make-hash-table :test 'equal))
(defvar tv-save-place-ignore-file-regexps '("\\.git/" "-autoloads.el\\'"))
(defun tv-save-place ()
(let ((file (buffer-file-name))
pos)
(when (and file
(cl-loop for re in tv-save-place-ignore-file-regexps
never (string-match re file)))
(widen)
(setq pos (point))
(unless (<= pos 1)
(puthash file pos tv-save-place-cache)))))
(defun tv-save-place-refresh-cache ()
(cl-loop for k being the hash-keys of tv-save-place-cache
using (hash-values v)
for buf = (get-buffer (file-name-nondirectory k))
when (buffer-live-p buf)
do (with-current-buffer buf
(unless (eq (point) v)
(tv-save-place)))))
(defun tv-save-place-restore-pos ()
(let* ((file (buffer-file-name))
(pos (gethash file tv-save-place-cache)))
(when pos (goto-char pos))))
;;;###autoload
(define-minor-mode tv-save-place-mode
"Save position in files."
:group 'convenience
:global t
(if tv-save-place-mode
(progn
(add-hook 'kill-buffer-hook 'tv-save-place)
(add-hook 'before-revert-hook 'tv-save-place)
(add-hook 'find-file-hook 'tv-save-place-restore-pos 100)
(add-hook 'kill-emacs-hook 'tv-save-place-refresh-cache))
(remove-hook 'kill-buffer-hook 'tv-save-place)
(remove-hook 'before-revert-hook 'tv-save-place)
(remove-hook 'find-file-hook 'tv-save-place-restore-pos)
(remove-hook 'kill-emacs-hook 'tv-save-place-refresh-cache)))
(provide 'tv-save-place.el)
;;; tv-save-place.el ends here