aboutsummaryrefslogtreecommitdiff
path: root/modules/textadept/mime_types.lua
blob: f3265b533ee3ce7112204f6b99ac9d0358c7cbff (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
-- Copyright 2007-2010 Mitchell mitchell<att>caladbolg.net. See LICENSE.

local textadept = _G.textadept
local locale = _G.locale
local events = _G.events

---
-- 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)<br />
--   Replacement for [`buffer:set_lexer_language()`][buffer_set_lexer_language].<br />
--   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.<br />
--   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)<br />
--   Replacement for [`buffer:set_lexer_language()`][buffer_set_lexer_language].<br />
--   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.<br />
--   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
events.connect('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

events.connect('file_opened', handle_new)
events.connect('file_saved_as', handle_new)
events.connect('buffer_after_switch', restore_lexer)
events.connect('view_new', restore_lexer)
events.connect('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