aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--core/.view.luadoc12
-rw-r--r--core/file_io.lua10
-rw-r--r--core/ui.lua35
-rw-r--r--doc/manual.md11
-rw-r--r--modules/textadept/bookmarks.lua2
-rw-r--r--modules/textadept/find.lua4
-rw-r--r--modules/textadept/menu.lua8
-rw-r--r--modules/textadept/run.lua4
-rw-r--r--modules/textadept/session.lua8
-rw-r--r--src/textadept.c61
10 files changed, 78 insertions, 77 deletions
diff --git a/core/.view.luadoc b/core/.view.luadoc
index 2ae3cb43..4f4af0d2 100644
--- a/core/.view.luadoc
+++ b/core/.view.luadoc
@@ -33,16 +33,12 @@ function split(view, vertical) end
function unsplit(view) end
---
--- Switches to buffer number *n* in the view.
--- *relative* indicates whether or not *n* is an index relative to the current
--- buffer's index in `_BUFFERS` instead of an absolute index.
+-- Switches to buffer *buffer* or the buffer *buffer* number of buffers relative
+-- to the current one.
-- Emits `BUFFER_BEFORE_SWITCH` and `BUFFER_AFTER_SWITCH` events.
-- @param view The view to switch buffers in.
--- @param n A relative or absolute buffer index in `_BUFFERS`. An absolute index
--- of `-1` goes to the last buffer.
--- @param relative Optional flag indicating whether *n* is a relative or
--- absolute index. The default value is `false`, for an absolute index.
+-- @param buffer A buffer or relative buffer number (typically 1 or -1).
-- @see _G._BUFFERS
-- @see events.BUFFER_BEFORE_SWITCH
-- @see events.BUFFER_AFTER_SWITCH
-function goto_buffer(view, n, relative) end
+function goto_buffer(view, buffer) end
diff --git a/core/file_io.lua b/core/file_io.lua
index c4aecfe3..84f0287f 100644
--- a/core/file_io.lua
+++ b/core/file_io.lua
@@ -104,7 +104,7 @@ function io.open_file(filenames)
local filename = lfs.abspath((filenames[i]:gsub('^file://', '')))
for j = 1, #_BUFFERS do
if filename == _BUFFERS[j].filename then
- view:goto_buffer(j) -- already open
+ view:goto_buffer(_BUFFERS[j]) -- already open
goto continue
end
end
@@ -234,10 +234,10 @@ end
-- @see io.save_file
-- @name save_all_files
function io.save_all_files()
- local current_buffer = _BUFFERS[buffer]
+ local current_buffer = buffer
for i = 1, #_BUFFERS do
if _BUFFERS[i].filename and _BUFFERS[i].modify then
- view:goto_buffer(i)
+ view:goto_buffer(_BUFFERS[i])
io.save_file()
end
end
@@ -272,7 +272,7 @@ end
-- @name close_all_buffers
function io.close_all_buffers()
while #_BUFFERS > 1 do
- view:goto_buffer(#_BUFFERS)
+ view:goto_buffer(_BUFFERS[#_BUFFERS])
if not io.close_buffer() then return false end
end
return io.close_buffer() -- the last one
@@ -311,7 +311,7 @@ end)
events_connect(events.FILE_OPENED, function()
local buf = _BUFFERS[1]
if #_BUFFERS == 2 and not (buf.filename or buf._type or buf.modify) then
- view:goto_buffer(1)
+ view:goto_buffer(_BUFFERS[1])
io.close_buffer()
end
end)
diff --git a/core/ui.lua b/core/ui.lua
index a529c4a4..a30b5e7a 100644
--- a/core/ui.lua
+++ b/core/ui.lua
@@ -59,10 +59,11 @@ local function _print(buffer_type, ...)
events.emit(events.FILE_OPENED)
elseif not ui.silent_print then
for i = 1, #_VIEWS do
- if _VIEWS[i].buffer._type == buffer_type then ui.goto_view(i) break end
+ local view = _VIEWS[i]
+ if view.buffer._type == buffer_type then ui.goto_view(view) break end
end
if view.buffer._type ~= buffer_type then
- view:goto_buffer(_BUFFERS[print_buffer])
+ view:goto_buffer(print_buffer)
end
end
local args, n = {...}, select('#', ...)
@@ -169,7 +170,7 @@ function ui.switch_buffer()
title = _L['Switch Buffers'], columns = columns, items = utf8_list,
width = CURSES and ui.size[1] - 2 or nil
}
- if button == 1 and i then view:goto_buffer(i) end
+ if button == 1 and i then view:goto_buffer(_BUFFERS[i]) end
end
---
@@ -201,16 +202,16 @@ function ui.goto_file(filename, split, preferred_view, sloppy)
local other_view = _VIEWS[preferred_view]
for i = 1, #_VIEWS do
if (_VIEWS[i].buffer.filename or ''):find(patt) then
- ui.goto_view(i)
+ ui.goto_view(_VIEWS[i])
return
end
- if not other_view and _VIEWS[i] ~= view then other_view = i end
+ if not other_view and _VIEWS[i] ~= view then other_view = _VIEWS[i] end
end
if other_view then ui.goto_view(other_view) end
end
for i = 1, #_BUFFERS do
if (_BUFFERS[i].filename or ''):find(patt) then
- view:goto_buffer(i)
+ view:goto_buffer(_BUFFERS[i])
return
end
end
@@ -234,15 +235,15 @@ function ui.set_theme(name, props)
_HOME..'/themes/?.lua')
if not name or not lfs.attributes(name) then return end
props = props or {}
- local current_buffer, current_view = _BUFFERS[buffer], _VIEWS[view]
+ local current_buffer, current_view = buffer, view
for i = 1, #_BUFFERS do
- view:goto_buffer(i)
+ view:goto_buffer(_BUFFERS[i])
dofile(name)
for prop, value in pairs(props) do buffer.property[prop] = value end
end
view:goto_buffer(current_buffer)
for i = 1, #_VIEWS do
- ui.goto_view(i)
+ ui.goto_view(_VIEWS[i])
dofile(name)
for prop, value in pairs(props) do buffer.property[prop] = value end
end
@@ -448,8 +449,9 @@ end)
events_connect(events.BUFFER_BEFORE_SWITCH,
function() view._prev_buffer = buffer end)
events_connect(events.BUFFER_DELETED, function()
- local i = _BUFFERS[view._prev_buffer]
- if i and _BUFFERS[buffer] ~= i then view:goto_buffer(i) end
+ if _BUFFERS[view._prev_buffer] and buffer ~= view._prev_buffer then
+ view:goto_buffer(view._prev_buffer)
+ end
end)
-- Enables and disables mouse mode in curses and focuses and resizes views based
@@ -494,7 +496,7 @@ if CURSES then
if event == buffer.MOUSE_PRESS then
local view = get_view(ui.get_split_table(), y - 1, x) -- title is at y = 1
if not view[1] and not view[2] then
- ui.goto_view(_VIEWS[view])
+ ui.goto_view(view)
resize = nil
else
resize = function(y2, x2)
@@ -558,13 +560,10 @@ local dialog
local get_split_table
---
--- Shifts to view number *n*.
--- *relative* indicates whether or not *n* is an index relative to the current
--- view's index in `_VIEWS` instead of an absolute index.
+-- Shifts to view *view* or the view *view* number of views relative to the
+-- current one.
-- Emits `VIEW_BEFORE_SWITCH` and `VIEW_AFTER_SWITCH` events.
--- @param n A relative or absolute view index in `_VIEWS`.
--- @param relative Optional flag that indicates whether *n* is a relative or
--- absolute index. The default value is `false`, for an absolute index.
+-- @param view A view or relative view number (typically 1 or -1).
-- @see _G._VIEWS
-- @see events.VIEW_BEFORE_SWITCH
-- @see events.VIEW_AFTER_SWITCH
diff --git a/doc/manual.md b/doc/manual.md
index 16ba4854..dd222a5d 100644
--- a/doc/manual.md
+++ b/doc/manual.md
@@ -1976,9 +1976,12 @@ FILTER |Renamed |[default\_filter][]
dir\_foreach() |Changed |[dir\_foreach()][] _(changed args)_
**ui** | |
SILENT\_PRINT |Renamed |[silent\_print][]
+goto\_view(n, relative) |Changed |[goto\_view][](view)
**ui.find** | |
FILTER |Renamed |[find\_in\_files\_filter][]
find\_in\_files(dir) |Changed |[find\_in\_files][](dir, filter)
+**view** | |
+goto\_buffer(n, relative) |Changed |[goto\_buffer][](buffer)
**textadept.editing** | |
AUTOPAIR |Replaced|[auto\_pairs][]
TYPEOVER\_CHARS |Replaced|[typeover\_chars][]
@@ -2008,14 +2011,16 @@ MAX\_RECENT\_FILES |Renamed |[max\_recent\_files][]
[COMPILE\_OUTPUT]: api.html#events.COMPILE_OUTPUT
[RUN\_OUTPUT]: api.html#events.RUN_OUTPUT
[BUILD\_OUTPUT]: api.html#events.BUILD_OUTPUT
+[quick\_open]: api.html#io.quick_open
+[quick\_open\_filters]: api.html#io.quick_open_filters
+[quick\_open\_max]: api.html#io.quick_open_max
[default\_filter]: api.html#lfs.default_filter
[dir\_foreach()]: api.html#lfs.dir_foreach
[silent\_print]: api.html#ui.silent_print
+[goto\_view]: api.html#ui.goto_view
[find\_in\_files\_filter]: api.html#ui.find.find_in_files_filter
[find\_in\_files]: api.html#ui.find.find_in_files
-[quick\_open]: api.html#io.quick_open
-[quick\_open\_filters]: api.html#io.quick_open_filters
-[quick\_open\_max]: api.html#io.quick_open_max
+[goto\_buffer]: api.html#view.goto_buffer
[auto\_pairs]: api.html#textadept.editing.auto_pairs
[typeover\_chars]: api.html#textadept.editing.typeover_chars
[auto\_indent]: api.html#textadept.editing.auto_indent
diff --git a/modules/textadept/bookmarks.lua b/modules/textadept/bookmarks.lua
index 9f2d1c10..c11f0fa5 100644
--- a/modules/textadept/bookmarks.lua
+++ b/modules/textadept/bookmarks.lua
@@ -62,7 +62,7 @@ function M.goto_mark(next)
while line >= 0 do
local mark = string.format('%s:%d: %s', basename, line + 1,
buffer:get_line(line):match('^[^\r\n]*'))
- utf8_list[#utf8_list + 1], buffers[#utf8_list + 1] = mark, i
+ utf8_list[#utf8_list + 1], buffers[#utf8_list + 1] = mark, buffer
line = buffer:marker_next(line + 1, 2^M.MARK_BOOKMARK)
end
end
diff --git a/modules/textadept/find.lua b/modules/textadept/find.lua
index cc73964e..6b4efeee 100644
--- a/modules/textadept/find.lua
+++ b/modules/textadept/find.lua
@@ -408,10 +408,10 @@ local function is_ff_buf(buf) return buf._type == _L['[Files Found Buffer]'] end
function M.goto_file_found(line, next)
local ff_view, ff_buf = nil, nil
for i = 1, #_VIEWS do
- if is_ff_buf(_VIEWS[i].buffer) then ff_view = i break end
+ if is_ff_buf(_VIEWS[i].buffer) then ff_view = _VIEWS[i] break end
end
for i = 1, #_BUFFERS do
- if is_ff_buf(_BUFFERS[i]) then ff_buf = i break end
+ if is_ff_buf(_BUFFERS[i]) then ff_buf = _BUFFERS[i] break end
end
if not ff_view and not ff_buf then return end
if ff_view then ui.goto_view(ff_view) else view:goto_buffer(ff_buf) end
diff --git a/modules/textadept/menu.lua b/modules/textadept/menu.lua
index e09c6ca4..6f3346d9 100644
--- a/modules/textadept/menu.lua
+++ b/modules/textadept/menu.lua
@@ -257,8 +257,8 @@ local default_menubar = {
},
{
title = _L['_Buffer'],
- {_L['_Next Buffer'], function() view:goto_buffer(1, true) end},
- {_L['_Previous Buffer'], function() view:goto_buffer(-1, true) end},
+ {_L['_Next Buffer'], function() view:goto_buffer(1) end},
+ {_L['_Previous Buffer'], function() view:goto_buffer(-1) end},
{_L['_Switch to Buffer...'], ui.switch_buffer},
SEPARATOR,
{
@@ -303,8 +303,8 @@ local default_menubar = {
},
{
title = _L['_View'],
- {_L['_Next View'], function() ui.goto_view(1, true) end},
- {_L['_Previous View'], function() ui.goto_view(-1, true) end},
+ {_L['_Next View'], function() ui.goto_view(1) end},
+ {_L['_Previous View'], function() ui.goto_view(-1) end},
SEPARATOR,
{_L['Split View _Horizontal'], function() view:split() end},
{_L['Split View _Vertical'], function() view:split(true) end},
diff --git a/modules/textadept/run.lua b/modules/textadept/run.lua
index f7119a23..db5c48af 100644
--- a/modules/textadept/run.lua
+++ b/modules/textadept/run.lua
@@ -349,10 +349,10 @@ function M.goto_error(line, next)
if not cwd then return end -- no previously run command
local msg_view, msg_buf = nil, nil
for i = 1, #_VIEWS do
- if is_msg_buf(_VIEWS[i].buffer) then msg_view = i break end
+ if is_msg_buf(_VIEWS[i].buffer) then msg_view = _VIEWS[i] break end
end
for i = 1, #_BUFFERS do
- if is_msg_buf(_BUFFERS[i]) then msg_buf = i break end
+ if is_msg_buf(_BUFFERS[i]) then msg_buf = _BUFFERS[i] break end
end
if not msg_view and not msg_buf then return end
if msg_view then ui.goto_view(msg_view) else view:goto_buffer(msg_buf) end
diff --git a/modules/textadept/session.lua b/modules/textadept/session.lua
index e497aad0..11515de0 100644
--- a/modules/textadept/session.lua
+++ b/modules/textadept/session.lua
@@ -45,7 +45,7 @@ function M.load(filename)
local f = io.open(filename, 'rb')
if not f or not io.close_all_buffers() then return false end
io.recent_files = {}
- local current_view, splits = 1, {[0] = {}}
+ local current_view, splits = view, {[0] = {}}
for line in f:lines() do
if line:find('^buffer:') then
local patt = '^buffer: (%d+) (%d+) (%d+) (.+)$'
@@ -76,7 +76,7 @@ function M.load(filename)
elseif line:find('^%s*split%d:') then
local level, num, type, size = line:match('^(%s*)split(%d): (%S+) (%d+)')
local view = splits[#level] and splits[#level][tonumber(num)] or view
- ui.goto_view(_VIEWS[view])
+ ui.goto_view(view)
splits[#level + 1] = {view:split(type == 'true')}
splits[#level + 1][1].size = tonumber(size) -- could be 1 or 2
elseif line:find('^%s*view%d:') then
@@ -84,9 +84,9 @@ function M.load(filename)
local view = splits[#level][tonumber(num)] or view
buf_idx = tonumber(buf_idx)
if buf_idx > #_BUFFERS then buf_idx = #_BUFFERS end
- view:goto_buffer(buf_idx)
+ view:goto_buffer(_BUFFERS[buf_idx])
elseif line:find('^current_view:') then
- current_view = tonumber(line:match('^current_view: (%d+)')) or 1
+ current_view = _VIEWS[tonumber(line:match('^current_view: (%d+)')) or 1]
elseif line:find('^recent:') then
io.recent_files[#io.recent_files + 1] = line:match('^recent: (.+)$')
end
diff --git a/src/textadept.c b/src/textadept.c
index 9f54d270..df929a3e 100644
--- a/src/textadept.c
+++ b/src/textadept.c
@@ -722,25 +722,36 @@ static void goto_view(Scintilla *view) {
if (!initing && !closing) lL_event(lua, "view_after_switch", -1);
}
+/**
+ * Checks whether the function argument narg is a Scintilla view and returns
+ * this view cast to a Scintilla.
+ * @param L The Lua state.
+ * @param arg The stack index of the Scintilla view.
+ * @return Scintilla view
+ */
+static Scintilla *lL_checkview(lua_State *L, int arg) {
+ luaL_getmetatable(L, "ta_view");
+ lua_getmetatable(L, arg);
+ luaL_argcheck(L, lua_rawequal(L, -1, -2), arg, "View expected");
+ lua_getfield(L, (arg > 0) ? arg : arg - 2, "widget_pointer");
+ Scintilla *view = (Scintilla *)lua_touserdata(L, -1);
+ lua_pop(L, 3); // widget_pointer, metatable, metatable
+ return view;
+}
+
/** `ui.goto_view()` Lua function. */
static int lui_goto_view(lua_State *L) {
- int n = luaL_checkinteger(L, 1), relative = lua_toboolean(L, 2);
- if (relative && n == 0) return 0;
- lua_getfield(L, LUA_REGISTRYINDEX, "ta_views");
- if (relative) {
+ if (lua_isnumber(L, 1)) {
+ lua_getfield(L, LUA_REGISTRYINDEX, "ta_views");
l_pushview(L, focused_view), lua_gettable(L, -2);
- n = lua_tointeger(L, -1) + n;
+ int n = lua_tointeger(L, -1) + lua_tointeger(L, 1);
if (n > (int)lua_rawlen(L, -2))
n = 1;
else if (n < 1)
n = lua_rawlen(L, -2);
- lua_rawgeti(L, -2, n);
- } else {
- luaL_argcheck(L, n > 0 && n <= (int)lua_rawlen(L, -1), 1,
- "no View exists at that index");
- lua_rawgeti(L, -1, n);
+ lua_rawgeti(L, -2, n), lua_replace(L, 1);
}
- Scintilla *view = l_toview(L, -1);
+ Scintilla *view = lL_checkview(L, 1);
focus_view(view);
#if GTK
// ui.dialog() interferes with focus so gtk_widget_grab_focus() does not
@@ -1937,32 +1948,22 @@ static int s_buttonpress(GtkWidget*_, GdkEventButton *event, void*__) {
}
#endif
-/**
- * Checks whether the function argument narg is a Scintilla view and returns
- * this view cast to a Scintilla.
- * @param L The Lua state.
- * @param arg The stack index of the Scintilla view.
- * @return Scintilla view
- */
-static Scintilla *lL_checkview(lua_State *L, int arg) {
- luaL_getmetatable(L, "ta_view");
- lua_getmetatable(L, arg);
- luaL_argcheck(L, lua_rawequal(L, -1, -2), arg, "View expected");
- lua_getfield(L, (arg > 0) ? arg : arg - 2, "widget_pointer");
- Scintilla *view = (Scintilla *)lua_touserdata(L, -1);
- lua_pop(L, 3); // widget_pointer, metatable, metatable
- return view;
-}
-
/** `view.goto_buffer()` Lua function. */
static int lview_goto_buffer(lua_State *L) {
Scintilla *view = lL_checkview(L, 1), *prev_view = focused_view;
- int n = luaL_checkinteger(L, 2), relative = lua_toboolean(L, 3);
+ luaL_argcheck(L, lua_istable(L, 2) || lua_isnumber(L, 2), 2,
+ "Buffer or relative index expected");
+ int relative = lua_isnumber(L, 2);
+ if (!relative) {
+ lua_getfield(L, LUA_REGISTRYINDEX, "ta_buffers");
+ lua_pushvalue(L, 2), lua_gettable(L, -2), lua_replace(L, 2);
+ luaL_argcheck(L, lua_isnumber(L, 2), 2, "Buffer expected");
+ }
// If the indexed view is not currently focused, temporarily focus it so
// `_G.buffer` in handlers is accurate.
if (view != focused_view) focus_view(view);
if (!initing) lL_event(L, "buffer_before_switch", -1);
- lL_gotodoc(L, view, n, relative);
+ lL_gotodoc(L, view, lua_tointeger(L, 2), relative);
if (!initing) lL_event(L, "buffer_after_switch", -1);
if (focused_view != prev_view) focus_view(prev_view);
return 0;