;; -*-mode: Emacs-Lisp; outline-minor-mode:t-*-
;; Copyright (C) 1996-2010  Dirk-Jan C. Binnema.
;; URL: http://www.djcbsoftware.nl/dot-emacs.html
;; This file is free software licensed under the terms of the
;; GNU General Public License, version 3 or later.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


(defun setup-bzr-emacs-loadpath()
  (let ((startup-file "/usr/share/emacs/site-lisp/debian-startup.el"))
    (if (and (or (not (fboundp 'debian-startup))
               (not (boundp  'debian-emacs-flavor)))
          (file-readable-p startup-file))
      (progn
        (load-file startup-file)
        (setq debian-emacs-flavor 'emacs-snapshot)
        (debian-startup debian-emacs-flavor)
        
        (mapcar '(lambda (f)
                   (and (not (string= (substring f -3) "/.."))
                     (file-directory-p f) 
                     (add-to-list 'load-path f)))
          (directory-files "/usr/share/emacs/site-lisp" t))))))

(when (or
        (string= emacs-version "23.1.91.5")
        (string= emacs-version "23.1.92.1"))
  (setup-bzr-emacs-loadpath))



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; loadpath: add everything under ~/.emacs.d to it
(let* ((my-lisp-dir "~/.emacs.d/")
        (default-directory my-lisp-dir))
  (setq load-path (cons my-lisp-dir load-path))
  (normal-top-level-add-subdirs-to-load-path))

;; elpa
(when (file-exists-p "~/.emacs.d/elpa/package.el")
  (when (load (expand-file-name "~/.emacs.d/elpa/package.el"))
    (package-initialize)))

;; load my handy functions
(require 'djcb-funcs nil 'noerror) ;; load it it can be found...
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; general settings
(menu-bar-mode  t)                       ;; show the menu...
(mouse-avoidance-mode 'jump)             ;; mouse ptr when cursor is too close
(tool-bar-mode -1)                       ;; turn-off toolbar 
(scroll-bar-mode t)                      ;; show a scrollbar...
(set-scroll-bar-mode 'right)             ;; ... on the right

(setq cua-enable-cua-keys nil)           ;; only for rectangles
(cua-mode t)

(setq ;; scrolling
  scroll-margin 5                        ;; do smooth scrolling, ...
  scroll-conservatively 100000           ;; ... the defaults ...
  scroll-up-aggressively 0               ;; ... are very ...
  scroll-down-aggressively 0             ;; ... annoying
  scroll-preserve-screen-position t)     ;; preserve screen pos with C-v/M-v 

(setq fringe-mode '(1 . 0))              ;; emacs 22+
 
(transient-mark-mode t)                  ;; make current 'selection' visible
(delete-selection-mode t)                ;; delete the sel with a keyp

(setq x-select-enable-clipboard t        ;; copy-paste should work ...
  interprogram-paste-function            ;; ...with...
  'x-cut-buffer-or-selection-value)      ;; ...other X clients

(setq search-highlight t                 ;; highlight when searching... 
  query-replace-highlight t)             ;; ...and replacing
(fset 'yes-or-no-p 'y-or-n-p)            ;; enable y/n answers to yes/no 

(setq completion-ignore-case t           ;; ignore case when completing...
  read-file-name-completion-ignore-case t) ;; ...filenames too

(put 'narrow-to-region 'disabled nil)    ;; enable...
(put 'erase-buffer 'disabled nil)        ;; ... useful things
(file-name-shadow-mode t)                ;; be smart about filenames in mbuf

(setq inhibit-startup-message t          ;; don't show ...    
  inhibit-startup-echo-area-message t)   ;; ... startup messages
(setq require-final-newline t)           ;; end files with a newline
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; highlight the current line
(when (fboundp 'global-hl-line-mode)
  (global-hl-line-mode t)) ;; turn it on for all modes by default

;; show-paren-mode: subtle blinking of matching paren (defaults are ugly)
;; http://www.emacswiki.org/cgi-bin/wiki/ShowParenMode
(when (fboundp 'show-paren-mode)
  (show-paren-mode t)
  (setq show-paren-style 'parenthesis))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(require 'uniquify) ;; make buffer names more unique
(setq 
  uniquify-buffer-name-style 'post-forward
  uniquify-separator ":"
  uniquify-after-kill-buffer-p t
  uniquify-ignore-buffers-re "^\\*")

;; put something different in the scratch buffer
(setq initial-scratch-message
  ";; scratch buffer created -- happy hacking\n")
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; the frame title
(setq-default
 frame-title-format
 '(:eval
   (format "%s@%s:%s"
           (or (file-remote-p default-directory 'user) user-login-name)
           (or (file-remote-p default-directory 'host) system-name)
           (file-name-nondirectory (or (buffer-file-name) default-directory)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; the modeline
(line-number-mode t)                     ;; show line numbers
(column-number-mode t)                   ;; show column numbers
(size-indication-mode t)                 ;; show file size (emacs 22+)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; the minibuffer
(setq
  enable-recursive-minibuffers t         ;;  allow mb cmds in the mb
  max-mini-window-height .25             ;;  max 2 lines
  minibuffer-scroll-window nil
  resize-mini-windows t)

(icomplete-mode t)                       ;; completion in minibuffer
(setq 
  icomplete-prospects-height 1           ;; don't spam my minibuffer
  icomplete-compute-delay 0)             ;; don't wait
(require 'icomplete+ nil 'noerror)       ;; drew adams' extras
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ibuffer
(when (require 'ibuffer nil 'noerror) 
  (setq ibuffer-saved-filter-groups
    (quote (("default1"   
              ("Org"   (mode . org-mode))
              ("Mail"
                (or
                  (mode . message-mode)
                  (mode . bbdb-mode)
                  (mode . mail-mode)
                  (mode . wl-folder-mode)
                  (mode . wl-summary-mode)
                  (mode . wl-draft-mode)
                  (mode . mime-edit-mode)
                  (mode . mime-view-mode)
                  (mode . gnus-group-mode)
                  (mode . gnus-summary-mode)
                  (mode . gnus-article-mode)
                  (name . "^\\*BBDB\\*$")
                  (name . "^\\.bbdb$")
                  (name . "^\\.newsrc-dribble")))
              ("mu"       (name . "src/mu/"))
              ("sofia"    (name . "sofia"))
              ("flatland" (name . "flatland"))
             
              ("Programming"
               (or
                 (mode . c-mode)
                 (mode . c++-mode)
                 (mode . gud-mode)
                 (mode . makefile-mode)
                 (mode . autoconf-mode)))
              ("Scripting"
               (or
                 (mode . perl-mode)
                 (mode . sh-mode)
                 (mode . python-mode)))
              ("crap" (or
                        (name . "^\\*trace")
                        (name . "^\\*completions")
                        (name . "^\\*Quail")
                        (name . "^\\*magit")
                        (name . "^\\*Backtrace\\*$")
                        (name . "^\\*compilation\\*$")
                        (name . "^\\*scratch\\*$")
                        (name . "^\\*Messages\\*$")))
              ("ERC"   (mode . erc-mode))
              ("Magit" (name . "^\\*magit\\*$"))
              )))))

(add-hook 'ibuffer-mode-hook
  (lambda ()
    (ibuffer-switch-to-saved-filter-groups "default1")))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; overrride the default function....
(defun emacs-session-filename (SESSION-ID)
  (concat "~/.emacs.d/cache/session." SESSION-ID))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ;; use tempbuf to kill unneeded buffer automagically
;; (when (require 'tempbuf nil 'noerror)
;;   (mapcar '(lambda(p) (add-hook p 'turn-on-tempbuf-mode))
;;     '(w3-mode-hook Man-mode-hook view-mode-hook compilation-mode-hook)))

;; (defun djcb-tempbuf-add-hook-maybe (symbol)
;;   (when (and (fboundp 'turn-on-tempbuf-mode) (fboundp symbol))
;;     (add-hook symbol turn-on-tempbuf-mode)))
;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; utf8 / input-method
(setq locale-coding-system 'utf-8)
(set-terminal-coding-system 'utf-8)
(set-keyboard-coding-system 'utf-8)
(set-selection-coding-system 'utf-8)
(prefer-coding-system 'utf-8)
(set-language-environment "UTF-8")       ; prefer utf-8 for language settings
(set-input-method nil)                   ; no funky input for normal editing;
(setq read-quoted-char-radix 10)         ; use decimal, not octal
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; saving things across sessions
;; bookmarks
(setq bookmark-default-file "~/.emacs.d/data/bookmarks" ;; bookmarks
  bookmark-save-flag 1)                            ;; autosave each change

;; saveplace: save location in file when saving files
(setq save-place-file "~/.emacs.d/cache/saveplace")
(setq-default save-place t)            ;; activate it for all buffers
(require 'saveplace)                   ;; get the package
;;
;; savehist: save some history
(setq savehist-additional-variables    ;; also save...
  '(search ring regexp-search-ring)    ;; ... my search entries
  savehist-autosave-interval 60        ;; save every minute (default: 5 min)
  savehist-file "~/.emacs.d/cache/savehist")   ;; keep my home clean
(savehist-mode t)                      ;; do customization before activation

;; recentf
(require 'recentf)    ;; save recently used files
(setq
  recentf-save-file "~/.emacs.d/cache/recentf"
  recentf-max-saved-items 100     ;; max save 100
  recentf-max-menu-items 15)      ;; max 15 in menu
(recentf-mode t)                  ;; turn it on

;; abbrevs (abbreviations)
(setq abbrev-file-name                 ;; tell emacs where to read abbrev
      "~/.emacs.d/data/abbrev_defs")  ;; definitions from...
(abbrev-mode t)                        ;; enable abbrevs (abbreviations) ...
(setq default-abbrev-mode t            ;; turn it on
  save-abbrevs t)                      ;; don't ask
(when (file-exists-p abbrev-file-name)
  (quietly-read-abbrev-file))          ;;  don't tell
(add-hook 'kill-emacs-hook             ;; write when ...
  'write-abbrev-file)                  ;; ... exiting emacs

;; filecache: http://www.emacswiki.org/cgi-bin/wiki/FileNameCache
(eval-after-load "filecache" 
  '(progn (message "Loading file cache...")
     (file-cache-add-directory "~/")
     (file-cache-add-directory-list '("~/Desktop" "~/Documents"))))

;; backups
(setq make-backup-files t ;; do make backups
  backup-by-copying t     ;; and copy them here
  backup-directory-alist '(("." . "~/.emacs.d/cache/backups")) 
  version-control t
  kept-new-versions 2
  kept-old-versions 5
  delete-old-versions t)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; misc small stuff
;; time-stamps (better not use those in version-controlled files)
(setq ;; when there's "Time-stamp: <>" in the first 10 lines of the file
  time-stamp-active t        ; do enable time-stamps
  time-stamp-line-limit 10   ; check first 10 buffer lines for Time-stamp: <>
  time-stamp-format "%04y-%02m-%02d %02H:%02M:%02S (%u)") ; date format
(add-hook 'write-file-hooks 'time-stamp) ; update when saving

(setq auto-save-list-file-prefix
  "~/.emacs.d/cache/auto-save-list/.saves-")
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; color-theme: give me some colors
(when (and (require 'color-theme nil 'noerror)
        (require 'color-theme-init nil 'noerror)
        (color-theme-djcb-dark)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; enable the sr-speedbar if it's available
(when (require 'sr-speedbar nil 'noerror)
  (setq speedbar-supported-extension-expressions
    '(".org" ".[ch]\\(\\+\\+\\|pp\\|c\\|h\\|xx\\)?"
       ".tex\\(i\\(nfo\\)?\\)?" ".el"
       ".java" ".p[lm]" ".pm" ".py"  ".s?html"  "Makefile.am" "configure.ac"))
  (setq
    sr-speedbar-width-x 20
    sr-speedbar-right-side t))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; tramp, for remote access
(require 'tramp)
;; we need a bit more funky pattern, as tramp will start $SHELL
;; (sudo -s), ie., zsh for root user
(setq shell-prompt-pattern "^[^a-zA-Z].*[#$%>] *")
(setq tramp-default-method "ssh")
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; global keybindings
(global-set-key (kbd "RET")         'newline-and-indent)
(global-set-key (kbd "C-<f4>")      'kill-buffer-and-window)
(global-set-key (kbd "<delete>")    'delete-char)  ; delete == delete    
(global-set-key (kbd "M-g")         'goto-line)    ; M-g  'goto-line

(when (fboundp 'ibuffer)
  (global-set-key (kbd "C-x C-b") 'ibuffer))   ;; ibuffer

;; C-pgup goes to the start, C-pgdw goes to the end
(global-set-key (kbd "<C-prior>")
  (lambda()(interactive)(goto-char(point-min))))
(global-set-key (kbd "<C-next>")
  (lambda()(interactive)(goto-char(point-max))))

(global-set-key (kbd "C-z") 'undo)   ;; use it like CUA, not like 'suspend'
(global-set-key (kbd "C-c DEL") 'pop-global-mark) ; jump back to prev location

(require 'magit nil 'noerror)
;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; programming
(autoload 'linum-mode "linum" "mode for line numbers" t) 
(global-set-key (kbd "C-<f5>") 'linum-mode)                 ;; line numbers
(autoload 'magit-status "magit" "marius' git mode")
(global-set-key (kbd "C-<f6>") 'magit-status)               ;; ...git mode
(global-set-key (kbd "C-<f7>") 'compile)                     ;; compile
(global-set-key (kbd "C-<f8>") 'comment-or-uncomment-region) ;; (un)comment

(when (fboundp 'sr-speedbar-toggle)
  (global-set-key (kbd "C-<f9>") 'sr-speedbar-toggle)
  (global-set-key (kbd "C-<f10>") 'sr-speedbar-select-window)) ; speedbar

(global-set-key (kbd "C-<f11>") 'whitespace-mode)             ;; show blanks

(global-set-key (kbd "<S-f9>")   'djcb-fullscreen-toggle)       ;; fullscreen
(global-set-key (kbd "<S-<f10>")  'package-list-packages)       ;; elpa

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; elscreen
(setq elscreen-prefix-key (kbd "C-c q")) ; 'q' for 'quick jump to'
(when (require 'elscreen nil 'noerror)
    (global-set-key (kbd "<f12>"    )  'elscreen-create)
    (global-set-key (kbd "<s-f12>"   )  'elscreen-kill)  
    (global-set-key (kbd "<C-M-tab>")  'elscreen-previous) 
    (global-set-key (kbd "<C-tab>"  )  'elscreen-next))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; productivity stuff; f9-f12 
(global-set-key (kbd "C-c l") 'org-store-link)  ;; Links
(global-set-key (kbd "C-c a") 'org-agenda)      ;; Agenda
(global-set-key (kbd "C-c b") 'org-iswitchb)    ;; switch
(global-set-key (kbd "<f5>")  'wl)              ;; Wanderlust
(global-set-key (kbd "<f6>")  'org-agenda-list) ;; Agenda
(global-set-key (kbd "<f7>") 'org-todo-list)   ;; todo-list (NextActions)
(global-set-key (kbd "<f8>")  'remember)        ;; remember
;;(autoload 'notmuch "notmuch" "notmuch" t)   ;; 
;;(global-set-key (kbd "<f9>")  'notmuch)     ;; notmuch
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; program shortcuts
(global-set-key (kbd "C-c b") 'browse-url)  ;; Browse (W3M)
(global-set-key (kbd "C-c e") 'djcb-erc-start-or-switch) ;; ERC

(global-set-key (kbd "C-c f") 'browse-url-firefox)  ;; Firefox...
(setq browse-url-firefox-new-window-is-tab t)  ;; ... use tabs

(global-set-key (kbd "C-c g") 'w3m-goto-url)   ;; Goto-url (W3M)
;;(global-set-key (kbd "C-c t") 'twitter-get-friends-timeline) ;; Twitter
(global-set-key (kbd "C-c i") 'identica-mode) ;; identi.ca
(global-set-key (kbd "C-c n") 'normal-mode)

;; specific file shortcuts; s-f 
(global-set-key (kbd "C-c W") ;; wanderlust
  (lambda()(interactive)(find-file wl-init-file))) 
(global-set-key (kbd "C-c S") ;; scratch
  (lambda()(interactive)(switch-to-buffer "*scratch*")))
(global-set-key (kbd "C-c E") ;; .emacs
  (lambda()(interactive)(find-file "~/.emacs.d/init.el")))
(global-set-key (kbd "C-c O") ;; org-init
  (lambda()(interactive)(find-file "~/.emacs.d/org/org-mode-init.el"))) 
(global-set-key (kbd "C-c G") ;; gtd.org
  (lambda()(interactive)(find-file (concat org-directory
                                     "agenda/todo.org"))))
(global-set-key (kbd "C-c R") ;; remember.org
  (lambda()(interactive)(find-file org-default-notes-file))) 

(global-set-key (kbd "C-c N") ;; 
  (lambda()(interactive)
    (ispell-change-dictionary "nederlands")
    (flyspell-buffer))) 

;; use super + arrow keys to switch between visible buffers
(require 'windmove)
(windmove-default-keybindings 'super) ;; will be overridden
(global-set-key (kbd "<C-s-left>")  'windmove-left)
(global-set-key (kbd "<C-s-right>") 'windmove-right)
(global-set-key (kbd "<C-s-up>")    'windmove-up)
(global-set-key (kbd "<C-s-down>")  'windmove-down)

;; restore window configuration
(require 'winner)
(setq winner-dont-bind-my-keys t) ;; winner conflicts with org
(global-set-key (kbd "<s-left>")      'winner-undo)
(global-set-key (kbd "<XF86Forward>") 'winner-redo)
(global-set-key (kbd "<s-right>") 'winner-redo)
(global-set-key (kbd "<XF86Back>") 'winner-undo)
(winner-mode t)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; development stuff
(global-set-key (kbd "C-c p") ;; jump to my projects file
  (lambda()(interactive)(find-file (concat "~/.emacs.d/data/projects-"
                                     (user-login-name) ".org"))))

;; make
(global-set-key (kbd "C-c m a") 'compile)
(global-set-key (kbd "C-c m t") ;; tidy
  (lambda()(interactive)(shell-command "make clean &")))
(global-set-key (kbd "C-c m i")
  (lambda()(interactive)(djcb-sudo-shell-command "make install &")))
(global-set-key (kbd "C-c m u")
  (lambda()(interactive)(djcb-sudo-shell-command "make uninstall &")))

;; check and dist
(global-set-key (kbd "C-c m D")
  (lambda()(interactive)(djcb-sudo-shell-command "make dist &")))
(global-set-key (kbd "C-c m C")
  (lambda()(interactive)(djcb-sudo-shell-command "make check &")))
(global-set-key (kbd "C-c m X") ;; Both
  (lambda()(interactive)(djcb-sudo-shell-command "make distcheck &")))

;; configure / autogen / configure
(global-set-key (kbd "C-c m r") ;; Reconfigure
  (lambda()(interactive)(shell-command 
                          (if (file-exists-p "autogen.sh") "autogen.sh &"
                          "autoreconf -i &"))))
(global-set-key (kbd "C-c m c") 'djcb-configure) ;; package-Configure

;; debian
(global-set-key (kbd "C-c m d u") ;; Update changelog
  (lambda()(interactive)(shell-command "dch -i &")))
(global-set-key (kbd "C-c m d b") ;; Build package
  (lambda()(interactive)(shell-command "dpkg-buildpackage -rfakeroot -B &")))



;; scratchbox versions of the above; assumes that dirs are the same

(global-set-key (kbd "C-c s m a") 
  (lambda()(interactive)(djcb-sbox-shell-command "make &")))
(global-set-key (kbd "C-c s m t") ;; tidy
  (lambda()(interactive)(djcb-sbox-shell-command "make clean &")))
(global-set-key (kbd "C-c s m i")
  (lambda()(interactive)(djcb-sbox-shell-command "make install &")))
(global-set-key (kbd "C-c s m u")
  (lambda()(interactive)(djcb-sbox-shell-command "make uninstall &")))

;; check and dist
(global-set-key (kbd "C-c s m D")
  (lambda()(interactive)(djcb-sbox-shell-command "make dist &")))
(global-set-key (kbd "C-c s m C")
  (lambda()(interactive)(djcb-sbox-shell-command "make check &")))
(global-set-key (kbd "C-c s m X") ;; Both
  (lambda()(interactive)(djcb-sbox-shell-command "make distcheck &")))

;; configure / autogen / configure
(global-set-key (kbd "C-c s m r") ;; Reconfigure
  (lambda()(interactive)(djcb-sbox-shell-command 
                          (if (file-exists-p "autogen.sh") "autogen.sh &"
                          "autoreconf -i &"))))
(global-set-key (kbd "C-c s m c") 'djcb-sbox-configure) ;; package-Configure

;; debian
(global-set-key (kbd "C-c s m d u") ;; Update changelog
  (lambda()(interactive)(djcb-sbox-shell-command "dch -i &")))
(global-set-key (kbd "C-c s m d b") ;; Build package
  (lambda()(interactive)(djcb-sbox-shell-command
                          "dpkg-buildpackage -rfakeroot -B &")))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; company mode
(when (require 'company nil 'noerror)
  (company-mode t)
  (setq company-idle-delay t)
  (setq company-begin-commands '(self-insert-command)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; yasnippet
(when (require 'yasnippet-bundle nil 'noerror) ;; note: yasnippet-bundle
  (setq yas/root-directory "~/.emacs.d/yas") ;; my own snippets
  (yas/initialize)
  (yas/load-directory yas/root-directory)
  (setq yas/wrap-around-region t)
  (setq yas/prompt-functions '(yas/x-prompt yas/ido-prompt))
  (yas/global-mode 1) ;;  make it global
  (add-to-list 'auto-mode-alist '("~/.emacs.d/yas/.*" . snippet-mode)))
(add-hook 'snippet-mode-hook 'yas/minor-mode)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ultimate smart-tab.... from: http://gist.github.com/215930
;; Change the default hippie-expand order and add yasnippet to the front.
;; by-default, we only try the hippie expand, but we override the function
;; list for specific buffers...
(setq hippie-expand-try-functions-list
      '(yas/hippie-try-expand))
;;        try-expand-dabbrev
;;        try-expand-dabbrev-all-buffers
;;        try-expand-dabbrev-from-kill
;;        try-complete-file-name
;;        try-complete-lisp-symbol))
;;
;; Helps when debugging which try-function expanded
(setq hippie-expand-verbose t)

;;; tabkey2, the ultimate smart-tab...
;;(when (require 'tabkey2 nil 'noerror)
;;  (tabkey2-mode t))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; from: http://www.emacswiki.org/emacs/SlickCopy
(defadvice kill-ring-save (before slick-copy activate compile)
  "When called interactively with no active region, copy a single
line instead."
  (interactive
    (if mark-active (list (region-beginning) (region-end))
      (message "Copied line")
      (list (line-beginning-position)
               (line-beginning-position 2)))))

(defadvice kill-region (before slick-cut activate compile)
  "When called interactively with no active region, kill a single
line instead."
  (interactive
    (if mark-active (list (region-beginning) (region-end))
      (list (line-beginning-position)
        (line-beginning-position 2)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; custom menu; http://emacs-fu.blogspot.com/2009/04/adding-custom-menus.html
(easy-menu-define djcb-menu global-map "MyMenu"
  '("djcb"
     ("Programs" ;; submenu
       ["mutt"  (djcb-term-start-or-switch "mutt" t)]
       ["mc"    (djcb-term-start-or-switch "mc" t)]
       ["htop"  (djcb-term-start-or-switch "htop" t)]
       ["iotop" (djcb-term-start-or-switch "iotop" t)])

     ("Org"
       ["html"  (org-export-as-html 3 nil nil nil t)])

     ("TeXDrive"  :visible (or (string= major-mode "html-helper-mode") 
                             (string= major-mode "html-mode"))
       ["Insert formula"   texdrive-insert-formula 
         :help "Insert some formula"]
       ["Generate images"  texdrive-generate-images 
         :help "(Re)generate the images for the formulae"])
     ("Twitter" ;; submenu
       ["View friends" twitter-get-friends-timeline]
       ["What are you doing?" twitter-status-edit])

     ("Misc"  ;; submenu
       ["Save & exit" (save-buffers-kill-emacs t)]
       ["Count words" djcb-count-words]
       ["Show/hide line numbers" linum]
       ["Toggle full-screen" djcb-fullscreen-toggle])))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ido makes competing buffers and ffinding files easier
;; http://www.emacswiki.org/cgi-bin/wiki/InteractivelyDoThings
(require 'ido) 
(ido-mode 'both) ;; for buffers and files
(setq 
  ido-save-directory-list-file "~/.emacs.d/cache/ido.last"
  ido-ignore-buffers ;; ignore these guys
  '("\\` " "^\*Mess" "^\*Back" ".*Completion" "^\*Ido" "^\*trace"
     "^\*compilation" "^\*GTAGS" "^session\.*" "^\*")
  ido-work-directory-list '("~/" "~/Desktop" "~/Documents" "~src")
  ido-case-fold  t                 ; be case-insensitive
  ido-enable-last-directory-history t ; remember last used dirs
  ido-max-work-directory-list 30   ; should be enough
  ido-max-work-file-list      50   ; remember many
  ido-use-filename-at-point nil    ; don't use filename at point (annoying)
  ido-use-url-at-point nil         ; don't use url at point (annoying)
  ido-enable-flex-matching nil     ; don't try to be too smart
  ido-max-prospects 8              ; don't spam my minibuffer
  ido-confirm-unique-completion t) ; wait for RET, even with unique completion

;; when using ido, the confirmation is rather annoying...
 (setq confirm-nonexistent-file-or-buffer nil)

;; increase minibuffer size when ido completion is active
(add-hook 'ido-minibuffer-setup-hook 
  (function
    (lambda ()
      (make-local-variable 'resize-minibuffer-window-max-height)
      (setq resize-minibuffer-window-max-height 1))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; spelling
(setq ispell-program-name "aspell"
  ispell-extra-args '("--sug-mode=ultra"))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; macros to save me some type creating keyboard macros
(defmacro set-key-func (key expr)
  "macro to save me typing"
  (list 'local-set-key (list 'kbd key) 
        (list 'lambda nil 
              (list 'interactive nil) expr)))
(defmacro set-key (key str) (list 'local-set-key (list 'kbd key) str))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; org-mode
(setq org-directory "~/.emacs.d/org/")
(require 'org-mode-init nil 'noerror)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;text-mode
(add-hook 'text-mode-hook
  (lambda() 
    (set-fill-column 78)                    ; lines are 78 chars long ...
    (auto-fill-mode t)                      ; ... and wrapped around 
    (set-input-method "latin-1-prefix")))    ; make " + e => e-umlaut etc.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; w3m / browsing
(setq w3m-init-file "~/.emacs.d/mylisp/djcb-w3m.el")

(if (file-exists-p "/usr/bin/conkeror")
  (setq browse-url-browser-function 'browse-url-generic
    browse-url-generic-program "/usr/bin/conkeror"
     browse-url-default-browser "/usr/bin/conkeror")
  (setq
    browse-url-browser-function 'browse-url-default-browser))
(setq browse-url-new-window-flag t)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ERC, the emacs IRC client
(when (require 'erc nil 'noerror)
  (require 'djcb-erc nil 'noerror))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; htmlize; http://fly.cc.fer.hr/~hniksic/emacs/htmlize.el.html
(autoload 'htmlize-region "htmlize" "htmlize the region" t)
(autoload 'htmlize-buffer "htmlize" "htmlize the buffer" t)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; email / news
(add-hook 'post-mode-hook
  (lambda()
    (auto-fill-mode t)
    (setq fill-column 72)    ; rfc 1855 for usenet
    (set-input-method "latin-1-prefix")    ; make " + e => e-umlaut etc.
    (turn-on-orgstruct)      ; enable org-mode-style structure editing
    (require 'boxquote)) nil 'noerror) ; put text in boxes

(autoload 'post-mode "post" "mode for e-mail" t)
(add-to-list 'auto-mode-alist  
  '("\\.*mutt-*\\|.article\\|\\.followup" . post-mode)) 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; wanderlust && bbdb
(setq wl-init-file "~/.emacs.d/wl/wl-init.el")
(autoload 'wl "wl" "Wanderlust" t)
(autoload 'wl-draft "wl-draft" "Write draft with Wanderlust." t)
(require 'mime-w3m nil 'noerror) ;; use W3M for HTML-email
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; html/html-helper mode
;; my handy stuff for both html-helper and x(ht)ml mode
(add-hook 'html-helper-mode-hook
  (lambda()
    (abbrev-mode t)             ; support abbrevs
    (auto-fill-mode -1)         ; don't do auto-filling
    ;; my own texdrive, for including TeX formulae
    ;; http://www.djcbsoftware.nl/code/texdrive/
    (when (require 'texdrive) (texdrive-mode t))) nil 'noerror)
(setq auto-mode-alist (cons '("\\.html$" . html-helper-mode) auto-mode-alist))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; TeX/LaTex
(defun djcb-tex-mode-hook ()
  "my TeX/LaTeX (auctex) settings"
  (interactive)
  (setq
    TeX-brace-indent-level 0 ;; don't screw up \index
    LaTeX-item-ident       2
    TeX-parse-self         t ; Enable parse on load.
    TeX-auto-save          t)) ; Enable parse on save.
  
(add-hook 'tex-mode-hook 'djcb-tex-mode-hook)
(add-hook 'LaTeX-mode-hook 'djcb-tex-mode-hook)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Elisp
(add-hook 'emacs-lisp-mode-hook 
  (lambda()
    (setq mode-name "ELisp")
    (local-set-key (kbd "C-<f7>") ;; overrides global C-f7 (compile) 
      '(lambda()(interactive) 
         (let ((debug-on-error t)) 
           (eval-buffer)
           (message "buffer evaluated")))) ; 

    ;; complete lisp symbols as well
    (make-local-variable 'hippie-expand-try-functions-list)
    (setq hippie-expand-try-functions-list
      '(yas/hippie-try-expand
        try-complete-lisp-symbol))

    (linum-mode t)
    (setq lisp-indent-offset 2) ; indent with two spaces, enough for lisp
    (require 'folding nil 'noerror)
    (font-lock-add-keywords nil '(("^[^\n]\\{80\\}\\(.*\\)$"
                                    1 font-lock-warning-face prepend)))
    (font-lock-add-keywords nil 
      '(("\\<\\(FIXME\\|TODO\\|XXX+\\|BUG\\)" 
          1 font-lock-warning-face prepend)))  
    (font-lock-add-keywords nil 
      '(("\\<\\(djcb-require-maybe\\|add-hook\\|setq\\)" 
          1 font-lock-keyword-face prepend)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; perl/cperl mode
(defalias 'perl-mode 'cperl-mode) ; cperl mode is what we want
(add-hook 'cperl-mode-hook
  (lambda()
    (eval-when-compile (require 'cperl-mode))
    (abbrev-mode -1)                 ; turn-off the annoying elecric crap
    (linum-mode 1)                   ; show line numbers 
    (setq 
      cperl-hairy t                  ; parse hairy perl constructs
      cperl-indent-level 4           ; indent with 4 positions
      cperl-invalid-face nil        ; don't show stupid underlines
      cperl-electric-keywords t)))   ; complete keywords
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 
;; gtags
(add-hook 'gtags-mode-hook 
  (lambda()
    (local-set-key (kbd "M-.")   'gtags-find-tag)   ; find a tag, also M-.
    (local-set-key (kbd "M-,")   'gtags-find-rtag)  ; reverse tag
    (local-set-key (kbd "C-c n") 'gtags-pop-stack)
    (local-set-key (kbd "C-c p") 'gtags-find-pattern)
    (local-set-key (kbd "C-c g") 'gtags-find-with-grep)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; c-mode / c++-mode

(defun djcb-c-mode-common ()
  (interactive) 
  (linum-mode 1)                                ; show line numbers

  (when (fboundp 'yas/minor-mode)               ; use Yasnippet...      
    (yas/minor-mode 1))                         ; when available
  
  (when (require 'dtrt-indent nil 'noerror)     ; try to guess indentation 
    (dtrt-indent-mode t))                       ; style
  
  (when (require 'gtags nil 'noerror)           ; enable gnu-global (gtags nil 'noerror)
    (gtags-mode t)
    (when (fboundp 'djcb-gtags-create-or-update)
      (djcb-gtags-create-or-update)))           ; auto-update tags
  
  (when (require 'doxymacs nil 'noerror)                ; enable doxygen
    (doxymacs-mode t)                           ;
    (doxymacs-font-lock))                       ;

  (when (fbound 'company-mode)
    (company-mode t))
  
  (if (fboundp 'tabkey2-mode)                   ; turn off in c-mode
    (tabkey2-mode -1))                          ; it's annoying
  
  (font-lock-add-keywords nil                   ; add some extra 
    '(("\\<\\(FIXME\\|TODO\\|XXX+\\|BUG\\)"     ; colorful
        1 font-lock-warning-face prepend)))     ; keywords
  (font-lock-add-keywords nil                   ;
    '(("\\<\\(__FUNCTION__\\|__LINE__\\)"       ;
        1 font-lock-preprocessor-face prepend)));
  
  (setq
    compilation-scroll-output 'first-error      ; scroll until first error
    compilation-read-command nil                ; don't need enter
    compilation-window-height 12)               ; keep it readable

  (setq
    c-lineup-close-paren t                      ;
    
    c-basic-offset 8                            ; linux kernel style
    c-syntactic-indentation t                   ; indent smartly
    c-hungry-delete-key t)                      ; eat as much as possible
  
  (setq-default c-electric-flag t)              ; turn off, it's annoying

   ;; keybindings
  (local-set-key (kbd "RET")        'c-context-line-break)

  (local-set-key (kbd "C-c <up>")   'previous-error) 
  (local-set-key (kbd "C-c <down>") 'next-error)
  
  ;; or
  (local-set-key (kbd "<C-XF86Back>")   'previous-error) 
  (local-set-key (kbd "<C-XF86Forward>") 'next-error)
  
  (local-set-key (kbd "C-c <") (lambda()(interactive) (align nil nil)))
  
  (local-set-key (kbd "C-c i")      'djcb-include-guards)  
  (local-set-key (kbd "C-c o")      'ff-find-other-file)
  (local-set-key (kbd "C-h C-j")    'gtk-lookup-symbol)

  (c-set-style "linux"))

(defun djcb-c-mode ()
  ;; warn when lines are > 80 characters (in c-mode)
  (font-lock-add-keywords 'c++-mode  '(("^[^\n]\\{80\\}\\(.*\\)$"
                                         1 font-lock-warning-face prepend))))

(defun djcb-c++-mode ()
  ;; warn when lines are > 100 characters (in c++-mode)
  (font-lock-add-keywords 'c++-mode  '(("^[^\n]\\{100\\}\\(.*\\)$"
                                         1 font-lock-warning-face prepend))))

(add-hook 'c-mode-common-hook 'djcb-c-mode-common) ; run before all c-modes
(add-hook 'c-mode-hook 'djcb-c-mode)               ; run before c mode
(add-hook 'c++-mode-hook 'djcb-c++-mode)           ; run before c++ mode

(add-hook 'magit-mode-hook
  (lambda()
    (local-set-key (kbd "<tab>") 'magit-toggle-section)
    (setq magit-save-some-buffers 'dontask)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;  Makefiles
(add-hook 'makefile-mode-hook
  (lambda()
    (require 'show-wspace)
    (show-ws-highlight-trailing-whitespace)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; compilation; if compilation is successful, autoclose the compilation win
;; http://www.emacswiki.org/cgi-bin/wiki/ModeCompile
;; TODO: don't hide when there are warnings either (not just errors)
(setq compilation-window-height 12)
(setq compilation-finish-functions nil) ;; keep it open
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; customization for term, ansi-term
;; disable cua and transient mark modes in term-char-mode
;; http://www.emacswiki.org/emacs/AnsiTermHints
;; remember: Term-mode remaps C-x to C-c
(defadvice term-char-mode (after term-char-mode-fixes ())
  (set (make-local-variable 'cua-mode) nil)
  (set (make-local-variable 'transient-mark-mode) nil)
  (set (make-local-variable 'global-hl-line-mode) nil)
  (ad-activate 'term-char-mode)
  (term-set-escape-char ?\C-x))

(add-hook 'term-mode-hook 
  (lambda()
    (when (fboundp 'tabkey2-mode)
      (tabkey2-mode -1))
    (local-set-key [(tab)] nil)
    (local-set-key (kbd "<C-f1>") 
      '(lambda()(interactive)
         (shell-command "killall -SIGWINCH mutt slrn irssi zsh")))))    
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; twitter: http://www.busydoingnothing.co.uk/twitter-el/
(autoload 'twitter-get-friends-timeline "twitter" nil t)
(autoload 'twitter-status-edit "twitter" nil t)
(add-hook 'twitter-status-edit-mode-hook 'longlines-mode)
;; identi.ca: 
(autoload 'identica-mode "identica-mode" nil t)
(setq identica-username "djcb")
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; safe locals; we mark these as 'safe', so emacs22+ won't give us annoying
;; warnings
(setq safe-local-variable-values
      (quote ((auto-recompile . t)
              (outline-minor-mode . t)
              auto-recompile outline-minor-mode)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;