aboutsummaryrefslogtreecommitdiffhomepage
path: root/core/ext/pm.lua
blob: cec2fd5b6af43c15e457dc0519d1f60c69e702d5 (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
-- Copyright 2007-2010 Mitchell mitchell<att>caladbolg.net. See LICENSE.

local pm = textadept.pm

local current_browser = nil

-- For restoring browser cursors
local last_browser_text = nil
local browser_cursors = {}

textadept.events.add_handler('pm_contents_request',
  function(full_path, expanding)
    for _, browser in pairs(pm.browsers) do
      if browser.matches(full_path[1]) then
        current_browser = browser
        if last_browser_text and last_browser_text ~= pm.entry_text then
          -- Switching browsers, save the current one's cursor.
          -- Don't reset last_browser_text here though, we still need to detect
          -- the switch when the 'pm_view_filled' event is called so as to restore
          -- the cursor to the new browser.
          browser_cursors[last_browser_text] = pm.cursor
        end
        pm.fill(browser.get_contents_for(full_path, expanding), expanding)
        textadept.events.handle('pm_view_filled')
      end
    end
  end)

textadept.events.add_handler('pm_item_selected',
  function(selected_item) current_browser.perform_action(selected_item) end)

textadept.events.add_handler('pm_context_menu_request',
  function(selected_item, event)
    local menu = current_browser.get_context_menu(selected_item)
    if menu then pm.show_context_menu(menu, event) end
  end)

textadept.events.add_handler('pm_menu_clicked',
  function(menu_id, selected_item)
    current_browser.perform_menu_action(menu_id, selected_item)
  end)

-- LuaDoc is in core/.browser.lua.
function pm.toggle_visible()
  if pm.width > 0 then
    pm.prev_width = pm.width
    pm.width = 0
  else
    pm.width = pm.prev_width or 150
  end
end

textadept.events.add_handler('pm_view_filled',
  function() -- try to restore previous browser cursor
    if last_browser_text ~= pm.entry_text then
      last_browser_text = pm.entry_text
      local previous_cursor = browser_cursors[pm.entry_text]
      if previous_cursor then pm.cursor = previous_cursor end
    end
  end)