This web site is partly supported by these ads:
Defindit Docs and Howto Home

This page last modified: Aug 09 2011
;; Tom Laudeman's .emacs file.

;; Download a text version at http://defindit.com/readme_files/tom_emacs.txt

;; Weird Mac stuff.
;; http://www.emacswiki.org/emacs/MetaKeyProblems#toc15
;; http://www.emacswiki.org/emacs/CarbonEmacsPackage
;; http://xahlee.info/emacs/emacs_hyper_super_keys.html

;; values: super, hyper, meta, nil

;; Since using define-key here doesn't work, I'm guessing that the
;; super key binding happens after .emacs is loaded.

;; doesn't work 
;;(define-key user-minor-mode-map "s-p" 'down-one)

;; I didn't try global-set-key. Maybe it would work. I switched to
;; define-key and the user-minor-mode-map so that my keybindings would
;; override all the goofy mode maps (like the HTML mode map).
;; (global-set-key [s-p] 'down-one)

(setq mac-option-key-is-meta nil)
(setq mac-command-key-is-meta t)
(setq mac-command-modifier 'meta)
(setq mac-option-modifier nil)

;; The daffy Mac and Aquamacs don't read .bash_profile and .bashrc
;; like the rest of the planet. You could go down the rabbit hole
;; http://developer.apple.com/library/mac/#qa/qa1067/_index.html
;; Instead, just get Emacs to add stuff to your path. The Mac GPG
;; tools are in /usr/local/bin.

;; http://lists.gnu.org/archive/html/help-gnu-emacs/2011-04/msg00210.html

(setenv "PATH" (concat "/usr/local/bin" path-separator (getenv "PATH")))

;; http://www.andreas-wilm.com/src/dot.emacs.html
;; This would work too, but has the path separator hard coded.
;; (setenv "PATH" (concat (getenv "PATH") ":/opt/local/bin"))

;; Andreas says: delete next line and you get: *ERROR*: Searching for
;; program: No such file or directory, gpg. 

;; You must open a shell and determine the correct path to gpg
;; manually, then put that path in the line below.

(setq exec-path (append exec-path '("/usr/local/bin")))

(defun safe-require (package_name)
  (condition-case err
      ((lambda ()
	 (require package_name)
	 (message "safe-require ok: %s" package_name)
	 ))
    (error
     (message "%s" (error-message-string err))
     )))

;; Show which function the cursor is in.
(which-function-mode t)

;; default the cursor to blinking.
(blink-cursor-mode t)

;; Tell the man-page functions woman* to open documents in the same
;; frame, not a new frame.
(setq woman-use-own-frame nil)

;; http://www.nongnu.org/emacs-tiny-tools/keybindings/index-body.html
;; Does not work in gnu emacs 23.1.1
;; (setq delete-key-deletes-forward t)

;; Save/restore desktop from the launch directory. Disable at launch
;; with --no-desktop.
;; http://www.gnu.org/software/emacs/manual/html_node/emacs/Saving-Emacs-Sessions.html
(setq destkop-path ".")
(condition-case err
    (desktop-save-mode 1)
  (error ))


;; Enable auto-complete mode
(add-to-list 'load-path "/home/twl8n/.emacs.d")
(when (require 'auto-complete-config nil t)
  (add-to-list 'ac-dictionary-directories "/home/twl8n/.emacs.d/ac-dict")
  (ac-config-default)
  (ac-set-trigger-key "TAB"))

(safe-require 'undo-tree)

;; This works, but my new func safe-require is better.

;; (when (require 'undo-tree nil t)
;;   (princ "undo-tree loaded")
;;   )

; Replace $RSENSE_HOME with the directory where RSense was installed in full path
;; Example for UNIX-like systems
;; (setq rsense-home "/home/tomo/opt/rsense-0.2")
;; or
;; (setq rsense-home (expand-file-name "~/opt/rsense-0.2"))
;; Example for Windows
;; (setq rsense-home "C:\\rsense-0.2")
(setq rsense-home "/home/twl8n/opt")
(add-to-list 'load-path (concat rsense-home "/etc"))
(safe-require 'rsense)


;; EasyPG is part of Emacs from v 22 on (or is that 23?), so don't
;; enable it, and absolutely do not install the standalone. Installing
;; the standalone breaks the internal functions.

;; Enable downcase-region. I don't know why this would be disabled by
;; default, but it is. Something about "confusing to new users". I
;; don't have it bound to a key, but I use it, especially in keyboard
;; macros, so it needs to be working.
(put 'downcase-region 'disabled nil)

;; Skip the startup "message", which looks like a "screen" or
;; "splash". Whatever. This makes it not appear.
(setq inhibit-startup-message t)

;; Not sure what these skip, but I doubt I want to see the splash or
;; startup screen.
;; Non-nil inhibits the startup screen.
;; It also inhibits display of the initial message in the `*scratch*' buffer.
(setq inhibit-startup-screen t)
(setq inhibit-splash-screen t)

;; http://www.emacswiki.org/emacs/CuaMode
;; http://www.gnu.org/software/emacs/manual/html_node/emacs/CUA-Bindings.html
;; New register binding are added to cua mode because C-x conflicts
;; with the normal register commands. Use C-1 C-c to copy to register
;; 1. Use C-1 C-v to paste the contents of register 1. M-x
;; copy-to-register still works.
(condition-case err
    (cua-mode t)
  (error ))

;; Don't tabify after rectangle commands
(setq cua-auto-tabify-rectangles nil)

;; No region when it is not highlighted
(transient-mark-mode 1) 

;; Standard Windows behaviour is t, but since I usually use a C-x
;; command immediately after copy, I have it set to nil
(setq cua-keep-region-after-copy nil)

;; Makes killing/yanking interact with X clipboard and X11 selection. There
;; are several other settings that deal wit the "X selection" which is
;; not quite the same as the X clipboard.
;; http://www.gnu.org/software/emacs/manual/html_node/emacs/Cut_002fPaste-Other-App.html
(setq x-select-enable-clipboard t)  

;; Enable ido-mode for fancy completion on buffer switch and file
;; open. We don't seem to need the require 'ido in recent versions of
;; Emacs.  http://www.emacswiki.org/cgi-bin/wiki/InteractivelyDoThings
(condition-case err
    (ido-mode t)
  (error ))

;; Tell emacs that read-only files whether write protected on disk or
;; set to read-only via toggle-read-only are *not* editable.  The
;; normal "can't edit" was broken somewhere around version 22.1.1 and
;; now it insists on using the version control system (which doesn't
;; work either).
(setq view-mode-only t)

;; Disable the damnable hard to read colorized source code, aka syntax
;; highlighting aka font lock mode.  Automatically becomes
;; buffer-local when set in any fashion, so you have to use the global
;; version of the function. For more info do describe-function on
;; font-lock-mode (Yes, there is a variable and a function with the
;; same name, apparently.) This does not work: (setq font-lock-mode
;; nil) Emacs gets upset when calling the function
;; global-font-lock-mode with an arg nil, so I call it with zero and
;; that's fine. All this time I thought nil was a value.

;; Later, I decided to bind the toggle to a key. Therefore the default
;; is on, but I generally turn it off with C-xt.

;; (global-font-lock-mode nil)
;; (global-font-lock-mode 0)

;; This works.
(setq font-lock-global-modes '(not perl-mode))

;; This works now that the defun is correct. Don't need it, but I've
;; left it here for historical purposes.
;; (defun turn-off-font-lock ()
;;    "Disable font-lock-mode"
;;  (interactive)  
;;    (font-lock-mode nil))
;; (add-hook 'perl-mode-hook 'turn-off-font-lock)

;; Paste (yank) at the text cursor location, not at the 
;; location of the mouse pointer. This only applies to graphical (X)
;; emacs sessions. 
(setq mouse-yank-at-point t)

;; Disable the nasty zmacs region highlighting in xemacs. Having it on
;; breaks mark-search-cut behavior. 
(setq zmacs-regions nil)

;; Uncomment to automatically load ispell at startup.
;(load "ispell")

;; Uncomment for hexl 
;(autoload 'hexl-find-file "hexl" "Edit file FILENAME in hexl-mode." t)
;(define-key global-map "\C-c\C-h" 'hexl-find-file)

;; Uncomment if you like lots of backup versions
;(setq version-control t)

;; Stop emacs from automatically converting end of line characters.
;; Auto converting Windows or Mac eol to Linux eol can be really, really
;; confusing.
(setq inhibit-eol-conversion t)

;; Prevent loading default.el. 
(setq inhibit-default-init 1)

;; valid values for require-final-newline
;; nil
;; t
;; (quote query)
(setq require-final-newline nil)


;; http://stackoverflow.com/questions/683425/globally-override-key-binding-in-emacs
;; I think this allows my preferred mode map to continue working when
;; other minor modes are active. See my user-minor-mode-map define-key
;; bindings below.

(defvar user-minor-mode-map (make-sparse-keymap) "user-minor-mode keymap.")

(define-minor-mode user-minor-mode
  "A minor mode so that my key settings override annoying major modes."
  t
  " user-keys"
  'user-minor-mode-map)


;; Turn user-minor-mode on/off 1/0 in the mini-buffer.
;; Oct 5 2009 Was 1 which was clearly a mistake. 

(defun user-minibuffer-setup-hook ()
  (user-minor-mode 0))

(add-hook 'minibuffer-setup-hook 'user-minibuffer-setup-hook)

(user-minor-mode 1)

;; No need to do the uppercase and lowercase versions of keystrokes
;; since key maps default to case-insensitive (case does not
;; matter). C-xf also matches C-xF

;; (defun noop ()
;;   "Noop for disabled keys"
;;   (interactive)
;;   ;; print a useful message in the mini-buffer
;;   nil
;;   )

;; Unset certain undo bindings because I hit \C-? which turns out to be 
;; identical to \C-_ 
;; Even though \C-/ won't (apparently) do anything in -nw mode,
;; unset it anyway so I don't hit it in windowing mode.
;; (define-key user-minor-mode-map "\C-_" 'noop)
;; (define-key user-minor-mode-map (kbd "C-/") 'noop)

;; Default is to center text. Too close to M-C-s and I never use
;; it. Disable.  In a lisp eval window, it didn't like the usual
;; shortcuts for Meta, so I just inserted an escape char.
;; (define-key user-minor-mode-map "s" 'noop)

;; Make the "delete" key in the cursor key (aka key pad) area perform
;; a forward delete, backspace (the key above \) performs a backward
;; delete. Sadly, [delete] and [kp-delete] can be different and can be
;; aliased to something else, therefore you must re-bind them
;; separately. (Apple in their bizarre wisdom have written "delete" on
;; the backspace key.)

;; Using the syntax (square brackets): [delete], [DEL], and [\d] did
;; not work.

;; Do not use "\d" because \d is some weird alias for whatever Emacs
;; thinks is the local definition of the delete key. That local
;; definition is often wrong.

;; (define-key global-map "\d" 'delete-char)


(define-key global-map [delete] 'delete-char)
(define-key global-map [kp-delete] 'delete-char)
(define-key global-map [backspace] 'backward-delete-char)

;; The global-map is overridden by the minibuffer-local-map. When I
;; fixed the stupid Mac delete, it broke backspace in the
;; minibuffer. This fixes both keys for the minibuffer.

(define-key minibuffer-local-map [delete] 'delete-char)
(define-key minibuffer-local-map [kp-delete] 'delete-char)
(define-key minibuffer-local-map [backspace] 'backward-delete-char)
(define-key minibuffer-local-map "\C-h" 'backward-delete-char)

(define-key user-minor-mode-map [delete] 'delete-char)
(define-key user-minor-mode-map [kp-delete] 'delete-char)
(define-key user-minor-mode-map [backspace] 'backward-delete-char)

(define-key user-minor-mode-map "\C-x\C-d" 'dired)
(define-key user-minor-mode-map "\C-xt" 'font-lock-mode)
(define-key user-minor-mode-map "\C-[#" 'comment-region)
(define-key user-minor-mode-map "\C-x#" 'comment-region)
(define-key user-minor-mode-map "\C-s" 'search-forward)
(define-key user-minor-mode-map "\C-r" 'search-backward)
(define-key user-minor-mode-map "\C-x\C-n" 'next-error)
(define-key user-minor-mode-map "\C-x\C-p" 'previous-error)
(define-key user-minor-mode-map "\C-xc" 'compile)
(define-key user-minor-mode-map "\C-h" 'backward-delete-char)

(define-key user-minor-mode-map "\C-[g" 'goto-line)
(define-key user-minor-mode-map "\C-xn" 'other-window)
(define-key user-minor-mode-map "\C-[q" 'query-replace)
(define-key user-minor-mode-map "\C-xf" 'find-file)
(define-key user-minor-mode-map "\C-[\C-[" 'repeat-complex-command)
(define-key user-minor-mode-map "\C-[r" 'replace-string)
(define-key user-minor-mode-map "\C-[f" 'fill-paragraph)
(define-key user-minor-mode-map "\C-z" 'advertised-undo)
;; Setting keys to nil did not work. They still kept their default actions.
;; C-S-backspace is control-shift-backspace
(define-key user-minor-mode-map [C-S-backspace] 'delete-backward-char) 
(define-key user-minor-mode-map [C-backspace] 'delete-backward-char)
(define-key user-minor-mode-map [M-backspace] 'delete-backward-char)
(define-key user-minor-mode-map [insert] nil)
(define-key user-minor-mode-map [insertchar] nil)
;; Standard bind is to recenter-top-bottom. 
(define-key user-minor-mode-map "\C-l" 'recenter)

;; Stupid xemacs can't grok "\C-[\C-[" so re-purpose C-x[ It
;; normally means page up, but I always use something else for page up.
;; (define-key user-minor-mode-map "\C-x\["
;;  'repeat-complex-command) (global-set-key "\C-x\["
;;  'repeat-complex-command)

;;  Use a new function for page up and page down.
;;  This one will place the cursor on the line where you started if
;;  you do the opposite. The default Emacs scroll-up and scroll-down
;;  don't return the cursor to the same line. That's bad.
;;  This still doesn't work quite right if you hit the top or bottom of the buffer.
;;  That could be fixed by remembering how far the last scroll was, and
;;  reversing when necessary. 

(define-key user-minor-mode-map "\C-[a" 'backward-screen)
(define-key user-minor-mode-map "\C-[z" 'forward-screen)
(define-key user-minor-mode-map [(prior)]   'backward-screen)
(define-key user-minor-mode-map [(next)]   'forward-screen)
(define-key user-minor-mode-map "\C-[p" 'down-one)
;; doesn't work 
;;(define-key user-minor-mode-map "s-p" 'down-one)
(define-key user-minor-mode-map "\C-[n" 'up-one)

;;  Remap the danged downcase-region keys
;;  because I'm always hitting these instead of C-l
;;  C-xl might be ok. Consider commenting it out, even 
;;  though l is for "lower" which isn't the command. It should
;;  be d for "downcase". 
(define-key user-minor-mode-map "\C-[l" 'recenter)
(define-key user-minor-mode-map "\C-x\C-l" 'recenter)


;;  Use new kdb syntax available as of 19.30
;;  http://tiny-tools.sourceforge.net/emacs-keys.html

;;  None of these work in -nw
;; ;(define-key user-minor-mode-map (kbd "C-S-N") 'up-one)
;; ;(define-key user-minor-mode-map (kbd "C-S-P") 'down-one)
;; ;(global-set-key [(control shift n)] 'up-one)
;;  (list ?C-S-n (type-of ?C-S-n)) 
;;  (list ?C-n (type-of ?C-n)) 
;; ;;(define-key user-minor-mode-map (kbd "C-N") 'up-one)
;; ;;(define-key user-minor-mode-map (kbd "C-P") 'down-one)

(defun forward-screen ()
  "scroll down one screen in display."
  (interactive)
  (forward-line (- (window-height) 2)))

(defun backward-screen ()
  "scroll down one screen in display."
  (interactive)
  (forward-line (- (- (window-height) 2))))

;;  sep 19 2008 Could bind unindent and force-indent to keys, or just
;;  create a keyboard macro everytime I need one of them.

(defun unindent ()
  ;; remove whitespace from the beginning of a line
  (interactive)
  (beginning-of-line)
  (re-search-forward "^[ 	]*")
  (replace-match "")
  )

(defun force-indent ()
  "remove leading whitespace and insert a tab"
  (interactive)
  (unindent)
  (insert "	")
  )


;;  man page mode uses one of my favorite key bindings.  over load
;;  it's function with mine. Switching my key bindings to
;;  user-minor-mode-map may have fixed this.

(defun Man-next-manpage ()
  "overload"
  (interactive)
  (up-one))

(defun Man-previous-manpage ()
  "overload"
  (interactive)
  (down-one))

(defun up-one ()
  "scroll up one line in display."
  (interactive)
  (scroll-up 1)
  (forward-line 1))

(defun down-one ()
  "scroll down one line in display."
  (interactive)
  (scroll-down 1)
  (forward-line -1))

 (setq auto-mode-alist (cons (cons "\\.java$" 'c-mode) auto-mode-alist))
 (setq auto-mode-alist (cons (cons "\\.cgi$" 'perl-mode) auto-mode-alist))

 ; This is an example of keyboard rebinding on the Macintosh.
 ; I've preserved this for historical interest only.
 ; It makes these assignments:
 ;   F5 splits the display vertically
 ;   F6 enlarges the window containing the cursor
 ;   F7 shrinks the window containing the cursor
 ;   F8 eliminates all split windows
 ; See the file ~/lisp/mac/Macintosh-win.el for the codes to define other keys.

 ;; (setq mac-raw-map-hooks
 ;; 	  (list
 ;; 	   '(define-key mac-raw-map "\040" 'split-window-vertically)
 ;; 	   '(define-key mac-raw-map "\041" 'enlarge-window)
 ;; 	   '(define-key mac-raw-map "\042" 'shrink-window)
 ;; 	   '(define-key mac-raw-map "\044" 'delete-other-windows)))


;; No idea what this was supposed to do.
;; (put 'upcase-region 'disabled nil)

;; The code below (when uncommented) creates an irritating bug in that
;; it overrides custom-font-faces when first loaded, but allows
;; custom-font-faces to work when loaded via load-file. I'm fairly
;; certain the the code below was a poor work-around for default font
;; loading.

;; (assq-delete-all 'font default-frame-alist)
;; (add-to-list
;;  'default-frame-alist
;;  '(font . "-Adobe-Courier-Medium-R-Normal--17-120-100-100-M-100-ISO8859-1"))

;; The font below was present in Fedora 6, but not in Fedora 8
;; Why did the available fonts change?
;;   '(font . "-Adobe-Courier-Medium-R-Normal--14-140-75-75-M-90-ISO8859-1"))


;; In customization group Editing Basics, Line Move Visual controls
;; whether or not the cursor moves to logical lines or visual
;; lines. The difference is for continuation lines, the visual line is
;; the next line on the screen. The logical line is the next actual
;; line in the file, and not necessarily what is "visual" on the
;; screen. This should not be confused with visual line mode.

;; http://www.gnu.org/software/emacs/manual/html_node/emacs/Continuation-Lines.html
;; http://www.gnu.org/software/emacs/manual/html_node/emacs/Visual-Line-Mode.html#Visual-Line-Mode

;; Keywords: line wrap, wrapping, continuation, visual, logical,
;; cursor, cursor jump, cursor skip, skip line, skip continuation,
;; wrapped lines, continuation lines, line continuation, line
;; continuation mode, cursor movement mode, cursor mode, next line,
;; next logical line, skip to logical line, cursor move, line visual
;; move, line-move-visual, move logical, logical lines

(condition-case err
    (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.
     '(cua-mode t nil (cua-base))
     '(ess-S-assign "_")
     '(ido-everywhere t)
     '(ido-show-dot-for-dired t)
     '(line-move-visual nil))
  (error ))

;; (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 (:inherit nil :stipple nil :background nil :foreground "black" :inverse-video nil :box nil :strike-through nil :overline nil :underline nil :slant normal :weight normal :height 136 :width normal :foundry "urw" :family "Nimbus Mono L")))))

(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.
 '(cua-mode t nil (cua-base))
 '(ess-S-assign "_")
 '(ido-everywhere t)
 '(ido-show-dot-for-dired t)
 '(line-move-visual nil))

(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 (:inherit nil :stipple nil :background "White" :foreground "black" :inverse-video nil :box nil :strike-through nil :overline nil :underline nil :slant normal :weight normal :height 160 :width normal :foundry "apple" :family "Monaco")))))