aboutsummaryrefslogtreecommitdiffhomepage
path: root/modules/lua/commands.lua
blob: 1f6d25a58d72f8d1cf43a133dc749b3bef0f7f88 (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
-- Copyright 2007 Mitchell mitchell<att>caladbolg.net. See LICENSE.

---
-- Commands for the lua module.
module('modules.lua.commands', package.seeall)

---
-- Patterns for auto 'end' completion for control structures.
-- @class table
-- @name control_structure_patterns
-- @see try_to_autocomplete_end
local control_structure_patterns = {
  '^%s*for', '^%s*function', '^%s*if', '^%s*repeat', '^%s*while',
  'function%s*%b()%s*$', '^%s*local%s*function'
}

---
-- Tries to autocomplete Lua's 'end' keyword for control structures like 'if',
-- 'while', 'for', etc.
-- @see control_structure_patterns
function try_to_autocomplete_end()
  local buffer = buffer
  buffer:begin_undo_action()
  buffer:line_end() buffer:new_line()
  local line_num = buffer:line_from_position(buffer.current_pos)
  local line = buffer:get_line(line_num - 1)
  for _, patt in ipairs(control_structure_patterns) do
    if line:match(patt) then
      local indent = buffer.line_indentation[line_num - 1]
      buffer:add_text( patt:match('repeat') and '\nuntil' or '\nend' )
      buffer.line_indentation[line_num + 1] = indent
      buffer.line_indentation[line_num] = indent + buffer.indent
      buffer:line_up() buffer:line_end()
      break
    end
  end
  buffer:end_undo_action()
end

---
-- Determines the Lua file being 'require'd, searches through package.path for
-- that file, and opens it in Textadept.
function goto_required()
  local buffer = buffer
  local line = buffer:get_line( buffer:line_from_position(buffer.current_pos) )
  local patterns = { 'require%s*(%b())', 'require%s*(([\'"])[^%2]+%2)' }
  local file
  for _, patt in ipairs(patterns) do
    file = line:match(patt)
    if file then break end
  end
  file = file:sub(2, -2):gsub('%.', '/')
  for path in package.path:gmatch('[^;]+') do
    path = path:gsub('?', file)
    local f = io.open(path)
    if f then f:close() textadept.io.open(path) break end
  end
end

---
-- Executes the current file.
function run()
  local buffer = buffer
  local out = io.popen('lua "'..buffer.filename..'" 2>&1'):read('*all')
  buffer = textadept.new_buffer()
  buffer:set_text('Command output:\n'..out)
  buffer:set_save_point()
end

-- Lua-specific key commands.
local keys = _G.keys
if type(keys) == 'table' then
  local m_editing = modules.textadept.editing
  local m_handlers = textadept.handlers
  keys.lua = {
    al = { textadept.io.open, _HOME..'/modules/lua/init.lua' },
    ac = {
      g = { goto_required }
    },
    ['s\n'] = { try_to_autocomplete_end },
    cq = { m_editing.block_comment, '--~' },
    cg = { run },
    ['('] = { function()
      buffer.word_chars =
        '_.:abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
      m_editing.show_call_tip(modules.lua.api, true)
      buffer:set_chars_default()
      return false
    end },
  }
end