;;==============================================================================
;;; keys.el
;;
;;; Emacs Key Bindings
;;
;;; Author: Kyle W T Sherman
;;
;; Time-stamp: <2008-07-24 11:14:39 (kyle)>
;;==============================================================================

(message ";;; keys --> Start")

;;==============================================================================
;;; Global Keys
;;==============================================================================

(message ";;; keys --> Global Keys")

;;------------------------------------------------------------------------------
;; Function Keys
;;------------------------------------------------------------------------------

(message ";;; keys --> Function Keys")

;; function keys
(when (functionp 'help)
  (global-set-key [(f1)] 'help))        ; defaults to `help-for-help'
(when (functionp 'help-for-help)
  (global-set-key [(shift f1)] 'help-for-help)) ; defaults to undefined
(when (functionp 'help-command)
  (global-set-key [(f2)] 'help-command)) ; defaults to Prefix Command
(when (functionp 'kmacro-start-macro-or-insert-counter)
  (global-set-key [(f3)] 'kmacro-start-macro-or-insert-counter)) ; defaults to `kmacro-start-macro-or-insert-counter'
(when (functionp 'kmacro-end-or-call-macro)
  (global-set-key [(f4)] 'kmacro-end-or-call-macro)) ; defaults to `kmacro-end-or-call-macro'
(when (functionp 'kmacro-end-and-call-macro)
  (global-set-key [(f5)] 'kmacro-end-and-call-macro)) ; defaults to undefined
(when (functionp 'ispell-word)
  (global-set-key [(f6)] 'ispell-word)) ; defaults to undefined
(when (functionp 'ispell)
  (global-set-key [(shift f6)] 'ispell)) ; defaults to undefined
(when (functionp 'mode-compile)
  (global-set-key [(f7)] 'mode-compile)) ; defaults to undefined
;; (when (functionp 'cycle-buffer-backward-permissive)
;;   (global-set-key [(f8)] 'cycle-buffer-backward-permissive)) ; defaults to undefined
(when (functionp 'flymake-goto-next-error)
  (global-set-key [(f8)] 'flymake-goto-next-error)) ; defaults to undefined
(when (functionp 'cycle-buffer-backward)
  (global-set-key [(f9)] 'cycle-buffer-backward)) ; defaults to undefined
(when (functionp 'cycle-buffer-backward-permissive)
  (global-set-key [(shift f9)] 'cycle-buffer-backward-permissive)) ; defaults to undefined
(when (functionp 'cycle-buffer)
  (global-set-key [(f10)] 'cycle-buffer)) ; defaults to `tmm-menubar'
(when (functionp 'cycle-buffer-permissive)
  (global-set-key [(shift f10)] 'cycle-buffer-permissive)) ; defaults to undefined

;;------------------------------------------------------------------------------
;; Extended Keys
;;------------------------------------------------------------------------------

(message ";;; keys --> Extended Keys")

;; ;; shift arrow keys
;; (define-key function-key-map "\eO2A" [S-up])
;; (define-key function-key-map "\eO2B" [S-down])
;; (define-key function-key-map "\eO2D" [S-left])
;; (define-key function-key-map "\eO2C" [S-right])

;; buffer switching
(when (functionp 'cycle-buffer)
  (global-set-key [(control left)] 'cycle-buffer-backward)
  (global-set-key [(control right)] 'cycle-buffer)
  (global-set-key [(control up)] 'cycle-buffer-backward-permissive)
  (global-set-key [(control down)] 'cycle-buffer-permissive)
  (global-set-key "\M-Od" 'cycle-buffer-backward)
  (global-set-key "\M-Oc" 'cycle-buffer)
  (global-set-key "\M-Oa" 'cycle-buffer-backward-permissive)
  (global-set-key "\M-Ob" 'cycle-buffer-permissive))
;; these default to undefined

;; split move
(when (functionp 'split-move-up)
  (global-set-key [(meta up)] 'split-move-up)
  (global-set-key [?\e (up)] 'split-move-up))
(when (functionp 'split-move-down)
  (global-set-key [(meta down)] 'split-move-down)
  (global-set-key [?\e (down)] 'split-move-down))
;; these default to undefined

;; beginning of line
(global-set-key [(home)] 'beginning-of-line) ; defaults to `move-beginning-of-line'

;; end of line
(global-set-key [(end)] 'end-of-line) ; defaults to `move-end-of-line'

;; beginning of buffer
(global-set-key [(control home)] 'beginning-of-buffer) ; defaults to undefined

;; end of buffer
(global-set-key [(control end)] 'end-of-buffer) ; defaults to undefined

;; cycle buffer
(when (functionp 'cycle-buffer)
  (global-set-key [(control tab)] 'cycle-buffer)) ; defaults to undefined

;; cycle buffer backward
(when (functionp 'cycle-buffer-backward)
  (global-set-key [(control shift tab)] 'cycle-buffer-backward)) ; defaults to undefined

;; ;; undo only
;; (global-set-key [(control plus)] 'undo-only) ; defaults to undefined

;;------------------------------------------------------------------------------
;; Modified Standard Keys
;;------------------------------------------------------------------------------

(message ";;; keys --> Modified Standard Keys")

;; set C-h to backward delete so the backspace key works (for old laptops)
;;(global-set-key "\C-h" 'backward-delete-char) ; defaults to `help-command'
;;(global-set-key "\C-h" 'help) ; defaults to `help-command'

;; modify backspace
;;(global-set-key "\C-h" 'backward-delete-char-untabify) ; defaults to `help-command'
;;(define-key isearch-mode-map "\C-h" 'isearch-delete-char) ; defaults to `help-command'

;; newline and indent
;;(global-set-key [(control return)] 'newline-and-indent) ; defaults to `self-insert-command'
;;(global-set-key [(return)] 'newline-and-indent) ; defaults to `self-insert-command'
;;(global-set-key "\C-m" 'newline-and-indent) ; defaults to `self-insert-command'
;;(global-set-key (kbd "RET") 'newline-and-indent) ; defaults to `self-insert-command'
(global-set-key "\r" 'newline-and-indent) ; defaults to `self-insert-command'

;; ;; indent or expand (tab)
;; ;;(global-set-key [(tab)] 'indent-or-expand) ; defaults to `lisp-indent-line'
;; (global-set-key "\C-i" 'indent-or-expand) ; defaults to `lisp-indent-line'
;; (global-set-key (kbd "TAB") 'indent-or-expand) ; defaults to `lisp-indent-line'

;; ;; indent line and next line
;; (global-set-key "\C-xt" 'indent-down) ; defaults to undefined

;; set mark
(global-set-key (kbd "M-SPC") 'set-mark-command) ; defaults to `just-one-space'

;; help
;;(global-set-key "\C-x\C-h" 'help) ; defaults to undefined
(global-set-key "\C-x\C-h" 'help-command) ; defaults to undefined
;;(global-set-key "\C-c\C-h" 'help-command) ; defaults to undefined
(global-set-key "\C-x?" 'help)          ; defaults to undefined

;; ;; cursor movement keys (single character)
;; (global-set-key "\M-j" 'backward-char) ; defaults to `indent-new-comment-line'
;; (global-set-key "\M-l" 'forward-char)   ; defaults to `downcase-word'
;; (global-set-key "\M-i" 'previous-line)  ; defaults to `tab-to-tab-stop'
;; (global-set-key "\M-k" 'next-line)      ; defaults to `kill-sentence'

;; ;; cursor movement keys (multi-character)
;; (global-set-key "\C-\M-j" 'backward-word) ; defaults to `indent-new-comment-line'
;; (global-set-key "\C-\M-l" 'forward-word) ; defaults to `reposition-window'
;; ;;(global-unset-key "\C-\M-i")
;; ;;(global-unset-key (kbd "C-M-i"))
;; ;;(global-unset-key (kbd "M-TAB"))
;; ;;(global-set-key "\M-\t" 'scroll-down-plus) ; defaults to `lisp-complete-symbol'
;; (global-set-key "\C-\M-i" 'scroll-down-plus) ; defaults to `lisp-complete-symbol'
;; (global-set-key "\C-\M-k" 'scroll-up-plus) ; defaults to `kill-sexp'
;; (global-set-key "\C-\M-u" 'beginning-of-line) ; defaults to `backward-up-list'
;; (global-set-key "\C-\M-o" 'end-of-line) ; defaults to `split-line'

;; describe function or variable at point
(when (functionp 'describe-function-or-variable-at-point)
  (global-set-key "\C-hz" 'describe-function-or-variable-at-point) ; defaults to undefined
  (global-set-key "\C-x\C-hz" 'describe-function-or-variable-at-point)) ; defaults to undefined

;; alternates for \M-x
(global-set-key "\C-x\C-m" 'execute-extended-command) ; defaults to Prefix Command
(global-set-key "\C-c\C-m" 'execute-extended-command) ; defaults to undefined
(global-set-key "\M-a" 'execute-extended-command) ; defaults to `backward-sentence'

;; ibuffer
(when (functionp 'ibuffer)
  (global-set-key "\C-x\C-b" 'ibuffer)) ; defaults to `list-buffers'

;; ;; mark whole buffer
;; (global-set-key "\C-xh" 'mark-whole-buffer) ; defaults to `mark-whole-buffer'

;; ;; source .emacs file
;; (global-set-key "\C-c=" '(load-library "~/.emacs")) ; defaults to undefined

;; regular expression searches
(when (functionp 'isearch-forward-regexp)
  (global-set-key "\M-s" 'isearch-forward-regexp)) ; defaults to undefined
(when (functionp 'isearch-backward-regexp)
  (global-set-key "\M-r" 'isearch-backward-regexp)) ; defaults to `move-to-window-line'

;; query replace regexp
(global-set-key "\M-&" 'query-replace-regexp) ; defaults to `query-replace'

;; insert menu prompt
(when (functionp 'insert-menu-prompt)
  (global-set-key "\C-xi" 'insert-menu-prompt)) ; defaults to `insert-file'

;; goto line
;;(global-set-key "\M-g" 'goto-line)
;;(global-set-key "\C-c\C-g" 'goto-line)
(global-set-key "\C-xg" 'goto-line)     ; defaults to undefined

;; switch windows
(global-set-key "\M-o" 'other-window)   ; defaults to `facemenu-keymap'

;; split windows
(global-set-key "\M-0" 'delete-window)  ; defaults to `digit-argument'
(global-set-key "\M-1" 'delete-other-windows) ; defaults to `digit-argument'
(global-set-key "\M-2" 'split-window-vertically) ; defaults to `digit-argument'
(global-set-key "\M-3" 'split-window-horizontally) ; defaults to `digit-argument'

;; swap windows
(when (functionp 'swap-windows)
  (global-set-key "\C-xO" 'swap-windows) ; defaults to `other-window'
  (global-set-key "\M-p" 'swap-windows)) ; defaults to undefined

;; kill buffer
(global-set-key "\C-x\C-k" 'kill-this-buffer) ; defaults to `kmacro-keymap'

;; ;; revert buffer
;; (global-set-key "\C-x\C-r" 'revert-buffer) ; defaults to `find-file-read-only'

;; ;; backward kill word
;; (global-set-key "\C-w" 'backward-kill-word) ; defaults to `kill-region'
;; (global-set-key "\C-x\C-k" 'kill-region) ; defaults to `kmacro-keymap'

;; copy line
(when (functionp 'copy-line)
  (global-set-key "\C-x\C-y" 'copy-line)) ; defaults to undefined

;; cut line
(when (functionp 'cut-line)
  (global-set-key "\C-x\M-y" 'cut-line)) ; defaults to undefined

;; delete line
(when (functionp 'delete-line)
  (global-set-key "\C-\M-d" 'delete-line)) ; defaults to `down-list'

;; duplicate line
(when (functionp 'duplicate-line)
  (global-set-key "\C-x\C-d" 'duplicate-line)) ; defaults to `list-directory'

;; kill ring browser
(when (functionp 'browse-kill-ring)
  (global-set-key "\C-\M-y" 'browse-kill-ring)) ; defaults to undefined

;; better forward-word
(when (functionp 'forward-word-plus)
  (global-set-key "\M-f" 'forward-word-plus) ; defaults to `forward-word'
  (global-set-key [?\e (right)] 'forward-word-plus)) ; defaults to `forward-word'

;; better backward-word
(when (functionp 'backward-word-plus)
  (global-set-key "\M-b" 'backward-word-plus) ; defaults to `backward-word'
  (global-set-key [?\e (left)] 'backward-word-plus)) ; defaults to `backward-word'

;; better scroll-up
(when (functionp 'scroll-up-plus)
  (global-set-key [(next)] 'scroll-up-plus)) ; defaults to `scroll-up'

;; better scroll-down
(when (functionp 'scroll-down-plus)
  (global-set-key [(prior)] 'scroll-down-plus)) ; defaults to `scroll-down'

;; better downcase-word
(when (functionp 'downcase-word-plus)
  ;;(global-set-key "\M-l" 'downcase-word-plus)) ; defaults to `downcase-word'
  (global-set-key "\C-c\C-l" 'downcase-word-plus)) ; defaults to undefined

;; better upcase-word
(when (functionp 'upcase-word-plus)
  ;;(global-set-key "\M-u" 'upcase-word-plus)) ; defaults to `upcase-word'
  (global-set-key "\C-c\C-u" 'upcase-word-plus)) ; defaults to undefined

;; enlarge window by 5
(when (functionp 'enlarge-window-5)
  (global-set-key "\C-x&" 'enlarge-window-5)) ; defaults to undefined

;; shrink window by 5
(when (functionp 'shrink-window-5)
  (global-set-key "\C-x%" 'shrink-window-5)) ; defaults to `View-goto-percent'

;; ;; print time stamp
;; (when (functionp 'print-time-stamp)
;;   (global-set-key "\C-xa" 'print-time-stamp)) ; defaults to Prefix Command

;; ;; insert uui
;; (when (functionp 'uuid)
;;   ;;(global-set-key "\C-xu" 'uuid)        ; defaults to `advertised-undo'
;;   (global-set-key "\C-cu" 'uuid))       ; defaults to undefined

;; ;; insert guid
;; (when (functionp 'guid)
;;   (global-set-key "\C-cg" 'guid))       ; defaults to undefined

;; evaluate current sexp
(global-set-key "\C-x\C-e" 'eval-current-sexp) ; defaults to `eval-last-sexp'
;; \C-\M-x defaults to `eval-defun'

;; evaluate all sexp's in current buffer
(when (functionp 'eval-sexp-buffer)
  (global-set-key "\C-x\M-e" 'eval-sexp-buffer)) ; defaults to undefined

;; indent current sexp
(global-set-key "\M-\C-q" 'indent-current-sexp) ; defaults to `indent-sexp' or `indent-pp-sexp'

;; indent all sexp's in current buffer
(when (functionp 'indent-sexp-buffer)
  (global-set-key "\C-x\M-q" 'indent-sexp-buffer)) ; defaults to undefined

;; append equal characters up to column 80
(when (functionp 'append-equal-to-column-80)
  (global-set-key "\C-c=" 'append-equal-to-column-80)) ; defaults to undefined

;; append dash characters up to column 80
(when (functionp 'append-dash-to-column-80)
  (global-set-key "\C-c-" 'append-dash-to-column-80)) ; defaults to undefined

;; append asterisk characters up to column 80
(when (functionp 'append-asterisk-to-column-80)
  (global-set-key "\C-c8" 'append-asterisk-to-column-80)) ; defaults to undefined

;; add lisp comment block (equal)
(when (functionp 'lisp-comment-block-equal)
  (global-set-key "\C-c\C-c=" 'lisp-comment-block-equal)) ; defaults to undefined

;; add lisp comment block (dash)
(when (functionp 'lisp-comment-block-dash)
  (global-set-key "\C-c\C-c-" 'lisp-comment-block-dash)) ; defaults to undefined

;; ;; add c/c++/java comment block
;; (when (functionp 'c-comment-block)
;;   (global-set-key "\C-c*" 'c-comment-block)) ; defaults to undefined

;; ;; add db change log template line
;; (when (functionp 'db-change-log-template-line)
;;   (global-set-key "\C-ct" 'db-change-log-template-line)) ; defaults to undefined

;; ;; add db change log template line legacy
;; (when (functionp 'db-change-log-template-line-legacy)
;;   (global-set-key "\C-cT" 'db-change-log-template-line-legacy)) ; defaults to undefined

;; ;; add xml header
;; (when (functionp 'xml-header)
;;   (global-set-key "\C-cx" 'xml-header)) ; defaults to undefined

;; line up assignment commands
(when (functionp 'local-lineup-assignment-commands)
  (global-set-key "\C-c_" 'local-lineup-assignment-commands)) ; defaults to undefined

;; line up declaration commands
(when (functionp 'local-lineup-declaration-commands)
  (global-set-key "\C-c|" 'local-lineup-declaration-commands)) ; defaults to undefined

;; hippie expand
(when (functionp 'hippie-expand)
  (global-set-key "\M-/" 'hippie-expand)) ; defaults to `dabbrev-expand'

;; complete tag
(when (functionp 'complete-tag)
  (global-set-key "\M-\r" 'complete-tag)) ; defaults to undefined

;; comment region
(when (functionp 'comment-region)
  (global-set-key "\C-c]" 'comment-region)) ; defaults to undefined

;; uncomment region
(when (functionp 'uncomment-region)
  (global-set-key "\C-c[" 'uncomment-region)) ; defaults to undefined

;; unset set fill column
(global-unset-key "\C-xf")              ; defaults to `set-fill-column'

;; compare windows
(when (functionp 'compare-windows)
  (global-set-key "\C-xw" 'compare-windows)) ; defaults to undefined

;; occur
(when (functionp 'occur)
  (global-set-key "\C-cu" 'occur))      ; defaults to undefined

;; calendar
(when (functionp 'calendar)
  (global-set-key "\C-xc" 'calendar))   ; defaults to undefined

;; diary
(when (functionp 'diary)
  (global-set-key "\C-xy" 'diary))      ; defaults to undefined

;; planner
(when (functionp 'plan)
  (global-set-key "\C-xp" 'plan))       ; defaults to undefined

;; bookmarks
(when (functionp 'load-bookmarks)
  (global-set-key "\C-clb" 'load-bookmarks)) ; defaults to undefined

;; ;; ispell word
;; (when (functionp 'ispell-word)
;;   (global-set-key "\M-$" 'ispell-word)) ; defaults to `ispell-word'

;; dictionary search
(when (functionp 'dictionary-search)
  (global-set-key "\C-cs" 'dictionary-search) ; defaults to undefined
  (global-set-key "\C-cm" 'dictionary-match-words)) ; defaults to undefined

;; toggle number mode
(when (functionp 'setnu-mode)
  (global-set-key "\C-x\C-n" 'setnu-mode)) ; defaults to `set-goal-column'

;; find file as root
(when (functionp 'find-file-root)
  (global-set-key "\C-x\C-r" 'find-file-root) ; defaults to `find-file-read-only'
  ;;(define-key minibuffer-local-map "\C-r" 'find-file-root) ; defaults to `isearch-backward'
  )

;; find alternative file as root
(when (functionp 'find-alternative-file-root)
  (global-set-key "\C-x\C-r" 'find-alternative-file-root)) ; defaults to `find-file-read-only'

;; find file as root or find alternative file as root
(when (functionp 'find-file-root-or-find-alternative-file-root)
  (global-set-key "\C-x\C-r" 'find-file-root-or-find-alternative-file-root)) ; defaults to `find-file-read-only'

;;==============================================================================
;;; Mode Specific Keys
;;==============================================================================

(message ";;; keys --> Mode Specific Keys")

;; when viewing help, q key quits
;;(define-key view-mode-map "q" 'bury-buffer)

;; scroll backwards in Info mode
;;(define-key Info-mode-map [delete] 'Info-scroll-down)

;; scroll backwards in View mode
;;(define-key view-mode-map [delete] 'View-scroll-page-backward)

;;==============================================================================
;;; Hardware Specific Keys
;;==============================================================================

(message ";;; keys --> Hardware Specific Keys")

;; translate C-h to DEL
;; (keyboard-translate ?\C-h ?\C-?)

;; translate DEL to C-h
;; (keyboard-translate ?\C-? ?\C-h)

;;==============================================================================
;;; Local Keys
;;==============================================================================

(safe-load-compile "keys-local")        ; local key bindings

(message ";;; keys --> End")

;;==============================================================================
;;; End of File
;;==============================================================================