   (switch-to-buffer BUFNAME)
   (delete-other-windows))
 
-(defun ska-mail-insert-answer-gap ()
-  "Insert a nicely formatted gap when answering an email."
-  (interactive)
-  (if (bolp)
-      (insert-char ?\n 1)
-    (insert-char ?\n 2))
-  (save-excursion
-    (insert-char ?\n 2)
-    (insert " >")
-    (when (not (looking-at " ")) (insert " "))))
-
 (defun ska-electric-transpose-chars ()
   "Replacement for the default transpose-chars command.
 This is usually bound to C-t and it behaves somewhat unintelligent

  (setq truncate-lines (not truncate-lines)))


(defun ska-coding-keys (map)
  "Sets the key bindings which shall be available in all programming
languages. Argument MAP is the local keymap (e.g. cperl-mode-map)."
  (define-key map '[(return)]                 'newline-and-indent)
  (define-key map '[(control b) (control c)]  'll-debug-copy-and-comment-region-or-line)
  (define-key global-map '[(control v) (control d)]
    #'(lambda ()
        (interactive)
        (if current-prefix-arg
            (ll-debug-insert-variable-output)
          (ll-debug-insert-debug-output))))
  )



;;;; =================================================================
;;;; Packages
(ska-init-message "Setting up packages")

;; Company
(when (try-require 'company)
  (add-hook 'after-init-hook 'global-company-mode))

;; Highlight Context Line (my own)
(ska-init-message "   Highlight Context Line")
(when (try-require 'highlight-context-line)
  (highlight-context-line-mode 1))

;; Home and End
(ska-init-message "   Home/End")
(if (try-require 'home-end)
    (progn
      (global-set-key '[(home)] 'home-end-home)
      (global-set-key '[(end)] 'home-end-end))
  ;; else fall back to this impl
  (ska-init-message "emacs goodies not installed")
  (defun chb-home ()
    (interactive)
    (if (not (bolp))
        (beginning-of-line)
      (if (eq this-command last-command)
          (cond
           ((not (= (point) (window-start)))
            (move-to-window-line 0)
            (beginning-of-line))
           (t
            (goto-char (point-min)))))))
  
  (defun chb-end ()
    (interactive)
    (if (not (eolp))
        (end-of-line)
      (if (eq this-command last-command)
          (cond
           ((not (= (point) (save-excursion
                              (move-to-window-line -1)
                              (end-of-line)
                              (point))))
            (move-to-window-line -1)
            (end-of-line))
           (t
            (goto-char (point-max)))))))
  (global-set-key '[(home)] 'chb-home)
  (global-set-key '[(end)] 'chb-end))
  



;; Recent Files
(ska-init-message "   RecentFiles")
(when (try-require 'recentf)
  (setq recentf-exclude 
        '("~$" "^/ftp:" "^/ssh:"))
  (setq recentf-max-saved-items 99)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  ;; Here I override some functionf of recentf to get a merged list
  ;; This will have to prove stable
  (defun recentf-save-list ()
    "Save the recent list.
Load the list from the file specified by `recentf-save-file',
merge the changes of your current session, and save it back to the
file."
    (interactive)
    (let ((instance-list (copy-list recentf-list)))
      (recentf-load-list)
      (recentf-merge-with-default-list instance-list)
      (recentf-cleanup)
      (recentf-write-list-to-file)))

  (defun recentf-merge-with-default-list (other-list)
    "Add all items from `other-list' to `recentf-list'."
    (dolist (oitem other-list)
      ;; add-to-list already checks for equal'ity
      (add-to-list 'recentf-list oitem)))

  (defun recentf-write-list-to-file ()
    "Write the recent files list to file.
Uses `recentf-list' as the list and `recentf-save-file' as the
file to write to."
    (condition-case error
        (with-temp-buffer
          (erase-buffer)
          (set-buffer-file-coding-system recentf-save-file-coding-system)
          (insert (format recentf-save-file-header (current-time-string)))
          (recentf-dump-variable 'recentf-list recentf-max-saved-items)
          (recentf-dump-variable 'recentf-filter-changer-current)
          (insert "\n\n;;; Local Variables:\n"
                  (format ";;; coding: %s\n" recentf-save-file-coding-system)
                  ";;; End:\n")
          (write-file (expand-file-name recentf-save-file))
          (when recentf-save-file-modes
            (set-file-modes recentf-save-file recentf-save-file-modes))
          nil)
      (error
       (warn "recentf mode: %s" (error-message-string error)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


  (recentf-mode 1))

;; MTorus
(ska-init-message "   MTorus")
(when (try-require 'mtorus)
  (mtorus-init)
  (mtorus-install-suggested-bindings)

  (defun mtorus-my-buffer-skip-p (buffer)
    "The my predicate used for `mtorus-buffer-skip-p'
is to skip only the special buffers whose name begins with a space."
    (or
     (string-match "^\\*\\(Messages\\|helm\\|Help\\)\\*" (buffer-name buffer))
     (string-match "^\\*\\(cider-err\\|cider-doc\\)" (buffer-name buffer))
     (string-match "^\\*\\(nrepl-mess\\)" (buffer-name buffer))
     (string-match "^[ ]+"         (buffer-name buffer))
     (string-match "[Cc]ompletio" (buffer-name buffer))
     (string-match "slime-events" (buffer-name buffer))
     (string-match "SLIME Note" (buffer-name buffer))
     (string-match "inferior-lisp" (buffer-name buffer))
     ;; interferes with compiler.clj in morocco
     ;; (string-match "[Cc]ompil" (buffer-name buffer))
     ))
  
  (setq mtorus-buffer-skip-p 'mtorus-my-buffer-skip-p))

  
;; Picklist
;; (when (try-require 'picklist)
;;   (setq picklist-permanent-submenu t)
;;   (setq picklist-commands-submenu t)
;;   (setq picklist-number-of-menu-entries 50)
;;   (setq picklist-dont-include 
;;         '("~$" "tmp/." "contact/." "INBOX" ".bbdb" ".newsrc."
;;           "places.el"))
;;   (setq picklist-sort-function #'picklist-sort-atime)
;;   ;(setq picklist-add-menu-before "Edit") 
;;   (picklist-initialize))

;; Autoinsert
(ska-init-message "   Autoinsert")
(when (try-require 'autoinsert)
  (if (boundp 'my-copyright-holder)
      (setq auto-insert-copyright my-copyright-holder)
    (setq auto-insert-copyright (user-full-name)))

  (add-hook 'find-file-hooks 
            '(lambda ()
               (auto-insert)
               )))


;; Menubar, now with ActiveMenu for Gnu Emacs
;; For Gtk-Gnu-Emacs this is just broken, so disable it for the time being
;;(ska-init-message "   ActiveMenu")
;;(menu-bar-mode nil)
;; (defvar menubar-active-p nil)
;; (defun ska-toggle-menubar-mode ()
;;   (interactive)
;;   (setq menubar-active-p (not menubar-active-p))
;;   (menu-bar-mode menubar-active-p))
;;(when (try-require 'active-menu)
;;  (turn-on-active-menu)
;;  (setq active-menu-frame-compensation 2)) ;; depends on menubarfont


(ska-init-message "   Ispell")
(setq ispell-program-name "hunspell")

(ska-init-message "   Flyspell")
(require 'flyspell)
(define-key flyspell-mode-map '[(control \.)] 'ska-point-to-register)
(define-key flyspell-mode-map '[(control \,)] 'ska-jump-to-register)


;; Helm, heir of anything
(ska-init-message "   Helm")
(when (and (try-require 'helm)
           (try-require 'helm-config))
  ; I hate the interference with find-file (helm-mode 1)
  ;; Move helm prefix 
  (global-set-key (kbd "C-S-SPC") 'helm-command-prefix)
  (global-unset-key (kbd "C-x c"))
  
  t)

(ska-init-message "   Mail And News (Gnus, Message, BBDB)")

(define-skeleton ska-skel-mail-sgdh
  "Opening (german with women)."
  nil
  "Sehr geehrte Damen und Herren,\n\n"
  )

(define-skeleton ska-skel-mail-sgh
  "Opening (german)."
  nil
  "Sehr geehrte Herren,\n\n"
  )


(define-skeleton ska-skel-mail-ska
  "My Name."
  nil
  "Stefan Kamphausen\n"
  )

(define-skeleton ska-skel-mail-regards
  "Closing (english)."
  nil
  "Regards\n"
  "Stefan Kamphausen\n"
  )

(define-skeleton ska-skel-mail-mfg
  "Closing (german)."
  nil
  "Mit freundlichen Gren\n"
  "Stefan Kamphausen\n"
  )

(define-skeleton ska-skel-mail-coffee-or-tee
  "Ask my colleagues for coffee or tee."
  nil
  "(kaff|t)ee?"
  )

(defun ska-message-keys ()
  "Set keybindings for mail writing buffers."
  (interactive)
  (local-set-key '[(control b) (control d)]  'ska-skel-mail-sgdh)
  (local-set-key '[(control b) (control h)]  'ska-skel-mail-sgh)
  (local-set-key '[(control b) (control m)]  'ska-skel-mail-mfg)
  (local-set-key '[(control b) (control n)]  'ska-skel-mail-ska)
  (local-set-key '[(control b) (control r)]  'ska-skel-mail-regards)
  (local-set-key '[(control b) (control k)]  'ska-skel-mail-coffee-or-tee)
  (local-set-key '[(control b) (control b)]  'ska-mail-insert-answer-gap)
  (local-set-key '[(control b) (control s) (control p)]  'ska-gnus-sent-mail-as-private)
  (local-set-key '[(control b) (control s) (control b)]  'ska-gnus-sent-mail-as-bn)
  (local-set-key '[(control b) (control s) (control t)]  'ska-gnus-sent-mail-as-tt)
  (local-set-key '[(control b) (control c)]  'ska-gruesse)
  (local-set-key '[(control b) (control f)]  'Footnote-add-footnote)
  (local-set-key '[(control b) (f)]          'Footnote-back-to-message)
  )

(defun ska-article-keys ()
  "Set keybindings in Gnus Article buffers."
  (local-set-key '[(control b) (control s)]  'gnus-mime-save-part)
)

(setq gnus-init-file (expand-file-name "~/.emacs.d/gnus-init.el"))
(setq gnus-directory (expand-file-name "~/contact/gnus/"))
(setq message-directory (expand-file-name     "~/contact/gnus/"))
(setq bbdb-file (expand-file-name   "~/.emacs.d/bbdb"))
(add-hook 'gnus-select-group-hook
          '(lambda ()
             (setq bbdb/news-auto-create-p
                   (cond
                    ((string-match  "^nnml.mail.acrolinx"
                                    gnus-newsgroup-name)
                     t)
                    ((string-match  "^nnml.mail.software.*"
                                    gnus-newsgroup-name)
                     nil)
                    ((string-match  "^nntp" gnus-newsgroup-name)
                     nil)
                    (t
                     'bbdb-ignore-most-messages-hook)))))
(setq bbdb-ignore-most-messages-alist 
      '(("Subject" . "\\(emacs\\|cdargs\\|cl-ipc\\|Web-Kontakt\\)"))
      bbdb-ignore-some-messages-alist
      '(("From" . "daemon")
        ("From" . "abuse@")
        ("From" . "news@")
        ("From" . "cron")
        ("From" . "root@")
        ("From" . "noreply@")
        ("From" . "prontomail")
        ("From" . "majordomo")
        ("From" . "postmaster")
        ("From" . "quota")
        ("Subject" . "MAILER DAEMON")))
(setq bbdb-notice-hook 'bbdb-auto-notes-hook)
(setq bbdb-auto-notes-alist
      '(("Organization" (".*" company 0))
        ("Newsgroup" ("[^,]+" newsgroups 0))
        ("Subject" (".*" last-subj 0 t))
        ("User-Agent" (".*" mailer 0))
        ("X-Newsreader" (".*" mailer 0))
        ("X-gpg-key-ID" (".*" key 0))
        ("X-Mailer" (".*" mailer 0))
        ("X-URL" (".*" url 0))
        ;;("X-Face" (".+" face 0 'replace))
        ;;("Face" (".+" cface 0 'replace))
        ))
(setq bbdb-default-area-code "030"
            bbdb-north-american-phone-numbers-p nil
            bbdb-complete-name-allow-cycling t)

;; Organizing with Org and Remember
(ska-init-message "   OrgMode")

(setq org-replace-disputed-keys t)
(when (try-require 'org)
  ;; basic setup
  (add-to-list 'auto-mode-alist '("\\.org$" . org-mode))
  (define-key global-map "\C-cl" 'org-store-link)
  (setq org-log-done t)
  (setq org-hide-leading-stars t)
  (setq org-startup-folded nil)
  ;; agenda
  (setq org-agenda-files 
        (list "~/Dokumente/org.org"))
  ;; setup according to 
  ;; http://www.newartisans.com/blog_files/org.mode.day.planner.php
  (setq remember-data-file (expand-file-name "~/org/inbox.org"))
  (setq remember-handler-functions '(org-remember-handler))
  (setq remember-annotation-functions '(org-remember-annotation))
  (setq org-default-notes-file nil)
  (setq org-remember-store-without-prompt t)
  (setq org-remember-templates
        ;; name accesschar template file headline
        '(("default" ?d "* TODO %?\n  %u" "~/org/inbox.org"
           "Tasks")))
  ;; shiny and new: mobileorg
  (setq org-mobile-directory "~/org/mobile/")

  ;; (setq org-mobile-directory "~/org/")
  (add-hook 'org-mobile-post-push-hook
            (lambda () (shell-command "scp -r ~/org/mobile/* ska@vvoo.de:org/webdav/export/")))
  (add-hook 'org-mobile-pre-pull-hook
            (lambda () (shell-command "scp ska@vvoo.de:org/webdav/export/mobileorg.org ~/org/mobile/ ")))
  (add-hook 'org-mobile-post-pull-hook
            (lambda () (shell-command "scp ~/org/mobile/mobileorg.org ska@vvoo.de:org/webdav/export/")))

)


(ska-init-message "   Remember")
(when (try-require 'remember)
  (add-hook 'remember-mode-hook 'org-remember-apply-template))
;; Keybinding in ska-ctrl-v-map below


;; We always need footnotes
(ska-init-message "   Footnotes")
(try-require 'footnote)

;; Highlight current line
;; (ska-init-message "   HighLine")
;; (when (try-require 'highline)
;;   (global-highline-mode))

;; Magit
(ska-init-message "   Magit")
(when (try-require 'magit)
  t)


;; Filladapt
;; (ska-init-message "   FillAdapt")
;; (when (try-require 'filladapt)
;;   t)

(ska-init-message "   MouseDrag")
(when (try-require 'mouse-drag)
  (setq mouse-throw-with-scroll-bar t)
  (global-set-key [down-mouse-2] 'mouse-drag-drag)
  (global-set-key [C-down-mouse-2] 'mouse-drag-throw))

(ska-init-message "   LinNum")
(when (try-require 'linum)
  (global-linum-mode))

(ska-init-message "   TimeStamp")
(when (try-require 'time-stamp)
  (add-hook 'write-file-hooks 
            '(lambda ()
               (time-stamp)
               )))

(ska-init-message "   WindResize")
(when (try-require 'windresize)
  (global-set-key '[(f8)] 'windresize)
  (global-set-key '[(control shift left)]  'windresize-select-left)
  (global-set-key '[(control shift right)] 'windresize-select-right)
  (global-set-key '[(control shift up)]    'windresize-select-up)
  (global-set-key '[(control shift down)]  'windresize-select-down))

(ska-init-message "   FDLCaps")
(try-require 'fdlcap)

(ska-init-message "   iedit")
(when (try-require 'iedit)
  (global-set-key '[(control \;)] 'iedit-mode))

(ska-init-message "   key-chord")
(when (try-require 'key-chord)
  (key-chord-mode 1))
  ;; Moved keychords to local hooks and test for availability of key-chord

(ska-init-message "   which-func")
(when (try-require 'which-func)
  (setq which-func-modes t) ; enable for all modes
  (which-function-mode 1))

(ska-init-message "   ERC")
(when (try-require 'erc)
  ;; defaults to freenode (setq erc-default-server "10.42.254.90")
  (setq erc-autojoin-channels-alist
        '(("#clojure")))
  (setq erc-keywords '("ska" "kamphausen" "ska2342"))
)

;; Pulse might have been funny for highlight-context-line but it keeps
;; the cursor in the pulsing line, so kick it. sad.
;; (ska-init-message "   Pulse")
;; (try-require 'pulse)
;; Keybinding in ska-ctrl-v-map below

(ska-init-message "   Taskjuggler")
(try-require 'taskjuggler-mode)


(ska-init-message "   Markdown")
(when (try-require 'markdown-mode)
  (add-to-list 'auto-mode-alist '("\\.markdown\\'" . markdown-mode))
  (add-to-list 'auto-mode-alist '("\\.md\\'" . markdown-mode)))
;;;; =================================================================
;;;; Hooks

(ska-init-message "Customizing Hooks")

(defun prog-modes-common-hook-function (&optional no-auto-fill 
                                             no-untabify
                                             no-setnu)
  (unless no-auto-fill (auto-fill-mode 1))
  ;; (unless no-setnu (turn-on-setnu-mode))
  (unless no-untabify
    (add-hook 'write-contents-hooks #'ska-untabify
              nil t)))

(add-hook 'after-save-hook
          'executable-make-buffer-file-executable-if-script-p)

;; Text
(ska-init-message "  Text Mode")
(add-hook 'text-mode-hook
          '(lambda () 
             (auto-fill-mode 1)
             ;;(filladapt-mode)
             ))

;; Emacs Lisp
(ska-init-message "  Elisp")
(add-hook 'emacs-lisp-mode-hook
          #'(lambda ()
              (prog-modes-common-hook-function)
              (key-chord-define-local "jj" "(")
              (key-chord-define-local "kk" ")")
              (turn-on-eldoc-mode)))


;; C/C++

(define-skeleton ska-skel-c-main
  "Insert a typical main function."
  nil

               "}\n\n\n1;\n"
               ))


;; My old Common Lisp, Slime SBCL stuff is missing here.


;; TeX, LaTeX
;;(ska-init-message "  LaTeX")
;;(try-require 'tex-site)

;; (setq TeX-parse-self t) ; Enable parse on load.
;; (setq TeX-auto-save t) ; Enable parse on save.
;; (setq-default TeX-master nil)
;; (setq reftex-plug-into-AUCTeX t)


;; (defun ska-latex-mode-keys ()
;;   "Set keys for latex-mode (AUCTeX) . "
;;   (local-set-key '[(tab)]                    'LaTeX-indent-line)
;; ;  (define-key LaTeX-math-keymap
;; ;    (concat LaTeX-math-abbrev-prefix "/")    'LaTeX-math-frac)
;; )
;; (defun ska-bibtex-mode-keys ()
;; 
;; 
;; 

;; (add-hook 'LaTeX-mode-hook
;;           '(lambda ()
;;              ;; meine keybindings
;;              (ska-latex-mode-keys)
;;              (auto-fill-mode 1)
;;              ;(setq ispell-parser 'tex)
;;              (LaTeX-math-mode)
;;              (turn-on-reftex)
;; ;             (when (string-match "diary" buffer-file-name)
;; ;               (turn-on-tex-diary))
;;              ))

;; (add-hook 'TeX-language-de-hook
;;           '(lambda ()
;;              ;;(ispell-change-dictionary "deutsch")
;;              ))

 ;; (add-hook 'BibTeX-mode-hook
 ;;          '(lambda ()
 ;;             (ska-bibtex-mode-keys)
 ;;             ))


;; Java JDE missing


;;;; =================================================================
;;;; MISC SETTINGS
(ska-init-message "Misc Settings")
(setq-default indent-tabs-mode nil)
(fset 'yes-or-no-p 'y-or-n-p)
;(setq eshell-ask-to-save-history 'always)
(setq inhibit-splash-screen t)
;(setq apropos-do-all t)
(put 'narrow-to-region 'disabled nil)
(put 'erase-buffer 'disabled nil)
(put 'scroll-left 'disabled nil)


(define-key ska-ctrl-v-map '[(control t)] 'fdlcap-change-case-current-word)

(global-set-key '[(control up)]           '(lambda ()
                                              (interactive)
                                              (scroll-down 1)))
(global-set-key '[(control down)]         '(lambda ()
                                             (interactive)
                                             (scroll-up 1)))

(global-set-key '[(home)]                 'chb-home)
(global-set-key '[(end)]                  'chb-end)


