;; SYNOPSIS:  user configuration for gnu/X-emacs/linux/bsd/nt/95/sunos.
;; AUTHOR: GPL(C) Mohsin Ahmed, http://www.cs.albany.edu/~mosh

(load-library "mosh-vars.el")

(put 'eval-expression  'disabled nil)
(put 'narrow-to-region 'disabled nil)
(put 'downcase-region  'disabled nil)
(put 'upcase-region    'disabled nil)
(put 'erase-buffer     'disabled nil)
(put 'set-goal-column  'disabled nil)

;; (setq debug-on-error  t  )
;; (setq debug-on-error  nil)
;; (cancel-debug-on-entry)
;; Debugger:
;;    h:  help             b:  set bp  (*).
;;    d:  step into        u:  unset bp (*).
;;    bc: step to cursor   c:  continue to next bp?
;;    r:  return,          q:  quit

(setq
   ;  insert-default-directory  nil
   ;  default-directory         "~/"
      default-directory         (or (getenv "PWD") "~/")
      gc-cons-threshold         8000000    ; Garbage collect after 8Mb.
      enable-local-variables    "Query"    ; Ask before running embedded cmds.

      words-include-escapes       nil    ; For word motion.
      hscroll-step                1      ; columns to try scrolling
      scroll-step                 1      ; rows    to try scrolling
      sentence-end-double-space   nil    ; single space ends a sentence.

      fill-individual-varying-indent t    ; indent doesn't end para.

      next-line-add-newlines      nil    ; Don't add extra nl at buffer_end.
      require-final-newline       t      ; no more binary files edited.
      three-step-help             t      ; (control ?h) will go thru steps.
      kill-whole-line             t      ; (control ?k) at line-begin removes also \n.
                                         ; Now use (control ?D)el to delete to eoln.
      track-eol                   nil    ; if cursor on end-of-line ..

  ;   search-upper-case       'not-yanks ; Keep case, but not in yanks.
      search-highlight              t    ; color the string
      query-replace-highlight       t    ; color the strings

  ;   highlight-nonselected-windows nil  ; do not color other windows.
      default-tab-width             8    ; not 4, for verysys.
      comment-column                0    ; For elisp comments.
      make-backup-files             nil  ; No file~ backups.
      version-control               nil  ; Force backup with C-u C-x C-s

  ;   jka-compr-use-shell           nil
      parens-require-spaces         t     ; M-( inserts [space] )
      show-paren-mode               t
      mouse-yank-at-point           t
      x-select-enable-clipboard     nil   ; don't mix yank and clip.
                                          ; Changed simple.el
  ;   interprogram-cut-function     nil   ; 'w32-set-clipboard-data
      inhibit-startup-message       t
  ;   display-time-day-and-date     t
      line-number-mode              t
      save-place-file          "~/emacs.pos"
      bookmark-default-file    "~/emacs.bmk"
  ;   hide-ifdef-initially     t
      auto-save-default        nil        ; NFS is too slow.
      auto-save-interval       6000
      auto-save-list-file-prefix nil      ; no ~/.saves-*
      grep-find-command       "rgrep -f*.[ch]* -type:text -plain " ; occur/grep modes.
      line-number-display-limit       50000000 ; 50Mb
)

(setq-default
      truncate-lines         nil ; Don't put \ and wrap on screen.
      case-fold-search       t   ; Case InSensitive Search.
      case-replace           nil ; Replace only as told, no changes.
      completion-ignore-case t   ; Allow case mixing.
      
      fill-column            75  ; Wrap at this col. was 75
      fill-prefix            ""  ; set with (control ?x) .
      indent-tabs-mode       nil ; No tabs for indentation!
      save-place             t   ; Remember point, across sessions.

                                 ; used by tab-to-tab-stop
      tab-stop-list        (list
                             4  8 12 16 20 24 28 32 36 40
                             44 48 52 56 60 64 68 72 76 80 )
      calendar-time-display-form  '(24-hours ":" minutes am-pm)
   ;  url-be-asynchronous         't         ; for w3.el
)

; (setq shell-file-name     "bash")


;;; [[REQUIRES]]

(require 'hippie-exp)            ; Easy expansions abbrevs.
(require 'saveplace)             ; Save Places
; (require 'desktop)             ; Remember open buffers between sessions, see later.
(require 'paren)                 ; shows (mis)matching parens
; (require 'jka-compr)             ; Can edit file.[gz,Z]
(require 'icomplete)             ; M-x ... Shows completions in minibuffer.
(cond (gnuemacs
(require 'imenu)                 ; For M-x auto completions
(require 'misc)                  ; For 'copy-from-above-command
))
; (require 'bookmark)
; (require 'info)
; (require 'etags)
; (require 'time-stamp)
; (require 'cc-mode)
(require 'hideshow)              ; Expand/hiding { blocks }
(require 'hideif)                ; Expand/hiding #ifdef   #endif
; (require 'ediff)
(require 'rect)



(require 'mosh)    ; (load-library "~/emacs/mosh.el")
(require 'mosh-keyf);(load-library "~/emacs/mosh-keyf.el")
(require 'loops)   ; (load-library "~/emacs/loops.el")
(require 'mclip)   ; (load-library "~/emacs/mclip.el")
(require 'mword)   ; (load-library "~/emacs/mfile.el")
(require 'mfunc)   ; (load-library "~/emacs/mfunc.el")
(require 'align)   ; (load-library "~/emacs/align.el")
(require 'mcolors) ; (load-library "~/emacs/mcolors.el")
(require 'mdiff)   ; (load-library "~/emacs/mdiff.el")
(require 'mverilog); (load-library "~/emacs/mverilog.el")

(if gnuemacs
    (load-library "~/emacs/patches20.el")
)

; (load-library "~/emacs/mpas.el")     ; autoloaded
; (load-library "~/emacs/macrokey.el") ; autoloaded
; (load-library "~/emacs/scanundo.el") ; autoloaded
; (load-library "~/emacs/help-sym.el") ; autoloaded
; (load-library "~/emacs/c-proto.el")  ; autoloaded
; (load-library "~/emacs/indentel.el") ; autoloaded


(autoload 'macro-install      "macrokey"   ""  t)
(autoload 'mosh-scan-undos    "scanundo"   ""  t)
(autoload 'describe-symbols   "help-sym"   ""  t)
(autoload 'c-prototype        "c-proto"    ""  t)
(autoload 'mpas-mode          "mpas"       ""  t)
(autoload 'my-lisp-indent-line "indentel"  ""  t)

;; better than verilog-mode.el!

; (autoload 'verilog-mode       "verilog-mode"   ""  t) <- Too slow.
(autoload 'perl-mode          "perl-mode" ""  t) ; cperl-mode hangs

;;; [[SOUND]]
; Turn off: Control panel, Drivers, beep.sys, Diable.
; (set-message-beep 'hand)

;;; [[FONT]]
;; Font-menu: [S mouse-1]
;; Select:  (insert (prin1-to-string (w32-select-font)))
;; List:    (insert (prin1-to-string (x-list-fonts "*")))
;; See xfontsel, xlsfonts
;; xemacs can use "-Misc-Fixed-Medium-R-Normal--20-200-75-75-C-100-ISO8859-1"

(cond
 (w32emacs
  (set-default-font
  ; "-*-Fixedsys-normal-r-*-*-12-90-*-*-c-*-*-ansi-" ;; Large
  "-*-Courier-normal-r-*-*-17-102-120-120-c-*-iso8859-1" ;; small
  )
  (set-frame-size (selected-frame) 80 35)
 )
 ((and gnuemacs xwindows)
   ;(set-default-font "7x13")
    (set-default-font "10x20")
    (set-frame-size (selected-frame) 80 22)
    (set-face-underline-p 'region nil)
 )
 (ttyemacs
  (global-set-key [(control ?h)] 'backward-delete-char-untabify)
  (global-set-key "\eOP" 'help)              ; F1.
  (global-set-key "\eOQ" 'set-mark-command)  ; F2
  )
)

;;; [[COLORS]] grey99 is near white, grey0 is black
; (mosh-set-colors  BACKGROUND     FOREGROUND  MODELINE CURSOR BACKLIT/DARK)
;
; (mosh-set-colors "grey22" "grey85" "LightGoldenrod3" "yellow" 'dark)
; (mosh-set-colors "grey12" "grey85" "grey70"   "yellow" 'dark)
; (mosh-set-colors "DarkSlateGrey" "MistyRose" "tan"   "yellow" 'dark)
; (mosh-set-colors "grey60" "grey5" "grey10" "yellow" 'light)
; (mosh-set-colors "grey12" "grey85" "LightGoldenrod3" "yellow" 'dark)
;(xemacs
; (mosh-set-colors "grey12" "grey85" "LightGoldenrod3" "yellow" 'dark))

(cond
 (w32emacs
  ;(mosh-set-colors "DarkSlateGrey" "black" "grey30" "yellow" t)
  (mosh-set-colors "grey50" "black" "grey30" "yellow" t))
 (window-system
  (mosh-set-colors "grey60" "grey5" "lightyellow" "yellow" 'light))
)

(cond
 (window-system
  (set-frame-position (selected-frame) 10 10)
  (mosh-frame-title)
))


;;; [[MODELINE]]

(add-hook 'find-file-hooks 'mosh-find-file-hook)

(defun mosh-find-file-hook ()
  "This hook sets the modeline to full file name."
  (setq mode-line-buffer-identification 'buffer-file-name)
)

;;; [[MODES]]

(column-number-mode 1)
(icomplete-mode 1)

(cond (w32emacs
       (scroll-bar-mode  -1)
       (menu-bar-mode    -1))
      (gnuemacs
       (show-paren-mode 1))
)

;;; [[WRITE FILE HOOK]]
; (setq write-file-hooks nil)    ; For binary files.
; (setq kill-emacs-hook nil)     ; Incase you cannot quit, do (control ?x) (control ?e).
; (add-hook 'write-file-hooks 'mosh-trim-blanks)

;;; [[ETAGS]]
;   visit-tags-table
;   mosh-tag-dir
;   find-tag-other-window
;   find-tag
;   find-tag-other-window
;   (find-tag t t)          ; Find tag RE-defn.
;   tags-search
;   tags-query-replace


;;; [[MODE SETUP]]
; Defined in files.el
(setq auto-mode-alist (append auto-mode-alist '(
   ;;  ("\\.[ch]$"     .   c++-mode)
       ("\\.[CHch][CHch]?" .   c++-mode)
       ("\\.mf$"       .   latex-mode)
       ("\\.sli$"      .   slitex-mode)
       ("\\.pic$"      .   latex-mode)
       ("\\.st$"       .   smalltalk-mode)
       ("\\.nr$"       .   nroff-mode)
       ("emacs$"       .   emacs-lisp-mode)
       ("\\.cod$"      .   asm-mode)      ; NT cl /Fc
       ("\\.pro$"      .   prolog-mode)
       ("\\.p$"        .   prolog-mode)   ; Pascal?
       ("\\<p-"        .   cperl-mode)
       ("\\.cmd$"      .   cperl-mode)     ; For NT.
       ("\\.bat$"      .   cperl-mode)     ; For NT.
       ("\\.\\([pP][Llm]\\|al\\)$" . cperl-mode)
       ("\\.arc"       .   archive-mode)
       ("\\.v$"        .   mverilog-mode)
       ;;;  Let #!interpreter decide the mode,
       ;;;  See variable interpreter-mode-alist.
       ;;;  default should be last.
       ("part[0-9]$" .   text-mode)       ; FAQs.
      ))
)

;;; [[DIFF/EDIFF]]
; "x x" == "xx"?

(setq
 compare-windows-whitespace  "[ \t\n]+"  ; compare-windows default.
 compare-ignore-case           nil       ; case-sensitive
 diff-switches                 "-b"      ; vc.el, ignore blanks, was -c context.
 ediff-shell                   shell-file-name
 ediff-diff-options            "-w"      ; (ignore) -w space, -c case,
 ediff-ignore-similar-regions  t         ; don't bother about spaces.
 ediff-window-setup-function  'ediff-setup-windows-plain
 ;;                           'ediff-setup-windows-multiframe ; not on NT.
 ediff-force-faces            t
)

(setq-default
 ediff-highlight-all-diffs    t
)

;;; [[HIPPIE]]

(setq
 hippie-expand-verbose        t
 dabbrev-abbrev-char-regexp   "\\sw\\|\\s_"
 he-dabbrev-skip-space        t
)

(setq hippie-expand-try-functions-list          ; can be set for each mode.
    '(
      try-expand-dabbrev-visible                ; won't pick nearest first.
      try-expand-dabbrev                        ; dynamic abbrev
      try-expand-dabbrev-all-buffers
    ; try-expand-all-abbrevs                    ; Don't have any abbrevs.
      try-complete-file-name-partially
      try-complete-file-name                    ; Also on A-f
      try-expand-line
      try-expand-line-all-buffers
      try-expand-dabbrev-from-kill
      try-expand-whole-kill
    ; try-complete-lisp-symbol
    ; try-complete-lisp-symbol-partially
))


;;; [[TIMESTAMP]]
; (setq write-file-hooks nil)
; (setq time-stamp-start "Time-stamp: ")
; (setq time-stamp-end   "$")

; Turn off timestamping for speeding up saving.
(add-hook 'write-file-hooks 'time-stamp)

; (setq time-stamp-format '(mosh-name-date))

;;; [[PERL MODE]]
;; See lisp/cperl-mode-bad.el:1262 /^.if window-system/
;; cperl-array-face, cperl-hash-face

(add-hook 'cperl-mode-hook 'mosh-perl-mode)

(defun mosh-perl-mode ()
  "Perl mode customizations, M-h for perl help."
  (interactive)
  (require 'cperl-mode)

  (mosh-map-local-keys
     [(control up  )]   'mosh-prev-perl   ; perl-beginning-of-function
     [(control down)]   'mosh-next-perl   ; perl-end-of-function
     [(control ?j  )]   'cperl-linefeed
     [(alt   ?h    )]   'cperl-get-help
     [(super ?h    )]   'cperl-info-on-current-command
     [(meta  ?h    )]   'cperl-info-on-current-command ; M-h was mark-paragraph
     [(hyper ?h    )]   'cperl-info-on-command
     [       return ]   'newline-and-indent
     [\e ?\;        ]   'cperl-indent-for-comment
     [\e tab        ]   'cperl-indent-for-comment
  )

  (setq
        cperl-info-page         "Perl5"
        cperl-indent-level      4
        cperl-font-lock         t
        cperl-brace-offset     -2  ; puts '{' in same col as '}'.
      ; cperl-hairy             t
  )
  ; (font-lock-mode t)
)

;;; [[HTML MODE]]

(defun mhtml-mode ()          "mode for editing html."
  (interactive)
  (html-mode)
  (require 'mhtml)
  (setq mode-name "mhtml-mode")

  (mosh-map-local-keys
      [(control return)]  '(insert "<br>\n")
      [(shift   return)]  '(insert "<p>\n")
      [f12 ?/]    '(insert mhtml-comment)
      [f12 ?=]    '(insert mhtml-hrule)
      [f12 ?F]    'mhtml-region-to-file
      [f12 ?H]    '(insert mhtml-header)
      [f12 ?h]    '(insert mhtml-href)
      [f12 ?i]    '(insert mhtml-image)
      [f12 ?l]    '(insert mhtml-list)
      [f12 ?m]    '(insert mhtml-mark)
      [f12 ?p]    '(insert mhtml-page)
  )
)

;;; [[SHELL MODE]]
;;; For running vis. 98-12-10

(add-hook 'shell-mode-hook 'mosh-shell-hook)

(defun mosh-shell-hook ()
  (mosh-map-local-keys
                      ;; clean up the window.
   [?\e delete]      '(progn
                        (delete-region (point) (point-min))
                        ; (goto-char (point-max))
                     )
   [?\e home]        '(shell-command-on-region (point) (point-min)
                         "al-vis2" nil 'replace-region)
   [?\e end ]        '(shell-command-on-region (point) (point-min)
                         "al-vis1" nil 'replace-region)
   )
)

;;; [[PASCAL MODE]]

;;; [[ C++/C/CC-MODE ]]

(setq c++-mode-hook  'mosh-cc-mode)
(setq c-mode-hook    'mosh-c-mode)

(defun mosh-c-mode ()
  (c++-mode)
  (mosh-cc-mode)
)


(defun mosh-cc-mode () (interactive)
  "cc mode."

  (hs-minor-mode 1)
  (hs-show-all)
  ; (font-lock-mode)

  (modify-syntax-entry ?_ "w")
  (setq mosh-list-re   "[{}]\\|:$")

  (setq c-basic-offset              4)
  (setq c-block-comments-indent-p nil) ;; see lisp/cc-mode.el:313
  (setq c-recognize-knr-p         nil)
 ;(c-set-offset 'statement-cont    0)  ;; No No.
  (c-set-offset 'substatement-open 0)  ;; Keep '{' under 'if'.
  (c-set-offset 'arglist-close     0)  ;; Keeps '}' under match.
  (c-set-offset 'case-label       '*)  ;; case a: indent by half.
  (c-set-offset 'access-label     -2)  ;; public: hangs outside

  (mosh-map-local-keys
      [(control     up)]  'mosh-prev-c-defun
      [(control   down)]  'mosh-next-c-defun
      [(control return)]  'mosh-close-paren
      [         return ]  'newline-and-indent
      [(hyper   return)]  '(c-indent-region (point-min) (point-max))

      [ ?\e       home ]  'hide-ifdef-mode       ; Start/stop mode
      [ ?\e       left ]  'hide-ifdef-block      ; Collapse #if #endif
      [ ?\e      right ]  'show-ifdef-block      ; Expand   #if #endif
      [(super       up)]  'backward-ifdef        ; Previous #..
      [(super     down)]  'forward-ifdef         ; Next     #..

      [(shift     left)]  'hs-hide-block         ; Collapse {block}.
      [(shift    right)]  'hs-show-block         ; Expand   {block}.
      [(meta ?n)]         'next-error
  )
)

;;; [[ASSEMBLER]]

(add-hook 'asm-mode-hook 'mosh-asm-mode)

(defun mosh-asm-mode ()
  "Assembler mode customization"
  (message "Assembler mode")
  (modify-syntax-entry ?_ "w"   )    ; Underscore is a word.
 ;(setq tab-width 8)                 ; For .cod files, now default
 ;(mosh-no-tabs)
  (mosh-map-local-keys
       [(control up)]   'mosh-prev-asm-defun
       [(control down)] 'mosh-next-asm-defun
  )
)

;;; [[GDB/DEBUGGER]]

(add-hook 'gdb-mode-hook 'mosh-gdb-mode)

(defun mosh-gdb-mode ()
  "GDB mode customization, set it in the C++ file also!"
  (interactive)
  (mosh-map-local-keys
       [          up]  '(if (eobp) (comint-previous-input 1) (previous-line 1))
       [        down]  '(if (eobp) (comint-next-input     1) (next-line     1))
       [         f21]  'gud-next    ; Pause/break key.
  )
  (mosh-map-global-keys
       [(hyper right)] 'gud-step    ; step into.
       [(hyper down)]  'gud-next    ; step over to next stmt.
       [(hyper left)]  'gud-finish  ; finish current function
       [(hyper  f12)]  'gud-print   ; print object under cursor.

       [(super left )] 'gud-up
       [(super right)] 'gud-down
  )
  ; (load-file "patches20.el") ; for color arrow in gdb.
)

;;; [[GNUS]]
; See http://www.cs.washington.edu/homes/voelker/ntemacs.html

(setq gnus-select-method '(nntp "nntp.maxim.net"
    (nntp-port-number 119)))

;;; [[SMTP]]
(setenv "MAILHOST" "smtp2.maxim.net")    ; for POP3.
(setq smtpmail-default-smtp-server "smtp2.maxim.net")
(setq smtpmail-local-domain nil)
(setq send-mail-function 'smtpmail-send-it)
(require 'smtpmail)
(if gnuemacs (require 'mailalias))

;;; [[RMAIL]]

(defun mosh-rmail-hook ()
  "Rmail setup, RMAIL options only for saspc128."
  (interactive)
  (setq rmail-display-summary t)
  (setq    rmail-primary-inbox-list '("po:mosh") rmail-pop-password-required t)
  (define-key rmail-summary-mode-map [kp-prior] 'scroll-other-window-down)
  (define-key rmail-summary-mode-map [kp-next ]  'scroll-other-window  )
)

;; mailcrypt options.
(if (file-readable-p (concat emacs_dir "/lisp/mailcrypt.el"))
    (progn
      (require 'mailcrypt)
      (add-hook 'rmail-mode-hook 'mc-install-read-mode)
      (add-hook 'rmail-summary-mode-hook 'mc-install-read-mode)
      (add-hook 'mail-setup-hook 'mc-install-write-mode)
      ;; (setq mc-remailer-pseudonyms '("Elvis Presley" "Vanna"))
    )
)

;;; [[TEX/LATEX/MF]]

(add-hook 'latex-mode-hook 'mosh-latex-mode-hook)

(defun mosh-latex-mode-hook ()
  "Latex Mode, make _ and backslash part of the word."
  (modify-syntax-entry ?\\ "w")
)

;;; [[TEXT]]
(add-hook 'text-mode-hook 'mosh-text-hook)

(defun mosh-text-hook ()
  (mosh-map-local-keys
   [(control   up)]   'mosh-prev-section
   [(control down)] 'mosh-next-section
   [(shift    end)] 'forward-sentence
   [(shift   home)] 'backward-sentence
))

;;; [[ELISP MODE]]

(defun mosh-lisp-hook ()
  ;; (require 'lisp-patch)
  (define-key shared-lisp-mode-map "\t" 'my-lisp-indent-line)
  (setq indent-line-function 'my-lisp-indent-line)

  (mosh-map-local-keys
       [(control up)]      'mosh-prev-lisp-defun
       [(control down)]    'mosh-next-lisp-defun
       [return]            'newline-and-indent
  )
)

(add-hook 'emacs-lisp-mode-hook       'mosh-lisp-hook)
(add-hook 'lisp-interaction-mode-hook 'mosh-lisp-hook)
(add-hook 'lisp-mode-hook             'mosh-lisp-hook)



;;; [[DESKTOP/SAVEPLACE]]
(require 'desktop)

(setq desktop-missing-file-warning nil)
(setq desktop-basefilename  "hist/emacs.dsk")

(setq desktop-globals-to-save (list
    'command-history
    'extended-command-history
    'file-name-history
    'shell-command-history
))

(desktop-load-default)
(desktop-read)
(add-hook 'kill-emacs-hook
          'mosh-desktop-save
          'bookmark-save)

(defun mosh-desktop-save () (desktop-save "~/"))



;;; [[KEYS-DEFINED]]
;; Console/Text mode keys.

(cond (ttyemacs
  (global-set-key [(control ?x)(control ?m)] 'tmm-menubar)
  (global-set-key [(control ?=)]  'mosh-match-paren-other-window)
  ;; (swap-bs-and-del)                       ; Not on linux console.
))

;;; [[KBD]]
;; +--------------------------------------------------------------------------
;; |    Macro            s   A   H  Mark Goto Copy Ffile Paste
;; |[f1  f2   f3  f4]  [f5  f6  f7  f8]  [f9   f10 f11   f12] [Prt Scrl Pause]
;; | [S  ]                    [  S  ] |
;; | [C f13 M][ Space       ][ M s h] |
;; +----------------------------------+
;; | NL  kp-/   kp-*   kp--] Stop     |
;; |     Undo   Eval  [kp-+] Bury     |
;; |                 [Enter] Search   |
;; +--------------------------------------------------------------------------

(define-key function-key-map [f5] 'event-apply-super-modifier)
(define-key function-key-map [f6] 'event-apply-alt-modifier)
(define-key function-key-map [f7] 'event-apply-hyper-modifier)
(define-key function-key-map [kp-enter] 'event-apply-control-modifier)

(mosh-map-global-keys
       [(super   ?5)]  'query-replace           ; Also M-% == S-M-5.
       [(alt     ?5)]  'query-replace-regexp
       [(hyper   ?5)]  'tags-query-replace
       [(alt     ?4)]  '(progn (setq tab-width 4) (recenter))
       [(alt     ?8)]  '(progn (setq tab-width 8) (recenter))
       [(meta    ?=)]  'mosh-fill-to-column
       [(alt     ?j)]  'join-lines
       [(alt     ?q)]  '(fill-paragraph '1)     ; Now on M-q also.
       [(alt     ?s)]  'shell
       [(meta    ?t)]  '(progn (transpose-lines 1) (previous-line 1)) ; A<>M.
       [(meta    ?q)]  '(fill-paragraph '1)     ; Now on M-q and A-q also.
       [(meta    ?.)]  'find-tag                ; predefined.
       [(meta    ?')]  'mosh-tag-dir            ; Set default dir.
       [(meta    ?\;)] '(find-tag last-tag t)   ; Find tag RE-defn. No Regexp!
       [(meta    ?/)]  'tags-search             ; Grouped by kbd positions.
       [(meta    ?,)]  'tags-loop-continue
       [(meta    ?`)]  'mosh-case-word
       [(meta    ?w)]  'mosh-copy-region
       [(meta    ?y)]  'mosh-yank-pop
       [(control ?`)]  'mosh-toggle-case
       [(control ?y)]  'mosh-yank

       [(alt     ?i)]  '(mosh-toggle-var     truncate-lines)
       [(super   ?i)]  '(mosh-toggle-var     completion-ignore-case)
       [(hyper   ?i)]  '(mosh-toggle-var     case-fold-search)
       [(hyper   ?I)]  '(mosh-toggle-var-def case-fold-search) ; does it toggle?

       [(hyper   ?/)]  'dabbrev-completion
       [(super   ?/)]  'dabbrev-expand
       [(alt     ?/)]  'imenu
       [(meta    ? )]  'mosh-delete-whitespace
       [(alt     ? )]  'just-one-space
       [(hyper   ? )]  'mosh-justify-right    ; Spaces to justify to fill-col
       [(super   ? )]  'indent-relative       ; align above.
       [(control ?>)]  '(mosh-tab-one)                  ; S-C-.
       [(control ?<)]  '(mosh-untab-one)                ; S-C-,
       [?\e      ?g ]  'goto-line
       [?\e      ?\e]  'execute-extended-command ; ie. M-x
)

;;; [[Control-X prefix]]

(mosh-map-global-keys
        [(control ?x) (control ?x)]  'mosh-exchange-mark-and-point
        [(control ?x) ?w]     'mosh-which-func
        [(control ?x) ?  ]    'mosh-align-rectangle
        [(control ?x) ?|]     'mosh-align-table
)

;;; [[KEYPAD]]

(mosh-map-global-keys
        [(meta kp-insert)]       'toggle-read-only
        [      kp-insert ]       'overwrite-mode

        [         kp-add ]       'bury-buffer
        [(control kp-add)]       'repeat-complex-command
        [(hyper   kp-add)]       'command-history-mode

        [         kp-multiply ]  'eval-defun
        [(shift   kp-multiply)]  'eval-region
        [(control kp-multiply)]  'eval-buffer
        [(super   kp-multiply)]  '(byte-compile-file buffer-file-truename t)

        [         kp-divide ]    'advertised-undo
        [(control kp-divide)]    'mosh-scan-undos
        [(meta    kp-divide)]    '(progn
                                    (revert-buffer nil 'no-confirm)
                                    (message "reverted"))

        [         kp-subtract ]  'mosh-stop
        [(control kp-subtract)]  'mosh-kill-junk
        [(hyper   kp-subtract)]  '(progn
                                   (setq kill-emacs-hook nil) (kill-emacs))
        [?\e      kp-subtract]     'save-buffers-kill-emacs

        [         kp-space ]     'mosh-match-paren-other-window
        [(control kp-space)]     'mosh-exchange-mark-and-point

        [         backspace]   'backward-delete-char-untabify ; Confuses Isearch.
        [(alt     backspace)]  'mosh-eat-whitespace-backward
        [(meta    backspace)]  'backward-kill-word      ; kills too much.
        [(control backspace)]  '(mosh-kill-words -1)
        [(shift   backspace)]  '(mosh-kill-line -1)    ; Kill to start of line

        [         delete ]    'delete-char
        [(control delete)]    '(mosh-kill-words 1)
        [(meta    delete)]    'mosh-trim-blanks

        [         kp-delete ] 'delete-char
        [(alt     kp-delete)] 'mosh-delete-whitespace
        [(meta    kp-delete)] 'kill-word                  ; kills too much.
        [(control kp-delete)] '(mosh-kill-words 1)
        [(shift   kp-delete)] 'kill-line                  ; del to eoln.

     ;; [         tab ]   'tab-to-tab-stop       ; Set by each mode.
     ;; [(meta    tab)]   ; Used by NT.
        [(shift   tab)]   'tab-to-tab-stop       ; S-Tab == C-q TAB.
        [(control tab)]   'hippie-expand
        [          f14]   'hippie-expand         ; UNDO key/bombay.
     ;; [(hyper   tab)]   'hippie-complete-file
        [(hyper   tab)]   'mosh-ispell-expand
        [(alt     tab)]   '(let ((case-fold-search nil)) (hippie-expand 1))
        [(super   tab)]   'ispell-complete-word

     ; Comment Column: C-x; to set it, Esc; to create/move to it, and
     ;                 Esc C-j to use/dup it below.

     ;; ALIGN, 'mosh-tab-to-column Spaces to col i or fill-col
)

;;; [[ARROW KEYS]]

(mosh-map-global-keys

 ;; HOME/END

      [          home ]  '(move-to-window-line '+0)      ; Window top/end.
      [          end  ]  '(move-to-window-line '-1)
      [       kp-end  ]  'end-of-line
      [       kp-home ]  'beginning-of-line              ; Line Home/End

  ;;  [(shift kp-home)]  'backward-sexp                  ; <= sexp
  ;;  [(shift kp-end )]  'forward-sexp                   ; => sexp
  ;;  [(hyper kp-home)]  'backward-list                  ; <= block.
  ;;  [(hyper kp-end )]  'forward-list                   ; => block.
  ;;                     'down-list                      ; => into next ().
      [(super kp-home)]  'backward-up-list               ; <= out of () to (
      [(super kp-end)]   'up-list                        ; => out of () to ).


     ;[            down ]       predefined.
     ;[            up   ]       predefined.
      [(meta       down)]  '(mosh-vert-up -1)
      [(meta       up  )]  'mosh-vert-up
      [?\e         down ]  '(mosh-vert-up -1)
      [?\e         up   ]  'mosh-vert-up

      [(control kp-up  )]      'beginning-of-defun
      [(control kp-down)]      '(end-of-defun)
      [(alt     kp-up  )]      '(move-to-window-line '+0)
      [(alt     kp-down)]      '(move-to-window-line '-1)
      [(hyper   kp-up  )]      '(scroll-other-window-down 1)
      [(hyper   kp-down)]      '(scroll-other-window      1)
      [(super   kp-up  )]      '(recenter  1)
      [(super   kp-down)]      '(recenter -2)
      [(        shift   kp-8   )] '(scroll-down 1)
      [(        shift   kp-2   )] '(scroll-up   1)
      [(control meta    kp-up  )] '(mosh-compare-lines -1)
      [(control meta    kp-down)] '(mosh-compare-lines  1)

  ;; PAGE-UP/PAGE-DOWN

      [(control next )]  'end-of-buffer
      [(control prior)]  'beginning-of-buffer

      [          kp-next ]   'scroll-up
      [          kp-prior]   'scroll-down
      [(control  kp-next )]  'end-of-buffer
      [(control  kp-prior)]  'beginning-of-buffer
      [(meta     kp-next )]  'scroll-other-window
      [(meta     kp-prior)]  'scroll-other-window-down
      [(hyper    kp-next )]  'forward-page
      [(hyper    kp-prior)]  'backward-page

      [(meta     kp-left )]  'scroll-right
      [(meta     kp-right)]  'scroll-left
      [(shift    kp-left )]  '(mosh-color-word -1 'bold)
      [(shift    kp-right)]  '(mosh-color-word  1 'bold)
      [             left  ]  'backward-char
      [             right ]  'forward-char
      [(control     left )]  '(mosh-move-word -1 t)
      [(control     right)]  '(mosh-move-word  1 t)

      [         kp-left  ]  'backward-char
      [         kp-right ]  'forward-char
      [(control kp-left )]  'backward-word
      [(control kp-right)]  'forward-word

      [(meta       left )]   'copy-from-above-command
      [(meta       right)]   '(copy-from-above-command 1)
      [?\e         left  ]   'copy-from-above-command
      [?\e         right ]   '(copy-from-above-command 1)

      [(control meta left )]  '(mosh-convex-align -1 1)
      [(control meta right)]  '(mosh-convex-align  1 1)
      [(shift   meta left )]  '(mosh-convex-align -1 -1)
      [(shift   meta right)]  '(mosh-convex-align  1 -1)
)

;;; [[FUNCTION KEYS/PAUSE/CLIPBOARD]]

(mosh-map-global-keys
    [f2]          'macro-install
    [pause]       'save-buffer
    [mouse-3]     'mosh-paste-clip
)

;; (define-key global-map [f3] 'Control-X-prefix)
(define-key help-map "S" 'describe-symbols)

;;; [[[SEARCH]]]
;; Do isearch with just keypad.
;;
;; [SEARCH] was [scroll] but not in 1933.
;; (global-set-key [SEARCH]                   'isearch-forward)
;; (define-key isearch-mode-map [SEARCH]      'isearch-repeat-forward)
;;
;; With this you can yank a lot, then edit off extra stuff.
;; Now C-u C-s is same as C-s .. C-u,  for regexp isearch.
;; "C-s C-w ... C-x C-x"   to mark a series of words.
;;
;; [SEARCH]  to start search.
;; [right]   to yank words,
;; [up/down] to (re)search,
;; [left]    to exit.
;; [stop]    to abort,

(add-hook 'isearch-mode-end-hook 'mosh-which-func)

(defun mosh-isearch-exit-copy ()
  "Stops isearch, and copies region from point to search start,
   and goes back to search start. On [isearch kp-delete]."
   (interactive)
   (isearch-exit)
   (mosh-copy-region)
)


(mosh-map-mode-keys    isearch-mode-map

        [backspace  ]  'isearch-delete-char         ;; Delete
        [delete     ]  'isearch-abort               ;; Stop
        [kp-enter   ]  'isearch-repeat-forward      ;; Next
        [kp-add     ]  'isearch-exit                ;; Ok.
        [kp-delete  ]  'mosh-isearch-exit-copy      ;; Copy
        [kp-subtract]  'isearch-abort               ;; Stop
        [kp-space   ]  'isearch-edit-string         ;; Edit
        [kp-multiply]  'isearch-toggle-regexp       ;; *
        [kp-divide  ]  'isearch-toggle-case-fold    ;; Case

        [         up    ]   'isearch-repeat-backward
        [         down  ]   'isearch-repeat-forward
        [         left  ]   'isearch-exit
        [         right ]   'isearch-yank-word
        [(control right)]   'isearch-yank-char

        [      kp-right ]   'isearch-yank-word
        [      kp-left  ]   'isearch-exit
        [      kp-up    ]   'isearch-repeat-backward
        [      kp-down  ]   'isearch-repeat-forward
        [      kp-home  ]   'isearch-ring-advance
        [      kp-end   ]   'isearch-ring-retreat

        [f12]         'mosh-which-func

        [?\C-e]       'isearch-edit-string
        [?\C-u]       'isearch-toggle-regexp
        [?\C-c]       'isearch-toggle-case-fold
        [?\C-x]       'isearch-exit
)


(mosh-map-global-keys
 ;; Like vim's #* search word under cursor.
 [f7 f7] '(re-search-backward (concat "\\<" (current-word) "\\>"))
 [f8 f8] '(progn
            (mosh-color-regexp (current-word))
            (re-search-forward  (concat "\\<" (current-word) "\\>")))
)

(cond  (gnuemacs
  (mosh-map-mode-keys  minibuffer-local-isearch-map
          [(control up  )]   'isearch-ring-retreat-edit
          [(control down)]   'isearch-ring-advance-edit
          [         up   ]   'isearch-reverse-exit-minibuffer
          [         down ]   'isearch-reverse-exit-minibuffer
  )
  ; Emacs20 fixes C-tab, which must be hippie-expand for us.
  ; See (describe-symbols "minibuffer.*map")
  (mosh-map-mode-keys minibuffer-local-completion-map
         [(control tab)]    'hippie-expand
         [(meta   tab)]     'file-cache-minibuffer-complete ; was on C-tab.
  )
  (mosh-map-mode-keys minibuffer-local-map
         [(control tab)]     'hippie-expand
         [(meta    tab)]     'file-cache-minibuffer-complete ; was on C-tab.
  )

  (mosh-map-mode-keys minibuffer-local-ns-map
         [(control tab)]     'hippie-expand
         [(meta    tab)]     'file-cache-minibuffer-complete ; was on C-tab.
  )
  (mosh-map-mode-keys minibuffer-local-must-match-map
         [(control tab)]     'hippie-expand
         [(meta    tab)]     'file-cache-minibuffer-complete ; was on C-tab.
  )
))

;;; [[ Prefix keys ]].

(global-unset-key [f8 ])
(global-unset-key [f9 ])
(global-unset-key [f10])
(global-unset-key [f11])
(global-unset-key [f12])

(mosh-key-prefix
 [f8 ]     "mark"
 [f9 ]     "goto"
 [f10]     "copy"
 [f11]     "findfile"
 [f12]     "paste"
 [f22]     "color"
)

(mosh-map-global-keys

 ;; [[MARK f8]]
 [f8 ?1]     'mosh-note-align-column
 [f9 ?1]     '(mosh-tab-to-column mosh-align-column)

 ;; [[JUMP f9]]
 [f9 ?d]   'mosh-compare-windows ; jump to difference.
 [f9 ?m]   '(mosh-mark-key 1)
)

;;; [[FIND-FILE f11]]

(mosh-map-global-keys
    [f11 f8 ]   'dired
    [f11 f9 ]   'mosh-err-file
    [f11 f10]   'mosh-file-here
    [f11 f11]   'find-file
    [f11 f12]   'find-file-read-only
)

(map-key-findfile
    [f11 ?c]    "~/cc/Text/c-ref.txt"
    [f11 ?e]    "~/emacs/emacs20.el"
    [f11 ?E]     emacs_dir
    [f11 ?h]    "~/cc/Text/html-ref.txt"
    [f11 ?i]    "~/init/index.txt"
    [f11 ?l]    "~/cc/Text/log-ref.txt"
    [f11 ?m]    "~/emacs/mosh.el"
    [f11 ?v]    "~/cc/Text/veri-ref.txt"
)

;;; [[ COPY f10 ]]

(mosh-map-global-keys
    [f10 f10]   'mosh-copy-region
    [f10 ?l]    '(mosh-copy-thing 'line)       ;; Paste with f12 l.
    [f10 ?c]    'mosh-copy-to-clip
    [f10 ?f]    '(mosh-copy-thing 'file-name)
    [f10 ?R]    'copy-rectangle-to-register
    [f10 ?w]    '(mosh-copy-thing 'word)
)

;;; [[ PRINT PASTE f12 ]]

(mosh-map-global-keys
     [f12 ?\C-p]  'print-buffer
     [f12 ?\C-r]  'print-region

     [f12 f12]    'mosh-which-func
     [f12  ?c]    'mosh-paste-clip
     [f12  ?d]    '(insert (format-time-string "%y-%m-%d"))
     [f12  ?D]    '(insert (current-time-string))
     [f12  ?e]    '(insert "mailto:" user-mail-address)
     [f12  ?f]    '(insert (buffer-file-name))   ; ./filename, not .\\truename
     [f12  ?F]    '(insert "FCC: ~/mail/sent\n")
     [f12  ?h]    'mosh-insert-header
     [f12  ?i]    '(insert isearch-string)
     [f12  ?m]    '(insert (mosh-name-date))
     [f12  ?M]    '(insert user-login-name)
     [f12  ?p]    '(insert mosh-copy-thing)
     [f12  ?n]    '(insert user-full-name)
     [f12  ?R]    'insert-register

     [f12  ?t]    '(insert (format-time-string "%R")) 
     [f12  ?T]    '(insert "Time-stamp: \" \"")
     [f12  ?u]    '(insert user-url)
     [f12  ?v]    'view-register
     [f12  ?w]    '(insert (mosh-which-func))

     [f12  ?\C-b]  'ps-print-buffer
     [f12  ?\C-r]  'ps-print-region
     [f12  ?\C-D]  '(insert mosh-main-debug)
     [f12  ?\C-W]  'mosh-insert-trace

     [f12 return]   '(insert "\nfcc: ~/nsmail/emacs.sent")

)

;;; [[ COLOR f22 (PrintScreen on sunkbd) ]]

(mosh-map-global-keys
    [f22 f22  ]  'font-lock-fontify-buffer
    [f22 ?/   ]  'mosh-color-regexp
    [f22 ?\t  ]  'mosh-color-tabs
    [f22 ?F   ]  'font-lock-mode
    [f22 ?R   ]  'mosh-color-rectangle
    [f22 ?m   ]  '(mosh-color-match 'green)
    [f22 ?r   ]  'mosh-color-region
    [f22 ?l   ]  '(mosh-color-line 'blue)
    [f22 ?W   ]  '(mosh-color-word -1 'bold)
    [f22 ?w   ]  '(mosh-color-word  1 'bold)
)

(mosh-map-global-keys
    [f13]        'mosh-mark-key           ; [[ PROPs Key]]
    [f19]        'isearch-forward-regexp  ; [[ FIND SEARCH f19 ]]
)

;;;EOF

