diff --git a/geany/smartIndent.lua b/geany/smartIndent.lua index 93f85ce..01281eb 100644 --- a/geany/smartIndent.lua +++ b/geany/smartIndent.lua @@ -22,23 +22,84 @@ function isMember (str, list) end -- Check content -valid_shebang = {"#!/bin/sh", "#!/bin/bash"} -if (geany.text () == nil) then return end -if not (isMember (geany.lines (1), valid_shebang)) then return end +if (geany.text () == nil) then geany.status ("smartIndent: File is empty") return end +valid_shebang = { + bash = {"#!/bin/sh", "#!/bin/bash"}, + octave = {"#!/usr/bin/octave", "#!/usr/local/bin/octave"}, + lua = {"#!/usr/bin/lua", "#!/usr/local/bin/lua"} +} + +-- settings +TAB = "\t" +rep = 0 + +increase_words = { + bash = {"for", "while", "if", "function", "{", "case", "until", "select"}, + octave = {"for", "while", "if", "function"}, + lua = {"for", "while", "if", "function"}, +} +decrease_words = { + bash = {"done", "fi", "}", "esac"}, + octave = {"end"}, + lua = {"end"} +} +neutral_words = { + bash = {"else", "elif", "then", "do"}, + octave = {"else", "elseif"}, + lua = {"else", "elseif"} +} + +-- look for a valid/known shebang in file +for k,v in pairs(valid_shebang) do + if (isMember (geany.lines (1), valid_shebang[k])) then + increase_words = increase_words[k] + decrease_words = decrease_words[k] + neutral_words = neutral_words[k] + geany.status ("smartIndent: " .. k) + break + end +end + +local unknown = function() + geany.status ("smartIndent: Unkown shebang") + return +end + +--[[ +uh this looks so ugly +checking if we can follow indent rules by file extension instead of shebang +--]] +if (#increase_words == 0) then + local filename = geany.filename () + if (filename ~= nil) then + local file_extensions = {".sh", ".m", ".lua"} + local file_ext_idx = {"bash", "octave", "lua"} + for n = 1,#file_extensions do + local from, to = filename:find (file_extensions[n], 1, true) + if (from ~= nil and to ~= nil) then + if (#filename == to) then + local k = file_ext_idx[n] + increase_words = increase_words[k] + decrease_words = decrease_words[k] + neutral_words = neutral_words[k] + geany.status ("smartIndent: " .. k) + break + end + end + end + else + unknown () + return + end +end +if (#increase_words == 0) then unknown () return end -- Object of reformatted content local this = {} setmetatable (this, {__index = table}) --- settings -TAB = "\t" -rep = 0 -increase_words = {"for", "while", "if", "function", "{", "case", "until", "select"} -decrease_words = {"done", "fi", "}", "esac"} -neutral_words = {"else", "elif", "then", "do"} local line = "" - -- split content into table by new line for n in geany.lines() do line = geany.lines(n):trim()