martanne / vis

A vi-like editor based on Plan 9's structural regular expressions

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[question] newline at end of file

dkwo opened this issue · comments

commented

what is the recommended way to place a newline at the end of file in vis?
currently i get \ No newline at end of file all the time in git

Thanks.

We were just discussing this on the #vis-editor channel and the conclusion was that the safest way would be adding to your ~/.config/vis/visrc.lua something like this (see vis API documentation for more information):

-- adds a newline before each write if there is no newline
vis.events.subscribe(vis.events.FILE_SAVE_PRE, function (file, path)
    if
            file.size==0
            or file.size==1 and file:content(0,1)~="\n"
            or file.size>1 and file:content(file.size-2,file.size)~="\n\n"
    then
            file:delete(file.size,file.size)
            file:insert(file.size,"\n")
    end
    return true
end)

Alternative condition would be

if file:content(file.size-1,file.size)~="\n" then

Something like this would help?

commented

Thank you. That produces visrc.lua:2: attempt to index a nil value (field 'events').
I must note that my visrc.lua was previously empty. Is there anything I'm missing, or even a sensible visrc that I could look at?

commented

Thanks a lot!

commented

With

$ cat .config/vis/visrc.lua 
-- load standard vis module, providing parts of the Lua API
require('vis')

vis.events.subscribe(vis.events.INIT, function()
-- Your global configuration options
end)

vis.events.subscribe(vis.events.WIN_OPEN, function(win)
	-- luacheck: no unused args
	-- Your per window configuration options e.g.
	-- vis:command('set number')
end)

-- adds a newline before each write if there is no newline
vis.events.subscribe(vis.events.FILE_SAVE_PRE, function (file, path)
	if
		file.size==0
		or file.size==1 and file:content(0,1)~="\n"
		or file.size>1 and file:content(file.size-2,file.size)~="\n\n"
	then
		file:delete(file.size,file.size)
		file:insert(file.size,"\n")
	end
	return true
end)

there's an undesired effect that, if a newline is already present, then another line is added at the end of file. Can this be avoided?

commented

Thanks, that seems to work as desired.