Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. --[[
  2. Smart Indent for bash scripts in geany using lua plugin
  3. https://github.com/markuman/dotfiles/
  4. ]]--
  5. --[[
  6. -- some string help functions
  7. --]]
  8. function trim (str)
  9. return (str:gsub("^%s*(.-)%s*$", "%1"))
  10. end
  11. mt = getmetatable ("")
  12. mt.__index["trim"] = trim
  13. function isMember (str, list)
  14. for n = 1,#list do
  15. if (str:find (list[n], 1, true) == 1) then
  16. return true
  17. end
  18. end
  19. return false
  20. end
  21. -- Check content
  22. if (geany.text () == nil) then geany.status ("smartIndent: File is empty") return end
  23. valid_shebang = {
  24. bash = {"#!/bin/sh", "#!/bin/bash"},
  25. octave = {"#!/usr/bin/octave", "#!/usr/local/bin/octave"},
  26. lua = {"#!/usr/bin/lua", "#!/usr/local/bin/lua"}
  27. }
  28. -- settings
  29. TAB = "\t"
  30. rep = 0
  31. increase_words = {
  32. bash = {"for", "while", "if", "function", "{", "case", "until", "select"},
  33. octave = {"for", "while", "if", "function"},
  34. lua = {"for", "while", "if", "function"},
  35. }
  36. decrease_words = {
  37. bash = {"done", "fi", "}", "esac"},
  38. octave = {"end"},
  39. lua = {"end"}
  40. }
  41. neutral_words = {
  42. bash = {"else", "elif", "then", "do"},
  43. octave = {"else", "elseif"},
  44. lua = {"else", "elseif"}
  45. }
  46. -- look for a valid/known shebang in file
  47. for k,v in pairs(valid_shebang) do
  48. if (isMember (geany.lines (1), valid_shebang[k])) then
  49. increase_words = increase_words[k]
  50. decrease_words = decrease_words[k]
  51. neutral_words = neutral_words[k]
  52. geany.status ("smartIndent: " .. k)
  53. break
  54. end
  55. end
  56. local unknown = function()
  57. geany.status ("smartIndent: Unkown shebang")
  58. return
  59. end
  60. --[[
  61. uh this looks so ugly
  62. checking if we can follow indent rules by file extension instead of shebang
  63. --]]
  64. if (#increase_words == 0) then
  65. local filename = geany.filename ()
  66. if (filename ~= nil) then
  67. local file_extensions = {".sh", ".m", ".lua"}
  68. local file_ext_idx = {"bash", "octave", "lua"}
  69. for n = 1,#file_extensions do
  70. local from, to = filename:find (file_extensions[n], 1, true)
  71. if (from ~= nil and to ~= nil) then
  72. if (#filename == to) then
  73. local k = file_ext_idx[n]
  74. increase_words = increase_words[k]
  75. decrease_words = decrease_words[k]
  76. neutral_words = neutral_words[k]
  77. geany.status ("smartIndent: " .. k)
  78. break
  79. end
  80. end
  81. end
  82. else
  83. unknown ()
  84. return
  85. end
  86. end
  87. if (#increase_words == 0) then unknown () return end
  88. -- Object of reformatted content
  89. local this = {}
  90. setmetatable (this, {__index = table})
  91. local line = ""
  92. -- split content into table by new line
  93. for n in geany.lines() do
  94. line = geany.lines(n):trim()
  95. -- first decrease
  96. if (isMember (line, decrease_words)) then
  97. rep = rep - 1
  98. end
  99. -- indent current line
  100. if (#line > 0) then
  101. if (isMember (line, neutral_words)) then
  102. this:insert (TAB:rep (rep - 1))
  103. else
  104. this:insert (TAB:rep (rep))
  105. end
  106. end
  107. this:insert (line)
  108. this:insert ("\n")
  109. -- last increase
  110. if (isMember (line, increase_words)) then
  111. rep = rep + 1
  112. end
  113. end
  114. -- remove last new line
  115. this:remove()
  116. -- update content
  117. geany.text (this:concat ())