The very unofficial .emacs home BenjaminRutt.emacs
what is this all about ?
customization basics
special topics
local dotfiles
dotfiles on the web
new and updated pages
useful sites and pages
search locally
OS/2 Emacs
Latest Additions
local files:
John J. Glynn
David Jolley

linked files:


The Emacs wiki
ODP search for Emacs
;;File:       .emacs
;;Author:   Benjamin Rutt

;global keyb maps  
(global-set-key "\C-xg" 'goto-line)
(global-set-key [home] 'beginning-of-line)
(global-set-key [end] 'end-of-line)
(global-set-key [\C-home] 'beginning-of-buffer)
(global-set-key [\C-end] 'end-of-buffer)
(global-set-key [\S-tab] 'indent-region)
(global-set-key [?\C-/] 'void) ;forward reference
(global-set-key [\C-backspace] 'backward-kill-word)
(global-set-key "\C-s" 'isearch-forward-regexp)
(global-set-key "\C-r" 'isearch-backward-regexp)
(global-set-key "\C-\M-s" 'tags-search)
(global-set-key "\C-x\C-n" 'find-file-other-frame) ;open new frame with a file
(global-set-key "\C-x\C-c" 'intelligent-close) ;forward reference
(global-set-key "\C-x55" 'split-window-fork) ;forward reference
(global-set-key "\M-n" 'scroll-n-lines-ahead) ;forward reference
(global-set-key "\M-p" 'scroll-n-lines-behind) ;forward reference
(global-set-key "\M-u" 'void) ;don't bind upcase word
(global-set-key "\M-l" 'void) ;don't bind downcase word
(global-set-key "\C-c\C-c" 'comment-region) ;have to force it for some reason
(global-set-key [\C-tab] 'yic-next-buffer) ;forward reference
(global-set-key [\C-\S-tab] 'yic-prev-buffer) ;forward reference

; abbreviation mode
(setq-default abbrev-mode t)
(if (file-exists-p "~/.abbrev")
    (read-abbrev-file "~/.abbrev"))
(setq save-abbrevs t)

;;dynamic abbreviation customizations
(setq dabbrev-case-replace nil)


;; When you scroll down with the cursor, emacs will move down the buffer one 
;; line at a time, instead of in larger amounts.
(setq scroll-step 1)

;syntax hilite
(global-font-lock-mode 1)
(setq font-lock-maximum-decoration t)

;show paren, brace, and curly brace "partners" at all times
(show-paren-mode t)

;show column number in status bar
(column-number-mode t)

;show more info in taskbar/icon than just "Emacs"
(setq-default frame-title-format (list "%65b %f"))
(setq-default icon-title-format (list "%b"))

;show time on status bar

;make the y or n suffice for a yes or no question
(fset 'yes-or-no-p 'y-or-n-p)

; don't automatically add new lines when scrolling down at the bottom
; of a buffer 
(setq next-line-add-newlines nil)

;be able to do Ctrl-X, u/l  to upper/lowercase regions without confirm
(put 'downcase-region 'disabled nil)
(put 'upcase-region 'disabled nil)

;show ascii table
(defun ascii-table ()
  "Print the ascii table. Based on a defun by Alex Schroeder <>"
  (switch-to-buffer "*ASCII*")
  (insert (format "ASCII characters up to number %d.\n" 254))
  (let ((i 0))
    (while (< i 254)
      (setq i (+ i 1))
      (insert (format "%4d %c\n" i i))))

;insert date into buffer
(defun insert-date ()
  "Insert date at point."
  (insert (format-time-string "%a %b %e, %Y %l:%M %p")))

;convert a buffer from dos ^M end of lines to unix end of lines
(defun dos2unix ()
    (goto-char (point-min))
      (while (search-forward "\r" nil t) (replace-match "")))

;vice versa
(defun unix2dos ()
    (goto-char (point-min))
      (while (search-forward "\n" nil t) (replace-match "\r\n")))

;;This method, when bound to C-x C-c, allows you to close an emacs frame the 
;;same way, whether it's the sole window you have open, or whether it's
;;a "child" frame of a "parent" frame.  If you're like me, and use emacs in
;;a windowing environment, you probably have lots of frames open at any given
;;time.  Well, it's a pain to remember to do Ctrl-x 5 0 to dispose of a child
;;frame, and to remember to do C-x C-x to close the main frame (and if you're
;;not careful, doing so will take all the child frames away with it).  This
;;is my solution to that: an intelligent close-frame operation that works in 
;;all cases (even in an emacs -nw session).
(defun intelligent-close ()
  "quit a frame the same way no matter what kind of frame you are on"
  (if (eq (car (visible-frame-list)) (selected-frame))
      ;;for parent/master frame...
      (if (> (length (visible-frame-list)) 1)
          ;;close a parent with children present
   (delete-frame (selected-frame))
        ;;close a parent with no children present
    ;;close a child frame
    (delete-frame (selected-frame))))

;;a no-op function to bind to if you want to set a keystroke to null
(defun void ()
  "this is a no-op"

;;compute the length of the marked region 
(defun region-length ()
  "length of a region"
  (message (format "%d" (- (region-end) (region-beginning)))))
(defun split-window-fork ()
   "spawns a new frame so that a 2-way split window in one frame becomes "
   "2 top-level frames.  Has the same action as ")
    (let ((current_window (selected-window))
          (other_window (next-window (selected-window)))
          (current_buffer (window-buffer (selected-window)))
          (other_buffer (window-buffer (next-window (selected-window)))))
      (select-window other_window)

;;the following snippet was copied from the Oreilly-published  book 
;;"Writing GNU Emacs Extensions" by Bob Glickstein.
(defalias 'scroll-ahead 'scroll-up)
(defalias 'scroll-behind 'scroll-down)
(defun scroll-n-lines-ahead (&optional n)
  "Scroll ahead N lines (1 by default)."
  (interactive "P")
    (scroll-ahead (prefix-numeric-value n))
    (next-line 1)))

(defun scroll-n-lines-behind (&optional n)
  "Scroll behind N lines (1 by default)."
  (interactive "P")
    (scroll-behind (prefix-numeric-value n))
    (previous-line 1)))

;;begin buffer-switching methods, which I bind to Ctrl-TAB and Ctrl-Shift-TAB
;; ----------------------------------------------------------------------
;;     Original yic-buffer.el
;;     From: (young-il choo)
;;     Date: 7 Aug 90 23:39:19 GMT
;;     Modified 
;; ----------------------------------------------------------------------

(defun yic-ignore (str)
   ;;buffers I don't want to switch to 
   (string-match "\\*Buffer List\\*" str)
   (string-match "^TAGS" str)
   (string-match "^\\*Messages\\*$" str)
   (string-match "^\\*Completions\\*$" str)
   (string-match "^ " str)

   ;;Test to see if the window is visible on an existing visible frame.
   ;;Because I can always ALT-TAB to that visible frame, I never want to 
   ;;Ctrl-TAB to that buffer in the current frame.  That would cause 
   ;;a duplicate top-level buffer inside two frames.
   (memq str                
          (lambda (x) 
              (frame-selected-window x))))

(defun yic-next (ls)
  "Switch to next buffer in ls skipping unwanted ones."
  (let* ((ptr ls)
         bf bn go
    (while (and ptr (null go))
      (setq bf (car ptr)  bn (buffer-name bf))
      (if (null (yic-ignore bn))        ;skip over
   (setq go bf)
        (setq ptr (cdr ptr))
    (if go
        (switch-to-buffer go))))

(defun yic-prev-buffer ()
  "Switch to previous buffer in current window."
  (yic-next (reverse (buffer-list))))

(defun yic-next-buffer ()
  "Switch to the other buffer (2nd in list-buffer) in current window."
  (bury-buffer (current-buffer))
  (yic-next (buffer-list)))
;;end of yic buffer-switching methods

;set up my tab length as a variable
(defun get-my-tab-length () 3)

;build a list from 1 to n
(defun iota
  (if (= n 0) '()
    (append (iota (- n 1)) (list n))))

;build the tab list
(defun create-tab-list
  (mapcar (lambda (n) (* (get-my-tab-length) n)) (iota length)))

(defun my-c-mode-hook ()

  (local-set-key "\M-f" 'c-forward-into-nomenclature)
  (local-set-key "\M-b" 'c-backward-into-nomenclature)

  ;set up the tab stop list so I can do manual Ctrl-I tabs to specific points
  (setq tab-stop-list (create-tab-list 60))
  (setq indent-tabs-mode t)
  (setq tab-width (get-my-tab-length))
  (setq c-basic-offset (get-my-tab-length))
  (setq standard-indent (get-my-tab-length))
  (setq c-style-variables-are-local-p nil)

  ;don't give me newline automatically after electric expressions are entered
  (setq c-auto-newline nil)

  ;if (0)          becomes        if (0)
  ;    {                          {
  ;       ;                           ;
  ;    }                          }
  (c-set-offset 'substatement-open 0)

  ;first arg of arglist to functions: tabbed in once
  ;(default was c-lineup-arglist-intro-after-paren)
  (c-set-offset 'arglist-intro '+)

  ;second line of arglist to functions: tabbed in once
  ;(default was c-lineup-arglist)
  (c-set-offset 'arglist-cont-nonempty '+)

  ;switch/case:  make each case line indent from switch
  (c-set-offset 'case-label '+)

  ;make the ENTER key indent next line properly
  (local-set-key "\C-m" 'newline-and-indent)

  ;syntax-highlight aggressively
  (setq font-lock-support-mode 'lazy-lock-mode)
  (setq lazy-lock-defer-contextually t)
  (setq lazy-lock-defer-time 0)

  ;make DEL take all previous whitespace with it
  (c-toggle-hungry-state 1)

  ;make open-braces after a case: statement indent to 0 (default was '+)
  (c-set-offset 'statement-case-open 0)

  ;make a #define be left-aligned 
  (setq c-electric-pound-behavior (quote (alignleft)))

  ;do not impose restriction that all lines not top-level be indented at least
  ;1 (was imposed by gnu style by default)
  (setq c-label-minimum-indentation 0)
(add-hook 'c++-mode-hook 'my-c-mode-hook)
(add-hook 'c-mode-hook 'my-c-mode-hook)

;;WINDOWS section
(if (or (eq system-type 'windows-nt)
        (eq system-type 'ms-dos))

    ;;progn is like (begin e1 e2 ...) on scheme.  i.e. execute one or more
    ;;statements in sequential order

      ;;get around inadequacies of WIN9x/NT's window manager, which
      ;;always places new application windows in the same place in
      ;;some cases.  I want random placement of new windows so I can
      ;;Alt-TAB easier, and know where I'm going.  The following code
      ;;places the top left corner of the initial frame at a random
      ;;horizontal location on screen and a fixed vertical location.
      (setq initial-frame-alist
            `((top  . 0)
              ;;Parse out milliseconds field, and scale it down.
       ;;The current-time function is random enough for purposes here.
       (left . ,(/ (car (cdr (cdr (current-time)))) 8000))))

;;LOCAL HOOKS SECTION - note this section should be last to allow an
;;.emacs_local file to override the .emacs.  The .emacs_local file should
;;contain anything specific to a particular environment/platform/machine.
(if (file-exists-p "~/.emacs_local") 
    (load-file "~/.emacs_local"))


;The following is a failed attempt to make sure that .emacs is
;byte-compiled into a new .emacs.elc whenever it changes, to support
;the fastest loading of the latest changes.  This does not work,
;because emacs always loads the ~/.emacs.elc if it exists, and you
;can't overwrite it with a byte-compile command to replace it if you
;are in the middle of executing it.  If anyone has an improvement that
;does work, please email me.  

; (if (file-newer-than-file-p "~/.emacs" "~/.emacs.elc") 
;     (progn 
;       (message "byte-compiling .emacs to update .emacs.elc!")  
;       (byte-compile-file "~/.emacs")))

;useful system-information variables:

All content copyright by the contributors. Website maintained with Emacs , wsmake and html-helper-mode
Emacs community logo by Daniel Lundin Last updated on Sat Jan 22 14:49:24 2005 by Ingo Koch