diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/ext/mime_types.conf | 293 | ||||
-rw-r--r-- | core/ext/mime_types.lua | 359 |
2 files changed, 349 insertions, 303 deletions
diff --git a/core/ext/mime_types.conf b/core/ext/mime_types.conf new file mode 100644 index 00000000..c46efdbe --- /dev/null +++ b/core/ext/mime_types.conf @@ -0,0 +1,293 @@ +% Copyright 2007-2009 Mitchell mitchell<att>caladbolg.net. See LICENSE. +% +% Configuration file for mime-types. +% Each line defines a mime-type (by extension, shebang word, or Lua pattern) and +% its associated lexer. A shebang word is one that is contained in the file's +% shebang line (#!). Only the last space, the one separating the mime-type from +% its lexer, in a definition is significant. +% +% Syntax: +% % Comment (must start a line) +% ext lexer_name +% #shbang_word lexer_name +% /lua_pattern lexer_name + + +% Actionscript +as actionscript + +% ADA +ada ada +adb ada +ads ada + +% ANTLR +g antlr + +% APDL +ans apdl +inp apdl +mac apdl + +% Applescript +applescript applescript + +% ASP +asa asp +asp asp + +% AWK +awk awk +#awk awk + +% Batch +bat batch +cmd batch + +% Boo +boo boo + +% C/C++ +c cpp +cc cpp +C cpp +cpp cpp +cxx cpp +c++ cpp +h cpp +hh cpp +hpp cpp + +% C# +cs csharp + +% CSS +css css + +% D +d d + +% diff +diff diff +patch diff + +% Eiffel +e eiffel +eif eiffel + +% Erlang +erl erlang + +% Forth +f forth +frt forth +fs forth + +% Fortran +for fortran +fort fortran +f77 fortran +f90 fortran +f95 fortran + +% Gap +g gap +gd gap +gi gap +gap gap + +% Gettext +po gettext +pot gettext + +% GNUPlot +dem gnuplot +plt gnuplot + +% Groovy +groovy groovy +grv groovy + +% Haskell +hs haskell + +% HTML +htm html +html html +shtm html +shtml html + +% IDL +idl idl + +% ini +ini ini +reg ini + +% Io +io io + +% Java +bsh java +java java + +% Javascript +js javascript + +% Latex +bbl latex +dtx latex +ins latex +ltx latex +tex latex +sty latex + +% Lisp +el lisp +lisp lisp +lsp lisp + +% Lua +lua lua +#lua lua + +% Makefile +GNUmakefile makefile +iface makefile +mak makefile +makefile makefile +Makefile makefile + +% Maxima +maxima maxima + +% Objective C +m objective_c +objc objective_c + +% OCAML +ml ocaml +mli ocaml +mll ocaml +mly ocaml + +% Pascal +dpk pascal +dpr pascal +p pascal +pas pascal + +% Perl +al perl +perl perl +pl perl +pm perl +#perl perl + +% PHP +inc php +php php +php3 php +php4 php +phtml php +#php php + +% Pike +pike pike +pmod pike + +% Postscript +eps postscript +ps postscript + +% Properties +props props +properties props + +% Python +sc python +py python +pyw python +#python python + +% R +R r +Rout r +Rhistory r +Rt r +Rout.save r +Rout.fail r + +% Rebol +r rebol + +% Rexx +orx rexx +rex rexx + +% RHTML +rhtml rhtml + +% Ruby +rb ruby +rbw ruby +#ruby ruby + +% Ragel +rl ragel + +% Scheme +scm scheme + +% Shell +bash shell +csh shell +sh shell +#sh shell + +% Smalltalk +changes smalltalk +st smalltalk +sources smalltalk + +% SQL +sql sql + +% TCL +tcl tcl +tk tcl + +% Vala +vala vala + +% Verilog +v verilog +ver verlilog + +% VHDL +vh vhdl +vhd vhdl +vhdl vhdl + +% Visual Basic +asa visualbasic +bas visualbasic +cls visualbasic +ctl visualbasic +dob visualbasic +dsm visualbasic +dsr visualbasic +frm visualbasic +pag visualbasic +pag visualbasic +vb visualbasic +vba visualbasic +vbs visualbasic + +% XML +xhtml xml +xml xml +xsd xml +xsl xml +xslt xml +/^%s*<%?xml%s xml diff --git a/core/ext/mime_types.lua b/core/ext/mime_types.lua index e4960e41..2948ade5 100644 --- a/core/ext/mime_types.lua +++ b/core/ext/mime_types.lua @@ -7,337 +7,90 @@ local locale = _G.locale module('textadept.mime_types', package.seeall) --- --- [Local table] Language names with their associated lexers. --- @class table --- @name languages -local languages = { - as = 'actionscript', - ada = 'ada', - antlr = 'antlr', - apdl = 'apdl', - applescript = 'applescript', - asp = 'asp', - awk = 'awk', - batch = 'batch', - boo = 'boo', - cpp = 'cpp', - csharp = 'csharp', - css = 'css', - d = 'd', - diff = 'diff', - django = 'django', - eiffel = 'eiffel', - erlang = 'erlang', - forth = 'forth', - fortran = 'fortran', - gap = 'gap', - gettext = 'gettext', - gnuplot = 'gnuplot', - groovy = 'groovy', - haskell = 'haskell', - html = 'html', - idl = 'idl', - ini = 'ini', - io = 'io', - java = 'java', - js = 'javascript', - latex = 'latex', - lisp = 'lisp', - lua = 'lua', - makefile = 'makefile', - maxima = 'maxima', - sql = 'sql', - objc = 'objective_c', - ocaml = 'ocaml', - pascal = 'pascal', - perl = 'perl', - php = 'php', - pike = 'pike', - postscript = 'postscript', - props = 'props', - python = 'python', - r = 'r', - ragel = 'ragel', - rebol = 'rebol', - rexx = 'rexx', - rhtml = 'rhtml', - ruby = 'ruby', - scheme = 'scheme', - shell = 'shellscript', - smalltalk = 'smalltalk', - tcl = 'tcl', - vala = 'vala', - verilog = 'verilog', - vhdl = 'vhdl', - vb = 'visualbasic', - xml = 'xml' -} - -local l = languages ---- --- [Local table] File extensions with their associated languages. +-- [Local table] File extensions with their associated lexers. -- @class table -- @name extensions -local extensions = { - -- Actionscript - as = l.as, - -- Ada - ada = l.ada, adb = l.ada, ads = l.ada, - -- ANTLR - g = l.antlr, - -- APDL - ans = l.apdl, - inp = l.apdl, - mac = l.apdl, - -- Applescript - applescript = l.applescript, - -- ASP - asa = l.asp, asp = l.asp, - -- AWK - awk = l.awk, - -- Batch - bat = l.batch, - cmd = l.batch, - -- Boo - boo = l.boo, - -- C/C++ - c = l.cpp, cc = l.cpp, C = l.cpp, cpp = l.cpp, cxx = l.cpp, ['c++'] = l.cpp, - h = l.cpp, hh = l.cpp, hpp = l.cpp, - -- C# - cs = l.csharp, - -- CSS - css = l.css, - -- D - d = l.d, - -- Diff - diff = l.diff, - patch = l.diff, - -- Eiffel - e = l.eiffel, eif = l.eiffel, - -- Erlang - erl = l.erlang, - -- Forth - f = l.forth, frt = l.forth, fs = l.forth, - -- Fortran - ['for'] = l.fortran, fort = l.fortran, f77 = l.fortran, f90 = l.fortran, - f95 = l.fortran, - -- Gap - g = l.gap, gd = l.gap, gi = l.gap, gap = l.gap, - -- Gettext - po = l.gettext, pot = l.gettext, - -- GNUPlot - dem = l.gnuplot, - plt = l.gnuplot, - -- Goovy - groovy = l.groovy, grv = l.groovy, - -- Haskell - hs = l.haskell, - -- HTML - htm = l.html, html = l.html, - shtm = l.html, shtml = l.html, - -- IDL - idl = l.idl, - -- ini - ini = l.ini, - reg = l.ini, - -- Io - io = l.io, - -- Java - bsh = l.java, - java = l.java, - -- Javascript - js = l.js, - -- Latex - bbl = l.latex, - dtx = l.latex, - ins = l.latex, - ltx = l.latex, - tex = l.latex, - sty = l.latex, - -- Lisp - el = l.lisp, - lisp = l.lisp, - lsp = l.lisp, - -- Lua - lua = l.lua, - -- Makefile - GNUmakefile = l.makefile, - iface = l.makefile, - mak = l.makefile, makefile = l.makefile, Makefile = l.makefile, - -- Maxima - maxima = l.maxima, - -- Objective C - m = l.objc, - objc = l.objc, - -- OCAML, - ml = l.ocaml, mli = l.ocaml, mll = l.ocaml, mly = l.ocaml, - -- Pascal - dpk = l.pascal, dpr = l.pascal, - p = l.pascal, pas = l.pascal, - -- Perl - al = l.perl, - perl = l.perl, pl = l.perl, pm = l.perl, - -- PHP - inc = l.php, - php = l.php, php3 = l.php, php4 = l.php, phtml = l.php, - -- Pike - pike = l.pike, pmod = l.pike, - -- Postscript - eps = l.postscript, - ps = l.postscript, - -- Properties - props = l.props, properties = l.props, - -- Python - sc = l.python, - py = l.python, pyw = l.python, - -- R - R = l.r, Rout = l.r, Rhistory = l.r, Rt = l.r, ['Rout.save'] = l.r, - ['Rout.fail'] = l.r, - -- Rebol - r = l.rebol, - -- Rexx - orx = l.rexx, - rex = l.rexx, - -- RHTML - rhtml = l.rhtml, - -- Ruby - rb = l.ruby, rbw = l.ruby, - -- Ragel - rl = l.ragel, - -- Scheme - scm = l.scheme, - -- Shell - bash = l.shell, - csh = l.shell, - sh = l.shell, - -- Smalltalk - changes = l.smalltalk, - st = l.smalltalk, sources = l.smalltalk, - -- SQL - sql = l.sql, - -- TCL - tcl = l.tcl, tk = l.tk, - -- Vala - vala = l.vala, - -- Verilog - v = l.verilog, ver = l.verilog, - -- VHDL - vh = l.vhdl, vhd = l.vhdl, vhdl = l.vhdl, - -- Visual Basic - asa = l.vb, - bas = l.vb, - cls = l.vb, ctl = l.vb, - dob = l.vb, dsm = l.vb, dsr = l.vb, - frm = l.vb, - pag = l.vb, - vb = l.vb, vba = l.vb, vbs = l.vb, - -- XML - xhtml = l.xml, xml = l.xml, xsd = l.xml, xsl = l.xml, xslt = l.xml -} +local extensions = {} --- --- [Local table] Shebang words and their associated languages. +-- [Local table] Shebang words and their associated lexers. -- @class table -- @name shebangs -local shebangs = { - awk = l.awk, - lua = l.lua, - perl = l.perl, - php = l.php, - python = l.python, - ruby = l.ruby, - sh = l.shell, -} +local shebangs = {} --- --- [Local table] First-line patterns and their associated languages. +-- [Local table] First-line patterns and their associated lexers. -- @class table -- @name patterns -local patterns = { - ['^%s*<%?xml%s'] = l.xml -} +local patterns = {} ---- --- [Local function] Sets the buffer's lexer language based on a filename. --- @param filename The filename used to set the lexer language. --- @return boolean indicating whether or not a lexer language was set. -local function set_lexer_from_filename(filename) - local lexer - if filename then - local ext = filename:match('[^/\\]+$'):match('[^.]+$') - lexer = extensions[ext] - end - buffer:set_lexer_language(lexer or 'container') - return lexer -end - ---- --- [Local function] Sets the buffer's lexer language based on a shebang line. -local function set_lexer_from_sh_bang() - local line = buffer:get_line(0) - if line:find('^#!') then - line = line:gsub('[\\/]', ' ') - for word in line:gmatch('%S+') do - if shebangs[word] then - buffer:set_lexer_language(shebangs[word]) - return true +-- Load mime-types from mime_types.conf +local f = io.open(_HOME..'/core/ext/mime_types.conf', 'rb') +if f then + for line in f:lines() do + if not line:find('^%s*%%') then + if line:find('^%s*[^#/]') then -- extension definition + local ext, lexer_name = line:match('^%s*(.+)%s+(%S+)$') + if ext and lexer_name then extensions[ext] = lexer_name end + else -- shebang or pattern + local ch, text, lexer_name = line:match('^%s*([#/])(.+)%s+(%S+)$') + if ch and text and lexer_name then + (ch == '#' and shebangs or patterns)[text] = lexer_name + end end end end end --- --- [Local function] Sets the buffer's lexer language based on a pattern that --- matches its first line. -local function set_lexer_from_pattern() - local line = buffer:get_line(0) - for patt, lexer in pairs(patterns) do - if line:find(patt) then - buffer:set_lexer_language(lexer) - return true - end +-- [Local function] Sets the buffer's lexer based on filename, shebang words, or +-- first line pattern. +local function set_lexer() + local lexer + if buffer.filename then + lexer = extensions[buffer.filename:match('[^/\\.]+$')] end -end - ---- --- [Local function] Loads a language module based on a filename (if it hasn't --- been loaded already). --- @param filename The filename used to load a language module from. -local function load_language_module_from_filename(filename) - if not filename then return end - local ext = filename:match('[^/\\]+$'):match('[^.]+$') - local lang = extensions[ext] - if lang then - local ret, err = pcall(require, lang) - if ret then - _m[lang].set_buffer_properties() - elseif not ret and not err:find("^module '"..lang.."' not found:") then - textadept.events.error(err) + if not lexer then + local line = buffer:get_line(0) + if line:find('^#!') then + for word in line:gsub('[/\\]', ' '):gmatch('%S+') do + if shebangs[word] then + lexer = shebangs[word] + break + end + end + end + if not lexer then + for patt, lex in pairs(patterns) do + if line:find(patt) then + lexer = lex + break + end + end end end + buffer:set_lexer_language(lexer or 'container') end --- -- [Local function] Performs actions suitable for a new buffer. -- Sets the buffer's lexer language and loads the language module. local function handle_new() - local buffer = buffer - if not set_lexer_from_filename(buffer.filename) then - set_lexer_from_sh_bang() - end - load_language_module_from_filename(buffer.filename) -end - ---- --- [Local function] Performs actions suitable for when buffers are switched. --- Sets the buffer's lexer language. -local function handle_switch() - if not set_lexer_from_filename(buffer.filename) then - if not set_lexer_from_sh_bang() then set_lexer_from_pattern() end + set_lexer() + if buffer.filename then + local lang = extensions[buffer.filename:match('[^/\\.]+$')] + if lang then + local ret, err = pcall(require, lang) + if ret then + _m[lang].set_buffer_properties() + elseif not ret and not err:find("^module '"..lang.."' not found:") then + textadept.events.error(err) + end + end end end textadept.events.add_handler('file_opened', handle_new) textadept.events.add_handler('file_saved_as', handle_new) -textadept.events.add_handler('buffer_switch', handle_switch) -textadept.events.add_handler('view_new', handle_switch) +textadept.events.add_handler('buffer_switch', set_lexer) +textadept.events.add_handler('view_new', set_lexer) |