aboutsummaryrefslogtreecommitdiff
path: root/config.dot/luakit.link/sessions.lua
blob: 7daa6eddb2e7ce4245854f2a7f5121ea49d732da (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
--
-- Session management for luakit
-- By Silvio Rhatto <rhatto@riseup.net>
-- Using https://luakit.github.io/docs/modules/session.html
-- Inspired by https://github.com/IsoLinearCHiP/luakit-sessman
--

-- Requirements
local lfs     = lfs
local info    = info
local window  = require "window"
local modes   = require "modes"
local session = require "session"

-- Base folder
function basedir() 
    return os.getenv("XDG_DATA_HOME") or os.getenv("HOME") .. "/.local/share"
end

-- The directory where sessions are stored
local path = basedir() .. "/luakit/sessions/"
if not lfs.attributes(path) then lfs.mkdir(path) end

-- Commands
modes.add_cmds({
  { ":sessionload, :sload, :loadsession, :openssession", "Load a session", function (w, a)
    local file           = path .. a.arg
    session.session_file = file

    session.restore(false)
    w:close_win()
  end},
  { ":sessionsave, :ssave, :savesession", "Save a session", function (w, a)
    local file           = path .. a.arg
    session.session_file = file

    session.save(file)
    msg.info("Saved session at " .. file)
  end},
  { ":sessionremove, :srm, :removesession", "Remove a session", function (w, a)
    local file           = path .. a.arg
    session.session_file = nil

    local deleted = session.load(true, file)
  end},
  { ":sessions", "Session manager", function (w)
    local files = ''

    for file in lfs.dir(path) do
      if not (file == "." or file == "..") then
        files = files .. ' ' .. file
      end
    end

    w:notify("Available sessions:" .. files)
  end },
})

-- Bindings
modes.add_binds("normal", {
  -- Quit and save session binding
  { "^q$", "Quit and save current session", function (w)
    session.save()

    for _, ww in pairs(window.bywidget) do
      ww:close_win(true)
    end
  end},
})