Merge branch 'master' of git.ganneff.de:emacs
[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 my-dired-init ()
15 "Bunch of stuff to run for dired when it's loaded."
16 (define-key dired-mode-map [return] 'dired-single-buffer)
17 (define-key dired-mode-map [mouse-1] 'dired-single-buffer-mouse)
18 (define-key dired-mode-map "r" 'wdired-change-to-wdired-mode)
19 (define-key dired-mode-map "^"
20 (function
21 (lambda nil (interactive) (dired-single-buffer "..")))))
22
23 ;;;###autoload
24 (defun ido-disable-line-trucation () (set (make-local-variable 'truncate-lines) nil))
25
26
27 ;;;###autoload
28 (defun sacha/isearch-yank-current-word ()
29 "Pull current word from buffer into search string."
30 (interactive)
31 (save-excursion
32 (skip-syntax-backward "w_")
33 (isearch-yank-internal
34 (lambda ()
35 (skip-syntax-forward "w_")
36 (point)))))
37
38 ;;;###autoload
39 (defun sacha/search-word-backward ()
40 "Find the previous occurrence of the current word."
41 (interactive)
42 (let ((cur (point)))
43 (skip-syntax-backward "w_")
44 (goto-char
45 (if (re-search-backward (concat "\\_<" (current-word) "\\_>") nil t)
46 (match-beginning 0)
47 cur))))
48
49 ;;;###autoload
50 (defun sacha/search-word-forward ()
51 "Find the next occurrance of the current word."
52 (interactive)
53 (let ((cur (point)))
54 (skip-syntax-forward "w_")
55 (goto-char
56 (if (re-search-forward (concat "\\_<" (current-word) "\\_>") nil t)
57 (match-beginning 0)
58 cur))))
59
60 ;;;###autoload
61 (defun sacha/increase-font-size ()
62 (interactive)
63 (set-face-attribute 'default
64 nil
65 :height
66 (ceiling (* 1.10
67 (face-attribute 'default :height)))))
68 ;;;###autoload
69 (defun sacha/decrease-font-size ()
70 (interactive)
71 (set-face-attribute 'default
72 nil
73 :height
74 (floor (* 0.9
75 (face-attribute 'default :height)))))
76
77 ;;;###autoload
78 (defun epa-dired-mode-hook ()
79 (define-key dired-mode-map ":" 'epa-dired-prefix))
80
81 ;;;###autoload
82 (defun my-c-return ()
83 "When in minibuffer use `icicle-candidate-action', otherwise use `cua-set-rectangle-mark'."
84 (interactive)
85 (if (window-minibuffer-p (selected-window))
86 (call-interactively 'icicle-candidate-action)
87 (call-interactively 'cua-set-rectangle-mark)))
88
89 ;;; define filter. The filter is called on each entry in the agenda.
90 ;;; It defines a regexp to search for two timestamps, gets the start
91 ;;; and end point of the entry and does a regexp search. It also
92 ;;; checks if the category of the entry is in an exclude list and
93 ;;; returns either t or nil to skip or include the entry.
94
95 ;;;###autoload
96 (defun org-mycal-export-limit ()
97 "Limit the export to items that have a date, time and a range. Also exclude certain categories."
98 (setq org-tst-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ... [0-9]\\{2\\}:[0-9]\\{2\\}[^\r\n>]*?\\)>")
99 (setq org-tstr-regexp (concat org-tst-regexp "--?-?" org-tst-regexp))
100 (save-excursion
101 ; get categories
102 (setq mycategory (org-get-category))
103 ; get start and end of tree
104 (org-back-to-heading t)
105 (setq mystart (point))
106 (org-end-of-subtree)
107 (setq myend (point))
108 (goto-char mystart)
109 ; search for timerange
110 (setq myresult (re-search-forward org-tstr-regexp myend t))
111 ; search for categories to exclude
112 (setq mycatp (member mycategory org-export-exclude-category))
113 ; return t if ok, nil when not ok
114 (if (and myresult (not mycatp)) t nil)))
115
116 ;;;###autoload
117 (defun mycal-export-limit ()
118 "Limit the export to items that don't match an unwanted category "
119 (setq mycategory (org-get-category))
120 (not (member mycategory org-export-exclude-category)))
121
122 ;;; activate filter and call export function
123 ;;;###autoload
124 (defun org-mycal-export ()
125 (interactive)
126 (let ((org-icalendar-verify-function 'mycal-export-limit))
127 (org-export-icalendar-combine-agenda-files)))
128
129 ;;;###autoload
130 (defun revert-all-buffers ()
131 "Refreshes all open buffers from their respective files."
132 (interactive)
133 (dolist (buf (buffer-list))
134 (with-current-buffer buf
135 (when (and (buffer-file-name) (not (buffer-modified-p)) (file-exists-p (buffer-file-name)))
136 (revert-buffer t t t) )))
137 (message "Refreshed open files.") )
138
139 ;;;###autoload
140 (defun move-line-up ()
141 "Move up the current line."
142 (interactive)
143 (transpose-lines 1)
144 (forward-line -2)
145 (indent-according-to-mode))
146
147 ;;;###autoload
148 (defun move-line-down ()
149 "Move down the current line."
150 (interactive)
151 (forward-line 1)
152 (transpose-lines 1)
153 (forward-line -1)
154 (indent-according-to-mode))
155
156 ;;;###autoload
157 (defun jj-untabify-buffer ()
158 "Get rid of all tabs"
159 (interactive)
160 (untabify (point-min) (point-max)))
161
162 ;;;###autoload
163 (defun prelude-sudo-edit (&optional arg)
164 "Edit currently visited file as root.
165
166 With a prefix ARG prompt for a file to visit.
167 Will also prompt for a file to visit if current
168 buffer is not visiting a file."
169 (interactive "P")
170 (if (or arg (not buffer-file-name))
171 (find-file (concat "/sudo:root@localhost:"
172 (icicle-find-file-of-content)))
173 (find-alternate-file (concat "/sudo:root@localhost:" buffer-file-name))))
174
175 ;; a great lisp coding hook
176 ;;;###autoload
177 (defun lisp-coding-defaults ()
178 (paredit-mode +1)
179 (rainbow-delimiters-mode +1))
180
181 ;;;###autoload
182 (defun interactive-lisp-coding-defaults ()
183 (paredit-mode +1)
184 (rainbow-delimiters-mode +1)
185 (whitespace-mode -1))
186
187 ;;;###autoload
188 (defun prelude-remove-elc-on-save ()
189 "If you're saving an elisp file, likely the .elc is no longer valid."
190 (make-local-variable 'after-save-hook)
191 (add-hook 'after-save-hook
192 (lambda ()
193 (if (file-exists-p (concat buffer-file-name "c"))
194 (delete-file (concat buffer-file-name "c"))))))
195
196 ;;;###autoload
197 (defun prelude-emacs-lisp-mode-defaults ()
198 (run-hooks 'lisp-coding-hook)
199 (turn-on-eldoc-mode)
200 (prelude-remove-elc-on-save)
201 (rainbow-mode +1)
202 (setq mode-name "EL"))
203
204 ;;;###autoload
205 (defun clean-mode-line ()
206 (interactive)
207 (loop for cleaner in mode-line-cleaner-alist
208 do (let* ((mode (car cleaner))
209 (mode-str (cdr cleaner))
210 (old-mode-str (cdr (assq mode minor-mode-alist))))
211 (when old-mode-str
212 (setcar old-mode-str mode-str))
213 ;; major mode
214 (when (eq mode major-mode)
215 (setq mode-name mode-str)))))
216
217 ;;;###autoload
218 (defun force-backup-of-buffer ()
219 (let ((buffer-backed-up nil))
220 (backup-buffer)))
221
222 ;;;###autoload
223 (defun prelude-kill-other-buffers ()
224 "Kill all buffers but the current one.
225 Doesn't mess with special buffers."
226 (interactive)
227 (require 'dash)
228 (-each
229 (->> (buffer-list)
230 (-filter #'buffer-file-name)
231 (--remove (eql (current-buffer) it)))
232 #'kill-buffer))
233
234 ;;;###autoload
235 (defun just-one-space-with-newline ()
236 "Call just-one-space with a negative argument"
237 (interactive)
238 (just-one-space -1))
239
240 ;(setq org-icalendar-verify-function 'org-mycal-export-limit)
241 ;(org-export-icalendar-combine-agenda-files)
242
243 (provide 'ganneff)
244
245 ;;; ganneff.el ends here