-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy-std-lib.el
274 lines (248 loc) · 9.16 KB
/
my-std-lib.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
;;; my-std-lib.el --- different usefull functions. -*- lexical-binding: t; indent-tabs-mode: t; -*-
(if (locate-library "cl-lib")
(progn
(require 'cl-lib)
(unless (fboundp 'lexical-let)
(defmacro lexical-let (bindings &rest body)
(unless lexical-binding
(my/-warning "You are trying to use lexical let with lexical-binding = nil."))
`(let ,bindings ,@body)))
;; (when (autoloadp (symbol-function 'cl-labels))
;; (autoload-do-load (symbol-function 'cl-labels)))
;; (lexical-let ((old-labels (symbol-function 'cl-labels)))
;; (defmacro cl-labels (bindings &rest body)
;; (unless lexical-binding
;; (my/-warning "You are trying to use cl-labels with lexical-binding = nil."))
;; `(,old-labels ,bindings ,@body)))
)
(require 'cl)
(defalias 'cl-letf 'letf)
(defalias 'cl-labels 'labels)
(defalias 'cl-defmacro 'defmacro*)
(defalias 'cl-defun 'defun*)
(defalias 'cl-do 'do))
;;; Code:
(defvar my/-load-in-progress nil)
(defvar my/-load-file-name nil)
(lexical-let ((original-load (symbol-function 'load)))
(let ((original-doc (documentation 'load t)))
(defun load (file &optional noerror nomessage nosuffix must-suffix)
(setq my/-load-in-progress t
my/-load-file-name file)
(prog1
(funcall original-load file noerror nomessage nosuffix must-suffix)
(setq my/-load-in-progress nil
my/-load-file-name nil)))
(put 'load 'function-documentation original-doc)))
;; old and potentially buggy
;; (defmacro alambda (arglist &rest body)
;; "Anaphoric lambda."
;; (declare (indent defun))
;; `(cl-labels ((self ,arglist ,@body))
;; #'self))
;; new and potentially better than old
(when (version< emacs-version "24.4")
(unless (fboundp 'self)
(fset 'self nil)))
(defmacro alambda (args &rest body)
"Anaphoric lambda."
(declare (indent defun))
`(lexical-let ((self))
(setq self #'(lambda ,args
(cl-letf (((symbol-function 'self) self))
,@body)))
self))
(defmacro add-hook-that-fire-once (hook arglist &rest body)
"Hook that autoremove itself after first execution"
(declare (indent defun))
`(add-hook ,hook (alambda ,arglist
,@body
(remove-hook ,hook self))))
(defun my/-is-interactive-frame-available ()
(and (not noninteractive)
(not (and (daemonp)
(null (cdr (frame-list)))
(eq (selected-frame) terminal-frame)))))
(defmacro my/--with-captures (captures &rest body)
(declare (indent defun))
(if captures
`(lexical-let (,@(mapcar #'(lambda (c) (list c c)) captures))
,@body)
`(progn ,@body)))
(cl-defmacro my/-exec-after-interactive-frame-available ((&rest captures) &rest body)
(declare (indent defun))
`(if (my/-is-interactive-frame-available)
(progn ,@body)
(my/--with-captures ,captures
(add-hook-that-fire-once 'after-make-frame-functions (frame)
(with-selected-frame frame
,@body)))))
(cl-defmacro my/-exec-after-delay ((&rest captures) delay &rest body)
(declare (indent defun))
`(my/--with-captures ,captures
(run-at-time ,delay nil #'(lambda () ,@body))))
(cl-defmacro my/-exec-after-all-parts-of-config-loaded ((&rest captures) &rest body)
(declare (indent defun))
`(my/--with-captures ,captures
(add-hook-that-fire-once 'my/-config-loaded-hook ()
,@body)))
;; with-eval-after-load for Emacs < 24.4
(unless (fboundp 'with-eval-after-load)
(defmacro with-eval-after-load (file &rest body)
(declare (indent 1) (debug t))
`(eval-after-load ,file '(progn ,@body))))
;; defvar-local for Emacs < 24.2
(unless (fboundp 'defvar-local)
(defmacro defvar-local (var val &optional docstring)
"Define VAR as a buffer-local variable with default value VAL.
Like `defvar' but additionally marks the variable as being automatically
buffer-local wherever it is set."
(declare (debug defvar) (doc-string 3))
`(progn
(defvar ,var ,val ,docstring)
(make-variable-buffer-local ',var))))
(unless (fboundp 'setq-local)
(defmacro setq-local (var val)
(declare (indent defun))
"Set variable VAR to value VAL in current buffer."
`(set (make-local-variable ',var) ,val)))
(when (version< emacs-version "24.4")
(unless (fboundp 'incf)
(defmacro incf (n)
`(setq ,n (1+ ,n))))
(lexical-let ((original-backtrace-frame (symbol-function 'backtrace-frame)))
(defun backtrace-frame (nframes &optional base)
(let ((i (if base
(let ((k 8) found bt)
(while (and (not found)
(setq bt (cadr (funcall original-backtrace-frame
(incf k)))))
;; (message "%s:%s" k (backtrace-frame k))
(when (eq bt base) (setq found t)))
(when found (+ nframes (- k 3))))
(+ nframes 6))))
(when i
(funcall original-backtrace-frame i))))))
(defun my/-error-message (error-class msg &optional fatal face tracedepth)
(unless tracedepth (setq tracedepth 8))
(let* ((ecs (concat "[" error-class "]"))
(errbuf (get-buffer-create "*my/-errors*"))
call-stack
(btf (let ((i 2) ci bf)
(while (setq ci (backtrace-frame i 'my/-error-message))
(when (eq (car ci) t)
(push (cdr ci) call-stack))
(when (< i (+ 2 tracedepth))
(push (cdr ci) bf))
(setq i (1+ i)))
bf))
(errbrief (concat
(format "%s: %s" (propertize ecs 'face (or face 'error)) msg)
(and load-in-progress
(format "\n\tIn %s." (or load-file-name (buffer-file-name))))
(and my/-load-in-progress
(format "\n\tIn %s." my/-load-file-name))))
(errstr (concat
errbrief
;;(format "%s: %s" (propertize ecs 'face (or face 'error)) msg)
(and call-stack
(format "\n Call stack:\n\t\t%s"
(mapconcat #'(lambda (elt) (format "%s" elt))
call-stack "\n\t\t")))
(and btf
(format "\n Backtrace:\n\t\t%s"
(mapconcat #'(lambda (elt) (format "%s" elt))
btf "\n\t\t"))))))
(message "%s" errbrief)
(with-current-buffer errbuf
(unless (eq major-mode 'special-mode)
(special-mode))
(read-only-mode -1)
(goto-char (point-min))
(insert errstr)
(newline)
(read-only-mode 1)
(font-lock-add-keywords nil `((,(concat "^\\[" error-class "\\]") 0 'error t))))
(when (or fatal (not (my/-is-interactive-frame-available)))
(my/-exec-after-interactive-frame-available (errstr errbuf ecs)
(my/-exec-after-delay () 2
(message "%s" errstr)
(pop-to-buffer errbuf)
(search-backward ecs))))))
(defun my/-warning (msg)
(my/-error-message "WARNING" msg nil 'compilation-warning 0))
(defun my/-init-error-warning (msg)
(my/-error-message "INIT_ERROR" msg nil nil 5))
(defun my/-init-error-fatal (msg)
(my/-error-message "INIT_ERROR" msg t))
(defun my/-load-directory (dir &optional noelc)
(when (file-directory-p dir)
(let ((files (directory-files dir t (if noelc
"[0-9a-zA-Z].*\\.el$"
"[0-9a-zA-Z].*\\.elc?$"))))
(unless noelc
(cl-do ((cns files (cdr cns)))
((null cns))
(unless (null (cdr cns))
(let ((fst (car cns))
(scd (cadr cns)))
(when (and (string-suffix-p ".elc" scd t)
(string-prefix-p fst scd t))
(setf (car cns) scd
(cdr cns) (cddr cns)))))))
(dolist (file files)
(load file)))))
(with-eval-after-load "package"
(defun require-package (package &optional min-version no-refresh)
"Install given PACKAGE, optionally requiring MIN-VERSION.
If NO-REFRESH is non-nil, the available package lists will not be
re-downloaded in order to locate PACKAGE."
(if (package-installed-p package min-version)
t
(if (or (assoc package package-archive-contents) no-refresh)
(condition-case-unless-debug err (package-install package)
(error (my/-warning (format "Package: %s, Error: %s." package err))))
(progn
(package-refresh-contents)
(require-package package min-version t)))))
(defun my/-install-favourite-packages ()
(interactive)
(when (boundp 'my/-favourite-packages-list)
(dolist (pkg my/-favourite-packages-list)
(require-package pkg))))
(defun my/-install-elpa-package (&optional package)
(interactive)
(let (package-archives)
(add-to-list 'package-archives
'("GNU/ELPA" . "http://elpa.gnu.org/packages/"))
(package-refresh-contents)
(if package
(package-install package)
(call-interactively #'package-install)))))
(if (fboundp 'save-mark-and-excursion)
(defalias 'my/-save-mark-and-excursion 'save-mark-and-excursion)
(defalias 'my/-save-mark-and-excursion 'save-excursion))
(unless (version< emacs-version "24.1")
(defun reverse-input-method (input-method)
"Build the reverse mapping of single letters from INPUT-METHOD."
(interactive
(list (read-input-method-name "Use input method (default current): ")))
(if (and input-method (symbolp input-method))
(setq input-method (symbol-name input-method)))
(let ((current current-input-method)
(modifiers '(nil (control) (meta) (control meta))))
(when input-method
(activate-input-method input-method))
(when (and current-input-method quail-keyboard-layout)
(dolist (map (cdr (quail-map)))
(let* ((to (car map))
(from (quail-get-translation
(cadr map) (char-to-string to) 1)))
(when (and (characterp from) (characterp to))
(dolist (mod modifiers)
(define-key local-function-key-map
(vector (append mod (list from)))
(vector (append mod (list to)))))))))
(when input-method
(activate-input-method current)))))
;; my-std-lib.el ends here