updates
[emacs.git] / .emacs.d / elisp / local / ganneff.el
1 ;;; ganneff1.el --- Some functions and stuff I use
2
3 ;;; Copyright (C) 2012.2013 Joerg Jaspert
4
5 ;; Filename: ganneff.de
6 ;; Author: Joerg Jaspert <joerg@debian.org>
7
8 ;;; Commentary:
9 ;; This is just stuff I use in my emacs configuration.
10
11 ;;; Code:
12
13 ;;;###autoload
14 (defun ido-disable-line-trucation () (set (make-local-variable 'truncate-lines) nil))
15
16 ; match-paren will either jump to the "other" paren or simply insert %
17 ; #+BEGIN_SRC emacs-lisp tangle:yes
18 ;;;###autoload
19 (defun match-paren (arg)
20 "Go to the matching parenthesis if on parenthesis otherwise insert %."
21 (interactive "p")
22 (cond ((looking-at "\\s\(") (forward-list 1) (backward-char 1))
23 ((looking-at "\\s\)") (forward-char 1) (backward-list 1))
24 (t (self-insert-command (or arg 1)))))
25
26 ;;;###autoload
27 (defun sacha/isearch-yank-current-word ()
28 "Pull current word from buffer into search string."
29 (interactive)
30 (save-excursion
31 (skip-syntax-backward "w_")
32 (isearch-yank-internal
33 (lambda ()
34 (skip-syntax-forward "w_")
35 (point)))))
36
37 ;;;###autoload
38 (defun sacha/search-word-backward ()
39 "Find the previous occurrence of the current word."
40 (interactive)
41 (let ((cur (point)))
42 (skip-syntax-backward "w_")
43 (goto-char
44 (if (re-search-backward (concat "\\_<" (current-word) "\\_>") nil t)
45 (match-beginning 0)
46 cur))))
47
48 ;;;###autoload
49 (defun sacha/search-word-forward ()
50 "Find the next occurrance of the current word."
51 (interactive)
52 (let ((cur (point)))
53 (skip-syntax-forward "w_")
54 (goto-char
55 (if (re-search-forward (concat "\\_<" (current-word) "\\_>") nil t)
56 (match-beginning 0)
57 cur))))
58
59 ;;;###autoload
60 (defun sacha/increase-font-size ()
61 (interactive)
62 (set-face-attribute 'default
63 nil
64 :height
65 (ceiling (* 1.10
66 (face-attribute 'default :height)))))
67 ;;;###autoload
68 (defun sacha/decrease-font-size ()
69 (interactive)
70 (set-face-attribute 'default
71 nil
72 :height
73 (floor (* 0.9
74 (face-attribute 'default :height)))))
75
76 ;;;###autoload
77 (defun epa-dired-mode-hook ()
78 (define-key dired-mode-map ":" 'epa-dired-prefix))
79
80 ;;;###autoload
81 (defun my-c-return ()
82 "When in minibuffer use `icicle-candidate-action', otherwise use `cua-set-rectangle-mark'."
83 (interactive)
84 (if (window-minibuffer-p (selected-window))
85 (call-interactively 'icicle-candidate-action)
86 (call-interactively 'cua-set-rectangle-mark)))
87
88 ;;; define filter. The filter is called on each entry in the agenda.
89 ;;; It defines a regexp to search for two timestamps, gets the start
90 ;;; and end point of the entry and does a regexp search. It also
91 ;;; checks if the category of the entry is in an exclude list and
92 ;;; returns either t or nil to skip or include the entry.
93
94 ;;;###autoload
95 (defun revert-all-buffers ()
96 "Refreshes all open buffers from their respective files."
97 (interactive)
98 (dolist (buf (buffer-list))
99 (with-current-buffer buf
100 (when (and (buffer-file-name) (not (buffer-modified-p)) (file-exists-p (buffer-file-name)))
101 (revert-buffer t t t) )))
102 (message "Refreshed open files.") )
103
104 ;;;###autoload
105 (defun move-line-up ()
106 "Move up the current line."
107 (interactive)
108 (transpose-lines 1)
109 (forward-line -2)
110 (indent-according-to-mode))
111
112 ;;;###autoload
113 (defun move-line-down ()
114 "Move down the current line."
115 (interactive)
116 (forward-line 1)
117 (transpose-lines 1)
118 (forward-line -1)
119 (indent-according-to-mode))
120
121 ;;;###autoload
122 (defun jj-untabify-buffer ()
123 "Get rid of all tabs"
124 (interactive)
125 (untabify (point-min) (point-max)))
126
127 ;;;###autoload
128 (defun prelude-sudo-edit (&optional arg)
129 "Edit currently visited file as root.
130
131 With a prefix ARG prompt for a file to visit.
132 Will also prompt for a file to visit if current
133 buffer is not visiting a file."
134 (interactive "P")
135 (if (or arg (not buffer-file-name))
136 (find-file (concat "/sudo:root@localhost:"
137 (icicle-find-file-of-content)))
138 (find-alternate-file (concat "/sudo:root@localhost:" buffer-file-name))))
139
140 ;; a great lisp coding hook
141 ;;;###autoload
142 (defun lisp-coding-defaults ()
143 (paredit-mode +1)
144 (rainbow-delimiters-mode +1))
145
146 ;;;###autoload
147 (defun interactive-lisp-coding-defaults ()
148 (paredit-mode +1)
149 (rainbow-delimiters-mode +1)
150 (whitespace-mode -1))
151
152 ;;;###autoload
153 (defun prelude-remove-elc-on-save ()
154 "If you're saving an elisp file, likely the .elc is no longer valid."
155 (make-local-variable 'after-save-hook)
156 (add-hook 'after-save-hook
157 (lambda ()
158 (if (file-exists-p (concat buffer-file-name "c"))
159 (delete-file (concat buffer-file-name "c"))))))
160
161 ;;;###autoload
162 (defun prelude-emacs-lisp-mode-defaults ()
163 (run-hooks 'lisp-coding-hook)
164 (turn-on-eldoc-mode)
165 (prelude-remove-elc-on-save)
166 (rainbow-mode +1)
167 (setq mode-name "EL"))
168
169 ;;;###autoload
170 (defun clean-mode-line ()
171 (interactive)
172 (loop for cleaner in mode-line-cleaner-alist
173 do (let* ((mode (car cleaner))
174 (mode-str (cdr cleaner))
175 (old-mode-str (cdr (assq mode minor-mode-alist))))
176 (when old-mode-str
177 (setcar old-mode-str mode-str))
178 ;; major mode
179 (when (eq mode major-mode)
180 (setq mode-name mode-str)))))
181
182 ;;;###autoload
183 (defun force-backup-of-buffer ()
184 (let ((buffer-backed-up nil))
185 (backup-buffer)))
186
187 ;;;###autoload
188 (defun prelude-kill-other-buffers ()
189 "Kill all buffers but the current one.
190 Doesn't mess with special buffers."
191 (interactive)
192 (require 'dash)
193 (-each
194 (->> (buffer-list)
195 (-filter #'buffer-file-name)
196 (--remove (eql (current-buffer) it)))
197 #'kill-buffer))
198
199 ;;;###autoload
200 (defun just-one-space-with-newline ()
201 "Call just-one-space with a negative argument"
202 (interactive)
203 (just-one-space -1))
204
205 ;(setq org-icalendar-verify-function 'org-mycal-export-limit)
206 ;(org-export-icalendar-combine-agenda-files)
207
208
209 ;;;###autoload
210 (defun font-lock-comment-annotations ()
211 "Highlight a bunch of well known comment annotations.
212
213 This functions should be added to the hooks of major modes for programming."
214 (font-lock-add-keywords
215 nil '(("\\<\\(FIX\\(ME\\)?\\|TODO\\|OPTIMIZE\\|HACK\\|REFACTOR\\):"
216 1 font-lock-warning-face t))))
217
218 ;;;###autoload
219 (defun jj-open-shell ()
220 "Open a shell in the directory of the current buffer file"
221
222 (interactive)
223 (when buffer-file-name
224 (setenv "ZSTARTDIR" (file-truename buffer-file-name)))
225 (when dired-directory
226 (setenv "ZSTARTDIR" (concat (file-truename dired-directory) "/dired")))
227 (start-process "open-shell" nil "/usr/bin/x-terminal-emulator"))
228
229 ; From: http://www.blogbyben.com/2013/09/emacs-function-humanifying-urls.html,
230 ; licensed CC BY 3.0. Author: Ben Simon
231 ;;;###autoload
232 (defun url-humanify ()
233 "Take the URL at point and make it human readable."
234 (interactive)
235 (let* ((area (bounds-of-thing-at-point 'url))
236 (num-params (count-matches "&" (car area) (cdr area)))
237 (i 0))
238 (beginning-of-thing 'url)
239 (when (search-forward "?" (cdr area) t nil)
240 (insert "\n ")
241 (while (< i num-params)
242 (search-forward "&" nil t nil)
243 (insert "\n ")
244 (save-excursion
245 (previous-line)
246 (beginning-of-line)
247 (let ((start (search-forward "="))
248 (end (search-forward "&")))
249 (url-decode-region start end)))
250 (setq i (+ i 1))))))
251
252 ; From: http://www.blogbyben.com/2013/09/emacs-function-humanifying-urls.html,
253 ; licensed CC BY 3.0. Author: Ben Simon
254 ;;;###autoload
255 (defun url-decode-region (start end)
256 "Replace a region with the same contents, only URL decoded."
257 (interactive "r")
258 (let ((text (url-unhex-string (buffer-substring start end))))
259 (delete-region start end)
260 (insert text)))
261
262 ;;;###autoload
263 (defun align-code (beg end &optional arg)
264 (interactive "rP")
265 (if (null arg)
266 (align beg end)
267 (let ((end-mark (copy-marker end)))
268 (indent-region beg end-mark nil)
269 (align beg end-mark))))
270
271 ;;;###autoload
272 (defun insert-date (prefix)
273 "Insert the current date. With prefix-argument, use ISO format. With
274 two prefix arguments, write out the day and month name."
275 (interactive "P")
276 (let ((format (cond
277 ((not prefix) "%d.%m.%Y")
278 ((equal prefix '(4)) "%Y-%m-%d")
279 ((equal prefix '(16)) "%A, %d. %B %Y")
280 ((equal prefix '(64)) "%Y-%m-%dT%H:%M:%S.%3N")
281 ))
282 (system-time-locale "de_DE"))
283 (insert (format-time-string format))))
284
285 ;;;###autoload
286 (defun occur-dwim ()
287 "Call `occur' with a sane default."
288 (interactive)
289 (push (if (region-active-p)
290 (buffer-substring-no-properties
291 (region-beginning)
292 (region-end))
293 (thing-at-point 'symbol))
294 regexp-history)
295 (call-interactively 'occur))
296
297 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
298 ;; change case of letters ;;
299 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
300 ;; http://ergoemacs.org/emacs/modernization_upcase-word.html
301 ;;;###autoload
302 (defun toggle-letter-case ()
303 "Toggle the letter case of current word or text selection.
304 Toggles between: “all lower”, “Init Caps”, “ALL CAPS”."
305 (interactive)
306 (let (p1 p2 (deactivate-mark nil) (case-fold-search nil))
307 (if (region-active-p)
308 (setq p1 (region-beginning) p2 (region-end))
309 (let ((bds (bounds-of-thing-at-point 'word) ) )
310 (setq p1 (car bds) p2 (cdr bds)) ) )
311
312 (when (not (eq last-command this-command))
313 (save-excursion
314 (goto-char p1)
315 (cond
316 ((looking-at "[[:lower:]][[:lower:]]") (put this-command 'state "all lower"))
317 ((looking-at "[[:upper:]][[:upper:]]") (put this-command 'state "all caps") )
318 ((looking-at "[[:upper:]][[:lower:]]") (put this-command 'state "init caps") )
319 ((looking-at "[[:lower:]]") (put this-command 'state "all lower"))
320 ((looking-at "[[:upper:]]") (put this-command 'state "all caps") )
321 (t (put this-command 'state "all lower") ) ) )
322 )
323
324 (cond
325 ((string= "all lower" (get this-command 'state))
326 (upcase-initials-region p1 p2) (put this-command 'state "init caps"))
327 ((string= "init caps" (get this-command 'state))
328 (upcase-region p1 p2) (put this-command 'state "all caps"))
329 ((string= "all caps" (get this-command 'state))
330 (downcase-region p1 p2) (put this-command 'state "all lower")) )
331 )
332 )
333
334
335 (provide 'ganneff)
336
337 ;;; ganneff.el ends here