-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
how to read file in to existing buffer #3474
Comments
You can use There is a caveat though: |
Hi, I have a function for inserting a file: function editor.InsertFile(Current,item)
if io.exists(item) then
local filename,err = ioutil.ReadFile(item)
if not err then
Current.Buf:Insert(editor.GetTextLoc(Current), filename)
end
end
end This function has dependencies: -- Import go libraries
local ioutil = import("io/ioutil")
local osutil = import("os")
-- Write io.exists function - this one is for Windows
function io.exists(filename)
-- Check it's a string
if type(filename)~="string" then return false end
-- Check for unacceptable characters
if filename:match("[\n<|>$]") then return false end
-- TODO: Check for valid filename
-- Convert to unix directory separators - for Go
filename = filename:gsub([[/]],[[\]])
-- Use osutils
local fileinfo, err = osutil.Stat(filename)
if osutil.IsNotExist(err) then
return false
else
return true
end
end
-- Set up editor table and functions
editor = {}
function editor.GetTextLoc(Current)
-- Returns Loc-tuple w/ current marked text or whole line (begin, end)
-- https://github.com/NicolaiSoeborg/manipulator-plugin/blob/master/manipulator.lua
local a, b = nil, nil
if editor.HasSelection(Current) then
if Current.Cursor.CurSelection[1]:GreaterThan(-Current.Cursor.CurSelection[2]) then
a, b = Current.Cursor.CurSelection[2], Current.Cursor.CurSelection[1]
else
a, b = Current.Cursor.CurSelection[1], Current.Cursor.CurSelection[2]
end
else
local eol = string.len(Current.Buf:Line(Current.Cursor.Loc.Y))
a, b = Current.Cursor.Loc, buffer.Loc(eol, Current.Cursor.Y)
end
return buffer.Loc(a.X, a.Y), buffer.Loc(b.X, b.Y)
end
function editor.HasSelection(Current)
return Current.Cursor:HasSelection()
end
Sorry this is a bit convoluted, I keep all my Lua code in one file, crediting plugin developers for their contributions. Hope this helps. Kind Regards Gavin Holt |
I have some existing buffer, I would like to read/merge/insert text from another file, aka:
r filename.txt
in vi. How to do it in micro? ThanksThe text was updated successfully, but these errors were encountered: