Just a few bits and pieces I've collected in my ~/.emacs file over the years... If you're interested in customizing emacs then the book Writing GNU Emacs Extensions by Bob Glickstein is well worth picking up.
Modern versions of XEmacs come with a fantastic tool called "customize". It's on the Options menu. However, there are far too many options to easily manage. The best way to look for things is by going for Options/Customize/Apropos, which will let you search all customizable things.
Here are a few things that I find useful to customize:
gnuserv is a very handy way of using XEmacs. Instead of waiting ages for each XEmacs to load, you load up XEmacs once and thereafter you bring up new windows by using the "gnuclient" command. All it takes is to insert this fragment of elisp into your .emacs file.
(if (string-match
"XEmacs\\|Lucid" emacs-version)
(progn (require 'gnuserv)
(gnuserv-start)))
If you've got a mouse with a wheel and it's set up in X, this will make it work inside XEmacs, too.
(autoload 'mwheel-install "mwheel" "Enable wheely mouse") (mwheel-install)
The latest versions of XEmacs don't install the version control functions by default (but there is a menu option to load them), so if you're used to using your fingers for vc, then you lose. The following loads in the vc functions, as you would expect them.
(require 'vc-hooks)
I like to have my comments word wrap automatically when I'm in programming languages. For some reason pressing M-q also seems to format the code as well. The easiest way of doing this seems to be turning on auto-fill-mode whilst programmming.
(add-hook 'c-mode-common-hook 'turn-on-auto-fill) (add-hook 'cperl-mode-hook 'turn-on-auto-fill)
The following bit of elisp code can be inserted into your .emacs file to provide a function dos-to-unix (which can be called via M-x). This will just strip out all the ^M characters.
(defun dos-to-unix () "Convert a DOS buffer to Unix format." (interactive) (beginning-of-buffer) (replace-string "\r\n" "\n"))
This function allows you to execute the last macro you recorded (with C-x ( and C-x ) ) where you click the mouse. This can be very handy for doing lots of the same thing, when they're not lined up nicely.
(defun mouse-call-last-kbd-macro (event)
"*Execute last keyboard macro at point of mouse click"
(interactive "e")
(progn
(mouse-set-point event)
(call-last-kbd-macro)))
(global-set-key [(control meta button1)] 'mouse-call-last-kbd-macro)
This function strips off trailing whitespace at the end of a line, which is almost never needed. This is particularly necessary with modern versions of cperl-mode, as it highlights such trailing whitespace for you...
(defun global-trim ()
"Trim all trailing whitespace in the current buffer."
(interactive)
(save-excursion
(goto-char (point-min))
(while (re-search-forward "[ \t]+$" nil t)
(replace-match "" t t))))
One of the nice things about vi is that you can use the % key to find matching partner of a parenthesis. This function and keybinding makes emacs do the same, but only when the cursor is on top of a parenthesis, otherwise it just inserts a percent sign as you would expect.
(defun match-paren (arg)
"Go to the matching parenthesis if on parenthesis otherwise insert %."
(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 "%" 'match-paren)
This function just provides a nice easy way to put something at the top of the screen. By default, it will try to put the current function to the top of the screen, but if you give it a prefix (with C-u), it will put the current line there instead.
(defun defun-to-top-of-window (&optional arg)
"Move the function which the point is in to the top of the window. If
given an argument, moves current line to top of window instead."
(interactive "P")
(if arg
(recenter 0)
(save-excursion
(beginning-of-defun)
(backward-paragraph)
(forward-line)
(recenter 0))))
(global-set-key [f10] 'defun-to-top-of-window)