aboutsummaryrefslogtreecommitdiff
path: root/plugins/restoretabs.lua
blob: 4c333040da09ded67c66cde40b95f0a95d518df5 (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
-- mod-version:3
-- Not perfect, because we can't actually figure out when something closes, but should be good enough, so long as we check the list of open views.
-- Maybe find a better way to get at "Node"?
local core = require "core"
local RootView = require "core.rootview"
local command = require "core.command"
local keymap = require "core.keymap"

local update = RootView.update
local initialized_tab_system = false

local tab_history = { }
local history_size = 10

RootView.update = function(self)
  update(self)
  if not initialized_tab_system then
    local Node = getmetatable(self.root_node)
    local old_close = Node.close_view

    Node.close_view = function(self, root, view)
      if view.doc and view.doc.abs_filename then
        local closing_filename = view.doc.abs_filename
        for i,filename in ipairs(tab_history) do
          if filename == closing_filename then
            table.remove(tab_history, i)
            break
          end
        end
        table.insert(tab_history, closing_filename)
        if #tab_history > history_size then
          table.remove(tab_history, 1)
        end
      end
      old_close(self, root, view)
    end

    initialized_tab_system = true
  end
end


command.add(nil, {
  ["restore-tabs:restore-tab"] = function()
    if #tab_history > 0 then
      local file = tab_history[#tab_history]
      core.root_view:open_doc(core.open_doc(file))
      table.remove(tab_history)
    end
  end
})

keymap.add {
  ["ctrl+shift+t"] = "restore-tabs:restore-tab"
}