aboutsummaryrefslogtreecommitdiff
path: root/modules/textadept/run.lua
blob: 38eb63013446e0f6db7a8e70ea2ec32f85bb1df1 (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
-- Copyright 2007-2013 Mitchell mitchell.att.foicica.com. See LICENSE.

local M = {}

--[[ This comment is for LuaDoc.
---
-- Compile and run/execute source files with Textadept.
-- Typically, [language-specific modules][] populate the `compile_command`,
-- `run_command`, and `error_detail` tables for a particular language's file
-- extension.
--
-- [language-specific modules]: _M.html#Compile.and.Run
-- @field cwd (string, Read-only)
--   The working directory for the most recently executed compile or run
--   command.
-- @field _G.events.COMPILE_OUTPUT (string)
--   Called after executing a language's compile command.
--   By default, compiler output is printed to the message buffer. To override
--   this behavior, connect to the event with an index of `1` and return `true`.
--   Arguments:
--
--   * `lexer`: The lexer language name.
--   * `output`: The string output from the command.
-- @field _G.events.RUN_OUTPUT (string)
--   Called after executing a language's run command.
--   By default, output is printed to the message buffer. To override this
--   behavior, connect to the event with an index of `1` and return `true`.
--   Arguments:
--
--   * `lexer`: The lexer language name.
--   * `output`: The string output from the command.
module('_M.textadept.run')]]

-- Events.
local events, events_connect, events_emit = events, events.connect, events.emit
events.COMPILE_OUTPUT, events.RUN_OUTPUT = 'compile_output', 'run_output'

local preferred_view

-- Executes a compile or run command.
-- Emits a `COMPILE_OUTPUT` or `RUN_OUTPUT` event based on the `compiling` flag.
-- @param cmd_table Either `compile_command` or `run_command`.
-- @param compiling Flag indicating whether or not the command is a compiler
--   command. The default value is `false`.
-- @see _G.events
local function command(cmd_table, compiling)
  if not buffer.filename then return end
  buffer:annotation_clear_all()
  buffer:save()
  local command = cmd_table[buffer.filename:match('[^.]+$')]
  if not command then return end
  if type(command) == 'function' then command = command() end

  preferred_view = view
  local filepath = buffer.filename:iconv(_CHARSET, 'UTF-8')
  local filedir, filename = '', filepath
  if filepath:find('[/\\]') then
    filedir, filename = filepath:match('^(.+[/\\])([^/\\]+)$')
  end
  local filename_noext = filename:match('^(.+)%.')
  command = command:gsub('%%%b()', {
    ['%(filepath)'] = filepath, ['%(filedir)'] = filedir,
    ['%(filename)'] = filename, ['%(filename_noext)'] = filename_noext,
  })
  local current_dir = lfs.currentdir()
  lfs.chdir(filedir)
  local event = compiling and events.COMPILE_OUTPUT or events.RUN_OUTPUT
  local lexer = buffer:get_lexer()
  events_emit(event, lexer, '> '..command:iconv('UTF-8', _CHARSET))
  local p = io.popen(command..' 2>&1')
  for line in p:lines() do
    events_emit(event, lexer, line:iconv('UTF-8', _CHARSET))
  end
  local ok, status, code = p:close()
  if ok and code then events_emit(event, lexer, status..': '..code) end
  M.cwd = filedir
  lfs.chdir(current_dir)
end

-- Parses the given message for an error description and returns a table of the
-- error's details.
-- @param message The message to parse for errors.
-- @see error_detail
local function get_error_details(message)
  for _, error_detail in pairs(M.error_detail) do
    local captures = {message:match(error_detail.pattern)}
    if #captures > 0 then
      local details = {}
      for detail, i in pairs(error_detail) do details[detail] = captures[i] end
      return details
    end
  end
  return nil
end

-- Prints the output from a run or compile command.
-- If the output is an error message, an annotation is shown in the source file
-- if the file is currently open in another view.
-- @param lexer The current lexer.
-- @param output The output to print.
local function print_output(lexer, output)
  gui.print(output)
  local error_details = get_error_details(output)
  if not error_details or not error_details.message then return end
  for i = 1, #_VIEWS do
    local filename = _VIEWS[i].buffer.filename
    if filename and filename:find(error_details.filename..'$') then
      gui.goto_view(i)
      buffer.annotation_text[error_details.line - 1] = error_details.message
      buffer.annotation_style[error_details.line - 1] = 8 -- error_details
      return
    end
  end
end

---
-- Map of file extensions (excluding the leading '.') to their associated
-- "compile" shell command line strings or functions returning such strings.
-- Command line strings may have the following macros:
--
--   + `%(filepath)`: The full path of the current file.
--   + `%(filedir)`: The current file's directory path.
--   + `%(filename)`: The name of the file, including its extension.
--   + `%(filename_noext)`: The name of the file, excluding its extension.
--
-- This table is typically populated by [language-specific modules][].
--
-- [language-specific modules]: _M.html#Compile.and.Run
-- @class table
-- @name compile_command
M.compile_command = {}

---
-- Compiles the file based on its extension using the command from the
-- `compile_command` table.
-- Emits a `COMPILE_OUTPUT` event.
-- @see compile_command
-- @see _G.events
-- @name compile
function M.compile() command(M.compile_command, true) end
events_connect(events.COMPILE_OUTPUT, print_output)

---
-- Map of file extensions (excluding the leading '.') to their associated
-- "run" shell command line strings or functions returning such strings.
-- Command line strings may have the following macros:
--
--   + `%(filepath)`: The full path of the current file.
--   + `%(filedir)`: The current file's directory path.
--   + `%(filename)`: The name of the file, including its extension.
--   + `%(filename_noext)`: The name of the file, excluding its extension.
--
-- This table is typically populated by [language-specific modules][].
--
-- [language-specific modules]: _M.html#Compile.and.Run
-- @class table
-- @name run_command
M.run_command = {}

---
-- Runs/executes the file based on its extension using the command from the
-- `run_command` table.
-- Emits a `RUN_OUTPUT` event.
-- @see run_command
-- @see _G.events
-- @name run
function M.run() command(M.run_command) end
events_connect(events.RUN_OUTPUT, print_output)

---
-- Map of lexer names to their error string details, tables containing the
-- following fields:
--
--   + `pattern`: A Lua pattern that matches the language's error string,
--     capturing the filename the error occurs in, the line number the error
--     occurred on, and optionally the error message.
--   + `filename`: The numeric index of the Lua capture containing the filename
--      the error occurred in.
--   + `line`: The numeric index of the Lua capture containing the line number
--      the error occurred on.
--   + `message`: (Optional) The numeric index of the Lua capture containing the
--     error's message. An annotation will be displayed if a message was
--     captured.
--
-- When an error message is double-clicked, the user is taken to the point of
-- error.
-- This table is usually populated by [language-specific modules][].
--
-- [language-specific modules]: _M.html#Compile.and.Run
-- @class table
-- @name error_detail
M.error_detail = {}

---
-- Goes to line number *line_num* in the file an error occurred at based on the
-- error message at position *pos* in the buffer and displays an annotation with
-- the error message.
-- This is typically called by an event handler for when the user double-clicks
-- on an error message.
-- @param pos The position of the caret in the buffer.
-- @param line_num The line number the caret is on with the error message.
-- @see error_detail
function goto_error(pos, line_num)
  if buffer._type ~= _L['[Message Buffer]'] and
     buffer._type ~= _L['[Error Buffer]'] then
    return
  end
  local error_details = get_error_details(buffer:get_line(line_num))
  if not error_details then return end
  gui.goto_file(M.cwd..error_details.filename, true, preferred_view, true)
  local line, message = error_details.line, error_details.message
  buffer:goto_line(line - 1)
  if message then
    buffer.annotation_text[line - 1] = message
    buffer.annotation_style[line - 1] = 8 -- error
  end
end
events_connect(events.DOUBLE_CLICK, goto_error)

return M