keymap.lua 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. local command = require "core.command"
  2. local keymap = {}
  3. keymap.modkeys = {}
  4. keymap.map = {}
  5. keymap.reverse_map = {}
  6. local modkey_map = {
  7. ["left ctrl"] = "ctrl",
  8. ["right ctrl"] = "ctrl",
  9. ["left shift"] = "shift",
  10. ["right shift"] = "shift",
  11. ["left alt"] = "alt",
  12. ["right alt"] = "altgr",
  13. }
  14. local modkeys = { "ctrl", "alt", "altgr", "shift" }
  15. local function key_to_stroke(k)
  16. local stroke = ""
  17. for _, mk in ipairs(modkeys) do
  18. if keymap.modkeys[mk] then
  19. stroke = stroke .. mk .. "+"
  20. end
  21. end
  22. return stroke .. k
  23. end
  24. function keymap.add(map, overwrite)
  25. for stroke, commands in pairs(map) do
  26. if type(commands) == "string" then
  27. commands = { commands }
  28. end
  29. if overwrite then
  30. keymap.map[stroke] = commands
  31. else
  32. keymap.map[stroke] = keymap.map[stroke] or {}
  33. for i = #commands, 1, -1 do
  34. table.insert(keymap.map[stroke], 1, commands[i])
  35. end
  36. end
  37. for _, cmd in ipairs(commands) do
  38. keymap.reverse_map[cmd] = stroke
  39. end
  40. end
  41. end
  42. function keymap.get_binding(cmd)
  43. return keymap.reverse_map[cmd]
  44. end
  45. function keymap.on_key_pressed(k)
  46. local mk = modkey_map[k]
  47. if mk then
  48. keymap.modkeys[mk] = true
  49. -- work-around for windows where `altgr` is treated as `ctrl+alt`
  50. if mk == "altgr" then
  51. keymap.modkeys["ctrl"] = false
  52. end
  53. else
  54. local stroke = key_to_stroke(k)
  55. local commands = keymap.map[stroke]
  56. if commands then
  57. for _, cmd in ipairs(commands) do
  58. local performed = command.perform(cmd)
  59. if performed then break end
  60. end
  61. return true
  62. end
  63. end
  64. return false
  65. end
  66. function keymap.on_key_released(k)
  67. local mk = modkey_map[k]
  68. if mk then
  69. keymap.modkeys[mk] = false
  70. end
  71. end
  72. keymap.add {
  73. ["ctrl+shift+p"] = "core:do-command",
  74. ["ctrl+p"] = "core:open-project-file",
  75. ["ctrl+o"] = "core:open-file",
  76. ["ctrl+n"] = "core:new-doc",
  77. ["alt+shift+j"] = "root:split-left",
  78. ["alt+shift+l"] = "root:split-right",
  79. ["alt+shift+i"] = "root:split-up",
  80. ["alt+shift+k"] = "root:split-down",
  81. ["alt+j"] = "root:switch-to-left",
  82. ["alt+l"] = "root:switch-to-right",
  83. ["alt+i"] = "root:switch-to-up",
  84. ["alt+k"] = "root:switch-to-down",
  85. ["ctrl+w"] = "root:close",
  86. ["ctrl+tab"] = "root:switch-to-next-tab",
  87. ["ctrl+shift+tab"] = "root:switch-to-previous-tab",
  88. ["ctrl+pageup"] = "root:move-tab-left",
  89. ["ctrl+pagedown"] = "root:move-tab-right",
  90. ["alt+1"] = "root:switch-to-tab-1",
  91. ["alt+2"] = "root:switch-to-tab-2",
  92. ["alt+3"] = "root:switch-to-tab-3",
  93. ["alt+4"] = "root:switch-to-tab-4",
  94. ["alt+5"] = "root:switch-to-tab-5",
  95. ["alt+6"] = "root:switch-to-tab-6",
  96. ["alt+7"] = "root:switch-to-tab-7",
  97. ["alt+8"] = "root:switch-to-tab-8",
  98. ["alt+9"] = "root:switch-to-tab-9",
  99. ["ctrl+f"] = "find-replace:find",
  100. ["ctrl+r"] = "find-replace:replace",
  101. ["f3"] = "find-replace:repeat-find",
  102. ["shift+f3"] = "find-replace:previous-find",
  103. ["ctrl+g"] = "doc:go-to-line",
  104. ["ctrl+s"] = "doc:save",
  105. ["ctrl+shift+s"] = "doc:save-as",
  106. ["ctrl+z"] = "doc:undo",
  107. ["ctrl+y"] = "doc:redo",
  108. ["ctrl+x"] = "doc:cut",
  109. ["ctrl+c"] = "doc:copy",
  110. ["ctrl+v"] = "doc:paste",
  111. ["escape"] = { "command:escape" },
  112. ["tab"] = { "command:complete", "doc:indent" },
  113. ["shift+tab"] = "doc:unindent",
  114. ["backspace"] = "doc:backspace",
  115. ["shift+backspace"] = "doc:backspace",
  116. ["ctrl+backspace"] = "doc:delete-to-previous-word-boundary",
  117. ["ctrl+shift+backspace"] = "doc:delete-to-previous-word-boundary",
  118. ["delete"] = "doc:delete",
  119. ["return"] = { "command:submit", "doc:newline" },
  120. ["ctrl+return"] = "doc:newline-below",
  121. ["ctrl+shift+return"] = "doc:newline-above",
  122. ["ctrl+j"] = "doc:join-lines",
  123. ["ctrl+a"] = "doc:select-all",
  124. ["ctrl+d"] = "doc:select-word",
  125. ["ctrl+l"] = "doc:select-lines",
  126. ["ctrl+/"] = "doc:toggle-line-comments",
  127. ["ctrl+up"] = "doc:move-lines-up",
  128. ["ctrl+down"] = "doc:move-lines-down",
  129. ["ctrl+shift+d"] = "doc:duplicate-lines",
  130. ["ctrl+shift+k"] = "doc:delete-lines",
  131. ["left"] = "doc:move-to-previous-char",
  132. ["right"] = "doc:move-to-next-char",
  133. ["up"] = { "command:select-previous", "doc:move-to-previous-line" },
  134. ["down"] = { "command:select-next", "doc:move-to-next-line" },
  135. ["ctrl+left"] = "doc:move-to-previous-word-boundary",
  136. ["ctrl+right"] = "doc:move-to-next-word-boundary",
  137. ["ctrl+["] = "doc:move-to-previous-start-of-block",
  138. ["ctrl+]"] = "doc:move-to-next-start-of-block",
  139. ["home"] = "doc:move-to-start-of-line",
  140. ["end"] = "doc:move-to-end-of-line",
  141. ["ctrl+home"] = "doc:move-to-start-of-doc",
  142. ["ctrl+end"] = "doc:move-to-end-of-doc",
  143. ["pageup"] = "doc:move-to-previous-page",
  144. ["pagedown"] = "doc:move-to-next-page",
  145. ["shift+left"] = "doc:select-to-previous-char",
  146. ["shift+right"] = "doc:select-to-next-char",
  147. ["shift+up"] = "doc:select-to-previous-line",
  148. ["shift+down"] = "doc:select-to-next-line",
  149. ["ctrl+shift+left"] = "doc:select-to-previous-word-boundary",
  150. ["ctrl+shift+right"] = "doc:select-to-next-word-boundary",
  151. ["ctrl+shift+["] = "doc:select-to-previous-start-of-block",
  152. ["ctrl+shift+]"] = "doc:select-to-next-start-of-block",
  153. ["shift+home"] = "doc:select-to-start-of-line",
  154. ["shift+end"] = "doc:select-to-end-of-line",
  155. ["ctrl+shift+home"] = "doc:select-to-start-of-doc",
  156. ["ctrl+shift+end"] = "doc:select-to-end-of-doc",
  157. ["shift+pageup"] = "doc:select-to-previous-page",
  158. ["shift+pagedown"] = "doc:select-to-next-page",
  159. }
  160. return keymap