blob: 9687d55e9f2fc07b894f40c009568d15ab281ae7 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001" Vim filetype plugin file.
Bram Moolenaar71b6d332022-09-10 13:13:14 +01002" Language: Lua
Bram Moolenaar519cc552021-11-16 19:18:26 +00003" Maintainer: Doug Kearns <dougkearns@gmail.com>
4" Previous Maintainer: Max Ischenko <mfi@ukr.net>
Bram Moolenaar71b6d332022-09-10 13:13:14 +01005" Contributor: Dorai Sitaram <ds26@gte.com>
Bram Moolenaar3c053a12022-10-16 13:11:12 +01006" C.D. MacEachern <craig.daniel.maceachern@gmail.com>
Bram Moolenaar71badf92023-04-22 22:40:14 +01007" Tyler Miller <tmillr@proton.me>
brianhuster00a00f52025-02-25 20:22:18 +01008" Phm Bình An <phambinhanctb2004@gmail.com>
9" Last Change: 2025 Feb 25
Bram Moolenaar071d4272004-06-13 20:20:40 +000010
Bram Moolenaar071d4272004-06-13 20:20:40 +000011if exists("b:did_ftplugin")
12 finish
13endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014let b:did_ftplugin = 1
15
brianhuster00a00f52025-02-25 20:22:18 +010016" keep in sync with syntax/lua.vim
17if !exists("lua_version")
18 " Default is lua 5.3
19 let lua_version = 5
20 let lua_subversion = 3
21elseif !exists("lua_subversion")
22 " lua_version exists, but lua_subversion doesn't. In this case set it to 0
23 let lua_subversion = 0
24endif
25
Bram Moolenaarf1568ec2011-12-14 21:17:39 +010026let s:cpo_save = &cpo
27set cpo&vim
28
Bram Moolenaar71badf92023-04-22 22:40:14 +010029setlocal comments=:---,:--
Bram Moolenaar71b6d332022-09-10 13:13:14 +010030setlocal commentstring=--\ %s
Bram Moolenaar519cc552021-11-16 19:18:26 +000031setlocal formatoptions-=t formatoptions+=croql
Bram Moolenaar071d4272004-06-13 20:20:40 +000032
Bram Moolenaar71b6d332022-09-10 13:13:14 +010033let &l:define = '\<function\|\<local\%(\s\+function\)\='
34
brianhuster00a00f52025-02-25 20:22:18 +010035let &l:include = '\v<((do|load)file|require)[^''"]*[''"]\zs[^''"]+'
36setlocal includeexpr=LuaInclude(v:fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000037setlocal suffixesadd=.lua
38
brianhuster00a00f52025-02-25 20:22:18 +010039let b:undo_ftplugin = "setlocal cms< com< def< fo< inc< inex< sua<"
Bram Moolenaar071d4272004-06-13 20:20:40 +000040
Bram Moolenaar519cc552021-11-16 19:18:26 +000041if exists("loaded_matchit") && !exists("b:match_words")
Bram Moolenaar071d4272004-06-13 20:20:40 +000042 let b:match_ignorecase = 0
43 let b:match_words =
Bram Moolenaar71b6d332022-09-10 13:13:14 +010044 \ '\<\%(do\|function\|if\)\>:' ..
45 \ '\<\%(return\|else\|elseif\)\>:' ..
46 \ '\<end\>,' ..
47 \ '\<repeat\>:\<until\>,' ..
48 \ '\%(--\)\=\[\(=*\)\[:]\1]'
49 let b:undo_ftplugin ..= " | unlet! b:match_words b:match_ignorecase"
Bram Moolenaar519cc552021-11-16 19:18:26 +000050endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000051
Bram Moolenaar519cc552021-11-16 19:18:26 +000052if (has("gui_win32") || has("gui_gtk")) && !exists("b:browsefilter")
Doug Kearns93197fd2024-01-14 20:59:02 +010053 let b:browsefilter = "Lua Source Files (*.lua)\t*.lua\n"
54 if has("win32")
55 let b:browsefilter ..= "All Files (*.*)\t*\n"
56 else
57 let b:browsefilter ..= "All Files (*)\t*\n"
58 endif
Bram Moolenaar71b6d332022-09-10 13:13:14 +010059 let b:undo_ftplugin ..= " | unlet! b:browsefilter"
Bram Moolenaar519cc552021-11-16 19:18:26 +000060endif
Bram Moolenaarf1568ec2011-12-14 21:17:39 +010061
Ennofdfcce52024-12-03 22:23:48 +010062if has("folding") && get(g:, "lua_folding", 0)
63 setlocal foldmethod=expr
64 setlocal foldexpr=LuaFold(v:lnum)
65 let b:lua_lasttick = -1
66 let b:undo_ftplugin ..= "|setl foldexpr< foldmethod< | unlet! b:lua_lasttick b:lua_foldlists"
67endif
68
69
70" The rest of the file needs to be :sourced only once per Vim session
71if exists('s:loaded_lua') || &cp
72 let &cpo = s:cpo_save
73 unlet s:cpo_save
74 finish
75endif
76let s:loaded_lua = 1
77
78let s:patterns = [
79 \ ['do', 'end'],
80 \ ['if\s+.+\s+then', 'end'],
81 \ ['repeat', 'until\s+.+'],
82 \ ['for\s+.+\s+do', 'end'],
83 \ ['while\s+.+\s+do', 'end'],
84 \ ['function.+', 'end'],
85 \ ['return\s+function.+', 'end'],
86 \ ['local\s+function\s+.+', 'end'],
87 \ ]
88
brianhuster00a00f52025-02-25 20:22:18 +010089function LuaInclude(fname) abort
90 let lua_ver = str2float(printf("%d.%02d", g:lua_version, g:lua_subversion))
91 let fname = tr(a:fname, '.', '/')
92 let paths = lua_ver >= 5.03 ? [ fname.'.lua', fname.'/init.lua' ] : [ fname.'.lua' ]
93 for path in paths
94 if filereadable(path)
95 return path
96 endif
97 endfor
98 return fname
99endfunction
100
101function LuaFold(lnum) abort
Ennofdfcce52024-12-03 22:23:48 +0100102 if b:lua_lasttick == b:changedtick
103 return b:lua_foldlists[a:lnum-1]
104 endif
105 let b:lua_lasttick = b:changedtick
106
107 let b:lua_foldlists = []
108 let foldlist = []
109 let buf = getline(1, '$')
110 for line in buf
111 for t in s:patterns
112 let tagopen = '\v^\s*'..t[0]..'\s*$'
113 let tagclose = '\v^\s*'..t[1]..'\s*$'
114 if line =~# tagopen
115 call add(foldlist, t)
116 break
117 elseif line =~# tagclose
118 if len(foldlist) > 0 && line =~# foldlist[-1][1]
119 call remove(foldlist, -1)
120 else
121 let foldlist = []
122 endif
123 break
124 endif
125 endfor
126 call add(b:lua_foldlists, len(foldlist))
127 endfor
128
129 return lua_foldlists[a:lnum-1]
130endfunction
131
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100132let &cpo = s:cpo_save
133unlet s:cpo_save
Bram Moolenaar71b6d332022-09-10 13:13:14 +0100134
135" vim: nowrap sw=2 sts=2 ts=8 noet: