;; -*-mode: Emacs-Lisp; folding-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.

;; set the load path  

;; 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))

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

;;stuff in separate files;; maybe use autoload?
(require 'djcb-org nil 'noerror)
(require 'djcb-erc nil 'noerror)
(require 'djcb-wl nil 'noerror) ;; wl & bbdb ssetup
(require 'djcb-funcs nil 'noerror) ;; load it it can be found...
(require 'djcb-ibuffer nil 'noerror)
(require 'djcb-menu nil 'noerror)
(require 'djcb-prog nil 'noerror) ;; my programming / markup settngs

;; 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 

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

(setq ;; scrolling
  scroll-margin 0                        ;; 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+
(delete-selection-mode 1)                ;; 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

(setq initial-scratch-message
  ";; scratch buffer created -- happy hacking\n")

   (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)))))

(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

;; slick-copy: make copy-past a bit more intelligent
;; 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."
    (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."
    (if mark-active (list (region-beginning) (region-end))
      (list (line-beginning-position)
        (line-beginning-position 2)))))

;; key board / input method settings
(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

;; 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+)

(if (require 'sml-modeline nil 'noerror)    ;; use sml-modeline if available
    (sml-modeline-mode 1)                   ;; show buffer pos in the mode line
    (scroll-bar-mode -1))                   ;; turn off the scrollbar
  (scroll-bar-mode 1)                       ;; otherwise, show a scrollbar...
  (set-scroll-bar-mode 'right))             ;; ... on the right

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

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

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

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

;; http://www.emacswiki.org/cgi-bin/wiki/ShowParenMode
(when (fboundp 'show-paren-mode)
  (show-paren-mode t)
  (setq show-paren-style 'parenthesis))

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

;; 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
  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)

;; 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

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

;; some misc other packages
;; color theme
(when (require 'color-theme nil 'noerror)
  (when (require 'color-theme-init nil 'noerror)
  (if (require 'zenburn nil 'noerror)
(setq-default cursor-type '(hbar . 2))

;; the in-frame speedbar
(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"))
    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].*[#$%>] *")
  tramp-default-method "ssh"
  tramp-persistency-file-name "~/.emacs.d/cache/tramp")

;; 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 
    (local-unset-key (kbd "<tab>"))))

;; multi-term
(when (require 'multi-term nil 'noerror)
  (setq multi-term-program "/bin/zsh"))

;; w3m / browsing settings
(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")
    browse-url-browser-function 'browse-url-default-browser))
(setq browse-url-new-window-flag t)

;; ido-mode
;; http://www.emacswiki.org/cgi-bin/wiki/InteractivelyDoThings
(require 'ido) 
(ido-mode 'both) ;; for buffers and files
  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 
    (lambda ()
      (make-local-variable 'resize-minibuffer-window-max-height)
      (setq resize-minibuffer-window-max-height 1))))

;; yasnippet mode
(when (require 'yasnippet nil 'noerror) ;; not: yasnippet-bundle
  (setq yas/root-directory
       "~/.emacs.d/yas/custom")) ;; my own snippets
  (mapc '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 '("yas/.*" . snippet-mode)))

;; twitter/identica/advogato
;; 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)
(add-hook 'identica-mode-hook
  (lambda () (load-library "secrets.el.gpg")))
;; advogato
(autoload 'advogato-mode "advogato" nil t)
(add-hook 'advogato-mode-hook
  (lambda () (load-library "secrets.el.gpg")))

;; 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>")
(global-set-key (kbd "<C-next>")

;; programming
(autoload 'linum-mode "linum" "mode for line numbers" t) 
(global-set-key (kbd "C-<f5>") 'linum-mode)                 ;; line numbers
(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-c d") 'djcb-dup)

;; elscreen
(setq elscreen-prefix-key (kbd "C-c q")
  elscreen-display-tab nil
  elscreen-display-screen-number nil)
(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

(global-set-key (kbd "C-c W") ;; wanderlust
  (lambda()(interactive)(find-file wl-init-file))) 
(global-set-key (kbd "C-c O") ;; org-init
  (lambda()(interactive)(find-file "~/.emacs.d/org/djcb-org.el"))) 
(global-set-key (kbd "C-c G") ;; gtd.org
  (lambda()(interactive)(find-file (concat org-directory
(global-set-key (kbd "C-c R") ;; remember.org
  (lambda()(interactive)(find-file org-default-notes-file))) 

;; program shortcuts
(global-set-key (kbd "C-c E") ;; .emacs
  (lambda()(interactive)(find-file "~/.emacs.d/init.el")))
;; Firefox...
(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 i") 'identica-mode) ;; identi.ca
(global-set-key (kbd "C-c n") 'normal-mode)

(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)

(when (fboundp 'djcb-uber-tab) 
  (when (fboundp 'yas/trigger-key)
    (setq yas/trigger-key (kbd "C-<tab>")))
  (global-set-key (kbd "<tab>") 'djcb-uber-tab)
  (global-set-key (kbd "<C-S-iso-lefttab>") 'djcb-uber-tab))

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