aboutsummaryrefslogtreecommitdiff
path: root/plugins/drawwhitespace.lua
blob: 0e4cf1945402c63ce69a13baf7adb76a29fa64ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
local config = require "core.config"
local style = require "core.style"
local DocView = require "core.docview"
local command = require "core.command"

-- originally written by luveti

config.whitespace_map = { [" "] = "·", ["\t"] = "»" }
config.draw_whitespace = true

local draw_line_text = DocView.draw_line_text

function DocView:draw_line_text(idx, x, y)
  draw_line_text(self, idx, x, y)
  if not config.draw_whitespace then return end

  local text = self.doc.lines[idx]
  local tx, ty = x, y + self:get_line_text_y_offset()
  local font = self:get_font()
  local color = style.whitespace or style.syntax.comment
  local map = config.whitespace_map

  for i = 1, #text do
    local chr = text:sub(i, i)
    local rep = map[chr]
    if rep then
      renderer.draw_text(font, rep, tx, ty, color)
    end
    tx = tx + font:get_width(chr)
  end
end

command.add("core.docview", {
  ["drawwhitespace:toggle"] = function() config.draw_whitespace = not config.draw_whitespace end,
  ["drawwhitespace:no"]     = function() config.draw_whitespace = false                      end,
  ["drawwhitespace:yes"]    = function() config.draw_whitespace = true                       end,
})