Scite Buffer Switch

lua-users home
wiki

When navigating between many open buffers, it's often desirable to move back to the most recently accessed buffers. This is the default mode of editors such as Visual Studio, for instance. (There was apparently a buffers.zorder.switching=1 mode but it was unstable). This script presents a drop-down list of opened buffers in recently-used order. The method is to watch the OnOpen and OnSwitchFile events.

This will bind Ctrl+K to the function do_buffer_list.

command.name.4.*=Switch Buffers
command.4.*=do_buffer_list
command.subsystem.4.*=3
command.mode.4.*=savebefore:no
command.shortcut.4.*=Ctrl+K

Put this in your startup script, or set ext.lua.startup.script.

--switch_buffers.lua
--drops down a list of buffers, in recently-used order

local buffers = {}
local append = table.insert

local function buffer_switch(f)
--- swop the new current buffer with the last one!
  local idx  
  for i,file in ipairs(buffers) do
     if file == f then  idx = i; break end
  end
  if idx then 
     table.remove(buffers,idx)
     table.insert(buffers,1,f)
  else append(buffers,f)
  end
end

function OnOpen(f)
  buffer_switch(f)
end

function OnSwitchFile(f)
  buffer_switch(f)
end

function do_buffer_list()
  local s = ''
  local sep = ';'
  local n = table.getn(buffers)
  for i = 2,n-1 do
      s = s..buffers[i]..sep
  end
  s = s..buffers[n]
  _UserListSelection = fn
  editor.AutoCSeparator = string.byte(sep)
  editor:UserListShow(12,s)
  editor.AutoCSeparator = string.byte(' ')
end

function OnUserListSelection(t,str)
  if t == 12 then 
     scite.Open(str)
     return true
  else
     return false
  end
end


RecentChanges · preferences
edit · history
Last edited November 15, 2012 7:27 am GMT (diff)