Emacs customization examples

[ambient page updated 24 Feb 05] ... [ home ] ... [ garrett@math.umn.edu ]

; This top piece could be a ~/.emacs file
:
; One's ~/.emacs file is run when Emacs is started

(setq load-path 
		(append load-path 
				  (
					list "~/lisp" "~/lisp/text" "~/lisp/small"
				  )
		)

;; the latter adds the indicated unix directories to the search
;; path for the following... The two files loaded are inside ~/lisp

(load-library "mysettings")
(load-library "myfunctions")

;; the following are inside "~/lisp/small"

(load-library "remove-html") 
(load-library "get-login")
(load-library "quote-region")

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; the following is a sample "mysettings.el", where _variables_ are set
;;
;; A few add-hook's
;;
;; _Function_ definitions (and their additions) are in myfuntions.el

(custom-set-variables
 '(tool-bar-mode 0)       ;; gets rid of toolbar
 '(blink-cursor nil)      ;; no blinking
 '(menu-bar-mode 0)       ;; no menu bar
 
 '(delete-key-deletes-forward t) ;; delete key deletes forward (as expected)
 '(line-number-mode t)           ;; show line and column number of cursor
 '(column-number-mode t)
 '(mouse-yank-at-point t)        ;; paste where the cursor is, not mouse
 ;; '(make-backup-files nil)     ;; defaults to no backups, anyway
 '(visible-bell t)               ;; flash
 '(user-mail-address "garrett@math.umn.edu")             ;; for mail
)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Set up colors 
;

(defun good-colors ()
  (progn
	 (set-background-color "DimGray")
	 (set-foreground-color "LightGray")
	 (set-cursor-color "DarkSlateBlue")
	 (set-border-color "DimGray")
	 (set-mouse-color "DarkSlateBlue")
	 
	 (set-face-background 'default "DimGray")
	 (set-face-background 'region "DarkSlateGray")
	 (set-face-background 'highlight "DarkSlateBlue")
	 (set-face-background 'modeline "DarkSlateBlue") ;;; CornflowerBlue")
	 
	 (set-face-foreground 'default "LightGray")
	 (set-face-foreground 'region "Ivory")
	 (set-face-foreground 'highlight "LightGray")  ;;; DimGray")
	 (set-face-foreground 'modeline "LightGray")
	 ))

(good-colors)  ;; calls the previously-defined function

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; a global abbreviation

(global-set-key "\M-n" 'bookmark-jump)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; close potential security hole

(setq enable-local-variables nil)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; allow interactive-mode evaluation, obviously...

(put 'eval-expression 'disabled nil)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; uncompress before editing
(auto-compression-mode 1)

;; perl rather than prolog from .pl files
(autoload 'perl-mode "perl-mode" "Major mode for editing Perl scripts." t)
(setq auto-mode-alist 
      (append (list (cons "\\.pl$" 'perl-mode)) auto-mode-alist))

(setq backup-by-copying-when-linked t)
(setq dired-use-gzip-instead-of-compress t)

;; allow elisp evaluations anywhere
(global-set-key "\C-j" 'eval-print-last-sexp)

;; default "history" length is just 32, apparently! Jeez.
(setq comint-input-ring-size 2000)

;; text mode as default              ;; I don't actually do this...
(setq default-major-mode 'text-mode)

;; auto-fill-mode:
(setq text-mode-hook 'turn-on-auto-fill)
(setq tex-mode-hook 'turn-on-auto-fill)
(setq mail-mode-hook 'turn-on-auto-fill)

;; Abbreviation stuff

(setq abbrev-file-name "~/lisp/abbrev_defs")

;; reads abbreviation file
(read-abbrev-file "~/lisp/abbrev_defs")      ;; reinstated 22 Dec 2003

;; easier addition of abbreviations
;; (define-key text-mode-map "\C-x+" 'add-mode-abbrev)

;; easier toggle of abbrev mode
;; (define-key text-mode-map "\C-xa" 'abbrev-mode)

;; Easier goto-line
(global-set-key "\M-;" 'goto-line)

;; Easier append-to-file (3-25-96)
(global-set-key "\M-]" 'append-to-file)

;; suppress echoing of passwords in shell buffers
(add-hook 'comint-output-filter-functions
			 'comint-watch-for-password-prompt)

;; highlighting in certain modes, etc.
;; no, now they're ok...
(add-hook 'shell-mode-hook 'turn-on-font-lock)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defun write-to-minibuffer (input-string)
  "Write input string to minibuffer"
  (message input-string)                  ;; writes to minibuffer!!!
)

(defun insert-my-url ()
  "Insert my tmp-directory url in text."
  (interactive)
  (insert "\thttp://www.math.umn.edu/~garrett/")
)

(global-set-key "\M-+" 'insert-my-url)

;; evidently there was no rmail-mode-hook otherwise...
;; _not_ created in "mysettings.el"

(setq rmail-mode-hook
		'(lambda ()
			(define-key rmail-mode-map "n" 'better-rmail-next-message)
			(define-key rmail-mode-map "p" 'better-rmail-previous-message)
			)
)

(global-set-key  "\M-_" 'revert-buffer)

(defun bash-head ()
  (interactive)
  (insert "#!/bin/bash")
)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; A more complicated example of doing stuff...

; Goal is to do "grep" on lines in passwd file, requiring match
; to _all_ strings obtained by splitting input'd string on 
; whitespace
;
; Main function is the interactive "get-login-and-insert", which will
; find all logins matching a given list of regexps, for which the user
; is prompted.
;

(defun find-lines-matching-string (inp)              ;; in current buffer...
  (interactive "sSub-string to match: ")
  (let (
		  (return-value-list (list))
		  (regexp-to-match "")
		  )
	 (setq regexp-to-match (concat "^.*" inp ".*$"))
	 (save-excursion
		(while (re-search-forward 
				  regexp-to-match
				  (point-max) t
				  )
		  (setq return-value-list (cons (match-string 0) return-value-list)) 
		  )
		) ;; end of save-excursion
	 return-value-list
	 )
  )

(defun find-lines-matching-list-of-strings (list-of-strings)
  (let (
		  (return-value-list (list))
		  (the-line "")
		  )
	 (save-excursion
		(while (re-search-forward 
				  "^.+$"                    ;; a line
				  (point-max) t
				  )
		  (setq the-line (match-string 0))
		  (if (test-string-for-match-with-list-of-strings the-line list-of-strings)
				(setq return-value-list (cons the-line return-value-list))
				)
		  )
		) ;; end of save-excursion
	 return-value-list
	 )
  )

(defun test-string-for-match-with-list-of-strings (str str-list)
  (let (
		  (test-str "")
		  (return-boolean t)
		  )
	 (while (and
				( > (length str-list) 0 )
				return-boolean
				)
		(setq test-str (car str-list))
		(setq str-list (cdr str-list))
		(if (string-match test-str str)
			 () ;; do nothing
		  (setq return-boolean nil)
		  )
		)
	 ;
	 ; by now, either didn't match or length of str-list is 0
	 ; meaning that all strings in str-list _did_ match 
	 ;
	 return-boolean
	 ) ;; end of "let"
  )

(defun extract-matching-substrings-from-list-of-strings (reg str-list)
  (let 
		(
		 (return-list (list))
		 (list-element "")
		 )
	 (setq reg (concat "\\(" reg "\\)"))
		(while (> (length str-list) 0)
		  (setq list-element (car str-list))
		  (if (string-match reg list-element)
				(progn
				  (setq return-list (cons (substring 
													list-element
													(match-beginning 0) 
													(match-end 0)
													)
												  return-list)
						  )
				  )
			 () ;; else do nothing
			 )
		  (setq str-list (cdr str-list))
		  )
		return-list   ;; return value
		)
  )

(defun get-logins-matching-fragments-of-string (str)
  (let (
		  (addr-str "")
		  )
	 (save-excursion
		(find-file-read-only "/etc/passwd")
		(setq buffer-name "very_unlikely_buffer_name")
		(rename-buffer buffer-name)
		(setq 
		 addr-str
		 (list-to-comma-separated 
		  (extract-matching-substrings-from-list-of-strings 
			"^[^:]+"
			(find-lines-matching-list-of-strings (split-on-whitespace str))
			)
		  )
		 )
		(kill-buffer buffer-name)
		) ;; end of save-excursion
	 addr-str           ;; return value
	 ) ;; end of "let"
  )

(defun get-login-and-insert (str)
  (interactive "sString to split and match pieces of: ")
  (insert (get-logins-matching-fragments-of-string str))
)

(defun list-to-comma-separated (the-list)
  (let (
		  (return-string "")
		  )
  (while (> (length the-list) 0)
	 (if (> (length return-string) 0)
		  (setq return-string (concat return-string ", " (car the-list)))
		(setq return-string (car the-list))  ;; else
		)
	 (setq the-list (cdr the-list))
	 ) ;; end of "while"
  return-string
  ) ;; end of "let"
)

(defun get-login-from-passwd-file (string-to-match)
  (find-file-read-only "/etc/passwd")
  (setq buffer-name "very_unlikely_buffer_name")
  (rename-buffer buffer-name)
  (setq mystr (findstring string-to-match))
  (kill-buffer buffer-name)
  mystr
)

(defun split-on-whitespace (str)
  "Accepts a single string input. Splits on whitespace. Returns list of strings."
  (setq list-to-match (list))
  (while 
		(> (length str) 0)
	 (progn
		(string-match "\\([^ ]*\\)[ ]*\\(.*\\)" str)
		(setq list-to-match (cons 
									(substring str (match-beginning 1) (match-end 1))
									list-to-match)
				)
		(setq str (substring str (match-beginning 2) (match-end 2)))
		)
	 )
  list-to-match      ;; return value
  )

(defun match-string-to-list-of-strings (str list-of-str)
  (setq return-value t)
  (while (> (length list-of-str) 0)
	 (if (string-match (car list-of-str) str)
		  (setq list-of-str (cdr list-of-str))
		(setq return-value nil)
		)
	 )
  return-value  ;; return value "t" or "nil"
)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


Unless explicitly noted otherwise, everything here, work by Paul Garrett, is licensed under a Creative Commons Attribution 3.0 Unported License. ... [ garrett@math.umn.edu ]

The University of Minnesota explicitly requires that I state that "The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota."