-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathphantom-inline-comment.el
More file actions
413 lines (348 loc) · 13.9 KB
/
phantom-inline-comment.el
File metadata and controls
413 lines (348 loc) · 13.9 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
;;; phantom-inline-comment --- Visible but not affect the code
;; Copyright (C) 2019- blue0513
;; 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 of the License, 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 this program; if not, write to the Free Software
;; Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
;; Author: blue0513
;; URL: https://github.com/blue0513/phantom-inline-comment
;; Version: 0.1.0
;;; Commentary:
;; Edit your init.el
;;
;; (require 'phantom-inline-comment)
;;
;;; Code:
(require 'popwin)
(defvar phantom-inline-comment-minor-mode-map
(let ((kmap (make-sparse-keymap)))
(define-key kmap (kbd "C-c C-c") 'phantom-inline-comment--apply-buffer)
(define-key kmap (kbd "C-g") 'phantom-inline-comment--cancel)
kmap))
(defvar phantom-inline-comment-show-mode-map
(let ((kmap (make-sparse-keymap)))
(define-key kmap (kbd "RET") 'phantom-goto-comment)
kmap))
(defvar phantom-inline-comment-edit-buffer "*phantom-inline-comment-edit*")
(defvar phantom-inline-comment-show-buffer "*phantom-inline-comments*")
(defvar phantom-saved-file
(locate-user-emacs-file "phantom-inline-comments" ".phantom-inline-comments"))
(defvar phantom-inline-comment-state nil) ;; 'add or 'edit
(defvar phantom-inline-comment-visibility 'show) ;; 'hide or 'show
(defvar inline--phantom-comments nil)
(defvar phantom-inline-comment-target-buffer nil)
(defface phantom-inline-commnet-face '((t (:inherit highlight))) nil)
(defface phantom-inline-commnet-list-face '((t (:foreground "purple"))) nil)
(define-minor-mode phantom-inline-comment-minor-mode
:init-value nil
:global nil
:keymap phantom-inline-comment-minor-mode-map
:lighter " Phantom")
(define-minor-mode phantom-inline-comment-auto-restore-mode
"Minor mode for phantom-inline-comment-auto-restore"
:init-value nil
:global nil
:lighter " PicAR"
(if phantom-inline-comment-auto-restore-mode
(add-hook 'find-file-hook 'phantom-inline-comment-restore-data-if-necessary)
(remove-hook 'find-file-hook #'phantom-inline-comment-restore-data-if-necessary)))
(define-minor-mode phantom-inline-comment-auto-save-mode
"Global minor mode for phantom-inline-comment-auto-save"
:init-value nil
:global t
:lighter " PicAS"
(if phantom-inline-comment-auto-save-mode
(add-hook 'kill-emacs-hook 'phantom-inline-comment-save-data)
(remove-hook 'kill-emacs-hook #'phantom-inline-comment-save-data)))
(defun phantom-show-mode nil
"Major mode for `phantom-show-mode' buffers."
(interactive)
(kill-all-local-variables)
(setq major-mode 'phantom-show-mode)
(setq mode-name "phantom-comments")
(use-local-map phantom-inline-comment-show-mode-map))
(defun pic--find-overlays-specifying ()
"Find overlays below the cursor."
(let ((overlays (overlays-at (point-at-eol)))
found)
(while overlays
(let ((overlay (car overlays)))
(if (overlay-get overlay 'phantom)
(setq found (cons overlay found))))
(setq overlays (cdr overlays)))
found))
(defun pic--get-overlays-after-string ()
"Get text property from the overlays below the cursor."
(let* ((ovs (pic--find-overlays-specifying))
(ov (car ovs)))
(if (overlayp ov)
(overlay-get ov 'after-string)
"")))
(defun pic--exist-overlays ()
(let* ((ovs (pic--find-overlays-specifying))
(ov (car ovs)))
(if (overlayp ov)
ov)))
(defun pic--dump-comment-data (data)
"Save DATA into annotation file."
(with-temp-file phantom-saved-file
(if (not (null data))
(prin1 data (current-buffer))
(erase-buffer))))
(defun pic--read-overlay-data (ov)
"Read OV and generate list containing phantom-inlin-comment's info."
(let* ((filename (buffer-file-name (overlay-buffer ov)))
(start (overlay-start ov))
(end (overlay-end ov))
(comment (substring-no-properties (overlay-get ov 'after-string))))
(list filename start end comment)))
(defun pic--convert-overlay-data (overlays)
"Return human readable string list from OVERLAYS."
(let* ((result nil))
(dolist (overlay overlays)
(push (pic--read-overlay-data overlay) result))
result))
(defun pic--build-file-info-list ()
(let* ((header "FileName:LineNum/Comment")
(phantoms inline--phantom-comments)
(raw-file-info-list
(mapconcat
#'(lambda (phantom)
(with-current-buffer (overlay-buffer phantom)
(let* ((line (s-trim-left (buffer-substring (overlay-start phantom)
(overlay-end phantom))))
(line-num (count-lines (point-min) (overlay-start phantom)))
(comment (overlay-get phantom 'after-string))
(string
(concat (format "%s:%d %s" (buffer-name) line-num comment))))
(put-text-property 0 (length string) 'phantom-buffer (buffer-name) string)
(put-text-property 0 (length string) 'phantom-comment line-num string)
string)))
phantoms
" "))
(file-info-list (split-string raw-file-info-list)))
(list header file-info-list)))
(defun pic--create-overlay-from (filename start end comment)
"Generate overlay from FILENAME, START, END, COMMENT."
(let* ((buffer (get-file-buffer filename))
(ov (make-overlay start end buffer))
(propertized-str (propertize comment 'face 'phantom-inline-commnet-face)))
(overlay-put ov 'phantom t)
(overlay-put ov 'after-string propertized-str)
ov))
(defun pic--load-data-as-string ()
"Return strings in `phantom-saved-file'."
(with-temp-buffer
(when (file-exists-p phantom-saved-file)
(insert-file-contents phantom-saved-file))
(buffer-string)))
(defun pic-load-all-data ()
(let* ((str (pic--load-data-as-string))
(all-data (if (not (string-equal "" str))
(car (read-from-string str))
nil)))
all-data))
(defun pic--check-should-restore-data ()
"Check if current-file is target to be restored."
(let* ((filenames (pic--filenames-from-saved-data)))
(if (buffer-file-name)
(member (buffer-file-name) filenames))))
(defun phantom-inline-comment-restore-data-if-necessary ()
"Restore `phantom-inline-comment' if needed."
(when (pic--check-should-restore-data)
(phantom-inline-comment-restore-data)
(message "Phantom Inline Comment Restored !")
(sit-for 0.5)))
(defun generate-inline-phantom-comment (msg)
"Generate overlay below the cursor with MSG."
(pcase-let* ((pos-eol (point-at-eol))
(buffer phantom-inline-comment-target-buffer)
(ov (make-overlay pos-eol (1+ pos-eol) buffer))
(str (concat (when (eq pos-eol (point-max)) "\n")
msg "\n"))
(propertized-str (propertize str 'face 'phantom-inline-commnet-face)))
(overlay-put ov 'phantom t)
(overlay-put ov 'after-string propertized-str)
ov))
(defun pic--toggle-text-visibility (phantom bool)
"Modify visibility of PHANTOM comment depends on BOOL."
(if (overlayp phantom)
(let* ((str (overlay-get phantom 'after-string)))
(put-text-property 0 (max 0 (- (length str) 1)) 'invisible bool str))))
(defun phantom-inline-comment--clean-up-comments ()
(let* ((result nil))
(dolist (phantom-comment inline--phantom-comments)
(if (overlay-start phantom-comment)
(push phantom-comment result)))
(setq inline--phantom-comments result)))
(defun pic--filenames-from-saved-data ()
(let* ((result nil))
(dolist (data (pic-load-all-data))
(let* ((filename (nth 0 data)))
(push filename result)))
result))
(defun phantom-inline-comment--hide (phantom)
"Fold PHANTOM."
(pic--toggle-text-visibility phantom t))
(defun phantom-inline-comment--open (phantom)
"Unfold PHANTOM."
(pic--toggle-text-visibility phantom nil))
(defun phantom-inline-comment--delete (phantom)
"Delete overlay with PHANTOM property."
(when (overlay-get phantom 'phantom)
(delete-overlay phantom))
(phantom-inline-comment--clean-up-comments))
(defun phantom-inline-comment--add (msg)
"Push overlay with MSG to the stack."
(push (generate-inline-phantom-comment msg) inline--phantom-comments))
(defun phantom-inline-comment--delete-all ()
"Delete all the overlays in the stack."
(mapc #'phantom-inline-comment--delete inline--phantom-comments)
(setq inline--phantom-comments nil)
(message "All the phantom inline comments are deleted!"))
(defun phantom-inline-comment--display-edit-buffer ()
"Open popup-window to edit a phantom inline comment."
(interactive)
(popwin:popup-buffer
(generate-new-buffer phantom-inline-comment-edit-buffer))
(phantom-inline-comment-minor-mode 1))
(defun phantom-inline-comment--delete-below ()
"Delete overlay after Find it."
(interactive)
(let* ((ovs (pic--find-overlays-specifying))
(ov (car ovs)))
(if ov
(phantom-inline-comment--delete ov)
(message "No phantom inline comment is found"))))
(defun phantom-inline-comment--apply-buffer ()
"Apply change in edit-buffer to the master buffer."
(interactive)
(let* ((str (buffer-string)))
(popwin:close-popup-window)
(pop-to-buffer phantom-inline-comment-target-buffer)
(cond ((eq phantom-inline-comment-state 'add)
(phantom-inline-comment--add str))
((eq phantom-inline-comment-state 'edit)
(phantom-inline-comment--add str)
(phantom-inline-comment--delete-below)))))
(defun phantom-inline-comment--toggle-all ()
"Toggle visibility of phantom comments."
(interactive)
(cond ((eq phantom-inline-comment-visibility 'show)
(phantom-inline-comment--hide-all))
((eq phantom-inline-comment-visibility 'hide)
(phantom-inline-comment--open-all))))
(defun phantom-inline-comment--edit-below ()
"Edit text on overlay and Open edit-buffer."
(let* ((prev-comment (pic--get-overlays-after-string))
(raw-prev-comment (substring-no-properties prev-comment)))
(phantom-inline-comment--display-edit-buffer)
(insert raw-prev-comment)))
(defun phantom-goto-comment ()
(interactive)
(let ((buffer-name (get-text-property (point) 'phantom-buffer))
(line (get-text-property (point) 'phantom-comment)))
(if (null buffer-name)
(message "No phantom comment at this line.")
(pop-to-buffer (get-buffer buffer-name))
(goto-line line))))
(defun phantom-inline-comment--show (header lines)
(if (= (length lines) 0)
(message "No phantom inline comments found.")
(popwin:popup-buffer
(generate-new-buffer phantom-inline-comment-show-buffer))
(phantom-show-mode)
(dolist (line lines)
(let* ((line-info
(propertize
line 'font-lock-face 'phantom-inline-commnet-list-face)))
(insert (concat line-info "\n"))))
(setq header-line-format
(concat (propertize " " 'display '((space :align-to 0))) header))
(setq buffer-read-only t)))
(defun phantom-inline-comment--show-all ()
"Show all phantom overlays."
(apply #'phantom-inline-comment--show
(pic--build-file-info-list)))
(defun phantom-inline-comment--hide-all ()
"Fold all the phantoms."
(let* ((overlays inline--phantom-comments))
(mapc #'phantom-inline-comment--hide overlays)
(setq phantom-inline-comment-visibility 'hide)
(message "All the phantom inline comments are hidden!")))
(defun phantom-inline-comment--open-all ()
"Unfold all the phantoms."
(let* ((overlays inline--phantom-comments))
(mapc #'phantom-inline-comment--open overlays)
(setq phantom-inline-comment-visibility 'show)
(message "All the phantom inline comments are appeared!")))
(defun phantom-inline-comment--cancel ()
"Close popup-window."
(interactive)
(popwin:close-popup-window))
(defun phantom-inline-comment-add ()
"Add phantom inline comment below line of the cursor."
(setq phantom-inline-comment-state 'add)
(setq phantom-inline-comment-target-buffer (current-buffer))
(phantom-inline-comment--display-edit-buffer))
(defun phantom-inline-comment-edit-below ()
"Edit phantom inline comment below line of the cursor."
(setq phantom-inline-comment-state 'edit)
(setq phantom-inline-comment-target-buffer (current-buffer))
(phantom-inline-comment--edit-below))
(defun phantom-inline-comment--save-data ()
"Save phantom-inline-comments from `inline--phantom-comments'."
(pic--dump-comment-data (pic--convert-overlay-data inline--phantom-comments)))
(defun phantom-inline-comment--restore-data ()
"Push stored pahntom-inline-comments into `inline--phantom-comments'."
(dolist (data (pic-load-all-data))
(let* ((filename (nth 0 data))
(start (nth 1 data))
(end (nth 2 data))
(comment (nth 3 data)))
(if (string-equal filename (buffer-file-name))
(push (pic--create-overlay-from filename start end comment) inline--phantom-comments)
))))
;;; Main Functions
(defun phantom-inline-comment ()
"Add or Edit phantom inline comment below line of the cursor."
(interactive)
(if (pic--exist-overlays)
(phantom-inline-comment-edit-below)
(phantom-inline-comment-add)))
(defun phantom-inline-comment-delete()
"Delete phantom inline comment below line of the cursor."
(interactive)
(phantom-inline-comment--delete-below))
(defun phantom-inline-comment-delete-all ()
"Delete all the phantom inline comments."
(interactive)
(phantom-inline-comment--delete-all))
(defun phantom-inline-comment-show-all ()
"Show all the phantom inline comments."
(interactive)
(phantom-inline-comment--show-all))
(defun phantom-inline-comment-toggle-all ()
"Fold/Unfold all the phantom inline comments."
(interactive)
(phantom-inline-comment--toggle-all))
(defun phantom-inline-comment-save-data ()
"Save phantom-inline-comments from `phantom-save-file'."
(interactive)
(phantom-inline-comment--save-data))
(defun phantom-inline-comment-restore-data ()
"Restore phantom-inline-comments from `phantom-save-file'."
(interactive)
(phantom-inline-comment--restore-data))
;; * provide
(provide 'phantom-inline-comment)
;;; phantom-inline-comment.el ends here