Skip to content

Instantly share code, notes, and snippets.

@malthe
Created February 24, 2012 11:06
Show Gist options
  • Save malthe/1900182 to your computer and use it in GitHub Desktop.
Save malthe/1900182 to your computer and use it in GitHub Desktop.
Dot-emacs
;; .emacs
;; =========================================================================
;;
;; Malthe Borch
;;
;; Last updated: Fri Feb 24, 2012.
;; Add own packages to load path
(let* ((my-lisp-dir "~/.emacs.d/site-lisp")
(default-directory my-lisp-dir)
(orig-load-path load-path))
(setq load-path (cons my-lisp-dir nil))
(normal-top-level-add-subdirs-to-load-path)
(nconc load-path orig-load-path))
;; Set exec path for shell
(setq exec-path
(append
(list "/usr/bin") exec-path))
(setenv "PATH"
(concat '"/usr/bin:/usr/local/bin:" (getenv "PATH")))
;; Package imports
(require 'python-mode)
(require 'haskell-mode)
(require 'sgml-mode)
(require 'css-mode)
(require 'gist)
(require 'tls)
(require 'erc-truncate)
(require 'ido)
(require 'color-theme)
(require 'erc-services)
(require 'nxml-mode)
(require 'xwidget)
;; Function: Nuke dashes
(defun nuke-dashes ()
(interactive) (save-excursion (goto-char (point-min)) (while (re-search-forward "^-" nil t) (replace-match ""))))
;; Function: Flymake temporary directory
(defun flymake-create-temp-intemp (file-name prefix)
"Return file name in temporary directory for checking FILE-NAME.
This is a replacement for `flymake-create-temp-inplace'. The
difference is that it gives a file name in
`temporary-file-directory' instead of the same directory as
FILE-NAME.
For the use of PREFIX see that function.
Note that not making the temporary file in another directory
\(like here) will not if the file you are checking depends on
relative paths to other files \(for the type of checks flymake
makes)."
(unless (stringp file-name)
(error "Invalid file-name"))
(or prefix
(setq prefix "flymake"))
(let* ((name (concat
(file-name-nondirectory
(file-name-sans-extension file-name))
"_" prefix))
(ext (concat "." (file-name-extension file-name)))
(temp-name (make-temp-file name nil ext))
)
(flymake-log 3 "create-temp-intemp: file=%s temp=%s" file-name temp-name)
temp-name))
;; Function: Initialize flymake
;;
;; ~/bin/pychecker.sh
;; ---------------------------------------------------------
;; #!/bin/bash
;; # PYTHON=`~/bin/pyidentify.py $1`
;; PYTHON=python2
;; # PYTHON=$PYTHON epylint "$1" 2>/dev/null
;; PYTHON=$PYTHON pyflakes "$1"
;; PYTHON=$PYTHON pep8 --ignore=E221,E701,E202 --repeat "$1"
;; true
(when (load "flymake" t)
(defun flymake-pyflakes-init ()
(let* ((temp-file (flymake-init-create-temp-buffer-copy
'flymake-create-temp-intemp))
(local-file (file-relative-name
temp-file
(file-name-directory buffer-file-name))))
(list "~/bin/pychecker.sh" (list local-file))))
(add-to-list 'flymake-allowed-file-name-masks
'("\\.py\\'" flymake-pyflakes-init)))
;; Function: Confirm exit
(defun confirm-exit-emacs ()
"ask for confirmation before exiting emacs"
(interactive)
(if (yes-or-no-p "Are you sure you want to exit? ")
(save-buffers-kill-emacs)))
;; Function: Window shifting. C-x-o lets us go forward a window (or several). This
;; one lets us go back one or more windows. From Glickstein.
(defun other-window-backward (&optional n)
"Select previous Nth window."
(interactive "P")
(other-window (- (prefix-numeric-value n))))
;;; Function: Count words in a region
(defun count-words-region (beginning end)
"Print number of words in the region."
(interactive "r")
(message "Counting words in region ... ")
;;; 1. Set up appropriate conditions.
(save-excursion
(let ((count 0))
(goto-char beginning)
;;; 2. Run the while loop.
(while (and (< (point) end)
(re-search-forward "\\w+\\W*" end t))
(setq count (1+ count)))
;;; 3. Send a message to the user.
(cond ((zerop count)
(message
"The region does NOT have any words."))
((= 1 count)
(message
"The region has 1 word."))
(t
(message
"The region has %d words." count))))))
;; Function: Visit ANSI-terminal (and give proper name)
(defun visit-ansi-term ()
"If we are in an *ansi-term*, rename it.
If there is no *ansi-term*, run it.
If there is one running, switch to that buffer."
(interactive)
(if (equal "*ansi-term*" (buffer-name))
(call-interactively 'rename-buffer)
(if (get-buffer "*ansi-term*")
(switch-to-buffer "*ansi-term*")
(ansi-term "/bin/bash"))))
;; pdbtrack constants
(defconst py-pdbtrack-stack-entry-regexp
; "^> \\([^(]+\\)(\\([0-9]+\\))\\([?a-zA-Z0-9_]+\\)()"
;; "^> \\(.*\\)(\\([0-9]+\\))\\([?a-zA-Z0-9_]+\\)()"
"> \\(.*\\)(\\([0-9]+\\))\\(.*\\)()"
;; "^ \\(.*\\)"
"Regular expression pdbtrack uses to find a stack trace entry.")
;; Function: Flymake error messages
;;
;; Additional functionality that makes flymake error messages appear
;; in the minibuffer when point is on a line containing a flymake
;; error. This saves having to mouse over the error, which is a
;; keyboard user's annoyance
;;flymake-ler(file line type text &optional full-file)
(defun show-fly-err-at-point ()
"If the cursor is sitting on a flymake error, display the
message in the minibuffer"
(interactive)
(let ((line-no (line-number-at-pos)))
(dolist (elem flymake-err-info)
(if (eq (car elem) line-no)
(let ((err (car (second elem))))
(message "%s" (fly-pyflake-determine-message err)))))))
(defun fly-pyflake-determine-message (err)
"pyflake is flakey if it has compile problems, this adjusts the
message to display, so there is one ;)"
(cond ((not (or (eq major-mode 'Python) (eq major-mode 'python-mode) t)))
((null (flymake-ler-file err))
;; normal message do your thing
(flymake-ler-text err))
(t ;; could not compile err
(format "compile error, problem on line %s" (flymake-ler-line err)))))
(defadvice flymake-goto-next-error (after display-message activate compile)
"Display the error in the mini-buffer rather than having to mouse over it"
(show-fly-err-at-point))
(defadvice flymake-goto-prev-error (after display-message activate compile)
"Display the error in the mini-buffer rather than having to mouse over it"
(show-fly-err-at-point))
(defadvice flymake-mode (before post-command-stuff activate compile)
"Add functionality to the post command hook so that if the
cursor is sitting on a flymake error the error information is
displayed in the minibuffer (rather than having to mouse over
it)"
(set (make-local-variable 'post-command-hook)
(cons 'show-fly-err-at-point post-command-hook)))
;; Function: Color theme 'Dark Bliss'
(defun color-theme-dark-bliss ()
""
(interactive)
(color-theme-install
'(color-theme-dark-bliss
((foreground-color . "#eeeeee")
(background-color . "#001122")
(background-mode . dark)
(cursor-color . "#ccffcc"))
(bold ((t (:bold t))))
(bold-italic ((t (:italic t :bold t))))
(default ((t (nil))))
(font-lock-builtin-face ((t (:foreground "#f0f0aa"))))
(font-lock-comment-face ((t (:italic t :foreground "#aaccaa"))))
(font-lock-delimiter-face ((t (:foreground "#aaccaa"))))
(font-lock-constant-face ((t (:bold t :foreground "#ffaa88"))))
(font-lock-doc-string-face ((t (:foreground "#eeccaa"))))
(font-lock-doc-face ((t (:foreground "#eeccaa"))))
(font-lock-reference-face ((t (:foreground "#aa99cc"))))
(font-lock-function-name-face ((t (:foreground "#ffbb66"))))
(font-lock-keyword-face ((t (:foreground "#ccffaa"))))
(font-lock-preprocessor-face ((t (:foreground "#aaffee"))))
(font-lock-string-face ((t (:foreground "#bbbbff")))))))
;; Function: Insert UNICODE character
(defun unicode-insert (char)
"Read a unicode code point and insert said character.
Input uses `read-quoted-char-radix'. If you want to copy
the values from the Unicode charts, you should set it to 16."
(interactive (list (read-quoted-char "Char: ")))
(ucs-insert char))
;; Use archive mode to open Python eggs
(add-to-list 'auto-mode-alist '("\\.egg\\'" . archive-mode))
(setq mac-command-key-is-meta nil)
(setq mac-option-modifier 'meta)
;; i already said hi..
(setq inhibit-startup-message t)
;; Show column-number in the mode line
(column-number-mode t)
;; setup path
(add-hook 'erc-join-hook 'erc-truncate-enable)
(setq minibuffer-frame-alist
'((top . 1) (left . 1) (width . 80) (height . 2)))
(setq-default c-basic-offset 4
tab-width 4
indent-tabs-mode nil)
(setq cssm-indent-level 4)
(setq cssm-newline-before-closing-bracket t)
(setq cssm-indent-function #'cssm-c-style-indenter)
(setq cssm-mirror-mode nil)
(setq-default
visible-bell t ;; Turns off audible bell
)
(setq-default indent-tabs-mode nil)
(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.
'(column-number-mode t)
'(grep-find-command "find -L . -not -path \"*svn*\" -not -path \"*.git*\" -type f -print0 | xargs -0 grep -n -e ")
'(history-delete-duplicates t)
'(icicle-reminder-prompt-flag 6)
'(paren-match-face (quote paren-face-match-light))
'(paren-sexp-mode t)
'(rst-level-face-base-color "blue")
'(safe-local-variable-values (quote ((encoding . utf-8))))
'(show-trailing-whitespace t)
'(tool-bar-mode nil)
'(uniquify-buffer-name-style (quote post-forward-angle-brackets) nil (uniquify))
'(whitespace-global-mode nil))
;; hl-line mode and colour
(global-hl-line-mode 1)
(show-paren-mode 0)
(scroll-bar-mode)
(setq my-color-themes (list 'color-theme-robin-hood 'color-theme-hober
'color-theme-resolve ))
(color-theme-initialize)
;; Use lazy lock for syntax coloring, otherwise large files will not
;; be colored. Also allows large files to load faster. Thanks Andrew
;; Innes
(cond ((fboundp 'global-font-lock-mode)
;; Turn on font-lock in all modes that support it
(global-font-lock-mode t)
;; Maximum colors
(setq font-lock-maximum-decoration t)
(setq font-lock-support-mode 'font-lock-mode)
(setq lazy-lock-defer-on-scrolling nil)
(setq lazy-lock-defer-time 1)
(setq lazy-lock-stealth-time 20)
(setq lazy-lock-stealth-lines 250)
(setq lazy-lock-stealth-verbose nil)
(setq font-lock-stealth-time 20)
(setq font-lock-stealth-lines 250)
(require 'font-lock)
))
(custom-set-faces
;; 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 "#001122" :foreground "#eeeeee" :inverse-video nil :box nil :strike-through nil :overline nil :underline nil :slant normal :weight normal :height 90 :width normal :foundry "unknown" :family "DejaVu Sans Mono"))))
'(bold-italic ((t (:weight bold))))
'(cursor ((t (:background "#fce94f" :foreground "black"))))
'(fixed-pitch ((t nil)))
'(font-lock-function-name-face ((t (:foreground "#edd400" :weight bold))))
'(font-lock-string-face ((t (:foreground "#ad7fa8"))))
'(hl-line ((t (:background "#555555" :inverse-video nil))))
'(italic ((((supports :slant italic)) nil)))
'(mouse ((t (:background "#8ae234" :foreground "black"))))
'(paren-face-match ((((class color)) nil)))
'(paren-face-match-light ((((class color)) (:underline t))))
'(twitter-header-face ((t (:background "purple"))))
'(whitespace-highlight ((((class color) (background dark)) (:background "sea green")))))
(set-face-foreground 'hl-line nil)
;; Answer yes or no questions with y or n
(fset 'yes-or-no-p 'y-or-n-p)
;; pdb.set_trace() macro
(fset 'pdb-set
"import pdb; pdb.set_trace()")
(setq auto-mode-alist
(cons '("\\.\\(cpy\\|vpy\\)\\'" . python-mode)
auto-mode-alist))
;; Truncate lines, see http://www.emacswiki.org/cgi-bin/wiki/TruncateLines
(setq truncate-partial-width-windows nil)
;; ido configuration
(ido-mode t)
(ido-everywhere t)
(set-face-background 'ido-first-match "black")
(set-face-foreground 'ido-subdir "pink")
(icomplete-mode 1)
(setq ido-enable-flex-matching t)
(setq ido-auto-merge-work-directories-length -1)
(setq ido-default-buffer-method 'samewindow)
(setq ido-show-dot-for-dired 1)
(setq ido-confirm-unique-completion 0)
(setq ido-ignore-extensions 1)
; sort ido filelist by mtime instead of alphabetically
(add-hook 'ido-make-file-list-hook 'ido-sort-mtime)
(add-hook 'ido-make-dir-list-hook 'ido-sort-mtime)
(defun ido-sort-mtime ()
(setq ido-temp-list
(sort ido-temp-list
(lambda (a b)
(let ((ta (nth 5 (file-attributes (concat ido-current-directory a))))
(tb (nth 5 (file-attributes (concat ido-current-directory b)))))
(if (= (nth 0 ta) (nth 0 tb))
(> (nth 1 ta) (nth 1 tb))
(> (nth 0 ta) (nth 0 tb)))))))
(ido-to-end ;; move . files to end (again)
(delq nil (mapcar
(lambda (x) (if (string-equal (substring x 0 1) ".") x))
ido-temp-list))))
(add-to-list 'ido-ignore-buffers "^ ")
(add-to-list 'ido-ignore-buffers "*Messages*")
(add-to-list 'ido-ignore-buffers "*Buffer*")
(add-to-list 'ido-ignore-buffers "*Completions*")
(add-to-list 'ido-ignore-buffers "*About*")
(setq completion-ignored-extensions
(append completion-ignored-extensions '(".pyc" ".egg-info")))
;; encoding
(setq locale-coding-system 'utf-8)
(set-terminal-coding-system 'utf-8)
(prefer-coding-system 'utf-8)
;; major mode hooks
(autoload 'css-mode "css-mode" "Mode for editing CSS files" t)
(autoload 'kss-mode "kss-mode" "Mode for editing KSS files" t)
(setq auto-mode-alist
(append '(("\\.css$" . css-mode))
auto-mode-alist))
(setq auto-mode-alist
(append '(("\\.kss$" . kss-mode))
auto-mode-alist))
(setq auto-mode-alist
(append '(("\\.css.dtml$" . css-mode))
auto-mode-alist))
(setq auto-mode-alist
(append '(("\\.pt$" . nxml-mode))
auto-mode-alist))
(setq auto-mode-alist
(append '(("\\.zcml$" . nxml-mode))
auto-mode-alist))
(autoload 'css-mode "css-mode")
(setq auto-mode-alist
(cons '("\\.css\\'" . css-mode) auto-mode-alist))
(setq cssm-indent-function #'cssm-c-style-indenter)
(autoload 'js2-mode "js2" nil t)
(add-to-list 'auto-mode-alist '("\\.js$" . js2-mode))
(setq make-backup-files nil)
(autoload 'perl-mode "cperl-mode" "alternate mode for editing Perl programs"
t) (setq interpreter-mode-alist (append
interpreter-mode-alist '(("miniperl" .
perl-mode)))) (setq auto-mode-alist (append ' (("\\.[pP][Llm]$" . perl-mode)
("\\.h\\'" . c++-mode) ("\\.c\\'" .
c++-mode)) auto-mode-alist)) (setq cperl-indent-level 2) (setq
cperl-brace-offset -2) (setq cperl-continued-statement-offset 2) (setq
cperl-electric-keywords nil)
(add-to-list 'auto-mode-alist '("\\.org\\'" . org-mode))
(add-hook 'org-mode-hook 'turn-on-font-lock)
;; Color theme
(color-theme-dark-bliss)
;; Clipboard
(global-set-key "\C-w" 'clipboard-kill-region)
(global-set-key "\M-w" 'clipboard-kill-ring-save)
(global-set-key "\C-y" 'clipboard-yank)
(global-set-key (kbd "M-v") 'yank)
;; Nuke dashes command (experimental)
(global-set-key "\C-d" 'nuke-dashes)
;; Don't need M-x to execute a command
(global-set-key "\C-x\C-m" 'execute-extended-command)
(global-set-key "\C-c\C-m" 'execute-extended-command)
;; Confirm exit
(global-set-key "\C-x\C-c" 'confirm-exit-emacs)
;; More keys to invoke auto-complete
(global-set-key "\M-\\" 'dabbrev-expand)
(global-set-key [?\s-\\] 'dabbrev-expand)
;; Flymake start/stop
(global-set-key "\C-F" 'flymake-mode)
;; Python PDB trace statement insert
(global-set-key "\M-t" 'pdb-set)
(global-set-key "\M-p" 'pdb-set)
;; Some nice keys to move around in the buffer
(global-set-key [?\C-<] 'bs-cycle-next)
(global-set-key [?\C->] 'bs-cycle-previous)
(global-set-key [?\C-.] 'bs-cycle-next)
(global-set-key [?\C-,] 'bs-cycle-previous)
(global-set-key "\C-m" 'newline-and-indent)
;; Org-mode keys
(global-set-key "\C-cl" 'org-store-link)
(global-set-key "\C-ca" 'org-agenda)
(global-set-key "\C-cb" 'org-iswitchb)
;; Window movement (backwards)
(global-set-key "\C-x\p" 'other-window-backward)
;; Open ANSI-terminal
(global-set-key (kbd "C-=") 'visit-ansi-term)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment