Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitff0a5ac

Browse files
committed
refactor(runtime): port scripts.vim to Lua
1 parent3cd22a3 commitff0a5ac

File tree

2 files changed

+361
-2
lines changed

2 files changed

+361
-2
lines changed

‎runtime/filetype.lua‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ vim.g.did_load_ftdetect = 1
3333
ifvim.g.did_load_filetypesthen
3434
vim.api.nvim_create_autocmd({'BufRead','BufNewFile'}, {
3535
group='filetypedetect',
36-
command="if !did_filetype() && expand('<amatch>') !~ g:ft_ignore_pat | runtime! scripts.vim | endif",
36+
command="if !did_filetype() && expand('<amatch>') !~ g:ft_ignore_pat | runtime! scripts.lua | endif",
3737
})
3838

3939
vim.api.nvim_create_autocmd('StdinReadPost', {
4040
group='filetypedetect',
41-
command='if !did_filetype() | runtime! scripts.vim | endif',
41+
command='if !did_filetype() | runtime! scripts.lua | endif',
4242
})
4343
end
4444

‎runtime/lua/vim/scripts.lua‎

Lines changed: 359 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,359 @@
1+
-- Ported from scripts.vim
2+
3+
-- Only do the rest when the FileType autocommand has not been triggered yet.
4+
ifvim.fn.did_filetype()~=0then
5+
return
6+
end
7+
8+
-- Load the user defined scripts file first
9+
-- Only do this when the FileType autocommand has not been triggered yet
10+
ifvim.fn.exists('myscriptsfile')~=0andvim.fn.filereadable(vim.fn.expand('myscriptsfile'))~=0then
11+
vim.cmd('execute source myscriptsfile')
12+
ifvim.fn.did_filetype()~=0then
13+
return
14+
end
15+
end
16+
17+
localgetlines=vim.filetype.getlines
18+
localfindany=vim.filetype.findany
19+
localmatchregex=vim.filetype.matchregex
20+
21+
localdetect=require('vim.filetype.detect')
22+
23+
---@private
24+
localfunctionscripts_shebang(path,bufnr)
25+
localfirst_line=getlines(bufnr,1)
26+
-- Check for a line like "#!/usr/bin/env {options} bash". Turn it into
27+
-- "#!/usr/bin/bash" to make matching easier.
28+
-- Recognize only a few {options} that are commonly used.
29+
ifmatchregex(first_line,[[^#!\s*\S*\<env\s]])then
30+
first_line=first_line:gsub('%S+=%S+','')
31+
first_line=first_line
32+
:gsub('%-[iS]','',1)
33+
:gsub('%-%-ignore%-environment','',1)
34+
:gsub('%-%-split%-string','',1)
35+
first_line=vim.fn.substitute(first_line,[[\<env\s\+]],'','')
36+
end
37+
38+
-- Get the program name.
39+
-- Only accept spaces in PC style paths: "#!c:/program files/perl [args]".
40+
-- If the word env is used, use the first word after the space:
41+
-- "#!/usr/bin/env perl [path/args]"
42+
-- If there is no path use the first word: "#!perl [path/args]".
43+
-- Otherwise get the last word after a slash: "#!/usr/bin/perl [path/args]".
44+
localname
45+
iffirst_line:find('^#!%s*%a:[/\\]')then
46+
name=vim.fn.substitute(first_line,[[^#!.*[/\\]\(\i\+\).*]],'\\1','')
47+
elseifmatchregex(first_line,[[^#!.*\<env\>]])then
48+
name=vim.fn.substitute(first_line,[[^#!.*\<env\>\s\+\(\i\+\).*]],'\\1','')
49+
elseifmatchregex(first_line,[[^#!\s*[^/\\ ]*\>\([^/\\]\|$\)]])then
50+
name=vim.fn.substitute(first_line,[[^#!\s*\([^/\\ ]*\>\).*]],'\\1','')
51+
else
52+
name=vim.fn.substitute(first_line,[[^#!\s*\S*[/\\]\(\i\+\).*]],'\\1','')
53+
end
54+
55+
-- tcl scripts may have #!/bin/sh in the first line and "exec wish" in the
56+
-- third line. Suggested by Steven Atkinson.
57+
ifgetlines(bufnr,3):find('^exec wish')then
58+
name='wish'
59+
end
60+
61+
ifmatchregex(name,[[^\(bash\d*\|\|ksh\d*\|sh\)\>]])then
62+
-- Bourne-like shell scripts: bash bash2 ksh ksh93 sh
63+
returndetect.sh(path,bufnr,first_line)
64+
elseifmatchregex(name,[[^csh\>]])then
65+
returndetect.shell(path,bufnr,vim.g.filetype_cshor'csh')
66+
elseifmatchregex(name,[[^tcsh\>]])then
67+
returndetect.shell(path,bufnr,'tcsh')
68+
end
69+
70+
localpatterns= {
71+
['^zsh\\>']= {'zsh', {vim_regex=true } },
72+
['^\\(tclsh\\|wish\\|expectk\\|itclsh\\|itkwish\\)\\>']= {'tcl', {vim_regex=true } },
73+
['^expect\\>']= {'expect', {vim_regex=true } },
74+
['^gnuplot\\>']= {'gnuplot', {vim_regex=true } },
75+
['make\\>']= {'make', {vim_regex=true } },
76+
['^pike\\%(\\>\\|[0-9]\\)']= {'pike', {vim_regex=true } },
77+
lua='lua',
78+
perl='lua',
79+
php='php',
80+
python='python',
81+
['^groovy\\>']= {'groovy', {vim_regex=true } },
82+
raku='raku',
83+
ruby='ruby',
84+
['node\\(js\\)\\=\\>\\|js\\>']= {'javascript', {vim_regex=true } },
85+
['rhino\\>']= {'javascript', {vim_regex=true } },
86+
-- BC calculator
87+
['^bc\\>']= {'bc', {vim_regex=true } },
88+
['sed\\>']= {'sed', {vim_regex=true } },
89+
ocaml='ocaml',
90+
-- Awk scripts; also finds "gawk"
91+
['awk\\>']= {'awk', {vim_regex=true } },
92+
wml='wml',
93+
scheme='scheme',
94+
cfengine='cfengine',
95+
escript='erlang',
96+
haskell='haskell',
97+
clojure='clojure',
98+
['scala\\>']= {'scala', {vim_regex=true } },
99+
-- Free Pascal
100+
['instantfpc\\>']= {'pascal', {vim_regex=true } },
101+
['fennel\\>']= {'fennel', {vim_regex=true } },
102+
-- MikroTik RouterOS script
103+
['rsc\\>']= {'routeros', {vim_regex=true } },
104+
-- Fish shell
105+
['fish\\>']= {'fish', {vim_regex=true } },
106+
['gforth\\>']= {'forth', {vim_regex=true } },
107+
}
108+
109+
fork,vinpairs(patterns)do
110+
localft=type(v)=='table'andv[1]orv
111+
localopts=type(v)=='table'andv[2]or {}
112+
ifopts.vim_regexandmatchregex(name,k)orname:find(k)then
113+
returnft
114+
end
115+
end
116+
end
117+
118+
localfunctionscripts_no_shebang(path,bufnr)
119+
locallines=getlines(bufnr,1,5)
120+
121+
iflines[1]:find('^:$')then
122+
-- Bourne-like shell scripts: sh ksh bash bash2
123+
returndetect.sh(path,bufnr,lines[1])
124+
elseifmatchregex('\n'..table.concat(lines,'\n'),[[\n\s*emulate\s\+\%(-[LR]\s\+\)\=[ckz]\=sh\>]])then
125+
-- Z shell scripts
126+
return'zsh'
127+
end
128+
129+
localpatterns= {
130+
['^#compdef\\>']= {'zsh', {vim_regex=true } },
131+
['^#autoload\\>']= {'zsh', {vim_regex=true } },
132+
-- ELM Mail files
133+
['^From [a-zA-Z][a-zA-Z_0-9%.=%-]*(@[^ ]*)? .* 19%d%d$']='mail',
134+
['^From [a-zA-Z][a-zA-Z_0-9%.=%-]*(@[^ ]*)? .* 20%d%d$']='mail',
135+
['^From %- .* 19%d%d$']='mail',
136+
['^From %- .* 20%d%d$']='mail',
137+
-- Mason
138+
['^<[%%&].*>']='mason',
139+
-- Vim scripts (must have '" vim' as the first line to trigger this)
140+
['^" *[vV]im$[']='vim',
141+
-- libcxx and libstdc++ standard library headers like ["iostream["] do not have
142+
-- an extension, recognize the Emacs file mode.
143+
['%-%*%-.*[cC]%+%+.*%-%*%-']='cpp',
144+
['^\\*\\* LambdaMOO Database, Format Version\\%([1-3]\\>\\)\\@!\\d\\+\\*\\*$']= {
145+
'moo',
146+
{vim_regex=true },
147+
},
148+
-- Diff file:
149+
-- - "diff" in first line (context diff)
150+
-- - "Only in " in first line
151+
-- - "--- " in first line and "+++ " in second line (unified diff).
152+
-- - "*** " in first line and "--- " in second line (context diff).
153+
-- - "# It was generated by makepatch " in the second line (makepatch diff).
154+
-- - "Index: <filename>" in the first line (CVS file)
155+
-- - "=== ", line of "=", "---", "+++ " (SVK diff)
156+
-- - "=== ", "--- ", "+++ " (bzr diff, common case)
157+
-- - "=== (removed|added|renamed|modified)" (bzr diff, alternative)
158+
-- - "# HG changeset patch" in first line (Mercurial export format)
159+
['^\\(diff\\>\\|Only in\\|\\d\\+\\(,\\d\\+\\)\\=[cda]\\d\\+\\>\\|# It was generated by makepatch\\|Index:\\s\\+\\f\\+\\r\\=$\\|=====\\f\\+\\d\\+\\.\\d\\+ vs edited\\|==== //\\f\\+#\\d\\+\\|# HG changeset patch\\)']= {
160+
'diff',
161+
{vim_regex=true },
162+
},
163+
function()
164+
if
165+
lines[1]:find('^%-%-%-')andlines[2]:find('^%+%+%+')
166+
orlines[1]:find('^%* looking for')andlines[2]:find('^%* comparing to')
167+
orlines[1]:find('^%*%*%*')andlines[2]:find('^%-%-%-')
168+
orlines[1]:find('^===')and ((lines[2]:find('^'..string.rep('=',66))andlines[3]:find('^%-%-%')andlines[4]:find(
169+
'^%+%+%+'
170+
))or (lines[2]:find('^%-%-%-')andlines[3]:find('^%+%+%+')))
171+
orfindany(lines[1], {'^=== removed','^=== added','^=== renamed','^=== modified'})
172+
then
173+
return'diff'
174+
end
175+
end,
176+
-- PostScript Files (must have %!PS as the first line, like a2ps output)
177+
['^%%![\t]*PS']='postscr',
178+
function()
179+
-- M4 scripts: Guess there is a line that starts with "dnl".
180+
for_,lineinipairs(lines)do
181+
ifmatchregex(line,'^\\%sdnl\\>')then
182+
return'm4'
183+
end
184+
end
185+
ifvim.env.TERM=='amiga'andfindany(lines[1]:lower(), {'^;','^%.bra'})then
186+
-- AmigaDos scripts
187+
return'amiga'
188+
end
189+
end,
190+
-- SiCAD scripts (must have procn or procd as the first line to trigger this)
191+
['^ *proc[nd] *$']= {'sicad', {ignore_case=true } },
192+
-- Purify log files start with "**** Purify"
193+
['^%*%*%*%* Purify']='purifylog',
194+
-- XML
195+
['<%?%s*xml.*%?>']='xml',
196+
-- XHTML (e.g.: PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN")
197+
['\\<DTD\\s\\+XHTML\\s']='xhtml',
198+
-- HTML (e.g.: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN")
199+
-- Avoid "doctype html", used by slim.
200+
['\\c<!DOCTYPE\\s\\+html\\>']= {'html', {vim_regex=true } },
201+
-- PDF
202+
['^%%PDF%-']='pdf',
203+
-- XXD output
204+
['^%x%x%x%x%x%x%x: %x%x ?%x%x ?%x%x ?%x%x']='xxd',
205+
-- RCS/CVS log output
206+
['^RCS file:']= {'rcslog', {start_lnum=1,end_lnum=2 } },
207+
-- CVS commit
208+
['^CVS:']= {'cvs', {start_lnum=2 } },
209+
['^CVS:']= {'cvs', {start_lnum=-1 } },
210+
-- Prescribe
211+
['^!R!']='prescribe',
212+
-- Send-pr
213+
['^SEND%-PR:']='sendpr',
214+
-- SNNS files
215+
['^SNNS network definition file']='snnsnet',
216+
['^SNNS pattern definition file']='snnspat',
217+
['^SNNS result file']='snnsres',
218+
['&%%.-[Vv]irata']= {'virata', {start_lnum=1,end_lnum=5 } },
219+
['[0-9:.]* *execve%(']='strace',
220+
['^__libc_start_main']='strace',
221+
-- VSE JCL
222+
['^\\* $$ JOB\\>']= {'vsejcl', {vim_regex=true } },
223+
['^// *JOB\\>']= {'vsejcl', {vim_regex=true } },
224+
-- TAK and SINDA
225+
['K & K Associates']= {'takout', {start_lnum=4 } },
226+
['TAK 2000']= {'takout', {start_lnum=2 } },
227+
['S Y S T E M S I M P R O V E D']= {'syndaout', {start_lnum=3 } },
228+
['Run Date:']= {'takcmp', {start_lnum=6 } },
229+
['Node File 1']= {'sindacmp', {start_lnum=9 } },
230+
function()
231+
-- DNS zone files
232+
if
233+
findany(
234+
lines[1]..lines[2]..lines[3]..lines[4],
235+
{'^; <<>> DiG [0-9%.]+.* <<>>','%$ORIGIN','%$TTL','IN%s+SOA'}
236+
)
237+
then
238+
return'bindzone'
239+
end
240+
-- BAAN
241+
if-- 1 to 80 '*' characters
242+
lines[1]:find('|%*'..string.rep('%*?',79))andlines[2]:find('VRC')
243+
orlines[2]:find('|%*'..string.rep('%*?',79))andlines[3]:find('VRC')
244+
then
245+
return'baan'
246+
end
247+
end,
248+
-- Valgrind
249+
['^==%d+== valgrind']='valgrind',
250+
['^==%d+== Using valgrind']= {'valgrind', {start_lnum=3 } },
251+
-- Go docs
252+
['PACKAGE DOCUMENTATION$']='godoc',
253+
-- Renderman Interface Bytestream
254+
['^##RenderMan']='rib',
255+
-- Scheme scripts
256+
['exec%s%+%S*scheme']= {'scheme', {start_lnum=1,end_lnum=2 } },
257+
-- Git output
258+
['^\\(commit\\|tree\\|object\\)\\x\\{40,\\}\\>\\|^tag\\S\\+$']= {'git', {vim_regex=true } },
259+
function()
260+
-- Gprof (gnu profiler)
261+
iflines[1]=='Flat profile:'andlines[2]==''andlines[3]:find('^Each sample counts as .* seconds%.$')then
262+
return'gprof'
263+
end
264+
end,
265+
-- Erlang terms
266+
-- (See also: http://www.gnu.org/software/emacs/manual/html_node/emacs/Choosing-Modes.html#Choosing-Modes)
267+
['%-%*%-.*erlang.*%-%*%-']= {'erlang', {ignore_case=true } },
268+
-- YAML
269+
['^&YAML']='yaml',
270+
-- MikroTik RouterOS script
271+
['^#.*by RouterOS']='routeros',
272+
-- Sed scripts
273+
-- #ncomment is allowed but most likely a false positive so reuire a space
274+
-- before any trailing comment text
275+
['^#n%s']='sed',
276+
['^#n$']='sed',
277+
}
278+
279+
fork,vinpairs(patterns)do
280+
iftype(v)=='string'then
281+
-- Check the first line only
282+
iflines[1]:find(k)then
283+
returnv
284+
end
285+
elseiftype(v)=='function'then
286+
localft=v()
287+
ifftthen
288+
returnft
289+
end
290+
else
291+
localopts=type(v)=='table'andv[2]or {}
292+
ifopts.start_lnumthen
293+
assert(notopts.ignore_case,'ignore_case=true is ignored when start_lnum is also present, needs refactor')
294+
for_,lineinipairs(getlines(bufnr,opts.start_lnum,opts.end_lnum))do
295+
ifline:find(k)then
296+
returnv[1]
297+
end
298+
end
299+
else
300+
-- Check the first line only
301+
localline=opts.ignore_caseandlines[1]:lower()orlines[1]
302+
ifopts.vim_regexandmatchregex(line,k)orline:find(k)then
303+
returnv[1]
304+
end
305+
end
306+
end
307+
end
308+
309+
-- CVS diff
310+
for_,lineinipairs(getlines(bufnr,1,-1))do
311+
ifnotline:find('^%?')then
312+
ifmatchregex(line,[[^Index:\s\+\f\+$]])then
313+
return'diff'
314+
elseif
315+
-- Locale input files: Formal Definitions of Cultural Conventions
316+
-- Filename must be like en_US, fr_FR@euro or en_US.UTF-8
317+
findany(path, {'%a%a_%a%a$','%a%a_%a%a[%.@]','%a%a_%a%ai18n$','%a%a_%a%aPOSIX$','%a%a_%a%atranslit_'})
318+
then
319+
for_,line_inipairs(getlines(bufnr,1,100))do
320+
if
321+
findany(line_, {
322+
'^LC_IDENTIFICATION$',
323+
'^LC_CTYPE$',
324+
'^LC_COLLATE$',
325+
'^LC_MONETARY$',
326+
'^LC_NUMERIC$',
327+
'^LC_TIME$',
328+
'^LC_MESSAGES$',
329+
'^LC_PAPER$',
330+
'^LC_TELEPHONE$',
331+
'^LC_MEASUREMENT$',
332+
'^LC_NAME$',
333+
'^LC_ADDRESS$',
334+
})
335+
then
336+
return'fdcc'
337+
end
338+
end
339+
end
340+
end
341+
end
342+
end
343+
344+
localcpo_save=vim.o.cpoptions
345+
vim.cmd('set cpo&vim')
346+
347+
localpath=vim.fn.expand('<afile>')
348+
localbufnr=vim.api.nvim_get_current_buf()
349+
350+
localfirst_line=getlines(bufnr,1)
351+
localft=first_line:find('^#!')andscripts_shebang(path,bufnr)orscripts_no_shebang(path,bufnr)
352+
353+
-- luacheck: push ignore 122
354+
355+
vim.bo[bufnr].filetype=ft
356+
-- Restore 'cpopoptions'
357+
vim.o.cpoptions=cpo_save
358+
359+
-- luacheck: pop

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp