logo

oasis-root

Compiled tree of Oasis Linux based on own branch at <https://hacktivis.me/git/oasis/> git clone https://anongit.hacktivis.me/git/oasis-root.git

vis-std.lua (3998B)


  1. -- standard vis event handlers
  2. vis.events.subscribe(vis.events.INIT, function()
  3. if os.getenv("TERM_PROGRAM") == "Apple_Terminal" then
  4. vis:command("set change256colors false")
  5. end
  6. vis:command("set theme default")
  7. end)
  8. vis:option_register("theme", "string", function(name)
  9. if name ~= nil then
  10. local theme = 'themes/'..name
  11. package.loaded[theme] = nil
  12. require(theme)
  13. end
  14. local lexers = vis.lexers
  15. lexers.lexers = {}
  16. if not lexers.load then return false end
  17. if not lexers.property then lexers.load("text") end
  18. local colors = lexers.colors
  19. local default_colors = { "black", "red", "green", "yellow", "blue", "magenta", "cyan", "white" }
  20. for _, c in ipairs(default_colors) do
  21. if not colors[c] or colors[c] == '' then
  22. colors[c] = c
  23. end
  24. end
  25. for win in vis:windows() do
  26. win:set_syntax(win.syntax)
  27. end
  28. return true
  29. end, "Color theme to use, filename without extension")
  30. vis:option_register("syntax", "string", function(name)
  31. if not vis.win then return false end
  32. if not vis.win:set_syntax(name) then
  33. vis:info(string.format("Unknown syntax definition: `%s'", name))
  34. return false
  35. end
  36. return true
  37. end, "Syntax highlighting lexer to use")
  38. vis:option_register("horizon", "number", function(horizon)
  39. if not vis.win then return false end
  40. vis.win.horizon = horizon
  41. return true
  42. end, "Number of bytes to consider for syntax highlighting")
  43. vis.events.subscribe(vis.events.WIN_HIGHLIGHT, function(win)
  44. if not win.syntax or not vis.lexers.load then return end
  45. local lexer = vis.lexers.load(win.syntax, nil, true)
  46. if not lexer then return end
  47. -- TODO: improve heuristic for initial style
  48. local viewport = win.viewport.bytes
  49. if not viewport then return end
  50. local horizon_max = win.horizon or 32768
  51. local horizon = viewport.start < horizon_max and viewport.start or horizon_max
  52. local view_start = viewport.start
  53. local lex_start = viewport.start - horizon
  54. viewport.start = lex_start
  55. local data = win.file:content(viewport)
  56. local token_styles = lexer._TAGS
  57. local tokens = lexer:lex(data, 1)
  58. local token_end = lex_start + (tokens[#tokens] or 1) - 1
  59. for i = #tokens - 1, 1, -2 do
  60. local token_start = lex_start + (tokens[i-1] or 1) - 1
  61. if token_end < view_start then
  62. break
  63. end
  64. local name = tokens[i]
  65. local style = token_styles[name]
  66. if style ~= nil then
  67. win:style(style, token_start, token_end)
  68. end
  69. token_end = token_start - 1
  70. end
  71. end)
  72. local modes = {
  73. [vis.modes.NORMAL] = '',
  74. [vis.modes.OPERATOR_PENDING] = '',
  75. [vis.modes.VISUAL] = 'VISUAL',
  76. [vis.modes.VISUAL_LINE] = 'VISUAL-LINE',
  77. [vis.modes.INSERT] = 'INSERT',
  78. [vis.modes.REPLACE] = 'REPLACE',
  79. }
  80. vis.events.subscribe(vis.events.WIN_STATUS, function(win)
  81. local left_parts = {}
  82. local right_parts = {}
  83. local file = win.file
  84. local selection = win.selection
  85. local mode = modes[vis.mode]
  86. if mode ~= '' and vis.win == win then
  87. table.insert(left_parts, mode)
  88. end
  89. table.insert(left_parts, (file.name or '[No Name]') ..
  90. (file.modified and ' [+]' or '') .. (vis.recording and ' @' or ''))
  91. local count = vis.count
  92. local keys = vis.input_queue
  93. if keys ~= '' then
  94. table.insert(right_parts, keys)
  95. elseif count then
  96. table.insert(right_parts, count)
  97. end
  98. if #win.selections > 1 then
  99. table.insert(right_parts, selection.number..'/'..#win.selections)
  100. end
  101. local size = file.size
  102. local pos = selection.pos
  103. if not pos then pos = 0 end
  104. table.insert(right_parts, (size == 0 and "0" or math.ceil(pos/size*100)).."%")
  105. if not win.large then
  106. local col = selection.col
  107. table.insert(right_parts, selection.line..', '..col)
  108. if size > 33554432 or col > 65536 then
  109. win.large = true
  110. end
  111. end
  112. local left = ' ' .. table.concat(left_parts, " » ") .. ' '
  113. local right = ' ' .. table.concat(right_parts, " « ") .. ' '
  114. win:status(left, right);
  115. end)
  116. -- default plugins
  117. require('plugins/filetype')
  118. require('plugins/textobject-lexer')
  119. require('plugins/digraph')
  120. require('plugins/number-inc-dec')
  121. require('plugins/complete-word')
  122. require('plugins/complete-filename')