diff options
author | mitchell <70453897+orbitalquark@users.noreply.github.com> | 2020-10-17 16:02:51 -0400 |
---|---|---|
committer | mitchell <70453897+orbitalquark@users.noreply.github.com> | 2020-10-17 16:02:51 -0400 |
commit | c511448b28a5b5c1e60ab87518f65245d169ae5b (patch) | |
tree | 6c0d4b904e22863ad2fe8dd3c55ff5d96cab835f /modules | |
parent | 66bfb5b77b53ebfaea6c93be2be7504427098ee6 (diff) |
Added `textadept.run.set_arguments()`.
This replaces custom code in *modules/textadept/menu.lua*.
Diffstat (limited to 'modules')
-rw-r--r-- | modules/textadept/menu.lua | 28 | ||||
-rw-r--r-- | modules/textadept/run.lua | 45 |
2 files changed, 46 insertions, 27 deletions
diff --git a/modules/textadept/menu.lua b/modules/textadept/menu.lua index 1c7aa4b1..6f59b7ff 100644 --- a/modules/textadept/menu.lua +++ b/modules/textadept/menu.lua @@ -172,33 +172,7 @@ local default_menubar = { SEPARATOR, {_L['Run'], textadept.run.run}, {_L['Compile'], textadept.run.compile}, - {_L['Set Arguments...'], function() - if not buffer.filename then return end - local run_commands = textadept.run.run_commands - local compile_commands = textadept.run.compile_commands - local base_commands, utf8_args = {}, {} - for i, commands in ipairs{run_commands, compile_commands} do - -- Compare the base run/compile command with the one for the current - -- file. The difference is any additional arguments set previously. - base_commands[i] = commands[buffer.filename:match('[^.]+$')] or - commands[buffer:get_lexer()] or '' - local current_command = commands[buffer.filename] or '' - local args = current_command:sub(#base_commands[i] + 2) - utf8_args[i] = args:iconv('UTF-8', _CHARSET) - end - local button, utf8_args = ui.dialogs.inputbox{ - title = _L['Set Arguments...']:gsub('_', ''), informative_text = { - _L['Command line arguments'], _L['For Run:'], _L['For Compile:'] - }, text = utf8_args, width = not CURSES and 400 or nil - } - if button ~= 1 then return end - for i, commands in ipairs{run_commands, compile_commands} do - -- Add the additional arguments to the base run/compile command and set - -- the new command to be the one used for the current file. - commands[buffer.filename] = string.format('%s %s', base_commands[i], - utf8_args[i]:iconv(_CHARSET, 'UTF-8')) - end - end}, + {_L['Set Arguments...'], textadept.run.set_arguments}, {_L['Build'], textadept.run.build}, {_L['Stop'], textadept.run.stop}, {_L['Next Error'], function() textadept.run.goto_error(true) end}, diff --git a/modules/textadept/run.lua b/modules/textadept/run.lua index eedc2b68..e2c54db8 100644 --- a/modules/textadept/run.lua +++ b/modules/textadept/run.lua @@ -271,6 +271,51 @@ end events.connect(events.RUN_OUTPUT, print_output) --- +-- Appends the command line argument strings *run* and *compile* to their +-- respective run and compile commands for file *filename* or the current file. +-- If either is `nil`, prompts the user for missing the arguments. Each filename +-- has its own set of compile and run arguments. +-- @param filename Optional path to the file to set run/compile arguments for. +-- @param run Optional string run arguments to set. If `nil`, the user is +-- prompted for them. Pass the empty string for no run arguments. +-- @param compile Optional string compile arguments to set. If `nil`, the user +-- is prompted for them. Pass the empty string for no compile arguments. +-- @see run_commands +-- @see compile_commands +-- @name set_arguments +function M.set_arguments(filename, run, compile) + assert_type(filename, 'string/nil', 1) + assert_type(run, 'string/nil', 2) + assert_type(compile, 'string/nil', 3) + if not filename then filename = buffer.filename end + local base_commands, utf8_args = {}, {} + for i, commands in ipairs{M.run_commands, M.compile_commands} do + -- Compare the base run/compile command with the one for the current + -- file. The difference is any additional arguments set previously. + base_commands[i] = commands[filename:match('[^.]+$')] or + commands[buffer:get_lexer()] or '' + local current_command = commands[filename] or '' + local args = (i == 1 and run or compile) or + current_command:sub(#base_commands[i] + 2) + utf8_args[i] = args:iconv('UTF-8', _CHARSET) + end + if not run or not compile then + local button, utf8_args = ui.dialogs.inputbox{ + title = _L['Set Arguments...']:gsub('_', ''), informative_text = { + _L['Command line arguments'], _L['For Run:'], _L['For Compile:'] + }, text = utf8_args, width = not CURSES and 400 or nil + } + if button ~= 1 then return end + end + for i, commands in ipairs{M.run_commands, M.compile_commands} do + -- Add the additional arguments to the base run/compile command and set + -- the new command to be the one used for the current file. + commands[filename] = string.format('%s %s', base_commands[i], + utf8_args[i]:iconv(_CHARSET, 'UTF-8')) + end +end + +--- -- Map of project root paths and "makefiles" to their associated "build" shell -- command line strings or functions that return such strings. -- Functions may also return a working directory to operate in. By default, it |