This repository was archived by the owner on Sep 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinit.lua
343 lines (303 loc) · 10.6 KB
/
init.lua
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
-- Copyright 2007-2022 Mitchell. See LICENSE.
local M = {}
--[[ This comment is for LuaDoc.
---
-- The reST module for Textadept.
-- It provides utilities for editing reST and Sphinx documents.
--
-- **WARNING:** this module is deprecated. It may not work in Textadept 12.0.
--
-- ### Key Bindings
--
-- + `Ctrl+Alt+J` (`^⌘J` | `M-S-J`)
-- Jump to the selected section.
-- + `Shift+Enter` (`⇧↩` | `S-Enter`)
-- Open the image specified by the directive on the current line.
--
-- @field DOCUTILS_PATH (string)
-- The absolute path to the directory that contains the Python [Docutils][] library if it is
-- not in the environment's `PYTHONPATH`.
-- The default value is `nil`, which indicates Docutils is installed.
--
-- [Docutils]: http://docutils.sourceforge.net/
module('_M.rest')]]
M.DOCUTILS_PATH = nil
-- Autocompletion and documentation.
local sep = string.char(buffer.auto_c_type_separator)
local XPM = textadept.editing.XPM_IMAGES
local dirs = {
'admonition', 'attention', 'caution', 'citations', 'class', 'code', 'compound', 'container',
'contents', 'csv-table', 'danger', 'date', 'default-role', 'epigraph', 'error', 'figure',
'footer', 'footnotes', 'header', 'highlights', 'hint', 'image', 'important', 'include',
'line-block', 'list-table', 'math', 'meta', 'note', 'parsed-literal', 'pull-quote', 'raw',
'replace', 'restructuredtext-test-directive', 'role', 'rubric', 'section-autonumbering',
'sectnum', 'sidebar', 'table', 'target-notes', 'tip', 'title', 'topic', 'unicode', 'warning'
}
for i = 1, #dirs do dirs[i] = ('%s::%s%d'):format(dirs[i], sep, XPM.METHOD) end
local sphinx_dirs = {
'centered', 'code-block', 'deprecated', 'glossary', 'highlight', 'hlist', 'index',
'literalinclude', 'note', 'only', 'productionlist', 'rubric', 'sectionauthor', 'seealso',
'tabularcolumns', 'toctree', 'versionadded', 'versionchanged', 'warning'
}
for i = 1, #sphinx_dirs do dirs[#dirs + 1] = ('%s::%s%d'):format(sphinx_dirs[i], sep, XPM.SLOT) end
-- LuaFormatter off
local options = {
image = {'alt', 'height', 'width', 'scale', 'align', 'target'},
figure = {'alt', 'height', 'width', 'scale', 'align', 'target', 'figwidth', 'figclass'},
sidebar = {'subtitle'},
code = {'number-lines'},
['csv-table'] = {
'widths', 'header-rows', 'stub-columns', 'header', 'file', 'url', 'encoding', 'delim', 'quote',
'keepspace', 'escape'
},
['list-table'] = {'widths', 'header-rows', 'stub-columns'},
contents = {'depth', 'local', 'backlinks'},
sectnum = {'depth', 'prefix', 'suffix', 'start'},
['section-autonumbering'] = {'depth', 'prefix', 'suffix', 'start'},
unicode = {'ltrim', 'rtrim', 'trim'},
include = {
'start-line', 'end-line', 'start-after', 'end-before', 'literal', 'code', 'number-lines',
'encoding', 'tab-width'
},
raw = {'file', 'url', 'encoding'},
role = {'class'},
toctree = {'maxdepth', 'numbered', 'titlesonly', 'glob', 'hidden', 'includehidden'},
hlist = {'columns'},
glossary = {'sorted'},
highlight = {'linenothreshold'},
['code-block'] = {'linenos', 'emphasize-lines'},
sourcecode = {'linenos', 'emphasize-lines'},
['literal-include'] = {
'tab-width', 'linenos', 'emphasize-lines', 'language', 'encoding', 'pyobject', 'lines',
'start-after', 'end-before', 'prepend', 'append'
}
}
-- LuaFormatter on
for _, v in pairs(options) do
v[#v + 1], v[#v + 2] = 'name', 'class' -- global options
for i = 1, #v do v[i] = ('%s::%s%d'):format(v[i], sep, XPM.VARIABLE) end
end
local roles = {
'emphasis', 'literal', 'code', 'math', 'pep-reference', 'PEP', 'rfc-reference', 'RFC', 'string',
'subscript', 'sub', 'superscript', 'sup', 'title-reference', 'title', 't', 'raw'
}
for i = 1, #roles do roles[i] = ('%s:%s%d'):format(roles[i], sep, XPM.METHOD) end
local sphinx_roles = {
'ref', 'doc', 'download', 'envvar', 'token', 'keyword', 'option', 'term', 'abbr', 'command',
'dfn', 'file', 'guilabel', 'kbd', 'mailheader', 'makevar', 'manpage', 'menuselection', 'mimetype',
'newsgroup', 'program', 'regexp', 'samp', 'index', 'pep', 'rfc'
}
for i = 1, #sphinx_roles do
roles[#roles + 1] = ('%s:%s%d'):format(sphinx_roles[i], sep, XPM.SLOT)
end
textadept.editing.autocompleters.rest = function()
local list = {}
-- Retrieve the symbol behind the caret.
local line, pos = buffer:get_cur_line()
local line_part = line:sub(1, pos - 1)
local part = line_part:match('[%w-]*$')
local name = '^' .. part
-- Determine whether or not the symbol is a directive, parameter, or role, and autocomplete
-- as appropriate.
if line_part:find('^%s*%.%. [%w-]*$') then
-- Autocomplete directive.
for _, dir in ipairs(dirs) do if dir:find(name) then list[#list + 1] = dir end end
elseif line_part:find('^%s*:[%w-]*$') then
-- Autocomplete parameter or role.
for i = buffer:line_from_position(buffer.current_pos) - 1, 1, -1 do
line = buffer:get_line(i)
local dir_options = options[line:match('^%s*%.%. ([%w-]+)::[ \r\n]')]
if dir_options then
-- Autocomplete parameter.
for _, option in ipairs(dir_options) do
if option:find(name) then list[#list + 1] = option end
end
break
end
if not line:find('^%s*:') then
-- Autocomplete role.
for _, role in ipairs(roles) do if role:find(name) then list[#list + 1] = role end end
break
end
end
elseif line_part:find('%s+:[%w-]*$') then
-- Autocomplete role.
for _, role in ipairs(roles) do if role:find(name) then list[#list + 1] = role end end
end
return #part, list
end
textadept.editing.api_files.rest = {_HOME .. '/modules/rest/api', _USERHOME .. '/modules/rest/api'}
-- Commands.
-- Add '`' to autopaired and typeover characters.
events.connect(events.LEXER_LOADED, function(name)
if textadept.editing.auto_pairs and textadept.editing.typeover_chars then
local rest = name == 'rest'
textadept.editing.auto_pairs[string.byte('`')] = rest and '`' or nil
textadept.editing.typeover_chars[string.byte('`')] = rest and 1 or nil
end
end)
-- Enable folding by the Sphinx convention for detected Sphinx files:
-- # > * > = > - > ^ > ".
events.connect(events.LEXER_LOADED, function(name)
if name == 'rest' and buffer:get_line(1):find('^%s*%.%. .-sphinx') then
buffer.property['fold.scintillua.rest.by.sphinx.convention'] = '1'
buffer:colorize(1, buffer.end_styled)
end
end)
local cmd = 'python "' .. _HOME .. '/modules/rest/rst2pseudoxml.py" ' .. '--report=2 --halt=5 "%s"'
-- Show syntax errors as annotations.
events.connect(events.FILE_AFTER_SAVE, function()
if buffer.lexer_language ~= 'rest' then return end
buffer:annotation_clear_all()
view.annotation_visible = view.ANNOTATION_BOXED
local jumped = false
local filename = buffer.filename:iconv(_CHARSET, 'UTF-8')
os.spawn(cmd:format(filename), M.DOCUTILS_PATH, nil, function(chunk)
for line in chunk:gmatch('[^\r\n]+') do
local line_num, msg = line:match('^[^:]+:(%d+):%s*(.+)$')
if line_num and msg and (msg:find('WARNING') or msg:find('ERROR') or msg:find('SEVERE')) then
if msg:find('Unknown interpreted text role') then
-- Ignore role errors when it comes to Sphinx roles.
-- TODO: determine if the document is Sphinx or not?
for _, role in ipairs(sphinx_roles) do
if msg:find(string.format('"%s"', role)) then goto continue end
end
end
buffer.annotation_text[line_num] = msg
local GETNAMEDSTYLE = _SCINTILLA.properties.named_styles[1]
local style = buffer:private_lexer_call(GETNAMEDSTYLE, 'error')
buffer.annotation_style[line_num] = style
if not jumped then
buffer:goto_line(line_num)
jumped = true
end
::continue::
end
end
end)
end)
---
-- Prompts the user to select a section title to jump to.
-- Requires the entire document to be styled.
-- @name goto_section
function M.goto_section()
if buffer.end_styled < buffer.length then buffer:colorize(1, -1) end
local items = {}
for i = 1, buffer.line_count - 2 do
if buffer.fold_level[i + 1] & buffer.FOLDLEVELHEADERFLAG > 0 then
local name = buffer:get_line(i + 1):match('^.') .. buffer:get_line(i):match('^[^\r\n]*')
if name then items[#items + 1], items[#items + 2] = i + 1, name end
end
end
local i = ui.dialogs.list{
title = 'Goto Section', columns = {'Line', 'Name'}, items = items, search_column = 2
}
if i then textadept.editing.goto_line(tonumber(items[i])) end
end
---
-- Opens the image specified in an "image" or "figure" directive on the current line.
-- @name open_image
function M.open_image()
local line = buffer:get_cur_line()
local file = line:match('^%s*%.%. image::%s+(%S+)') or line:match('^%s*%.%. figure::%s+(%S+)')
if not file or not buffer.filename then return end
local cmd = 'xdg-open "%s"'
if WIN32 then
cmd = 'start "" "%s"'
elseif OSX then
cmd = 'open "file://%s"'
end
os.spawn(cmd:format(buffer.filename:match('^.+[/\\]') .. file))
end
keys.rest[not (OSX and not CURSES) and 'ctrl+alt+g' or 'ctrl+cmd+g'] = M.goto_section
keys.rest['shift+\n'] = M.open_image
-- Snippets.
local snip = snippets.rest
snip.attention = [[
.. attention::
%0
]]
snip.danger = [[
.. danger::
%0
]]
snip.error = [[
.. error::
%0
]]
snip.hint = [[
.. hint::
%0
]]
snip.important = [[
.. important::
%0
]]
snip.note = [[
.. note::
%0
]]
snip.tip = [[
.. tip::
%0
]]
snip.warning = [[
.. warning::
%0
]]
snip.admonition = [[
.. admonition:: %1(title)
%0
]]
snip.image = [[
.. image:: %1(picture.jpg)
:height: %2(100px)
:width: %3(200px)
:scale: %4(50%%)
:alt: %5(alternate text)
:align: %6(right)
]]
snip.figure = [[
.. figure:: %1(picture.jpg)
:height: %2(100px)
:width: %3(200px)
:scale: %4(50%%)
:alt: %5(alternate text)
:align: %6(right)
:figwidth: %7(figure width)
:figclass: %8(figure class)
%9(caption)
%10(legend)
]]
snip.codeblock = [[
.. code-block:: %1(language)
%2(:linenos:
)%0
]]
snip.footnote = [[[#f%1(number)]_
.. rubric:: %2(Footnotes)
.. [#f1] Text of the first footnote.
.. [#f2] Text of the second footnote.
]]
snip.replace = [[
.. |%1(name)| replace:: %2(replacement *text*)
]]
snip.repeatingimage = [[
.. |%1(caution)| image:: %2(warning.png)
:height: %3(100px)
:width: %4(200px)
:scale: %5(50%%)
:alt: %6(warning)
:align: %7(right)
]]
snip.comment = ".. %0"
snip.anchor = [[
.. _`%1(anchor)`: ]]
snip.reference = [[:ref:`%1(anchor)`]]
snip.emphasis = [[*%1(text)*]]
snip.strong_emphasis = [[**%1(text)**]]
snip.code = [[``%1(text)``]]
snip.external_link_together = [[`%1(link text) <%2(http://example.com/)>`_]]
snip.external_link_separated = [[`%1(link text)`_
.. _`%1`: %2(http://example.com/)]]
return M