-
Notifications
You must be signed in to change notification settings - Fork 0
/
projectIDE-session.el
180 lines (136 loc) · 6.12 KB
/
projectIDE-session.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
;;; projectIDE-session.el --- projectIDE session file
;;
;; Copyright (C) 2015-2016 Mola-T
;; Author: Mola-T <[email protected]>
;; URL: https://github.com/mola-T/projectIDE
;; Version: 1.0
;; Keywords: project, convenience
;;
;;; License:
;; This file is NOT part of GNU Emacs.
;;
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;
;;; Commentary:
;;
;; This file is a part of projectIDE.
;; It helps saving and restoring project buffer point position
;;
;;; code:
(require 'cl-lib)
(require 'projectIDE-header)
(require 'projectIDE-debug)
(defgroup projectIDE-session nil
"Setting for projectIDE session."
:tag "Session"
:group 'projectIDE)
(defcustom projectIDE-enable-session t
"Remember point location when closing buffer of a point
so that it can be restored."
:tag "Enable session?"
:type 'bool
:group 'projectIDE-session)
(defcustom projectIDE-session-limit 80
"The maximum number of session saved for each project."
:tag "Session limit"
:type 'integer
:group 'projectIDE-session)
(defcustom projectIDE-session-reduce-number 40
"The number of session will be reduce when session limit is reached."
:tag "Session reduce number"
:type 'integer
:group 'projectIDE-session)
(defvar projectIDE-session-timer nil
"Store the timer of `projectIDE-session-timer-function'.
Never attempt to modify it directly.")
(cl-defstruct projectIDE-session
point
word
time)
(defun projectIDE-save-session ()
"Save the location of point when project buffer is closed."
(when (and projectIDE-enable-session
(not (string= (car-safe (projectIDE-get-module-var (projectIDE-get-Btrace-signature) 'projectIDE-session)) "f")))
(let* ((signature (projectIDE-get-Btrace-signature))
(session-hash (and signature
(projectIDE-get-module-persist-memory signature 'projectIDE-session))))
(unless session-hash
(setq session-hash (make-hash-table :test 'equal)))
(when (>= (hash-table-count session-hash) projectIDE-session-limit)
(projectIDE-message 'Info
"Cleaning session cache ..."
t)
(let ((files (hash-table-keys session-hash))
remove-files)
(dolist (file files)
(unless (file-exists-p file)
(remhash file session-hash)))
(setq files (hash-table-keys session-hash))
(sort files (lambda (file1 file2)
(time-less-p
(projectIDE-session-time (gethash file2 session-hash))
(projectIDE-session-time (gethash file1 session-hash)))))
(setq remove-files (cl-subseq files (- projectIDE-session-limit projectIDE-session-reduce-number)))
(dolist (file remove-files)
(remhash file session-hash))))
(puthash
(buffer-file-name)
(make-projectIDE-session :point (point)
:word (word-at-point)
:time (current-time))
session-hash)
(projectIDE-set-module-persist-memory signature 'projectIDE-session session-hash))))
(defun projectIDE-restore-session ()
"Restore the location of point when project buffer is opened."
(when (and projectIDE-enable-session
(not (string= (car-safe (projectIDE-get-module-var (projectIDE-get-Btrace-signature) 'projectIDE-session)) "f")))
(let* ((signature (projectIDE-get-Btrace-signature))
(session-hash (and signature
(projectIDE-get-module-persist-memory signature 'projectIDE-session)))
(session (and session-hash (gethash (buffer-file-name) session-hash))))
(when (projectIDE-session-p session)
(when (<= (projectIDE-session-point session) (point-max))
(goto-char (projectIDE-session-point session)))
(if (equal (word-at-point) (projectIDE-session-word session))
(recenter)
(goto-char (point-min)))))))
(defun projectIDE-keep-opened-buffer-order ()
"Advice after `set-buffer' to maintain opened buffer order.
But this is not add to advice directly. Instead, it is encapsulated
by a timer function and ther timer function is the function that
actually advice after `set-buffer' so that `projectIDE-get-Btrace-signature'
can get a correct signature."
(when (and (projectIDE-get-Btrace-signature) projectIDE-enable-session)
(projectIDE-add-opened-buffer (projectIDE-get-Btrace-signature) (buffer-file-name)))
(setq projectIDE-session-timer nil))
(defun projectIDE-session-timer-function (&rest args)
"Encapsulate `projectIDE-keep-opened-buffer-order' to a timer.
Advice this function after `set-buffer'."
(unless projectIDE-session-timer
(setq projectIDE-session-timer
(run-with-idle-timer 0 nil 'projectIDE-keep-opened-buffer-order))))
(defun projectIDE-session-initialize ()
"Initialize projectIDE session."
(add-hook 'projectIDE-open-project-buffer-hook 'projectIDE-restore-session)
(add-hook 'projectIDE-kill-project-buffer-hook 'projectIDE-save-session)
(advice-add 'set-buffer :after 'projectIDE-session-timer-function))
(defun projectIDE-session-terminate ()
"Terminate projectIDE-session."
(remove-hook 'projectIDE-open-project-buffer-hook 'projectIDE-restore-session)
(remove-hook 'projectIDE-kill-project-buffer-hook 'projectIDE-save-session)
(advice-remove 'set-buffer 'projectIDE-session-timer-function))
(provide 'projectIDE-session)
;;; projectIDE-session.el ends here