[vim/vim] filetype.vim: use iteration instead of recursion to strip backup suffix. (Issue #12553)

53 views
Skip to first unread message

rajeevvp

unread,
Jun 17, 2023, 4:09:37 AM6/17/23
to vim/vim, Subscribed

Steps to reproduce

Create a file with more than 10 backup suffix (~) chars. in the name.

$ echo hello > '~~~~~~~~~~~'

Open the file in vim.

$ vim ~~~~~~~~~~~
Error detected while processing BufRead Autocommands for "*~"..BufRead Autocommands for "*~"..BufRead Autocommands for "*~"..BufRead Autocommands for "*~"..BufRead Autocommands for "*~"..BufRead Autocommands for "*~"..BufRead Autocommands for "*~"..BufRead Autocommands for "*~"..BufRead Autocommands for "*~"..BufRead Autocommands for "*~":
E218: Autocommand nesting too deep
No matching autocommands: filetypedetect BufRead ~
$

I've come up with this patch (sorry if it's ugly; this is my first vimscript!):

--- filetype.vim.orig	2023-06-17 00:53:06.712264000 +0000
+++ filetype.vim	2023-06-17 07:49:29.335998000 +0000
@@ -21,8 +21,14 @@
 	\ exe "doau filetypedetect BufRead " . fnameescape(expand("<afile>:r"))
 au BufNewFile,BufRead *~
 	\ let s:name = expand("<afile>") |
-	\ let s:short = substitute(s:name, '\~$', '', '') |
-	\ if s:name != s:short && s:short != "" |
+	\ while 1 |
+	\   let s:short = substitute(s:name, '\~$', '', '') |
+	\   if s:name == s:short || s:short == "" |
+	\      break |
+	\   endif |
+	\   let s:name = s:short |
+	\ endwhile |
+	\ if s:short != "" |
 	\   exe "doau filetypedetect BufRead " . fnameescape(s:short) |
 	\ endif |
 	\ unlet! s:name s:short

Expected behaviour

vim should open the file without the error message.

Version of Vim

9.0.1637

Environment

Operating system: FreeBSD-13.2
Terminal: xterm-379
$TERM: xterm
Shell: bash

Logs and stack traces

No response


Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/12553@github.com>

Christian Brabandt

unread,
Jun 20, 2023, 7:31:47 AM6/20/23
to vim/vim, Subscribed

let s:short = substitute(s:name, '~$', '', '') |

why not:

let s:short = substitute(s:name, '\~\+$', '', '') |

so remove as many ~ at the end of the name as possible?


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/12553/1598603851@github.com>

rajeevvp

unread,
Jun 20, 2023, 6:32:58 PM6/20/23
to vim/vim, Subscribed

why not

let s:short = substitute(s:name, '\~\+$', '', '') |

so remove as many ~ at the end of the name as possible?

Duh. Obvious when it's pointed out, innit? :) That works just fine (and I'll preemptively close this PR).


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/12553/1599660896@github.com>

rajeevvp

unread,
Jun 20, 2023, 6:32:59 PM6/20/23
to vim/vim, Subscribed

Closed #12553 as completed.


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issue/12553/issue_event/9585223595@github.com>

Reply all
Reply to author
Forward
0 new messages