(setq default-directory "/home/james/dev/")

;; local lisp
(setq load-path (append load-path '("/usr/local/emacs/site-lisp")))

;;(load "regex-tool" t)    ; load regex-tool if it's available

;; fix fking clipboard
;;(setq x-select-request-type '(UTF8_STRING COMPOUND_TEXT TEXT STRING))
(setq x-select-enable-clipboard t)
;(setq interprogram-paste-function 'x-cut-buffer-or-selection-value)

;; recent files menu
;(require 'recentf)
(recentf-mode 1)

(add-to-list 'load-path
             "~/.emacs.d/plugins")
(require 'yasnippet-bundle)
(setq yas/trigger-key (kbd "C-c <kp-multiply>"))

(require 'htmlize)

;; align stuff
(require 'align)

;; wikipedia shite
;(autoload 'wikipedia-mode "wikipedia-mode.el"
;  "Major mode for editing documents in Wikipedia markup." t)
;(add-to-list 'auto-mode-alist 
;   '("\\.wiki\\'" . wikipedia-mode))
 (autoload 'longlines-mode "longlines.el"
   "Minor mode for editing long lines." t)


;; python-mode settings - from http://www.rwdev.eu/articles/emacspyeng
(require 'python)
(setq auto-mode-alist (cons '("\\.py$" . python-mode) auto-mode-alist))
(setq interpreter-mode-alist(cons '("python" . python-mode)
                             interpreter-mode-alist))
;; path to the python interpreter, e.g.: ~rw/python27/bin/python2.7
(setq py-python-command "python")
(autoload 'python-mode "python-mode" "Python editing mode." t)
(require 'python-mode)

;; pymacs settings
;(setq pymacs-python-command py-python-command)
(autoload 'pymacs-apply "pymacs")
(autoload 'pymacs-call "pymacs")
(autoload 'pymacs-eval "pymacs" nil t)
(autoload 'pymacs-exec "pymacs" nil t)
(autoload 'pymacs-load "pymacs" nil t)
(require 'pymacs)

(pymacs-load "ropemacs" "rope-")
(setq ropemacs-enable-autoimport t)
(setq ropemacs-autoimport-modules '("os" "shutil"))


;; autocomplete - http://cx4a.org/software/auto-complete/manual.html
(require 'auto-complete-config)
(add-to-list 'ac-dictionary-directories "~/.emacs.d/ac/ac-dict")
(ac-config-default)
(global-auto-complete-mode t)
(define-key ac-complete-mode-map "\t" 'ac-complete)
(define-key ac-complete-mode-map "\r" nil)

;(require 'pycomplete)



;; tabbed emacs
(require 'tabbar)
(global-set-key [(control f10)] 'tabbar-mode)

;; if you encounter a file with ^M or ... at the end of every line,
;; get rid of them by pressing [F10]
;(global-set-key [f10]      'cut-ctrlM) ; cut all ^M.
;(defun cut-ctrlM ()
;   "Cut all visible ^M."
;   (interactive)
;   (beginning-of-buffer)
;   (while (search-forward "\r" nil t)
;   (replace-match "" nil t))
;)

;; Enable wheelmouse support by default for emacs 21
(cond (window-system
       (mwheel-install)
))

;; File timestamping
(add-hook 'write-file-hooks 'time-stamp)

;;{{{ java crap

;; semantic - also required for JDE
;(add-to-list 'load-path "/usr/share/xemacs/site-lisp/semantic-1.3.2")
;(require 'semantic-c) ; for C code integration
;(require 'semantic-el) ; for Emacs Lisp code integration
;(require 'semantic-make) ; for Makefile integration
;(require 'semantic-imenu) ; if you use imenu or wich-function

;; jde
;(add-to-list 'load-path (expand-file-name "/usr/share/xemacs/site-lisp/jde-2.2.2"))
;(add-to-list 'load-path "/usr/share/xemacs/site-lisp/speedbar-0.12")
;(require 'jde)

;;}}}

;; tramp - ange-ftp but with ssh
(require 'tramp)
(setq tramp-default-method "scp")

;; vi-style line numbering
;(require 'setnu)

(setq frame-title-format '(buffer-file-name "%f" "%b"))

;; highlight searches
; (set-face-background 'region "red")

;; save all backups in one directory
;; from http://www.emacswiki.org/emacs/BackupDirectory
(setq
   backup-by-copying t      ; don't clobber symlinks
   backup-directory-alist
    '(("." . "/home/james/.bak"))    ; don't litter my fs tree
   delete-old-versions t
   kept-new-versions 6
   kept-old-versions 2
   version-control t)       ; use versioned backups

;; highlight search results
(setq query-replace-highlight t)
(setq search-highlight t)

;; little timestamp function
(defun insert-time-stamp ()
  "Insert current date and time."
  (interactive "*")
  (insert (current-time-string)))

;; save system between loads
(require 'session)
(add-hook 'after-init-hook 'session-initialize)

;; use cperl mode
;;{{{

(defun modify-alist (alist-symbol key value &optional search-cdr)
  (let ((alist (symbol-value alist-symbol)))
    (while alist
      (if (eq (if search-cdr
              (cdr (car alist))
            (car (car alist))) key)
          (setcdr (car alist) value)
   (setq alist (cdr alist))))))

(setq cperl-indent-level 4
      cperl-close-paren-offset -4
      cperl-continued-statement-offset 4
      cperl-indent-parens-as-block t
      cperl-tab-always-indent t)

(setq auto-mode-alist
      (append '(("\\.\\([pP][Llm]\\|al\\)$" . cperl-mode))  auto-mode-alist ))

(modify-alist 'interpreter-mode-alist 'perl-mode 'cperl-mode t)
(modify-alist 'auto-mode-alist        'perl-mode 'cperl-mode t)

;; perl descriptions
(autoload 'describe-perl-symbol "perl-descr"
  "One-line information on a perl symbol" t)
(autoload 'switch-to-perl-doc-buffer "perl-descr"
  "One-line information on a perl symbol" t)
(setq cperl-mode-hook 
           (function (lambda ()
                (local-set-key "\eOQ" 'describe-perl-symbol)
                   (local-set-key "\eOD" 'switch-to-perl-doc-buffer))))

;;}}}


;; folding mode is back!
;;{{{

(setq folding-default-keys-function
      'folding-bind-backward-compatible-keys)

(if (load "folding" 'nomessage 'noerror)
    (folding-mode-add-find-file-hook))

(autoload 'apache-mode "apache-mode" "autoloaded" t)
(add-to-list 'auto-mode-alist '("\\.htaccess$"   . apache-mode))
(add-to-list 'auto-mode-alist '("httpd\\.conf$"  . apache-mode))
(add-to-list 'auto-mode-alist '("srm\\.conf$"    . apache-mode))
(add-to-list 'auto-mode-alist '("access\\.conf$" . apache-mode))

;;}}}


;; hideshow
;(load-library "hideshow")
;(add-hook 'cperl-mode-hook               ; other modes similarly
;          '(lambda () (hs-minor-mode 1)))
;(add-hook 'perl-mode-hook               ; other modes similarly
;          '(lambda () (hs-minor-mode 1)))

;; font coloring
(global-font-lock-mode t)
;(font-lock-mode t)
;(setq font-lock-maximum-decoration 't)

;; automatically update timestamp as saving a file with $Lastupdate header.
;; example: $Lastupdate: Wed Feb 22 22:43:01 2012 $
(defun save-buffer-wrapper ()
  (interactive)
  (let ((lastupdate (concat "$Lastupdate: " (current-time-string) " $")))
    (save-excursion
      (goto-char (point-min))
      (goto-char (point-min))
      (while (re-search-forward
              "$\\Lastupdate\\([A-Za-z0-9: ]*\\)?\\$" nil t)
        (replace-match lastupdate nil t)))
    (save-buffer)))
(global-set-key "\C-x\C-s" 'save-buffer-wrapper)

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

;; http://www.enigmacurry.com/2009/01/21/autocompleteel-python-code-completion-in-emacs/
;; Auto-completion Integrates: 1) Rope 2) Yasnippet  all with AutoComplete.el 

(defun prefix-list-elements (list prefix)
  (let (value)
    (nreverse
     (dolist (element list value)
      (setq value (cons (format "%s%s" prefix element) value))))))
(defvar ac-source-rope
  '((candidates
     . (lambda ()
         (prefix-list-elements (rope-completions) ac-target))))
  "Source for Rope")
(defun ac-python-find ()
  "Python `ac-find-function'."
  (require 'thingatpt)
  (let ((symbol (car-safe (bounds-of-thing-at-point 'symbol))))
    (if (null symbol)
        (if (string= "." (buffer-substring (- (point) 1) (point)))
            (point)
          nil)
      symbol)))
(defun ac-python-candidate ()
  "Python `ac-candidates-function'"
  (let (candidates)
    (dolist (source ac-sources)
      (if (symbolp source)
          (setq source (symbol-value source)))
      (let* ((ac-limit (or (cdr-safe (assq 'limit source)) ac-limit))
             (requires (cdr-safe (assq 'requires source)))
             cand)
        (if (or (null requires)
                (>= (length ac-target) requires))
            (setq cand
                  (delq nil
                        (mapcar (lambda (candidate)
                                  (propertize candidate 'source source))
                                (funcall (cdr (assq 'candidates source)))))))
        (if (and (> ac-limit 1)
                 (> (length cand) ac-limit))
            (setcdr (nthcdr (1- ac-limit) cand) nil))
        (setq candidates (append candidates cand))))
    (delete-dups candidates)))
(add-hook 'python-mode-hook
          (lambda ()
                 (auto-complete-mode 1)
                 (set (make-local-variable 'ac-sources)
                      (append ac-sources '(ac-source-rope) '(ac-source-yasnippet)))
                 (set (make-local-variable 'ac-find-function) 'ac-python-find)
                 (set (make-local-variable 'ac-candidate-function) 'ac-python-candidate)
                 (set (make-local-variable 'ac-auto-start) nil)))

;;Ryan's python specific tab completion                                                                        
(defun ryan-python-tab ()
  ; Try the following:                                                                                         
  ; 1) Do a yasnippet expansion
  ; 2) Do a Rope code completion                                                            ; 3) Do an indent

  (interactive)
  (if (eql (ac-start) 0)
      (indent-for-tab-command)))

(defadvice ac-start (before advice-turn-on-auto-start activate)
  (set (make-local-variable 'ac-auto-start) t))
(defadvice ac-cleanup (after advice-turn-off-auto-start activate)
  (set (make-local-variable 'ac-auto-start) nil))

(define-key python-mode-map "\t" 'ryan-python-tab)
;;; End Auto Completion

;; set up custom keys
;;{{{

(global-set-key [f11] 'kill-buffer)
(global-set-key [f12] 'kill-buffer-and-window)
;(global-set-key [S-mouse-3] 'imenu)
(define-key global-map "\C-x\C-g" 'comment-region)
(define-key global-map "\M-s" 'speedbar)
(define-key global-map "\C-z" 'undo)
(define-key global-map [f3]   'delete-window)
(global-set-key "\C-h" 'delete-backward-char)
(global-set-key [f1] 'help-command)

(define-key global-map [f2]   'replace-string)
(define-key global-map [f4]   'repeat-complex-command)
;(define-key global-map [f5]   'goto-line)
(define-key global-map [f6]   'find-file)
;(define-key global-map [f7]   'set-mark-command)
(define-key global-map [f7]   'goto-line)
(define-key global-map [f8]   'yank)
(define-key global-map [f9]   'save-buffer)

(global-set-key [(alt /)] `dabbrev-expand)
(global-set-key [(control f1)] 'toggle-read-only)
(global-set-key [(control f2)] 'line-number-mode)
(global-set-key [(control =)] `zap-up-to-char)
(global-set-key [(shift button2)] `imenu)

(global-set-key [(control f3)] 'fold-enter)
(global-set-key [(control f4)] 'fold-exit)

;; windows emu
(require 'pc-select)
(pc-selection-mode)
(global-set-key [(alt c)] `copy-primary-selection)
(global-set-key [(alt v)] `yank)
(global-set-key [(alt x)] `copy-region-as-kill)
(global-set-key [(alt left)] `backward-word)
(global-set-key [(alt right)] `forward-word)
(global-set-key [(alt up)] `backward-paragraph)
(global-set-key [(alt down)] `forward-paragraph)

;; Keypad stuff - taken from http://www.dotfiles.com/files/6/127_.emacs
(define-key function-key-map [(control kp-home)] [(control home)])
(define-key function-key-map [(control kp-end)] [(control end)])
(define-key function-key-map [(control kp-prior)] [(control prior)])
(define-key function-key-map [(control kp-next)] [(control next)])
(define-key function-key-map [(control kp-left)] [(control left)])
(define-key function-key-map [(control kp-right)] [(control right)])
(define-key function-key-map [(control kp-up)] [(control up)])
(define-key function-key-map [(control kp-down)] [(control down)])
(define-key function-key-map [(meta kp-home)] [(meta home)])
(define-key function-key-map [(meta kp-end)] [(meta end)])
(define-key function-key-map [(meta kp-prior)] [(meta prior)])
(define-key function-key-map [(meta kp-next)] [(meta next)])
(define-key function-key-map [(control meta kp-home)] [(control meta home)])
(define-key function-key-map [(control meta kp-end)] [(control meta end)])
(define-key function-key-map [(control meta kp-prior)] [(control meta prior)])
(define-key function-key-map [(control meta kp-next)] [(control meta next)])

;;}}}

;; color-theme
(require 'color-theme-autoloads "color-theme-autoloads")
(color-theme-initialize)
(color-theme-calm-forest)

;; put scrollbar on right
(custom-set-variables
  ;; custom-set-variables was added by Custom.
  ;; If you edit it by hand, you could mess it up, so be careful.
  ;; Your init file should contain only one such instance.
  ;; If there is more than one, they won't work right.
 '(auto-compression-mode t nil (jka-compr))
 '(case-fold-search t)
 '(cperl-hash-face ((((class color) (background light)) (:foreground "Red"))))
 '(cperl-here-face ((t (:foreground "white"))))
 '(current-language-environment "English")
 '(default-input-method "rfc1345")
 '(develock-auto-enable nil)
 '(efs-use-passive-mode t)
 '(global-font-lock-mode t nil (font-lock))
 '(inhibit-startup-screen t)
 '(menu-bar-mode t)
 '(query-user-mail-address nil)
 '(scroll-bar-mode (quote right))
 '(show-paren-mode t nil (paren))
 '(time-stamp-format "%02d/%02m/%04y %02H:%02M:%02S %u")
 '(tool-bar-mode nil nil (tool-bar))
 '(transient-mark-mode t)
 '(user-mail-address "james@NOSPAMNOSPAMNOSPAM.co.uk")
 '(whitespace-silent t)
 '(yas/use-menu (quote abbreviate)))

;(if (eq window-system "x")
;    (set-default-font "-unknown-Inconsolata-normal-normal-normal-*-*-*-*-*-m-0-iso10646-1"))

;;* Font selection
;;{{{

(cond ((or (eq window-system 'mac) (eq window-system 'ns))
       (set-face-font 'default '"-apple-inconsolata-medium-r-normal--16-0-72-72-m-0-iso10646-1"))
      ((eq window-system 'w32)
       (set-face-font 'default '"-outline-Inconsolata-normal-normal-normal-mono-16-*-*-*-c-*-iso8859-1"))
      ((and (eq window-system 'x) (eq emacs-major-version 23))
       (set-face-font 'default '"-unknown-Inconsolata-normal-normal-normal-*-16-*-*-*-m-0-iso10646-1")
       (add-to-list 'default-frame-alist '(font . "-unknown-Inconsolata-normal-normal-normal-*-16-*-*-*-m-0-iso10646-1"))
       )
      ((eq window-system 'x)
       (set-face-font 'default '"10x20")))

(add-hook 'before-make-frame-hook
         (lambda ()
       (set-frame-font "-unknown-Inconsolata-normal-normal-normal-*-16-*-*-*-m-0-iso10646-1")
         ))

;;}}}