.emacs

My .emacs file is a collection of hacks, both stolen from the internet and written myself.

LISP:
  1. ;; -----------------------------------------------------------------------
  2. ;; Elliot's .emacs file
  3. ;; -----------------------------------------------------------------------
  4.  
  5. ;; -----------------------------------------------------------------------
  6. ;; Garbage Collector
  7. ;; -----------------------------------------------------------------------
  8. ;; We have extra ram these days. Use 3 megs of memory to speed up
  9. ;; runtime a bit. This shaves off about 1/10 of a second on startup.
  10. (setq gc-cons-threshold (max 3000000 gc-cons-threshold))
  11.  
  12. ;; -----------------------------------------------------------------------
  13. ;; Platform Selection
  14. ;; -----------------------------------------------------------------------
  15. (defvar running-apple (string-match "apple" system-configuration))
  16.  
  17. (defmacro Xlaunch (&rest x)
  18.   (list 'if (eq window-system 'x) (cons 'progn x)))
  19. (defmacro Aqua (&rest x)
  20.   (list 'if (and (eq window-system 'mac) running-apple) (cons 'progn x)))
  21.  
  22. ;; -----------------------------------------------------------------------
  23. ;; User interface
  24. ;; -----------------------------------------------------------------------
  25. ;; ------------------------------------------ [ Initial position for X11 ]
  26. (Xlaunch
  27.  (setq default-frame-alist
  28.        '((wait-for-wm . nil)
  29.          (top . 0)
  30.          (width . 85)
  31.          (height . 75)
  32.          (tool-bar-lines . 0)
  33.          (menu-bar-lines . 0))))
  34.  
  35. ;; ------------------------------------------ [ Initial position for Mac ]
  36. (Aqua
  37.  (setq default-frame-alist
  38.        '((wait-for-wm . nil)
  39.          (top . 0)
  40.          (width . 85)
  41.          (height . 57)
  42.          (tool-bar-lines . 0)
  43.          (menu-bar-lines . 0))))
  44.  
  45. ;; Interface needs to be minimal...
  46. (put 'scroll-left 'disabled nil)
  47. (if (fboundp 'tool-bar-mode) (tool-bar-mode -1))
  48. (if (fboundp 'menu-bar-mode) (menu-bar-mode -1))
  49. (if (fboundp 'blink-cursor-mode) (blink-cursor-mode -1))
  50. (transient-mark-mode t)
  51.  
  52. ;; -----------------------------------------------------------------------
  53. ;; Load paths
  54. ;; -----------------------------------------------------------------------
  55. (defvar my-load-path (expand-file-name "~/.elliot-unix/emacs/site-lisp"))
  56. (add-to-list 'load-path my-load-path)
  57. (setq byte-compile-warnings nil)
  58.  
  59. ;; -----------------------------------------------------------------------
  60. ;; Color Themes
  61. ;; -----------------------------------------------------------------------
  62. (require 'color-theme)
  63. ;;(setq color-theme-is-global t)
  64. ;; Use color themes only in windowed modes.
  65. (cond (window-system
  66.        (require 'color-theme-black-forest)
  67.        (color-theme-black-forest)))
  68.  
  69. ;; -----------------------------------------------------------------------
  70. ;; Personal Keybindings
  71. ;; -----------------------------------------------------------------------
  72. (global-set-key '[f2] 'grepsource)
  73. (global-set-key '[f3] 'svn-status)
  74. (global-set-key '[f4] 'gdb)
  75. (global-set-key '[f5] 'compile)
  76. ;;;;;;; DO NOT USE [f9], [f10], [f11], OR [f12]. They're taken by the
  77. ;;;;;;; OS on OSX for the Expose stuff.
  78.  
  79. (global-set-key "\C-x\M-f" 'find-file-at-point)
  80. (global-set-key "\M-gc" 'goto-char)
  81.  
  82. ;;; Unbind the stupid minimize that I always hit.
  83. (global-unset-key "\C-z")
  84.  
  85. (global-set-key [(shift home)] '(lambda () (interactive) (other-window -1)))
  86. (global-set-key [(shift end)] '(lambda () (interactive) (other-window 1)))
  87.  
  88. ;; Setup hippie-expand (we're going to have to make an eval-after-load
  89. ;; section later)
  90. (global-set-key "\M-/" 'hippie-expand)
  91. (setq hippie-expand-try-functions-list
  92.       '(try-expand-dabbrev
  93.         try-complete-file-name
  94.         try-expand-all-abbrevs
  95.         try-expand-list
  96.         try-expand-dabbrev-all-buffers
  97.         try-expand-dabbrev-from-kill))
  98.  
  99. ;; -----------------------------------------------------------------------
  100. ;; Global Hook Additions
  101. ;; -----------------------------------------------------------------------
  102. ;;(add-hook 'before-save-hook 'copyright-update)
  103. ;;(setq-default copyright-query nil)
  104.  
  105. ;; -----------------------------------------------------------------------
  106. ;; Initial register values
  107. ;; -----------------------------------------------------------------------
  108. (set-register ?e '(file . "~/.elliot-unix/emacs/emacs"))
  109. (cond ((file-exists-p (expand-file-name "~/Projects/rldev"))
  110.        (set-register ?k '(file . "~/Projects/rldev/lib/reallive.kfn")))
  111.       ((file-exists-p (expand-file-name "~/rldev"))
  112.        (set-register ?k '(file . "~/rldev/lib/reallive.kfn"))))
  113.  
  114. ;; -----------------------------------------------------------------------
  115. ;; Autoloads (aka, the way to make emacs fast)
  116. ;; -----------------------------------------------------------------------
  117. (autoload 'hide-ifdef-define "hideif" nil t)
  118. (autoload 'hide-ifdef-undef  "hideif" nil t)
  119. (autoload 'c-mode "cc-mode" "C Editing Mode" t)
  120. (autoload 'c++-mode "cc-mode" "C++ Editing Mode" t)
  121. (autoload 'objc-mode "cc-mode" "ObjC Editing Mode" t)
  122. (autoload 'ruby-mode "ruby-mode" "Major mode for editing ruby scripts." t)
  123. (autoload 'bison-mode "bison-mode" "Major mode for editing yacc files." t)
  124. (autoload 'flex-mode "flex-mode" "Major mode for editing lex files." t)
  125. (autoload 'make-regexp "make-regexp"
  126.   "Return a regexp to match a string item in STRINGS." t)
  127. (autoload 'make-regexps "make-regexp"  "Return a regexp to REGEXPS." t)
  128. (autoload 'ascii-table "ascii-table" "Displays the ASCII table." t)
  129. (autoload 'filladapt-mode "filladapt" "Filladapt mode." t)
  130. (autoload 'doxymacs-mode "doxymacs" "Deal with doxygen." t)
  131. (autoload 'cg-mode "cg-mode" "Edit files written in nVidia's Cg language." t)
  132. (autoload 'lua-mode "lua-mode" "Lua editing mode." t)
  133. (autoload 'rubydb "rubydb3x" "Ruby debugger" t)
  134.  
  135. (autoload 'svn-status "psvn" "psvn.el status mode." t)
  136. (autoload 'malyon "malyon" "Z-Machine interpreter." t)
  137.  
  138. ;; Autoloads to my custom header inserters
  139. (autoload 'insert-c++-seperator-line "e-seperators" nil t)
  140. (autoload 'insert-c-seperator-line "e-seperators" nil t)
  141. (autoload 'insert-elisp-seperator-line "e-seperators" nil t)
  142. (autoload 'insert-script-seperator-line "e-seperators" nil t)
  143. (autoload 'insert-c-section-header "e-seperators" nil t)
  144. (autoload 'insert-c++-section-header "e-seperators" nil t)
  145. (autoload 'insert-elisp-section-header "e-seperators" nil t)
  146. (autoload 'insert-script-section-header "e-seperators" nil t)
  147. (autoload 'insert-c++-big-header "e-seperators" nil t)
  148. (autoload 'insert-elisp-big-header "e-seperators" nil t)
  149. (autoload 'insert-script-big-header "e-seperators" nil t)
  150. (autoload 'insert-text-seperator-line "e-seperators" nil t)
  151.  
  152. ;; RLVM and rldev related modes
  153. (autoload 'kfn-mode "kfn-mode" nil t)
  154. (autoload 'kepago-mode "kepago-mode" nil t)
  155.  
  156. ;; -----------------------------------------------------------------------
  157. ;; auto-mode-alist
  158. ;; -----------------------------------------------------------------------
  159. (setq auto-mode-alist
  160.       (append '(("\\.[Cc][Xx][Xx]$" . c++-mode)
  161.                 ("\\.[Cc][Pp][Pp]$" . c++-mode)
  162.                 ("\\.[Hh][Xx][Xx]$" . c++-mode)
  163.                 ("\\.[Tt][Cc][Cc]$" . c++-mode)
  164.                 ("\\.h$" . c++-mode)
  165.                 ("\\.i$" . c++-mode)    ; SWIG
  166.                 ("\\.mm?$" . objc-mode)
  167.                 ("_emacs" . lisp-mode)
  168.                 ("\\.el\\.gz$" . lisp-mode)
  169.                 ("\\.mak$" . makefile-mode)
  170.                 ("\\.conf$" . conf-mode)
  171.                 ("Doxyfile.tmpl$" . makefile-mode)
  172.                 ("Doxyfile$" . makefile-mode)
  173.                 ("\\.ke$" . kepago-mode)
  174.                 ("\\.kfn$" . kfn-mode)
  175.                 ("\\.rb$" . ruby-mode)
  176.                 ("\\.cml$" . xml-mode)
  177.                 ("\\.cg$" . cg-mode)
  178.                 ("\\.y$" . bison-mode)
  179.                 ("\\.yy$" . bison-mode)
  180.                 ("\\.l$" . flex-mode)
  181.                 ("\\.ll$" . flex-mode)
  182.                 ("\\.lua$" . lua-mode)
  183.                 ("\\.org$" . org-mode)
  184.                 ) auto-mode-alist))
  185.  
  186. (setq interpreter-mode-alist
  187.       (append '(("ruby" . ruby-mode))
  188.               interpreter-mode-alist))
  189.  
  190. ;; -----------------------------------------------------------------------
  191. ;; Startup variables
  192. ;; -----------------------------------------------------------------------
  193. (setq user-full-name "Elliot Glaysher"
  194.           user-mail-address "glaysher@umich.edu"
  195.  
  196.       enable-local-variables :safe
  197.       inhibit-startup-message t
  198.       default-major-mode 'text-mode
  199.       require-final-newline t
  200.       default-tab-width 4
  201.       frame-title-format (concat user-login-name "@" system-name))
  202.  
  203. (add-hook 'suspend-hook 'do-auto-save) ;; Auto-Save on ^Z
  204.  
  205. (setq-default echo-keystrokes 2
  206.               next-screen-context-lines 4
  207.               compilation-scroll-output t
  208.                           indent-tabs-mode nil
  209.               tags-revert-without-query t)
  210.  
  211. (put 'eval-expression 'disabled nil)
  212. (fset 'yes-or-no-p 'y-or-n-p) ;; Make all yes-or-no questions as y-or-n
  213.  
  214. ;; -----------------------------------------------------------------------
  215. ;; Modules loaded at startup (and their configuration)
  216. ;; -----------------------------------------------------------------------
  217. ;; ----------------------------------------------------------- [ icicles ]
  218. (require 'icicles)
  219. (icy-mode 1)
  220. (define-key icicle-mode-map [f5] nil)          ;; Give me my compile back!
  221. (setq icicle-reminder-prompt-flag 0)
  222.  
  223. ;; ---------------------------------------------------------- [ diminish ]
  224. ;; Makes minor mode names in the modeline shorter.
  225. (require 'diminish)
  226. (diminish 'icicle-mode "")
  227.  
  228. (eval-after-load "filladapt"
  229.   '(diminish 'filladapt-mode "Fill"))
  230. (eval-after-load "abbrev"
  231.   '(diminish 'abbrev-mode "Abv"))
  232. (eval-after-load "doxymacs"
  233.   '(diminish 'doxymacs-mode "dox"))
  234.  
  235. ;; -------------------------------------------------------- [ backup-dir ]
  236. ;; Changes the location where backup files are placed. Instead of
  237. ;; being spread out all over the filesystem, they're now placed in one
  238. ;; location.
  239. (if (file-accessible-directory-p (expand-file-name "~/.Trash"))
  240.         (add-to-list 'backup-directory-alist
  241.                                  (cons "." (expand-file-name "~/.Trash/emacs-backups/"))))
  242.  
  243. ;; ------------------------------------------------------------- [ pager ]
  244. ;;; Excellent package for better scrolling in emacs
  245. ;;; should be default package. But now it can be downloaded
  246. ;;; from: http://user.it.uu.se/~mic/pager.el
  247. (require 'pager)
  248. (global-set-key "\C-v"     'pager-page-down)
  249. (global-set-key [next]     'pager-page-down)
  250. (global-set-key "\ev"      'pager-page-up)
  251. (global-set-key [prior]    'pager-page-up)
  252. (global-set-key '[M-up]    'pager-row-up)
  253. (global-set-key '[M-kp-8]  'pager-row-up)
  254. (global-set-key '[M-down]  'pager-row-down)
  255. (global-set-key '[M-kp-2]  'pager-row-down)
  256.  
  257. ;; -------------------------------------------------- [ browse-kill-ring ]
  258. ;; Select something that you put in the kill ring ages ago.
  259. (autoload 'browse-kill-ring "browse-kill-ring" "Browse the kill ring." t)
  260. (global-set-key (kbd "C-c k") 'browse-kill-ring)
  261.  
  262. ;; ------------------------------------------------------------- [ shell ]
  263. (eval-after-load "shell"
  264.   '(progn
  265.      (ansi-color-for-comint-mode-on)))
  266.  
  267. ;; ------------------------------------------------------------ [ ispell ]
  268. (eval-after-load "ispell"
  269.   '(progn
  270.          ;; Use the -C option when running aspell, which will
  271.          ;; ConsiderCamelCaseToBeCorrect
  272.          (setq ispell-extra-args '("-C"))))
  273. ;;"-W" "3"))))
  274.  
  275. ;; ------------------------------------------------------------- [ tramp ]
  276. (eval-after-load "tramp"
  277.   '(add-to-list 'tramp-default-method-alist
  278.                 '(".*\\.umich\\.edu\\'" "" "ssh")))
  279.  
  280. ;; ----------------------------------------------------------- [ flymake ]
  281. ;; Mostly stolen from http://www.emacswiki.org/cgi-bin/emacs-en/FlymakeRuby
  282. (eval-after-load "flymake"
  283.   '(progn
  284.      (push '(".+\\.rb$" flymake-ruby-init) flymake-allowed-file-name-masks)
  285.      (push '("Rakefile$" flymake-ruby-init) flymake-allowed-file-name-masks)
  286.  
  287.      (push '("^\\(.*\\):\\([0-9]+\\): \\(.*\\)$" 1 2 nil 3)
  288.            flymake-err-line-patterns)
  289.  
  290.      (set-face-background 'flymake-errline "red4")
  291.      (set-face-background 'flymake-warnline "dark slate blue")))
  292.  
  293. ;; ----------------------------------------------------------- [ ibuffer ]
  294. ;; *Nice* buffer switching
  295. (global-set-key (kbd "C-x C-b") 'ibuffer)
  296.  
  297. (setq ibuffer-show-empty-filter-groups nil)
  298. (setq ibuffer-saved-filter-groups
  299.       '(("default"
  300.          ("svn" (or (mode . svn-status-mode)
  301.                     (mode . svn-log-edit-mode)
  302.                     (name . "^\\*svn-")
  303.                     (name . "^\\*vc\\*$")
  304.                     (name . "^\\*Annotate")
  305.                     (name . "^\\*vc-")))
  306.          ("emacs" (or (name . "^\\*scratch\\*$")
  307.                       (name . "^\\*Messages\\*$")
  308.                       (name . "^\\*Help\\*$")
  309.                                           (name . "^\\*info\\*$")
  310.                                           (name . "^\\*Occur\\*$")
  311.                       (name . "^\\*grep\\*$")
  312.                       (name . "^\\*Compile-Log\\*$")
  313.                       (name . "^\\*Backtrace\\*$")
  314.                                           (name . "^\\*Process List\\*$")
  315.                                           (name . "^\\*gud\\*$")
  316.                                           (name . "^\\*Man")
  317.                                           (name . "^\\*WoMan")
  318.                       (name . "^\\*Kill Ring\\*$")
  319.                       (name . "^\\*Completions\\*$")
  320.                       (name . "^\\*tramp")
  321.                       (name . "^\\*shell\\*$")
  322.                       (name . "^\\*compilation\\*$")))
  323.          ("emacs source" (or (mode . emacs-lisp-mode)
  324.                                                          (filename . "/Applications/Emacs.app")
  325.                              (filename . "/bin/emacs")))
  326.          ("agenda" (or (name . "^\\*Calendar\\*$")
  327.                        (name . "^diary$")
  328.                        (name . "^\\*Agenda")
  329.                        (name . "^\\*org-")
  330.                        (name . "^\\*Org")
  331.                        (mode . org-mode)
  332.                        (mode . muse-mode)))
  333.          ("latex" (or (mode . latex-mode)
  334.                       (mode . LaTeX-mode)
  335.                       (mode . bibtex-mode)
  336.                       (mode . reftex-mode)))
  337.          ("dired" (or (mode . dired-mode))))))
  338.  
  339. (add-hook 'ibuffer-mode-hook
  340.           (lambda ()
  341.             (ibuffer-switch-to-saved-filter-groups "default")))
  342.  
  343. ;; Order the groups so the order is : [Default], [agenda], [emacs]
  344. (defadvice ibuffer-generate-filter-groups (after reverse-ibuffer-groups ()
  345.                                                  activate)
  346.   (setq ad-return-value (nreverse ad-return-value)))
  347.  
  348. ;; ------------------------------------------------------------- [ tempo ]
  349. (defun tempo-space ()
  350.   (interactive "*")
  351.   (or (tempo-expand-if-complete)
  352.           (insert " ")))
  353.  
  354. ;; ---------------------------------------------------------- [ org-mode ]
  355. (define-key global-map "\C-cl" 'org-store-link)
  356. (define-key global-map "\C-ca" 'org-agenda)
  357. (define-key global-map "\C-co" 'org-jump-to-project-todo)
  358.  
  359. (defun org-jump-to-project-todo ()
  360.   (interactive)
  361.   (cond (local-project-root
  362.          (find-file-other-window
  363.           (expand-file-name (concat "~/org/" local-project-name ".org"))))
  364.         (t (message "No project set!"))))
  365.  
  366. (eval-after-load 'org
  367.   '(progn
  368.          ;; Add all org files in the org directory to the agenda
  369.          (mapcar
  370.           (lambda (file)
  371.                   (add-to-list 'org-agenda-files file))
  372.           (directory-files (expand-file-name "~/org/") t "\\.org"))))
  373.  
  374. ;; ----------------------------------------------------------- [ doxygen ]
  375. ;; Disable doxymacs prompts
  376. (defadvice doxymacs-insert-function-comment (around no-prompt activate compile)
  377.   "Prevents tempo from prompting during doxymacs template insertion"
  378.   (let ((tempo-interactive nil)) ad-do-it))
  379.  
  380. (defadvice doxymacs-insert-file-comment  (around no-prompt activate compile)
  381.   "Prevents tempo from prompting during doxymacs template insertion"
  382.   (let ((tempo-interactive nil)) ad-do-it))
  383.  
  384. ;; -----------------------------------------------------------------------
  385. ;; Utility Methods
  386. ;; -----------------------------------------------------------------------
  387. ;; Taken from O'Reilly Writing Emacs Extensions p 30-31
  388.  
  389. ;; Restrict buffer movement to existing buffers
  390. (defadvice switch-to-buffer (before existing-buffer activate compile)
  391.   "When interactive, switch to existing buffers only, unless given a
  392. prefix argument"
  393.   (interactive
  394.    (list (read-buffer "Switch to buffer: "
  395.               (other-buffer)
  396.               (null current-prefix-arg)))))
  397.  
  398. ;; -----------------------------------------------------------------------
  399.  
  400. (defadvice switch-to-buffer-other-window (before existing-buffer
  401.                          activate compile)
  402.   "When interactive, switch to existing buffers only, unless given a
  403. prefix argument"
  404.   (interactive
  405.    (list (read-buffer "Switch to buffer: "
  406.               (other-buffer)
  407.               (null current-prefix-arg)))))
  408.  
  409. ;; -----------------------------------------------------------------------
  410.  
  411. (defadvice switch-to-buffer-other-frame (before existing-buffer activate
  412.                         compile)
  413.   "When interactive, switch to existing buffers only, unless given a
  414. prefix argument"
  415.   (interactive
  416.    (list (read-buffer "Switch to buffer: "
  417.               (other-buffer)
  418.               (null current-prefix-arg)))))
  419.  
  420. ;; -------------------------------------------------------- [ grepsource ]
  421. (defconst grepsource-command
  422.   "find . -name "*.c" -or -name "*.cc" -or -name "*.cpp" -or -name "*.m" -or -name "*.mm" -or -name "*.java" -or -name "*.h" -or -name "*.hh" -or -name "*.hpp" -or -name "*.el" | xargs grep -n "
  423.   "Base command for grepsource. (Basically quoted version of
  424. whatever my current grepsource alias is.")
  425.  
  426. (defun grepsource (cmd-args)
  427.   "Invoke an in-emacs equivalent of my grepsource bash alias,
  428. defaulting withing the current project."
  429.   (interactive (list (read-from-minibuffer "Grep project for string: ")))
  430.   (let ((default-directory (or local-project-root default-directory)))
  431.     (grep (concat grepsource-command """
  432.                   (replace-regexp-in-string """ "\\\\"" cmd-args) """))))
  433.  
  434. ;; ------------------------------------------------- [ intelligent-close ]
  435. (defun intelligent-close ()
  436.   "quit a frame the same way no matter what kind of frame you are on.
  437. This method, when bound to C-x C-c, allows you to close an emacs frame the
  438. same way, whether it's the sole window you have open, or whether it's
  439. a "child" frame of a "parent" frame.  If you're like me, and use emacs in
  440. a windowing environment, you probably have lots of frames open at any given
  441. time.  Well, it's a pain to remember to do Ctrl-x 5 0 to dispose of a child
  442. frame, and to remember to do C-x C-x to close the main frame (and if you're
  443. not careful, doing so will take all the child frames away with it).  This
  444. is my solution to that: an intelligent close-frame operation that works in