.emacs-my
author Oleksandr Gavenko <gavenkoa@gmail.com>
Fri, 19 Dec 2014 01:07:36 +0200
changeset 1178 2eeb9a3996b4
parent 1177 4a7ffe52d06c
child 1179 b2e1cb526649
permissions -rw-r--r--
subword-mode for java-mode

;; -*- mode: emacs-lisp; coding: utf-8; fill-column: 78 -*-
;;
;; Written by Oleksandr Gavenko <gavenkoa@gmail.com>, 2008-2010.
;;
;; This file formed from parts and ideas from many sites/docs and
;; placed in public domain.
;;
;; Config file for GNU Emacs.
;;
;; For load order see README.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "recentf")

(setq recentf-save-file "~/.recentf")
(when (eq system-type 'cygwin)
  (setq recentf-save-file "~/.recentf-cygwin"))

(setq recentf-auto-cleanup 'mode)
;; Prevent TRAMP to login on remote host when loading. Its take time and ask passwords!
(setq recentf-keep '(file-remote-p file-readable-p))
(setq recentf-exclude                   ; Use ``M-x recentf-cleanup`` to update recentf-list.
      '("/devel/[^/]*-\\(orig\\|tmp\\|xxx\\)"
        "/devel/my-\\(merge\\|pull\\)/"))
(setq recentf-max-saved-items 10000)

(require 'recentf)
(recentf-mode t)

(global-set-key (kbd "\e\eq")  'recentf-open-files)

(setq recentf-max-menu-items 40)
(setq recentf-show-file-shortcuts-flag nil)

;; (setq recentf-menu-filter 'recentf-arrange-by-dir) ; Too slow with dir widgets.
;; Don't sort ``recentf-list`` so ``recentf-open-files`` show files in historical order!
(setq recentf-menu-filter nil)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "revert buffer")

(global-set-key [f5]    'revert-buffer)
(setq revert-without-query (quote (".*")))
(setq auto-revert-interval 2)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "debugging")

;; Shut off message buffer by setting nil.
(setq message-log-max 512)

;; Prevent Emacs from loading 'default.el', which loaded after '.emacs'.
;; Also '-q' prevent loading your init file.
(setq inhibit-default-init nil)         ; t/nil

(defun my-debug (mode)
  "With prefix enable debug backtrace when problems occur else disable."
  (interactive "P")
  (when mode
    ;; (setq debug-on-signal t)
    (setq mode t))
  (setq debug-on-error mode)
  ;; Get trace when press C-g.
  (setq debug-on-quit mode)
  ;; (setq debug-on-signal mode)
  )

(my-debug nil)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "user info")

;; Set following in ~/.emacs-auth:
;; (setq user-full-name "Oleksandr Gavenko")
;; (setq user-mail-address "gavenkoa@gmail.com")
;; (setq user-nick "gavenkoa")
;; (setq user-home-page "http://gavenkoa.users.sf.net")

(unless (and (boundp 'user-nick) (stringp user-nick))
  (setq user-nick (user-login-name)) )
;; auto-insert and copyright package use this to insert copyright owner. Gnus
;; uses this for Organization header.
(setenv "ORGANIZATION"
        (concat
         user-full-name
         " <" user-mail-address  ">"
         (when (boundp 'user-home-page) (concat ", " user-home-page))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "my defun, defmacro, defvar")

(require 'cl)

(defmacro my-filter (pred list)
  "Construct list with elements from LIST which satisfy PRED."
  (let ( (r (make-symbol "r_")) )
    `(let ( (,r (list nil)) )
       (mapc (lambda (item)
               (when (,pred item)
                 (nconc ,r (cons item nil))))
             ,list)
       (cdr ,r))))

(defun my-fold (f x list)
  "Recursively applies (F i j) to LIST starting with X.
For example, (fold F X '(1 2 3)) computes (F (F (F X 1) 2) 3)."
  (let ((li list) (x2 x))
    (while li
      (setq x2 (funcall f x2 (pop li)))
      )
    x2
    ) )

(unless (fboundp 'ignore-errors)
  (defmacro ignore-errors (&rest body)
    "Execute BODY; if an error occurs, return nil.
Otherwise, return result of last form in BODY."
    (declare (debug t) (indent 0))
    `(condition-case nil (progn ,@body) (error nil)))
  )

(defun my-lookup-key (key)
  "Search for KEY in all known keymaps."
  (mapatoms (lambda (ob) (when (and (boundp ob) (keymapp (symbol-value ob)))
                      (when (lookup-key (symbol-value ob) key)
                        (message "%S" ob))))
            obarray))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "mode groups")

(defmacro my-defun-rename-symb-tree (name doc func)
  "Travel by TREE and applies FUNC to each symbol."
  `(defun ,name (tree)
     ,doc
     (cond
      ((symbolp tree)
       (,func tree)
       )
      ((listp tree)
       (mapcar ',name tree)
       )
      (t (error "Only tree of symbols allowed."))
      )))

(my-defun-rename-symb-tree
 my-feature2mode
 "Convert TREE of features to TREE of modes for these features. Single symbol allowed."
 (lambda (symb) (intern (concat (symbol-name symb) "-mode"))))

(my-defun-rename-symb-tree
 my-mode2hook
 "Convert TREE of modes to TREE of hooks for these modes. Single symbol allowed."
 (lambda (symb) (intern (concat (symbol-name symb) "-hook")))
 )

(my-defun-rename-symb-tree
 my-mode2modemap
 "Convert TREE of modes to TREE of keymaps for these modes. Single symbol allowed."
 (lambda (symb) (intern (concat (symbol-name symb) "-map")))
 )

(defvar my-devel-mode-list
  '(
    sh-mode script-mode tcl-mode
    c-mode c++-mode java-mode js-mode
    python-mode perl-mode cperl-mode
    lisp-mode
    sql-mode
    emacs-lisp-mode
    makefile-mode makefile-gmake-mode
    nsis-mode
    bat-generic-mode
    html-mode nxml-mode
    LilyPond-mode
    texinfo-mode
    )
  "List of development modes.")

(defvar my-devel-mode-hook-list
   (my-mode2hook my-devel-mode-list)
  "List of development mode hooks.")

(defvar my-scroll-margin-mode-list
  '(
    vc-dir-mode
    recentf-dialog-mode
    org-agenda-grid-mode           ; XXX for this item not worked!
    log-view-mode
    dired-mode
    compilation-mode
    )
  "List of modes for enabling scroll margin.")

(defvar my-scroll-margin-mode-hook-list
  (my-mode2hook my-scroll-margin-mode-list)
  "List of mode hooks for enabling scroll margin.")

(defvar my-text-mode-list
  '(
    text-mode
    outline-mode
    rst-mode
    diff-mode
    dict-c5-mode
    )
  "List of text modes.")

(defvar my-text-mode-hook-list
  (my-mode2hook my-text-mode-list)
  "List of text mode hooks.")

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "package, elpa")

;; (setq package-enable-at-startup t)
(when (fboundp 'package-initialize)
  (package-initialize))

(setq package-archives
      '(
        ("elpa" . "http://tromey.com/elpa/")
        ("gnu" . "http://elpa.gnu.org/packages/")
        ("marmalade" . "http://marmalade-repo.org/packages/")
        ("melpa" . "http://melpa.milkbox.net/packages/")
        ))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "appearance")

;; To maximize frame on full screen, not work with Windows Emacs 21.4.
;; And maked different height with Emacs 22.3 and 23.1.
;; (setq initial-frame-alist '((fullscreen . fullboth)))

(setq display-buffer-reuse-frames t)

;; Next code work with Emacs 21.4, 22.3, 23.1.
(when window-system
  (let (
        (px (display-pixel-width))
        (py (display-pixel-height))
        (fx (frame-char-width))
        (fy (frame-char-height))
        tx ty
        )
    ;; Next formulas discovered empiric on Windows host with default font.
    (setq tx (- (/ px fx) 7))
    (setq ty (- (/ py fy) 4))
    (setq initial-frame-alist '((top . 2) (left . 2)))
    (add-to-list 'initial-frame-alist (cons 'width tx))
    (add-to-list 'initial-frame-alist (cons 'height ty))
    ) )

(menu-bar-mode -1)
(when window-system
  (mouse-avoidance-mode 'animate)
  (scroll-bar-mode 1)
  (tool-bar-mode -1)
  (setq-default line-spacing nil)
  (defun my-popup-menu ()
    "Menu from keyboard by emulating mouse event."
    (interactive)
    (mouse-popup-menubar
     (list (list (/ (display-pixel-width) 2) 10) (get-buffer-window (buffer-name)))
     nil)
    )
  (global-set-key [f10] 'my-popup-menu)
  (global-set-key [apps] 'my-popup-menu)
  (global-set-key [menu] 'my-popup-menu)
  (when (>= emacs-major-version 22) (tooltip-mode -1))
  )

;; Prefer horizontal windows splitting.
(when (>= emacs-major-version 23)
  (setq split-height-threshold nil)
  (setq split-width-threshold nil)
  )

(setq frame-title-format '("EMACS " system-name ": %b"))

;; Deprecated: `default-header-line-format', `default-mode-line-format'.
;; For `mode-line-format' default value was used.
(setq-default header-line-format nil)

(setq default-left-fringe-width nil)
(setq default-right-fringe-width nil)
(setq default-left-margin-width nil)
(setq default-right-margin-width nil)

(if (< emacs-major-version 24)
    (setq default-truncate-lines nil)
  (setq truncate-lines nil)
  )
(setq truncate-partial-width-windows nil)

;; show column & line numbers in status bar
(setq column-number-mode t)
(setq line-number-mode t)
(setq size-indication-mode t)
;; (linum-mode 1)

;; Also useful such format: (setq display-time-format " %H:%M %d-%m-%y ")
(setq display-time-24hr-format t)
(setq display-time-day-and-date nil)
(setq display-time-default-load-average nil)
(display-time)                          ; display-time-mode

(when window-system
  (set-background-color "white")
  (set-foreground-color "black")
  (set-cursor-color "brown")
  ;; (set-mouse-color "white")
  (setq cursor-type 'box)           ; box, hollow, bar, hbar
  ;;(setq blink-matching-delay 0.01)
  (blink-cursor-mode 1)
  (setq use-file-dialog t)
  (setq use-dialog-box t)
  ;; (set-face-font 'default "7x14")
  )

;; See what I am typing immediately (for keystroke in minibuffer).
(setq echo-keystrokes 0.2)

(when (eq window-system 'x)
  (set-frame-font "-misc-fixed-medium-r-normal--14-*-*-*-c-*-iso10646-1")
  (set-fontset-font "fontset-default" 'latin
                    "-misc-fixed-medium-r-normal--14-*-*-*-c-*-iso10646-1")
  (set-fontset-font "fontset-default" 'phonetic
                    "-misc-fixed-medium-r-normal--14-*-*-*-c-*-iso10646-1")
  (set-fontset-font "fontset-default" 'cyrillic
                    "-misc-fixed-medium-r-normal--14-*-*-*-c-*-iso10646-1")
  (set-fontset-font "fontset-default" 'greek
                    "-misc-fixed-medium-r-normal--14-*-*-*-c-*-iso10646-1"))

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

(when (boundp 'confirm-kill-emacs)
  (setq confirm-kill-emacs 'y-or-n-p))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "silent runing")

;; (setq inhibit-splash-screen t) ; nonexist on 21.4.1
(setq inhibit-startup-message t)

(setq initial-scratch-message nil)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "shell, bash, Cygwin, MSYS")

(defvar my-use-windows-shell nil
  "If t 'cmdproxy.exe' will be used as shell. Affect on M-x shell like
  commands. If nil, 'sh' will be used." )

(defun follow-cygwin-symlink ()
  "Follow new-style (and also UCS-16) Cygwin symlinks."
  (save-excursion
    (goto-char 0)
    (when (looking-at "!<symlink>\xff\xfe")
      (find-alternate-file
       (substring
        (decode-coding-string
         (buffer-substring (match-end 0) (point-max))
         'utf-16-le)
        0 -1)                           ; -1 for stripping final \0.
       ))))

(defun my-dos2cygwin-path (path)
  "Convert DOS path to Cygwin according to current mount table."
  (interactive (list (read-directory-name "Enter DOS path: ")))
  (setq path (replace-regexp-in-string "\\\\" "/" (expand-file-name path)))
  (let ( (table cygwin-mount-table--internal) item prefix )
    (while table
      (setq item (car table))
      (setq prefix (concat "\\`" (regexp-quote (car item))))
      (setq table (cdr table))
      (when (string-match prefix path)
        (setq path (replace-regexp-in-string prefix (cdr item) path))
        (setq table nil)
        ) )
    path
    ))

(when (eq system-type 'windows-nt)
  (ignore-errors
    (require 'cygwin-mount)
    (cygwin-mount-activate)
    )
  (add-hook 'find-file-hook 'follow-cygwin-symlink)
  ;; Workaround for Cygwin shell, when set 'CYGWIN=noglob'. By default 'shell-quote-argument'
  ;; quoted by double '\' chars this cause failure.
  (defun shell-quote-argument (argument)
    (concat "'" argument "'")
    )
  ;; Workaround for Cygwin when 'shell-file-name' is 'bash'.
  (setq null-device "/dev/null")
  ;; Use shell from Cygwin/MinGW.
  (setq shell-file-name "bash")
  (setenv "SHELL" "/bin/bash")
  (modify-coding-system-alist 'process "bash" '(cp1251-unix . cp1251-unix))
  )

(when (eq window-system 'w32)
  ;; Fix 'starttls.el' on native Windows Emacs with gnutls-cli from Cygwin.
  ;; 'gnutls-cli' run with '-s' opt and process wait for SIGALRM.
  ;; But build-in native Emacs 'kill' command can not send such Cygwin
  ;; specific sygnal. So 'starttls-negotiate-gnutls' function infinitely
  ;; wait for 'gnutls-cli' output.
  (defadvice signal-process (around cygwin (process sigcode))
    "Use 'kill.exe' instead build-in Emacs 'kill'."
    (if (eq sigcode 'SIGALRM)
        (shell-command
         (format "kill.exe -s SIGALRM %d"
                 (if (processp process) (process-id process) process)))
      ad-do-it
      ))
  (ad-activate 'signal-process)
  (modify-coding-system-alist 'process "gnutls-cli" '(binary . binary))
  )

(when (eq window-system 'w32)
  (add-to-list 'exec-suffixes ".py")
  (add-to-list 'exec-suffixes ".sh")
  (defun executable-find (command) (locate-file command exec-path exec-suffixes))
  )

(ansi-color-for-comint-mode-on)

(setq explicit-bash-args '("-i"))
(setq explicit-sh-args '("-i"))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "term")

(setq term-buffer-maximum-size (lsh 1 14))

(eval-after-load 'term
  '(progn
    (defun my-term-send-delete-word-forward () (interactive) (term-send-raw-string "\ed"))
    (defun my-term-send-delete-word-backward () (interactive) (term-send-raw-string "\e\C-h"))
    (define-key term-raw-map [C-delete] 'my-term-send-delete-word-forward)
    (define-key term-raw-map [C-backspace] 'my-term-send-delete-word-backward)
    (defun my-term-send-forward-word () (interactive) (term-send-raw-string "\ef"))
    (defun my-term-send-backward-word () (interactive) (term-send-raw-string "\eb"))
    (define-key term-raw-map [C-left] 'my-term-send-backward-word)
    (define-key term-raw-map [C-right] 'my-term-send-forward-word)
    (defun my-term-send-m-right () (interactive) (term-send-raw-string "\e[1;3C"))
    (defun my-term-send-m-left () (interactive) (term-send-raw-string "\e[1;3D"))
    (define-key term-raw-map [M-right] 'my-term-send-m-right)
    (define-key term-raw-map [M-left] 'my-term-send-m-left)
    ))

(defun my-term-mode-hook ()
  (goto-address-mode 1))
(add-hook 'term-mode-hook #'my-term-mode-hook)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "proced")

(setq-default proced-format 'medium)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "whitespaces")

(setq default-indicate-empty-lines t)
(setq default-indicate-buffer-boundaries 'left)

;; (setq-default show-trailing-whitespace t)

(setq whitespace-style '(face trailing tabs))
(setq whitespace-global-modes (append my-devel-mode-list my-text-mode-list))
(ignore-errors
  (require 'whitespace)
  (global-whitespace-mode 1))

(setq next-line-add-newlines nil)

;; See also 'mode-require-final-newline'.
(add-hook 'text-mode-hook (lambda () (setq require-final-newline nil)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "server")

(when (and (>= emacs-major-version 22) (not (eq system-type 'cygwin)))
  (require 'server)
  (when (and (= emacs-major-version 23) (>= emacs-minor-version 1) (<= emacs-minor-version 2) (equal window-system 'w32))
    (defun server-ensure-safe-dir (dir) "Noop" t)) ; Suppress error directory ~/.emacs.d/server is unsafe on windows.
  (when (or (= emacs-major-version 22) (not (eq (server-running-p server-name) t)))
    (server-start))
  )

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "standart/general settings")

;; Try to speed things up, especially in VM.
(setq gc-cons-threshold 2000000)

;; Default 'command-history' length too short (in Emacs 23.2 is 30).
(setq history-length 200)

;; Don't beep in my headphones!
(setq ring-bell-function '(lambda () "Empty ring-bell-function." nil))
(setq visible-bell t) ; With default ring-bell-function in text terminal
                      ; revert screen if press [end] or [home]
(setq track-eol nil)

;; (setq enable-recursive-minibuffers t)

;; Prompt before evaluating local bits of lisp.  This stops people
;; putting things at the end of files which delete all your files!
(setq enable-local-variables t
      enable-local-eval      1)

(setq kill-whole-line t)

;; Disable because of bug with gettext.el. See
;;   http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=287261
;; (setq view-read-only t)

(setq read-quoted-char-radix 16)

(when (and (>= emacs-major-version 22) (not (eq system-type 'darwin)))
  (setq standard-display-table (make-display-table))
  (let ( (i ?\x80) hex hi low )
    (while (<= i ?\xff)
      (setq hex (format "%x" i))
      (setq hi (elt hex 0))
      (setq low (elt hex 1))
      (aset standard-display-table (unibyte-char-to-multibyte i)
            (vector (make-glyph-code ?\\ 'escape-glyph)
                    (make-glyph-code ?x 'escape-glyph)
                    (make-glyph-code hi 'escape-glyph)
                    (make-glyph-code low 'escape-glyph)))
      (setq i (+ i 1))))
  )

(when (>= emacs-major-version 23)
  (define-key global-map "\C-x8g" (lambda nil (interactive) (ucs-insert `,(cdr (assoc-string "HRYVNIA SIGN" (ucs-names) t)))))
  (define-key global-map "\C-x8e" (lambda nil (interactive) (ucs-insert `,(cdr (assoc-string "EURO SIGN" (ucs-names) t)))))
  )

;; generic-define-* before (require 'generic-x) allow load all useful extra modes.
(setq generic-define-mswindows-modes t)
(setq generic-define-unix-modes t)
(require 'generic-x)

;; The following commands are usually disabled by default. Enable them...
(put 'eval-expression  'disabled nil)
(put 'downcase-region  'disabled nil)
(put 'upcase-region    'disabled nil)
(put 'narrow-to-page   'disabled nil)
(put 'narrow-to-region 'disabled nil)
(put 'scroll-left      'disabled nil)

(setq
 use-dialog-box t
 x-gtk-show-hidden-files t
 )

(defun my-prevent-kill-buffer ()
  (if (member (buffer-name) '("*scratch*" "NOTE.org")) nil t))
(add-to-list 'kill-buffer-query-functions 'my-prevent-kill-buffer)

(define-key global-map "\C-v" nil)
(define-key global-map "\C-vt" (lambda nil (interactive) (switch-to-buffer "*scratch*")))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "scrolling")

(defvar my-scroll-margin 4)

(setq-default
 ;; Set to zero as this recomment documentation.
 scroll-step 0
 ;; If the value is greater than 100, redisplay will never recenter point, but
 ;; will always scroll just enough text to bring point into view
 scroll-conservatively 1000
 scroll-preserve-screen-position t
 )

;; Set margin only for desired modes! Do not frustrate calendar any more.
(make-variable-buffer-local 'scroll-margin)
(mapc (lambda (hook) (add-hook hook (lambda nil (setq scroll-margin my-scroll-margin))))
      ;; TODO its good invoke delete-dups for list, but delete-dups not exist in Emacs 21.4
      (append my-text-mode-hook-list my-devel-mode-hook-list my-scroll-margin-mode-hook-list) )

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "chars, unicode")

(defun my-print-unicode (&optional start end)
  "Print UNICODE table."
  (interactive "nstart: \nnend: ")
  (switch-to-buffer (get-buffer-create "*UNICODE*"))
  (erase-buffer)
  (let ( (i start) )
    (while (<= i end)
      (insert (format "%s: U+%04x, %s\n" (char-to-string i) i (get-char-code-property i 'name)))
      (setq i (1+ i))
      )))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "search, isearch, occur")

(setq-default case-fold-search t)

(setq query-replace-highlight t)        ; highlight during query
(setq search-highlight t)               ; highlight incremental search

;; Make old Emacs key binding like in Emacs 23.x.
(when (< emacs-major-version 23)
  (global-set-key (kbd "M-s o") 'occur)
  )

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "grep, find")

;; -ls produce very noisy output:
;; (setq find-ls-option '("-ls" . ""))
;; So I use next expression, which work with GNU find, I replace %s with '0'
;; to avoid unnecessary sys calls and this make output aligned by column:
(setq find-ls-option '("-printf ' -rw-rw-rw- 0 %AY-%Am-%Ad %AH:%AM %p\n'" . ""))

;; Do not set t because some grep do not has --color options.
(setq grep-highlight-matches nil)
(setq grep-use-null-device nil)

(eval-after-load 'grep
  '(progn
     (add-to-list 'grep-find-ignored-directories "build" t)
     (add-to-list 'grep-find-ignored-directories "dist" t)
     (add-to-list 'grep-find-ignored-directories "lib" t)
     (add-to-list 'grep-find-ignored-directories "_build" t)
     (add-to-list 'grep-find-ignored-directories "_dist" t)
     (add-to-list 'grep-find-ignored-directories "_lib" t)

     (when (boundp 'grep-find-ignored-files)
       (add-to-list 'grep-find-ignored-files "*TAGS")
       (add-to-list 'grep-find-ignored-files "GPATH")
       )
     ))
(global-set-key [f7] 'rgrep)
(global-set-key [M-f7] 'rgrep)

(defun lzgrep ()
  (interactive)
  (let ( (grep-template "zgrep <C> -nH -e <R> <F>") (grep-find-ignored-files nil) )
    (flet ( (grep-compute-defaults () nil) )
      (call-interactively #'lgrep))))

(defun rzgrep ()
  (interactive)
  (let ( (grep-find-template "find . -type f <F> -exec zgrep <C> -nH -e <R> {} +") (grep-find-ignored-files nil) )
    (flet ( (grep-compute-defaults () nil) )
      (call-interactively #'rgrep))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "highlighting")

;; Increase scrolling speed by defer consuming operation.
;; I disable this due to bugs in Emacs (it doesn't highlight at all until you
;; press any key).
;; (setq jit-lock-defer-time 0.01)

(setq font-lock-maximum-decoration t)
(global-font-lock-mode 1)

(global-hi-lock-mode 1)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "highlight selected text")

(delete-selection-mode 1)

(when (<= emacs-major-version 23)
  ;; 1/-1, when the mark is active, the region is highlighted.
  (transient-mark-mode 1)

  ;; Order of next items is important, (assignment must done before pc-selection-mode enabled).
  (require 'pc-select)
  (setq pc-select-selection-keys-only t)  ; To avoid some key bindings as F6, etc.
  (setq pc-select-meta-moves-sexps t)
  (cond
   ((= emacs-major-version 21) (pc-selection-mode))
   ((>= emacs-major-version 22) (pc-selection-mode 1))
   )
  )

(when window-system
  (set-face-attribute 'region nil :background "light blue"))

(when (eq window-system 'x)
  (setq x-select-enable-clipboard t)    ; for Emacs 21.2.1 and newer
  )

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "highlighting current line")

(when window-system
  (custom-set-faces '(hl-line ((t (:inherit highlight :background "light yellow")))))
  (global-hl-line-mode 1)
  )

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "paren, braces")

(show-paren-mode 1) ; Parenthesis matching via highlighting.
(setq show-paren-style (quote parenthesis))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "keyboard, mouse")

(setq mouse-yank-at-point t)

;; (pc-bindings-mode) ; I define own keybinding...

;; cyrillic-jis-russian  for 567 is :,.
;; cyrillic-jcuken  for SHIFT 567 is :,.
;; russian-computer for SHIFT 567 is %^&
(setq my-russian-input-method 'russian-computer)
(setq my-ukranian-input-method 'ukrainian-computer)
(setq my-ipa-input-method 'ipa-x-sampa)
(when (<= emacs-major-version 21)
  (setq my-russian-input-method 'cyrillic-jcuken)
  (setq my-ukranian-input-method 'cyrillic-jcuken))
(setq default-input-method my-russian-input-method)

(defun my-toggle-input-method (&optional arg)
  (interactive "P")
  (if (numberp arg)
      (cond
       ((eq arg 1)
        (activate-input-method nil))
       ((eq arg 2)
        (activate-input-method my-russian-input-method))
       ((eq arg 3)
        (activate-input-method my-ukranian-input-method))
       ((eq arg 4)
        (activate-input-method my-ipa-input-method)) )
    (toggle-input-method arg)) )

(global-set-key (kbd "C-\\") 'my-toggle-input-method)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "mouse")

;; Scroll Bar gets dragged by mouse butn 1
(global-set-key [vertical-scroll-bar down-mouse-1] 'scroll-bar-drag)
;; Paste at point NOT at cursor
(setq mouse-yank-at-point t)
(when window-system
  (mouse-wheel-mode 1)
  )

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "key binding, short-keys")

(defun my-smart-beginning-of-line ()
  "Move point to beginning-of-line. If repeat command it cycle
position between `back-to-indentation' and `beginning-of-line'."
  (interactive "^")
  (if (and (eq last-command 'my-smart-beginning-of-line)
           (= (line-beginning-position) (point)))
      (back-to-indentation)
    (beginning-of-line)))

(global-set-key [home]     'my-smart-beginning-of-line)
(global-set-key [end]      'end-of-line)
(global-set-key [C-home]   'beginning-of-buffer)
(global-set-key [C-end]    'end-of-buffer)
(global-set-key [C-delete] 'kill-word)
(global-set-key [delete]   'delete-char)
;; (global-set-key [backspace]  'backward-delete-char-untabify) ; not work properly in *info* mode

(global-set-key [f2]    'save-buffer)
(global-set-key [S-f6]  'rename-buffer)
(global-set-key [M-f4]  'save-buffers-kill-emacs)
(global-set-key [f6]    'toggle-truncate-lines)
(global-set-key [s-tab] 'other-window)

;; Disable suspend. It is ugly.
(when window-system
  (global-set-key (kbd "C-z") nil))
(global-set-key (kbd "C-x C-z") nil)

;; (global-set-key [language-change] 'ignore)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "coding system, charset, locale, lang")

;; Emacs 23.1 no longer need codepage-setup.
(when (<= emacs-major-version 22)
  (codepage-setup 866)
  (codepage-setup 1251)
  )

;; Comment because prefer-coding-system will be ignored.
;; (setq-default coding-system-for-read  'cp1251-dos)
;; (setq-default coding-system-for-write 'cp1251-dos)

;; (setq locale-coding-system  'cp1251-dos)
;; (set-language-environment 'UTF-8)
;; (set-terminal-coding-system 'cp1251)
;; (set-keyboard-coding-system 'cp1251)

(modify-coding-system-alist 'file "\\.el" 'iso-2022-7bit)
(cond
 ((equal window-system 'w32)          ; also (string-equal system-type "windows-nt")
  ;; (set-selection-coding-system 'utf-16-le-dos)
  (setq-default buffer-file-coding-system 'cp1251)
  (setq default-file-name-coding-system 'cp1251)
  (setq default-process-coding-system '(cp1251 . cp1251))
  )
 ((equal window-system 'x)
  (prefer-coding-system 'utf-8-unix)
  (setq selection-coding-system nil)
  (setq x-select-request-type '(UTF8_STRING COMPOUND_TEXT TEXT STRING))
  (modify-coding-system-alist 'process ".*" 'utf-8-unix)
  )
 ((eq system-type 'cygwin)
  (when (string-match "1251\\'" (getenv "LANG"))
    (prefer-coding-system 'cp1251-unix)
    (prefer-coding-system 'utf-8-unix)
    (modify-coding-system-alist 'process ".*" 'cp1251-unix)
    )
  )
 ((eq system-type 'darwin)
  nil
  )
 )

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "selecting/switching, creating/killing buffers, open file")
(message "ido, ffap")

;; Loaded and used by ido.
;; (require 'ffap)

(require 'ido)

(setq ido-enable-flex-matching t)

(setq ido-use-filename-at-point 'guess)
(setq ido-use-url-at-point t)
(setq ido-enable-tramp-completion t)
(setq ido-use-virtual-buffers t)

(setq ido-create-new-buffer 'always)

(setq ido-file-extensions-order '(".java" ".c" ".py" ".xml" ".txt" ".el" ".ini" ".cfg" ".cnf" ".log"))

(setq ido-ignore-buffers
      '("\\` "))
;; ido-ignore-directories
;; ido-ignore-files

(ido-mode 1)

(global-set-key [?\C-x right] 'next-buffer)
(global-set-key [?\C-x left]  'previous-buffer)
(global-set-key (kbd "C-x C-k") 'kill-this-buffer)
(defun my-kill-this-buffer-maybe-switch-to-next ()
  "Kill current buffer. Switch to next buffer if previous command
was switching to next buffer or this command itself allowing
sequential closing of uninteresting buffers."
  (interactive)
  (let ( (cmd last-command) )
    (kill-this-buffer)
    (when (memq cmd (list 'next-buffer this-command))
      (next-buffer))))
(global-set-key [?\C-x deletechar] 'my-kill-this-buffer-maybe-switch-to-next)
(global-set-key [?\C-x up] '(lambda ()
                              (interactive)
                              (other-window -1)))
(global-set-key [?\C-x down] 'other-window)

(require 'uniquify)
(setq uniquify-buffer-name-style 'post-forward)
(setq uniquify-separator "|")
(setq uniquify-after-kill-buffer-p t)

(setq read-buffer-completion-ignore-case t)
(setq read-file-name-completion-ignore-case t)

;; buffer-menu better then buffer-list, but ibuffer much better.
(global-set-key "\C-x\C-b" 'ibuffer)

(icomplete-mode 1)
(setq icomplete-with-completion-tables t)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "completion")

;; I remove partial-completion-mode because it depricated in Emacs 24.0.
;; Completion controled by 'completion-styles' variable.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "yasnippet")

(setq my-yas-root-directory "~/.emacs.d/my-yas")
(setq yas/ignore-filenames-as-triggers t)

;; (eval-after-load 'yasnippet
;;   '(progn
;;      (cond
;;       ((listp yas/root-directory) (add-to-list 'yas/root-directory my-yas-root-directory))
;;       ((stringp yas/root-directory) (setq yas/root-directory (list my-yas-root-directory yas/root-directory)))
;;       (t (error "I expect that yas/root-directory is a list of string")) )
;;      (mapc 'yas/load-directory yas/root-directory)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "dired")

(setq-default save-place t)
(require 'saveplace)

(require 'dired)

(setq dired-dwim-target t)
;; dangerous
;; (setq
;;  dired-recursive-copies 'top
;;  dired-recursive-deletes 'top)

(defun my-dired-up-dir ()
  "'Reuse' buffer if enter to dir or open new buffer if enter to file."
  (interactive)
  ;; (dired-current-directory) always end with trailing '/' char.
  (let* ( (dir (dired-current-directory)) (i (- (length dir) 2)) upperdir )
    (while (and
            (>= i 0)
            (not (equal (aref dir i) ?/)) )
      (setq i (- i 1))
      )
    (setq upperdir (substring dir 0 (+ i 1)))
    (when (file-directory-p upperdir)
      (find-alternate-file upperdir)
      (dired-goto-file dir)
      )
    ))
(define-key dired-mode-map (kbd "<backspace>") 'my-dired-up-dir)

(defun my-dired-enter-to-dir ()
  "'Reuse' buffer if enter to dir or open new buffer if enter to file."
  (interactive)
  (let ( (file (dired-get-file-for-visit)) )
    (if (file-directory-p file)
        (find-alternate-file file)
      (find-file file)
      )))
(define-key dired-mode-map (kbd "<return>")
  'my-dired-enter-to-dir)

;; Make behaviour same as in GUI.
(unless window-system
  (define-key dired-mode-map (kbd "DEL") 'my-dired-up-dir)
  (define-key dired-mode-map (kbd "RET") 'my-dired-enter-to-dir)
  )

(when (>= emacs-major-version 22)
  ;; Enable 'a' command.
  (put 'dired-find-alternate-file 'disabled nil)
  )

(defun my-file-name-tmp-p (file)
  (string-match
   `,(concat
    "\\(?:^#.*#\\|~\\|"
    "\\." (regexp-opt '("base" "local" "orig" "other" "rej" "diff" "log" "stackdump" "pyc" "pyo"))
    "\\)\\'")
   (or (and (file-directory-p file) "") (file-name-nondirectory file))))

(defun my-dired-flag-tmp-files ()
  "Flag all temporary files for deletion."
  (interactive)
  (dired-mark-if
   (let ( (fn (dired-get-filename 'verbatim t)) )
     (and fn (my-file-name-tmp-p fn)) )
   "backup file"
   )
  )

(define-key dired-mode-map (kbd "`") 'my-dired-flag-tmp-files)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "ls-lisp, dired ls")

;; If non-nil - use 'insert-directory-program', which I dislike.
(setq ls-lisp-use-insert-directory-program nil)
(setq ls-lisp-ignore-case t)
(setq ls-lisp-dirs-first t)
(if (memq system-type '(windows-nt cygwin))
    (setq ls-lisp-verbosity nil)
  (setq  ls-lisp-verbosity '(links uid gid)))
;; Force use 'ls-lisp-format-time-list'.
(setq ls-lisp-use-localized-time-format t)
(setq ls-lisp-format-time-list
      '("%Y-%m-%d %H:%M:%S"
        "%Y-%m-%d %H:%M   "))
(require 'ls-lisp)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "compression, archive")

(require 'jka-compr) ; Automatic decompression, hooks for tar-mode.
(when (fboundp 'auto-compression-mode)
  (auto-compression-mode 1))

(modify-coding-system-alist 'file "\\.\\(war\\|ear\\|sar\\|egg\\)\\'" 'no-conversion)

(add-to-list 'auto-mode-alist '("\\.\\(war\\|ear\\|sar\\|egg\\)\\'" . archive-mode))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "image, png, svg")

(when (fboundp 'auto-image-file-mode)
  (auto-image-file-mode 1))
(eval-after-load 'image-file
  '(progn
     ;; Exclude .svg image from supported image list, as Emacs doesn't come
     ;; with SVG shared library.
     (setq image-file-name-extensions (remove "svg" image-file-name-extensions))
     ;; Re-initialize the image-file handler.
     (auto-image-file-mode t)
     ))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "url")

;; http://tools.ietf.org/html/rfc3986
;; http://en.wikipedia.org/wiki/Percent-encoding
(defun my-percent-decode (str)
  (decode-coding-string
   (let* ( (s (split-string str "%")) )
     (my-fold
      'concat
      (car s)
      (mapcar
       (lambda (x)
         (concat (unibyte-string (string-to-number (substring x 0 2) 16)) (substring x 2)))
       (cdr s))
      )) 'utf-8))

(defun my-percent-decode-region (beg end &optional arg)
  "Convert percent encoded string to native."
  (interactive "r\nP")
  (let ( (result (my-percent-decode (buffer-substring-no-properties beg end))) )
    (if (not arg)
        result
      (delete-region beg end)
      (insert result))
    ) )

(defun my-percent-encode (str)
  (apply 'concat
         (mapcar
          (lambda (ch) (if (or (and (<= ?a ch) (>= ?z ch))
                          (and (<= ?A ch) (>= ?Z ch))
                          (memq ch '(?- ?_ ?. ?~)))
                      (char-to-string ch)
                    (format "%%%02X" ch)))
          (encode-coding-string str 'utf-8) )))

(defun my-percent-encode-region (beg end &optional arg)
  "Encode string to percent encoding."
  (interactive "r\nP")
  (let ( (result (my-percent-encode (buffer-substring-no-properties beg end))) )
    (if (not arg)
        result
      (delete-region beg end)
      (insert result))
    ) )

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "browser")

(cond
 ((equal window-system 'w32)
  (setq browse-url-browser-function 'browse-url-default-windows-browser))
 ((boundp 'debian-emacs-flavor)
  (setq browse-url-browser-function 'browse-url-firefox))
 (t
  (setq browse-url-browser-function 'browse-url-mozilla)))

(defun my-cygwin-search (str)
  "Search for Cygwin package on-line."
  (interactive (list (read-input "Search for Cygwin package on-line: ")))
  (browse-url (format "http://cygwin.com/cgi-bin2/package-grep.cgi?grep=%s" str))
  )

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "netbeans")

(defun netbeans-find-command ()
  "Search for NetBeans executable in PATH, later in '/opt/netbeans*/bin/'."
  (or
   (executable-find "netbeans")
   (car (last (sort (file-expand-wildcards "/opt/[Nn]etbeans*/bin/netbeans" t) 'equal))) ))
(defvar netbeans-command (netbeans-find-command)
  "Command to run NetBeans.")
(defun netbeans-open-file (file &optional line)
  "Open FILE on LINE in NetBeans."
  (if (integerp line)
      (start-process "netbeans" nil netbeans-command "--open" (format "%s:%d" file line))
    (start-process "netbeans" nil netbeans-command "--open" file)))
(defun netbeans-open-this-buffer ()
  "Open file for burrent buffer in NetBeans."
  (interactive)
  (unless (stringp (buffer-file-name))
    (error "Buffer have no association with a file"))
  (if (file-regular-p (buffer-file-name))
      (netbeans-open-file (buffer-file-name) (line-number-at-pos))
    (message "Current buffer wasnt' associated with a real file")))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "calendar")

;; (setq mark-holidays-in-calendar t)
;; (setq all-christian-calendar-holidays t)
;; (setq calendar-date-display-form (quote ((format "%04s-%02d-%02d" year (string-to-int month) (string-to-int day)))))
;; (setq calendar-time-display-form (quote (24-hours ":" minutes (if time-zone " (") time-zone (if time-zone ")"))))
(setq calendar-week-start-day 1)
(setq european-calendar-style t)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "standard hooks")

(add-hook 'write-file-hooks 'time-stamp)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "logging, logs")

(defun my-auto-revert-tail-mode-hook ()
  (when (string-match "/logs?/\\|\\.\\(?:log\\|out\\)\\'\\|/.xsession-errors\\'"
                      (buffer-file-name (current-buffer)))
    (auto-revert-tail-mode 1)
    (log4-hi-mode 1)
    (setq scroll-margin my-scroll-margin)
    ))
(add-hook 'find-file-hook 'my-auto-revert-tail-mode-hook)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "auto-fill")

(setq-default fill-column 78)

(defvar my-fill-column 100
  "I use greater value then 78 for comment in prog source.")

;; By default used American convention - sentence and with two spaces. Change
;; it to one space. Has affect on filling and M-a, M-e commands.
(setq sentence-end-double-space nil)

;; Turn on auto-fill mode
(add-hook 'html-mode-hook 'turn-on-auto-fill)
(add-hook 'text-mode-hook 'turn-on-auto-fill)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "cacl, calculator")

(setq-default calc-group-digits t)
(setq-default calc-group-char "'")

(defun my-calc-eval-region (arg beg end)
  "Calculate the region and display the result in the echo area.
With prefix ARG non-nil, insert the result at the end of region."
  (interactive "P\nr")
  (require 'calc)
  (let* ((expr (buffer-substring-no-properties beg end))
         (result (calc-eval expr)))
    (if (null arg)
        (message "%s = %s" expr result)
      (goto-char end)
      (save-excursion
        (insert result)))))

;; ----------------------------------------------------------------
(message "rst, reStructuredText")

;; Maintaining the table of contents up-to-date.
(add-hook 'rst-adjust-hook 'rst-toc-update)

(unless window-system
  (eval-after-load 'rst
    '(progn
       (custom-set-faces
        '(rst-level-1-face ((t (:background "yellow"))) t)
        '(rst-level-2-face ((t (:background "yellow"))) t)
        '(rst-level-3-face ((t (:background "yellow"))) t)
        '(rst-level-4-face ((t (:background "yellow"))) t)
        '(rst-level-5-face ((t (:background "yellow"))) t)
        '(rst-level-6face ((t (:background "yellow"))) t)
        ) ) ) )

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "TeX, LaTeX")

(setq tex-run-command "initex")

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "AUC TeX")

;(add-hook 'LaTeX-mode-hook 'LaTeX-install-toolbar)
;; (setq TeX-parse-self t)             ; Enable parse on load.
;; (setq TeX-auto-save t)              ; Enable parse on save.
;; Query for master file. If you often use \include or \input, you should make AUCTEX aware of the
;; multi-file document structure.
(setq-default TeX-master nil)

;(setq TeX-PDF-mode t)
;(setq TeX-interactive-mode t)
;(setq TeX-source-specials-mode 1)

;;; some more menu entries in the command list:
;;; see tex-mik.el from package auctex: %v is defined in tex-mik.el
;;; other variables are defined in tex.el from auctex
;;; the meaning of some auctex-varibles:
        ;symbols defined in tex.el and tex-mik.el:
        ;%b name slave tex-file  %t name master tex-file
        ;%d dvi-file  %f ps-file
        ;%l "latex --src-specials"
        ;%n line number  %p printcommand  %q "lpq"
        ;%r (TeX-style-check TeX-print-style)
        ;%s master-file-name without extention
        ;%v yap command view line
;(eval-after-load "tex"
;  '(progn
;     (add-to-list 'TeX-command-list
;         (list "->PS landscape for pdf"
;           "dvips %d -N0 -Ppdf -G0 -T 297mm,210mm -o %f "
;           'TeX-run-command nil t))
;     (add-to-list 'TeX-command-list
;         (list "All Texify run-viewer"
;           "texify --tex-opt=--src --run-viewer --clean %s.tex"
;           'TeX-run-command nil t))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "reftex")

;; Reftex is included with Emacs 21.1.

;; (autoload 'reftex-mode     "reftex" "RefTeX Minor Mode" t)
;; (autoload 'turn-on-reftex  "reftex" "RefTeX Minor Mode" nil)
;; (autoload 'reftex-citation "reftex-cite" "Make citation" nil)
;; (autoload 'reftex-index-phrase-mode "reftex-index" "Phrase mode" t)
;; (add-hook 'LaTeX-mode-hook 'turn-on-reftex)   ; with AUCTeX LaTeX mode
;; (add-hook 'latex-mode-hook 'turn-on-reftex)   ; with Emacs latex mode

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "PreviewLatex")

;(load "preview-latex.el" nil t t)

;(add-hook 'LaTeX-mode-hook #'LaTeX-preview-setup)
;(autoload 'LaTeX-preview-setup "preview")

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "Info")

(unless (getenv "INFOPATH")
  (setenv "INFOPATH" (expand-file-name "~/usr/share/info:")))

;; Assume that cygwin-mount already activated.
(when (featurep 'cygwin-mount)
  (setenv "INFOPATH" "/usr/share/info/:~/usr/share/info/:")
  ;; Redefine path-separator to UNIX to update Info-directory-list.
  (let ( (path-separator ":") )
    (require 'info)
    (info-initialize)
    ))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "info-lookup")

;; Info index nodes for automake under Debian.
(setq my-fix-for-automake-info-lookup
      '(("(automake-1.11)Macro Index" nil
         "^`" "['(]")
        ("(automake-1.11)Variable Index" nil
         "^`" "['(]")
        ("(automake-1.11)General Index" nil
         "^`" "['(]")))

;; Add `my-fix-for-automake-info-lookup' entries to the end of doc-spec for
;; some modes.
(eval-after-load 'info-look
  '(progn
     (mapc
      (lambda (mode)
        (let ( (doc-spec (info-lookup->doc-spec 'symbol mode)) )
          (mapc
           (lambda (doc-spec-item)
             (setcdr (last doc-spec) (list doc-spec-item)))
           my-fix-for-automake-info-lookup)))
      '(makefile-mode autoconf-mode))
     (info-lookup-maybe-add-help
      :mode 'makefile-gmake-mode
      :other-modes '(makefile-mode))
     ))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "man, woman")

;; Assume that cygwin-mount already activated.
(when (featurep 'cygwin-mount)
  (let ( (path (expand-file-name "~")) )
    (when (string-match "\\([c-z]\\):/" path)
      (setenv "MANPATH" (concat "/cygdrive/" (substring path 0 1) "/" (substring path 3) "/usr/share/man/:"))
      ) ))

(setq woman-use-own-frame nil)
(setq woman-fill-frame t)
;; I prefer EN man pages.
(when (memq system-type '(windows-nt cygwin))
  (setq manual-program "LANG=C man")
  )

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "comint")

;; If non-nil, add a `/' to completed directories, ` ' to file names.
(setq comint-completion-addsuffix t)
;; Non-nil means go to the end of the line before sending input.
(setq comint-eol-on-send t)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "spell, ispell, aspell, flyspell")

;; Settings for spelling done in '.emacs-autogen'.

(add-hook 'log-edit-mode-hook 'flyspell-mode)

;; (setq-default ispell-extra-args  '("--sug-mode=ultra"))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "dict, dictd, dictionary")

(when (fboundp 'dict-c5-mode)
  (add-to-list 'auto-mode-alist '("\\.dict-c5$" . dict-c5-mode))
  )

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "figlet")

(defun my-figlet-region (&optional b e)
  (interactive "r")
  (shell-command-on-region b e "figlet" (current-buffer) t)
  (comment-region (mark) (point)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "jdone")

(ignore-errors
  (require 'jdone)
  (jdone-setup-key-binding)
  (jdone-integrate-hook)
  )

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "org-mode, GTD, PIM, organize, todo")

(when (>= emacs-major-version 22)
  (require 'org))

;; XXX org-todo-keywords '((sequence "TODO" "START" "|" "DONE")) for org-version 4.67c
(when (or (featurep 'org) (featurep 'org-install))
  (add-to-list 'auto-mode-alist '("\\.org$" . org-mode))
  (setq org-directory "~/devel/my/gtd")
  (setq
   org-agenda-ndays 31
   org-deadline-warning-days 7
   org-agenda-show-all-dates t
   org-agenda-format-date "%Y-%m-%d, %A %e %B"
   org-agenda-skip-deadline-if-done t
   org-agenda-skip-scheduled-if-done t
   org-agenda-start-on-weekday nil
   org-reverse-note-order t
   org-hide-leading-stars t
   org-tags-column 64
   org-archive-save-context-info '(time file olpath todo itags)
   )
  (setq my-org-agenda-todo-file (concat org-directory "/TODO.org"))
  (setq my-org-agenda-note-file (concat org-directory "/NOTE.org"))
  (setq my-org-agenda-learning-file (concat org-directory "/LEARNING.org"))
  (setq org-agenda-files `(,my-org-agenda-todo-file ,my-org-agenda-note-file ,my-org-agenda-learning-file))
  (define-key global-map "\C-va" 'org-agenda)
  (define-key global-map "\C-ve" (lambda nil (interactive) (find-file my-org-agenda-note-file)))

  (setq org-todo-keywords '("TODO" "DONE"))

  ;; My tags for remember buffer.
  (setq org-tag-alist
        '(
          ("ADMIN" . ?a)
          ("BLOG" . ?b)
          ("DEVEL" . ?d)
          ("HOME" . ?h)
          ("GET" . ?g)
          ("LIFE" . ?l)
          ("MAIL" . ?m)
          ("JOB" . ?j)
          ("QUESTION" . ?q)
          ("PROJECT" . ?p)
          ("READ" . ?r)
          ("SEE" . ?s)
          ))
  ;; With this variable tags duplicated in *Org Tags* menu. I use
  ;; `org-tag-alist' instead until bug fixed.
  (setq org-tag-persistent-alist nil)

  (setq org-support-shift-select t)

  (when (>= emacs-major-version 24)
    (require 'org-capture))

  (cond
   ((featurep 'org-capture)
    (setq org-default-notes-file my-org-agenda-todo-file)
    (setq org-capture-templates
          '(("t" "Todo" entry (file my-org-agenda-todo-file) "* TODO %?\n  SCHEDULED: %T")))
    (define-key global-map "\C-vr"
      (lambda () (interactive) (org-capture nil "t")))
    )
   ((or (featurep 'remember) (fboundp 'remember))
    (setq remember-annotation-functions '(org-remember-annotation))
    (setq remember-handler-functions '(org-remember-handler))
    (add-hook 'remember-mode-hook 'org-remember-apply-template)
    (define-key global-map "\C-vr" 'org-remember)
    (setq org-remember-templates
          ;; With one item org-mode do not prompt choose template.
          `(
            ("todo" ?t "* TODO %?" ,my-org-agenda-todo-file)
            ;; ("note" ?n "* %?\n" ,my-org-agenda-note-file)
            ))
    (setq org-remember-store-without-prompt t)
    (org-remember-insinuate)
    ))

  )


(defun my-org-archive-location (org-file)
  (concat (if (string-match "\\(.*\\)\\.org$" org-file)
              (match-string 1 org-file)
            org-file)
          "_done.org"))

(defun my-org-archive ()
  "Move marked by `org-done-keywords' entries to archive file defined by `my-org-archive-location'."
  (interactive)
  (let ( (org-archive (my-org-archive-location (buffer-file-name)))
         (entry-re (concat "^\\* "))
         (entry-done-re (concat "^\\* *" (mapconcat 'regexp-quote org-done-keywords "\\|") " "))
         entry-beg entry-end )
    (save-excursion
      (show-all)
      (kill-new "")
      (goto-char (point-min))
      (while (re-search-forward entry-done-re nil t)
        (setq entry-beg (line-beginning-position))
        ()
        (if (re-search-forward entry-re nil t)
            (beginning-of-line)
          (goto-char (point-max)))
        (setq entry-end (point))
        (let ( (last-command 'kill-region) )
          (kill-region entry-beg entry-end))
        )
      (find-file org-archive)
      (goto-char (point-max))
      (insert ?\n)
      (yank)
      )))

(setq org-agenda-include-diary t)

(defun my-org-kill-by-tag (tag)
  "Put all entries that matches TAG from current org-file to `kill-ring'."
  (interactive (list (completing-read "Enter tag: " (org-get-buffer-tags))))
  (kill-new "")
  (org-scan-tags
   (lambda ()
     (let ( (last-command 'kill-region) )
       (org-cut-subtree)))
   '(member tag tags-list)
   nil) )

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "TODO, XXX, FIXME highlight")

(defface my-contrasty-face
  '((t :background "pink" :foreground "red" :weight bold))
  "Font for showing conflicts."
  :group 'basic-faces)

(dolist (mode (append my-devel-mode-list my-text-mode-list))
  (font-lock-add-keywords
   mode
   `(
     ( ,(concat "\\<\\(" (regexp-opt '("TODO" "FIX" "FIXME" "HACK" "XXX")) ":?\\)\\>") 1 'font-lock-warning-face t)
     ;; 64 times, for highlight C-u C-u C-u <key>
     ("\\([^[:space:]]\\)\\1\\{63\\}" 0 'my-contrasty-face t)
     ))
  )

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "fortune")
(setq fortune-file "~/XXX")

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "mail, message")

(setq mail-user-agent 'message-user-agent)

(defun my-compose-mail ()
  (interactive)
  (compose-mail nil nil `(("Organization" . ,(getenv "ORGANIZATION")))))
(global-set-key (kbd "C-x m") #'my-compose-mail)

(setq message-citation-line-format "On %Y-%m-%d, %N wrote:
")
(setq message-citation-line-function 'message-insert-formatted-citation-line)

(setq mail-signature t)
(setq mail-signature-file "~/.signature")
;; (add-hook 'mail-setup-hook 'fortune-to-signature)

;; (setq mail-from-style 'angles)

(setq mail-personal-alias-file "~/.mailrc")
;; Initialize from your mail aliases file...
(setq mail-aliases t)
(require 'ecomplete)
(setq message-mail-alias-type '(abbrev ecomplete))

(eval-after-load 'message
  '(progn
     (require 'mailabbrev)
     (define-key message-mode-map "\e\t" 'mail-abbrev-complete-alias)
     ))

(defun my-message-mode-hook ()
  (setq fill-column 78)
  (turn-on-auto-fill)
  (flyspell-mode 1))
(add-hook 'message-mode-hook 'my-message-mode-hook)

;; Mark all my messages by "common" string in "Message-Id" field to simplify
;; lookup for followups to me.
(setq message-user-fqdn "gavenkoa.example.com")

;; Kill message buffer after mail send. You always can use C-c C-s to preserve it.
(setq message-kill-buffer-on-exit t)

(defconst my-safe-filename-char-regex "[[:alnum:]-_!.@]"
  "Safe file names.")

(defun my-clean-filename (filename)
  (mapconcat
   (lambda (ch) (or (when (string-match my-safe-filename-char-regex (char-to-string ch)) (char-to-string ch)) "-"))
   filename "") )

(defun my-message-save ()
  "Store message in `gnus-article-save-directory' after
successful sending. It is possible that mail rejected and I lost
it completely, this func save it for me."
  (unless (eq major-mode 'message-mode)
    (error "Attempt to call my-message-save in non message-mode buffer"))
  (make-directory gnus-article-save-directory t)
  (let ( (buf (current-buffer))
         (field-to (my-clean-filename (or (message-fetch-field "Newsgroups") (message-fetch-field "To"))))
         (field-subject (my-clean-filename (message-fetch-field "Subject")))
         file )
    (when (> (length field-to) 32)
      (setq field-to (substring field-to 0 32)))
    (when (> (length field-subject) 64)
      (setq field-subject (substring field-subject 0 64)))
    (setq file (concat gnus-article-save-directory "/" (format-time-string "%F_%T") "_" field-to "_" field-subject))
    (with-temp-file file
      (insert-buffer buf)
      )) )
(add-hook 'message-sent-hook 'my-message-save)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "smtp, smtpmail")

(when (and (not (eq system-type 'gnu/linux)) (boundp 'smtpmail-smtp-server))
  (require 'smtpmail)
  (setq
   ;; If you use the default mail user agent.
   send-mail-function 'smtpmail-send-it
   ;; If you use Message or Gnus.
   message-send-mail-function 'smtpmail-send-it
   )
  )
;; (setq smtpmail-debug-verb t)
;; (setq smtpmail-debug-info t)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "gnus, mh-e")

(setq gnus-site-init-file "~/.gnus.el")

(setq
 gnus-read-newsrc-file nil
 gnus-save-newsrc-file nil
 gnus-backup-startup-file t
 gnus-use-dribble-file t
 gnus-save-killed-list t
 )

(defun my-kill-gnus ()
  "Kill Gnus when exiting Emacs."
  (let ( (gnus-interactive-exit nil) )
    (gnus-group-exit)
    ))
(eval-after-load 'gnus '(add-hook 'kill-emacs-hook 'my-kill-gnus))

(eval-after-load 'gnus-art
  '(progn
     (setq gnus-visible-headers (concat gnus-visible-headers "\\|^Archived-At"))
     ))

;; Store gnus specific files to '~/.gnus'.
(setq
 gnus-directory "~/.gnus"
 gnus-agent-directory "~/.gnus/agent/"
 gnus-article-save-directory "~/.gnus/saved"
 gnus-kill-files-directory "~/.gnus/scores"
 gnus-cache-directory "~/.gnus/cache"
 mail-source-directory "~/.gnus/mail"
 message-directory "~/.gnus/mail"
 message-auto-save-directory "~/.gnus/autosave"
 ;; message-signature-directory
 mailcap-download-directory  "~/.gnus/mailcap"
 nnml-directory "~/.gnus/nnml-mail"
 spam-directory "~/.gnus/spam/"
 smime-certificate-directory "~/.gnus/certs/"
 nnfolder-directory "~/.gnus/archive"
 nnfolder-active-file "~/.gnus/archive/active"
 )

;; Remove gnus-ignored-newsgroups to show all GMail folders.
(setq gnus-ignored-newsgroups "some-non-existing")

;; (require 'spam)
;; (setq gnus-spam-process-newsgroups
;;       '(("^gmane\\."
;;          ((spam spam-use-gmane)))))

(setq
 gnus-novice-user nil
 gnus-expert-user nil
 gnus-interactive-catchup t
 gnus-interactive-exit t
 )

(setq
 gnus-read-active-file nil
 gnus-check-new-newsgroups nil
 gnus-check-bogus-newsgroups nil
 )

(setq nntp-connection-timeout 10)

(setq gnus-large-newsgroup 800)
(setq gnus-large-ephemeral-newsgroup 600)
(setq gnus-group-listing-limit 1000)

(setq
 gnus-group-goto-unread t
 gnus-summary-next-group-on-exit nil
 )

(setq
 gnus-permanently-visible-groups ".*"
 gnus-topic-display-empty-topics t)

;; (setq 'gnus-use-cache t)
(setq gnus-use-long-file-name t)
(setq gnus-cache-remove-articles '(read))

;; If you're spooling in overlapping mbox folders, you probably want to delete
;; all messages with duplicate message IDs.
(setq nnmail-treat-duplicates 'delete)

;; Do not use the html part of a message, use the text part if possible!
(setq mm-discouraged-alternatives '("text/html" "text/richtext"))
(setq mm-text-html-renderer 'w3m)

(defun my-w3m-view-url ()
  (interactive)
  (browse-url (w3m-anchor)))

(eval-after-load 'w3m
  '(progn
     (define-key w3m-minor-mode-map (kbd "RET") #'my-w3m-view-url)
     (define-key w3m-minor-mode-map (kbd "S-RET") #'w3m-safe-view-this-url)
     (define-key w3m-minor-mode-map (kbd "<left>") #'backward-char)
     (define-key w3m-minor-mode-map (kbd "<right>") #'forward-char)
     (define-key w3m-minor-mode-map (kbd "<up>") #'previous-line)
     (define-key w3m-minor-mode-map (kbd "<down>") #'next-line)))

(add-hook 'gnus-group-mode-hook 'gnus-topic-mode)

(eval-after-load 'gnus '(progn (gnus-demon-add-handler 'gnus-demon-scan-news 10 t)))

;; Show prefix and 'To' field instead 'From' for my mails.
(setq gnus-summary-to-prefix "==> ")
(setq gnus-summary-newsgroup-prefix "==> ")
(setq gnus-ignored-from-addresses (list (regexp-quote user-mail-address) (regexp-quote user-full-name)))

(setq gnus-posting-styles
      '((".*"
         (organization (getenv "ORGANIZATION"))
         (signature-file "~/.signature"))))

(gnus-add-configuration
 '(article (vertical 1.0 (summary .30 point) (article 1.0))))

(setq gnus-summary-display-arrow t)

;; `gnus-summary-line-format',
;; `gnus-server-line-format', `gnus-topic-line-format',
;; `gnus-group-mode-line-format', `gnus-summary-mode-line-format',
;; `gnus-article-mode-line-format', `gnus-server-mode-line-format',
;; `gnus-summary-pick-line-format'.

(when window-system
  (setq
   gnus-sum-thread-tree-root "● "
   gnus-sum-thread-tree-false-root " ○ "
   gnus-sum-thread-tree-indent " "
   gnus-sum-thread-tree-single-indent "⚇ "
   gnus-sum-thread-tree-leaf-with-other "├► "
   gnus-sum-thread-tree-single-leaf "└► "
   gnus-sum-thread-tree-vertical "│"
   ))

(setq gnus-user-date-format-alist '((t . "%Y-%m-%d %H:%M")))
(setq gnus-summary-line-format "%U%R%z %&user-date; %B %[%-22,22f%] %s\n")
(setq gnus-summary-mode-line-format "Gnus: %p %Z")

(setq gnus-treat-date-user-defined 'head)
(setq gnus-article-time-format "%Y-%m-%d %T%z %a")
(setq gnus-article-date-headers '(combined-lapsed user-defined))

;; Remember when I visit group last time.
(add-hook 'gnus-select-group-hook 'gnus-group-set-timestamp)

(defun gnus-user-format-function-d (header)
  (let ((time (gnus-group-timestamp gnus-tmp-group)))
    (if (and time (time-less-p (time-subtract (current-time) time) (seconds-to-time (* 3600 24 7 30 2))))
        (format-time-string "%Y-%m-%d %H:%M" time)
      "")))

;; %d (or with user defined format %ud) shown when I visit group last time in
;; %*Group* buffer.
(setq gnus-group-line-format "%M%S%p%P%6y:%B%(%-50,50G%)%3O %ud\n")

;; gnus-visible-headers gnus-extra-headers
(setq
 gnus-sorted-header-list
 '(
   "^From:"
   "^Subject:"
   "^Summary:"
   "^Keywords:"
   "^Newsgroups:"
   "^Followup-To:"
   "^To:"
   "^Cc:"
   "^Organization:"
   "^Date:"
   "^User-Agent:"
   "^X-Mailer:"
   "^X-Newsreader:"
   ))

(setq
 gnus-show-threads t
 gnus-thread-sort-functions '(gnus-thread-sort-by-date gnus-thread-sort-by-total-score)
 gnus-summary-thread-gathering-function 'gnus-gather-threads-by-subject
 gnus-summary-gather-subject-limit 'fuzzy
 gnus-thread-hide-killed t
 gnus-thread-ignore-subject t
 )

;; gnus-summary-mark-below gnus-summary-default-score gnus-summary-default-high-score gnus-summary-default-low-score

;; Scoring.
(setq
 gnus-use-scoring t
 gnus-save-score t
 gnus-score-expiry-days 60
 ;; gnus-decay-scores t
 gnus-score-decay-constant 3
 )

(setq gnus-score-interactive-default-score 100)

;; These variables are local to each summary buffer and usually set by the
;; score file: gnus-summary-mark-below, gnus-summary-expunge-below, gnus-thread-expunge-below

(setq gnus-use-adaptive-scoring t)
;; I use 100 for replay to me and 200 for essential mails, and -50 for bad mails.
(setq gnus-default-adaptive-score-alist
      '(
        (gnus-unread-mark)
        (gnus-ticked-mark (followup 100))
        (gnus-dormant-mark (followup 100))
        ;; (gnus-read-mark (followup -50))
        ;; (gnus-catchup-mark (subject -50))
        (gnus-del-mark (followup -50))
        (gnus-killed-mark (followup -50))
        (gnus-kill-file-mark (from -9999))
        ))

;; Increase the score for followups to a sent article.
(eval-after-load 'gnus-score
  '(progn
     ;; (add-hook 'message-sent-hook 'gnus-score-followup-article)
     (add-hook 'message-sent-hook 'gnus-score-followup-thread)
     ))

(defvar my-gnus-summary-kill-same-subject-min-len 8
  "Minimal length of subject string to ignore this subject.")
(defun my-gnus-summary-kill-same-subject (&optional unmark)
  "Add negative scores for all articles with same subject."
  (interactive "P")
  (when (or (not (integerp unmark)) (< 0 unmark))
    (let ( (subj (gnus-simplify-subject-fuzzy (gnus-summary-article-subject))) )
      (when (<= (length subj) my-gnus-summary-kill-same-subject-min-len)
        (gnus-summary-score-entry
         "subject" subj
         's (- gnus-score-interactive-default-score) (current-time-string)))))
  (gnus-summary-kill-same-subject unmark))
(eval-after-load 'gnus-sum
  '(define-key gnus-summary-mode-map (kbd "C-k") #'my-gnus-summary-kill-same-subject))

(defun my-gnus.mark-thread-as-read ()
  "Mark unmarked articles in current thread as read and move to
next thread without selecting article."
  (interactive)
  (gnus-summary-top-thread)
  (catch 'exit
    (while t
      (when (eq (gnus-summary-article-mark (gnus-summary-article-number)) gnus-unread-mark)
        (gnus-summary-mark-article (gnus-summary-article-number) gnus-del-mark))
      (when (or (not (gnus-summary-search-forward)) (eq (gnus-summary-thread-level) 0))
        (throw 'exit nil)) )))
(eval-after-load 'gnus-sum
  '(define-key gnus-summary-mode-map (kbd "H-k") #'my-gnus.mark-thread-as-read))

(defun my-gnus-thread-score-function (&rest scores)
  "If any followup have positive score assign greater available
score to thread, else assign lesser available score."
  (let ( (max (apply 'max scores)) (min (apply 'min scores)) )
    (if (< 0 max) max min)))
(setq gnus-thread-score-function #'my-gnus-thread-score-function)
(defun my-gnus-thread-total-score ()
  "Helper to debug `gnus-thread-score-function' function."
  (interactive)
  (message
   (int-to-string
    (gnus-thread-total-score
     (gnus-id-to-thread (mail-header-id (gnus-summary-article-header)))))))

;; Especially highlight my message and replays to me.
(eval-after-load 'gnus-sum
  '(progn
     (defface my-gnus-own-unread-face nil
       "Use this face to display own postings in Summary Buffer")
     (copy-face 'gnus-summary-high-unread-face 'my-gnus-own-unread-face)
     (set-face-background 'my-gnus-own-unread-face "linen")
     (add-to-list 'gnus-summary-highlight
                  '((and (> score 190) (eq mark gnus-unread-mark)) . my-gnus-own-unread-face))
     (defface my-gnus-own-ancient-face nil
       "Use this face to display own postings in Summary Buffer")
     (copy-face 'gnus-summary-high-ancient-face 'my-gnus-own-ancient-face)
     (set-face-background 'my-gnus-own-ancient-face "linen")
     (add-to-list 'gnus-summary-highlight
                  '((and (> score 190) (eq mark gnus-ancient-mark)) . my-gnus-own-ancient-face))
     (defface my-gnus-own-ticked-face nil
       "Use this face to display own postings in Summary Buffer")
     (copy-face 'gnus-summary-high-ticked-face 'my-gnus-own-ticked-face)
     (set-face-background 'my-gnus-own-ticked-face "linen")
     (add-to-list 'gnus-summary-highlight
                  '((and (> score 190) (or (eq mark gnus-dormant-mark) (eq mark gnus-ticked-mark))) . my-gnus-own-ticked-face))
     (defface my-gnus-fup-face nil
       "Use this face to display direct fups to my postings.")
     (copy-face 'gnus-summary-high-unread-face 'my-gnus-fup-face)
     (set-face-background 'my-gnus-fup-face "honeydew")
     (add-to-list 'gnus-summary-highlight
                  '((and (<= 90 score) (<= score 110) (eq mark gnus-unread-mark)) . my-gnus-fup-face))
     ))

;; (setq gnus-home-score-file
;;       ;; All groups that match the regexp `"\\.emacs"'
;;       '(("\\.emacs" "emacs.SCORE")
;;         ;; All the comp groups in one score file
;;         ("^comp" "comp.SCORE")))

;; Make C-Up, C-Down more like across paragraph moving.
(eval-after-load 'gnus
  '(progn
     (define-key gnus-summary-mode-map [(meta up)] '(lambda() (interactive) (scroll-other-window -1)))
     (define-key gnus-summary-mode-map [(meta down)] '(lambda() (interactive) (scroll-other-window 1)))
     (define-key gnus-summary-mode-map [(control down)] 'gnus-summary-next-thread)
     (define-key gnus-summary-mode-map [(control up)] 'gnus-summary-prev-thread)
     ))

;; (setq imap-log t)

;; (setq mail-user-agent 'mh-e-user-agent)

(custom-set-faces
 '(gnus-summary-cancelled ((t (:foreground "plum" :background nil))))
 '(gnus-signature-face ((t (:foreground "grey"))))
 )

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "bbdb")

(setq
 bbdb-offer-save 1
 bbdb-use-pop-up t
 bbdb-electric-p t
 bbdb-popup-target-lines  1
 )

(setq bbdb-complete-name-full-completion t)
(setq bbdb-completion-type 'primary-or-name)
(setq bbdb-complete-name-allow-cycling t)

(setq bbdb-file "~/.gnus/bbdb")

;; (bbdb-initialize 'gnus 'message)
;; (bbdb-insinuate-message)
;; (add-hook 'gnus-startup-hook 'bbdb-insinuate-gnus)


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "jabber")

(autoload 'jabber-connect-all "jabber")

(setq
 jabber-history-enabled t
 jabber-history-muc-enabled t
 jabber-use-global-history nil
 jabber-backlog-number 40
 jabber-backlog-days 30
 jabber-alert-presence-message-function (lambda (who oldstatus newstatus statustext) nil)
 )

(eval-after-load 'jabber
  '(progn
    ;; Redefine standard binding for sending message form RET to C-RET.
    (define-key jabber-chat-mode-map (kbd "RET") 'newline)
    (define-key jabber-chat-mode-map [C-return] 'jabber-chat-buffer-send)
    ;; fsm used in emacs jabber
    (when (featurep 'fsm)
      (setq fsm-debug nil)                  ; Disable *fsm-debug* buffer.
      )
    ;; Handle Emacs exit.
    (add-hook 'kill-emacs-hook 'jabber-disconnect)
    ))

(setq my-chat-prompt "[%t] %n>\n")
(setq
 jabber-chat-foreign-prompt-format my-chat-prompt
 jabber-chat-local-prompt-format my-chat-prompt
 jabber-groupchat-prompt-format my-chat-prompt
 jabber-muc-private-foreign-prompt-format "[%t] %g/%n>\n"
 )

(let ( (mgs-list '("Я тутачки, а где Вы меня ожидали?"
                   "Software Development == Church Development. Step 1. Build it. Step 2. Pray."
                   "Great books aren't written – they're rewritten."
                   "А любит Б, Б любит С, что делать A? Найти другую Б!")) )
  (random t)
  (setq jabber-default-show (nth (random (length mgs-list)) mgs-list))
  (setq jabber-default-status (nth (random (length mgs-list)) mgs-list))
  )

(defvar my-jabber-users nil
  "Assoc list of jabber user group. Keys are strings, values are lists of JIDs.")

(defun my-jabber-send (group)
  "GROUP is keys from `my-jabber-users'"
  (interactive
   (list (completing-read "Select group: " my-jabber-users))
   )
  (let (
        (msg (if (use-region-p)
                 (buffer-substring (region-beginning) (region-end))
               (buffer-string)))
        (jc (jabber-read-account))
        )
    (deactivate-mark)
    (mapc
     (lambda (user)
       (jabber-send-message jc user "" msg "normal")
       )
     (cdr (assoc group my-jabber-users))
     )
    )
  )

(global-set-key (kbd "C-x C-j C-s") 'my-jabber-send)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "rcirc")

;; Turn on logging everything to a special buffer, for debugging.
;; (setq rcirc-debug-flag t)

(add-hook 'rcirc-mode-hook (lambda () (rcirc-track-minor-mode 1)))
(setq rcirc-time-format "%H:%M ")
(defun rcirc-handler-301 (process cmd sender args)
  "/away message handler.")

(setq rcirc-default-server "irc.freenode.net")
(setq rcirc-default-port 6667)
(setq rcirc-default-nick user-nick)
(setq rcirc-default-user-full-name user-full-name)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "erc")

;; (add-to-list 'erc-modules 'notify)
;; (setq erc-notify-list '(""))

;; Take off noise message.
(setq erc-track-exclude-types '("JOIN" "PART" "QUIT" "NICK" "MODE"))

(setq erc-current-nick-highlight-type 'nick-or-keyword)
(setq erc-track-use-faces t)

(setq erc-server-coding-system 'utf-8)
(setq erc-encoding-coding-alist
      '(
        ("^icq-" . cp1251)
        ("#debian-russian" . koi8-r)
        ))

;; (setq erc-autojoin-channels-alist '(("freenode.net" "#emacs")))

(setq
 erc-server-auto-reconnect t
 erc-server-reconnect-timeout 60
 erc-server-reconnect-attempts 2
 )

;; Kill buffers for channels after /part
;; (setq erc-kill-buffer-on-part t)
;; Kill buffers for private queries after quitting the server
;; (setq erc-kill-queries-on-quit t)
;; Kill buffers for server messages after quitting the server
;; (setq erc-kill-server-buffer-on-quit t)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "devel, programming")

(which-func-mode 1)

(add-to-list 'auto-mode-alist '("\\.cu$" . c-mode))

(defun my-c++-header-file-p ()
  "Return non-nil, if in a C++ header."
  (and (string-match "\\.h$"
                     (or (buffer-file-name)
                         (buffer-name)))
       (save-excursion
         (re-search-forward "\\_<class\\_>" nil t))))

(when (>= emacs-major-version 22)
  (add-to-list 'magic-mode-alist '(my-c++-header-file-p . c++-mode))
  )

(setq-default comment-style (quote indent))
(setq-default comment-column 44)
(setq-default comment-fill-column my-fill-column)

(mapc (lambda (hook) (add-hook hook (lambda () (setq fill-column my-fill-column)) ))
      (append my-devel-mode-hook-list my-text-mode-hook-list))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "diff, patch, ediff, emerge")

(setq diff-switches "-u")

(setq ediff-diff-options "")
(setq ediff-custom-diff-options "-u")
(setq ediff-window-setup-function 'ediff-setup-windows-plain)
(setq ediff-split-window-function 'split-window-vertically)

;; Disable: sometimes it take a long time to process large hunks.
;; Use C-c C-b on hunk by own.
;; (defun my-diff-auto-refine-mode-on () (diff-auto-refine-mode 1))
;; (add-hook 'diff-mode-hook 'my-diff-auto-refine-mode-on)

(when window-system
  (eval-after-load 'diff-mode
    '(progn
       (set-face-foreground 'diff-added-face "DarkGreen")
       (set-face-foreground 'diff-removed-face "DarkRed")
       (set-face-background 'diff-refine-change "LightBlue1")
       )))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "vc-mode, VCS, version control, cvs, svn, mercurial, hg, bazaar, bzr, git, fossil")

;; `-b' switch to ignore changes in whitespaces.
;; (setq vc-git-diff-switches "-b")
;; (setq vc-diff-switches "-b")

(defun my-vc-root-diff (prefix)
  "Same as `vc-root-diff' but for Hg with C-u show latest MQ patch and
with C-u C-u show MQ patch and local changes."
  (interactive "P")
  (when (eq 'non-hg-mq
            (catch 'break
              (unless (and prefix (eq (vc-deduce-backend) 'Hg))
                (throw 'break 'non-hg-mq))
              (let* ( (rootdir (vc-call-backend 'Hg 'root default-directory)) (default-directory rootdir) )
                (unless (eq (vc-hg-command nil t rootdir "log" "-r" "qtip") 0)
                  (throw 'break 'non-hg-mq))
                (cond
                 ((equal prefix '(4))
                  (vc-diff-internal t (list 'Hg (list rootdir)) "qparent" "qtip"))
                 ((equal prefix '(16))
                  (vc-diff-internal t (list 'Hg (list rootdir)) "qparent" nil)) ))))
    (call-interactively 'vc-root-diff nil) ))

(global-set-key (kbd "C-x v D") 'my-vc-root-diff)

(when (equal window-system 'w32)
  (modify-coding-system-alist 'process "cvs" '(cp1251-dos . cp1251-dos))
  (modify-coding-system-alist 'process "svn" '(cp1251 . cp1251))
  (setq vc-svn-checkin-switches '("--encoding" "UTF-8"))
  )

(when window-system
  (setq
   vc-annotate-very-old-color "#0b5b20"
   vc-annotate-background "white"
   vc-annotate-color-map
   '(
     (20 .  "#EE0000")
     (40 .  "#E0800D")
     (60 .  "#D3001A")
     (80 .  "#C68027")
     (100 . "#B90034")
     (120 . "#AB8042")
     (140 . "#9E004F")
     (160 . "#91805C")
     (180 . "#840069")
     (200 . "#778077")
     (220 . "#690084")
     (240 . "#5C8091")
     (260 . "#4F009E")
     (280 . "#4280AB")
     (300 . "#3400B9")
     (320 . "#2780C6")
     (340 . "#1A00D3")
     (360 . "#0D80E0")))
  )

(defun my-log-edit-mode-hook ()
  (setq fill-column 78)
  )
(add-hook 'log-edit-mode-hook 'my-log-edit-mode-hook t)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "psvn")

(setq svn-status-verbose t)
(setq svn-status-hide-unmodified t)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "DVC")

(when (featurep 'dvc-emacs)
  (setq dvc-tips-enabled nil)
  )

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "indenting")

(setq standard-indent 4)
(setq c-basic-offset 4)
(setq sh-basic-offset 2)
(setq sgml-basic-offset 4)

;; TAB (tab settings)
(setq-default tab-width 4)
(setq-default indent-tabs-mode nil)     ; spaces instead of tabs by default
(setq tab-always-indent t)
(setq c-tab-always-indent t)
(let ( (line-width 400) i )
  (setq i (* (ceiling line-width 4) 4))
  (setq tab-stop-list nil)
  (while (>= i 0)
    (setq tab-stop-list (cons i tab-stop-list))
    (setq i (- i 4))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "compile")

;; Prompt for compilation command.
(setq compilation-read-command 1)
(setq compile-auto-highlight t)
(setq compile-command "mymake ")
(when (eq system-type 'berkeley-unix)
  (setq compile-command "gmake "))
;; With '1' compilation window shall scroll down, with `first-error' stops scrolling at the first error.
(setq compilation-scroll-output 1)
(setq compilation-ask-about-save t)

;; Show error in EN locale to easy search how fix problem in docs and Internet.
(setq compilation-environment '("LANG=C"))

(eval-after-load 'compile
  '(progn
     ;; My funny error messages.
     (add-to-list 'compilation-error-regexp-alist '("^\\( +\\[csc\\] \\|\\)\\(.*\\)(\\([0-9]*\\),\\([0-9]*\\)):" 2 3 4))
     (add-to-list 'compilation-error-regexp-alist '("^ *\\(.*\\)(\\([0-9]*\\)) +:" 1 2))
     (add-to-list 'compilation-error-regexp-alist '("^\"?\\([^\"]*\\)\"?,\\([0-9]*\\) .*\\[.*\\]: " 1 2)) ; KEIL compiler
     (add-to-list 'compilation-error-regexp-alist-alist '(maven "\\[ERROR\\] \\(.*\\.java\\):\\[\\([0-9]+\\),\\([0-9]+\\)\\]" 1 2 3))
     (add-to-list 'compilation-error-regexp-alist 'maven)
     (when (boundp 'compilation-mode-font-lock-keywords)
       (add-to-list 'compilation-mode-font-lock-keywords '("\\(/[Oo][Uu][Tt]:[^[:blank:]]+\\)" . 1))
       (add-to-list 'compilation-mode-font-lock-keywords '("[[:blank:]]\\(/F[oe][^[:blank:]]+\\)" . 1))
       )
     ))

(ignore-errors
  (require 'ansi-color)
  (defun my-colorize-compilation-buffer ()
    (when (eq major-mode 'compilation-mode)
      (ansi-color-apply-on-region compilation-filter-start (point-max))))
  (add-hook 'compilation-filter-hook 'my-colorize-compilation-buffer))

(defvar my-comint-send-hist-list nil
  "History list for `my-comint-send-string'."
  )
(defun my-comint-send-string (string)
  "Send string to comint buffers. Useful for *compilation* read-only buffer.
Automaticaly append final newline."
  (interactive
   (list (read-input "Type string: " nil 'my-comint-send-hist-list))
   )
  (comint-send-string (get-buffer-process (current-buffer)) (concat string "\n"))
  )
(eval-after-load 'compile
  '(progn
     (define-key compilation-mode-map [C-return] 'my-comint-send-string)
     ))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "scons")

(add-to-list 'auto-mode-alist '("SConstruct\\'" . python-mode))
(add-to-list 'auto-mode-alist '("SConscript\\'" . python-mode))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "gdb, gud")
;; Use C-x SPACE to set breakpoint in source file.

(setq gdb-show-main t)                  ; See also (gdb-many-windows)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "TAGS, etags, ctags, GNU GLOBAL")

;; One of 'tags-table-list' or 'tags-file-name' control which TAGS files to
;; use.

(ignore-errors
  (require 'etags-table)
  (setq etags-table-search-up-depth 8)
  (require 'etags-select)
  (global-set-key "\M-." 'etags-select-find-tag)
  )

(setq tags-add-tables t)
(global-set-key "\M-\r" 'complete-tag)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "CEDET, semantic, SRecord")

;; For debug use 'semantic-debug-idle-function' and 'semantic-debug-idle-work-function'.

(when (or
       (and (= emacs-major-version 23) (>= emacs-minor-version 2))
       (>= emacs-major-version 24) )
  (require 'cedet))

(when (featurep 'cedet)
  (require 'semantic)

  (global-semantic-idle-scheduler-mode 1)
  (global-semantic-idle-summary-mode 1)
  (global-semantic-idle-completions-mode 1)
  (global-semantic-mru-bookmark-mode 1)
  ;; (setq semantic-stickyfunc-sticky-classes '(function type variable include package))
  (global-semantic-stickyfunc-mode -1)
  (global-semantic-highlight-func-mode 1)
  (global-semantic-decoration-mode 1)
  (global-semantic-idle-local-symbol-highlight-mode 1)

  (setq semantic-idle-scheduler-idle-time 10)
  (setq semantic-idle-scheduler-work-idle-time 60)
  (setq semantic-idle-scheduler-max-buffer-size 100000)
  ;; (setq semantic-idle-work-parse-neighboring-files-flag nil)

  ;; semantic-dependency-system-include-path, semantic-customize-system-include-path

  ;; file local project unloaded system recursive
  (setq-mode-local c-mode semanticdb-find-default-throttle '(file local))
  (add-hook 'c-mode-hook (lambda nil (semantic-add-system-include "~/.emacs.d/include" 'c-mode)))
  (add-hook 'c-mode-hook (lambda nil (semantic-add-system-include "~/.emacs.d/include" 'c++-mode)))
  (global-semanticdb-minor-mode 1)

  (global-cedet-m3-minor-mode 1)

  (semantic-mode 1)
  (global-ede-mode 1)

  (require 'srecode)
  (unless (boundp 'srecode-map-load-path)
    (setq srecode-map-load-path nil))
  (add-to-list 'srecode-map-load-path (locate-user-emacs-file "srecode/"))
  (global-srecode-minor-mode 1)
  (add-hook 'prog-mode-hook 'srecode-minor-mode)

  (ignore-errors
    (cedet-ectag-version-check)
    (semantic-load-enable-primary-ectags-support))
  (ignore-errors
    (cedet-gnu-global-version-check)
    (require 'cedet-global)
    (semanticdb-enable-gnu-global-databases 'c-mode)
    (semanticdb-enable-gnu-global-databases 'c++-mode))

  (ignore-errors
    (require 'cedet-java))

  ;; (require 'semantic/db-javap)

  ;; (add-to-list 'ede-locate-setup-options 'ede-locate-idutils)
  ;; (add-to-list 'ede-locate-setup-options 'ede-locate-global)

  ;; (ignore-errors (require 'cedet-idutils))

  (defun my-c-mode-cedet-hook ()
    ;; (local-set-key [C-return] 'semantic-complete-symbol)
    ;; (local-set-key [C-return] 'semantic-complete-analyze-inline)
    (local-set-key "." 'semantic-complete-self-insert)
    (local-set-key ">" 'semantic-complete-self-insert))
  (add-hook 'c-mode-common-hook 'my-c-mode-cedet-hook)

  (ignore-errors
    (require 'semantic/ia)
    (define-key semantic-mode-map (kbd "C-c , .") 'semantic-ia-fast-jump)
    (define-key semantic-mode-map (kbd "C-c , d") 'semantic-ia-show-doc)
    (define-key semantic-mode-map (kbd "C-c , D") 'semantic-ia-show-summary)
    (define-key semantic-mode-map (kbd "C-c , ?") 'semantic-ia-complete-symbol-menu)
    ;; (define-key semantic-mode-map [(control return)] 'semantic-ia-complete-symbol)
    ;; (define-key semantic-mode-map (kbd "C-c , C") 'semantic-ia-describe-class)
    ;; semantic-decoration-include-visit
    ;; semantic-analyze-proto-impl-toggle
    )

  ;; END OF: (when (featurep 'cedet) ...)
  )

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "imenu")

(require 'imenu)

(defun my-imenu-to-menubar ()
  "Force imenu building when (menu-bar-mode -1)."
  (when imenu-generic-expression
    (imenu-add-menubar-index)
    (run-hooks 'menu-bar-update-hook) ))
(mapc (lambda (hook) (add-hook hook 'my-imenu-to-menubar))
      my-devel-mode-hook-list)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "windows inf files for driver installin")

(add-to-list 'auto-mode-alist '("\\.inf\\'" . conf-mode))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "fvwm-mode")

(eval-after-load 'fvwm-mode
  (setq fvwm-fvwmcommand-path (executable-find "FvwmCommand")))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "makefile, make")

(add-to-list 'auto-mode-alist '("\\(Makefile\\|Makefile\\..+\\)\\'" . makefile-gmake-mode) t)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "asm, assembler")

;; (setq-default asm-comment-char 59)
(add-hook 'asm-mode-hook '(lambda () (setq comment-start "/*") (setq comment-end "*/")) t)

(add-to-list 'auto-mode-alist '("\\.\\([sS]79\\|[sS]\\)\\'" . asm-mode))

;; (add-hook 'asm-mode-hook '(lambda () (local-unset-key ":")))
;; (add-hook 'asm-mode-hook '(lambda () (local-set-key ":" ":")))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "linker")

(when (fboundp 'iar-linker-config-mode)
  (add-to-list 'auto-mode-alist '("\\.icf\\'" . iar-linker-config-mode))
  )
(when (fboundp 'iar4-linker-config-mode)
  (add-to-list 'auto-mode-alist '("\\.xcl\\'" . iar4-linker-config-mode))
  )

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "lisp, elisp")

(setq list-command-history-max 256)

(add-hook 'emacs-lisp-mode-hook 'turn-on-eldoc-mode)
(defun my-emacs-lisp-mode-hook ()
  (setq tab-width 8))
(add-hook 'emacs-lisp-mode-hook 'my-emacs-lisp-mode-hook)

(defun my-elisp-find-tag ()
  (interactive)
  (require 'etags)
  (ring-insert find-tag-marker-ring (point-marker))
  (unless (find-variable-at-point)
    (find-function-at-point)
    ))
;; Goto elisp definition.
(define-key emacs-lisp-mode-map (kbd "M-.") 'my-elisp-find-tag)

;; http://www.emacswiki.org/emacs/PrettyLambda
(font-lock-add-keywords
 'emacs-lisp-mode
 `(
   ("(\\<\\(lambda\\)\\>"
    (1 (progn (compose-region (match-beginning 1) (match-end 1) ,(make-char 'greek-iso8859-7 107)) font-lock-keyword-face))
    )
   ))

(defun my-dump-funcs ()
  "Dump all function calls in current buffer. Useful to explore
elisp API from somebody else files."
  (interactive)
  (let* ( (cur-buffer (current-buffer)) (new-buffer (get-buffer-create (concat (buffer-name cur-buffer) "-funcs.el"))) symb )
    (while (search-forward-regexp "([[:alnum:]*]" nil t)
      (setq symb (thing-at-point 'symbol))
      (with-current-buffer new-buffer
        (insert-string symb)
        (insert-char ?\n 1)))
    (switch-to-buffer new-buffer)
    (shell-command-on-region (point-min) (point-max) "sort -u" nil t)
    ))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "c-mode, cc-mode, c++-mode")

;; Minor mode that highlights suspicious C and C++ constructions.
(global-cwarn-mode 1)

(setq c-echo-syntactic-information-p t)

(defun my-c-mode-common-hook ()
  ;; Automatically inserte newlines after special characters such as brace, comma, semi-colon, and colon.
  (c-toggle-auto-newline -1)
  ;; Delete all preceding whitespace by DEL.
  (c-toggle-hungry-state -1)
  ;; Auto indent after typing colon according to `c-hanging-colons-alist'.
  (c-toggle-electric-state 1)
  )
(add-hook 'c-mode-common-hook 'my-c-mode-common-hook)

(defconst my-c-style
  '((c-tab-always-indent . t)
    (c-comment-only-line-offset . 4)
    (c-hanging-braces-alist
     . (
        (brace-list-open)
        (substatement-open after)
        ))
    (c-hanging-colons-alist
     . (
        (access-label after)
        (case-label after)
        (inher-intro)
        (label after)
        (member-init-intro before)
        ))
    (c-cleanup-list
     . (
        defun-close-semi
        empty-defun-braces
        scope-operator
        ))
    (c-offsets-alist
     . (
        (access-label . -)
        (arglist-intro . ++)
        (arglist-cont-nonempty . ++)
        (arglist-close . ++)
        (block-open . 0)
        (case-label . 0)
        (cpp-define-intro . 0)
        (comment-intro . 0)
        (func-decl-cont . ++)
        (inexpr-class . 0)
        (inline-open . 0)
        (knr-argdecl-intro . -)
        (label . 0)
        (statement-block-intro . +)
        (statement-cont . ++)
        (substatement-open . 0)
        (inextern-lang . 0)
        ))
    ;; Certain syntactic errors are reported (like wrong indent).
    (c-report-syntactic-errors . t)
    ;; Echo syntactic information on TAB in message buffer.
    (c-echo-syntactic-information-p . t))
  "My C Programming Style")

(defun my-c-mode-style-hook ()
  (c-add-style "my" my-c-style t)
  ;; If set 'c-default-style' before 'c-add-style'
  ;; "Undefined style: my" error occured from 'c-get-style-variables'.
  (setq c-default-style
        '(
          (java-mode . "my") (c-mode . "my") (csharp-mode . "my") (c++-mode . "my") (objc-mode . "my")
          (idl-mode . "my")
          (awk-mode . "awk")
          (other . "my")
          ))
  )
(add-hook 'c-mode-common-hook 'my-c-mode-style-hook)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "python")

(setq python-indent 4)

(eval-after-load 'python-mode
  '(when (and (boundp 'py-version) (equal py-version "5.1.0"))
    (setq-default py-which-shell py-python-command)
    ;; (py-toggle-shells 'cpython)
    ))

(eval-after-load 'python
  '(define-key inferior-python-mode-map "\C-c\C-f" 'python-describe-symbol))

;; Enable "M-/", "C-c g", "C-c d", "C-c f" shortcuts.
(setq ropemacs-enable-shortcuts t)
(ignore-errors
  ;; (require 'pymacs)
  ;; (pymacs-load "ropemacs" "rope-")
  )
;; Automatically save project python buffers before refactorings
(setq ropemacs-confirm-saving 'nil)

(defun my-python-add-to-path (path)
  (interactive (list (read-directory-name "Enter new path for PYTHONPATH: ")))
  (when (featurep 'cygwin-mount)
    (setq path (my-dos2cygwin-path path)))
  (python-send-string (format "import sys; sys.path.append(\"%s\")" (expand-file-name path))) )

(defun my-python-django-fix (path)
  "XXX not work on Cygwin + naive Emacs."
  (interactive (list (read-directory-name "Enter new path for PYTHONPATH: ")))
  (when (featurep 'cygwin-mount)
    (setq path (my-dos2cygwin-path path))
    )
  (let ((file (concat path "manage.py")))
    (if (file-exists-p file)
        (python-send-string (format "import os; os.chdir(\"%s\"); execfile(\"%s\")" path file))
      (error (format "file '%s' does not exist" file))
      )))

;; Disable in flavor of Semantic and perfomance reason.
;; (when (>= emacs-major-version 22)
;;   (add-hook 'python-mode-hook 'turn-on-eldoc-mode))

(when (equal window-system 'w32)
  (add-to-list 'process-coding-system-alist '("python" cp1251-unix . cp1251-unix)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "perl, cperl")

;; Use cperl instead perl mode.
(mapc
 (lambda (pair)
   (if (eq (cdr pair) 'perl-mode)
       (setcdr pair 'cperl-mode)))
 (append auto-mode-alist interpreter-mode-alist))
;; Don't show all man page. I set man switches to "-a"...
(add-hook
 'cperl-mode-hook
 '(lambda ()
    (make-local-variable 'Man-switches)
    (setq Man-switches nil)))

;; Expands for keywords such as foreach, while, etc...
(setq cperl-electric-keywords t)

(setq
 cperl-indent-level 2
 cperl-close-paren-offset -2
 cperl-continued-statement-offset 2
 cperl-indent-parens-as-block t
 cperl-tab-always-indent t)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "SML, Standard ML")

(eval-after-load 'sml
  '(progn
     (define-key sml-mode-map (kbd "C-c C-p") 'sml-send-function)
     ))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "javascript, js")

(if (>= emacs-major-version 23)
    (add-to-list 'auto-mode-alist '("\\.js$" . js-mode))
  (add-to-list 'auto-mode-alist '("\\.js$" . javascript-generic-mode))
  )

(eval-after-load 'js '(modify-syntax-entry ?$ "w" js-mode-syntax-table))

;; BUG: all single char comments do not stop highlighting on end of line but
;; go to end of buffer. To fix use code:
;; (setq auto-mode-alist (rassq-delete-all 'js-mode auto-mode-alist))
;; (add-to-list 'auto-mode-alist '("\\.js$" . c++-mode))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "bat file, batch")

;; loaded from 'generic-x.el'

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "nsis-mode")

(when (fboundp 'nsis-mode)
  (add-to-list 'auto-mode-alist '("\\.\\(nsi\\|nsh\\)\\'" . nsis-mode))
  )

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "csharp, c-sharp")

(autoload 'csharp-mode "csharp-mode" "Major mode for editing C# code." t)
(add-to-list 'auto-mode-alist '("\\.cs$" . csharp-mode))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "java")

(add-hook 'java-mode-hook #'subword-mode)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "ECB")

(setq ecb-tip-of-the-day nil)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "htmlize")

(setq
 htmlize-html-charset "utf-8"
 htmlize-output-type 'inline-css
 htmlize-html-major-mode 'html-mode
 htmlize-convert-nonascii-to-entities nil)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "html")

(defun html-charref-escape-region (start end)
  (interactive "r")
  (save-excursion
    (save-restriction
      (narrow-to-region start end)
      (goto-char (point-min))
      (replace-string "&" "&amp;")
      (goto-char (point-min))
      (replace-string "<" "&lt;")
      (goto-char (point-min))
      (replace-string ">" "&gt;")
      )))

(defun html-charref-from-char (char)
  (format "&#%d;" char)
  )

(defun html-charref-from-string (string)
  (let ((res ""))
    (mapc
     (lambda (char) (setq res (concat res (html-charref-from-char char))))
     string)
    res
    ) )

(defun html-charref-escape-region2 (begin end &optional prefix)
  (interactive "r\nP")
  (if prefix
      (save-excursion
        (goto-char begin)
        (insert (html-charref-from-string (delete-and-extract-region begin end))))
    (html-charref-from-string (buffer-substring begin end))
    ))

(defun html-charref-to-string (html)
  (let ((res "") (pos 0))
    (while (string-match "&#\\([[:digit:]]+\\);" html pos)
      (setq res (concat res (string (string-to-int (substring html (match-beginning 1) (match-end 1)) 10))))
      (setq pos (match-end 0))
      )
    res
    ) )

(defun html-charref-unescape-region (begin end &optional prefix)
  (interactive "r\nP")
  (if prefix
      (save-excursion
        (goto-char begin)
        (insert (html-charref-to-string (delete-and-extract-region begin end))))
    (html-charref-to-string (buffer-substring begin end))
    ))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "nxml")

(setq nxml-sexp-element-flag t)
(setq nxml-child-indent 2)
(setq nxml-attribute-indent 4)

(add-to-list 'auto-mode-alist '("\.pom\\'" . nxml-mode))
(add-to-list 'auto-mode-alist '("\.xsd\\'" . nxml-mode))
(add-to-list 'auto-mode-alist '("\.rng\\'" . nxml-mode))
(add-to-list 'auto-mode-alist '("\.xul\\'" . nxml-mode))

(eval-after-load 'nxml-mode '(define-key nxml-mode-map [C-return] 'nxml-complete))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "relax ng, rng")

(eval-after-load 'rng-loc '(add-to-list 'rng-schema-locating-files "~/.emacs.d/rnc/schemas.xml"))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "web-mode")

(setq web-mode-markup-indent-offset 2
      web-mode-css-indent-offset 2
      web-mode-code-indent-offset 2
      web-mode-script-padding 2
      web-mode-style-padding 2)

(setq web-mode-enable-comment-keywords t)

(ignore-errors (require 'web-mode))
(when (featurep 'web-mode)
  (setq web-mode-engine-file-regexps (delq (assoc "jsp" web-mode-engine-file-regexps) web-mode-engine-file-regexps))
  (mapc (lambda (i) (add-to-list 'web-mode-engine-file-regexps i))
          '(("jsp" . "\\.tagf?\\'")
            ("jsp" . "\\.jspf?\\'")))
  (add-to-list 'auto-mode-alist '("\\.phtml\\'" . web-mode))
  (add-to-list 'auto-mode-alist '("\\.php\\'" . web-mode))
  (add-to-list 'auto-mode-alist '("\\.jspf?\\'" . web-mode))
  (add-to-list 'auto-mode-alist '("/WEB-INF/tags/.*\\.tagf?\\'" . web-mode))
  (add-to-list 'auto-mode-alist '("\\.as[cp]x\\'" . web-mode))
  (add-to-list 'auto-mode-alist '("\\.erb\\'" . web-mode))
  (add-to-list 'auto-mode-alist '("\\.php\\'" . web-mode))
  (add-to-list 'auto-mode-alist '("\\.mustache\\'" . web-mode))
  (add-to-list 'auto-mode-alist '("\\.djhtml\\'" . web-mode))
  (add-to-list 'auto-mode-alist '("\\.html?\\'" . web-mode)) )

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "psgml")

(setq my-html-template
      '("html"
        (nil
         "\n<head>" \n
         "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=" (read-input "Charset: ") "\">" \n
         "<title>" (setq str (read-input "Title: ")) "</title>\n"
         "</head>\n"
         "<body>\n<h1>" str "</h1>"
         "\n<address>" \n
         "<a href=\"mailto:" user-mail-address "\">" (user-full-name) "</a>" \n
         "</address>" \n
         "</body>\n"
         ))
      )

(setq sgml-set-face t)                  ; for highlighting in sgml
(eval-after-load 'sgml-mode
  '(progn
     (unless (featurep 'psgml)
       (setq html-tag-alist
             (cons
              my-html-template
              (my-filter
               (lambda (item) (not (equal (car item) "html")))
               html-tag-alist)))
       (add-to-list 'html-tag-alist '("script" (\n) ("type" "text/javascript") ))
       (add-to-list 'html-tag-alist '("style" (\n) ("type" "text/css") ))
       )))


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "jsp")

(define-derived-mode jsp-mode html-mode "JSP"
  "JSP editing mode. Redefine HTML comment syntax to JSP."
  (setq comment-start "<%--")
  (setq comment-end "--%>")
  (setq comment-start-skip "<[!%]--[ \t]*")
  (setq comment-end-skip "[ \t]*--[% \t\n]*>")
  (setq jsp-font-lock-syntactic-keywords
        '(("\\(<\\)%--" (1 "< b"))
          ("--%\\(>\\)" (1 "> b"))))
  (setcdr (assoc 'font-lock-syntactic-keywords font-lock-defaults) 'jsp-font-lock-syntactic-keywords) )

(unless (featurep 'web-mode)
  (add-to-list 'auto-mode-alist '("\\.jspf?\\'" . jsp-mode)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "sh, bash")

(add-to-list 'auto-mode-alist '("\\.cygport\\'" . shell-script-mode))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "pg, Proof General")

(setq proof-splash-enable nil)
;; (setq proof-toolbar-enable nil)

(setq
 isar-display:show-types t
 isar-display:show-sorts t
 isar-display:show-main-goal t
 isar-display:show-brackets t
 ;; Too many output, so commented:
 ;; isar-display:show-consts t
 )

(eval-after-load 'proof
  '(progn
     ;; (proof-maths-menu-toggle 1)
     ;; (unicode-tokens-mode 1)
     ;; (proof-imenu-toggle 1)
     ))
(eval-after-load 'isar
  '(progn
     (define-key isar-mode-map (kbd "C-c C-m") 'proof-goto-point)
     ))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "printing")

(setq ps-paper-type 'a4)

;; Use Notepad to print plain text files to the default Windows printer
;(setq lpr-command "notepad")
;(setq lpr-headers-switches '("/p"))    ; \ mis-use these
;(setq lpr-switches nil)                ; / two variables
;(setq printer-name nil)        ; notepad takes the default
;(setq lpr-printer-switch "/P") ;; run notepad as batch printer
;;

;; Printing to file.

;(setq printer-name "~/myprint.txt")
;(setq ps-printer-name nil)
;(setq ps-print-header nil)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "SQL")

(setq sql-password "")
(add-to-list 'auto-mode-alist '("\\.plsql\\'" . sql-mode))

;; Disable placeholders substitution in Oracle SQLi by Emacs itself.
;; This enable "define" and "&var" syntax.
(setq sql-oracle-scan-on nil)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "backuping")

(setq
 make-backup-files t
 ;; In other case (by renaming) you loose original file creation date.
 backup-by-copying t
 backup-directory-alist '(("." . "~/.backup")) ; don't litter my fs tree
 delete-old-versions t                         ; delete excess backup versions silently
 kept-old-versions 1                           ; store first original version
 kept-new-versions 3                           ; store last 3 version
 version-control t)                            ; use versioned backups

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "auto save")

(setq auto-save-default t)
;; If nil autosave to different than original to buffer file.
(setq auto-save-visited-file-name nil)
(setq auto-save-interval 300)
;; Note: if you kill unsaved file auto save file not deleted.
(setq delete-auto-save-files t)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Save and restore my buffers every time.

(setq desktop-base-file-name ".emacs.desktop")
(setq desktop-base-lock-name ".emacs.desktop.lock")
(when (eq system-type 'cygwin)
  (setq desktop-base-file-name ".emacs.desktop-cygwin")
  (setq desktop-base-lock-name ".emacs.desktop-cygwin.lock")
  )

(when (>= emacs-major-version 22)
  (desktop-save-mode 1)
  (setq
   desktop-globals-to-save
   (append
    '((file-name-history . 100)
      (compile-history . 100)
      (command-history . 100)
      (extended-command-history . 100)
      (shell-command-history . 100)
      (query-replace-history . 100)
      (regexp-history . 100)
      (grep-history . 100)
      (minibuffer-history . 100))
    desktop-globals-to-save))
  )

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(message "User welcome msg")

(add-hook 'emacs-startup-hook
          (lambda ()
            (let ( (mgs-list '("Welcome to emacs, the thermonuclear editor."
                               "You enter to Out Space. Emacs on."
                               "Nice day for Emacsing!")) )
              (message (nth (random (length mgs-list)) mgs-list)))))

(switch-to-buffer "*Messages*")
(setq default-directory "~/")
(switch-to-buffer "*scratch*")
(setq default-directory "~/")

;;; End loading...

;; Local variables:
;; mode: outline-minor
;; outline-regexp: "(message \""
;; End: