Friday, February 26, 2010

Emacs Fun

Since life has been a bit slow lately, I've returned to playing with Emacs.  I first delved into it during my sophomore year at CSU.  Since I was using Linux full-time at home (Windows having been relegated to its proper status as a gaming platform), I had decided it would be beneficial to learn a decent text editor.

Yes, insert any jokes here about Emacs being a great OS with a crappy text-editor :)

Anyway, I had already used VI and I decided it was a little too cumbersome for me.  Yes, I recognize the insanity of Emacs key-strokes (and the nasty Emacs-pinky syndrome), but for some reason it made more sense to me than VI did.

Anyway, I used Emacs full-time in several classes (C++ being one of them); when I started my first professional tech job (as a software tester), I abandoned Emacs for Eclipse.

Of course, apples and oranges; Eclipse is nothing short of amazing for programming Java, and really impressive for other languages (namely Python and Perl).

Since I've moved onto full-time software development, my inclination to use Emacs has further waned.  I'm working in FlexBuilder and Eclipse full-time, and I don't have much use for it.  Except at home where I recently have done quite a bit of Perl and Python work, as well as text-editing.

As a result, I decided to delve back into the murky waters of Emacs and setup my environment so it does everything I need.  It's been a ton of fun, and my environment has been pretty simple.

Here's my .emacs file.  Note, much of this functionality is not my own.  I've tried to give credit where it's due, but there are a few function calls I wouldn't have known without looking stuff up.  Regardless, this is what I've come up with so far.  I'm running Emacs23, so some of these tweaks might be specific to this version.


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; SETUP THE BASIC LAYOUT/USE OF EMACS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; set font size
(set-face-attribute 'default nil :height 110)

; turn on line numbers
(global-linum-mode 1)
(show-paren-mode 1)

; Parenthesis balancing
(setq show-paren-delay .25)
(show-paren-mode t)
(set-face-background 'show-paren-match-face "#aaaaaa")
(set-face-attribute 'show-paren-match-face nil
  :weight 'bold :underline nil :overline nil :slant 'normal)

; inhibit startup stuff
(setq initial-scratch-message nil)
(setq inhibit-startup-screen t)

; don't load default.el
(setq inhibit-default-init t)

; show a visible highlight
(setq transient-mark-mode t)

; show the buffer name in the window title
(setq frame-title-format '(buffer-file-name "%f" ("%b")))

; remove menus, toolbars, and scrollbars
(if (fboundp 'tool-bar-mode) (tool-bar-mode -1))
(if (fboundp 'menu-bar-mode) (menu-bar-mode -1))
(if (fboundp 'scroll-bar-mode) (scroll-bar-mode -1))

; Modify where backups/bookmarks are stored
(setq backup-directory-alist '(("" . "~/.emacs.d/backups")))

; Replace M-x with C-x C-m or C-x C-c
(global-set-key "\C-x\C-m" 'execute-extended-command)
(global-set-key "\C-c\C-m" 'execute-extended-command)

; don't continue to scroll at bottom of file
(setq next-line-add-newlines nil)
; ask y/n and yes/no
(fset 'yes-or-no-p 'y-or-n-p)

; Enable upcase-/downcase-region
(put 'downcase-region 'disabled nil)
(put 'upcase-region 'disabled nil)

; make text-mode default
(setq default-major-mode 'text-mode)

; don't blink cursor
(blink-cursor-mode nil)

; Highlight regions while searching/replacing
(setq search-highlight t
  query-replace-highlight t)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; SETUP CUSTOM KEYSTROKES FOR DOING MUNDANE TASKS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(global-unset-key "\C-xd")
(global-unset-key "\C-xm")
(global-unset-key "\C-z")
(global-unset-key "\C-x\C-z")
(global-unset-key "\C-l")

(global-set-key "\C-l"   'goto-line)
(global-set-key "\C-xdd" 'kill-whole-line)
(global-set-key "\C-xcw" 'kill-word)
(global-set-key "\C-x%"  'forward-list)
(global-set-key "\C-c%"  'backward-list)
(global-set-key "\C-c."  'command-history)
(global-set-key "\C-x:"  'cmd)
(global-set-key "\C-z"   'zap-to-char)
(global-set-key "\C-cc"  'calendar)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ALIASES
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defalias 'sh    'shell)
(defalias 'rr    'replace-string)
(defalias 'qrr   'query-replace-regexp)
(defalias 'cmp   'compile)
(defalias 'rcmp  'recompile)
(defalias 'trunc 'toggle-truncate-lines)
(defalias 'll    'load-library)
(defalias 'ind   'indent-region)
(defalias 'cmd   'shell-command)
(defalias 'cmdr  'shell-command-region)
(defalias 'lbf   'list-buffers)
(defalias 'sbf   'switch-to-buffer)
(defalias 'kbf   'kill-buffer)
(defalias 'spell 'ispell)
(defalias 'moon  'phases-of-moon)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; SPELLING
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(setq ispell-program-name "aspell"
  ispell-extra-args '("--sug-mode=ultra"))

; When we're in certain modes, turn on flyspell
(add-hook 'text-mode 'flyspell-mode)

(add-hook 'org-mode  'flyspell-mode)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; CALENDAR STUFF
;; Taken from:
;;   http://www.mygooglest.com/fni/dot-emacs.html
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(setq calendar-christian-all-holidays-flag t)
(setq holiday-islamic-holidays nil)
(setq holiday-oriental-holidays nil)
(setq holiday-bahai-holidays nil)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; CUT/COPY AND PASTE BEHAVIOR
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; modify cut/copy behavior: if no line is selected, cut/copy entire line
(defadvice kill-ring-save (before slick-copy activate compile) "When called
  interactively with no active region, copy a single line instead."
  (interactive (if mark-active (list (region-beginning) (region-end)) (message
  "Copied line") (list (line-beginning-position) (line-beginning-position
  2)))))

(defadvice kill-region (before slick-cut activate compile)
  "When called interactively with no active region, kill a single line instead."
  (interactive
    (if mark-active (list (region-beginning) (region-end))
      (list (line-beginning-position)
        (line-beginning-position 2)))))


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; LOAD THE ~/.emacs.d PATH RECURSIVELY
;; Taken from: http://www.djcbsoftware.nl/dot-emacs.html
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(let* ((my-lisp-dir "~/.emacs.d/")
       (default-directory my-lisp-dir))
  (setq load-path (cons my-lisp-dir load-path))
  (normal-top-level-add-subdirs-to-load-path))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; RECOMPILE ~/.emacs ON SAVE
;; Taken from:
;;   http://www.emacswiki.org/emacs/DotEmacsChallenge
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun autocompile nil
  "This function compiles the current file iff it is ~/.emacs"
  (interactive)
  (require 'bytecomp)
  (if (string= (buffer-file-name) (expand-file-name (concat default-directory ".emacs")))
      (byte-compile-file (buffer-file-name))))

(add-hook 'after-save-hook 'autocompile)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; COLOR THEME STUFF
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(require 'color-theme)
(color-theme-charcoal-black)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; TABBAR SETUP
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(dolist (func '(tabbar-mode tabbar-forward-tab tabbar-forward-group tabbar-backward-tab tabbar-backward-group))
  (autoload func "tabbar" "Tabs at the top of buffers and easy control-tab navigation"))
    
(defmacro defun-prefix-alt (name on-no-prefix on-prefix &optional do-always)
  `(defun ,name (arg)
      (interactive "P")
        ,do-always
        (if (equal nil arg)
          ,on-no-prefix
          ,on-prefix)))
    
(defun-prefix-alt shk-tabbar-next (tabbar-forward-tab) (tabbar-forward-group) (tabbar-mode 1))
(defun-prefix-alt shk-tabbar-prev (tabbar-backward-tab) (tabbar-backward-group) (tabbar-mode 1))

(global-set-key [(control tab)] 'shk-tabbar-next)
(global-set-key [(control shift tab)] 'shk-tabbar-prev)

No comments:

Post a Comment