(setq standard-display-table (make-display-table))
(set-input-mode nil nil 'We-will-use-eighth-bit-of-input-byte)
(standard-display-8bit 128 255)
(standard-display-8bit-1 standard-display-table 128 255)

;;
;; Key bindings
;;
(global-set-key [help] 'info)
(global-set-key [f3] 'find-file)
(global-set-key [(shift f3)] 'find-file-at-point)
(global-set-key [f2] 'save-buffer)
(global-set-key [f5] 'delete-other-windows)
(global-set-key [f6] 'other-window)
(global-set-key [f7] 'replace-string)
(global-set-key [f8] 'next-error)
(global-set-key [f9] 'My-Compile)
(global-set-key [f10] 'delete-frame)
(global-set-key "\M-0" 'buffer-menu)
(global-set-key "\C-y" 'Kill-Line)
(global-set-key [(control Icircumflex)] 'Kill-Line) ; C-y
(global-set-key [(control Iacute)] 'scroll-up) ; C-v
(global-set-key [(control Ugrave)] 'isearch-forward) ; C-s
(global-set-key [(control menu)] 'popup-mode-menu)
(global-set-key [(shift insert)] 'yank)
(global-set-key [(shift delete)] 'copy-region-as-kill)
(global-set-key [(control x) m] 'mh-smail)
(global-set-key [(control x) r j] 'bookmark-jump)
(global-set-key [(meta THORN)] 'execute-extended-command)

(autoload 'find-file-at-point "ffap")

(if (device-on-window-system-p)
    (progn
      (setq delete-key-deletes-forward t)
      (global-set-key [(kp-7)] 'beginning-of-line)
      (global-set-key [(kp-8)] 'previous-line)
      (global-set-key [(kp-9)] 'scroll-down)
      (global-set-key [(kp-4)] 'backward-char)
      (global-set-key [(kp-6)] 'forward-char)
      (global-set-key [(kp-1)] 'end-of-line)
      (global-set-key [(kp-2)] 'next-line)
      (global-set-key [(kp-3)] 'scroll-up)
      (global-set-key [(kp-5)] 'next-line)
      (global-set-key [(kp-0)] 'overwrite-mode)
      (global-set-key [(kp-decimal)] 'backward-or-forward-delete-char)
      (global-set-key [(shift kp-0)] 'yank)
      (global-set-key [(shift kp-decimal)] 'copy-region-as-kill)
      )
  (setq delete-key-deletes-forward nil))

(global-set-key [(alt i)]
  '(lambda ()
     (interactive)
     (shell-command (read-string "Shell command to insert: ") t)))

(quietly-read-abbrev-file)

(defun Kill-Line ()
  (interactive)
  (beginning-of-line)
  (kill-line))


(defun Kill-Buffer ()
  (interactive)
  (kill-buffer (current-buffer)))

;; To be called from gnudoit
(defun MyGNUS ()
  (deiconify-frame)
  (gnus))

(defun toolbar-news ()
  "Run Gnus in the frame it was started from."
  (interactive)
  (gnus))

;;;
;;; -------- Global options
;;;

(setq kill-whole-line t
      default-major-mode 'text-mode
      use-dialog-box nil
      inhibit-startup-message t
      gnus-inhibit-startup-message t
      gnus-read-active-file nil
      gnus-check-new-newsgroups 'ask-server
      delete-key-deletes-forward t
      options-save-faces t
      next-line-add-newlines nil
      scroll-step 1)

;(autoload 'maniac-fill-mode "maniac" nil t)

(add-hook 'find-file-not-found-hooks 'MyAutoInsert)
(add-hook 'text-mode-hook 'turn-on-auto-fill)
(require 'cc-mode)
(add-hook 'c-mode-common-hook 
	  '(lambda ()
	     (c-set-style "cc-mode")
	     (line-number-mode 1)
	     (column-number-mode 1)
	     (abbrev-mode 1)
	     (auto-fill-mode 1)
	     (setq tab-width 4)
	     )
	  )

(defun MyAutoInsert ()
  (let ((f-name (buffer-file-name)))
    (if (or (string-match "\\.C$" f-name) (string-match "\\.h$" f-name))
	(insert "// -*- C++ -*-\n"
		"// File: " (file-name-nondirectory f-name) "\n"
		"//\n"
		"// Created: " (current-time-string) "\n"
		"//\n"
		"// $Id$\n"
		"//\n"))))

(defun My-Compile ()
  "Saves all unsaved buffers, and runs 'compile'."
  (interactive)
  (save-some-buffers t)
  (compile compile-command)
  )

(gnuserv-start)

(setq auto-mode-alist
      (append auto-mode-alist
	      '(
		( "\\html.ru\\'"	. html-mode )
		( "\\html.en\\'"	. html-mode )
		( "\\tei\\'"		. sgml-mode )
		( "\\docbook\\'"	. sgml-mode )
		( "\\dsl\\'"		. scheme-mode )
		( "\\gwm\\'"		. lisp-mode )
		)
	      )
      )

;;
;; Supercite
;;


(load-library "supercite")
(setq sc-preferred-header-style 0
      sc-rewrite-header-list '((my-header-line-generator))
      sc-citation-leader " "
      sc-confirm-always-p nil
      sc-preferred-attribution-list 
      '( "x-attribution" "firstname" "initials" "sc-lastchoice" )
      )

(defun my-header-line-generator ()
  (let ((sc-mumble "")
	(whofrom (sc-whofrom)))
    (if whofrom
	(insert sc-reference-tag-string
		(sc-hdr "\"" (sc-mail-field "sc-attribution") "\" == ")
		(sc-hdr ""   (sc-mail-field "sc-author") " ")
		"writes:\n")
      )
    )
  )

(setq gnus-use-sc t
      gnus-select-method '(nntp "localhost")
      gnus-asynchronous nil
      nnspool-active-file "/var/lib/news/active"
      nnspool-active-times-file "/var/lib/news/active.times"
      default-mime-charset 'koi8-r
      mime-setup-use-sc t
      mime-charset-type-list  
      '((koi8-r         8 nil)
	(us-ascii	7 nil)
	(iso-8859-1	8 "quoted-printable")
	(iso-8859-2	8 "quoted-printable")
	(iso-8859-3	8 "quoted-printable")
	(iso-8859-4	8 "quoted-printable")
	(iso-8859-5	8 "quoted-printable")
	(iso-8859-7	8 "quoted-printable")
	(iso-8859-8	8 "quoted-printable")
	(iso-8859-9	8 "quoted-printable")
	(iso-2022-jp	7 "base64")
	(iso-2022-kr	7 "base64")
	(euc-kr		8 "base64")
	(gb2312		8 "quoted-printable")
	(big5		8 "base64")
	(iso-2022-jp-2	7 "base64")
	(iso-2022-int-1	7 "base64")
	)
      mime/field-encoding-method-alist
      '(
	("Subject" . nil)
	)
      mime-eword/charset-encoding-alist
      '((koi8-r		. nil)
	(us-ascii	. nil)
	)
      )

(require 'gnus-setup)
(require 'gnus-charset)

(gnus-set-newsgroup-default-charset ".*" "koi8-r")

(add-hook 'gnus-article-display-hook
	  '(lambda ()
	     (gnus-article-highlight)
	     (gnus-article-hide-headers-if-wanted)
	     (setq gnus-visible-headers 
		   '("^From:" "^X-Comment-To:" "^Comment-To:" "^Subject:" "^Date:" 
		     "^Followup-To:" "^Reply-To:" "^Organization:" "^Summary:" 
		     "^Keywords:" "^To:" "^Cc:" "^Posted-To:" "^Mail-Copies-To:" 
		     "^Apparently-To:" "^Gnus-Warning:" "^Resent-From"
		     )
		   )
	     (article-emphasize)))

(add-hook 'gnus-summary-mode-hook
	  '(lambda ()
	     (local-set-key [tab] 'gnus-summary-next-unread-article)))

(add-hook 'gnus-summary-exit-hook 'gnus-summary-bubble-group)

(add-hook 'message-mode-hook
	  '(lambda ()
	     (auto-fill-mode 1)
	     (font-lock-mode 1)
	     (setq fill-column 75)))

(add-hook 'message-setup-hook
	  '(lambda ()
	     ;; Add X-Comment-To: field. We need From: field from original
	     ;; letter. How to get right? 
	     (if message-reply-buffer
		 (let ((cur-buffer (current-buffer)) from-field)
		   (save-excursion
		     (set-buffer message-reply-buffer)
		     (if (string-match "^From: " (buffer-string))
			 (progn
			   (setq from-start (+ (match-end 0) 1))
			   (goto-char from-start)
			   (end-of-line)
			   (setq from-field (buffer-substring from-start (point))))))
		   (if from-field
		       (save-excursion
			 (goto-char (point-min))
			 (insert "X-Comment-To: " (Make-Comment-To from-field) "\n")))))
	     (setq message-signature 'My-Signature)))

(defun My-Signature ()
  (save-excursion
    (set-buffer (get-buffer-create "*fortune*"))
    (delete-region (point-min) (point-max))
    (call-process "/usr/games/fortune" nil t nil (concat (getenv "HOME") "/.fortunes"))
    (concat "\tBest regards, -- Boris.\n\n" (buffer-string))))

(defun Make-Comment-To (from)
  (if (string-match " *<.*> *" from)
      (concat (substring from 0 (match-beginning 0)) (substring from
								(match-end 0)))
    (if (string-match "\(.*\)" from)
	(substring from (+ (match-beginning 0) 1) (- (match-end 0) 1))
      from)))

(add-hook 'sc-load-hook 'sc-setup-filladapt)

;---

(setq nnmail-split-methods '(("inbox" "")))

(setq mc-pgp-user-id "0xFC7D5E25")

;;
;; Outline
;;
(add-hook 'outline-mode-hook 
	  '(lambda () 
	     (line-number-mode 1)
	     (column-number-mode 1)))
 
;;
;; SGML
;;

(setq sgml-catalog-files 
      (list "catalog"
	    "CATALOG"
	    "~/work/TEItools/site/dtd/catalog"
	    "~/work/TEItools/dtd/catalog"
	    "/usr/local/lib/xemacs-20/etc/sgml/CATALOG")
      sgml-custom-dtd 
      '(("TEI Lite" "<!doctype tei.2 public '-//TEI//DTD TEI Lite 1.5 //EN'>"
	 "/tmp/teilite.ced")
	("TZ" "<!doctype tz system>" "/tmp/tz.ced")
	("CALS" "<!doctype table PUBLIC '-//USA-DOD//DTD Table Model 951010//EN'>"
	 "/tmp/cals.ced")
        ("DocBook (book)" 
	 "<!doctype book public '-//Davenport//DTD DocBook V3.0//EN'>"
         "/tmp/docbook.ced")
        ("DocBook (part)" 
	 "<!doctype part public '-//Davenport//DTD DocBook V3.0//EN'>"
         "/tmp/docpart.ced")
        ("DocBook (chapter)" 
	 "<!doctype chapter public '-//Davenport//DTD DocBook V3.0//EN'>"
         "/tmp/docchap.ced")
        ("DocBook (article)" 
	 "<!doctype Article public '-//Davenport//DTD DocBook V3.0//EN'>"
         "/tmp/docart.ced")
	("HTML with frames"
	 "<!doctype html public '+//Silmaril//DTD HTML Pro v0r11 19970101//EN'>")
	)
      )

(defun My-SGML-keymap ()
  (local-set-key [(alt i)] 
		 '(lambda ()
		    (interactive)
		    (sgml-indent-line)
		    (insert "\n\n<item> ")
		    (sgml-indent-line)))
  (local-set-key [(alt l)] 
		 '(lambda ()
		    (interactive)
		    (insert "<list>\n\n<item> ")
		    (sgml-indent-line)))
  (local-set-key [(alt p)]
		 '(lambda ()
		    (interactive)
		    (sgml-indent-line)
		    (insert "\n\n<p> ")
		    (sgml-indent-line)))
  
  )

(add-hook 'sgml-mode-hook 
	  '(lambda ()
	     (make-local-variable 'compile-command)
	     (My-SGML-keymap)
	     (line-number-mode 1)
	     (column-number-mode 1)
	     (setq write-file-hooks (delete 'fix-iso2sgml write-file-hooks)
		   find-file-hooks (delete 'fix-iso2sgml find-file-hooks)
		   sgml-set-face t
		   sgml-insert-missing-element-comment nil
;		   compile-command 
;		   (concat "tei2dvi " (file-name-nondirectory (buffer-file-name)))
		   sgml-live-element-indicator t)
		   
	     (let ((ced-file-name 
		    (concat (file-name-sans-extension (buffer-file-name)) ".ced"))
		   )
	       (if (file-readable-p ced-file-name)
		   (sgml-load-dtd ced-file-name)))))

;;
;; Faces.
;;
(make-face 'sgml-comment-face)
(make-face 'sgml-doctype-face)
(make-face 'sgml-end-tag-face)
(make-face 'sgml-entity-face)
(make-face 'sgml-ignored-face)
(make-face 'sgml-ms-end-face)
(make-face 'sgml-ms-start-face)
(make-face 'sgml-pi-face)
(make-face 'sgml-sgml-face)
(make-face 'sgml-short-ref-face)
(make-face 'sgml-start-tag-face)

(set-face-foreground 'sgml-comment-face "dark green")
(set-face-foreground 'sgml-doctype-face "maroon")
(set-face-foreground 'sgml-end-tag-face "blue2")
(set-face-foreground 'sgml-entity-face "red2")
(set-face-foreground 'sgml-ignored-face "maroon")
(set-face-background 'sgml-ignored-face "gray90")
(set-face-foreground 'sgml-ms-end-face "maroon")
(set-face-foreground 'sgml-ms-start-face "maroon")
(set-face-foreground 'sgml-pi-face "maroon")
(set-face-foreground 'sgml-sgml-face "maroon")
(set-face-foreground 'sgml-short-ref-face "goldenrod")
(set-face-foreground 'sgml-start-tag-face "blue2")

(setq-default sgml-markup-faces
              '((comment . sgml-comment-face)
                (doctype . sgml-doctype-face)
                (end-tag . sgml-end-tag-face)
                (entity . sgml-entity-face)
                (ignored . sgml-ignored-face)
                (ms-end . sgml-ms-end-face)
                (ms-start . sgml-ms-start-face)
                (pi . sgml-pi-face)
                (sgml . sgml-sgml-face)
                (short-ref . sgml-short-ref-face)
                (start-tag . sgml-start-tag-face)))

;;
;; TeX
;;
(require 'tex-site)

(add-hook 'TeX-mode-hook
	  '(lambda ()
	     (local-set-key "\\" 'TeX-electric-macro)
	     )
	  )

(setq TeX-open-quote "<<"
      TeX-close-quote ">>"
      )

;;
;; Calc
;;
;(autoload 'calc-dispatch          "calc" "Calculator Options" t)
;(autoload 'full-calc              "calc" "Full-screen Calculator" t)
;(autoload 'full-calc-keypad       "calc" "Full-screen X Calculator" t)
;(autoload 'calc-eval              "calc" "Use Calculator from Lisp")
;(autoload 'defmath                "calc" nil t t)
;(autoload 'calc                   "calc" "Calculator Mode" t)
;(autoload 'quick-calc             "calc" "Quick Calculator" t)
;(autoload 'calc-keypad            "calc" "X windows Calculator" t)
;(autoload 'calc-embedded          "calc" "Use Calc from any buffer" t)
;(autoload 'calc-embedded-activate "calc" "Activate =>'s in buffer" t)
;(autoload 'calc-grab-region       "calc" "Grab region of Calc data" t)
;(autoload 'calc-grab-rectangle    "calc" "Grab rectangle of data" t)

;;
;; Scheme
;;
(add-hook 'scheme-mode-hook
	  '(lambda ()
	     (local-set-key "\C-c\C-c" 'comment-region)))

(autoload 'run-scheme "cmuscheme" "Run an inferior Scheme" t)
(setq scheme-program-name "mzscheme")
(add-hook 'inferior-scheme-mode-hook
	  '(lambda ()
	     (split-window)))



;; 
;; Allegro CL
;
(setq load-path
      (cons "/usr/local/lib/acl4.3/home/emacs/fi"
            load-path))
;(load "fi-site-init")

;;
;; W3
;; 
(setq url-be-asynchronous t)

;;
;; Misc
;;

(defun DefineKeyPadKeys ()
  (global-set-key 'kp-1 "1")
  (global-set-key 'kp-2 "2")
  (global-set-key 'kp-3 "3")
  (global-set-key 'kp-4 "4")
  (global-set-key 'kp-5 "5")
  (global-set-key 'kp-6 "6")
  (global-set-key 'kp-7 "7")
  (global-set-key 'kp-8 "8")
  (global-set-key 'kp-9 "9")
  (global-set-key 'kp-0 "0")
  (global-set-key 'kp-decimal ".")
  (global-set-key 'kp-enter "\C-m")
  (global-set-key 'kp-add "+")
  (global-set-key 'kp-subtract "-")
  (global-set-key 'kp-multiply "*")
  (global-set-key 'kp-divide "/")
  )

(if (device-on-window-system-p)
    (progn
      (require 'strokes)
;      (load-library "delbackspace")
      (add-hook 'calc-mode-hook '(lambda () (local-set-key 'kp-5 'calc-five)))
      (add-hook 'gnus-article-display-hook 'smiley-buffer)
      (add-hook 'gnus-summary-mode-hook 'smiley-buffer)
      (set-frame-width (selected-frame) 88)
      (set-frame-height (selected-frame) 35)
;      (set-frame-position (selected-frame) 10 30)
      (setq strokes-use-strokes-buffer nil
	    mouse-yank-at-point t)
      (require 'big-menubar)
      (load "recent-files")
      (recent-files-initialize)
      (load-library "time")
      (setq display-time-24hr-format t
	    display-time-mail-file t
	    display-time-form-list (list 'time 'load)
	    display-time-day-and-date t)
      (display-time)
      (blink-cursor-mode t)))

(require 'crypt)	     
(require 'filladapt)
(add-hook 'text-mode-hook 'turn-on-filladapt-mode)

;; Options Menu Settings
;; =====================
(cond
 ((and (string-match "XEmacs" emacs-version)
       (boundp 'emacs-major-version)
       (or (and
            (= emacs-major-version 19)
            (>= emacs-minor-version 14))
           (= emacs-major-version 20))
       (fboundp 'load-options-file))
  (load-options-file "/home/boris/.xemacs-options")))
;; ============================
;; End of Options Menu Settings

;;
;; I HATE it
;;
(add-spec-list-to-specifier 
 (face-property 'default 'font) 
 '((global (nil . "-adobe-courier-medium-r-*-*-*-180-*-*-*-*-koi8-*"))))

(add-hook 'shell-mode-hook 'install-shell-fonts)


;; bugfix for 20.3b94
(or (boundp 'custom-put)
    (defun custom-put (symbol ignored list)
      (custom-add-loads symbol list)))

(fset 'yes-or-no-p 'y-or-n-p)

(random t)

(require 'koi8-syntax)

;;
;; Ispell
;;
(setq ispell-dictionary-alist
      (append ispell-dictionary-alist
              (list
               (list
                "russian"
                "[áâ÷çäåöúéêëìíîïðòóôõæèãþûýÿùøüàñÁÂ×ÇÄÅÖÚÉÊËÌÍÎÏÐÒÓÔÕÆÈÃÞÛÝßÙØÜÀÑ]"
                "[^áâ÷çäåöúéêëìíîïðòóôõæèãþûýÿùøüàñÁÂ×ÇÄÅÖÚÉÊËÌÍÎÏÐÒÓÔÕÆÈÃÞÛÝßÙØÜÀÑ]"
                "[\0]"
                nil
                nil
                nil)))
      ispell-dictionary "russian")


;(require 'desktop)
;(desktop-load-default)
;(desktop-read)