Emacs Configuration

22 May 2008

Plea for help

Hakan Lindkvist wrote and told me that it was one of the top matches on Google for "emacs jump to matching parenthesis". (Yay! My existence is validated!) If that is what you are looking for, here is the function and keymap setting I have:

;;; Use "%" to jump to the matching parenthesis.
(defun goto-match-paren (arg)
  "Go to the matching parenthesis if on parenthesis, otherwise insert
the character typed."
  (interactive "p")
  (cond ((looking-at "\\s\(") (forward-list 1) (backward-char 1))
    ((looking-at "\\s\)") (forward-char 1) (backward-list 1))
    (t                    (self-insert-command (or arg 1))) ))
(global-set-key "%" `goto-match-paren)

I do not remember where I found that, which was using some Google search myself. I wish I did; it is something I use every day.

Grant Rettke was looking for Emacs and Lucida Console and found this page, reminding me that I really should write down the Bitstream Vera font location, the Windows vs. Emacs fonts section of the Emacs for Windows FAQ, and the magic for getting a font selection dialog on Windows Emacs:

(insert (w32-select-font))

Use a *scratch* buffer; it isn't interactive.

(The FAQ shows (w32-select-font nil t); right offhand I'm not sure what the arguments do.)

Other useful Emacs notes pages are:

Basic configuration

  • I put separate Emacs libraries that I have downloaded into a directory under my home directory: ~/emacs.d.
  • I prefer using spaces to tabs.
  • I want the column number shown in the mode line; it makes error messages easier to locate.
  • Iswitchb mode makes buffer manipulation easier.
  • Fonts and pretty colors are nice; I like green on black.
(put 'narrow-to-region 'disabled nil)

;;; Customizations go in emacs.d
(add-to-list 'load-path "~/emacs.d")

;; Tab using spaces
(setq-default indent-tabs-mode nil)

;;; Display column numbers
(column-number-mode 1)

;;; Better buffer switching
(iswitchb-mode 1)

;;; Use font-lock-mode
(global-font-lock-mode 1)

;; default to better frame titles
(setq frame-title-format (concat  "%b - emacs@" system-name))

;;; Pretty colors
(set-default-font "-outline-Lucida Console-normal-r-normal-normal-13-78-120-120-c-*-iso10646-1")
(set-background-color "black")
(set-foreground-color "green")
(set-cursor-color "green")
;; Frames
(add-to-list 'default-frame-alist '(font . "-outline-Lucida Console-normal-r-normal-normal-13-78-120-120-c-*-iso10646-1"))
(add-to-list 'default-frame-alist '(background-color . "black"))
(add-to-list 'default-frame-alist '(foreground-color . "green"))

General programming

  • I tend to use a maximized Emacs window and don't feel the need to restrict myself to short lines.
  • One thing I miss about vi is the ability to jump between matching delimiters; I found some Emacs Lisp on the 'net that modifies the % key binding to do that. It occasionally makes entering %'s a pain, but is generally worthwhile.
  • My one Emacs hack: a position stack---mark the current position, move, then jump back to the current position. Or further back. See the Postack section below.
;;; Use a wide window: put comments further right
(setq-default comment-column 65)

;;; Use "%" to jump to the matching parenthesis.
(defun goto-match-paren (arg)
  "Go to the matching parenthesis if on parenthesis, otherwise insert
the character typed."
  (interactive "p")
  (cond ((looking-at "\\s\(") (forward-list 1) (backward-char 1))
    ((looking-at "\\s\)") (forward-char 1) (backward-list 1))
    (t                    (self-insert-command (or arg 1))) ))
(global-set-key "%" `goto-match-paren)

;;; Position stack
(load-library "postack")
(global-set-key "\C-cm" 'postack-push)
(global-set-key "\C-cp" 'postack-pop)


;; Linus' kernel formatting
(defun linux-c-mode ()
  "C mode with adjusted defaults for use with the Linux kernel."
  (c-set-style "K&R")
  (setq tab-width 8)
  (setq indent-tabs-mode t)
  (setq c-basic-offset 8))


My C++ style is currently rather basic, but a moving target.

;;; C++ formatting
(c-set-style "stroustrup")
(add-hook 'c-mode-hook
          (lambda () "Set c-c c-c to comment-region"
            (local-set-key "\c-c\C-c" 'comment-region)
            (setq (comment-column 65))))


  • I like to be able to comment (or uncomment) a region easily; didn't C-c C-c used to do this?
  • Ok, I have no idea what eldoc does. But it must be good, right?
;;; cperl-mode customizations
(add-hook 'cperl-mode-hook
      '(lambda () "Set c-c c-c to comment-region"
         (local-set-key "\C-c\C-c" 'comment-region)
         (setq comment-column 65)))

(defun my-cperl-eldoc-documentation-function ()
  "Return meaningful doc string for `eldoc-mode'."
   (let ((cperl-message-on-help-error nil))

(add-hook 'cperl-mode-hook
          (lambda () "Set up eldoc mode for cperl"
            (set (make-local-variable 'eldoc-documentation-function)


;;; IDL mode customizations
(add-hook 'idl-mode-hook
      '(lambda () "Adjust IDL mode"
         (c-set-style "stroustrup")))


;;; tuareg mode
(add-to-list 'load-path "~/emacs.d/tuareg-mode-1.45.0")
(load-library "append-tuareg")


;;; gforth
(load-library "gforth")
(setenv "GFORTHPATH" "C:\\Program Files\\gforth")
(setq forth-program-name "C:\\cygwin\\bin\\gforth")
; (autoload 'forth-mode "gforth")
(add-to-list 'auto-mode-alist '("\\.fs\\'" . forth-mode))
; (autoload 'forth-block-mode "gforth")
(add-to-list 'auto-mode-alist '("\\.fb\\'" . forth-block-mode))
(add-hook 'forth-mode-hook '(lambda ()
                          (setq forth-indent-level 4)
                          (setq forth-minor-indent-level 2)
                          (setq forth-hilight-level 3)))


;;; Haskell
(add-to-list 'load-path "~/emacs.d/haskell-mode-2.0")
(add-to-list 'auto-mode-alist '("\\.[hg]s$"   . haskell-mode))
(add-to-list 'auto-mode-alist '("\\.hi$"      . haskell-mode))
(add-to-list 'auto-mode-alist '("\\.l[hg]s$"  . literate-haskell-mode))
(autoload 'haskell-mode "haskell-mode"
  "Major mode for editing Haskell scripts" t)
(autoload 'literate-haskell-mode "haskell-mode"
  "Major mode for editing literate Haskell scripts" t)
(add-hook 'haskell-mode-hook 'turn-on-font-lock)
(add-hook 'haskell-mode-hook 'turn-on-haskell-decl-scan)
(add-hook 'haskell-mode-hook 'turn-on-haskell-doc-mode)
(add-hook 'haskell-mode-hook 'turn-on-haskell-indent)
; (add-hook 'haskell-mode-hook 'turn-on-haskell-simple-indent)
; (add-hook 'haskell-mode-hook 'turn-on-haskell-hugs)
(autoload 'run-ghci "haskell-ghci"
  "Go to the *ghci* buffer" t nil)
(set-variable 'haskell-program-name "ghci")
(defalias 'run-haskell (quote switch-to-haskell))
(autoload (quote switch-to-haskell) "inf-haskell"
  "Show the inferior-haskell buffer.  Start the process if needed." t nil)


;;; Erlang
(add-to-list 'load-path "c:\\Program Files\\erl5.4.12\\lib\\tools-2.4.6\\emacs")
(load-library "erlang-start")

Miscellaneous configurations

System beep

My new laptop has a really annoying and apparently un-disableable system beep. To disable Emac's use:

;;; Disable system beep
(setq visual-bell t)

Oh, and to disable it for X:

xset b off

See the Visible bell mini-HOWTO.


Remote editing, over PuTTY's plink:

;;; tramp
(add-to-list 'load-path "~/emacs.d/tramp/lisp")
(add-to-list 'Info-default-directory-list "~/emacs.d/tramp/info")
(require 'tramp)
(setq tramp-default-method "plink") ; inline
; (setq tramp-default-method "pscp")        ; external (slow for small files)
(setq tramp-default-user "tmcguire")

Emacs IRC

The distributed team I am currently working with uses IRC (to an internal server) for some communication.

;;; erc
(add-to-list 'load-path "~/emacs.d/erc-5.1.4")
(require 'erc)
(require 'erc-track)
(require 'erc-fill)
(require 'erc-log)
;; See www.emacswiki.org/cgi-bin/wiki/ErcStartupFiles
(defmacro de-erc-connect (command server port nick)
  "Create interactive command `command', for connecting to an IRC server.  The command uses interactive mode if passed an argument."
  (fset command
        `(lambda (arg)
           (interactive "p")
           (if (not (= 1 arg))
               (call-interactively 'erc-select)
             (erc-select :server ,server :port ,port :nick ,nick)))))
; (de-erc-connect erc-ag2 "austingrid2.cadence.com" 443 "tmcguire")
; (defun erc-cadence ()
;   "Fire up ERC"
;   (interactive)
;   (select-frame (make-frame '((name . "Emacs ERC") (minibuffer . t))))
;   (call-interactively 'erc-ag2))
(add-hook 'erc-mode-hook
          '(lambda ()
             (erc-completion-mode 1)))
(erc-track-mode t)
(erc-fill-mode t)
(setq erc-log-insert-log-on-open nil)
(setq erc-log-channels t)
(setq erc-log-channels-directory "C:/My Documents/IM Sessions/")
(setq erc-save-buffer-on-part t)
(setq erc-hide-timestamps nil)
(defadvice save-buffers-kill-emacs (before save-logs (arg) activate)
  (save-some-buffers t (lambda () (when (and (eq major-mode 'erc-mode) (not (null buffer-file-name)))))))
(add-hook 'erc-insert-post-hook 'erc-save-buffer-in-logs)
(add-hook 'erc-mode-hook '(lambda () (when (not (featurep 'xemacs))
                                       (set (make-variable-buffer-local 'coding-system-for-write) 'emacs-mule))))

ReStructured Text

Restructured text is what I generally use to write these web pages.

;;; ReStructured Text
(load-library "rst")
(add-hook 'text-mode-hook 'rst-text-mode-bindings)
(add-hook 'text-mode-hook 'rst-set-paragraph-separation)
(add-to-list 'auto-mode-alist '("\\.rst$" . rst-mode) )


Maintain a stack of buffer positions, with the operations:

  • Push the current position onto the stack.
  • Pop the latest position off the stack and move point to it.

This is useful when following a chain of function calls/definitions. Certainly, TAGS might be nicer...

Put the code in postack.el.

;;; Maintain a stack of buffer positions, useful when recusively
;;; looking up function definitions.

(defvar postack-stack '() "The position stack")

(defun postack-goto (marker)
  "Should be marker-goto."
  (switch-to-buffer (marker-buffer pos))
  (goto-char (marker-position pos)))

(defun postack-push ()
  "Push the current position on the position stack."
  (let ((pos (point-marker)))
    (setq postack-stack (cons pos postack-stack))
    (message (format "Marked: (%s:%s)" (marker-buffer pos) (marker-position pos))) ))

(defun postack-pop ()
  "Remove the top position from the position stack and make it current."
  (let ((pos (car postack-stack)))
    (setq postack-stack (cdr postack-stack))
    (cond ((null pos)
           (message "Position stack empty"))
          ((markerp pos)
           (postack-goto pos)
           (message (format "Position: (%s:%s)" (marker-buffer pos) (marker-position pos))))
           (message "Invalid position in stack")) ) ))

;;; (global-set-key "\C-cm" 'postack-push)
;;; (global-set-key "\C-cp" 'postack-pop)

gloria i ad inferni
faciamus opus

Return to Top | About this site...
Last edited Thu Feb 4 01:04:48 2010.
Copyright © 2005-2016 Tommy M. McGuire