aboutsummaryrefslogtreecommitdiff
path: root/runtime/plugin/zip.lua
blob: dc3f09c9c961da0d2dae9b3f60f4f1616327e34c (plain) (blame)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
if vim.g.loaded_nvim_zip_plugin ~= nil or vim.fn.exists('#zip') == 1 then
  return
end
vim.g.loaded_nvim_zip_plugin = true

local api = vim.api

-- Extension aliases inherited from the legacy plugin:
-- https://github.com/neovim/neovim/blob/7ba955fe079d4aa2554fea8e7235651fafd40efb/runtime/plugin/zipPlugin.vim#L34-L35
---@type string[]
local extensions = {
  'aar',
  'apk',
  'cbz',
  'celzip',
  'crtx',
  'docm',
  'docx',
  'dotm',
  'dotx',
  'ear',
  'epub',
  'gcsx',
  'glox',
  'gqsx',
  'ja',
  'jar',
  'kmz',
  'odb',
  'odc',
  'odf',
  'odg',
  'odi',
  'odm',
  'odp',
  'ods',
  'odt',
  'otc',
  'otf',
  'otg',
  'oth',
  'oti',
  'otp',
  'ots',
  'ott',
  'oxt',
  'pkpass',
  'potm',
  'potx',
  'ppam',
  'ppsm',
  'ppsx',
  'pptm',
  'pptx',
  'sldx',
  'thmx',
  'vdw',
  'war',
  'whl',
  'wsz',
  'xap',
  'xlam',
  'xlsb',
  'xlsm',
  'xlsx',
  'xltm',
  'xltx',
  'xpi',
  'zip',
}
---@type string[]
local archive_patterns = {}
for _, extension in ipairs(extensions) do
  archive_patterns[#archive_patterns + 1] = ('*.%s'):format(extension)
end
vim.keymap.set('n', '<Plug>(nvim-zip-extract)', function()
  require('nvim.zip')._extract()
end, { silent = true, desc = 'Extract zip archive entry' })

local group = api.nvim_create_augroup('nvim.zip', { clear = true })

---@return boolean
local function legacy_loaded()
  return vim.fn.exists('#zip') == 1
end

api.nvim_create_autocmd('BufReadCmd', {
  group = group,
  pattern = 'zip://*',
  desc = 'Read zip archive entry',
  callback = function(ev)
    if legacy_loaded() then
      return true
    end
    require('nvim.zip').read(ev.buf, ev.match)
  end,
})

api.nvim_create_autocmd('BufReadCmd', {
  group = group,
  pattern = archive_patterns,
  desc = 'Browse zip archives',
  callback = function(ev)
    if legacy_loaded() then
      return true
    end
    if ev.match:match('^%a[%w+.-]*://') then
      return
    end
    require('nvim.zip').browse(ev.buf, ev.match)
  end,
})