Skip to content
This repository
Newer
Older
100644 270 lines (221 sloc) 9.531 kb
5fcb34d0 »
2008-11-18 initial commit
1 ;;; starter-kit-defuns.el --- Define some custom functions
2 ;;
3 ;; Part of the Emacs Starter Kit
4
5 (require 'thingatpt)
d7bdec66 »
2008-12-10 Rename some functions; remove mention of nxhtml until it's in ELPA.
6 (require 'imenu)
7
8 ;; Network
5fcb34d0 »
2008-11-18 initial commit
9
10 (defun view-url ()
11 "Open a new buffer containing the contents of URL."
12 (interactive)
13 (let* ((default (thing-at-point-url-at-point))
14 (url (read-from-minibuffer "URL: " default)))
15 (switch-to-buffer (url-retrieve-synchronously url))
16 (rename-buffer url t)
17 ;; TODO: switch to nxml/nxhtml mode
18 (cond ((search-forward "<?xml" nil t) (xml-mode))
19 ((search-forward "<html" nil t) (html-mode)))))
20
21 ;; Buffer-related
22
d7bdec66 »
2008-12-10 Rename some functions; remove mention of nxhtml until it's in ELPA.
23 (defun ido-imenu ()
7408e2be »
2009-03-11 Make ido-imenu recognize the symbol at point.
24 "Update the imenu index and then use ido to select a symbol to navigate to.
25 Symbols matching the text at point are put first in the completion list."
5fcb34d0 »
2008-11-18 initial commit
26 (interactive)
27 (imenu--make-index-alist)
28 (let ((name-and-pos '())
29 (symbol-names '()))
30 (flet ((addsymbols (symbol-list)
31 (when (listp symbol-list)
32 (dolist (symbol symbol-list)
33 (let ((name nil) (position nil))
34 (cond
35 ((and (listp symbol) (imenu--subalist-p symbol))
36 (addsymbols symbol))
37
38 ((listp symbol)
39 (setq name (car symbol))
40 (setq position (cdr symbol)))
41
42 ((stringp symbol)
43 (setq name symbol)
44 (setq position (get-text-property 1 'org-imenu-marker symbol))))
45
46 (unless (or (null position) (null name))
47 (add-to-list 'symbol-names name)
48 (add-to-list 'name-and-pos (cons name position))))))))
49 (addsymbols imenu--index-alist))
7408e2be »
2009-03-11 Make ido-imenu recognize the symbol at point.
50 ;; If there are matching symbols at point, put them at the beginning of `symbol-names'.
51 (let ((symbol-at-point (thing-at-point 'symbol)))
52 (when symbol-at-point
53 (let* ((regexp (concat (regexp-quote symbol-at-point) "$"))
54 (matching-symbols (delq nil (mapcar (lambda (symbol)
55 (if (string-match regexp symbol) symbol))
56 symbol-names))))
57 (when matching-symbols
58 (sort matching-symbols (lambda (a b) (> (length a) (length b))))
59 (mapc (lambda (symbol) (setq symbol-names (cons symbol (delete symbol symbol-names))))
60 matching-symbols)))))
5fcb34d0 »
2008-11-18 initial commit
61 (let* ((selected-symbol (ido-completing-read "Symbol? " symbol-names))
62 (position (cdr (assoc selected-symbol name-and-pos))))
63 (goto-char position))))
64
b2ffdf12 »
2009-03-11 Make coding-hook a real hook instead of a defun.
65 ;;; These belong in coding-hook:
66
a4a57d50 »
2009-05-20 Add a whitespace-mode function.
67 ;; We have a number of turn-on-* functions since it's advised that lambda
68 ;; functions not go in hooks. Repeatedly evaling an add-to-list with a
69 ;; hook value will repeatedly add it since there's no way to ensure
70 ;; that a lambda doesn't already exist in the list.
71
b2ffdf12 »
2009-03-11 Make coding-hook a real hook instead of a defun.
72 (defun local-column-number-mode ()
5fcb34d0 »
2008-11-18 initial commit
73 (make-local-variable 'column-number-mode)
b2ffdf12 »
2009-03-11 Make coding-hook a real hook instead of a defun.
74 (column-number-mode t))
75
76 (defun local-comment-auto-fill ()
77 (set (make-local-variable 'comment-auto-fill-only-comments) t)
78 (auto-fill-mode t))
79
80 (defun turn-on-hl-line-mode ()
733c6783 »
2010-11-14 hl-line-mode is useless in 8-color terms.
81 (when (> (display-color-cells) 8) (hl-line-mode t)))
b2ffdf12 »
2009-03-11 Make coding-hook a real hook instead of a defun.
82
83 (defun turn-on-save-place-mode ()
84 (setq save-place t))
85
a4a57d50 »
2009-05-20 Add a whitespace-mode function.
86 (defun turn-on-whitespace ()
87 (whitespace-mode t))
88
bc907d27 »
2009-09-06 Add org bindings; fix clojure-project root heuristic.
89 (defun turn-on-paredit ()
90 (paredit-mode t))
91
9e78e16f »
2009-07-30 Turn off toolbar even when in daemon-mode.
92 (defun turn-off-tool-bar ()
93 (tool-bar-mode -1))
94
90251f33 »
2011-05-06 Switch from idle-highlight to idle-highlight-mode.
95 (defun turn-on-idle-highlight ()
96 (idle-highlight-mode t))
97
c0b7ae5f »
2010-01-06 Add watchwords to coding hook.
98 (defun add-watchwords ()
99 (font-lock-add-keywords
100 nil '(("\\<\\(FIX\\|TODO\\|FIXME\\|HACK\\|REFACTOR\\):"
101 1 font-lock-warning-face t))))
102
b2ffdf12 »
2009-03-11 Make coding-hook a real hook instead of a defun.
103 (add-hook 'coding-hook 'local-column-number-mode)
104 (add-hook 'coding-hook 'local-comment-auto-fill)
105 (add-hook 'coding-hook 'turn-on-hl-line-mode)
c0b7ae5f »
2010-01-06 Add watchwords to coding hook.
106 (add-hook 'coding-hook 'turn-on-save-place-mode)
b2ffdf12 »
2009-03-11 Make coding-hook a real hook instead of a defun.
107 (add-hook 'coding-hook 'pretty-lambdas)
c0b7ae5f »
2010-01-06 Add watchwords to coding hook.
108 (add-hook 'coding-hook 'add-watchwords)
90251f33 »
2011-05-06 Switch from idle-highlight to idle-highlight-mode.
109 (add-hook 'coding-hook 'turn-on-idle-highlight)
b2ffdf12 »
2009-03-11 Make coding-hook a real hook instead of a defun.
110
111 (defun run-coding-hook ()
112 "Enable things that are convenient across all coding buffers."
f5a0b03a »
2009-03-11 Fix typo in running coding hook.
113 (run-hooks 'coding-hook))
5fcb34d0 »
2008-11-18 initial commit
114
115 (defun untabify-buffer ()
116 (interactive)
117 (untabify (point-min) (point-max)))
118
119 (defun indent-buffer ()
120 (interactive)
121 (indent-region (point-min) (point-max)))
122
1f6f58b4 »
2008-12-16 Extracted cleanup buffer function.
123 (defun cleanup-buffer ()
124 "Perform a bunch of operations on the whitespace content of a buffer."
125 (interactive)
126 (indent-buffer)
127 (untabify-buffer)
128 (delete-trailing-whitespace))
129
5fcb34d0 »
2008-11-18 initial commit
130 (defun recentf-ido-find-file ()
131 "Find a recent file using ido."
132 (interactive)
133 (let ((file (ido-completing-read "Choose recent file: " recentf-list nil t)))
134 (when file
135 (find-file file))))
136
137 ;; Cosmetic
138
139 (defun pretty-lambdas ()
140 (font-lock-add-keywords
fa60db0b »
2008-12-10 Revert "Use rx for regex construction for great justice."
141 nil `(("(?\\(lambda\\>\\)"
5fcb34d0 »
2008-11-18 initial commit
142 (0 (progn (compose-region (match-beginning 1) (match-end 1)
143 ,(make-char 'greek-iso8859-7 107))
144 nil))))))
145
146 ;; Other
147
d7bdec66 »
2008-12-10 Rename some functions; remove mention of nxhtml until it's in ELPA.
148 (defun eval-and-replace ()
5fcb34d0 »
2008-11-18 initial commit
149 "Replace the preceding sexp with its value."
150 (interactive)
151 (backward-kill-sexp)
152 (condition-case nil
153 (prin1 (eval (read (current-kill 0)))
154 (current-buffer))
155 (error (message "Invalid expression")
156 (insert (current-kill 0)))))
157
d7bdec66 »
2008-12-10 Rename some functions; remove mention of nxhtml until it's in ELPA.
158 (defun recompile-init ()
159 "Byte-compile all your dotfiles again."
5fcb34d0 »
2008-11-18 initial commit
160 (interactive)
d7bdec66 »
2008-12-10 Rename some functions; remove mention of nxhtml until it's in ELPA.
161 (byte-recompile-directory dotfiles-dir 0)
162 ;; TODO: remove elpa-to-submit once everything's submitted.
e3d337d8 »
2009-12-24 Fixing mismatched parens in recompile-init
163 (byte-recompile-directory (concat dotfiles-dir "elpa-to-submit/") 0))
5fcb34d0 »
2008-11-18 initial commit
164
2304babf »
2008-12-09 Prefix arg allows regen-autoloads to force even if "up to date".
165 (defun regen-autoloads (&optional force-regen)
420ef86d »
2008-12-03 Add cheat.el and lisppaste.el; clean up init.el.
166 "Regenerate the autoload definitions file if necessary and load it."
6ebc4e86 »
2008-12-09 Make regen-autoloads backwards-compatible with 22.
167 (interactive "P")
3967f4b7 »
2008-12-08 Remove ELPA'd ruby libs, clean up regen-autoloads.
168 (let ((autoload-dir (concat dotfiles-dir "/elpa-to-submit"))
169 (generated-autoload-file autoload-file))
6ebc4e86 »
2008-12-09 Make regen-autoloads backwards-compatible with 22.
170 (when (or force-regen
2304babf »
2008-12-09 Prefix arg allows regen-autoloads to force even if "up to date".
171 (not (file-exists-p autoload-file))
cc5b4ee6 »
2008-12-09 Fix a bug in regen-autoloads.
172 (some (lambda (f) (file-newer-than-file-p f autoload-file))
173 (directory-files autoload-dir t "\\.el$")))
174 (message "Updating autoloads...")
0eb65fef »
2009-05-26 Don't let hooks interfere with autoload generation.
175 (let (emacs-lisp-mode-hook)
176 (update-directory-autoloads autoload-dir))))
420ef86d »
2008-12-03 Add cheat.el and lisppaste.el; clean up init.el.
177 (load autoload-file))
178
5fcb34d0 »
2008-11-18 initial commit
179 (defun sudo-edit (&optional arg)
0e2d3afd »
2012-09-07 Bugfix in starter-kit: sudo-edit
180 (interactive "P")
e4045940 »
2009-08-11 Fix sudo-edit and turn off toolbar always.
181 (if (or arg (not buffer-file-name))
5fcb34d0 »
2008-11-18 initial commit
182 (find-file (concat "/sudo:root@localhost:" (ido-read-file-name "File: ")))
183 (find-alternate-file (concat "/sudo:root@localhost:" buffer-file-name))))
184
185 (defun lorem ()
186 "Insert a lorem ipsum."
187 (interactive)
188 (insert "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do "
189 "eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim"
190 "ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut "
191 "aliquip ex ea commodo consequat. Duis aute irure dolor in "
192 "reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla "
193 "pariatur. Excepteur sint occaecat cupidatat non proident, sunt in "
194 "culpa qui officia deserunt mollit anim id est laborum."))
195
196 (defun switch-or-start (function buffer)
197 "If the buffer is current, bury it, otherwise invoke the function."
198 (if (equal (buffer-name (current-buffer)) buffer)
199 (bury-buffer)
200 (if (get-buffer buffer)
201 (switch-to-buffer buffer)
202 (funcall function))))
203
00d6b281 »
2009-01-22 Added a space for insert-date fn
204 (defun insert-date ()
e9a61681 »
2009-01-21 Bring clojure-mode up-to-date (add imenu), add M-x insert-date.
205 "Insert a time-stamp according to locale's date and time format."
206 (interactive)
207 (insert (format-time-string "%c" (current-time))))
208
6c79eeb7 »
2008-12-15 Added pairing-bot function.
209 (defun pairing-bot ()
210 "If you can't pair program with a human, use this instead."
211 (interactive)
212 (message (if (y-or-n-p "Do you have a test for that? ") "Good." "Bad!")))
213
b418209c »
2009-07-20 Nonlisp paredit fixes; clojure-project bugfix.
214 (defun esk-paredit-nonlisp ()
215 "Turn on paredit mode for non-lisps."
9b39bcbd »
2009-09-08 Some JS fixes for espresso-mode; thanks to dgoodlad.
216 (set (make-local-variable 'paredit-space-delimiter-chars)
217 (list ?\"))
b418209c »
2009-07-20 Nonlisp paredit fixes; clojure-project bugfix.
218 (paredit-mode 1))
219
aecb8a9b »
2010-11-14 Paredit 22 final... Still not in elpa. =\
220 (defun esk-space-for-delimiter? (endp delimiter)
b7bce7be »
2011-08-01 add js-mode to esk-space-for-delimiter?
221 (not (member major-mode '(ruby-mode espresso-mode js-mode js2-mode))))
aecb8a9b »
2010-11-14 Paredit 22 final... Still not in elpa. =\
222
223 (eval-after-load 'paredit
224 '(add-to-list 'paredit-space-for-delimiter-predicates
225 'esk-space-for-delimiter?))
226
7297c0a6 »
2009-10-09 Add message-point binding.
227 (defun message-point ()
228 (interactive)
229 (message "%s" (point)))
230
6d57d6b9 »
2010-11-30 Shouldn't have to M-x ucs-insert to do ಠ_ಠ.
231 (defun esk-disapproval ()
232 (interactive)
233 (insert "_"))
234
d800723f »
2010-12-03 Add fix-agent function, courtesy of Steve Gilardi.
235 (defun esk-agent-path ()
236 (if (eq system-type 'darwin)
237 "*launch*/Listeners"
238 "*ssh*/agent\.*"))
239
240 (defun esk-find-agent ()
241 (let* ((path-clause (format "-path \"%s\"" (esk-agent-path)))
242 (find-command (format "$(find -L /tmp -uid $UID %s -type s 2> /dev/null)"
243 path-clause)))
244 (first (split-string
245 (shell-command-to-string
246 (format "/bin/ls -t1 %s | head -1" find-command))))))
247
248 (defun fix-agent ()
249 (interactive)
250 (let ((agent (esk-find-agent)))
251 (setenv "SSH_AUTH_SOCK" agent)
252 (message agent)))
253
92fe7e3a »
2009-09-06 Add toggle-fullscreen function. (for X only)
254 (defun toggle-fullscreen ()
255 (interactive)
256 ;; TODO: this only works for X. patches welcome for other OSes.
257 (x-send-client-message nil 0 nil "_NET_WM_STATE" 32
258 '(2 "_NET_WM_STATE_MAXIMIZED_VERT" 0))
259 (x-send-client-message nil 0 nil "_NET_WM_STATE" 32
260 '(2 "_NET_WM_STATE_MAXIMIZED_HORZ" 0)))
261
262
71f9f3d1 »
2009-02-19 Make git annotate ignore whitespace.
263 ;; A monkeypatch to cause annotate to ignore whitespace
264 (defun vc-git-annotate-command (file buf &optional rev)
265 (let ((name (file-relative-name file)))
266 (vc-git-command buf 0 name "blame" "-w" rev)))
267
5fcb34d0 »
2008-11-18 initial commit
268 (provide 'starter-kit-defuns)
e3d337d8 »
2009-12-24 Fixing mismatched parens in recompile-init
269 ;;; starter-kit-defuns.el ends here
Something went wrong with that request. Please try again.