orbitalquark / textadept

Textadept is a fast, minimalist, and remarkably extensible cross-platform text editor for programmers.
https://orbitalquark.github.io/textadept
MIT License
640 stars 38 forks source link

View and buffer switch on run in split view when textadept.run.run_in_background = true #383

Closed jxzwp closed 1 year ago

jxzwp commented 1 year ago

In 12 alpha 2, with textadept.run.run_in_background = true, when you have a split view and you run a file, an [Output Buffer] is created BUT focus is given to the other view in the split, and it's buffer is switched to be the same as that of the view that originally had focus.

For example, when you have a horizontal split and the top view is focused and you run the file in that view, an [Output Buffer] is created BUT focus is given to the bottom view and it's buffer is switched to be the same as the top view.

If you repeat the above when an [Output Buffer] is already open, it works as expected, with the current view retaining focus. When textadept.run.run_in_background = false it works as expected.

The problem is in core/ui.lua:70 print_to() which wrongly switches or splits the view for a "silent" print when the [Output Buffer] doesn't exist.

Below is my quick temporary fix:

local function print_to(buffer_type, silent, format, ...)
  local print_view, buffer = get_print_view(buffer_type), get_print_buffer(buffer_type)
  if not buffer or not silent and not print_view then -- no buffer or buffer not visible
    if not silent then      -- new line
      if #_VIEWS > 1 then
        ui.goto_view(1) -- go to another view to print to
      elseif not ui.tabs then
        view:split() -- create a new view to print to
      end
    end                     -- new line
    if not buffer then
      buffer = _G.buffer.new()
      buffer._type = buffer_type
    else
      view:goto_buffer(buffer)
    end
  elseif print_view and not silent then
    ui.goto_view(print_view)
  end
  local prev_line_count = buffer.line_count
  local args = table.pack(...)
  for i = 1, args.n do args[i] = tostring(args[i]) end
  buffer:append_text(table.concat(args, format and '\t' or ''))
  if format then buffer:append_text('\n') end
  buffer:goto_pos(buffer.length + 1)
  buffer:set_save_point()
  for _, view in ipairs(_VIEWS) do
    -- Scroll all views showing this buffer (if any).
    if view.buffer == buffer and view ~= _G.view then view:goto_pos(buffer.length + 1) end
  end
  buffer._folds = nil -- reset buffer state
  return buffer
end
orbitalquark commented 1 year ago

Wow, thanks so much for your detailed report and analysis! This should be fixed with https://github.com/orbitalquark/textadept/commit/ebe27677f05b6b649d5267163f78a2f3e2f02005.