; ~cymbala/.emacs_essential
;
; Emacs customization I like to have happen on any computer (see also
; ~/.emacs on laptop).
;
;    Time-stamp: <2004-10-03 22:23:16 cymbala>

(global-set-key [f2 ?l] '(lambda nil "" (interactive) (shell-command "xlock")))
(global-set-key [f11] 'bury-buffer)
(global-set-key "\C-x\C-c" 'buffer-menu) ; Use "F10 f e" instead

;;; Killed *scratch* with F12 one too many times...
(defun kill-this-buffer ()	; for the menubar
  "Kills the current buffer."
  (interactive)
  (if (not (string= "*scratch*" (buffer-name)))
      (kill-buffer (current-buffer))))

;;; Indent an XHTML document, top to bottom.
;;; (while (< (point) (point-max)) (progn (sgml-indent-or-tab) (next-line 1) (end-of-line)))

;;; Why-are-not-these-standard?
(resize-minibuffer-mode)


;;; Email
(setq mail-yank-prefix "> ")
(setq mail-signature t)
(setq mail-signature-file "~/.signature")
(global-set-key [f7] 'rmail)
(global-set-key [f12] 'kill-this-buffer)
(setq rmail-delete-after-output t)
(add-hook 'rmail-show-message-hook
	  (lambda 
	    nil (progn
		  (search-forward-regexp "^From: \\(.*\\)")
		  (setq foo (match-string 1))
		  (goto-char (point-min))
		  (if (search-forward-regexp "^Subject: \\(.*\\)" nil t)
		      (setq foo (concat foo ": " (match-string 1))))
		  (message "%s" foo))))


;;; Sun Apr 30 09:52:29 PDT 2000
;   pg. 299 GNU Emacs manual
(add-hook 'mail-setup-hook 'mail-abbrevs-setup)



;;; SGML
;;; originally just html-mode...
;;; OLD: (add-to-list 'auto-mode-alist '("\\.s?html?\\'" . sgml-html-mode))


;;; Keystrokes
(global-set-key [f4] 'query-replace-regexp)
(global-set-key [f5] 'goto-line) ; F5 is near middle of number keys.
(global-set-key [f5] 'erase-buffer)
(global-set-key [f6] 'find-file-literally)
(global-set-key "\C-x\C-b" 'buffer-menu)
(global-set-key "\C-co" 'occur)
(global-set-key "\C-cj" 'join-line)

(global-set-key [f19] 'ispell-buffer)
(add-hook 'mail-setup-hook
	   (lambda ()
	     (local-set-key [f19] 'ispell-message)))

;;; Thu Oct  5 05:32:21 PDT 2000
; Liked it better when HOME and END went to ends of buffer, not line.
(global-set-key [home] 'beginning-of-buffer)
(global-set-key [end] 'end-of-buffer)

;;; [f8] and [f9] together quickly jumps to other buffer and maximizes it.
;;;
;;; Thu Oct 12 05:02:39 PDT 2000
;;; Idea is to always press F8, then F9.
;;; If just one window, will bury it; else,
;;;   will switch to other window and make it only window.
;;
;;; Switch to other window and "maximize" it:
(global-set-key [f8] 'other-window)
(global-set-key [f9]
		(lambda ()
		  (interactive)
		  (if (one-window-p)
		      (bury-buffer)
		    (delete-other-windows))))

;;; Time.
(display-time)
(setq display-time-day-and-date nil)
(defun rjc-insert-date-yyyymmdd (arg1)
  "Date"
  (interactive "p")
  (insert (format-time-string "%Y.%m.%d")))


;;; Time-stamp.
; default:
;  (time-stamp-yy/mm/dd time-stamp-hh:mm:ss user-login-name)
(setq time-stamp-format
      '(time-stamp-yyyy-mm-dd time-stamp-hh:mm:ss user-login-name))
(setq time-stamp-format
      "%:y-%02m-%02d %02H:%02M:%02S %u")
(add-hook 'write-file-hooks 'time-stamp)


;;; GNU Emacs Pocket Reference
(setq default-major-mode 'text-mode)
(add-hook 'text-mode-hook 'turn-on-auto-fill)


;;; Mon Oct 23 07:04:46 PDT 2000
(autoload 'lout-mode "lout-mode" "Major mode for editing Lout text" t)
(setq auto-mode-alist
      (append '(("\\.lout\\'" . lout-mode)) auto-mode-alist))


;;; Syntax highlighting
(cond ((fboundp 'global-font-lock-mode)
       ;; Turn on font-lock in all modes that support it
       (global-font-lock-mode t)
       ;; Maximum colors
       (setq font-lock-maximum-decoration t)))

;;; Sun Jul 29 14:05:07 PDT 2001
(defun rjc-sort-lines (raw-prefix-argument)
       "Sort lines in buffer"
       (interactive "P")
       (save-excursion
	 (save-restriction
	   (end-of-buffer)
	   (sort-lines raw-prefix-argument 1 (point)))))


;;; Sat Apr  8 21:29:00 PDT 2000
;   SEE ALSO /etc/emacs/site-start.el
;   Advised Buffer Switching (Bob Glickstein, Writing GNU Emacs Extensions).
;   pg. 30-32
(defadvice switch-to-buffer (before existing-buffer
				    activate compile)
  "When interactive, switch to existing buffers only,
unless given a prefix argument."
  (interactive
   (list (read-buffer "Switch to buffer: "
		      (other-buffer)
		      (null current-prefix-arg)))))
(defadvice switch-to-buffer-other-window (before existing-buffer
				    activate compile)
  "When interactive, switch to existing buffers only,
unless given a prefix argument."
  (interactive
   (list (read-buffer "Switch to buffer: "
		      (other-buffer)
		      (null current-prefix-arg)))))
(defadvice switch-to-buffer-other-frame (before existing-buffer
				    activate compile)
  "When interactive, switch to existing buffers only,
unless given a prefix argument."
  (interactive
   (list (read-buffer "Switch to buffer: "
		      (other-buffer)
		      (null current-prefix-arg)))))

;;; Tue Apr  9 06:36:54 PDT 2002
(setq debug-on-error t)

;;; Fri Apr 19 01:13:22 PDT 2002
(defun rjc-conditional-xml-mode ()
  "If buffer is using SGML mode, say due to .htm, maybe switch to XML mode."
  (interactive "p")
  (save-excursion
    (save-restriction
      (goto-char (point-min))
      ;; "DTD XHTML" appears within a <!DOCTYPE for XHTML.
      (let ((my-doctype-chunk "DTD XHTML"))
	(if (and (string-equal mode-name "SGML")	
		 (search-forward-regexp my-doctype-chunk 100 t))
	    (xml-mode))))))
; Before:
; find-file-hooks (turn-on-font-lock-if-enabled vc-find-file-hook)
(add-hook 'find-file-hooks 'rjc-conditional-xml-mode)
; After:
; find-file-hooks (rjc-conditional-xml-mode turn-on-font-lock-if-enabled vc-find-file-hook)

;;; Tue Jun 25 00:16:47 PDT 2002
(defun rjc-html-convert-numeric-entities (args)
  "Convert single characters to numeric entities"
  (interactive "p")
  (let (
	(foo "bar")
	)
    (save-excursion
      (save-restriction
					; SEE: /www/public_html/admin/janitor/charcode.htm

					; Confirmed visually using
					; Microsoft Internet Explorer 5.5:
 					; 221, 222, 223, 224, 226 and 227.

					; dc1 : 221 (left single quote):
	(beginning-of-buffer)
	(while (search-forward "‘" nil t)
	  (replace-match "&#8216;" nil t))
					; dc2 : 222 (right single quote):
	(beginning-of-buffer)
	(while (search-forward "’" nil t)
	  (replace-match "&#8217;" nil t))
					; dc3 : 223 (left double quote):
	(beginning-of-buffer)
	(while (search-forward "“" nil t)
	  (replace-match "&#8220;" nil t))
					; dc4 : 224 (right double quote):
	(beginning-of-buffer)
	(while (search-forward "”" nil t)
	  (replace-match "&#8221;" nil t))
					; syn : 226 (en dash):
	(beginning-of-buffer)
	(while (search-forward "–" nil t)
	  (replace-match "&#8211;" nil t))
					; etb : 227 (em dash):
	(beginning-of-buffer)
	(while (search-forward "—" nil t)
	  (replace-match "&#8212;" nil t))

					; OTHERS:
					; etx : 203 (&fnof;):
	(beginning-of-buffer)
	(while (search-forward "ƒ" nil t)
	  (replace-match "&#402;" nil t))
					; eot : 204 (&bdquo;):
	(beginning-of-buffer)
	(while (search-forward "„" nil t)
	  (replace-match "&#8222;" nil t))
					; enq : 205 (&hellip;):
	(beginning-of-buffer)
	(while (search-forward "…" nil t)
	  (replace-match "&#8230;" nil t))
					; ack : 206 (&dagger;):
	(beginning-of-buffer)
	(while (search-forward "†" nil t)
	  (replace-match "&#8224;" nil t))
					; bel : 207 (&Dagger;):
	(beginning-of-buffer)
	(while (search-forward "‡" nil t)
	  (replace-match "&#8225;" nil t))
					; bs : 210 (&circ;):
	(beginning-of-buffer)
	(while (search-forward "ˆ" nil t)
	  (replace-match "&#710;" nil t))
					; vt : 213 (&lsaquo;):
	(beginning-of-buffer)
	(while (search-forward "‹" nil t)
	  (replace-match "&#8249;" nil t))
					; ff : 214 (&OElig;):
	(beginning-of-buffer)
	(while (search-forward "Œ" nil t)
	  (replace-match "&#338;" nil t))

					; LAST BUT NOT LEAST:
					; del : 377 (&yuml;):
	(beginning-of-buffer)
	(while (search-forward "ÿ" nil t)
	  (replace-match "&#255;" nil t))
	;;
	))))

;;; Sat Jun 29 12:17:10 PDT 2002
;;; USAGE: (add-hook 'mail-setup-hook 'rjc-mail-reply-to)
(defun rjc-mail-reply-to ()
  ""
  (interactive "p")
  (let (
	(my-email-address ""))
    (save-excursion
      (save-restriction
	 (setq my-email-address
	       (read-string "Enter email address for reply-to: "
			    "__@marxists.org"))
	 (if (string-equal my-email-address "")
	     (message "Insertion of \"Reply-To:\" cancelled by user.")
	   (mail-reply-to)
	   (insert my-email-address))))))

;;; Fri Aug 30 10:43:43 PDT 2002
(defun my-change-doctype-to-xhtml (args)
  "Change DOCTYPE to XHTML"
  (interactive "p")
  (let (
	(my-doctype-with-open-html-tag
	 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"
        \"http://www.w3.org/TR/2000/REC-xhtml1-20000126/DTD/xhtml1-transitional.dtd\">

<html xmlns=\"http://www.w3.org/1999/xhtml\">")
	)
    (save-excursion
      (save-restriction
	(beginning-of-buffer)
	(if (not (search-forward-regexp 
		  (concat
		   "<![dD][oO][cC][tT][yY][pP][eE]"
		   "[^<]+"  ""
		   "<[hH][tT][mM][lL]"
		   "[^>]*"  ">")
					; "An optional second argument
					;  bounds the search; it is a
					;  buffer position."

					; (Assumption: HTML tag is
					; within first (n)
					; characters.)
		  1000
					; "Optional third argument, if
					;  t, means if fail just return
					;  nil (no error)."
		  t))
	    (message "Did not find DOCTYPE to change.")

	  ;; All that just to do this...
	  (replace-match my-doctype-with-open-html-tag)
	  (xml-mode))))))

;;; Fri Feb 21 11:23:04 PST 2003
(defun my-sgml-numeric-entities-four-digits (args)
  "Change &<> to entities"
  (interactive "p")
  (let ((my-list nil))
    (save-excursion
      (save-restriction
	(beginning-of-buffer) (while (search-forward "&#151;" nil t)
				(replace-match "&#8212;"))
	))))

;;; Tue Oct 22 22:08:20 PDT 2002
(defun my-sgml-entities-bare-minimum (args)
  "Change &<> to entities"
  (interactive "p")
  (let ((my-list nil))
    (save-excursion
      (save-restriction
	(beginning-of-buffer) (while (search-forward "&" nil t)
				(replace-match "&amp;"))
	(beginning-of-buffer) (while (search-forward "<" nil t)
				(replace-match "&lt;"))
	;; ">" not required (just "&" and "<" are required) ...
	(beginning-of-buffer) (while (search-forward ">" nil t)
				(replace-match "&gt;"))
	))))

;;; Wed Jan 22 11:44:17 PST 2003
(defun rjc-sgml-entities-to-four-digits (args)
  "documentation"
  ;;
  ;; Example:
  ;;
  (interactive "P")
  (save-excursion
    (save-restriction
      (beginning-of-buffer) (while (re-search-forward "&#145;" nil t)
			      (replace-match "&#8216;"))
      (beginning-of-buffer) (while (re-search-forward "&#146;" nil t)
			      (replace-match "&#8217;"))
      (beginning-of-buffer) (while (re-search-forward "&#147;" nil t)
			      (replace-match "&#8220;"))
      (beginning-of-buffer) (while (re-search-forward "&#148;" nil t)
			      (replace-match "&#8221;"))
      )))

;;; Sat Oct 26 12:49:56 PDT 2002
(setq my-add-a '("DocBk XML V3.1.3" "<?xml version=\"1.0\"?>
<!DOCTYPE HTML PUBLIC \"-//Norman Walsh//DTD DocBk XML V3.1.3//EN\"
                      \"dtd/docbook-xml/docbookx.dtd\">
"))
;;
(setq my-add-b '("Simplified DocBk XML V3.1.3.6" "<?xml version=\"1.0\"?>
<!DOCTYPE article PUBLIC 
      \"-//Norman Walsh//DTD Simplified DocBk XML V3.1.3.6//EN\"
      \"dtd/docbook-xml/sdocbook.dtd\">
"))
;;
(setq my-add-c '("XHTML 1.0 Transitional" "<?xml version=\"1.0\"?>
<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"
                      \"dtd/xhtml1-transitional.dtd\">
"))
;; Ensure each one is available as a compiled doctype...
(setq sgml-custom-dtd (if (boundp 'sgml-custom-dtd)
			  (append sgml-custom-dtd my-add-a my-add-b my-add-c)
			(list my-add-a my-add-b my-add-c)))
;; (setq sgml-custom-dtd (list my-add-a my-add-b my-add-c))

;;;
;; Local Variables:
;; eval: (emacs-lisp-mode)
;; End: