aboutsummaryrefslogtreecommitdiff
path: root/core/ext
diff options
context:
space:
mode:
authormitchell <70453897+667e-11@users.noreply.github.com>2007-08-06 13:28:25 -0400
committermitchell <70453897+667e-11@users.noreply.github.com>2007-08-06 13:28:25 -0400
commit066e19ec0dea25d1fb855320c1fb2d5368e3da0d (patch)
treed221331e6e7d1b0e70bb245a0779603260300bf5 /core/ext
parente8482636ecf376b5f32b1fd04317485105b6b7ee (diff)
downloadtextadept-066e19ec0dea25d1fb855320c1fb2d5368e3da0d.tar.gz
textadept-066e19ec0dea25d1fb855320c1fb2d5368e3da0d.zip
Added more LuaDoc, shebang recognition; ext/core/mime_types.lua
Shebangs are split into words which are looked up in a shebangs table to determine the proper lexer language for the buffer.
Diffstat (limited to 'core/ext')
-rw-r--r--core/ext/mime_types.lua47
1 files changed, 42 insertions, 5 deletions
diff --git a/core/ext/mime_types.lua b/core/ext/mime_types.lua
index bf17219f..8daf658f 100644
--- a/core/ext/mime_types.lua
+++ b/core/ext/mime_types.lua
@@ -1,5 +1,9 @@
-- Copyright 2007 Mitchell mitchell<att>caladbolg.net. See LICENSE.
+---
+-- [Local table] Language names with their associated lexers.
+-- @type table
+-- @name languages
local languages = {
cpp = 'cpp',
css = 'css',
@@ -16,6 +20,10 @@ local languages = {
}
local l = languages
+---
+-- [Local table] File extensions with their associated languages.
+-- @type table
+-- @name extensions.
local extensions = {
c = l.cpp, cpp = l.cpp, cxx = l.cpp, h = l.cpp,
css = l.css,
@@ -33,8 +41,18 @@ local extensions = {
}
---
+-- [Local table] Shebang words and their associated languages.
+-- @type table
+-- @name shebangs
+local shebangs = {
+ lua = l.lua,
+ ruby = l.ruby
+}
+
+---
-- [Local] Sets the buffer's lexer language based on a filename.
-- @param filename The filename used to set the lexer language.
+-- @return boolean indicating if a lexer language was set.
local function set_lexer_from_filename(filename)
local lexer
if filename then
@@ -42,6 +60,22 @@ local function set_lexer_from_filename(filename)
lexer = extensions[ext]
end
buffer:set_lexer_language(lexer or 'container')
+ return lexer
+end
+
+---
+-- [Local] Sets the buffer's lexer language based on a shebang line.
+local function set_lexer_from_sh_bang()
+ local lexer
+ local line = buffer:get_line(0)
+ if line:match('^#!') then
+ line = line:gsub('[\\/]', ' ')
+ for word in line:gmatch('%S+') do
+ lexer = shebangs[word]
+ if lexer then break end
+ end
+ buffer:set_lexer_language(lexer)
+ end
end
---
@@ -64,19 +98,22 @@ end
---
-- [Local] Performs actions suitable for a new buffer.
--- Sets the lexer language and loads the language module based on the new
--- buffer's filename.
+-- Sets the buffer's lexer language and loads the language module.
local function handle_new()
local buffer = buffer
- set_lexer_from_filename(buffer.filename)
+ if not set_lexer_from_filename(buffer.filename) then
+ set_lexer_from_sh_bang()
+ end
load_language_module_from_filename(buffer.filename)
end
---
-- [Local] Performs actions suitable for when buffers are switched.
--- Sets the lexer language based on the current buffer's filename.
+-- Sets the buffer's lexer language.
local function handle_switch()
- set_lexer_from_filename(buffer.filename)
+ if not set_lexer_from_filename(buffer.filename) then
+ set_lexer_from_sh_bang()
+ end
end
local handlers = textadept.handlers