forked from mattkeller/mk-project
/
mk-project-sourcemarker.el
182 lines (164 loc) · 9.36 KB
/
mk-project-sourcemarker.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
181
182
;;; mk-project-sourcemarker.el --- Sourcemarker for mk-project.
;; Copyright (C) 2011 Andreas Raster <lazor at affenbande dot org>
;;
;; 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 2, 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., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
(require 'mk-project)
(require 'continue)
(defvar mk-sourcemarker-per-project-db t)
(eval-after-load "mk-project-sourcemarker"
'(progn
(add-to-list 'mk-proj-optional-vars 'sourcemarker-db-path)
(add-to-list 'mk-proj-optional-vars 'sourcemarker-db-symbol)
(add-to-list 'mk-proj-var-before-get-functions
'(sourcemarker-db-path . (lambda (var val &optional proj-name config-alist)
(if mk-sourcemarker-per-project-db
(if val
(expand-file-name val)
(mk-proj-get-cache-path 'sourcemarker-db nil 'copy))
(or (and val (expand-file-name val))
continue-db-path)))))
(add-to-list 'mk-proj-var-before-get-functions
'(sourcemarker-db-symbol . (lambda (var val &optional proj-name config-alist)
(let ((proj-name (or proj-name
(and (boundp 'mk-proj-name)
mk-proj-name))))
(cond (val val)
((and proj-name mk-sourcemarker-per-project-db)
(concat proj-name "-sourcemarker-db"))
(t continue-db-symbol))))))
(remove-hook 'find-file-hook 'continue-restore)
(add-hook 'find-file-hook 'mk-sourcemarker-restore)
(remove-hook 'after-save-hook 'continue-save)
(add-hook 'after-save-hook 'mk-sourcemarker-save)
(add-hook 'kill-buffer-hook 'mk-sourcemarker-save)
(run-with-idle-timer 120 t 'mk-sourcemarker-write-project-db)
(add-hook 'mk-proj-before-files-load-hook (lambda ()
(remove-hook 'find-file-hook 'mk-sourcemarker-restore)
(dolist (proj-name (append (list mk-proj-name) (mk-proj-get-config-val 'friends)))
(dolist (buf (append (mk-proj-file-buffers proj-name)))
(when buf
(with-current-buffer buf
(continue-save)))))
(mk-sourcemarker-load-project-db)
))
(add-hook 'mk-proj-after-load-hook (lambda ()
(mk-sourcemarker-restore-all)
(add-hook 'find-file-hook 'mk-sourcemarker-restore)
(mk-sourcemarker-display-most-recent-buffer)))
(add-hook 'mk-proj-before-files-unload-hook (lambda ()
(mk-sourcemarker-save-all)
(remove-hook 'kill-buffer-hook 'mk-sourcemarker-save)))
(add-hook 'mk-proj-after-unload-hook (lambda ()
(add-hook 'kill-buffer-hook 'mk-sourcemarker-save)))
))
(defmacro mk-sourcemarker-with-project-db (&rest body)
`(if (and mk-sourcemarker-per-project-db
(mk-proj-get-config-val 'sourcemarker-db-path)
(mk-proj-get-config-val 'sourcemarker-db-symbol))
(let ((global-db-symbol continue-db-symbol)
(global-db-path continue-db-path)
(continue-db-path (mk-proj-get-config-val 'sourcemarker-db-path))
(continue-db-symbol (mk-proj-get-config-val 'sourcemarker-db-symbol)))
(condition-case nil
(symbol-value (intern continue-db-symbol))
(error (setf (symbol-value (intern continue-db-symbol))
(make-hash-table :test 'equal))))
,@body)
,@body))
(defun mk-sourcemarker-load-project-db ()
(interactive)
(mk-proj-assert-proj)
(unless (intern-soft (mk-proj-get-config-val 'sourcemarker-db-symbol))
(mk-sourcemarker-with-project-db
(message "loading project db: %s" continue-db-symbol)
(continue-load-db))))
(defun mk-sourcemarker-write-project-db ()
(interactive)
(mk-proj-assert-proj)
(mk-sourcemarker-with-project-db
(continue-write-db)))
(defun mk-sourcemarker-restore ()
(interactive)
(if (or (condition-case nil (mk-proj-assert-proj) (error t))
(not (or (mk-proj-buffer-p (current-buffer))
(mk-proj-friendly-buffer-p (current-buffer)))))
(continue-restore)
(if (and (not (condition-case nil (mk-proj-assert-proj) (error t)))
(not (mk-sourcemarker-with-project-db (gethash (buffer-file-name (current-buffer)) (symbol-value (intern continue-db-symbol)))))
(mk-proj-get-config-val 'parent))
(mk-proj-with-current-project (mk-proj-get-config-val 'parent)
(mk-sourcemarker-restore))
(mk-sourcemarker-with-project-db
(message "restoring sourcemarker in buffer %s (%s)" (buffer-name (current-buffer)) (buffer-file-name (current-buffer)))
(continue-restore)))))
(defun mk-sourcemarker-restore-all ()
(interactive)
(mk-proj-assert-proj)
(dolist (buf (append (mk-proj-file-buffers) (mk-proj-friendly-file-buffers)))
(with-current-buffer buf
(mk-sourcemarker-restore))))
(defun mk-sourcemarker-save (&optional keep-timestamp)
(interactive)
(if (or (condition-case nil (mk-proj-assert-proj) (error t))
(not (or (mk-proj-buffer-p (current-buffer))
(mk-proj-friendly-buffer-p (current-buffer)))))
(continue-save)
(mk-sourcemarker-with-project-db
(continue-save))))
(defun mk-sourcemarker-save-all ()
(interactive)
(let* ((results '())
(sorted-buffers (dolist (buf (append (mk-proj-buffers) (mk-proj-friendly-buffers))
(sort results (lambda (a b) (< (car a) (car b)))))
(when (buffer-file-name buf)
(when (eq buf (current-buffer))
(mk-sourcemarker-save))
(mk-sourcemarker-with-project-db
(let* ((filename (buffer-file-name buf))
(sm (gethash filename (symbol-value (intern-soft continue-db-symbol)) nil))
(timestamp (and sm (read (cdr (assoc :timestamp sm))))))
(if timestamp
(add-to-list 'results `(,timestamp . ,buf))
(add-to-list 'results `(-1 . ,buf)))))))))
(dolist (tuple sorted-buffers)
(with-current-buffer (cdr tuple)
(mk-sourcemarker-save)))
(mk-sourcemarker-write-project-db)))
(defun mk-sourcemarker-display-most-recent-buffer ()
(mk-proj-assert-proj)
(let* ((results '())
(buffer (dolist (buf (append (mk-proj-buffers) (mk-proj-friendly-buffers)) (cdar (sort results (lambda (a b) (> (car a) (car b))))))
(when (buffer-file-name buf)
(mk-sourcemarker-with-project-db
(let* ((filename (buffer-file-name buf))
(sm (if (or (not (or (mk-proj-buffer-p (find-buffer-visiting filename))
(mk-proj-friendly-buffer-p (find-buffer-visiting filename))))
(not (mk-sourcemarker-with-project-db
(gethash filename (symbol-value (intern continue-db-symbol))))))
nil ;;(gethash filename (symbol-value (intern-soft global-db-symbol)) nil)
(gethash filename (symbol-value (intern-soft continue-db-symbol)) nil)))
(timestamp (and sm (read (cdr (assoc :timestamp sm))))))
(when timestamp
(add-to-list 'results
`(,timestamp . ,buf)))))))))
(let ((display-buffer-reuse-frames nil))
(let ((buffer (or buffer (car (mk-proj-file-buffers))))
(window (get-buffer-window buffer 'visible)))
(if window
(raise-frame (window-frame window))
(display-buffer buffer))))
))
(provide 'mk-project-sourcemarker)