Skip to content

Commit

Permalink
Add optional argument to :GuessIndent to respect user config (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
heygarrett authored Jul 17, 2022
1 parent 3c17b9a commit c37467b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
6 changes: 5 additions & 1 deletion doc/guess_indent.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ instead.
`filetype_exclude` A list of file types. If you open a buffer and its
'filetype' is contained in this list, then
guess-indent won't run automatically. Note this only
changes the behavior if `auto_cmd` is set to true.
changes the behavior if `auto_cmd` is set to true or if
`:GuessIndent`is run with argument `auto_cmd` .

`buftype_exclude` Same as `filetype_exclude` but for 'buftype' instead.

Expand All @@ -68,6 +69,9 @@ COMMANDS *GuessIndent-commands*
Guess-indent respects the 'verbose' option. This means that you can run it
using `:verbose GuessIndent` to get verbose output.

If you want to run guess-indent from your own auto command you can specify the
context as an argument using `:GuessIndent auto_cmd` .

==============================================================================
LICENSE *GuessIndent-license*
>
Expand Down
12 changes: 6 additions & 6 deletions lua/guess-indent/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ local M = {}

local function setup_commands()
vim.cmd([[
command! GuessIndent :lua require("guess-indent").set_from_buffer()
command! -nargs=? GuessIndent :lua require("guess-indent").set_from_buffer("<args>")
]])
end

local function setup_autocommands()
vim.cmd([[
augroup GuessIndent
autocmd!
autocmd BufReadPost * silent lua require("guess-indent").set_from_buffer(true)
autocmd BufReadPost * silent lua require("guess-indent").set_from_buffer("auto_cmd")
" Run once when saving for new files
autocmd BufNewFile * autocmd BufWritePost <buffer=abuf> ++once silent lua require("guess-indent").set_from_buffer(true)
autocmd BufNewFile * autocmd BufWritePost <buffer=abuf> ++once silent lua require("guess-indent").set_from_buffer("auto_cmd")
augroup END
]])
end
Expand Down Expand Up @@ -263,10 +263,10 @@ function M.guess_from_buffer()
end

-- Set the indentation based on the contents of the current buffer.
-- The argument `auto_cmd` should only be set to true if this function gets
-- The argument `context` should only be set to `auto_cmd` if this function gets
-- called by an auto command.
function M.set_from_buffer(auto_cmd)
if auto_cmd then
function M.set_from_buffer(context)
if context == "auto_cmd" then
-- Filter
local filetype = vim.bo.filetype
local buftype = vim.bo.buftype
Expand Down

0 comments on commit c37467b

Please sign in to comment.