Skip to content

Commit

Permalink
Merge pull request #33 from beauwilliams/setup
Browse files Browse the repository at this point in the history
Setup refactor. Fixes #31
  • Loading branch information
beauwilliams authored Sep 11, 2021
2 parents 4b5e780 + 5f1aa94 commit e7fb73f
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 63 deletions.
76 changes: 40 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
[![GitHub issues-closed](https://img.shields.io/github/issues-closed/beauwilliams/focus.nvim.svg)](https://github.com/beauwilliams/focus.nvim/issues?q=is%3Aissue+is%3Aclosed)
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](http://makeapullrequest.com)

# Breaking Change

You must now run the setup() function to being using focus.

Details below and code snippets are in the readme to get you started.

# Auto-resizing Focused Splits/Windows for Neovim

Expand All @@ -19,7 +23,7 @@

👁️ Currently focussed split/window automagically maximised to the perfect viewing size according to golden ratio

🏃 Move to existing windows or else create new splits automatically, using single command + can specify a file to open
🏃 Move to existing windows or else create new splits automatically, using single command + can specify a file to open

⏱ Supports lazy loading via packer

Expand All @@ -34,17 +38,24 @@


## Installation
#### [vim-plug](https://github.com/junegunn/vim-plug)
```vim
Plug 'beauwilliams/focus.nvim'
```

#### [packer.nvim](https://github.com/wbthomason/packer.nvim)
```lua
use 'beauwilliams/focus.nvim'
-- Or lazy load with `module` option. See further down for info
-- use {'beauwilliams/focus.nvim', module = "focus"}
use {'beauwilliams/focus.nvim', config = require("focus").setup()}
-- Or lazy load with `module` option. See further down for info on how to lazy load when using FocusSplit commands
-- Or lazy load this plugin by creating an arbitrary command using the cmd option in packer.nvim
-- use { 'beauwilliams/focus.nvim', cmd = "FocusSplitNicely" }
-- use { 'beauwilliams/focus.nvim', cmd = "FocusSplitNicely", module = "focus",
-- config = function()
-- require("focus").setup({hybridnumber = true})
-- end
-- }
```

#### [vim-plug](https://github.com/junegunn/vim-plug)
```vim
Plug 'beauwilliams/focus.nvim'
"You must run setup() to begin using focus
lua require("focus").setup()
```

## Vim Commands
Expand Down Expand Up @@ -91,7 +102,7 @@ vim.api.nvim_set_keymap('n', '<c-l>', ':FocusSplitNicely<CR>', { silent = true }

## Auto Splitting Directionally

Instead of worrying about multiple commands and shortcuts, simply think about splits as to which direction you would like to go
Instead of worrying about multiple commands and shortcuts, *simply think about splits as to which direction you would like to go*.

Calling a focus split command i.e :FocusSplitRight will do one of two things, **it will attempt to move across to the window** in the specified direction.
Otherwise, **if no window exists in the specified direction** relative to the current window **then it will instead create a new blank buffer window** in the direction specified,
Expand All @@ -110,7 +121,7 @@ local focusmap = function(direction)
vim.api.nvim_set_keymap('n', '<Leader>'..direction, ":lua require'focus'.split_command('"..direction.."')<CR>", { silent = true })
end
-- Use `<Leader>h` to split the screen to the left, same as command FocusSplitLeft etc
focusmap('h')
focusmap('h')
focusmap('j')
focusmap('k')
focusmap('l')
Expand All @@ -127,98 +138,91 @@ You can also specify a mapping, or perhaps a function to even add lazy loading.

## Configuration

Place some version of this in your configuration file, e.g. `init.lua`, etc.

**NOTE:** If for example your screen resolution is *1024x768* --> i.e on the smaller side, you may notice that focus by default can maximise a window *too much*.
That is, the window will sort of 'crush' some of your other splits due to the limited screen real estate. This is not an issue with focus,
but an issue with minimal screen real estate. In this case, you can simply reduce the width/height of focus by following the below instructions to set them.

**Example Configuration**
```lua
require("focus").setup({enable = true, cursorline = true, signcolumn = true, hybridnumber = true})
```
### Available Options

**Enable/Disable Focus**
```lua
local focus = require('focus')
-- Completely disable this plugin
-- Default: true
focus.enable = false
require("focus").setup({enable = false})
```

**Set Focus Width**
```lua
local focus = require('focus')
-- Force width for the focused window
-- Default: Calculated based on golden ratio
focus.width = 120
require("focus").setup({width = 120})
```

**Set Focus Height**
```lua
local focus = require('focus')
-- Force height for the focused window
-- Default: Calculated based on golden ratio
focus.height = 40
require("focus").setup({height = 40})
```

**Set Focus Tree Width**
```lua
local focus = require('focus')
-- Sets the width of directory tree buffers such as NerdTree, NvimTree and CHADTree
-- Default: vim.g.nvim_tree_width or 30
focus.treewidth = 20
require("focus").setup({treewidth = 20})
```

**Set Focus Auto Cursorline**
```lua
local focus = require('focus')
-- Displays a cursorline in the focussed window only
-- Not displayed in unfocussed windows
-- Default: true
focus.cursorline = false
require("focus").setup({cursorline = false})
```

**Set Focus Auto Sign Column**
```lua
local focus = require('focus')
-- Displays a sign column in the focussed window only
-- Not displayed in unfocussed windows
-- Default: true
focus.signcolumn = false
require("focus").setup({signcolumn = false})
```

**Set Focus Auto Numbers**
```lua
local focus = require('focus')
-- Displays line numbers in the focussed window only
-- Not displayed in unfocussed windows
-- Default: true
focus.number = false
require("focus").setup({number = false})
```

**Set Focus Auto Relative Numbers**
```lua
local focus = require('focus')
-- Displays relative line numbers in the focussed window only
-- Not displayed in unfocussed windows
-- See :h relativenumber
-- Default: false
focus.relativenumber = true
require("focus").setup({relativenumber = true})
```

**Set Focus Auto Hybrid Numbers**
```lua
local focus = require('focus')
-- Displays hybrid line numbers in the focussed window only
-- Not displayed in unfocussed windows
-- Combination of :h relativenumber, but also displays the line number of the current line only
-- Default: false
focus.hybridnumber = true
require("focus").setup({hybridnumber = true})
```

**Set Focus Window Highlighting**
```lua
local focus = require('focus')
-- Enable auto highlighting for focussed/unfocussed windows
-- Default: false
focus.winhighlight = true
require("focus").setup({winhighlight = true})

-- By default, the highlight groups are setup as such:
-- hi default link FocusedWindow VertSplit
Expand All @@ -231,9 +235,9 @@ vim.cmd('hi link FocusedWindow VisualNOS')

## Planned Improvements 😼

- [ ] Refactoring
- [ ] Adding `:h filetype` support as we go
- [x] Adding Auto Line Numbers, options for relative,norelative
- [x] Refactoring
- [ ] Adding more filetype support as we go
- [x] Adding Auto Line Numbers, options for relative, norelative

# FAQ

Expand Down
4 changes: 0 additions & 4 deletions autoload/focus.vim

This file was deleted.

30 changes: 21 additions & 9 deletions lua/focus/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,26 @@ local functions = require('focus.modules.functions')

local M = {}

M.init = function()
M.setup = function(options)
setmetatable(M, {
__newindex = config.set,
__index = config.get,
})
-- if options provided to setup, override defaults
if options ~= nil then
for k, v1 in pairs(options) do
config.defaults[k] = v1
end
end
-- Verify that configuration values are of the correct type
config.verify()

if M.enable == true then
-- Don't set up focus if its not enabled by the user
if M.enable then
-- Pass this module, noting that `__index` actually references the
-- configuration module, to setup the autocmds used for this plugin
commands.setup()
autocmd.setup(M)
resizer.split_resizer(M)
commands.setup()

if M.winhighlight then
-- Allows user-overridable highlighting of the focused window
Expand All @@ -27,9 +37,16 @@ M.init = function()

vim.wo.winhighlight = 'Normal:FocusedWindow,NormalNC:UnfocusedWindow'
end

-- Finally begin resizing when enabled and configs set
M.resize()
end
end

M.resize = function()
resizer.split_resizer(M)
end

-- Exported internal functions for use in commands etc
function M.split_nicely()
split.split_nicely()
Expand All @@ -52,9 +69,4 @@ function M.focus_toggle()
functions.focus_toggle()
end

setmetatable(M, {
__newindex = config.set,
__index = config.get,
})

return M
10 changes: 5 additions & 5 deletions lua/focus/modules/autocmd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ function autocmd.setup(config)
local autocmds = {
focus_init = {
-- Resize files with typical naming convention *.* i.e focus.lua
{ 'BufEnter', '*.*', ':lua require"focus".init()' },
{ 'BufEnter', '*.*', ':lua require"focus".resize()' },
-- Resize files with no filetype
{ 'Filetype', '', ':lua require"focus".init()' },
{ 'Filetype', '', ':lua require"focus".resize()' },
-- Resize startify
{ 'BufEnter', 'startify', ':lua require"focus".init()' },
-- So that we can resize windows such as NvimTree correctly, we run init when we open a buffer
{ 'BufEnter,WinEnter', 'NvimTree,nerdtree,CHADTree,qf', ":lua require'focus'.init()" },
{ 'BufEnter', 'startify', ':lua require"focus".resize()' },
-- So that we can resize windows such as NvimTree correctly, we run resize when we open a buffer
{ 'BufEnter,WinEnter', 'NvimTree,nerdtree,CHADTree,qf', ":lua require'focus'.resize()" },
},
}

Expand Down
10 changes: 1 addition & 9 deletions plugin/focus.vim
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
if exists('g:loaded_focus') | finish | endif

let g:loaded_focus = 1 "Don't Reload Twice"
let g:enabled_focus = 1
let g:enabled_focus = 1 "Focus is enabled when setup is run, we use this var to enable/disable/toggle

"A vim best practise
let s:save_cpo = &cpo
set cpo&vim


"hopefully focus is not loaded if we run vim with focus disabled i.e `nvim +DisableFocus`
if g:enabled_focus == 1
runtime autoload/focus.vim
endif



let &cpo = s:save_cpo
unlet s:save_cpo

0 comments on commit e7fb73f

Please sign in to comment.