├── README.md ├── detectindent.lua ├── help └── detectindent.md └── repo.json /README.md: -------------------------------------------------------------------------------- 1 | # Detectindent plugin for Micro text editor 2 | 3 | Whenever a file is opened, the detectindent plugin tries to automatically detect its indentation style (spaces or tabs) and set the `tabstospaces` option accordingly. If spaces indentation is detected, it also tries to detect the indentation width and set the `tabsize` option accordingly. 4 | -------------------------------------------------------------------------------- /detectindent.lua: -------------------------------------------------------------------------------- 1 | VERSION = "1.1.1" 2 | 3 | local config = import("micro/config") 4 | local util = import("micro/util") 5 | 6 | function onBufferOpen(buf) 7 | local spaces, tabs = 0, 0 8 | local space_count, prev_space_count = 0, 0 9 | local tabsizes = {} 10 | local i = 0 11 | while spaces + tabs < 500 and i < 1000 and i < buf:LinesNum() do 12 | space_count = 0 13 | local line = buf:Line(i) 14 | local r = util.RuneAt(line, 0) 15 | if r == " " then 16 | space_count = string.len(util.GetLeadingWhitespace(line)) 17 | -- for tab vs space detection, ignore space indents of 1 18 | -- which can be a false signal from C-style block comments 19 | if space_count > 1 then 20 | spaces = spaces + 1 21 | end 22 | 23 | -- treat whitespace-only lines as not changing the indent 24 | if string.len(line) == space_count then 25 | space_count = prev_space_count 26 | end 27 | -- look for an increasing number of spaces, ignoring increases of 1 28 | -- which can be a false signal from C-style block comments 29 | if space_count > prev_space_count + 1 then 30 | local t = space_count - prev_space_count 31 | if tabsizes[t] == nil then 32 | tabsizes[t] = 1 33 | else 34 | tabsizes[t] = tabsizes[t] + 1 35 | end 36 | end 37 | elseif r == "\t" then 38 | tabs = tabs + 1 39 | -- treat tabbed lines as not affecting spaced indentation 40 | space_count = prev_space_count 41 | elseif r == "" then 42 | -- treat empty lines as not changing the indent 43 | space_count = prev_space_count 44 | end 45 | prev_space_count = space_count 46 | i = i + 1 47 | end 48 | 49 | if spaces > tabs then 50 | buf:SetOptionNative("tabstospaces", true) 51 | -- get the indentation change used for the largest number of lines 52 | local tabsize = -1 53 | local maxcount = 0 54 | for t, count in pairs(tabsizes) do 55 | if count > maxcount then 56 | maxcount = count 57 | tabsize = t 58 | end 59 | end 60 | if tabsize > 0 then 61 | buf:SetOptionNative("tabsize", tabsize) 62 | end 63 | elseif tabs > spaces then 64 | buf:SetOptionNative("tabstospaces", false) 65 | end 66 | end 67 | 68 | function init() 69 | config.AddRuntimeFile("detectindent", config.RTHelp, "help/detectindent.md") 70 | end 71 | -------------------------------------------------------------------------------- /help/detectindent.md: -------------------------------------------------------------------------------- 1 | # Detectindent 2 | 3 | Whenever a file is opened, the detectindent plugin tries to automatically 4 | detect its indentation style (spaces or tabs) and set the `tabstospaces` 5 | option accordingly. If spaces indentation is detected, it also tries to 6 | detect the indentation width and set the `tabsize` option accordingly. 7 | 8 | To disable it, just turn off the plugin: 9 | 10 | ``` 11 | set detectindent off 12 | ``` 13 | -------------------------------------------------------------------------------- /repo.json: -------------------------------------------------------------------------------- 1 | [{ 2 | "Name": "detectindent", 3 | "Description": "Automatically detect indentation settings", 4 | "Tags": ["indent", "detect"], 5 | "Versions": [ 6 | { 7 | "Version": "1.1.1", 8 | "Url": "https://github.com/dmaluka/micro-detectindent/archive/v1.1.1.zip", 9 | "Require": { 10 | "micro": ">=2.0.0" 11 | } 12 | } 13 | ] 14 | }] 15 | --------------------------------------------------------------------------------