This repository has been archived by the owner on Aug 6, 2021. It is now read-only.
forked from technomancy/emacs-starter-kit
-
Notifications
You must be signed in to change notification settings - Fork 1
/
starter-kit-defuns.el
319 lines (269 loc) · 10.9 KB
/
starter-kit-defuns.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
;;; starter-kit-defuns.el --- Define some custom functions
;;
;; Part of the Emacs Starter Kit
(require 'thingatpt)
(require 'imenu)
;; Network
(defun view-url ()
"Open a new buffer containing the contents of URL."
(interactive)
(let* ((default (thing-at-point-url-at-point))
(url (read-from-minibuffer "URL: " default)))
(switch-to-buffer (url-retrieve-synchronously url))
(rename-buffer url t)
;; TODO: switch to nxml/nxhtml mode
(cond ((search-forward "<?xml" nil t) (xml-mode))
((search-forward "<html" nil t) (html-mode)))))
;; Buffer-related
(defun ido-imenu ()
"Update the imenu index and then use ido to select a symbol to navigate to.
Symbols matching the text at point are put first in the completion list."
(interactive)
(imenu--make-index-alist)
(let ((name-and-pos '())
(symbol-names '()))
(flet ((addsymbols (symbol-list)
(when (listp symbol-list)
(dolist (symbol symbol-list)
(let ((name nil) (position nil))
(cond
((and (listp symbol) (imenu--subalist-p symbol))
(addsymbols symbol))
((listp symbol)
(setq name (car symbol))
(setq position (cdr symbol)))
((stringp symbol)
(setq name symbol)
(setq position (get-text-property 1 'org-imenu-marker symbol))))
(unless (or (null position) (null name))
(add-to-list 'symbol-names name)
(add-to-list 'name-and-pos (cons name position))))))))
(addsymbols imenu--index-alist))
;; If there are matching symbols at point, put them at the beginning of `symbol-names'.
(let ((symbol-at-point (thing-at-point 'symbol)))
(when symbol-at-point
(let* ((regexp (concat (regexp-quote symbol-at-point) "$"))
(matching-symbols (delq nil (mapcar (lambda (symbol)
(if (string-match regexp symbol) symbol))
symbol-names))))
(when matching-symbols
(sort matching-symbols (lambda (a b) (> (length a) (length b))))
(mapc (lambda (symbol) (setq symbol-names (cons symbol (delete symbol symbol-names))))
matching-symbols)))))
(let* ((selected-symbol (ido-completing-read "Symbol? " symbol-names))
(position (cdr (assoc selected-symbol name-and-pos))))
(goto-char position))))
;;; These belong in coding-hook:
;; We have a number of turn-on-* functions since it's advised that lambda
;; functions not go in hooks. Repeatedly evaling an add-to-list with a
;; hook value will repeatedly add it since there's no way to ensure
;; that a lambda doesn't already exist in the list.
(defun local-column-number-mode ()
(make-local-variable 'column-number-mode)
(column-number-mode t))
(defun local-comment-auto-fill ()
(set (make-local-variable 'comment-auto-fill-only-comments) t)
(auto-fill-mode t))
(defun turn-on-hl-line-mode ()
(if window-system (hl-line-mode t)))
(defun turn-on-save-place-mode ()
(setq save-place t))
(defun turn-on-whitespace ()
(whitespace-mode t))
(defun turn-on-paredit ()
(paredit-mode t))
(defun turn-off-tool-bar ()
(tool-bar-mode -1))
(defun add-watchwords ()
(font-lock-add-keywords
nil '(("\\<\\(FIX\\|TODO\\|FIXME\\|HACK\\|REFACTOR\\):"
1 font-lock-warning-face t))))
(add-hook 'coding-hook 'local-column-number-mode)
(add-hook 'coding-hook 'local-comment-auto-fill)
(add-hook 'coding-hook 'turn-on-hl-line-mode)
(add-hook 'coding-hook 'turn-on-save-place-mode)
(add-hook 'coding-hook 'pretty-lambdas)
(add-hook 'coding-hook 'add-watchwords)
(defun run-coding-hook ()
"Enable things that are convenient across all coding buffers."
(run-hooks 'coding-hook))
(defun untabify-buffer ()
(interactive)
(untabify (point-min) (point-max)))
(defun indent-buffer ()
(interactive)
(indent-region (point-min) (point-max)))
(defun cleanup-buffer ()
"Perform a bunch of operations on the whitespace content of a buffer."
(interactive)
(indent-buffer)
(untabify-buffer)
(delete-trailing-whitespace))
(defun recentf-ido-find-file ()
"Find a recent file using ido."
(interactive)
(let ((file (ido-completing-read "Choose recent file: " recentf-list nil t)))
(when file
(find-file file))))
;; Cosmetic
(defun pretty-lambdas ()
(font-lock-add-keywords
nil `(("(?\\(lambda\\>\\)"
(0 (progn (compose-region (match-beginning 1) (match-end 1)
,(make-char 'greek-iso8859-7 107))
nil))))))
;; Other
(defun eval-and-replace ()
"Replace the preceding sexp with its value."
(interactive)
(backward-kill-sexp)
(condition-case nil
(prin1 (eval (read (current-kill 0)))
(current-buffer))
(error (message "Invalid expression")
(insert (current-kill 0)))))
(defun recompile-init ()
"Byte-compile all your dotfiles again."
(interactive)
(byte-recompile-directory dotfiles-dir 0)
;; TODO: remove elpa-to-submit once everything's submitted.
(byte-recompile-directory (concat dotfiles-dir "elpa-to-submit/") 0))
(defun regen-autoloads (&optional force-regen)
"Regenerate the autoload definitions file if necessary and load it."
(interactive "P")
(let ((autoload-dir (concat dotfiles-dir "/elpa-to-submit"))
(generated-autoload-file autoload-file))
(when (or force-regen
(not (file-exists-p autoload-file))
(some (lambda (f) (file-newer-than-file-p f autoload-file))
(directory-files autoload-dir t "\\.el$")))
(message "Updating autoloads...")
(let (emacs-lisp-mode-hook)
(update-directory-autoloads autoload-dir))))
(load autoload-file))
(defun sudo-edit (&optional arg)
(interactive "p")
(if (or arg (not buffer-file-name))
(find-file (concat "/sudo:root@localhost:" (ido-read-file-name "File: ")))
(find-alternate-file (concat "/sudo:root@localhost:" buffer-file-name))))
(defun lorem ()
"Insert a lorem ipsum."
(interactive)
(insert "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do "
"eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim"
"ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut "
"aliquip ex ea commodo consequat. Duis aute irure dolor in "
"reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla "
"pariatur. Excepteur sint occaecat cupidatat non proident, sunt in "
"culpa qui officia deserunt mollit anim id est laborum."))
(defun switch-or-start (function buffer)
"If the buffer is current, bury it, otherwise invoke the function."
(if (equal (buffer-name (current-buffer)) buffer)
(bury-buffer)
(if (get-buffer buffer)
(switch-to-buffer buffer)
(funcall function))))
(defun insert-date ()
"Insert a time-stamp according to locale's date and time format."
(interactive)
(insert (format-time-string "%c" (current-time))))
(defun pairing-bot ()
"If you can't pair program with a human, use this instead."
(interactive)
(message (if (y-or-n-p "Do you have a test for that? ") "Good." "Bad!")))
(defun esk-paredit-nonlisp ()
"Turn on paredit mode for non-lisps."
(set (make-local-variable 'paredit-space-delimiter-chars)
(list ?\"))
(paredit-mode 1))
(defun message-point ()
(interactive)
(message "%s" (point)))
;; -----------------------------------------------------------------------------
;; Frame size manipulation
;; -----------------------------------------------------------------------------
(defun frame-set-fullscreen (&optional f)
"This function is called by toggle-fullscreen if the frame was previously in a 'custom' size.
You can also make it so that this function is called directly when emacs starts by adding the following command to your initialisation file:
;; Set initialise frame size
(add-hook 'window-setup-hook 'frame-set-fullscreen t)
"
(interactive)
(if (not f)
(setq f (selected-frame))
)
;; store the current width and height into the frame's variables
(set-frame-parameter f 'previous-width (frame-width f))
(set-frame-parameter f 'previous-height (frame-height f))
(cond
( (eq system-type 'windows-nt)
;; under Windows
(w32-send-sys-command #xf030) ;; from Windows WM_SYSCOMMAND : SC_MAXIMIZE
)
( t
;; default to internal method
(set-frame-parameter f 'fullscreen 'fullboth)
)
)
;; This is a hack to get a flag that will also work on windows,
;; hopefully it won't be needed for long.
(set-frame-parameter f 'is-fullscreen t)
)
(defun frame-set-fullscreen-off (&optional f)
"This function is called by toggle-fullscreen if the frame was previously in a 'fullscreen' size.
This should restore your frame to its previous size.
Known bug: sometimes only one of the two dimensions gets restored.
"
(interactive)
(if (not f)
(setq f (selected-frame))
)
;; First make sure to be out of the fullscren mode
(cond
( (eq system-type 'windows-nt)
;; under Windows
(w32-send-sys-command #xf120) ;; from Windows WM_SYSCOMMAND: SC_RESTORE
)
( t
;; default to internal method
(set-frame-parameter f 'fullscreen 'nil)
)
)
;; see if some info is stored about previous sizes and restore them.
;; NOTE: this seem to be a little buggy: maybe a synchronisation
;; problem between the wm command to take effect and the execution
;; of the following instruction ???
(when (assq 'previous-width (frame-parameters f))
(set-frame-width f (frame-parameter f 'previous-width)))
(when (assq 'previous-height (frame-parameters f))
(set-frame-height f (frame-parameter f 'previous-height)))
(when (assq 'is-fullscreen (frame-parameters f))
(set-frame-parameter f 'is-fullscreen nil)
)
)
(defun frame-is-set-to-fullscreen (&optional f)
"Check wether the frame is fullscreen.
This function is here to take into account the hacks from frame-set-fullscreen"
(if (not f)
(setq f (selected-frame))
)
(or (frame-parameter f 'is-fullscreen)
(frame-parameter f 'fullscreen))
)
(defun toggle-fullscreen (&optional f)
"Switch between the fullscreen and custom sized frames"
(interactive)
(if (not f)
(setq f (selected-frame))
)
(if (frame-is-set-to-fullscreen f)
(frame-set-fullscreen-off f)
(frame-set-fullscreen f)
)
)
;; A monkeypatch to cause annotate to ignore whitespace
(defun vc-git-annotate-command (file buf &optional rev)
(let ((name (file-relative-name file)))
(vc-git-command buf 0 name "blame" "-w" rev)))
(provide 'starter-kit-defuns)
;;; starter-kit-defuns.el ends here