require 'clibs' -- for nylono

local Keys = {}

local WINDOWS = (NylonOs and NylonOs.IsWindows())

   --[[-- Free Ctrl keys:
      C-c (emacs this is a user prefix or something)
      C-l (i associate with window mgmt)
      C-o (emacs = "open line"? I've never used that)
      C-q (usally means insert literal)
      C-u (emacs = numbered prefix)
      C-w (emacs = kill-region)
   --]]--

--[[--
 useful keys
   [27] C-[ (esc)
   [28] C-\
   [29] C-]
   [30] C-^
   [31] C-_ (underscore)

   [417]   M-a (418=M-b, 419=M-c, 442=M-z, etc)
   [423]   M-g for M-g n and M-g p to move through search results
   [428]   M-l might be better for the app menu than C-l
           For some reason, in windows, right alt key doesn't work,
           only left key.  so I am inclined against Alt+# key combos now.
--]]--


Keys.control = {
--   menu = 12, -- C-l -- conflicts with Ion WM
--   menu = 17, -- C-q
--   menu = 20, -- C-t -- conlicts with buffer mgmt, C-t k, C-t o (Keys.command_prefix)
   menu = 429, -- M-m
   editTitle = 436, -- M-t
}



Keys.edit = {
   [18] = 'isearch_backward',        -- C-r
   [19] = 'isearch_forward',         -- C-s

   [1]   = 'move_beginning_of_line', -- C-a
   [5]   = 'move_end_of_line',       -- C-e

   [6]   = 'forward_char',           -- C-f
   [2]   = 'backward_char',          -- C-b
   [Pdcurses.key.right] = 'forward_char',  -- C-f
   [Pdcurses.key.left]  = 'backward_char', -- C-f

   [7]   = 'ctrlg',

   [422] = 'forward_word',           -- M-f
   [418] = 'backward_word',          -- M-b
   [437] = 'upcase_word',            -- M-u
   [428] = 'downcase_word',          -- M-d
   [419] = 'capitalize_word',        -- M-c

   [22]  = 'scroll_up_command',                   -- C-v
   [Pdcurses.key.npage]  = 'scroll_up_command',   -- pgdn
   [438] = 'scroll_down_command',                 -- M-v
   [Pdcurses.key.ppage]  = 'scroll_down_command', -- pgup

   [Pdcurses.key.dc] = 'delete_char', -- [Delete] 
   [4]   = 'delete_char',             -- C-d
   [8]   = 'delete_backward_char',    -- bksp

   -- 161217 not exactly sure why this is; on linux I get 263 for backspace key.
   [263]  = 'delete_backward_char',        -- C-z

   [420] = 'delete_word', -- M-d
   [11]  = 'kill_line',   -- C-k
   [441] = 'yank_pop',    -- M-y
   [26]  = 'undo',        -- C-z

   [445] = 'scroll_right',        -- C-pgup
   [446] = 'scroll_left',         -- C-pgdb

   [9]   = 'indent_for_tab_command', -- bksp
   [10]  = 'newline',                -- Enter
   [25]  = 'yank',        -- C-y
   [27]  = 'escape'
}


Keys.everybody = {
      [Pdcurses.key.down] = 'next_line',
      [Pdcurses.key.up]   = 'previous_line',

   [22]  = 'scroll_up_command',                   -- C-v
   [Pdcurses.key.npage]  = 'scroll_up_command',   -- pgdn
   [438] = 'scroll_down_command',                 -- M-v
   [Pdcurses.key.ppage]  = 'scroll_down_command', -- pgup
      

      [14]  = 'next_line',        -- C-n
      [15]  = 'context_menu',     -- C-o
      [16]  = 'previous_line',    -- C-p
      [23]  = 'kill_region',      -- C-w
      [431] = 'toggle_landscape', -- M-o
--      [431] = 'toggle_fullscreen', -- M-o
      [439] = 'kill_ring_save',   -- M-w
}

if WINDOWS then
   local keymap_everybody_windows = {
      [Pdcurses.key.ctl_tab] = 'other_window',
      [Pdcurses.key.ctl_enter]  = 'focused_window_to_primary', -- 
      [Pdcurses.key.ctl_home] = 'beginning_of_buffer', -- C-Home
      [Pdcurses.key.ctl_end] = 'end_of_buffer',       -- C-end
      [Pdcurses.key.sdown] = 'xyz', -- Shift-down
      [Pdcurses.key.sup]   = 'xyz', -- Shift-up
      [13] = 10,
   }
   for i,v in pairs(keymap_everybody_windows) do
     Keys.everybody[i] = v
   end	
end	

-- C-x prefix (or C-t, if you're me)
Keys.command_prefix = {
   [3] = 'save_buffers_kill_terminal',        -- C-x C-c
   [19] = 'save_buffer',                      -- C-x C-s
   [string.byte('k',1)] = 'kill_buffer',      -- C-x k
   [string.byte('u',1)] = 'undo',             -- C-x u
   [string.byte('o',1)] = 'other_window',     -- C-x o
   [string.byte('i',1)] = 'insert_file',      -- C-x o
   [string.byte('m',1)] = 'mail_record',      -- C-x k
   [string.byte(' ',1)] = 'set_mark_command', -- C-x k
}


-- C-c prefix
Keys.personal_prefix = {
   [string.byte('.',1)] = 'jumptotag', -- C-c .
   [3] = 'query_citations',            -- C-c C-c
   [string.byte('>',1)] = 'extract_to_new_record',
   [string.byte('n',1)] = 'insert_ref_to_new_record',
   [string.byte('y',1)] = 'special_yank',
}


-- abandoned
--   [string.byte('s',1)] = 'start_snipping_tool',


Keys.esc_prefix = { -- Duplicate Pdcurses' M-[a-z] mappings
   [string.byte('a',1)] = 417,
   [string.byte('b',1)] = 418,
   [string.byte('c',1)] = 419,
   [string.byte('d',1)] = 420,
   [string.byte('e',1)] = 421,
   [string.byte('f',1)] = 422,
   [string.byte('g',1)] = 423,

   [string.byte('l',1)] = 428,
   [string.byte('o',1)] = 431,
   [string.byte('s',1)] = 435,
   [string.byte('t',1)] = 436,
   [string.byte('u',1)] = 437,
   [string.byte('w',1)] = 439,
   [string.byte('y',1)] = 441,
}



return Keys