;; p4bl0's .emacs
(setq user-full-name "Pablo Rauzy"
      user-mail-address (concat "r@" "uzy" ".me"))

;; I use the Meta (Windows) key extensively for OpenBox
;(setq x-alt-keysym 'meta)
;(setq x-meta-keysym 'alt)
(setq x-super-keysym 'alt)

;; cool frame title with currently edited buffer name
(setq frame-title-format
      (concat "%b - " invocation-name "@" system-name))

;; add my .emacs.d and ~/local/share to load-path
(setq home-emacs-dir (concat (getenv "HOME") "/.emacs.d/"))
(setq local-emacs-dir (concat (getenv "HOME") "/local/share/emacs/site-lisp/"))
(setq my-load-path (list home-emacs-dir local-emacs-dir
                         (concat local-emacs-dir "mu4e/")))
(setq load-path (append my-load-path load-path))

;; synchronize exec-path and $PATH
(let ((path-from-shell
        "[ \t\n]*$" ""
        (shell-command-to-string "QUICKSHELL=1 $SHELL -l -i -c 'echo $PATH' 2>/dev/null"))))
  (setenv "PATH" path-from-shell)
  (setq exec-path (split-string path-from-shell path-separator)))

;; package
(require 'package)
(add-to-list 'package-archives
             '("marmalade" . "http://marmalade-repo.org/packages/"))
(add-to-list 'package-archives
             '("melpa" . "http://melpa.milkbox.net/packages/"))

;; utf-8 powaa!!
(set-language-environment 'utf-8)
(prefer-coding-system 'utf-8)
(set-terminal-coding-system 'utf-8)
(set-keyboard-coding-system 'utf-8)
(setq locale-coding-system 'utf-8)
(set-selection-coding-system 'utf-8)
(set-input-method nil)

;; no annoying blinking cursor
(blink-cursor-mode -1)

;; fancy streching cursor
(setq x-stretch-cursor t)

;; usual behavior when text is selected
(delete-selection-mode t)

;; default fill column is 70, why?
(setq default-fill-column 79)

;; my sentences end with a dot, not with two spaces
(setq sentence-end-double-space nil)

;; automatically indent wherever I am
(global-set-key (kbd "RET") 'newline-and-indent)

;; kill whole line with C-; (because ; is close to k)
(global-set-key (kbd "C-;") 'kill-whole-line)

;; replace-string and replace-regexp need a key binding
(global-set-key (kbd "C-c s") 'replace-string)
(global-set-key (kbd "C-c r") 'replace-regexp)

;; string-insert-rectangle is useful but not binded to any key by default
(global-set-key (kbd "C-x r a") 'string-insert-rectangle)

;; rect-missing-features is my first package :)
(load "rect-missing-features")
(global-set-key (kbd "C-x r z") 'string-insert-after-rectangle)
(global-set-key (kbd "C-x r e") 'string-insert-rectangle-eol)

;; keybindings for useful function
(global-set-key (kbd "C-c w") 'delete-trailing-whitespace)
(global-set-key (kbd "C-c a") 'align-entire)
(global-set-key (kbd "C-c A") 'align-string)
(global-set-key (kbd "C-c f") 'font-lock-fontify-buffer)
(global-set-key (kbd "C-c b") 'revert-buffer)
(global-set-key (kbd "C-c h H") 'hs-hide-all)
(global-set-key (kbd "C-c h S") 'hs-show-all)
(global-set-key (kbd "C-c h h") 'hs-hide-block)
(global-set-key (kbd "C-c h s") 'hs-show-block)
(global-set-key (kbd "C-c h t") 'hs-toggle-hiding)

;; enable disabled command
(put 'narrow-to-region 'disabled nil)
(put 'upcase-region 'disabled nil)
(put 'downcase-region 'disabled nil)

;; geiser
(require 'geiser-install)
(setq geiser-active-implementations '(racket)
      geiser-repl-history-filename (concat home-emacs-dir "geiser-history")
      geiser-mode-smart-tab-p t)
(defun jao-racket-help (file)
  (w3m (url-unhex-string file)))

;; merlin
; Add opam emacs directory to the load-path
(setq opam-share (substring (shell-command-to-string "opam config var share 2> /dev/null") 0 -1))
(add-to-list 'load-path (concat opam-share "/emacs/site-lisp"))
; Load merlin-mode
(require 'merlin)
; Start merlin on ocaml files
(add-hook 'tuareg-mode-hook 'merlin-mode t)
(add-hook 'caml-mode-hook 'merlin-mode t)
; Enable auto-complete
(setq merlin-use-auto-complete-mode 'easy)
; Use opam switch to lookup ocamlmerlin binary
(setq merlin-command 'opam)

;; tetris score file
(setq tetris-score-file (concat home-emacs-dir "tetris"))

;; in a text editor, everything is (at least) text
(setq default-major-mode 'text-mode)

;; colorize hexa color in css
(add-hook 'css-mode-hook 'rainbow-mode)

;; colorize number too (like constant)
(defun font-lock-fontify-numbers ()
  "Use this function as a hook to fontify numbers as constant"
  (font-lock-add-keywords nil
      '(("\\b\\(0x[0-9a-fA-F]+\\)" 1 font-lock-constant-face) ; hexa
        ("\\b\\(0b[01]+\\)" 1 font-lock-constant-face) ; bin
        ("\\b\\(-?[0-9]+\\.[0-9]+\\(d\\|f\\)?\\)" 1
         font-lock-constant-face) ; float
        ("[\`^\(){\[,\+\\-\\*/\%><=\s-]\\(-?[0-9]+U?L?L?\\)" 1
         font-lock-constant-face)))) ; int
;(add-hook 'font-lock-mode-hook 'font-lock-fontify-numbers)
(add-hook 'c-mode-hook 'font-lock-fontify-numbers)
(add-hook 'c++-mode-hook 'font-lock-fontify-numbers)
(add-hook 'scheme-mode-hook 'font-lock-fontify-numbers)
(add-hook 'tuareg-mode-hook 'font-lock-fontify-numbers)
(add-hook 'lisp-mode-hook 'font-lock-fontify-numbers)
(add-hook 'lisp-interaction-mode-hook 'font-lock-fontify-numbers)
(add-hook 'emacs-lisp-mode-hook 'font-lock-fontify-numbers)
(add-hook 'js-mode-hook 'font-lock-fontify-numbers)
(add-hook 'html-mode-hook 'font-lock-fontify-numbers)
(add-hook 'xml-mode-hook 'font-lock-fontify-numbers)
(add-hook 'css-mode-hook 'font-lock-fontify-numbers)
(add-hook 'perl-mode-hook 'font-lock-fontify-numbers)
(add-hook 'php-mode-hook 'font-lock-fontify-numbers)
(add-hook 'sh-mode-hook 'font-lock-fontify-numbers)

;; font-lock setting
(if (>= emacs-major-version 23)
    (set-default-font "DejaVu Sans Mono-8.5")) ; .Xdefaults
(global-font-lock-mode t)
(show-paren-mode t)
(transient-mark-mode t)
(set-face-background 'default "#333333") ; .Xdefaults
(set-face-foreground 'default "#cccccc") ; .Xdefaults
(set-face-background 'cursor "#ffaa00") ; .Xdefaults
(set-face-foreground 'cursor "#333333") ; .Xdefaults
(setq x-pointer-foreground-color "#aaaaaa") ; .Xdefaults
(set-face-background 'region "#33aaff")
(set-face-foreground 'region "#eeeeee")
(set-face-background 'show-paren-match-face "#666666")
(set-face-foreground 'show-paren-match-face "#ffffff")
(set-face-background 'show-paren-mismatch-face "#dd0000")
(set-face-foreground 'show-paren-mismatch-face "#333333")
(set-face-foreground 'font-lock-warning-face "#dd0000")
(set-face-foreground 'font-lock-preprocessor-face "#99cc33")
(set-face-foreground 'font-lock-constant-face "#dd00dd")
(set-face-foreground 'font-lock-comment-face "#888888")
(set-face-foreground 'font-lock-keyword-face "#dddd00")
(set-face-foreground 'font-lock-type-face "#ffaa00")
(set-face-foreground 'font-lock-function-name-face "#2277dd")
(set-face-foreground 'font-lock-string-face "#33aaff")
(set-face-foreground 'font-lock-variable-name-face "#00bb00")
;(global-hl-line-mode t)
;(set-face-background 'hl-line "#3a3a3a")
;(set-face-foreground 'hl-line "#eeeeee")
(set-face-foreground 'highlight "#333333")
(set-face-background 'highlight "#dddd00")
(set-face-foreground 'isearch "#333333")
(set-face-background 'isearch "#bbbb22")
(set-face-foreground 'isearch-lazy-highlight-face "#333333")
(set-face-background 'isearch-lazy-highlight-face "#bbbb66")
(set-face-background 'mode-line "#aaaaaa")
(set-face-foreground 'mode-line "#333333")
(set-face-foreground 'link "#4466ff")
(set-face-foreground 'link-visited "#aa33bb")
(set-face-foreground 'minibuffer-prompt "#bababa")
(set-face-foreground 'escape-glyph "#ccaa11")

;; tab = two spaces
(setq default-tab-width 2)
(setq-default tab-width 2)
(setq-default indent-tabs-mode nil)
(setq css-indent-offset 2)
(setq-default sh-basic-offset 2)
(setq-default sh-indentation 2)
(setq-default perl-indent-level 2)
(setq-default js-indent-level 2)
(setq-default python-indent 2)

;; set the random number seed from current time and pid
(random t)

;; kill the splash screen before its birth
(setq inhibit-splash-screen t)

;; get rid of the useless tool-bar and menu-bar
(tool-bar-mode -1) ; .Xdefaults
(menu-bar-mode -1) ; .Xdefaults

;; but give the emacs window a still good shape !
(setq initial-frame-alist '((width . 90) (height . 45))) ; .Xdefaults

;; scroll bar on the right side
(set-scroll-bar-mode 'right)

;; show column number in mode-line
(setq column-number-mode t)

;; line numbering with linum (WTF it's not activated by defaults?!)
(require 'linum)
(global-linum-mode 1)
(setq linum-disabled-modes-list '(mu4e-headers-mode))
(defun linum-on ()
  (unless (or (minibufferp) (member major-mode linum-disabled-modes-list))
    (linum-mode 1)))

;; texdrive for math formulae in html
(require 'texdrive)

;; Ask before killing `emacs --daemon`.
(global-set-key (kbd "C-x C-c")
    (lambda () (interactive)
      (cond ((y-or-n-p "Quit? ")

;; Often typing C-z instead of C-a ou C-e
(global-set-key (kbd "C-z")
   (lambda () (interactive)
     (message "Did you mean C-a or C-e? If not use C-Z."))))
(global-set-key (kbd "C-S-z") 'suspend-frame)

;; C-x 5 0 is really not handy on a laptop
(global-set-key (kbd "C-x w") 'delete-frame)

;; y or n ?
(fset 'yes-or-no-p 'y-or-n-p)

;; and no boring gtk+ dialog
(setq use-dialog-box nil)

;; Emacs is not Ed !
(setq window-min-height 3)

;; middle click should pastes at cursor position
(setq mouse-yank-at-point t)

;; recursively find all projects (directory with a .git inside)
(require 'cl) ; needed on some install to be able to use reduce...
(defun find-projects (dir)
  (let ((dir (if (string= (substring dir -1 nil) "/")
                 dir (concat dir "/"))))
    (if (not (file-directory-p dir))
      (reduce (lambda (list prj)
                (let ((prj-dir (concat dir prj)))
                  (if (not (file-exists-p (concat prj-dir "/.hidden")))
                       ((null prj) nil)
                       ((file-exists-p (concat prj-dir "/.git"))
                        (cons `(,prj . ,prj-dir) list))
                       ((and (file-directory-p prj-dir)
                             (not (string= prj ".."))
                             (not (string= prj ".")))
                        (append (find-projects prj-dir) list))
                       (t list))
              (directory-files dir)
              :initial-value nil))))

;; generate list of projects for ibuffer
(defun make-ibuffer-projects-list (prefix dir)
  (reduce (lambda (list prj)
             `(,(concat prefix (car prj)) (filename . ,(cdr prj)))
          (find-projects dir)
          :initial-value nil))

;; ibuffer
(require 'ibuffer)
(setq ibuffer-saved-filter-groups
         ,@(make-ibuffer-projects-list "Research: "
                                       (concat (getenv "HOME") "/research/"))
         ,@(make-ibuffer-projects-list "Project: "
                                       (concat (getenv "HOME") "/projects/"))
         ,@(make-ibuffer-projects-list "Web: "
                                       (concat (getenv "HOME") "/www/"))
          (filename . "/.emacs.d/"))
           (mode . c-mode)
           (mode . c++-mode)
           (mode . perl-mode)
           (mode . python-mode)
           (mode . ruby-mode)
           (mode . emacs-lisp-mode)
           (mode . lisp-mode)
           (mode . sh-mode)
           (mode . php-mode)
           (mode . xml-mode)
           (mode . html-mode)
           (mode . css-mode)
           (mode . js-mode)))
           (mode . message-mode)
           (mode . mail-mode)
           (mode . mu4e-main-mode)
           (mode . mu4e-headers-mode)
           (mode . mu4e-view-mode)
           (mode . mu4e-compose-mode)))
           (mode . erc-mode)
           (mode . identica-mode)
           (mode . twitter-mode)))
          (mode . dired-mode)))))
(setq ibuffer-show-empty-filter-groups nil)
(add-hook 'ibuffer-mode-hook
          (lambda ()
            (ibuffer-switch-to-saved-filter-groups "default")))
(global-set-key (kbd "C-x C-b") 'ibuffer)

;; C-tab to go through buffer like everywhere else
(global-set-key [C-tab] 'next-buffer)
(global-set-key [C-S-iso-lefttab] 'previous-buffer)

;; S-tab to indent and then move to the first non blank char
;(global-set-key [backtab]
;                (function
;                 (lambda ()
;                   (interactive)
;                   (indent-for-tab-command)
;                   (move-beginning-of-line nil)
;                   (forward-to-indentation 0))))
;okay this was (back-to-indentation) and already bound to M-m... <3 emacs.

;; use M-{up,right,down,left} for windmove
(windmove-default-keybindings 'meta)

;; fullscreen emacs !
; I don't need it anymore since i added the command to my Openbox (W-F11)
;(defun fullscreen-toggle ()
; "Toogle fullscreen"
; (interactive)
; (shell-command "wmctrl -r :ACTIVE: -b toggle,fullscreen"))
;(global-set-key (kbd "<f11>") 'fullscreen-toggle)

;; C-x k == C-x # when editing emacsclient is waiting
(add-hook 'server-switch-hook
  (lambda ()
    (local-set-key (kbd "C-x k")
      '(lambda ()
         (if server-buffer-clients

;; revert-all-buffer from emacswiki
(defun revert-all-buffers ()
  "Revert all opened buffers from their respective files"
  (let* ((list (buffer-list))
         (buffer (car list)))
    (while buffer
      (when (buffer-file-name buffer)
          (set-buffer buffer)
          (revert-buffer t t t)))
      (setq list (cdr list))
      (setq buffer (car list))))
  (message "All buffers reverted"))
(global-set-key (kbd "C-c B") 'revert-all-buffers)

;; browse kill ring
(require 'browse-kill-ring)
(global-set-key (kbd "C-x y") 'browse-kill-ring)

;; keep minibuffer history between session
(savehist-mode t)

;; autocompletion
(global-set-key (kbd "ESC ESC") 'dabbrev-expand) ; ESC ESC ESC not usable :-/

;(require 'autopair)
(setq autopair-blink nil)
(setq autopair-autowrap t)
(put 'autopair-insert-opening 'delete-selection t)
(put 'autopair-skip-close-maybe 'delete-selection t)
(put 'autopair-insert-or-skip-quote 'delete-selection t)
(put 'autopair-extra-insert-opening 'delete-selection t)
(put 'autopair-extra-skip-close-maybe 'delete-selection t)
(put 'autopair-backspace 'delete-selection 'supersede)
(put 'autopair-newline 'delete-selection t)

;; textmate-next-line from textmate.el - github.com/defunkt/textmate.el
(defun textmate-next-line ()
  "Go to next line and indent wherever you are in a line"
(global-set-key [C-return] 'textmate-next-line)

;; comment-or-uncomment-region-or-line
; it's almost the same as in textmate.el but I wrote it before I know about
; textmate.el, in fact that's how I found textmate.el, by googling this
; function to see if somebody already did that in a better way than me.
(defun comment-or-uncomment-region-or-line ()
  "Like comment-or-uncomment-region, but if there's no mark \(that means no
region\) apply comment-or-uncomment to the current line"
  (if (not mark-active)
        (line-beginning-position) (line-end-position))
      (if (< (point) (mark))
          (comment-or-uncomment-region (point) (mark))
        (comment-or-uncomment-region (mark) (point)))))
(global-set-key (kbd "C-/") 'comment-or-uncomment-region-or-line)

;; embedded php in html
;; (defun toggle-php-and-html-mode ()
;;   "Toggle between php-mode and html-mode"
;;   (interactive)
;;   (if (eq major-mode 'html-mode)
;;       (php-mode)
;;     (html-mode)))
;; (global-set-key (kbd "C-c g") 'toggle-php-and-html-mode)

;; toggle artiste-mode
(defun artist-mode-toggle ()
  "Toggle artist-mode"
  (if (eq major-mode 'artist-mode)
(global-set-key (kbd "C-c d") 'artist-mode-toggle)

;; find file as root
(defun djcb-find-file-as-root ()
  "Like `ido-find-file, but automatically edit the file with
root-privileges (using tramp/sudo), if the file is not writable by
  (let ((file (ido-read-file-name "Edit as root: ")))
    (unless (file-writable-p file)
      (setq file (concat "/sudo:root@localhost:" file)))
    (find-file file)))
(global-set-key (kbd "C-x C-S-f") 'djcb-find-file-as-root)

;; dired should reuse its buffer
(put 'dired-find-alternate-file 'disabled nil)
(fset 'dired-find-file 'dired-find-alternate-file)
(fset 'dired-advertised-find-file 'dired-find-alternate-file)

;; uniquify!
(require 'uniquify)
(setq uniquify-buffer-name-style 'reverse)
(setq uniquify-separator "|")
(setq uniquify-after-kill-buffer-p t)
(setq uniquify-ignore-buffers-re "^\\*")

;; yasnippet!
(add-to-list 'load-path
             (car (file-expand-wildcards
                   (concat home-emacs-dir "elpa/yasnippet-*"))))
(require 'yasnippet)
(define-key yas-minor-mode-map (kbd "<tab>") nil)
(define-key yas-minor-mode-map (kbd "TAB") nil)
(define-key yas-minor-mode-map (kbd "œ") 'yas-expand)
(yas-global-mode 1)

;; ido! is fantastic
(require 'ido)
(ido-mode t)
(setq ido-enable-flex-matching t)

;; clean the modeline
(require 'diminish)
(eval-after-load 'abbrev
  '(diminish 'abbrev-mode " Ab"))
(eval-after-load 'autopair
  '(diminish 'autopair-mode " P"))
(eval-after-load 'rainbow-mode
  '(diminish 'rainbow-mode " Rb"))
;(eval-after-load 'yasnippet
;  '(diminish 'yas/minor-mode " Y"))

;; iedit
(setq-default iedit-toggle-key-default (kbd "C-:"))

;; scrolling settings
(setq scroll-preserve-screen-position t)
(setq scroll-margin 2)
(setq scroll-step 1)

;; files should always end with a new line
(setq require-final-newline t)

;; tramp ça claque (really bad french play-on-word)
(setq tramp-default-method "scp")

;; scratch buffer for any mode
;(require 'scratch)
(global-set-key (kbd "C-c t") 'scratch)

;; web dev
(add-to-list 'auto-mode-alist '("\\.php[345]?\\'\\|\\.inc\\'" . php-mode))
(add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode))
(add-to-list 'auto-mode-alist
             '("\\.p?html\\(\\.[a-z]\\{2\\}\\)?\\'" . html-mode))

;; latex
(setq TeX-PDF-mode t)
(setq TeX-auto-save t)
(setq TeX-parse-self t)
(setq-default TeX-master nil)
(setq TeX-source-correlate-method 'synctex)
(add-hook 'LaTeX-mode-hook 'TeX-PDF-mode)
(add-hook 'LaTeX-mode-hook 'TeX-source-correlate-mode)
(add-hook 'LaTeX-mode-hook 'visual-line-mode)
(add-hook 'LaTeX-mode-hook 'flyspell-mode)
(add-hook 'LaTeX-mode-hook 'LaTeX-math-mode)
(add-hook 'LaTeX-mode-hook
          (lambda () (ispell-change-dictionary "en")))

;; disable flyspell annoying feature
;(fset 'flyspell-auto-correct-previous-word 'kill-whole-line)
(setq-default flyspell-auto-correct-binding (kbd "C-!"))

;; outline-mode
(add-to-list 'auto-mode-alist '("\\.outline\\'" . outline-mode))
(require 'outline-presentation)
(add-hook 'outline-presentation-mode-hook
          (lambda () (text-scale-increase 3)))

;; emails
(require 'mu4e)
(global-set-key (kbd "C-x m") 'mu4e)
(require 'smtpmail)
 user-mail-address-telecom (concat "pablo.rau" "zy@telecom-p" "aristech.fr")
 user-mail-address-enst (concat "rau" "zy@e" "nst.fr")
 ;; user-mail-address-upmc (concat "pablo.ra" "uzy@etu.u" "pmc.fr")
 ;; user-mail-address-ens (concat "pablo.r" "auzy@" "ens.fr")
 ;; user-mail-address-di (concat "rauz" "y@di.e" "ns.fr")
 ;; user-mail-address-clipper (concat "rauzy@c" "lipper.en" "s.fr")
 user-mail-address-normale (concat "pablo.r" "auzy@no" "rmale.fr")
 user-mail-address-m4n (concat "pr@m" "4n.fr")
 user-mail-address-grotas (concat "grotabl" "o@grot" "as.fr")
 user-mail-address-uzy user-mail-address)
 mu4e-maildir "~/mails"
 mu4e-sent-folder "/_sent"
 mu4e-drafts-folder "/_drafts"
 mu4e-trash-folder "/_trash"
 mu4e-user-mail-address-list (list user-mail-address-telecom
 mu4e-get-mail-command "offlineimap -a berthold"
 mu4e-headers-date-format "%Y-%m-%d %H:%M:%S"
 mu4e-headers-fields '((:date . 20)
                       (:flags . 5)
                       (:from-or-to . 25)
                       (:subject . nil))
 mu4e-bookmarks '(("flag:unread AND NOT flag:trashed" "Unread messages"  ?u)
                  ("flag:flagged"                     "Flagged messages" ?f)
                  ("date:today..now"                  "Today's messages" ?t)
                  ("date:7d..now"                     "Last 7 days"      ?w))
 mu4e-attachment-dir (expand-file-name (concat mu4e-maildir "/_files"))
 mu4e-headers-seen-mark '("S" . "☑")
 mu4e-headers-new-mark '("N" .  "✉")
 mu4e-headers-replied-mark '("R" . "↵")
 mu4e-headers-passed-mark '("P" . "⇉")
 mu4e-headers-encrypted-mark '("x" . "⚷")
 mu4e-headers-signed-mark '("s" . "✍")
 mu4e-headers-empty-parent-prefix '("-" . "◆")
 mu4e-headers-first-child-prefix '("\\" . "▶")
 mu4e-use-fancy-chars t
 mu4e-html2text-command "w3m -T text/html -dump")
 mail-user-agent 'mu4e-user-agent
 message-signature "Pablo Rauzy.\nhttp://pablo.rauzy.name/\n"
 mu4e-compose-signature "Pablo Rauzy.\nhttp://pablo.rauzy.name/\n"
 message-send-mail-function 'smtpmail-send-it
 smtp-mail-function 'smtpmail-send-it
 message-citation-line-format "On %Y-%m-%d, %f wrote:"
 message-citation-line-function 'message-insert-formatted-citation-line
 message-send-mail-partially-limit 5000000
 message-kill-buffer-on-exit t)
 smtpmail-starttls-credentials '(("z.mines-telecom.fr" 587 nil nil))
 smtpmail-default-smtp-server "z.mines-telecom.fr"
 smtpmail-smtp-server "z.mines-telecom.fr"
 smtpmail-smtp-service 587
 smtpmail-queue-mail nil
 smtpmail-queue-dir "~/mails/_queue")
 pgg-gpg-use-agent t
 mu4e-decryption-policy 'ask)

(defun my-mu4e-choose-from ()
  "Choose the From field from user mail address list"
    (goto-char (point-min))
    (while (not (looking-at "From: ")) (forward-line))
    (insert (concat "From: " user-full-name " <>"))
     (ido-completing-read "From: " mu4e-user-mail-address-list))))
(define-key message-mode-map (kbd "C-c C-f f") 'my-mu4e-choose-from)

;; Automatically set the right From when composing
;; Adapeted from: http://www.djcbsoftware.nl/code/mu/mu4e/Compose-hooks.html
(defun my-mu4e-set-from ()
  "Set the From address based on the To address of the original."
  (let ((msg mu4e-compose-parent-message))
    (setq user-mail-address
           ((null msg) user-mail-address-uzy)
             msg :to (list user-mail-address-ens
             msg :to (list user-mail-address-telecom
             msg :to (list user-mail-address-normale
                           (concat "roo" "t@norm" "ale.fr")))
             msg :to user-mail-address-m4n)
             msg :to user-mail-address-grotas)
             msg :to user-mail-address-upmc)
           (t user-mail-address-uzy)))))
(add-hook 'mu4e-compose-pre-hook 'my-mu4e-set-from)
(add-hook 'mu4e-compose-mode-hook
          (lambda () (flyspell-mode)))

;; message view action from EmacsWiki
(defun mu4e-msgv-action-view-in-browser (msg)
  "View the body of the message in a web browser."
  (let ((html (mu4e-msg-field (mu4e-message-at-point t) :body-html))
        (tmpfile (format "%s/%d.html" temporary-file-directory (random))))
    (unless html (error "No html part for this message"))
    (with-temp-file tmpfile
       "<head><meta http-equiv=\"content-type\""
    (browse-url (concat "file://" tmpfile))))
(add-to-list 'mu4e-view-actions
             '("View in browser" . mu4e-msgv-action-view-in-browser) t)

;; easy decrypt GPG armor message for reply
(defun mu4e-gpg-armor-reply (start end)
   (progn (barf-if-buffer-read-only)
  (let ((deletions 0))
      (goto-char start)
      (while (looking-at ">")
        (delete-forward-char 2)
        (setq deletions (+ deletions 2))
    (epa-decrypt-region start (- end deletions)))
    (goto-char start)
    (while (not (looking-at "-- "))
      (if (looking-at ">")
          (insert ">")
        (insert "> "))

;; don't let Customize mess with my .emacs
(setq custom-file (concat home-emacs-dir "custom.el"))
(load custom-file 'noerror)

;; start the server for emacsclient use

;; open my todo list
(find-file "~/.memo")
(rename-buffer "*memo*")

(defadvice epg--start (around advice-epg-disable-agent disable)
  "Make epg--start not able to find a gpg-agent"
  (let ((agent (getenv "GPG_AGENT_INFO")))
    (setenv "GPG_AGENT_INFO" nil)
    (setenv "GPG_AGENT_INFO" agent)))

(defun epg-disable-agent ()
  "Make EasyPG bypass any gpg-agent"
  (ad-enable-advice 'epg--start 'around 'advice-epg-disable-agent)
  (ad-activate 'epg--start)
  (message "EasyPG gpg-agent bypassed"))

(defun epg-enable-agent ()
  "Make EasyPG use a gpg-agent after having been disabled with epg-disable-agent"
  (ad-disable-advice 'epg--start 'around 'advice-epg-disable-agent)
  (ad-activate 'epg--start)
  (message "EasyPG gpg-agent re-enabled"))