Skip to content

Commit

Permalink
Support configurable keymap
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
malkoG committed Sep 30, 2023
1 parent be847df commit 89e4d8b
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 18 deletions.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

## Keymaps

```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


Expand Down
56 changes: 52 additions & 4 deletions lua/mastodon.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()<CR>",
["scroll-to-top"] = ":lua require('mastodon.commands').fetch_newer_statuses()<CR>",
["scroll-to-bottom"] = ":lua require('mastodon.commands').fetch_older_statuses()<CR>",

["reply"] = ":lua require('mastodon.actions').reply()<CR>",
["bookmark"] = ":lua require('mastodon.actions').toggle_bookmark()<CR>",
["favourite"] = ":lua require('mastodon.actions').toggle_favourite()<CR>",
["boost"] = ":lua require('mastodon.actions').toggle_boost()<CR>",
["print"] = ":lua require('mastodon.actions').print_verbose_information()<CR>",
},
["system-wide-keymaps"] = {
["home-timeline"] = ":lua require('mastodon').fetch_home_timeline()<CR>",
["bookmarks"] = ":lua require('mastodon').fetch_bookmarks()<CR>",
["favourites"] = ":lua require('mastodon').fetch_favourites()<CR>",
["mentions"] = ":lua require('mastodon').fetch_replies()<CR>",

["post-message"] = ":lua require('mastodon').toot_message()<CR>",
["select-account"] = ":lua require('mastodon').select_account()<CR>",
},
}

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" })
Expand Down
50 changes: 36 additions & 14 deletions plugin/mastodon.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,57 @@ 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,
desc = "Only works on Mastodon Buffers",
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()<CR>", default_opts)
map("n", ",mk", ":lua require('mastodon.commands').fetch_newer_statuses()<CR>", default_opts)
map("n", ",mj", ":lua require('mastodon.commands').fetch_older_statuses()<CR>", 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()<CR>", default_opts)
map("n", ",tb", ":lua require('mastodon.actions').toggle_bookmark()<CR>", default_opts)
map("n", ",tf", ":lua require('mastodon.actions').toggle_favourite()<CR>", default_opts)
map("n", ",tB", ":lua require('mastodon.actions').toggle_boost()<CR>", default_opts)
map("n", ",tv", ":lua require('mastodon.actions').print_verbose_information()<CR>", 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()<CR>", default_opts)
map("n", ",mR", ":lua require('mastodon').fetch_replies()<CR>", default_opts)
map("n", ",mf", ":lua require('mastodon').fetch_favourites()<CR>", default_opts)
map("n", ",mb", ":lua require('mastodon').fetch_bookmarks()<CR>", default_opts)
map("n", ",mh", ":lua require('mastodon').fetch_home_timeline()<CR>", default_opts)
map("n", ",ms", ":lua require('mastodon').select_account()<CR>", 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

0 comments on commit 89e4d8b

Please sign in to comment.