aboutsummaryrefslogtreecommitdiffhomepage
path: root/core/ext/mime_types.lua
blob: ec4ef80815ffc9bfc04a6331b0ce4a47d4e8ce6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
-- Copyright 2007 Mitchell mitchell<att>caladbolg.net. See LICENSE.

--- Handles file-specific settings (based on file extension).
module('textadept.mime_types', package.seeall)

---
-- [Local table] Language names with their associated lexers.
-- @class table
-- @name languages
local languages = {
  cpp = 'cpp',
  css = 'css',
  diff = 'diff',
  html = 'html',
  javascript = 'javascript',
  lua = 'lua',
  makefile = 'makefile',
  php = 'php',
  python = 'python',
  rhtml = 'rhtml',
  ruby = 'ruby',
  xml = 'xml'
}

local l = languages
---
-- [Local table] File extensions with their associated languages.
-- @class table
-- @name extensions
local extensions = {
  c = l.cpp, cpp = l.cpp, cxx = l.cpp, h = l.cpp,
  css = l.css,
  diff = l.diff, patch = l.diff,
  html = l.html, htm = l.html, shtml = l.html,
  iface = l.makefile,
  js = l.javascript,
  lua = l.lua,
  mak = l.makefile, makefile = l.makefile, Makefile = l.makefile,
  php = l.php,
  py = l.python, pyw = l.python,
  rhtml = l.rhtml,
  rb = l.rb, rbw = l.rb,
  xml = l.xml, xsl = l.xml, xslt = l.xml
}

---
-- [Local table] Shebang words and their associated languages.
-- @class table
-- @name shebangs
local shebangs = {
  lua = l.lua,
  ruby = l.ruby
}

---
-- [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 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

---
-- [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:match("^module '"..lang.."' not found:") then
      textadept.handlers.error(err)
    end
  end
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
    set_lexer_from_sh_bang()
  end
end

local handlers = textadept.handlers
handlers.add_handler_function('file_opened', handle_new)
handlers.add_handler_function('file_saved_as', handle_new)
handlers.add_handler_function('buffer_switch', handle_switch)
handlers.add_handler_function('view_new', handle_switch)