Skip to content

Commit

Permalink
Feat: Add buffer-specific toggles
Browse files Browse the repository at this point in the history
Closes #2.
  • Loading branch information
hankertrix committed Feb 11, 2025
1 parent d38617f commit b180bd3
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 5 deletions.
39 changes: 38 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ when it is not needed.

## Table of contents

- [Video Demonstration](#video-demonstration)
- [Video demonstration](#video-demonstration)
- [Requirements](#requirements)
- [Installation](#installation)
- [Configuration](#configuration)
- [Buffer-specific toggle](#buffer-specific-toggle)
- [Commands](#commands)
- [Licence](#licence)

## Video demonstration

Expand Down Expand Up @@ -273,6 +275,23 @@ you can use the following snippet to access it.
require("nerd_column").default_config
```

## Buffer-specific toggle

You can control the state of the plugin via the use of the global option
`vim.g.nerd_column_enabled` and the buffer-local option
`vim.b.nerd_column_enabled`. If you have set the plugin to be enabled
by default by setting `enabled` to `true` in your configuration, the global
option `vim.g.nerd_column_enabled` will be automatically set to `true`
when the plugin is set up.

The value of the buffer-local option `vim.b.nerd_column_enabled` is checked
first, and if it is not set, i.e. `vim.b.nerd_column_enabled` is `nil`,
then the value of the global option `vim.g.nerd_column_enabled`
will be used instead.

This way, you can disable the plugin in specific buffers by setting
`vim.b.nerd_column_enabled` to `false`.

## Commands

Nerd Column provides 3 commands:
Expand All @@ -284,20 +303,38 @@ Nerd Column provides 3 commands:
require("nerd_column").enable()
```

This command will set both `vim.g.nerd_column_enabled` and
`vim.b.nerd_column_enabled` to `true`, as most users would
expect the command to work globally.

- `NerdColumnDisable` to disable the plugin.
It can also be accessed from Lua be using:

```lua
require("nerd_column").disable()
```

This command will set both `vim.g.nerd_column_enabled` and
`vim.b.nerd_column_enabled` to `false`, as most users would
expect the command to work globally.

- `NerdColumnToggle` to toggle the plugin.
It can also be accessed from Lua be using:

```lua
require("nerd_column").toggle()
```

This command will get the current state of the plugin from
`vim.b.nerd_column_enabled` first. If `vim.b.nerd_column_enabled`
is not set, i.e. `vim.b.nerd_column_enabled` is `nil`, then
it will take the value of `vim.g.nerd_column_enabled`.
Then, the command will toggle the plugin state, and set both
`vim.g.nerd_column_enabled` and `vim.b.nerd_column_enabled` to the
toggled state. The reason for this is that most users would expect
the command to work globally, and that the toggle should act on
the current state of the plugin in the current buffer.

## [Licence]

This plugin is licenced under the [GNU AGPL v3 licence][Licence].
Expand Down
46 changes: 42 additions & 4 deletions lua/nerd_column.lua
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,35 @@ local function match_file_type(given_file_type, list_of_file_types)
return false
end

-- The function to get whether the plugin is enabled or not
---@return boolean nerd_column_is_enabled Whether the plugin is enabled or not
local function nerd_column_is_enabled()
--

-- Get the value of buffer local variable for
-- whether the plugin is enabled
local plugin_enabled = vim.b.nerd_column_enabled

-- If the buffer local variable isn't set,
-- then get the value of the global variable
if plugin_enabled == nil then plugin_enabled = vim.g.nerd_column_enabled end

-- Return whether the plugin is enabled or not
return plugin_enabled
end

-- The function to set the plugin state
---@param state boolean
---@return nil
local function set_nerd_column_state(state)
--

-- Set the plugin state in both the global variable
-- and the buffer local variable
vim.g.nerd_column_enabled = state
vim.b.nerd_column_enabled = state
end

-- The function to disable the colour column
---@param window integer The ID of the window in the buffer
---@return nil
Expand All @@ -231,7 +260,9 @@ local function on_change()

-- If the plugin is disabled, disable the colour column
-- and exit the function
if not config.enabled then return disable_colour_column(current_window) end
if not nerd_column_is_enabled() then
return disable_colour_column(current_window)
end

-- Get the current buffer
local current_buffer = vim.api.nvim_win_get_buf(current_window)
Expand Down Expand Up @@ -349,7 +380,7 @@ M.enable = function()
--

-- Enable the plugin
config.enabled = true
set_nerd_column_state(true)

-- Call the on change function
on_change()
Expand All @@ -360,7 +391,7 @@ M.disable = function()
--

-- Disable the plugin
config.enabled = false
set_nerd_column_state(false)

-- Disable the colour column
disable_colour_column(vim.api.nvim_get_current_win())
Expand All @@ -370,8 +401,11 @@ end
M.toggle = function()
--

-- Get the current state of the plugin
local is_enabled = nerd_column_is_enabled()

-- Toggle the plugin
config.enabled = not config.enabled
set_nerd_column_state(not is_enabled)

-- Call the on change function
on_change()
Expand All @@ -388,6 +422,10 @@ M.setup = function(user_config)
-- Merge the user's configuration with the default configuration
config = vim.tbl_extend("force", config, user_config)

-- If the plugin is enabled, set the vim global option
-- for the plugin to enabled
if config.enabled then vim.g.nerd_column_enabled = true end

-- Create the auto command to set the colour column
vim.api.nvim_create_autocmd(
{ "BufEnter", "CursorMoved", "CursorMovedI", "WinScrolled" },
Expand Down

0 comments on commit b180bd3

Please sign in to comment.