diff --git a/.gitignore b/.gitignore index 38e4180..338c30b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1 @@ -*.autosave.xopp -*~ +config.lua diff --git a/config.lua.example b/config.lua.example new file mode 100644 index 0000000..1f037dc --- /dev/null +++ b/config.lua.example @@ -0,0 +1,14 @@ +-- Configuration for vi-xournalpp plugin +-- Copy this file to config.lua and customize + +local layouts = require('layouts') + +-- Set your keyboard layout: 'qwerty', 'dvorak', or 'colemak' +layouts.active_layout = 'qwerty' + +-- Optional: Override specific keys +-- layouts.key_overrides = { +-- ['w'] = 'j', +-- } + +return layouts diff --git a/layouts.lua b/layouts.lua new file mode 100644 index 0000000..949a8bc --- /dev/null +++ b/layouts.lua @@ -0,0 +1,126 @@ +-- Keyboard layout mappings for vi-xournalpp plugin +-- This file contains default layout definitions +-- To customize, copy config.lua.example to config.lua and modify it + +local config = {} + +-- Active keyboard layout +-- Options: 'qwerty', 'dvorak', 'colemak', or your custom layout name +config.active_layout = 'qwerty' + +-- Custom key overrides (optional) +-- Use this to override specific keys for your personal preference +-- This applies AFTER layout mapping +-- Example: config.key_overrides = { ['w'] = 'j', ['s'] = 'k' } +config.key_overrides = {} + +-- Keyboard layout mappings +-- Maps QWERTY keys to their physical equivalents on other layouts +-- This preserves hand movement and muscle memory across layouts +config.layout_map = { + qwerty = {}, + + dvorak = { + ['q'] = 'apostrophe', + ['w'] = 'comma', + ['e'] = 'period', + ['r'] = 'p', + ['t'] = 'y', + ['y'] = 'f', + ['u'] = 'g', + ['i'] = 'c', + ['o'] = 'r', + ['p'] = 'l', + ['a'] = 'a', + ['s'] = 'o', + ['d'] = 'e', + ['f'] = 'u', + ['g'] = 'i', + ['h'] = 'd', + ['j'] = 'h', + ['k'] = 't', + ['l'] = 'n', + ['z'] = 'semicolon', + ['x'] = 'q', + ['c'] = 'j', + ['v'] = 'k', + ['b'] = 'x', + ['n'] = 'b', + ['m'] = 'm', + ['comma'] = 'w', + ['period'] = 'v', + ['slash'] = 'z', + ['less'] = 'w', + ['greater'] = 'v', + ['question'] = 'z', + }, + + colemak = { + ['q'] = 'q', + ['w'] = 'w', + ['e'] = 'f', + ['r'] = 'p', + ['t'] = 'g', + ['y'] = 'j', + ['u'] = 'l', + ['i'] = 'u', + ['o'] = 'y', + ['p'] = 'semicolon', + ['a'] = 'a', + ['s'] = 'r', + ['d'] = 's', + ['f'] = 't', + ['g'] = 'd', + ['h'] = 'h', + ['j'] = 'n', + ['k'] = 'e', + ['l'] = 'i', + ['z'] = 'z', + ['x'] = 'x', + ['c'] = 'c', + ['v'] = 'v', + ['b'] = 'b', + ['n'] = 'k', + ['m'] = 'm', + }, +} + +-- Helper function to map keys with modifiers dynamically +function config.map_key(key, layout_map) + if not key or key == '' or not layout_map then + return key + end + + -- Extract modifiers and base key + local modifiers = {} + local base_key = key + + for modifier in key:gmatch('<([^>]+)>') do + table.insert(modifiers, modifier) + end + + -- Get base key (everything after last >) + if #modifiers > 0 then + base_key = key:match('[^>]+$') or '' + end + + if base_key == '' then + return key + end + + -- Map the base key + local mapped_base = layout_map[base_key] or base_key + + -- Reconstruct with modifiers + if #modifiers > 0 then + local result = '' + for _, mod in ipairs(modifiers) do + result = result .. '<' .. mod .. '>' + end + return result .. mapped_base + end + + return mapped_base +end + +return config diff --git a/main.lua b/main.lua index 87716ba..cfab43e 100644 --- a/main.lua +++ b/main.lua @@ -3,6 +3,41 @@ require('modes') f = {} +-- Apply config if it exists +local function apply_config() + local success, config = pcall(require, 'config') + if not success or type(config) ~= 'table' then + if success and type(config) ~= 'table' then + print('vi-xournalpp: config.lua exists but does not return a table') + end + return + end + + local layout = config.active_layout or 'qwerty' + local layout_map = config.layout_map and config.layout_map[layout] + local key_overrides = config.key_overrides or {} + + if not layout_map and layout ~= 'qwerty' then + print("vi-xournalpp: Unknown layout '" .. layout .. "', using qwerty.") + layout_map = config.layout_map and config.layout_map.qwerty + end + + for _, binding in pairs(keybindings) do + local new_buttons = {} + for _, button in ipairs(binding.buttons) do + local mapped_button = button + if layout_map and layout ~= 'qwerty' then + mapped_button = config.map_key(button, layout_map) + end + mapped_button = key_overrides[mapped_button] or mapped_button + table.insert(new_buttons, mapped_button) + end + binding.buttons = new_buttons + end +end + +apply_config() + function initUi() index = 1 for _, binding in pairs(keybindings) do