summaryrefslogblamecommitdiffstats
path: root/pairs.lua
blob: 8ad1ea2d03ed788e61ec9f977f0e316a26233263 (plain) (tree)
1
2
3
4
5
6
7
8
9
10









                                            
                             



                              







                                               


                  


                                                                                                    

                
                                                               
 

                                                           
                                             
                                           


                                                     




                                                        






                                                                                
                                                   

   
                                              
                        
                                                           
                                                                                                      







                                                             

                                                                                       




                                    




                                                   

                                                     





                                              











                                                                        



                                          
                      
                        



                                                                                                                                                  

   


                                                          
               






                                                                                               

                                                                                                 



                                                                                       

                                                       
                                                                          
                                                                    









                                                                                        

                                                  
                                
                                 
              

                                                                     
                                                        

                                      
                    
                                                  
                   
                                         
                                                                                                                
                                                           
                                                                                    




                                                                                                            
                                            
                   
                                     

                                        
                                                 


                              

   
                                                    



                                                                               
                                                                                   




                            

                                            
                                                                                       

                                                               
                                
                                                                                                                                        
                          
                                             
           

   

                                                                 



                                                 

                                                                 
                                                 
                                                          

   

                                                            

                                           
                                                                               

                             
                                                                                                                                                    
                                                       




                   

                                                             

                                        
                                                                             

                              

                                                                                                       




                   













                                           
                         


                                                                  
                                              


           
                                                            
                                               



                            



                                                       
                                                


                                    


                                                                        

                                                                          







                                                                                                        



                 
                                                
                    

                                                                                                                                               
         
                        

                                                                                                                               
         
 

                                                                                

                                                                                                                                                                                    
                                                                                            
                                                                                                                                                                                          

                                                                                                                                                                                                               
                                                                                                                                                                  
                                                                                                                                                                                                           
                                                
                         
                              
                                    





                                          


                                            
                                              










                                                                                               


     
                 
                                  

                                
                                                                          


        
-- SPDX-License-Identifier: GPL-3.0-or-later
-- © 2020 Georgi Kirilov

require("vis")
local vis = vis

local l = require("lpeg")

local M

local builtin_textobjects = {
	["["] = { "[" , "]" },
	["{"] = { "{" , "}" },
	["<"] = { "<" , ">" },
	["("] = { "(" , ")" },
	['"'] = { '"' , '"' },
	["'"] = { "'" , "'" },
	["`"] = { "`" , "`" },
}

local builtin_motions = {
	["["] = { ["("] = true, ["{"] = true },
	["]"] = { [")"] = true, ["}"] = true },
}

local aliases = {}
for key, pair in pairs(builtin_textobjects) do aliases[pair[2]] = key ~= pair[2] and pair or nil end
for alias, pair in pairs(aliases) do builtin_textobjects[alias] = pair end
for alias, key in pairs({
	B = "{",
	b = "(",
}) do builtin_textobjects[alias] = builtin_textobjects[key] end

local function get_pair(key, win)
	return M.map[win.syntax] and M.map[win.syntax][key]
		or M.map[1] and M.map[1][key]
		or builtin_textobjects[key]
		or not key:match("%w") and {key, key}
end

local function at_pos(t, pos)
	if pos >= t[1] and pos < t[#t] then return t end
end

local function asymmetric(d, pos)
	local p
	local I = l.Cp()
	if #d == 1 then
		p = (d - l.B"\\") * I * ("\\" * l.P(1) + (l.P(1) - d))^0 * I * d
	else
		p = d * I * (l.P(1) - d)^0 * I * d
	end
	return l.Ct(I * p * I) * l.Cc(pos) / at_pos
end

local function symmetric(d1, d2, escaped, pos)
	local I = l.Cp()
	local skip = escaped and escaped + l.P(1) or l.P(1)
	return l.P{l.Ct(I * d1 * I * ((skip - d1 - d2) + l.V(1))^0 * I * d2 * I) * l.Cc(pos) / at_pos}
end

local function nth(t)
	if type(t) == "table" then
		local start, finish, c = 0, 0, vis.count or 1
		if #t == 5 then
			start, finish, c = nth(t[3])
		end
		if c then
			return {t[1], t[2]}, {t[#t - 1], t[#t]}, c > 1 and c - 1 or nil
		end
		return start, finish
	end
end

local precedence = {
	[vis.lexers.COMMENT] = {vis.lexers.STRING},
	[vis.lexers.STRING] = {},
}

local function selection_range(win, pos)
	for selection in win:selections_iterator() do
		if selection.pos == pos then
			return selection.range
		end
	end
end

local prev_match

local function any_captures(_, position, t)
	if type(t) == "table" then
		return position, t
	end
	prev_match = position
end

local function not_past(_, position, pos)
	local newpos =  prev_match > position and prev_match or position
	return newpos <= pos and newpos or false
end

local function match_at(str, pattern, pos)
	local string_pos = pos + 1
	prev_match = 0
	local I = l.Cp()
	local p = l.P{l.Cmt(l.Ct(I * (pattern/0) * I) * l.Cc(string_pos) / at_pos, any_captures) + 1 * l.Cmt(l.Cc(string_pos), not_past) * l.V(1)}
	local t = p:match(str)
	if not t then return end
	return t[1] - 1, t[#t] - 1
end

local function escaping_context(win, range, data)
	if not win.syntax then return {} end
	local rules = vis.lexers.lexers[win.syntax]._RULES
	local p
	for _, name in ipairs({vis.lexers.COMMENT, vis.lexers.STRING}) do
		if rules[name] then
			p = p and p + rules[name] / 0 or rules[name] / 0
		end
	end
	if not p then return {} end
	if not range then return {escape = p} end    -- means we are retrying with a "fake" pos
	local e1 = {match_at(data, p, range.start)}
	local e2 = range.finish - range.start > 1 and {match_at(data, p, range.finish - 1)} or e1
	if #e1 == 0 and #e2 == 0 then return {escape = p} end
	if #e1 == 0 and #e2 > 0 then return {escape = p, newpos = range.start} end
	if #e2 == 0 and #e1 > 0 then return {escape = p, newpos = range.finish - 1} end
	p = nil
	local escaped_range = {e1[1] + 1, e1[2]}
	local escaped_data = data:sub(e1[1] + 1, e1[2])
	for _, level in ipairs({vis.lexers.COMMENT, vis.lexers.STRING}) do
		if l.match(rules[level] / 0 * -1, escaped_data) then
			for _, name in ipairs(precedence[level]) do
				if rules[name] then
					p = p and p + rules[name] / 0 or rules[name] / 0
				end
			end
			return {escape = p, range = escaped_range}
		end
	end
end

local function get_range(key, win, pos, file_data)
	local d = get_pair(key, win)
	if not d then return end
	local offsets, correction
	repeat
		local sel_range = selection_range(win, pos)
		local c = escaping_context(win, sel_range, file_data)
		local range = c.range or {1, #file_data}
		if c.newpos then
			pos = c.newpos
		else
			pos = pos - (range[1] - 1)
		end
		correction = range[1] - 1
		local p = d[1] ~= d[2] and symmetric(d[1], d[2], c.escape, pos + 1) or asymmetric(d[1], pos + 1)
		local skip = c.escape and c.escape + 1 or 1
		local data = c.range and file_data:sub(unpack(c.range)) or file_data
		local pattern = l.P{l.Cmt(p, any_captures) + skip * l.Cmt(l.Cc(pos + 1), not_past) * l.V(1)}
		prev_match = 0
		offsets = {nth(pattern:match(data))}
		offsets[3] = nil
		if #offsets == 0 then
			pos = correction - 1
		end
	until #offsets > 0 or pos < 0
	for _, o in ipairs(offsets) do
		for i, v in ipairs(o) do
			o[i] = v - 1 + correction
		end
	end
	return unpack(offsets)
end

local function barf_linewise(content, start, finish)
	if vis.mode == vis.modes.VISUAL_LINE then
		local block = content:sub(start + 1, finish)
		local newlines = l.match(l.Ct((l.Cp() * l.P"\n" + 1)^0), block)
		if #newlines > 1 then
			return start + newlines[1] + 1, start + newlines[#newlines]
		end
	end
	return start, finish
end

local function get_delimiters(key, win, pos)
	local d = get_pair(key, win)
	if not d or type(d[1]) == "string" and type(d[2]) == "string" then return d end
	local content = win.file:content(0, win.file.size)
	local start, finish = get_range(key, win, pos, content)
	if start and finish then
		return {win.file:content(start[1], start[2] - start[1]), win.file:content(finish[1], finish[2] - finish[1]), d[3], d[4]}
	elseif #d > 2 then
		return {nil, nil, d[3], d[4]}
	end
end

local function outer(win, pos, content)
	local start, finish = get_range(M.key, win, pos, content)
	if not (start and finish) then return end
	return start[1], finish[2]
end

local function inner(win, pos, content)
	local start, finish = get_range(M.key, win, pos, content)
	if not (start and finish) then return end
	return barf_linewise(content, start[2], finish[1])
end

local function opening(win, pos, content)
	local start, _ = get_range(M.key, win, pos, content)
	if start then
		if pos == start[2] - 1 then
			start, _ = get_range(M.key, win, start[1] - 1, content)
		end
		if start then
			local exclusive = (vis.mode == vis.modes.OPERATOR_PENDING or vis.mode == vis.modes.VISUAL and pos < start[2] - 1) and 1 or 0
			return start[2] - 1 + exclusive
		end
	end
	return pos
end

local function closing(win, pos, content)
	local _, finish = get_range(M.key, win, pos, content)
	if finish then
		if pos == finish[1] then
			_, finish = get_range(M.key, win, finish[2], content)
		end
		if finish then
			local exclusive = (vis.mode == vis.modes.VISUAL and pos > finish[1]) and 1 or 0
			return finish[1] - exclusive
		end
	end
	return pos
end

local done_once

local function bail_early()
	if vis.count and vis.count > 1 then
		if done_once then
			done_once = nil
			return true
		else
			done_once = true
		end
	end
	return false
end

local function prep(func)
	return function(win, pos)
		if bail_early() then return pos end
		local content = win.file:content(0, win.file.size)
		return func(win, pos, content)
	end
end

local function new(execute, register, prefix, handler, help)
	local id = register(vis, prep(handler))
	if id < 0 then
		return false
	end
	if prefix then
		local binding = function(keys)
			if #keys < 1 then return -1 end
			if #keys == 1 then
				M.key = keys
				execute(vis, id)
			end
			return #keys
		end
		if execute ~= vis.textobject then
			vis:map(vis.modes.NORMAL, prefix, binding, help)
		end
		vis:map(vis.modes.VISUAL, prefix, binding, help)
		vis:map(vis.modes.OPERATOR_PENDING, prefix, binding, help)
		local builtin = execute == vis.motion and builtin_motions[prefix] or builtin_textobjects
		for key, _ in pairs(builtin) do
			if execute ~= vis.textobject then
				vis:unmap(vis.modes.NORMAL, prefix..key)
			end
			vis:unmap(vis.modes.VISUAL, prefix..key)
			vis:unmap(vis.modes.OPERATOR_PENDING, prefix..key)
		end
	end
	return id
end

vis.events.subscribe(vis.events.INIT, function()
	M.motion = {
		opening = new(vis.motion, vis.motion_register, M.prefix.opening, opening, "Move cursor to the beginning of a delimited block"),
		closing = new(vis.motion, vis.motion_register, M.prefix.closing, closing, "Move cursor to the end of a delimited block"),
	}
	M.textobject = {
		inner = new(vis.textobject, vis.textobject_register, M.prefix.inner, inner, "Delimited block (inner variant)"),
		outer = new(vis.textobject, vis.textobject_register, M.prefix.outer, outer, "Delimited block (outer variant)"),
	}

	local function cmp(_, _, c1, c2) return c1 == c2 end
	local function casecmp(_, _, c1, c2) return c1:lower() == c2:lower() end
	local function closing(s1, s2, cmpfunc) return l.Cmt(s1 * l.Cb("t") * l.C((1 - l.P(s2))^1) * s2, cmpfunc) end
	local tex_environment = {"\\begin{" * l.Cg(l.R("az", "AZ")^1, "t") * "}", closing("\\end{", "}", cmp), {"\\begin{\xef\xbf\xbd}", "\\end{\xef\xbf\xbd}"}, "environment name"}
	local tag_name = (l.S"_:" + l.R("az", "AZ")) * (l.R("az", "AZ", "09") + l.S"_:.-")^0
	local noslash = {--[[implicit:]] p=1, dt=1, dd=1, li=1, --[[void:]] area=1, base=1, br=1, col=1, embed=1, hr=1, img=1, input=1, link=1, meta=1, param=1, source=1, track=1, wbr=1}
	local function is_not(_, _, v) return v ~= 1 end
	local html_tag = {"<" * l.Cg(l.Cmt(tag_name / string.lower / noslash, is_not), "t") * (1 - l.S"><")^0 * (">" - l.B"/"), closing("</", ">", casecmp), {"<\xef\xbf\xbd>", "</\xef\xbf\xbd>"}, "tag name"}
	local xml_tag = {"<" * l.Cg(tag_name, "t") * (1 - l.S"><")^0 * (">" - l.B"/"), closing("</", ">", cmp), {"<\xef\xbf\xbd>", "</\xef\xbf\xbd>"}, "tag name"}
	local function any_pair(set, default) return {l.Cg(l.S(set), "s"), l.Cmt(l.Cb("s") * l.C(1), function(_, _, c1, c2) return builtin_textobjects[c1][2] == c2 end), builtin_textobjects[default]} end
	local any_bracket = any_pair("({[", "(")
	local presets = {
		{t = xml_tag},
		xml = {t = xml_tag},
		html = {t = html_tag},
		markdown = {t = html_tag},
		asp = {t = html_tag},
		jsp = {t = html_tag},
		php = {t = html_tag},
		rhtml = {t = html_tag},
		scheme = {b = any_bracket},
		lisp = {b = any_bracket},
		clojure = {b = any_bracket},
		latex = {t = tex_environment},
	}
	for syntax, bindings in pairs(presets) do
		if not M.map[syntax] then
			M.map[syntax] = bindings
		else
			for key, pattern in pairs(bindings) do
				if not M.map[syntax][key] then M.map[syntax][key] = pattern end
			end
		end
	end

end)

M = {
	map = {},
	get_pair = get_delimiters,
	get_range_inner = inner,
	get_range_outer = outer,
	prefix = {outer = "a", inner = "i", opening = "[", closing = "]"},
}

return M