This repository has been archived on 2022-03-04. You can view files and clone it, but cannot push or open issues or pull requests.
dotfiles/micro/.config/micro/init.lua

73 lines
2.2 KiB
Lua

local micro = import("micro")
local config = import("micro/config")
local buf = import("micro/buffer")
local util = import("micro/util")
local path = import("path")
local time = import("time")
--package.path = package.path .. ';/home/nico/.config/micro/?.lua';
--local fennel = require("fennel")
--table.insert(package.loaders or package.searchers, fennel.searcher)
--local config = require("config") -- will compile and load code in config.fnl
function gotofile(bp)
local selection = bp.Cursor.CurSelection -- cursor position
local line = bp.buf.Line(bp.buf, selection[1]["Y"]) -- get the current line
local path_start, path_end
-- TODO add other delimiters
-- search forward from cursor position for a delimiter
-- store it to find the end of the path
for v = selection[1]["X"], #line do
local r = util.RuneAt(line, v)
if r == ')' or r == "}" or r == "]" or r == " " then
path_end = v
break
end
end
-- do the same, looking backwards, for the start of the path
for v = selection[1]["X"],0, -1 do
local r = util.RuneAt(line,v)
if r == '(' or r == "{" or r == "[" or r == " " then
path_start = v+2
break
end
end
-- slice up the path and go to that file
if path_start ~= nil and path_end ~= nil then
-- get the path from the buffer
p = string.sub(line, path_start, path_end)
-- get the path relative to current file if it's relative
if not path.IsAbs(p) then
p = path.Join(path.Dir(bp.buf.Path), p)
end
-- open in a tab
-- TODO placement that's better than just splitting?
bp.NewTabCmd(bp, {p})
return false
end
end
function zettelInsert(bp)
local t = time.Now() -- current unix time
local ut = t.Unix(t)
local cursor = bp.Cursor.Loc
local cursorLoc = buf.Loc(cursor["X"], cursor["Y"])
local newFilePath = ut .. ".md"
local link = "[[" .. newFilePath .. "]]"
bp.Buf:Replace(cursorLoc, cursorLoc, link ) -- insert link at cursor position
local newBuffer = buf.NewBuffer("# " .. ut .. " - ", path.Join(path.Dir(bp.buf.Path), newFilePath))
bp.HSplitBuf(bp, newBuffer)
end
function init()
config.MakeCommand("gotofile", gotofile, config.NoComplete)
config.MakeCommand("zi", zettelInsert, config.NoComplete)
config.TryBindKey("MouseRight", "lua:initlua.gotofile", true)
end