~martanne/devel

vis: complete-filename: complete tilda as $HOME as well v1 APPLIED

=?UTF-8?q?Mat=C4=9Bj=20Cepl?=: 3
 complete-filename: complete tilda as $HOME as well
 complete-filename: complete tilda as $HOME as well
 complete-filename: speed-up the tilda handling

 3 files changed, 12 insertions(+), 2 deletions(-)
Matěj Cepl <mcepl@cepl.eu> wrote:
Next
Matěj Cepl <mcepl@cepl.eu> wrote:
Next
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~martanne/devel/patches/56961/mbox | git am -3
Learn more about email & git

[PATCH vis] complete-filename: complete tilda as $HOME as well Export this patch

References: https://github.com/martanne/vis/pull/1148#issuecomment-1792649763
---
 lua/plugins/complete-filename.lua | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/lua/plugins/complete-filename.lua b/lua/plugins/complete-filename.lua
index 43cf14b1..588ff877 100644
--- a/lua/plugins/complete-filename.lua
+++ b/lua/plugins/complete-filename.lua
@@ -22,6 +22,9 @@ local complete_filename = function(expand)
		range.finish = pos
	end

	-- Expand tilda for the home directory
	prefix = prefix:gsub("^~", os.getenv("HOME"), 1)

	local cmdfmt = "vis-complete --file '%s'"
	if expand then cmdfmt = "vis-open -- '%s'*" end
	local status, out, err = vis:pipe(cmdfmt:format(prefix:gsub("'", "'\\''")))
-- 
2.47.1
Matěj Cepl <mcepl@cepl.eu> wrote:
Matěj Cepl <mcepl@cepl.eu> wrote:

[PATCH vis v2] complete-filename: complete tilda as $HOME as well Export this patch

References: https://github.com/martanne/vis/pull/1148#issuecomment-1792649763
---
I am not certain whether this is truly necessary (that
string.find condition is implicitly included in string.gsub
itself), but if it makes you happy.
 lua/plugins/complete-filename.lua | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/lua/plugins/complete-filename.lua b/lua/plugins/complete-filename.lua
index 43cf14b1..02b9f3a9 100644
--- a/lua/plugins/complete-filename.lua
+++ b/lua/plugins/complete-filename.lua
@@ -22,6 +22,12 @@ local complete_filename = function(expand)
		range.finish = pos
	end

	-- Expand tilda for the home directory
	if prefix:find('^~') then
		local home = assert(os.getenv("HOME"), "$HOME variable not set!")
		prefix = prefix:gsub("^~", home, 1)
	end

	local cmdfmt = "vis-complete --file '%s'"
	if expand then cmdfmt = "vis-open -- '%s'*" end
	local status, out, err = vis:pipe(cmdfmt:format(prefix:gsub("'", "'\\''")))
-- 
2.48.0
Matěj Cepl <mcepl@cepl.eu> wrote:

[PATCH vis] complete-filename: speed-up the tilda handling Export this patch

When profiling (on 100,000 calls) with this test:

```lua
start_t = os.clock()
for i = 1, 100000 do
    _ = home .. prefix
end
end_t = os.clock()
print(end_t - start_t)
```

I get on my computer this (warm):

    os.getenv("HOME") 0.005506
    str:gsub("^~", home) 0.009333
    str:find("^~") 0.006041
    str:sub(2) 0.005455
    a .. b 0.003603

Difference between gsub and sub + .. seems negligible, but just
to make it correct, here we are.
---
 lua/plugins/complete-filename.lua | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/lua/plugins/complete-filename.lua b/lua/plugins/complete-filename.lua
index 02b9f3a9..604361c2 100644
--- a/lua/plugins/complete-filename.lua
+++ b/lua/plugins/complete-filename.lua
@@ -23,9 +23,10 @@ local complete_filename = function(expand)
	end

	-- Expand tilda for the home directory
	if prefix:find('^~') then
	_, j = prefix:find('^~')
	if j ~= nil then
		local home = assert(os.getenv("HOME"), "$HOME variable not set!")
		prefix = prefix:gsub("^~", home, 1)
		prefix = home .. prefix:sub(j + 1)
	end

	local cmdfmt = "vis-complete --file '%s'"
-- 
2.48.0