From c0d1f9fd308c4d3fc5639992a29324f019b19118 Mon Sep 17 00:00:00 2001 From: malkoG Date: Sat, 30 Sep 2023 17:37:54 +0900 Subject: [PATCH] Support configurable keymap closes #19 This commit updates the keymaps and operations configuration in the Mastodon plugin. The keymaps are now defined in a separate Lua table, making it easier to modify or extend them. The operations, which represent the actions triggered by the keymaps, are also defined in a separate table for improved clarity and maintainability. Here are the keymap changes: - Buffer-wide keymaps: - `reload-statuses` is mapped to `,mr` - `scroll-to-top` is mapped to `,mk` - `scroll-to-bottom` is mapped to `,mj` - Status-wide keymaps: - `reply` is mapped to `,tr` - `bookmark` is mapped to `,tb` - `favourite` is mapped to `,tf` - `boost` is mapped to `,tB` - `print` is mapped to `,tv` Here are the system-wide keymaps: - `home-timeline` is mapped to `,mh` - `bookmarks` is mapped to `,mb` - `favourites` is mapped to `,mf` - `mentions` is mapped to `,mR` - `post-message` is mapped to `,mw` - `select-account` is mapped to `,ms` These changes enhance the flexibility and customizability of the Mastodon plugin. --- README.md | 29 +++++++++++++++++++++++ lua/mastodon.lua | 56 +++++++++++++++++++++++++++++++++++++++++---- plugin/mastodon.lua | 50 ++++++++++++++++++++++++++++------------ 3 files changed, 117 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 5586200..aa4dcb2 100644 --- a/README.md +++ b/README.md @@ -158,6 +158,35 @@ Within `Mastodon Buffer`, each line has metadata which includes status's id and * `,tB` - Enables you to boost current status * `,tv` - Enables you to see current status's raw data +## Configuration + +```lua +require("mastodon").setup({ + keymaps = { + ["buffer-wide-keymaps"] = { + ["reload-statuses"] = ",mr", + ["scroll-to-top"] = ",mk", + ["scroll-to-bottom"] = ",mj", + + ["reply"] = ",tr", + ["bookmark"] = ",tb", + ["favourite"] = ",tf", + ["boost"] = ",tB", + ["print"] = ",tv", + }, + ["system-wide-keymaps"] = { + ["home-timeline"] = ",mh", + ["bookmarks"] = ",mb", + ["favourites"] = ",mf", + ["mentions"] = ",mR", + + ["post-message"] = ",mw", + ["select-account"] = ",ms", + }, + } +}) +``` + # Explanation for developers diff --git a/lua/mastodon.lua b/lua/mastodon.lua index 8ba5ac6..87678b7 100644 --- a/lua/mastodon.lua +++ b/lua/mastodon.lua @@ -2,16 +2,64 @@ local commands = require("mastodon.commands") local M = {} -M.config = { - -- default config - opt = "Hello!", + + +M.default_keymaps = { + ["buffer-wide-keymaps"] = { + ["reload-statuses"] = ",mr", + ["scroll-to-top"] = ",mk", + ["scroll-to-bottom"] = ",mj", + + ["reply"] = ",tr", + ["bookmark"] = ",tb", + ["favourite"] = ",tf", + ["boost"] = ",tB", + ["print"] = ",tv", + }, + ["system-wide-keymaps"] = { + ["home-timeline"] = ",mh", + ["bookmarks"] = ",mb", + ["favourites"] = ",mf", + ["mentions"] = ",mR", + + ["post-message"] = ",mw", + ["select-account"] = ",ms", + }, +} + +M.operations = { + ["buffer-wide-keymaps"] = { + ["reload-statuses"] = ":lua require('mastodon').reload_statuses()", + ["scroll-to-top"] = ":lua require('mastodon.commands').fetch_newer_statuses()", + ["scroll-to-bottom"] = ":lua require('mastodon.commands').fetch_older_statuses()", + + ["reply"] = ":lua require('mastodon.actions').reply()", + ["bookmark"] = ":lua require('mastodon.actions').toggle_bookmark()", + ["favourite"] = ":lua require('mastodon.actions').toggle_favourite()", + ["boost"] = ":lua require('mastodon.actions').toggle_boost()", + ["print"] = ":lua require('mastodon.actions').print_verbose_information()", + }, + ["system-wide-keymaps"] = { + ["home-timeline"] = ":lua require('mastodon').fetch_home_timeline()", + ["bookmarks"] = ":lua require('mastodon').fetch_bookmarks()", + ["favourites"] = ":lua require('mastodon').fetch_favourites()", + ["mentions"] = ":lua require('mastodon').fetch_replies()", + + ["post-message"] = ":lua require('mastodon').toot_message()", + ["select-account"] = ":lua require('mastodon').select_account()", + }, +} + +M.default_config = { + keymaps = M.default_keymaps } -- setup is the public method to setup your plugin M.setup = function(args) -- you can define your setup function here. Usually configurations can be merged, accepting outside params and -- you can also put some validation here for those. - M.config = vim.tbl_deep_extend("force", M.config, args or {}) + M.config = vim.tbl_deep_extend("force", M.default_config, args or {}) + vim.g.mastodon_config = M.config namespace = vim.api.nvim_create_namespace("MastodonNS") local hl_for_whitespace = vim.api.nvim_get_hl(0, { name = "NormalFloat" }) diff --git a/plugin/mastodon.lua b/plugin/mastodon.lua index 8830697..84bae59 100644 --- a/plugin/mastodon.lua +++ b/plugin/mastodon.lua @@ -7,11 +7,29 @@ vim.api.nvim_create_user_command("MastodonLoadFavourites", require("mastodon").f vim.api.nvim_create_user_command("MastodonLoadReplies", require("mastodon").fetch_replies, {}) vim.api.nvim_create_user_command("MastodonReload", require("mastodon").reload_statuses, {}) +local operations = require("mastodon").operations + +local keymaps = vim.g.mastodon_config["keymaps"] +local default_keymaps = require("mastodon").default_config["keymaps"] + local map = vim.api.nvim_set_keymap local default_opts = { noremap = true, silent = true } local augroup = vim.api.nvim_create_augroup("user_cmds", { clear = false }) +local get_lhs = function(keymap_scope, operation) + local default_lhs = default_keymaps[keymap_scope][operation] + if keymaps == nil then + return default_lhs + end + + if keymaps[keymap_scope] == nil then + return default_lhs + end + + return keymaps[keymap_scope][operation] +end + vim.api.nvim_create_autocmd("FileType", { pattern = { "mastodon" }, group = augroup, @@ -19,23 +37,27 @@ vim.api.nvim_create_autocmd("FileType", { callback = function(event) -- if keymap starts with `,m`, -- buffer-wide or system-wide commands should be called - map("n", ",mr", ":lua require('mastodon').reload_statuses()", default_opts) - map("n", ",mk", ":lua require('mastodon.commands').fetch_newer_statuses()", default_opts) - map("n", ",mj", ":lua require('mastodon.commands').fetch_older_statuses()", default_opts) + local buffer_wide_operations = { "reload-statuses", "scroll-to-top", "scroll-to-bottom" } + for _, operation in ipairs(buffer_wide_operations) do + local lhs = get_lhs("buffer-wide-keymaps", operation) + local rhs = operations["buffer-wide-keymaps"][operation] + map("n", lhs, rhs, default_opts) + end -- If keymap starts with `,t` -- status-wide commands should be called - map("n", ",tr", ":lua require('mastodon.actions').reply()", default_opts) - map("n", ",tb", ":lua require('mastodon.actions').toggle_bookmark()", default_opts) - map("n", ",tf", ":lua require('mastodon.actions').toggle_favourite()", default_opts) - map("n", ",tB", ":lua require('mastodon.actions').toggle_boost()", default_opts) - map("n", ",tv", ":lua require('mastodon.actions').print_verbose_information()", default_opts) + local status_wide_operations = { "reply", "bookmark", "favourite", "boost", "print" } + for _, operation in ipairs(status_wide_operations) do + local lhs = get_lhs("buffer-wide-keymaps", operation) + local rhs = operations["buffer-wide-keymaps"][operation] + map("n", lhs, rhs, default_opts) + end end, }) -map("n", ",mw", ":lua require('mastodon').toot_message()", default_opts) -map("n", ",mR", ":lua require('mastodon').fetch_replies()", default_opts) -map("n", ",mf", ":lua require('mastodon').fetch_favourites()", default_opts) -map("n", ",mb", ":lua require('mastodon').fetch_bookmarks()", default_opts) -map("n", ",mh", ":lua require('mastodon').fetch_home_timeline()", default_opts) -map("n", ",ms", ":lua require('mastodon').select_account()", default_opts) +local system_wide_operations = { "home-timeline", "bookmarks", "favourites", "mentions", "post-message", "select-account" } +for _, operation in ipairs(system_wide_operations) do + local lhs = get_lhs("system-wide-keymaps", operation) + local rhs = operations["system-wide-keymaps"][operation] + map("n", lhs, rhs, default_opts) +end