1 ;;; ganneff1.el --- Some functions and stuff I use
3 ;;; Copyright (C) 2012.2013 Joerg Jaspert
5 ;; Filename: ganneff.de
6 ;; Author: Joerg Jaspert <joerg@debian.org>
9 ;; This is just stuff I use in my emacs configuration.
14 (defun ido-disable-line-trucation () (set (make-local-variable 'truncate-lines) nil))
16 ; match-paren will either jump to the "other" paren or simply insert %
17 ; #+BEGIN_SRC emacs-lisp tangle:yes
19 (defun match-paren (arg)
20 "Go to the matching parenthesis if on parenthesis otherwise insert %."
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)))))
27 (defun sacha/isearch-yank-current-word ()
28 "Pull current word from buffer into search string."
31 (skip-syntax-backward "w_")
32 (isearch-yank-internal
34 (skip-syntax-forward "w_")
38 (defun sacha/search-word-backward ()
39 "Find the previous occurrence of the current word."
42 (skip-syntax-backward "w_")
44 (if (re-search-backward (concat "\\_<" (current-word) "\\_>") nil t)
49 (defun sacha/search-word-forward ()
50 "Find the next occurrance of the current word."
53 (skip-syntax-forward "w_")
55 (if (re-search-forward (concat "\\_<" (current-word) "\\_>") nil t)
60 (defun sacha/increase-font-size ()
62 (set-face-attribute 'default
66 (face-attribute 'default :height)))))
68 (defun sacha/decrease-font-size ()
70 (set-face-attribute 'default
74 (face-attribute 'default :height)))))
77 (defun epa-dired-mode-hook ()
78 (define-key dired-mode-map ":" 'epa-dired-prefix))
82 "When in minibuffer use `icicle-candidate-action', otherwise use `cua-set-rectangle-mark'."
84 (if (window-minibuffer-p (selected-window))
85 (call-interactively 'icicle-candidate-action)
86 (call-interactively 'cua-set-rectangle-mark)))
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.
95 (defun revert-all-buffers ()
96 "Refreshes all open buffers from their respective files."
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.") )
105 (defun move-line-up ()
106 "Move up the current line."
110 (indent-according-to-mode))
113 (defun move-line-down ()
114 "Move down the current line."
119 (indent-according-to-mode))
122 (defun jj-untabify-buffer ()
123 "Get rid of all tabs"
125 (untabify (point-min) (point-max)))
128 (defun prelude-sudo-edit (&optional arg)
129 "Edit currently visited file as root.
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."
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))))
140 ;; a great lisp coding hook
142 (defun lisp-coding-defaults ()
144 (rainbow-delimiters-mode +1))
147 (defun interactive-lisp-coding-defaults ()
149 (rainbow-delimiters-mode +1)
150 (whitespace-mode -1))
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
158 (if (file-exists-p (concat buffer-file-name "c"))
159 (delete-file (concat buffer-file-name "c"))))))
162 (defun prelude-emacs-lisp-mode-defaults ()
163 (run-hooks 'lisp-coding-hook)
165 (prelude-remove-elc-on-save)
167 (setq mode-name "EL"))
170 (defun clean-mode-line ()
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))))
177 (setcar old-mode-str mode-str))
179 (when (eq mode major-mode)
180 (setq mode-name mode-str)))))
183 (defun force-backup-of-buffer ()
184 (let ((buffer-backed-up nil))
188 (defun prelude-kill-other-buffers ()
189 "Kill all buffers but the current one.
190 Doesn't mess with special buffers."
195 (-filter #'buffer-file-name)
196 (--remove (eql (current-buffer) it)))
200 (defun just-one-space-with-newline ()
201 "Call just-one-space with a negative argument"
205 ;(setq org-icalendar-verify-function 'org-mycal-export-limit)
206 ;(org-export-icalendar-combine-agenda-files)
210 (defun font-lock-comment-annotations ()
211 "Highlight a bunch of well known comment annotations.
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))))
219 (defun jj-open-shell ()
220 "Open a shell in the directory of the current buffer file"
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"))
229 ; From: http://www.blogbyben.com/2013/09/emacs-function-humanifying-urls.html,
230 ; licensed CC BY 3.0. Author: Ben Simon
232 (defun url-humanify ()
233 "Take the URL at point and make it human readable."
235 (let* ((area (bounds-of-thing-at-point 'url))
236 (num-params (count-matches "&" (car area) (cdr area)))
238 (beginning-of-thing 'url)
239 (when (search-forward "?" (cdr area) t nil)
241 (while (< i num-params)
242 (search-forward "&" nil t nil)
247 (let ((start (search-forward "="))
248 (end (search-forward "&")))
249 (url-decode-region start end)))
252 ; From: http://www.blogbyben.com/2013/09/emacs-function-humanifying-urls.html,
253 ; licensed CC BY 3.0. Author: Ben Simon
255 (defun url-decode-region (start end)
256 "Replace a region with the same contents, only URL decoded."
258 (let ((text (url-unhex-string (buffer-substring start end))))
259 (delete-region start end)
263 (defun align-code (beg end &optional arg)
267 (let ((end-mark (copy-marker end)))
268 (indent-region beg end-mark nil)
269 (align beg end-mark))))
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."
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")
282 (system-time-locale "de_DE"))
283 (insert (format-time-string format))))
287 "Call `occur' with a sane default."
289 (push (if (region-active-p)
290 (buffer-substring-no-properties
293 (thing-at-point 'symbol))
295 (call-interactively 'occur))
297 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
298 ;; change case of letters ;;
299 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
300 ;; http://ergoemacs.org/emacs/modernization_upcase-word.html
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”."
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)) ) )
312 (when (not (eq last-command this-command))
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") ) ) )
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")) )
337 ;;; ganneff.el ends here