forked from rgieseke/ta-common
-
Notifications
You must be signed in to change notification settings - Fork 0
/
display_filename.lua
95 lines (85 loc) · 2.98 KB
/
display_filename.lua
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
-- Shorten display filenames in buffer title and switch buffer dialog.
-- On Windows
-- C:\Documents and Settings\username\Desktop\...
-- is replaced with
-- Desktop\...,
-- on Max OS X and Linux
-- /home/username/..
-- or
-- /Users/username/...
-- with
-- ~/...
--
-- Modified from Textadept's
-- [core.gui](http://code.google.com/p/textadept/source/browse/core/gui.lua) and
-- [snapopen](http://code.google.com/p/textadept/source/browse/modules/textadept/snapopen.lua)
-- module.
module('_m.common.display_filename', package.seeall)
local L = _G.locale.localize
-- ## Fields
-- Read environment variable.
if WIN32 then
pattern = os.getenv('USERPROFILE')..'\\'
replacement = ''
else
pattern = '^'..os.getenv('HOME')
replacement = '~'
end
-- ## Commands
-- Sets the title of the Textadept window to the buffer's filename.
-- Parameter:<br>
-- _buffer_: The currently focused buffer.
local function set_title(buffer)
local buffer = buffer
local filename = buffer.filename or buffer._type or L('Untitled')
local dirty = buffer.dirty and '*' or '-'
gui.title = string.format('%s %s Textadept (%s)', filename:match('[^/\\]+$'),
dirty, filename:gsub(pattern, replacement))
end
-- Disconnect events that use `set_title` from `core/gui.lua`
-- and reconnect with new `set_title` function
local events = _G.events
events.disconnect('save_point_reached', 1)
events.connect('save_point_reached',
function() -- changes Textadept title to show 'clean' buffer
buffer.dirty = false
set_title(buffer)
end)
events.disconnect('save_point_left', 1)
events.connect('save_point_left',
function() -- changes Textadept title to show 'dirty' buffer
buffer.dirty = true
set_title(buffer)
end)
events.disconnect('buffer_after_switch', 3)
events.connect('buffer_after_switch',
function() -- updates titlebar and statusbar
set_title(buffer)
events.emit('update_ui')
end)
events.disconnect('view_after_switch', 2)
events.connect('view_after_switch',
function() -- updates titlebar and statusbar
set_title(buffer)
events.emit('update_ui')
end, 2)
-- Displays a dialog with a list of buffers to switch to and switches to the
-- selected one, if any.
function switch_buffer()
local items = {}
for _, buffer in ipairs(_BUFFERS) do
local filename = buffer.filename or buffer._type or L('Untitled')
local dirty = buffer.dirty and '*' or ''
items[#items + 1] = dirty..filename:match('[^/\\]+$')
items[#items + 1] = filename:gsub(pattern, replacement)
end
local response = gui.dialog('filteredlist',
'--title', L('Switch Buffers'),
'--button1', 'gtk-ok',
'--button2', 'gtk-cancel',
'--no-newline',
'--columns', 'Name', 'File',
'--items', items)
local ok, i = response:match('(%-?%d+)\n(%d+)$')
if ok == '1' then view:goto_buffer(tonumber(i) + 1, true) end
end