Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 229 lines (188 sloc) 8.267 kb
5fcb34d @technomancy initial commit
authored
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
6 (require 'imenu)
7
8 ;; Network
5fcb34d @technomancy initial commit
authored
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
23 (defun ido-imenu ()
7408e2b @nonsequitur Make ido-imenu recognize the symbol at point.
nonsequitur authored
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
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
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
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
65 ;;; These belong in coding-hook:
66
a4a57d5 @technomancy Add a whitespace-mode function.
authored
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
72 (defun local-column-number-mode ()
5fcb34d @technomancy initial commit
authored
73 (make-local-variable 'column-number-mode)
b2ffdf1 @technomancy Make coding-hook a real hook instead of a defun.
authored
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 ()
81 (if window-system (hl-line-mode t)))
82
83 (defun turn-on-save-place-mode ()
84 (setq save-place t))
85
a4a57d5 @technomancy Add a whitespace-mode function.
authored
86 (defun turn-on-whitespace ()
87 (whitespace-mode t))
88
bc907d2 @technomancy Add org bindings; fix clojure-project root heuristic.
authored
89 (defun turn-on-paredit ()
90 (paredit-mode t))
91
9e78e16 @technomancy Turn off toolbar even when in daemon-mode.
authored
92 (defun turn-off-tool-bar ()
93 (tool-bar-mode -1))
94
b2ffdf1 @technomancy Make coding-hook a real hook instead of a defun.
authored
95 (add-hook 'coding-hook 'local-column-number-mode)
96 (add-hook 'coding-hook 'local-comment-auto-fill)
97 (add-hook 'coding-hook 'turn-on-hl-line-mode)
98 (add-hook 'coding-hook 'pretty-lambdas)
99
100 (defun run-coding-hook ()
101 "Enable things that are convenient across all coding buffers."
f5a0b03 @technomancy Fix typo in running coding hook.
authored
102 (run-hooks 'coding-hook))
5fcb34d @technomancy initial commit
authored
103
104 (defun untabify-buffer ()
105 (interactive)
106 (untabify (point-min) (point-max)))
107
108 (defun indent-buffer ()
109 (interactive)
110 (indent-region (point-min) (point-max)))
111
1f6f58b @technomancy Extracted cleanup buffer function.
authored
112 (defun cleanup-buffer ()
113 "Perform a bunch of operations on the whitespace content of a buffer."
114 (interactive)
115 (indent-buffer)
116 (untabify-buffer)
117 (delete-trailing-whitespace))
118
5fcb34d @technomancy initial commit
authored
119 (defun recentf-ido-find-file ()
120 "Find a recent file using ido."
121 (interactive)
122 (let ((file (ido-completing-read "Choose recent file: " recentf-list nil t)))
123 (when file
124 (find-file file))))
125
126 ;; Cosmetic
127
128 (defun pretty-lambdas ()
129 (font-lock-add-keywords
fa60db0 @technomancy Revert "Use rx for regex construction for great justice."
authored
130 nil `(("(?\\(lambda\\>\\)"
5fcb34d @technomancy initial commit
authored
131 (0 (progn (compose-region (match-beginning 1) (match-end 1)
132 ,(make-char 'greek-iso8859-7 107))
133 nil))))))
134
135 ;; Other
136
d7bdec6 @technomancy Rename some functions; remove mention of nxhtml until it's in ELPA.
authored
137 (defun eval-and-replace ()
5fcb34d @technomancy initial commit
authored
138 "Replace the preceding sexp with its value."
139 (interactive)
140 (backward-kill-sexp)
141 (condition-case nil
142 (prin1 (eval (read (current-kill 0)))
143 (current-buffer))
144 (error (message "Invalid expression")
145 (insert (current-kill 0)))))
146
d7bdec6 @technomancy Rename some functions; remove mention of nxhtml until it's in ELPA.
authored
147 (defun recompile-init ()
148 "Byte-compile all your dotfiles again."
5fcb34d @technomancy initial commit
authored
149 (interactive)
d7bdec6 @technomancy Rename some functions; remove mention of nxhtml until it's in ELPA.
authored
150 (byte-recompile-directory dotfiles-dir 0)
151 ;; TODO: remove elpa-to-submit once everything's submitted.
e3d337d Fixing mismatched parens in recompile-init
Alex Malinovich authored
152 (byte-recompile-directory (concat dotfiles-dir "elpa-to-submit/") 0))
5fcb34d @technomancy initial commit
authored
153
2304bab @technomancy Prefix arg allows regen-autoloads to force even if "up to date".
authored
154 (defun regen-autoloads (&optional force-regen)
420ef86 @technomancy Add cheat.el and lisppaste.el; clean up init.el.
authored
155 "Regenerate the autoload definitions file if necessary and load it."
6ebc4e8 @technomancy Make regen-autoloads backwards-compatible with 22.
authored
156 (interactive "P")
3967f4b @technomancy Remove ELPA'd ruby libs, clean up regen-autoloads.
authored
157 (let ((autoload-dir (concat dotfiles-dir "/elpa-to-submit"))
158 (generated-autoload-file autoload-file))
6ebc4e8 @technomancy Make regen-autoloads backwards-compatible with 22.
authored
159 (when (or force-regen
2304bab @technomancy Prefix arg allows regen-autoloads to force even if "up to date".
authored
160 (not (file-exists-p autoload-file))
cc5b4ee @technomancy Fix a bug in regen-autoloads.
authored
161 (some (lambda (f) (file-newer-than-file-p f autoload-file))
162 (directory-files autoload-dir t "\\.el$")))
163 (message "Updating autoloads...")
0eb65fe @technomancy Don't let hooks interfere with autoload generation.
authored
164 (let (emacs-lisp-mode-hook)
165 (update-directory-autoloads autoload-dir))))
420ef86 @technomancy Add cheat.el and lisppaste.el; clean up init.el.
authored
166 (load autoload-file))
167
5fcb34d @technomancy initial commit
authored
168 (defun sudo-edit (&optional arg)
169 (interactive "p")
e404594 @technomancy Fix sudo-edit and turn off toolbar always.
authored
170 (if (or arg (not buffer-file-name))
5fcb34d @technomancy initial commit
authored
171 (find-file (concat "/sudo:root@localhost:" (ido-read-file-name "File: ")))
172 (find-alternate-file (concat "/sudo:root@localhost:" buffer-file-name))))
173
174 (defun lorem ()
175 "Insert a lorem ipsum."
176 (interactive)
177 (insert "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do "
178 "eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim"
179 "ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut "
180 "aliquip ex ea commodo consequat. Duis aute irure dolor in "
181 "reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla "
182 "pariatur. Excepteur sint occaecat cupidatat non proident, sunt in "
183 "culpa qui officia deserunt mollit anim id est laborum."))
184
185 (defun switch-or-start (function buffer)
186 "If the buffer is current, bury it, otherwise invoke the function."
187 (if (equal (buffer-name (current-buffer)) buffer)
188 (bury-buffer)
189 (if (get-buffer buffer)
190 (switch-to-buffer buffer)
191 (funcall function))))
192
00d6b28 @mjm Added a space for insert-date fn
mjm authored
193 (defun insert-date ()
e9a6168 @technomancy Bring clojure-mode up-to-date (add imenu), add M-x insert-date.
authored
194 "Insert a time-stamp according to locale's date and time format."
195 (interactive)
196 (insert (format-time-string "%c" (current-time))))
197
6c79eeb @technomancy Added pairing-bot function.
authored
198 (defun pairing-bot ()
199 "If you can't pair program with a human, use this instead."
200 (interactive)
201 (message (if (y-or-n-p "Do you have a test for that? ") "Good." "Bad!")))
202
b418209 @technomancy Nonlisp paredit fixes; clojure-project bugfix.
authored
203 (defun esk-paredit-nonlisp ()
204 "Turn on paredit mode for non-lisps."
9b39bcb @technomancy Some JS fixes for espresso-mode; thanks to dgoodlad.
authored
205 (set (make-local-variable 'paredit-space-delimiter-chars)
206 (list ?\"))
b418209 @technomancy Nonlisp paredit fixes; clojure-project bugfix.
authored
207 (paredit-mode 1))
208
7297c0a @technomancy Add message-point binding.
authored
209 (defun message-point ()
210 (interactive)
211 (message "%s" (point)))
212
92fe7e3 @technomancy Add toggle-fullscreen function. (for X only)
authored
213 (defun toggle-fullscreen ()
214 (interactive)
215 ;; TODO: this only works for X. patches welcome for other OSes.
216 (x-send-client-message nil 0 nil "_NET_WM_STATE" 32
217 '(2 "_NET_WM_STATE_MAXIMIZED_VERT" 0))
218 (x-send-client-message nil 0 nil "_NET_WM_STATE" 32
219 '(2 "_NET_WM_STATE_MAXIMIZED_HORZ" 0)))
220
221
71f9f3d @technomancy Make git annotate ignore whitespace.
authored
222 ;; A monkeypatch to cause annotate to ignore whitespace
223 (defun vc-git-annotate-command (file buf &optional rev)
224 (let ((name (file-relative-name file)))
225 (vc-git-command buf 0 name "blame" "-w" rev)))
226
5fcb34d @technomancy initial commit
authored
227 (provide 'starter-kit-defuns)
e3d337d Fixing mismatched parens in recompile-init
Alex Malinovich authored
228 ;;; starter-kit-defuns.el ends here
Something went wrong with that request. Please try again.