Emacs dotfile


This is my .emacs file, with all my settings for Emacs. All new shortcuts are described in the comments in the code.

;; Emacs dotfile


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;			      Appearance
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Emacs complains on startup, but it sets a variable width font 
;; on the the modeline
(set-face-font 'modeline "-*-helvetica-*-r-*-*-12-*-*-*-*-*")

;; Change the colour of the cursor. 
;; Nice when someone has broken the emacs settings and you would get
;; a white cursor on white, or black on black.
(set-cursor-color "pink")

;; Show line number
(setq line-number-mode 1)

;; Show column number
(setq column-number-mode 1)

;; Don't show banner on startup
(setq inhibit-startup-message t)

;; Add a blurb about what host emacs runs if not on the local display
(when (boundp 'x-display-name)
  (setq-default
   frame-title-format (concat invocation-name
         (unless (string= x-display-name ":0.0") (concat "@" system-name))
         " (%b)")
   icon-title-format frame-title-format
   ))

;; Title bar shows name of current buffer.  
(setq frame-title-format '("emacs: %*%+ %b"))

;; Disable the toolbar to save space for another few lines of code.
(cond
 ((and (string-match "GNU Emacs" (emacs-version))
       (boundp 'emacs-major-version)
       (>= emacs-major-version 20))
  ;; GNU Emacs 20 or later.
  (tool-bar-mode nil)))


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;			      Interface
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Syntax highligting for all supported file types
(global-font-lock-mode 1)

;; And enable as much highlighting as possible
(setq font-lock-maximum-decoration t)

;; Only scroll one line when reaching end of page
(setq scroll-step 1)

;; Make å, ä and ö work. 
(set-terminal-coding-system 'iso-latin-1)

;; Make all "yes or no" prompts show "y or n" instead
(fset 'yes-or-no-p 'y-or-n-p)

;; Disable Ctrl-Z minimization/suspension of emacs.
(global-set-key [C-z] nil)

;; Get ()-matching
(require 'paren)
(show-paren-mode 1)

;; Wheel mouse moves up and down 4 lines
(define-key global-map [mouse-4] (lambda () (interactive) (scroll-down 4)))
(define-key global-map [mouse-5] (lambda () (interactive) (scroll-up 4)))

;; Wheel mouse +  Control change buffer we are looking at
(defun next-buffer () 
  "Go to the buffer which is at the end of the buffer-list. 
   This is the symmetric of burry-buffer." 
  (interactive)
  (switch-to-buffer (nth (- (length (buffer-list)) 1) (buffer-list))))

(define-key global-map [(control mouse-4)] 'next-buffer)
(define-key global-map [(control mouse-5)] 'bury-buffer)


;; Home and End selects buffer
(global-set-key [home] 'next-buffer)
(global-set-key [end]  'bury-buffer)


;; Enable position saving through shortcuts.
;; Save current position with  Ctrl-F1 Ctrl-F2 Ctrl-F3 and Ctrl-F4
(global-set-key [C-f1] '(lambda () (interactive) (point-to-register ?1)))
(global-set-key [C-f2] '(lambda () (interactive) (point-to-register ?2)))
(global-set-key [C-f3] '(lambda () (interactive) (point-to-register ?3)))
(global-set-key [C-f4] '(lambda () (interactive) (point-to-register ?4)))

(defun jump-to-register-other (reg)
  (other-window 1)
  (jump-to-register reg)
  (hilit-recenter (/ (window-height) 2)))

(defun jump-to-register-here (reg)
  (jump-to-register reg)
  (hilit-recenter (/ (window-height) 2)))

;; Move to saved position with F1 F2 F3 and F4
(global-set-key [f1] '(lambda () (interactive) (jump-to-register-here ?1)))
(global-set-key [f2] '(lambda () (interactive) (jump-to-register-here ?2)))
(global-set-key [f3] '(lambda () (interactive) (jump-to-register-here ?3)))
(global-set-key [f4] '(lambda () (interactive) (jump-to-register-here ?4)))


;; Meta-up/down to do Page Up and Page Down, as the regular Page Up and 
;; Page down does not repeat, making it tedious to scroll large documents.
(global-set-key [M-up] 'scroll-down)
(global-set-key [M-down] 'scroll-up)


;; Highlight based on regexps
(global-set-key [M-f1] 'highlight-regexp)
(global-set-key [M-f2] 'highlight-lines-matching-regexp)
(global-set-key [M-f3] 'hi-lock-mode)
(global-set-key [M-f4] 'hi-lock-write-interactive-patterns)

;; Make changes since the file was opened stand out in red.
;; Press F12 to toggle on and off
(global-set-key [f12] 'highlight-changes-mode)
(add-hook 'c-mode-common-hook   'highlight-changes-mode)    ; in C-mode
(add-hook 'emacs-lisp-mode-hook 'highlight-changes-mode)    ; in Lisp-mode
(add-hook 'text-mode-hook       'highlight-changes-mode)    ; in Text-mode

;; Auto-fill in text mode
(add-hook 'text-mode-hook
          '(lambda ()
             (turn-on-auto-fill)
             (auto-fill-mode 1)
             ))

;; Make text-mode the default mode, so that we can use the tab-completion
;; feature in files that don't have an extension.
(setq default-major-mode 'text-mode)

;; Use spaces instead of tabs.
(setq-default indent-tabs-mode nil)

;; Move to match if on (){}[] when pressing %, otherwise insert %.
(defun match-paren (arg)
  "Go to the matching paren if on a paren; otherwise insert %."
  (interactive "p")
  (cond ((looking-at "\\s\(") (forward-list 1) (backward-char 1))
	((looking-at "\\s\)") (forward-char 1) (backward-list 1))
	((looking-at "\\s\{") (forward-list 1) (backward-char 1))
	((looking-at "\\s\}") (forward-char 1) (backward-list 1))
	((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)

;; Make sure that the file ends in a newline.
(setq require-final-newline t)


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;			      Spelling
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Do running spell check in text mode.
(add-hook 'text-mode-hook  'flyspell-mode)

;; Set the dictionary for the spell check.
(setq flyspell-mode-hook
      '(lambda () "Sets the dictionary for flyspell on startup."
	 (ispell-change-dictionary "svenska")))

;; Ctrl-Tab to spellcheck the word under the cursor.
(global-set-key [C-tab] 'ispell-word)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;			      Tab completion
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Define a -hook for all modes where we want tab completion.
(add-hook 'c-mode-common-hook
          (function (lambda ()
                    (local-set-key (kbd "") 'indent-or-complete)
                     )))
(add-hook 'text-mode-hook
          (function (lambda ()
                    (local-set-key (kbd "") 'indent-or-complete)
                     )))
(add-hook 'emacs-lisp-mode-hook 
          (function (lambda ()
                    (local-set-key (kbd "") 'indent-or-complete)
                     )))
(add-hook 'LaTeX-mode-hook 
          (function (lambda ()
                    (local-set-key (kbd "") 'indent-or-complete)
                     )))
(add-hook 'TeX-mode-hook 
          (function (lambda ()
                    (local-set-key (kbd "") 'indent-or-complete)
                     )))

(defun indent-or-complete ()
  "Complete if point is at end of a word, otherwise indent line."
  (interactive)
  (if (looking-at "\\>")
    (dabbrev-expand nil)
  (indent-for-tab-command)
   ))

;; The order that different completes are tested.
(setq hippie-expand-try-functions-list
      '(try-expand-dabbrev-visible
	try-expand-dabbrev
        try-expand-dabbrev-all-buffers
        try-expand-dabbrev-from-kill
        ; try-expand-all-abbrevs
        ; try-expand-line 
        ; try-expand-line-all-buffers
        ; try-expand-whole-kill
        ; try-expand-list
        ; try-expand-list-all-buffers
        ; try-complete-file-name-partially
        ; try-complete-file-name 
        ; try-complete-lisp-symbol-partially
        ; try-complete-lisp-symbol 
	))

;; TAB expands even during isearch (Ctrl-S)
(define-key isearch-mode-map [tab] 'isearch-yank-word)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;			      TAGS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Put the cursor over the function/variable that you want to lookup.
;; Press F10, and then Enter to confirm the search. (And give the location
;; of the TAGS-file if needed.)
;; Shift-F10 will open the matches in another frame.
;; Then use F9 and F11 to browse back and forth between the matches.

(global-set-key [f9]  'pop-tag-mark)
(global-set-key [f10] 'find-tag)
(global-set-key [S-f10] 'find-tag-other-frame)
(global-set-key [f11] "\C-u\M-.")

;; Load the TAGS file
;; TAGS file can be generated giving the following command in a shell
;; > find . -name "*.[chCH]" -print | etags - --declarations
;;(visit-tags-table "/path/to/TAGS" nil)


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;			Perl programming stuff
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Use cperl-mode instead of perl-mode 
(add-to-list 'auto-mode-alist '("\\.\\([pP][Llm]\\|al\\)\\'" . cperl-mode))
(add-to-list 'interpreter-mode-alist '("perl" . cperl-mode))
(add-to-list 'interpreter-mode-alist '("perl5" . cperl-mode))
(add-to-list 'interpreter-mode-alist '("miniperl" . cperl-mode))


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;			 Delete and backspace
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defun delete-word (arg)
  "Delete characters forward until encountering the end of a word.
With argument, do this that many times."
  (interactive "*p")
  (delete-region (point) (save-excursion (forward-word arg) (point))))

(defun backward-delete-word (arg)
  "Delete characters backward until encountering the end of a word.
With argument, do this that many times."
  (interactive "*p")
  (delete-word (- arg)))

;; Delete eats forward instead of backward
(normal-erase-is-backspace-mode 1)
(setq delete-key-deletes-forward t)

;; Meta-Backspace deletes the word from the cursor and backward.
(global-set-key [(meta backspace)] 'backward-delete-word)

;; Delete removes the character under the cursor.
(global-unset-key [(delete)])
(global-set-key [(delete)] 'delete-char)

;; Meta-delete removes the word from the cursor and forward.
(global-unset-key [(meta delete)])
(global-set-key [(meta delete)] 'delete-word)
(setq delete-key-deletes-forward t)


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;			 C programming stuff
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Hungry delete is nice since we use spaces instead of tabs.
(setq c-hungry-delete-key t) 

;; Let emacs insert newlines automatically whenever it can.
(setq c-auto-newline 1)

;; Set the K&R indentation style when starting C-mode.
(add-hook 'c-mode-hook
          '(lambda ()
             (c-set-style "k&r")
             (auto-fill-mode 1)
             ))


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;		          Small functions
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; UNIX-DOS-UNIX end of line conversions
(defun dos-unix ()
	(interactive)
	(goto-char (point-min))
	(while (search-forward "\r" nil t) (replace-match "")))

(defun unix-dos ()
	(interactive)
	(goto-char (point-min))
	(while (search-forward "\n" nil t) (replace-match "\r\n")))

;; Poor mans TTCN reader
(defun ttcn ()
  "Makes a trivial reformating of TTCN logs."
  (interactive)
  (goto-char (point-min))
  (while (search-forward "{" nil t) (replace-match "{\n\t"))
  (goto-char (point-min))
  (while (search-forward "," nil t) (replace-match ",\n\t"))
  (goto-char (point-min)))


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;		       Rubbish below this line
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


;; SSLUG Swedish spell checker.

;(require 'ispell)
;(autoload 'ispell-word "ispell"
;  "Check the spelling of word in buffer." t)
;(global-set-key "\e$" 'ispell-word)
;(autoload 'ispell-region "ispell"
;  "Check the spelling of region." t)
;(autoload 'ispell-buffer "ispell"
;  "Check the spelling of buffer." t)
;(autoload 'ispell-complete-word "ispell"
;  "Look up current word in dictionary and try to complete it." t)
;(autoload 'ispell-change-dictionary "ispell"
;  "Change ispell dictionary." t)
;(autoload 'ispell-message "ispell"
;  "Check spelling of mail message or news post.")

;(setq ispell-dictionary "svenska")
;(add-to-list 'ispell-dictionary-alist
;      '(("svenska"
;	 "[A-Za-z\345\344\366\305\304\366]" "[^A-Za-z\345\344\366\305\304\366]"
;	 "[']" nil ("-d" "svenska") "~list"
;	 iso-latin-1)
;	("svenska8"
;	 "[A-Za-z\345\344\366\305\304\366]" "[^A-Za-z\345\344\366\305\304\366]"
;	 "[']" nil ("-B" "-d" "svenska") "~list"
;	 iso-latin-1)
;          ))
;(setq ispell-default-dictionary "svenska")

;; Swedish on the fly spell checker.
;(require 'ispell)
;(add-to-list 'ispell-local-dictionary-alist
;             '("svenskax"
;               "[[:alpha:]]"
;               "[^[:alpha:]]"
;               "[']" t
;               ("-C" "-d" "svenska")
;               "~latin1" iso-8859-15))
;(setq ispell-default-dictionary "svenska")
;(add-hook 'text-mode-hook  'flyspell-mode)
;(add-hook 'LaTeX-mode-hook 'flyspell-mode)


Back to the index.