Skip to content
Newer
Older
100644 267 lines (219 sloc) 9.23 KB
5fcb34d @technomancy initial commit
authored Nov 18, 2008
1 ;;; starter-kit-defuns.el --- Define some custom functions
2 ;;
3 ;; Part of the Emacs Starter Kit
4
5 (require 'thingatpt)
d7bdec6 @technomancy Rename some functions; remove mention of nxhtml until it's in ELPA.
authored Dec 10, 2008
6 (require 'imenu)
7
8 ;; Network
5fcb34d @technomancy initial commit
authored Nov 18, 2008
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
d7bdec6 @technomancy Rename some functions; remove mention of nxhtml until it's in ELPA.
authored Dec 10, 2008
23 (defun ido-imenu ()
7408e2b @nonsequitur Make ido-imenu recognize the symbol at point.
nonsequitur authored Mar 11, 2009
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."
5fcb34d @technomancy initial commit
authored Nov 18, 2008
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))
7408e2b @nonsequitur Make ido-imenu recognize the symbol at point.
nonsequitur authored Mar 11, 2009
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)))))
5fcb34d @technomancy initial commit
authored Nov 18, 2008
61 (let* ((selected-symbol (ido-completing-read "Symbol? " symbol-names))
62 (position (cdr (assoc selected-symbol name-and-pos))))
63 (goto-char position))))
64
b2ffdf1 @technomancy Make coding-hook a real hook instead of a defun.
authored Mar 11, 2009
65 ;;; These belong in coding-hook:
66
a4a57d5 @technomancy Add a whitespace-mode function.
authored May 20, 2009
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
b2ffdf1 @technomancy Make coding-hook a real hook instead of a defun.
authored Mar 11, 2009
72 (defun local-column-number-mode ()
5fcb34d @technomancy initial commit
authored Nov 18, 2008
73 (make-local-variable 'column-number-mode)
b2ffdf1 @technomancy Make coding-hook a real hook instead of a defun.
authored Mar 11, 2009
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 ()
733c678 @technomancy hl-line-mode is useless in 8-color terms.
authored Nov 14, 2010
81 (when (> (display-color-cells) 8) (hl-line-mode t)))
b2ffdf1 @technomancy Make coding-hook a real hook instead of a defun.
authored Mar 11, 2009
82
83 (defun turn-on-save-place-mode ()
84 (setq save-place t))
85
a4a57d5 @technomancy Add a whitespace-mode function.
authored May 20, 2009
86 (defun turn-on-whitespace ()
87 (whitespace-mode t))
88
bc907d2 @technomancy Add org bindings; fix clojure-project root heuristic.
authored Sep 6, 2009
89 (defun turn-on-paredit ()
90 (paredit-mode t))
91
9e78e16 @technomancy Turn off toolbar even when in daemon-mode.
authored Jul 30, 2009
92 (defun turn-off-tool-bar ()
93 (tool-bar-mode -1))
94
c0b7ae5 @technomancy Add watchwords to coding hook.
authored Jan 6, 2010
95 (defun add-watchwords ()
96 (font-lock-add-keywords
97 nil '(("\\<\\(FIX\\|TODO\\|FIXME\\|HACK\\|REFACTOR\\):"
98 1 font-lock-warning-face t))))
99
b2ffdf1 @technomancy Make coding-hook a real hook instead of a defun.
authored Mar 11, 2009
100 (add-hook 'coding-hook 'local-column-number-mode)
101 (add-hook 'coding-hook 'local-comment-auto-fill)
102 (add-hook 'coding-hook 'turn-on-hl-line-mode)
c0b7ae5 @technomancy Add watchwords to coding hook.
authored Jan 7, 2010
103 (add-hook 'coding-hook 'turn-on-save-place-mode)
b2ffdf1 @technomancy Make coding-hook a real hook instead of a defun.
authored Mar 11, 2009
104 (add-hook 'coding-hook 'pretty-lambdas)
c0b7ae5 @technomancy Add watchwords to coding hook.
authored Jan 7, 2010
105 (add-hook 'coding-hook 'add-watchwords)
1a6e4d1 @technomancy Move idle-highlight activation to coding-hook.
authored May 6, 2010
106 (add-hook 'coding-hook 'idle-highlight)
b2ffdf1 @technomancy Make coding-hook a real hook instead of a defun.
authored Mar 11, 2009
107
108 (defun run-coding-hook ()
109 "Enable things that are convenient across all coding buffers."
f5a0b03 @technomancy Fix typo in running coding hook.
authored Mar 11, 2009
110 (run-hooks 'coding-hook))
5fcb34d @technomancy initial commit
authored Nov 18, 2008
111
112 (defun untabify-buffer ()
113 (interactive)
114 (untabify (point-min) (point-max)))
115
116 (defun indent-buffer ()
117 (interactive)
118 (indent-region (point-min) (point-max)))
119
1f6f58b @technomancy Extracted cleanup buffer function.
authored Dec 16, 2008
120 (defun cleanup-buffer ()
121 "Perform a bunch of operations on the whitespace content of a buffer."
122 (interactive)
123 (indent-buffer)
124 (untabify-buffer)
125 (delete-trailing-whitespace))
126
5fcb34d @technomancy initial commit
authored Nov 18, 2008
127 (defun recentf-ido-find-file ()
128 "Find a recent file using ido."
129 (interactive)
130 (let ((file (ido-completing-read "Choose recent file: " recentf-list nil t)))
131 (when file
132 (find-file file))))
133
134 ;; Cosmetic
135
136 (defun pretty-lambdas ()
137 (font-lock-add-keywords
fa60db0 @technomancy Revert "Use rx for regex construction for great justice."
authored Dec 10, 2008
138 nil `(("(?\\(lambda\\>\\)"
5fcb34d @technomancy initial commit
authored Nov 18, 2008
139 (0 (progn (compose-region (match-beginning 1) (match-end 1)
140 ,(make-char 'greek-iso8859-7 107))
141 nil))))))
142
143 ;; Other
144
d7bdec6 @technomancy Rename some functions; remove mention of nxhtml until it's in ELPA.
authored Dec 10, 2008
145 (defun eval-and-replace ()
5fcb34d @technomancy initial commit
authored Nov 18, 2008
146 "Replace the preceding sexp with its value."
147 (interactive)
148 (backward-kill-sexp)
149 (condition-case nil
150 (prin1 (eval (read (current-kill 0)))
151 (current-buffer))
152 (error (message "Invalid expression")
153 (insert (current-kill 0)))))
154
d7bdec6 @technomancy Rename some functions; remove mention of nxhtml until it's in ELPA.
authored Dec 10, 2008
155 (defun recompile-init ()
156 "Byte-compile all your dotfiles again."
5fcb34d @technomancy initial commit
authored Nov 18, 2008
157 (interactive)
d7bdec6 @technomancy Rename some functions; remove mention of nxhtml until it's in ELPA.
authored Dec 10, 2008
158 (byte-recompile-directory dotfiles-dir 0)
159 ;; TODO: remove elpa-to-submit once everything's submitted.
e3d337d Fixing mismatched parens in recompile-init
Alex Malinovich authored Dec 24, 2009
160 (byte-recompile-directory (concat dotfiles-dir "elpa-to-submit/") 0))
5fcb34d @technomancy initial commit
authored Nov 18, 2008
161
2304bab @technomancy Prefix arg allows regen-autoloads to force even if "up to date".
authored Dec 9, 2008
162 (defun regen-autoloads (&optional force-regen)
420ef86 @technomancy Add cheat.el and lisppaste.el; clean up init.el.
authored Dec 3, 2008
163 "Regenerate the autoload definitions file if necessary and load it."
6ebc4e8 @technomancy Make regen-autoloads backwards-compatible with 22.
authored Dec 9, 2008
164 (interactive "P")
3967f4b @technomancy Remove ELPA'd ruby libs, clean up regen-autoloads.
authored Dec 8, 2008
165 (let ((autoload-dir (concat dotfiles-dir "/elpa-to-submit"))
166 (generated-autoload-file autoload-file))
6ebc4e8 @technomancy Make regen-autoloads backwards-compatible with 22.
authored Dec 9, 2008
167 (when (or force-regen
2304bab @technomancy Prefix arg allows regen-autoloads to force even if "up to date".
authored Dec 9, 2008
168 (not (file-exists-p autoload-file))
cc5b4ee @technomancy Fix a bug in regen-autoloads.
authored Dec 9, 2008
169 (some (lambda (f) (file-newer-than-file-p f autoload-file))
170 (directory-files autoload-dir t "\\.el$")))
171 (message "Updating autoloads...")
0eb65fe @technomancy Don't let hooks interfere with autoload generation.
authored May 26, 2009
172 (let (emacs-lisp-mode-hook)
173 (update-directory-autoloads autoload-dir))))
420ef86 @technomancy Add cheat.el and lisppaste.el; clean up init.el.
authored Dec 3, 2008
174 (load autoload-file))
175
5fcb34d @technomancy initial commit
authored Nov 18, 2008
176 (defun sudo-edit (&optional arg)
177 (interactive "p")
e404594 @technomancy Fix sudo-edit and turn off toolbar always.
authored Aug 11, 2009
178 (if (or arg (not buffer-file-name))
5fcb34d @technomancy initial commit
authored Nov 18, 2008
179 (find-file (concat "/sudo:root@localhost:" (ido-read-file-name "File: ")))
180 (find-alternate-file (concat "/sudo:root@localhost:" buffer-file-name))))
181
182 (defun lorem ()
183 "Insert a lorem ipsum."
184 (interactive)
185 (insert "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do "
186 "eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim"
187 "ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut "
188 "aliquip ex ea commodo consequat. Duis aute irure dolor in "
189 "reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla "
190 "pariatur. Excepteur sint occaecat cupidatat non proident, sunt in "
191 "culpa qui officia deserunt mollit anim id est laborum."))
192
193 (defun switch-or-start (function buffer)
194 "If the buffer is current, bury it, otherwise invoke the function."
195 (if (equal (buffer-name (current-buffer)) buffer)
196 (bury-buffer)
197 (if (get-buffer buffer)
198 (switch-to-buffer buffer)
199 (funcall function))))
200
00d6b28 @mjm Added a space for insert-date fn
mjm authored Jan 22, 2009
201 (defun insert-date ()
e9a6168 @technomancy Bring clojure-mode up-to-date (add imenu), add M-x insert-date.
authored Jan 21, 2009
202 "Insert a time-stamp according to locale's date and time format."
203 (interactive)
204 (insert (format-time-string "%c" (current-time))))
205
6c79eeb @technomancy Added pairing-bot function.
authored Dec 15, 2008
206 (defun pairing-bot ()
207 "If you can't pair program with a human, use this instead."
208 (interactive)
209 (message (if (y-or-n-p "Do you have a test for that? ") "Good." "Bad!")))
210
b418209 @technomancy Nonlisp paredit fixes; clojure-project bugfix.
authored Jul 20, 2009
211 (defun esk-paredit-nonlisp ()
212 "Turn on paredit mode for non-lisps."
9b39bcb @technomancy Some JS fixes for espresso-mode; thanks to dgoodlad.
authored Sep 8, 2009
213 (set (make-local-variable 'paredit-space-delimiter-chars)
214 (list ?\"))
b418209 @technomancy Nonlisp paredit fixes; clojure-project bugfix.
authored Jul 20, 2009
215 (paredit-mode 1))
216
aecb8a9 @technomancy Paredit 22 final... Still not in elpa. =\
authored Nov 14, 2010
217 (defun esk-space-for-delimiter? (endp delimiter)
218 (not (member major-mode '(ruby-mode espresso-mode js2-mode))))
219
220 (eval-after-load 'paredit
221 '(add-to-list 'paredit-space-for-delimiter-predicates
222 'esk-space-for-delimiter?))
223
7297c0a @technomancy Add message-point binding.
authored Oct 9, 2009
224 (defun message-point ()
225 (interactive)
226 (message "%s" (point)))
227
6d57d6b @technomancy Shouldn't have to M-x ucs-insert to do ಠ_ಠ.
authored Nov 30, 2010
228 (defun esk-disapproval ()
229 (interactive)
230 (insert "ಠ_ಠ"))
231
d800723 @technomancy Add fix-agent function, courtesy of Steve Gilardi.
authored Dec 3, 2010
232 (defun esk-agent-path ()
233 (if (eq system-type 'darwin)
234 "*launch*/Listeners"
235 "*ssh*/agent\.*"))
236
237 (defun esk-find-agent ()
238 (let* ((path-clause (format "-path \"%s\"" (esk-agent-path)))
239 (find-command (format "$(find -L /tmp -uid $UID %s -type s 2> /dev/null)"
240 path-clause)))
241 (first (split-string
242 (shell-command-to-string
243 (format "/bin/ls -t1 %s | head -1" find-command))))))
244
245 (defun fix-agent ()
246 (interactive)
247 (let ((agent (esk-find-agent)))
248 (setenv "SSH_AUTH_SOCK" agent)
249 (message agent)))
250
92fe7e3 @technomancy Add toggle-fullscreen function. (for X only)
authored Sep 6, 2009
251 (defun toggle-fullscreen ()
252 (interactive)
253 ;; TODO: this only works for X. patches welcome for other OSes.
254 (x-send-client-message nil 0 nil "_NET_WM_STATE" 32
255 '(2 "_NET_WM_STATE_MAXIMIZED_VERT" 0))
256 (x-send-client-message nil 0 nil "_NET_WM_STATE" 32
257 '(2 "_NET_WM_STATE_MAXIMIZED_HORZ" 0)))
258
259
71f9f3d @technomancy Make git annotate ignore whitespace.
authored Feb 19, 2009
260 ;; A monkeypatch to cause annotate to ignore whitespace
261 (defun vc-git-annotate-command (file buf &optional rev)
262 (let ((name (file-relative-name file)))
263 (vc-git-command buf 0 name "blame" "-w" rev)))
264
5fcb34d @technomancy initial commit
authored Nov 18, 2008
265 (provide 'starter-kit-defuns)
e3d337d Fixing mismatched parens in recompile-init
Alex Malinovich authored Dec 24, 2009
266 ;;; starter-kit-defuns.el ends here
Something went wrong with that request. Please try again.