From 7b000605365fc28542ac0bc50b41fc08c5a13f90 Mon Sep 17 00:00:00 2001 From: mitchell <70453897+667e-11@users.noreply.github.com> Date: Thu, 10 Jun 2010 23:53:20 -0400 Subject: Moved textadept.mime_types into the textadept module. --- core/ext/key_commands.lua | 4 +- core/ext/menu.lua | 2 +- core/ext/mime_types.conf | 313 -------------------------------------- core/ext/mime_types.lua | 213 -------------------------- init.lua | 3 - modules/textadept/init.lua | 3 +- modules/textadept/mime_types.conf | 313 ++++++++++++++++++++++++++++++++++++++ modules/textadept/mime_types.lua | 267 ++++++++++++++++++++++++++++++++ 8 files changed, 585 insertions(+), 533 deletions(-) delete mode 100644 core/ext/mime_types.conf delete mode 100644 core/ext/mime_types.lua create mode 100644 modules/textadept/mime_types.conf create mode 100644 modules/textadept/mime_types.lua diff --git a/core/ext/key_commands.lua b/core/ext/key_commands.lua index aed8d4a0..bed228dd 100644 --- a/core/ext/key_commands.lua +++ b/core/ext/key_commands.lua @@ -261,7 +261,7 @@ if not MAC then ['\t'] = { toggle_setting, 'use_tabs' }, [' '] = { toggle_setting, 'view_ws' }, } - keys.cl = { t.mime_types.select_lexer } + keys.cl = { _m.textadept.mime_types.select_lexer } keys['f5'] = { 'colourise', b, 0, -1 } -- Views @@ -423,7 +423,7 @@ else ['\t'] = { toggle_setting, 'use_tabs' }, [' '] = { toggle_setting, 'view_ws' }, } - keys.cl = { t.mime_types.select_lexer } + keys.cl = { _m.textadept.mime_types.select_lexer } keys['f5'] = { 'colourise', b, 0, -1 } -- Views diff --git a/core/ext/menu.lua b/core/ext/menu.lua index 20134d5b..7fede4da 100644 --- a/core/ext/menu.lua +++ b/core/ext/menu.lua @@ -277,7 +277,7 @@ local menubar = { }, } local lexer_menu = { title = l.MENU_LEX_TITLE } -for _, lexer in ipairs(textadept.mime_types.lexers) do +for _, lexer in ipairs(_m.textadept.mime_types.lexers) do lexer_menu[#lexer_menu + 1] = { lexer, ID.LEXER_START + #lexer_menu } end table.insert(menubar, #menubar, gtkmenu(lexer_menu)) -- before 'Help' diff --git a/core/ext/mime_types.conf b/core/ext/mime_types.conf deleted file mode 100644 index 10f4a8bb..00000000 --- a/core/ext/mime_types.conf +++ /dev/null @@ -1,313 +0,0 @@ -% Copyright 2007-2010 Mitchell mitchellcaladbolg.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 dmd -di dmd - -% diff -diff diff -patch diff - -% dot -dot dot - -% 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 hypertext -html hypertext -shtm hypertext -shtml hypertext - -% IDL -idl idl - -% Inform -inf inform - -% ini -ini ini -reg ini - -% Io -io Io - -% Java -bsh java -java java - -% Javascript -js javascript - -%JSON -json json - -% Latex -bbl latex -dtx latex -ins latex -ltx latex -tex latex -sty latex - -% Lilypond -ly lilypond - -% 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 - -% NSIS -nsh nsis -nsi nsis - -% Objective C -m objective_c -objc objective_c - -% OCAML -ml ocaml -mli ocaml -mll ocaml -mly ocaml - -% Octave -#octave matlab - -% 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 bash -csh bash -sh bash -#sh bash - -% 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 vb -bas vb -cls vb -ctl vb -dob vb -dsm vb -dsr vb -frm vb -pag vb -pag vb -vb vb -vba vb -vbs vb - -% 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 deleted file mode 100644 index 8e014e18..00000000 --- a/core/ext/mime_types.lua +++ /dev/null @@ -1,213 +0,0 @@ --- Copyright 2007-2010 Mitchell mitchellcaladbolg.net. See LICENSE. - -local textadept = _G.textadept -local locale = _G.locale - ---- --- Handles file-specific settings. -module('textadept.mime_types', package.seeall) - --- Markdown: --- ## Overview --- --- Files can be recognized and associated with programming language lexers in --- three ways: --- --- * By file extension. --- * By keywords in the file's shebang (`#!/path/to/exe`) line. --- * By a pattern that matches the file's first line. --- --- If a lexer is not associated with a file you open, first make sure the lexer --- exists in `lexers/`. If it does not, you will need to write one. Consult the --- [lexer][lexer] module for a tutorial. --- --- [lexer]: ../modules/lexer.html --- --- ## Configuration Files --- --- Built-in mime-types are located in `core/ext/mime_types.conf`. You can --- override or add to them in your `~/.textadept/mime_types.conf`. --- --- #### Detection by File Extension --- --- file_ext lexer --- --- Note: `file_ext` should not start with a `.` (period). --- --- #### Detection by Shebang Keywords --- --- #shebang_word lexer --- --- Examples of `shebang_word`'s are `lua`, `ruby`, `python`. --- --- #### Detection by Pattern --- --- /pattern lexer --- --- Only the last space, the one separating the pattern from the lexer, is --- significant. No spaces in the pattern need to be escaped. --- --- ## Extras --- --- This module adds an extra function to `buffer`: --- --- * **buffer:set\_lexer** (language)
--- Replacement for [`buffer:set_lexer_language()`][buffer_set_lexer_language].
--- Sets a buffer._lexer field so it can be restored without querying the --- mime-types tables. Also if the user manually sets the lexer, it should be --- restored.
--- Loads the language-specific module if it exists. --- - lang: The string language to set. --- --- [buffer_set_lexer_language]: buffer.html#buffer:set_lexer_language - ---- --- File extensions with their associated lexers. --- @class table --- @name extensions -extensions = {} - ---- --- Shebang words and their associated lexers. --- @class table --- @name shebangs -shebangs = {} - ---- --- First-line patterns and their associated lexers. --- @class table --- @name patterns -patterns = {} - --- Load mime-types from mime_types.conf -local mime_types -local f = io.open(_HOME..'/core/ext/mime_types.conf', 'rb') -if f then - mime_types = f:read('*all') - f:close() -end -f = io.open(_USERHOME..'/mime_types.conf', 'rb') -if f then - mime_types = mime_types..'\n'..f:read('*all') - f:close() -end -for line in mime_types:gmatch('[^\r\n]+') 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 - ---- --- List of detected lexers. --- Lexers are read from `lexers/` and `~/.textadept/lexers/`. --- @class table --- @name lexers -lexers = {} - --- Generate lexer list -local lexers_found = {} -local lfs = require 'lfs' -for lexer in lfs.dir(_HOME..'/lexers/') do - if lexer:find('%.lua$') and lexer ~= 'lexer.lua' then - lexers_found[lexer:match('^(.+)%.lua$')] = true - end -end -if lfs.attributes(_USERHOME..'/lexers/') then - for lexer in lfs.dir(_USERHOME..'/lexers/') do - if lexer:find('%.lua$') and lexer ~= 'lexer.lua' then - lexers_found[lexer:match('^(.+)%.lua$')] = true - end - end -end -for lexer in pairs(lexers_found) do lexers[#lexers + 1] = lexer end -table.sort(lexers) - --- --- Replacement for buffer:set_lexer_language(). --- Sets a buffer._lexer field so it can be restored without querying the --- mime-types tables. Also if the user manually sets the lexer, it should be --- restored. --- Loads the language-specific module if it exists. --- @param buffer The buffer to set the lexer language of. --- @param lang The string language to set. --- @usage buffer:set_lexer('language_name') -local function set_lexer(buffer, lang) - buffer._lexer = lang - buffer:set_lexer_language(lang) - 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 - error(err) - end - buffer:colourise(0, -1) -end -textadept.events.add_handler('buffer_new', - function() buffer.set_lexer = set_lexer end) --- Scintilla's first buffer doesn't have this -if not RESETTING then buffer.set_lexer = set_lexer end - --- Performs actions suitable for a new buffer. --- Sets the buffer's lexer language and loads the language module. -local function handle_new() - local lexer - if buffer.filename then - lexer = extensions[buffer.filename:match('[^/\\.]+$')] - end - if not lexer then - local line = buffer:get_line(0) - if line:find('^#!') then - for word in line:gsub('[/\\]', ' '):gmatch('%S+') do - lexer = shebangs[word] - if lexer then 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(lexer or 'container') -end - --- Sets the buffer's lexer based on filename, shebang words, or --- first line pattern. -local function restore_lexer() - buffer:set_lexer_language(buffer._lexer or 'container') -end - -textadept.events.add_handler('file_opened', handle_new) -textadept.events.add_handler('file_saved_as', handle_new) -textadept.events.add_handler('buffer_after_switch', restore_lexer) -textadept.events.add_handler('view_new', restore_lexer) -textadept.events.add_handler('reset_after', - function() buffer:set_lexer(buffer._lexer) end) - ---- --- Prompts the user to select a lexer from a filtered list for the current --- buffer. -function select_lexer() - local out = - textadept.dialog('filteredlist', - '--title', locale.MT_SELECT_LEXER, - '--button1', 'gtk-ok', - '--button2', 'gtk-cancel', - '--no-newline', - '--string-output', - '--columns', 'Name', - '--items', unpack(lexers)) - local response, lexer = out:match('([^\n]+)\n([^\n]+)$') - if response and response ~= 'gtk-cancel' then buffer:set_lexer(lexer) end -end diff --git a/init.lua b/init.lua index 672fd668..3cee810f 100644 --- a/init.lua +++ b/init.lua @@ -16,9 +16,6 @@ if not textadept.user_dofile('init.lua') then -- Core extension modules to load on startup. require 'ext/find' -- provides functionality for find/replace require 'ext/command_entry' -- provides tab-completion for the command entry -require 'ext/mime_types' -- provides support for language detection based on - -- the file; loads its language-specific module if - -- it exists -- Generic modules to load on startup. require 'textadept' diff --git a/modules/textadept/init.lua b/modules/textadept/init.lua index 027a9518..bd4245c9 100644 --- a/modules/textadept/init.lua +++ b/modules/textadept/init.lua @@ -7,6 +7,7 @@ module('_m.textadept', package.seeall) require 'textadept.bookmarks' require 'textadept.editing' -require 'textadept.snippets' +require 'textadept.mime_types' require 'textadept.run' require 'textadept.session' +require 'textadept.snippets' diff --git a/modules/textadept/mime_types.conf b/modules/textadept/mime_types.conf new file mode 100644 index 00000000..10f4a8bb --- /dev/null +++ b/modules/textadept/mime_types.conf @@ -0,0 +1,313 @@ +% Copyright 2007-2010 Mitchell mitchellcaladbolg.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 dmd +di dmd + +% diff +diff diff +patch diff + +% dot +dot dot + +% 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 hypertext +html hypertext +shtm hypertext +shtml hypertext + +% IDL +idl idl + +% Inform +inf inform + +% ini +ini ini +reg ini + +% Io +io Io + +% Java +bsh java +java java + +% Javascript +js javascript + +%JSON +json json + +% Latex +bbl latex +dtx latex +ins latex +ltx latex +tex latex +sty latex + +% Lilypond +ly lilypond + +% 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 + +% NSIS +nsh nsis +nsi nsis + +% Objective C +m objective_c +objc objective_c + +% OCAML +ml ocaml +mli ocaml +mll ocaml +mly ocaml + +% Octave +#octave matlab + +% 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 bash +csh bash +sh bash +#sh bash + +% 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 vb +bas vb +cls vb +ctl vb +dob vb +dsm vb +dsr vb +frm vb +pag vb +pag vb +vb vb +vba vb +vbs vb + +% XML +xhtml xml +xml xml +xsd xml +xsl xml +xslt xml +/^%s*<%?xml%s xml diff --git a/modules/textadept/mime_types.lua b/modules/textadept/mime_types.lua new file mode 100644 index 00000000..ba7a33c3 --- /dev/null +++ b/modules/textadept/mime_types.lua @@ -0,0 +1,267 @@ +-- Copyright 2007-2010 Mitchell mitchellcaladbolg.net. See LICENSE. + +local textadept = _G.textadept +local locale = _G.locale + +--- +-- Handles file-specific settings. +module('_m.textadept.mime_types', package.seeall) + +-- Markdown: +-- ## Overview +-- +-- Files can be recognized and associated with programming language lexers in +-- three ways: +-- +-- * By file extension. +-- * By keywords in the file's shebang (`#!/path/to/exe`) line. +-- * By a pattern that matches the file's first line. +-- +-- If a lexer is not associated with a file you open, first make sure the lexer +-- exists in `lexers/`. If it does not, you will need to write one. Consult the +-- [lexer][lexer] module for a tutorial. +-- +-- [lexer]: ../modules/lexer.html +-- +-- ## Configuration Files +-- +-- Built-in mime-types are located in `core/ext/mime_types.conf`. You can +-- override or add to them in your `~/.textadept/mime_types.conf`. +-- +-- #### Detection by File Extension +-- +-- file_ext lexer +-- +-- Note: `file_ext` should not start with a `.` (period). +-- +-- #### Detection by Shebang Keywords +-- +-- #shebang_word lexer +-- +-- Examples of `shebang_word`'s are `lua`, `ruby`, `python`. +-- +-- #### Detection by Pattern +-- +-- /pattern lexer +-- +-- Only the last space, the one separating the pattern from the lexer, is +-- significant. No spaces in the pattern need to be escaped. +-- +-- ## Extras +-- +-- This module adds an extra function to `buffer`: +-- +-- * **buffer:set\_lexer** (language)
+-- Replacement for [`buffer:set_lexer_language()`][buffer_set_lexer_language].
+-- Sets a buffer._lexer field so it can be restored without querying the +-- mime-types tables. Also if the user manually sets the lexer, it should be +-- restored.
+-- Loads the language-specific module if it exists. +-- - lang: The string language to set. +-- +-- [buffer_set_lexer_language]: buffer.html#buffer:set_lexer_language + +-- Markdown: +-- ## Overview +-- +-- Files can be recognized and associated with programming language lexers in +-- three ways: +-- +-- * By file extension. +-- * By keywords in the file's shebang (`#!/path/to/exe`) line. +-- * By a pattern that matches the file's first line. +-- +-- If a lexer is not associated with a file you open, first make sure the lexer +-- exists in `lexers/`. If it does not, you will need to write one. Consult the +-- [lexer][lexer] module for a tutorial. +-- +-- [lexer]: ../modules/lexer.html +-- +-- ## Configuration Files +-- +-- Built-in mime-types are located in `core/ext/mime_types.conf`. You can +-- override or add to them in your `~/.textadept/mime_types.conf`. +-- +-- #### Detection by File Extension +-- +-- file_ext lexer +-- +-- Note: `file_ext` should not start with a `.` (period). +-- +-- #### Detection by Shebang Keywords +-- +-- #shebang_word lexer +-- +-- Examples of `shebang_word`'s are `lua`, `ruby`, `python`. +-- +-- #### Detection by Pattern +-- +-- /pattern lexer +-- +-- Only the last space, the one separating the pattern from the lexer, is +-- significant. No spaces in the pattern need to be escaped. +-- +-- ## Extras +-- +-- This module adds an extra function to `buffer`: +-- +-- * **buffer:set\_lexer** (language)
+-- Replacement for [`buffer:set_lexer_language()`][buffer_set_lexer_language].
+-- Sets a buffer._lexer field so it can be restored without querying the +-- mime-types tables. Also if the user manually sets the lexer, it should be +-- restored.
+-- Loads the language-specific module if it exists. +-- - lang: The string language to set. +-- +-- [buffer_set_lexer_language]: buffer.html#buffer:set_lexer_language + +--- +-- File extensions with their associated lexers. +-- @class table +-- @name extensions +extensions = {} + +--- +-- Shebang words and their associated lexers. +-- @class table +-- @name shebangs +shebangs = {} + +--- +-- First-line patterns and their associated lexers. +-- @class table +-- @name patterns +patterns = {} + +-- Load mime-types from mime_types.conf +local mime_types +local f = io.open(_HOME..'/modules/textadept/mime_types.conf', 'rb') +if f then + mime_types = f:read('*all') + f:close() +end +f = io.open(_USERHOME..'/mime_types.conf', 'rb') +if f then + mime_types = mime_types..'\n'..f:read('*all') + f:close() +end +for line in mime_types:gmatch('[^\r\n]+') 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 + +--- +-- List of detected lexers. +-- Lexers are read from `lexers/` and `~/.textadept/lexers/`. +-- @class table +-- @name lexers +lexers = {} + +-- Generate lexer list +local lexers_found = {} +local lfs = require 'lfs' +for lexer in lfs.dir(_HOME..'/lexers/') do + if lexer:find('%.lua$') and lexer ~= 'lexer.lua' then + lexers_found[lexer:match('^(.+)%.lua$')] = true + end +end +if lfs.attributes(_USERHOME..'/lexers/') then + for lexer in lfs.dir(_USERHOME..'/lexers/') do + if lexer:find('%.lua$') and lexer ~= 'lexer.lua' then + lexers_found[lexer:match('^(.+)%.lua$')] = true + end + end +end +for lexer in pairs(lexers_found) do lexers[#lexers + 1] = lexer end +table.sort(lexers) + +-- +-- Replacement for buffer:set_lexer_language(). +-- Sets a buffer._lexer field so it can be restored without querying the +-- mime-types tables. Also if the user manually sets the lexer, it should be +-- restored. +-- Loads the language-specific module if it exists. +-- @param buffer The buffer to set the lexer language of. +-- @param lang The string language to set. +-- @usage buffer:set_lexer('language_name') +local function set_lexer(buffer, lang) + buffer._lexer = lang + buffer:set_lexer_language(lang) + 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 + error(err) + end + buffer:colourise(0, -1) +end +textadept.events.add_handler('buffer_new', + function() buffer.set_lexer = set_lexer end) +-- Scintilla's first buffer doesn't have this +if not RESETTING then buffer.set_lexer = set_lexer end + +-- Performs actions suitable for a new buffer. +-- Sets the buffer's lexer language and loads the language module. +local function handle_new() + local lexer + if buffer.filename then + lexer = extensions[buffer.filename:match('[^/\\.]+$')] + end + if not lexer then + local line = buffer:get_line(0) + if line:find('^#!') then + for word in line:gsub('[/\\]', ' '):gmatch('%S+') do + lexer = shebangs[word] + if lexer then 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(lexer or 'container') +end + +-- Sets the buffer's lexer based on filename, shebang words, or +-- first line pattern. +local function restore_lexer() + buffer:set_lexer_language(buffer._lexer or 'container') +end + +textadept.events.add_handler('file_opened', handle_new) +textadept.events.add_handler('file_saved_as', handle_new) +textadept.events.add_handler('buffer_after_switch', restore_lexer) +textadept.events.add_handler('view_new', restore_lexer) +textadept.events.add_handler('reset_after', + function() buffer:set_lexer(buffer._lexer) end) + +--- +-- Prompts the user to select a lexer from a filtered list for the current +-- buffer. +function select_lexer() + local out = + textadept.dialog('filteredlist', + '--title', locale.MT_SELECT_LEXER, + '--button1', 'gtk-ok', + '--button2', 'gtk-cancel', + '--no-newline', + '--string-output', + '--columns', 'Name', + '--items', unpack(lexers)) + local response, lexer = out:match('([^\n]+)\n([^\n]+)$') + if response and response ~= 'gtk-cancel' then buffer:set_lexer(lexer) end +end -- cgit v1.2.3