-
Notifications
You must be signed in to change notification settings - Fork 2
/
cme-utils.el
357 lines (315 loc) · 11.2 KB
/
cme-utils.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
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
;;; cme-utils.el --- CME utilities
;; Copyright (C) 2021 Consciencia
;; Author: Consciencia <[email protected]>
;; Version: 1.0.0
;; Keywords: c c++ cme cedet
;; 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 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, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; :(
;;; Code:
(defun cme-map-create ()
(make-hash-table :test 'equal))
(defun cme-map-get (key map)
(gethash key map))
(defun cme-map-set (key val map)
(puthash key val map))
(defun cme-map-rem (key map)
(remhash key map))
(defun cme-map-everything (fun list)
(cond
((listp list)
(loop for x in list collect (funcall fun x)))
((hash-table-p list)
(loop for key being the hash-keys of list using (hash-values val)
collect (funcall fun key val)))
(t (error "Bad input to cme-map-everything"))))
(defun cme-map-to-alist (map)
(if (hash-table-p map)
(let ((acc nil))
(maphash (lambda (k v)
(setq acc (cons (cons k v) acc)))
map)
acc)
nil))
(defun cme-at (list idx)
(cond
((listp list) (nth idx list))
((hash-table-p list) (gethash idx list))
(t (error "Bad input to cme-at"))))
(defun cme-delete-dups-eq (list)
(let ((hash (make-hash-table :test #'eq :size (length list)))
(tail list) retail)
(puthash (car list) t hash)
(while (setq retail (cdr tail))
(let ((elt (car retail)))
(if (gethash elt hash)
(setcdr tail (cdr retail))
(puthash elt t hash)
(setq tail retail)))))
list)
(defun cme-list-init (list &optional unsafe)
(if unsafe
(nreverse (nthcdr 1 (nreverse list)))
(nreverse (nthcdr 1 (reverse list)))))
(defun cme-append-new-backbone (&rest lists)
(let ((result nil))
(dolist (list lists)
(dolist (element list)
(push element result)))
result))
(defun cme-longest-string (strings)
(let ((longest (car strings))
(head (cdr strings)))
(while head
(when (> (length (car head))
(length longest))
(setq longest (car head)))
(setq head (cdr head)))
longest))
;; Function s-replace-all not works correctly for all inputs
;; in all versions of emacs. This is workaround for that.
;; This function also handle correctly empty replacement list.
(defun cme-replace-all (replacements str)
(loop for (from . to) in replacements
do (setq str (s-replace-regexp from to str)))
str)
(defun cme-find-str-occurences (string)
(save-excursion
(let ((len (length string))
(result nil))
(goto-char (point-min))
(while (search-forward string nil 0)
(push (cons (- (point) len) (point)) result))
result)))
(defun cme-val-in-range (val range)
(when (semantic-tag-p range)
(setq range
(cons (semantic-tag-start range)
(semantic-tag-end range))))
(and (>= val (car range))
(<= val (cdr range))))
(defmacro cme-chain-forms (&rest forms)
(declare (indent 99))
(apply #'cme-chain-forms-helper (nreverse forms)))
(defun cme-chain-forms-helper (&rest forms)
(if forms
(append (car forms)
(if (cdr forms)
(list (apply #'cme-chain-forms-helper
(cdr forms)))))))
(defun cme-get-buffer (name)
(loop for buff in (buffer-list)
for buffname = (buffer-name buff)
if (string= buffname name)
return (cons buffname buff)))
(defun cme-pos-is-in-comment (&optional pos)
(interactive)
(if (not pos)
(setq pos (point)))
(let ((fontfaces (get-text-property pos 'face))
(comment-faces '(font-lock-comment-face
font-lock-comment-delimiter-face))
(result nil))
(when (not (listp fontfaces))
(setf fontfaces (list fontfaces)))
(setq result
(if font-lock-mode
(loop for font-face in fontfaces
if (memq font-face comment-faces)
return t)
(nth 4 (syntax-ppss pos))))
(if (called-interactively-p 'any)
(message "Comment state: %s" result)
result)))
(defun cme-pos-is-in-string (&optional pos)
(if (not pos)
(setq pos (point)))
(if font-lock-mode
(eq (get-text-property pos 'face)
'font-lock-string-face)
(nth 3 (syntax-ppss pos))))
(defun cme-extract-comments-from-region (start stop)
(when font-lock-mode
;; There is possibility that target region is in buffer with font lock
;; enabled but with no font locking done yet. In such case, we must
;; explicitly fontify that region where we search for comments.
;; Of course there is backup for buffers without font locking, but
;; this backup is enabled only when font lock mode is disabled so
;; its not usable in this situation.
(font-lock-fontify-region start stop))
(let ((result "")
(len (- stop start))
(finger start)
(was-in nil)
(is-in nil))
(dotimes (finger len result)
(setq is-in (cme-pos-is-in-comment (+ start finger)))
(if is-in
(setq result (concat result
(string
(char-after (+ start
finger))))))
(if (and was-in (not is-in))
(setq result (concat result
(if (equal (string
(aref result
(1- (length result)))) "\n")
"\n"
"\n\n"))))
(setq was-in is-in))))
(defun cme-check-schema (obj schema)
(let ((result t))
(loop for entry in schema
for field = (format "%s" (car entry))
for val = (cme-map-get field obj)
for predicates = (if (listp (cdr entry))
(cdr entry)
`(,(cdr entry)))
for predicates-output = (loop for p in predicates
collect (funcall p val))
do (progn
(if (not (eval `(or ,@predicates-output)))
(setq result nil))))
(when (not result)
(message "Schema check failed!"))
result))
(setq *cme-read-json-file-cache* (cme-map-create))
(setq *cme-read-json-ts-cache* (cme-map-create))
(defun cme-read-json-cached (path &optional schema)
(let ((json-object-type 'hash-table)
(json-array-type 'list)
(json-key-type 'string)
ts
result)
(if (file-exists-p path)
(progn
(setq ts (file-attribute-modification-time
(file-attributes path)))
(if (equal ts
(cme-map-get path
*cme-read-json-ts-cache*))
(setq result
(cme-map-get path
*cme-read-json-file-cache*))
(progn
(setq result (json-read-file path))
(cme-map-set path
result
*cme-read-json-file-cache*)
(cme-map-set path
ts
*cme-read-json-ts-cache*)))
(if (and result
schema
(not (cme-check-schema result schema)))
(setq result nil)))
(progn
(cme-map-rem path *cme-read-json-file-cache*)
(cme-map-rem path *cme-read-json-ts-cache*)))
(when (null result)
(message "Failed to read JSON from %s!"
path))
result))
(defun cme-mark-string ()
(interactive)
(ignore-errors
(when (eq (get-text-property (point) 'face)
'font-lock-string-face)
(let ((p (point)))
(while (eq (get-text-property (point) 'face)
'font-lock-string-face)
(forward-char 1))
(skip-chars-backward " \n\t\r")
(set-mark (point))
(goto-char p)
(while (eq (get-text-property (point) 'face)
'font-lock-string-face)
(forward-char -1))
(forward-char 1)
(skip-chars-forward " \n\t\r"))))
(setq transient-mark-mode (cons 'only transient-mark-mode)))
(defun cme-mark-tag ()
(interactive)
(if (eq (get-text-property (point) 'face)
'font-lock-string-face)
(cme-mark-string)
(if (semantic-current-tag)
(let* ((tag (semantic-current-tag))
(bounds (semantic-tag-bounds tag))
(b (car bounds))
(e (cadr bounds)))
(goto-char e)
(set-mark (point))
(goto-char b))))
(setq transient-mark-mode (cons 'only transient-mark-mode)))
(defmacro cme-with-simple-pop-up (name &rest content)
(declare (indent 1))
(let ((buffsym (gensym)))
`(let ((,buffsym (cme-get-buffer ,name))
(kill-on-quit nil))
(if ,buffsym
(setq ,buffsym (cdr ,buffsym))
(setq ,buffsym (generate-new-buffer ,name)))
(with-current-buffer ,buffsym
(read-only-mode -1)
(erase-buffer)
,@content
(read-only-mode t))
(switch-to-buffer-other-window ,name)
(shrink-window-if-larger-than-buffer)
(goto-char (point-min))
(if kill-on-quit
(local-set-key "q"
(lambda ()
(interactive)
(delete-window)
(kill-buffer ,name)))
(local-set-key "q" 'delete-window)))))
(defmacro cme-with-measure-time (&rest body)
(declare (indent 99))
`(let ((time (current-time)))
(cons (progn
,@body)
(float-time (time-since time)))))
(defun cme-push-mark ()
(interactive)
(xref-push-marker-stack))
(defun cme-pop-mark ()
(interactive)
(deactivate-mark t)
(let* ((buff (current-buffer))
(win (selected-window))
(start (window-start win))
(end (window-end win)))
(xref-pop-marker-stack)
(if (not (and (eq buff (current-buffer))
(>= (point) start)
(<= (point) end)))
(recenter))
(pulse-momentary-highlight-one-line (point))))
(defun cme-inspect-eieio (obj)
(require 'eieio-datadebug)
(data-debug-new-buffer "*Inspector*")
(data-debug-insert-object-slots obj ">>"))
(defmacro cme-silence-eldoc-for (func)
`(advice-add #',func
:around
(lambda (oldfun &rest args)
(let ((eldoc-message-function
#'(lambda (&rest args) nil)))
(apply oldfun args)))))
(defmacro cme-silence-eldoc-for-funcs (&rest funcs)
(cons 'progn
(loop for func in funcs
collect `(cme-silence-eldoc-for ,func))))
(provide 'cme-utils)
;;; cme-utils.el ends here