;;; uko-keymaps.el -- keymaps and keys used in UKO-II

;; Copyright (C) 2003 Michael Kuehn, Universität Koblenz-Landau

;; Author: Michael Kuehn <kuehn@uni-koblenz.de>
;; Maintainer: Michael Kuehn <kuehn@uni-koblenz.de>
;; Created: March 2003
;; 
;; $Id: uko-keymaps.el,v 1.23 2004/08/04 12:15:09 rriipp Exp $


;;Define all physical switches that are used

;; define a switch for 0 on the keypad
(defvar uko-physical-switch-0 [(kp-0)]
  "Physical switch on keypad number 0.")

;; define a switch for 1 on the keypad
(defvar uko-physical-switch-1 [(kp-1)]
  "Physical switch on keypad nr 1.")

;; define a switch for 2 on the keypad
(defvar uko-physical-switch-2 [(kp-2)]
  "Physical switch on keypad  nr 2.")

;; define a switch for 3 on the keypad
(defvar uko-physical-switch-3 [(kp-3)]
  "Physical switch on keypad  nr 3.")

;; define a vector of all physical switches
(defconst uko-physical-switches
  (vector uko-physical-switch-0 uko-physical-switch-1 uko-physical-switch-2 uko-physical-switch-3)
  "Vector of available physical switches.")

;; variable to save the local keymap temporarily
(defvar uko-save-local-keymap nil
  "Variable to save temporarily the local keymap.")

;; variable for the currently active kexmap
(defvar uko-active-keymap nil
  "Currently active keymap")

;; set the switch which is defined on 0 of the keypad (the command-button) to the function uko-init
(global-set-key uko-physical-switch-0 'uko-init)

;; Define the default keymap (i.e. tell what to do when a specific key is pressed)
;; Create new (empty) keymap
(defvar uko-default-keymap (make-sparse-keymap)
  "The default keymap")

;; And assign keys, where uko-default-keymap is the object to modify,
;; define each of the physical switches and assign functions to them
(define-key uko-default-keymap uko-physical-switch-1 'uko-letter-key-1)
(define-key uko-default-keymap uko-physical-switch-2 'uko-letter-key-2)
(define-key uko-default-keymap uko-physical-switch-3 'uko-letter-key-3)
(define-key uko-default-keymap uko-physical-switch-0 'uko-command)



;; Define the keymap to bw used in Word Mode [Eingabemodus, ex command-mode] (i.e. tell what to do when a specific key is pressed)
;; Create new (empty) keymap
(defvar uko-command-keymap (make-sparse-keymap)
  "The keymap for Word Mode [Eingabemodus, ex command-mode], where you get when you press 'Word/Text'\
in Letter Mode and the text extent is not empty.")
;; And assign keys, where uko-command-keymap is the object to modify,
;; define each of the physical switches and assign functions to them
(define-key uko-command-keymap uko-physical-switch-1 'uko-set-default-state)
(define-key uko-command-keymap uko-physical-switch-2 'uko-delete-code)
(if uko-select-active
    (define-key uko-command-keymap uko-physical-switch-3 'uko-selection)
  (define-key uko-command-keymap uko-physical-switch-3 'uko-expand))
(define-key uko-command-keymap uko-physical-switch-0 'uko-proposal-select)


(defvar uko-empty-command-keymap (make-sparse-keymap)
  "The keymap for Text Mode [Textmodus, ex empty-command-mode], where you get when you press 'Word/Text'\
in Letter Mode and the text extent is empty")
(define-key uko-empty-command-keymap uko-physical-switch-1 'uko-set-default-state)
(define-key uko-empty-command-keymap uko-physical-switch-2 'uko-delete-word)
(define-key uko-empty-command-keymap uko-physical-switch-3 'uko-move)
(define-key uko-empty-command-keymap uko-physical-switch-0 'uko-noop);'uko-macros)

(defvar uko-proposal-preview-keymap (make-sparse-keymap)
  "The keymap for Hitlist Mode [Trefferlistenmodus, ex proposal-preview-mode], where you get when you\
press 'Hit List' in Word Mode")
(define-key uko-proposal-preview-keymap uko-physical-switch-1 'uko-command-undo)
(define-key uko-proposal-preview-keymap uko-physical-switch-2 'uko-proposal-prev-preview)
(define-key uko-proposal-preview-keymap uko-physical-switch-3 'uko-proposal-next-preview)
(define-key uko-proposal-preview-keymap uko-physical-switch-0 'uko-proposal-select)


(defvar uko-proposal-block-keymap (make-sparse-keymap)
  "The keymap for Block Mode [Trefferlistenmodus, ex proposal-block-mode], where you get when\
the word was not under the first few predictions")
(define-key uko-proposal-block-keymap uko-physical-switch-1 'uko-proposal-cancel)
(define-key uko-proposal-block-keymap uko-physical-switch-2 'uko-proposal-prev-block)
(define-key uko-proposal-block-keymap uko-physical-switch-3 'uko-proposal-next-block)
(define-key uko-proposal-block-keymap uko-physical-switch-0 'uko-proposal-select-block)


(defvar uko-proposal-intra-keymap (make-sparse-keymap)
  "The Keymap for Hit in Block Mode [Treffer im Blockmodus, ex proposal-intra-keymap], where you get when\
you press 'select' in Block Mode")
(define-key uko-proposal-intra-keymap uko-physical-switch-1 'uko-proposal-intra-undo) 
(define-key uko-proposal-intra-keymap uko-physical-switch-2 'uko-proposal-intra-prev)
(define-key uko-proposal-intra-keymap uko-physical-switch-3 'uko-proposal-intra-next)
(define-key uko-proposal-intra-keymap uko-physical-switch-0 'uko-proposal-select)


(defvar uko-delete-keymap (make-sparse-keymap))
  "Keymap for Backspace Mode [Korrekturmodus, ex delete-mode], where you get when you press 'Backspace' in Word Mode."
(define-key uko-delete-keymap uko-physical-switch-1 'uko-delete-undo)
(define-key uko-delete-keymap uko-physical-switch-2 'uko-delete-char)
(define-key uko-delete-keymap uko-physical-switch-3 'uko-delete-undel)
(define-key uko-delete-keymap uko-physical-switch-0 'uko-delete-done)


(defvar uko-delete-word-keymap (make-sparse-keymap))
  "Keymap for Delete Mode [Löschmodus, ex delete-word-mode], where you get when you press 'Delete-Word' in Text Mode."
(define-key uko-delete-word-keymap uko-physical-switch-1 'uko-delete-word-undo)
(define-key uko-delete-word-keymap uko-physical-switch-2 'uko-delete-back-word)
(define-key uko-delete-word-keymap uko-physical-switch-3 'uko-delete-word-undel)
(define-key uko-delete-word-keymap uko-physical-switch-0 'uko-delete-word-done)


(defvar uko-move-keymap (make-sparse-keymap))
  "Keymap for the Navigation Mode [Navigationsmodus, ex move-mode], where you get when you press 'Navigation' in Text Mode."
(define-key uko-move-keymap uko-physical-switch-1 'uko-move-undo)
(define-key uko-move-keymap uko-physical-switch-2 'uko-move-backward)
(define-key uko-move-keymap uko-physical-switch-3 'uko-move-forward)
(define-key uko-move-keymap uko-physical-switch-0 'uko-move-done)


(defvar uko-spell-keymap (make-sparse-keymap))
  "Keymap for Spell Mode [Buchstabiermodus], I don't know how you get there, at the moment NOT UP TO DATE!"
(define-key uko-spell-keymap uko-physical-switch-1 'uko-command-undo)
(define-key uko-spell-keymap uko-physical-switch-2 'uko-spell-prev-letter)
(define-key uko-spell-keymap uko-physical-switch-3 'uko-spell-next-letter)
(define-key uko-spell-keymap uko-physical-switch-0 'uko-spell-done-letter)


(defvar uko-macros-keymap (make-sparse-keymap)
  "Keymap for Macro Select Mode [Makro-Auswahlmodus, ex macro-search-mode], where you get when you press\
'Makro' in Text Mode")
(define-key uko-macros-keymap uko-physical-switch-1 'uko-macros-letter-key-1)
(define-key uko-macros-keymap uko-physical-switch-2 'uko-macros-letter-key-2)
(define-key uko-macros-keymap uko-physical-switch-3 'uko-macros-letter-key-3)
(define-key uko-macros-keymap uko-physical-switch-0 'uko-macros-command)


(defvar uko-macros-command-keymap (make-sparse-keymap)
  "The Keymap for Macro Command Mode [Makro Aktion Modus], where you get when you press 'Command' in Macro Select Mode")
(define-key uko-macros-command-keymap uko-physical-switch-1 'uko-macros-cancel)
(define-key uko-macros-command-keymap uko-physical-switch-2 'uko-incremental-search-back)
(define-key uko-macros-command-keymap uko-physical-switch-3 'uko-incremental-search-next)
(define-key uko-macros-command-keymap uko-physical-switch-0 'uko-macros-execute)


(defvar uko-meta-keymap (make-sparse-keymap))
"Meta-keymap"
(define-key uko-meta-keymap uko-physical-switch-1 'uko-key-1)
(define-key uko-meta-keymap uko-physical-switch-2 'uko-key-2)
(define-key uko-meta-keymap uko-physical-switch-3 'uko-key-3)
(define-key uko-meta-keymap uko-physical-switch-0 'uko-key-0)

;;(global-set-key uko-physical-mouse-button 'uko-noop)

(defun uko-noop ()
  "Dont do nothin."
  (interactive)
  )

;; interactive function to set uko-macros-letter-key to 1
(defun uko-macros-letter-key-1 ()
  "interactive function. Sets uko-macros-letter-key to 1"
 (interactive)
 (uko-macros-letter-key 1)
)

;; interactive function to set uko-macros-letter-key to 2
(defun uko-macros-letter-key-2 ()
  "interactive function. Sets uko-macros-letter-key to 2"
 (interactive)
 (uko-macros-letter-key 2)
)

;; interactive function to set uko-macros-letter-key to 3
(defun uko-macros-letter-key-3 ()
  "interactive function. Sets uko-macros-letter-key to 3"
  (interactive)
  (uko-macros-letter-key 3)
)

;; function performing uko-incremental-search with the chosen key
(defun uko-macros-letter-key (key)
  "Function performing incremental search with key 'ke"
  (uko-incremental-search-letter key)
  )

(defun uko-init-keymap ()
  "?? (set the extent property `keymap' of UKO-META-KEYMAP)"
  (set-extent-property uko-expand-extent 'keymap uko-meta-keymap)
  )

(defun uko-key-0 ()
  (interactive)
  (uko-key-x 0)
)

(defun uko-key-1 ()
  (interactive)
  (uko-key-x 1)
)

(defun uko-key-2 ()
  (interactive)
  (uko-key-x 2)
)

(defun uko-key-3 ()
  (interactive)
  (uko-key-x 3)
)

(defun uko-key-x (k)
  (uko-button-invert k)
  (let ((key (aref uko-physical-switches k)))
    ;; LOG:
    (let* ( (uko-timestamp (current-time))
	    ;; (uko-timediff (time-subtract uko-timestamp uko-timestamp-old)) 
	    ) 
      ;; (setq uko-timestamp-old uko-timestamp) ; keep uko-timestamp
      (goto-char (point-max uko-keylog-buffer) uko-keylog-buffer)
      ;; (insert-string (concat (symbol-name (lookup-key uko-active-keymap key)) ";\t" (format-time-string "%T" uko-timestamp) ";\t" (int-to-string (cadr uko-timediff)) "\n") uko-keylog-buffer) )      
      (insert-string (concat (symbol-name (lookup-key uko-active-keymap key)) ";\t" (format-time-string "%T" uko-timestamp) "\t" (int-to-string (car uko-timestamp)) " " (int-to-string (cadr uko-timestamp)) " " (int-to-string (caddr uko-timestamp)) "\n") uko-keylog-buffer) )      
    ;; EXECUTE:
    (call-interactively (lookup-key uko-active-keymap key))    
    )
 )


;; switches the active and displayed keymap when changing mode
(defun uko-switch-keymap (keymap)
  "Switches the active Keymap (uko-active-keymap) to keymap"
  (setq uko-active-keymap keymap)
)
;(defun uko-switch-keymap (keymap)
;  (set-extend-property uko-expand-extent 'keymap keymap)
;)

;; set the command state: switch the keymap and label the buttons
;; scanning: when switching to this keymap the active button is set to "Expand"
(defun uko-set-command-state ()
  (interactive)
  (uko-switch-keymap uko-command-keymap)
      (uko-display-keyboard-buttons (cdr (assoc* 'word uko-button-labels)))
;  (uko-display-keyboard-layout  "  0 :  Undo " "   1 : Delete  " "  2: Spell  " "  3: Expand")
  )

;;  set the delete state: switch the keymap and label the buttons
;;  scanning: when switching to this keymap the active button is set to "Delete"
(defun uko-set-delete-state ()
  (interactive)
  (uko-switch-keymap uko-delete-keymap)
      (uko-display-keyboard-buttons (cdr (assoc* 'backspace uko-button-labels)))
; (message "    0: Undo    1 : Delete    2: Done   3: UnDelete")
  )

;; set the delete-word state: switch the keymap and label the buttons
;; scanning: when switching to this keymap the active button is set to "Delete"
(defun uko-set-delete-word-state ()
  (interactive)
  (uko-switch-keymap uko-delete-word-keymap)
      (uko-display-keyboard-buttons (cdr (assoc* 'delete uko-button-labels)))
;  (message "    0: Undo    1: Delete    2: Done   3: UnDelete")
  )

;; set the move state: switch the keymap and label the buttons
;; scanning: when switching to this keymap the active button is set to "Backward"
(defun uko-set-move-state ()
  (interactive)
  (uko-switch-keymap uko-move-keymap)
      (uko-display-keyboard-buttons (cdr (assoc* 'navigation uko-button-labels)))
;  (uko-display-keyboard-buttons uko-button-label-move)
;  (message "    0: Undo    1: Backward   2: Done   3: Forward")
  )

;; set the empty command state: switch the keymap and label the buttons
;; scanning: when switching to this keymap the active button is set to "Macro"
(defun uko-set-empty-command-state ()
  (interactive)
  (uko-switch-keymap uko-empty-command-keymap)
      (uko-display-keyboard-buttons (cdr (assoc* 'text uko-button-labels)))
;  (message "    0: Undo    1 : Delete Word    2: Macro    3: Move")
  )

;; set the proposal-preview state: switch the keymap and label the buttons
;; scanning: when switching to this keymap the active button is set to "Select"
(defun uko-set-proposal-preview-state ()
  (interactive)
;  (message "     Prev     Select     Next")
  (uko-switch-keymap uko-proposal-preview-keymap)
  (unless uko-proposal-hide-preblock
    (uko-proposal-show-buffer)
    )
  (uko-proposal-highlight-line)
  (uko-insert-proposal (uko-get-proposal))
      (uko-display-keyboard-buttons (cdr (assoc* 'hitlist uko-button-labels)))
  )

;; set the proposal-block state: switch the keymap and label the buttons
;; scanning: when switching to this keymap the active button is set to "Select"
(defun uko-set-proposal-block-state ()
  (interactive)
  (uko-switch-keymap uko-proposal-block-keymap)
  (uko-insert-proposal uko-code-extent)
  (uko-proposal-display-window)
  (uko-proposal-show-buffer)
  (uko-proposal-highlight-block)
      (uko-display-keyboard-buttons (cdr (assoc* 'block-mode uko-button-labels)))
  )

;; set the proposal-intra state: switch the keymap and label the buttons
;; scanning: when switching to this keymap the active button is set to "Select"
(defun uko-set-proposal-intra-state ()
  (interactive)
;  (message "     Prev     Select     Next")
  (uko-switch-keymap uko-proposal-intra-keymap)
      (uko-display-keyboard-buttons (cdr (assoc* 'hit-in-block uko-button-labels)))
   )

;; set the spell state: switch the keymap and label the buttons
;; scanning: when switching to this keymap the active button is set to "Select"
(defun uko-set-spell-state ()
  (interactive)
  (uko-switch-keymap uko-spell-keymap)
      (uko-display-keyboard-buttons (cdr (assoc* 'spell uko-button-labels)))
  )

  
;; from macros.el
;; ------------------------------------------------------------
;; this function sets up the xemacs environment for macro-mode
(defun uko-set-macros-state ()
  (setq uko-macros-text-window (selected-window))
  (uko-proposal-display-window)
  (set-window-buffer uko-proposal-window uko-macros-names-buffer)
  (select-window uko-proposal-window)
  (goto-char 1)
  (uko-incremental-search t)
;  (if uko-german-keyboard
     (uko-display-keyboard-buttons
      (append (mapcar 'uko-keyboard-layout-button uko-keyboard-layout)
	      (list "     Auswahl") ) ;then
      )
;    (uko-display-keyboard-buttons
;     (append (mapcar 'uko-keyboard-layout-button uko-keyboard-english-layout)
;	     (list "     Command") ) ;else
;     )
;    )

  (use-local-map uko-macros-keymap)
  ; (set-extent-property uko-expand-extent 'keymap uko-macros-keymap)
  )


;; this function labels the uko-keyboard button with the necessary names used in macro mode and
;; switches the local keymap to the macros-command-keymap
(defun uko-set-macros-command-state ()
"switches keymap to macros-command-keymap and labels buttons"
      (uko-display-keyboard-buttons (cdr (assoc* 'macro-command uko-button-labels)))
  (use-local-map uko-macros-command-keymap)
  ; (set-extent-property uko-expand-extent 'keymap uko-macros-command-keymap)
  )

;;------------------------------SELECT--------------------------------


;; defining the functions the different keys invoke in the select-mode
(defvar uko-select-keymap (make-sparse-keymap)
  "Keymap for uko-select-select-mode.")
(define-key uko-select-keymap uko-physical-switch-1 'uko-select-letter-key-1)
(define-key uko-select-keymap uko-physical-switch-2 'uko-select-letter-key-2)
(define-key uko-select-keymap uko-physical-switch-3 'uko-select-letter-key-3)
(define-key uko-select-keymap uko-physical-switch-0 'uko-select-command)

;; defining the functions the different keys invoke in the command-mode
(defvar uko-select-command-keymap (make-sparse-keymap)
  "Keymap for uko-select-command-mode.")
(define-key uko-select-command-keymap uko-physical-switch-1 'uko-select-cancel)
(define-key uko-select-command-keymap uko-physical-switch-2 'uko-select-previous)
(define-key uko-select-command-keymap uko-physical-switch-3 'uko-select-next)
(define-key uko-select-command-keymap uko-physical-switch-0 'uko-select-accept)




;;---------- DEFAULT-STATE ----------------------------------------------------

(defun uko-set-default-state ()
  "Asigns the initial captions to all buttons"
  (interactive)
  
;  (uko-display-keyboard-layout uko-keyboard-layout)
  ;; uko-display-keyboard-buttons gets a list where the first
  ;; (three) elements are "layouted" letter-maps and the last 
  ;; is "command"; These Captions are assigned to the buttons
;  (cdr (assoc* 'letter uko-german-button-labels)))
     (uko-display-keyboard-buttons
      (append (mapcar 'uko-keyboard-layout-button uko-keyboard-layout)
	      (list "     Wort / Text") ) ;then
      )
       
  ;;Highlight extent
  (set-extent-face uko-expand-extent 'highlight)
  ;;Switch the keymap to the default
 (uko-switch-keymap uko-default-keymap)
  )

