Skip to content

Commit

Permalink
feat: global and filetype-specific mappings
Browse files Browse the repository at this point in the history
  • Loading branch information
alohaia committed Oct 7, 2024
1 parent 9c9fb9f commit ddd8df6
Show file tree
Hide file tree
Showing 11 changed files with 221 additions and 363 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ require 'aloha' {
- `clone_depth`: `--depth` option for `clone`
- `shallow_submodules`: whether to add `--shallow-submodules` in `clone` command and add `--depth=1` in `submodule update` command
- `base_url`: base URL of GitHub, you can replace this to use a mirror site
- `strict_deps`: when set to `true`, a plugin's config won't be executed if any dependency of this plugin is not successfully loaded
- `strict_deps`: when set to `true`, a plugin's config won't be executed if any dependencies of this plugin is not successfully loaded
- `transparency`: transparent background and related config
- `mapleader`:`<Leader>` key
Expand Down Expand Up @@ -120,7 +120,7 @@ require 'aloha' {
> }
> },
> ['lewis6991/gitsigns.nvim'] = {
> dependency = 'nvim-lua/plenary.nvim'
> dependencies = 'nvim-lua/plenary.nvim'
> },
> ['nvim-telescope/telescope.nvim'] = {
> cmd = 'Telescope',
Expand All @@ -130,7 +130,7 @@ require 'aloha' {
> {mode = 'n', lhs = ',F'},
> {mode = 'n', lhs = ',g'},
> },
> dependency = {
> dependencies = {
> 'nvim-lua/plenary.nvim',
> 'nvim-lua/popup.nvim',
> 'nvim-telescope/telescope-fzy-native.nvim'
Expand All @@ -153,7 +153,7 @@ A key-value table of plugins. The key is a plugin's name like `alohaia/vim-hexow
- `map`(`table`): On which mapping(s) should the plugin be loaded.
- `enable`(`bool`, `function`): Whether to load this plugin.
- `branch`(`string`): Branch of the plugin. This is effective only at initial installation.
- `dependency`(`string`, `table`): Plugin's dependencies. A dependency should be in plugin list additionally and set `opt=true`.
- `dependencies`(`string`, `table`): Plugin's dependencies. A `dependencies` should be in the plugin list additionally.
- `disabled`(`bool`): Whether the plugin is disabled. Disabled plugins won't be installed or updated and will be removed while cleaning. The difference between disabled plugins and plugins that are not in the plugin list is that the former appears in completion list of packer [commands](#commands).
- `config`(`function`, `string`): Configuration for the plugin, Can be a function:
- Function: well be directly called in due course.
Expand Down
2 changes: 1 addition & 1 deletion init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ require 'aloha' {
strict_deps = true,
},
},
transparency = true,
transparency = true, -- may conflict with terminal styles
mapleader = ' ',
}
27 changes: 22 additions & 5 deletions lua/aloha/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,36 @@ return function(_configs)
end
})

-- set up mappings
-- set mapleader
vim.g.mapleader = aloha.map.leader
for _,map in pairs(aloha.map.list) do
vim.keymap.set(map[1], map[2], map[3], aloha.map.default_args + map[4])
-- set up mappings
if type(aloha.map.list.global_mappings) == "table" then
for _,map in pairs(aloha.map.list.global_mappings) do
vim.keymap.set(map[1], map[2], map[3], aloha.map.default_args + map[4])
end
end
if type(aloha.map.list.filetype_options) == "table" then
local ft_mappings_id = vim.api.nvim_create_augroup("aloha_ft_mappings", {clear=true})
for filetypes, mappings in pairs(aloha.map.list.filetype_options) do
vim.api.nvim_create_autocmd("FileType", {
group = ft_mappings_id,
pattern = filetypes,
callback = function ()
for _,map in ipairs(mappings) do
vim.keymap.set(map[1], map[2], map[3], aloha.map.default_args + map[4])
end
end
})
end
end

-- set up options
if aloha.options.global_options then
if type(aloha.options.global_options) == "table" then
for o,v in pairs(aloha.options.global_options) do
set_option(o, v)
end
end
if aloha.options.filetype_options then
if type(aloha.options.filetype_options) == "table" then
local ft_options_id = vim.api.nvim_create_augroup("aloha_ft_options", {clear=true})
for filetypes,options in pairs(aloha.options.filetype_options) do
vim.api.nvim_create_autocmd("FileType", {
Expand Down
26 changes: 15 additions & 11 deletions lua/aloha/packer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ local function exec(cmd, cwd, cmd_type)
end

local function is_opt(settings)
if settings.opt or settings.ft or settings.cmd or settings.enable ~= nil then
if settings.opt or settings.ft or settings.cmd or settings.map or settings.enable ~= nil then
return true
else
return false
Expand Down Expand Up @@ -454,16 +454,16 @@ local function load_dependencies(name)
warn('cant not find %s in configs', name)
is_success = false
else
local deps = packer.plugins[name].dependency
local deps = packer.plugins[name].dependencies
if type(deps) == 'string' then
if not packer:add(split(deps, '/')[2], deps) then
warn('failed to load dependency %s for %s', deps, name)
if is_opt(packer.plugins[deps]) and not packer:add(split(deps, '/')[2], deps) then
warn('failed to load dependencies %s for %s', deps, name)
is_success = false
end
elseif type(deps) == 'table' then
for _,dep in ipairs(deps) do
if not packer:add(split(dep, '/')[2], dep) then
warn('failed to load dependency %s for %s', dep, name)
if is_opt(packer.plugins[dep]) and not packer:add(split(dep, '/')[2], dep) then
warn('failed to load dependencies %s for %s', dep, name)
is_success = false
end
end
Expand Down Expand Up @@ -628,11 +628,15 @@ function packer:prepareOptPlugins()
-- map opt plugins
if settings.map then
for _,mapping in ipairs(settings.map) do
api.nvim_set_keymap(mapping.mode, mapping.lhs,
string.format([[<Cmd>lua aloha.packer.load_map('%s', '%s', '%s')<CR>]],
mapping.mode, mapping.lhs:gsub('<', '<lt>'), name),
{noremap = true, silent = true}
)
if not vim.fn.hasmapto(mapping.lhs, mapping.mode) then
api.nvim_set_keymap(mapping.mode, mapping.lhs,
string.format([[<Cmd>lua aloha.packer.load_map('%s', '%s', '%s')<CR>]],
mapping.mode, mapping.lhs:gsub('<', '<lt>'), name),
{noremap = true, silent = true}
)
-- else
-- warn('mapping to %s already exists', mapping.lhs)
end
end
end

Expand Down
Loading

0 comments on commit ddd8df6

Please sign in to comment.