dotfiles / .emacs

;; Red Hat Linux default .emacs initialization file  ; -*- mode: emacs-lisp -*-

;; add my emacs_sitelisp to library list
(add-to-list 'load-path "~/.emacs.d")

;; Set up the keyboard so the delete key on both the regular keyboard
;; and the keypad delete the character under the cursor and to the right
;; under X, instead of the default, backspace behavior.
(global-set-key [delete] 'delete-char)
(global-set-key [kp-delete] 'delete-char)

;; tabs are 4 spaces
(setq default-tab-width 4)
(setq tab-width 4)
(setq-default c-basic-offset 4)
;; never use tabs to indent
(setq indent-tabs-mode nil)
(setq-default indent-tabs-mode nil)

;; turn on font-lock mode
(global-font-lock-mode t)
;; enable visual feedback on selections
(setq-default transient-mark-mode t)

;; always end a file with a newline
(setq require-final-newline t)

;; stop at the end of the file, not just add lines
(setq next-line-add-newlines nil)

(when window-system
  ;; enable wheelmouse support by default
  ;; use extended compound-text coding for X clipboard
  (set-selection-coding-system 'compound-text-with-extensions))

  ;; 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.
 '(case-fold-search t)
 '(confirm-kill-emacs (quote yes-or-no-p))
 '(cua-mode nil nil (cua-base))
 '(current-language-environment "English")
 '(global-font-lock-mode t nil (font-lock))
 '(php-mode-force-pear t)
 '(scroll-bar-mode (quote right))
 '(text-mode-hook (quote (turn-on-auto-fill text-mode-hook-identify)))
 '(tool-bar-mode nil nil (tool-bar))
 '(transient-mark-mode t)
 '(uniquify-buffer-name-style (quote forward) nil (uniquify)))

(setq default-font
(set-default-font default-font)

;; templates
(require 'template)

;; cperl mode for perl files
;; never-ever use perl mode, always cperl
(autoload 'cperl-mode "cperl-mode")
(defalias 'perl-mode 'cperl-mode)

;; css mode for .css files
(autoload 'css-mode "css-mode")
;; html-mode for .html files
(require 'sgml-mode)

;; php mode
(autoload 'php-mode "php-mode")
;; python mode
(autoload 'python-mode "python-mode")
;; erlang mode
(autoload 'erlang-mode "erlang-mode")
(setq erlang-root-dir "/opt/local/lib/erlang")
(setq exec-path (cons "/opt/local/bin" exec-path))
(setq exec-path (cons "/opt/local/lib/erlang/bin" exec-path))
(require 'erlang-start)
(require 'erlang)
(defvar inferior-erlang-prompt-timeout t)
;; default node name to emacs@localhost
(setq inferior-erlang-machine-options '("-sname" "emacs"))
;; ruby mode
(autoload 'ruby-mode "ruby-mode")
(autoload 'javascript-mode "javascript" nil t)

;; autoloads
(setq auto-mode-alist
	'("\\.py$" . python-mode)
	'("\\.erl$" . erlang-mode)
	'("\\.css$" . css-mode)
    '("\\.php$" . php-mode)
	'("\\.inc$" . php-mode)
	'("\\.sql$" . sql-mode)
	'("\\.xml$" . nxml-mode)
	'("\\.js$" . javascript-mode)
	'("\\.rb$" . ruby-mode)
	'("\\.p?html$" . html-mode)
	'("\\.\\([pP][Llm]\\|al\\)$" . cperl-mode))))

;; cvs uses ssh
(setenv "CVS_RSH" "ssh")

  ;; custom-set-faces was added by Custom -- don't edit or cut/paste it!
  ;; Your init file should contain only one such instance.
; '(default ((t (:stipple nil :background "snow" :foreground "black" :inverse-video nil :box nil :strike-through nil :overline nil :underline nil :slant normal :weight normal :height 97 :width normal :family "b&h-lucidatypewriter")))))
  ;; custom-set-faces 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.
 '(default ((t (:stipple nil :background "#F5F4CB" :foreground "black" :inverse-video nil :box nil :strike-through nil :overline nil :underline nil :slant normal :weight normal :height 110 :width normal :family "b&h-lucidatypewriter"))))
 '(cperl-nonoverridable-face ((((class color) (background light)) (:foreground "chartreuse4"))))
 '(font-lock-comment-face ((((class color) (background light)) (:foreground "dark slate blue"))))
 '(font-lock-constant-face ((((class color) (background light)) (:foreground "CadetBlue" :weight bold))))
 '(font-lock-function-name-face ((((class color) (background light)) (:foreground "dark olive green" :weight bold))))
 '(font-lock-keyword-face ((((class color) (background light)) (:foreground "FireBrick"))))
 '(font-lock-string-face ((((class color) (background light)) (:foreground "salmon4"))))
 '(fringe ((((class color) (background light)) (:background "#E4E3BA"))))
 '(menu ((((type x-toolkit)) (:background "#E4E3BA")))))

;; '(menu ((((type x-toolkit)) (:background "#E4E3Ba"))))
;; '(tool-bar ((((type x w32 mac) (class color)) (:background "#E4E3Ba" :foreground "black" :box (:line-width 1 :style released-button))))))

(put 'upcase-region 'disabled nil)
(put 'downcase-region 'disabled nil)

(require 'ffap)

(require 'nxml-mode)

;; install keymap for nxml mode to reload the schema associated with
;; the current document
(define-key nxml-mode-map "\C-c\C-r" 'rng-reload-schema)

(defun rng-reload-schema ()
  (let ((schema-filename rng-current-schema-file-name))
    (when schema-filename
      (setq rng-current-schema (rng-load-schema schema-filename))
      (run-hooks 'rng-schema-change-hook)
      (message "Reloaded schema %s" schema-filename))
    (unless schema-filename

(defun rng-apply-find-schema-file (fn)
  (let ((schema-filename rng-current-schema-file-name))
    (unless schema-filename
      (error "This file is not associated with a schema file."))
    (funcall fn schema-filename)))

(defun rng-find-schema-file ()
  (rng-apply-find-schema-file 'find-file))

(defun rng-find-schema-file-other-window ()
  (rng-apply-find-schema-file 'find-file-other-window))

(require 'rnc-mode)

;; pymacs and rope (python ide stuff)
(require 'pymacs)
(pymacs-load "ropemacs" "rope-")
(setenv "PYMACS_PYTHON" "python2.5")

(require 'pysmell)
(add-hook 'python-mode-hook (lambda () (pysmell-mode 1)))

;; better buffer switching
(require 'ido)
(ido-mode t)
(setq ido-enable-flex-matching t)

;; C-x k kills client buffers too
(add-hook 'server-switch-hook 
            (lambda ()
              (when (current-local-map)
                (use-local-map (copy-keymap (current-local-map))))
              (local-set-key (kbd "C-x k") 'server-edit)))

;; tab complete everywhere
(global-set-key [(tab)] 'smart-tab)
(defun smart-tab ()
  "This smart tab is minibuffer compliant: it acts as usual in
    the minibuffer. Else, if mark is active, indents region. Else if
    point is at the end of a symbol, expands it. Else indents the
    current line."
  (if (minibufferp)
    (if mark-active
        (indent-region (region-beginning)
      (if (looking-at "\\_>")
          (dabbrev-expand nil)

;; start the server so emacsclient can work

;; lfo nose integration
;; if you want to use this, you'll probably need to redefine find-test-runner
(defun run-nose (&optional tests debug)
  "run nosetests"
  (let* ((nose (find-test-runner))
         (where (expand-file-name "../.." (file-name-directory nose)))
         (args (if debug "--pdb" ""))
         (tnames (if tests tests "")))
    (print nose)
    (print args)
    (print tnames)
    (funcall (if debug 'pdb 'compile)
             (format "%s -v %s -w %s -c %s/setup.cfg %s" 
                     (find-test-runner) args where where tnames)))

(defun nosetests-all (&optional debug)
  "run all tests"
  (run-nose nil debug))

(defun nosetests-pdb-all ()
  (nosetests-all t))

(defun nosetests-module (&optional debug)
  "run nosetests (via eggs/bin/test) on current buffer"
  (run-nose buffer-file-name debug))

(defun nosetests-pdb-module ()
  (nosetests-module t))

(defun nosetests-one (&optional debug)
  "run nosetests (via eggs/bin/test) on testable thing
 at point in current buffer"
  (run-nose (format "%s:%s" buffer-file-name (py-testable)) debug))

(defun nosetests-pdb-one ()
  (nosetests-one t))

(defun find-test-runner()
  "find eggs/bin/test in a parent dir of current buffer's file"
  (find-test-runner-in-dir (file-name-directory buffer-file-name)))

(defun find-test-runner-in-dir (dn)
  (let ((fn (expand-file-name "eggs/bin/test" dn)))
    (if (file-regular-p fn)
      (if (eq dn "/")
         (file-name-directory (directory-file-name dn)))))))

(defun py-testable ()
  (let ((remember-point (point)))
     "^ \\{0,4\\}\\(class\\|def\\)[ \t]+\\([a-zA-Z0-9_]+\\)" nil t)
    (setq t1 (buffer-substring-no-properties (match-beginning 2) (match-end 2)))
    (goto-char remember-point)
     "^\\(class\\|def\\)[ \t]+\\([a-zA-Z0-9_]+\\)" nil t)
    (setq outer 
          (buffer-substring-no-properties (match-beginning 1) (match-end 1)))
    (setq t2 (buffer-substring-no-properties (match-beginning 2) (match-end 2)))
        ((result (cond ((string= outer "def") t2)
                       ((string= t1 t2) t2)
                       (t (format "%s.%s" t2 t1)))))
      (goto-char remember-point)

(eval-after-load "compile"
  '(add-to-list 'compilation-error-regexp-alist
                '(".*File \"\\(.+\\)\", line \\([0-9]+\\)" 1 2)))

(add-hook 'python-mode-hook 
          (lambda ()
            (local-set-key "\C-ca" 'nosetests-all)
            (local-set-key "\C-cm" 'nosetests-module)
            (local-set-key "\C-c." 'nosetests-one)
            (local-set-key "\C-cpa" 'nosetests-pdb-all)
            (local-set-key "\C-cpm" 'nosetests-pdb-module)
            (local-set-key "\C-cp." 'nosetests-pdb-one)))