summaryrefslogtreecommitdiff
path: root/config/essentials/vis/visrc.lua
blob: 60162bc4127fe6e801a4a4cc24bdff12a8c49aaa (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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
------------------------------------
--- LIBRARIES
------------------------------------
require("vis")

-- plugins
require("build")
require("backup")
require("cursors")
require("title")
require("commentary")
require("complete-line")
local format = require("format")

-- todo:
-- c-scope
-- c-tags

------------------------------------
--- VARIABLES
------------------------------------

local m = vis.modes

------------------------------------
--- FUNCTIONS
------------------------------------

local function map_cmd(mode, map, command, help)
	vis:map(mode, map, function()
		vis:command(command)
	end, help)
end

-- TOOD: use window selection to restore position
local function wrap_restore(f, ...)
	local pos = vis.win.selection.pos
	f(...)
	vis.win.selection.pos = pos
end

local function map_keys(mode, map, keys, help)
	vis:map(mode, map, function()
		vis:feedkeys(keys)
	end, help)
end

------------------------------------
--- COMMANDS
-----------------------------------

vis:command_register("make", function()
	vis:command("!make && head -n 1")
end, "make")
vis:command_register("Q", function()
	vis:command("qa!")
end, "Quit all")
vis:command_register("delws", function()
	vis:command("x/[ \t]+$|^[ \t]+$/d")
end, "Remove trailing whitespace")

-------------------------------------
--- MAPPINGS
-------------------------------------

vis:map(m.NORMAL, " r", function()
	wrap_restore(vis.command, vis, "e $vis_filepath")
end, "Reload active file")

vis:map(m.NORMAL, "=", format.apply, "Format active file")

map_cmd(m.NORMAL, " c", "e ~/.config/vis/visrc.lua", "Edit config file")
map_cmd(m.NORMAL, " q", "q!", "Quit (force)")
map_cmd(m.NORMAL, " s", "!doas vis $vis_filepath", "Edit as superuser")
map_cmd(m.NORMAL, " w", "w", "Write")
map_cmd(m.NORMAL, " x", "!chmod u+x $vis_filepath", "Make active file executable")

vis:map(m.NORMAL, " eh", function()
	vis:command("!lowdown $vis_filepath > ${vis_filepath%.md}.html")
	vis:info("exported.")
end, "Export markdown to html")

map_keys(m.NORMAL, " nl", ":<seq -f '%0.0f. ' 1 ", "Insert numbered list")

-- select markdown list element:	,x/^(\d+\.|[-*])\s+.+\n(^ .+\n)*/

------------------------------------
--- EVENTS
------------------------------------

vis.events.subscribe(vis.events.INIT, function()
	vis.options.ignorecase = true
	vis.options.autoindent = true
	vis.options.shell = "/bin/sh"
	local theme = "nord"
	vis:command("set theme " .. theme)
end)

vis.events.subscribe(vis.events.WIN_OPEN, function(win) -- luacheck: no unused args
	win.options.relativenumbers = true

	if win.syntax == "bash" then
		map_keys(
			m.NORMAL,
			" v",
			"V:x/^(\\s*)(.+)$/ c/\\1>\\&2 printf '\\2: %s\\\\n' \"$\\2\"/<Enter><Escape>",
			"Print variable"
		)
	end

	vis:command_register("pipe", function()
		vis:pipe(win.file, nil, "sed 's/.*/- &/'")
	end, "pipe test")
end)