forked from evandroforks/SublimePackageDefault
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
set_unsaved_view_name.py
77 lines (56 loc) · 2.13 KB
/
set_unsaved_view_name.py
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
import string
import functools
import unicodedata
import sublime
import sublime_plugin
class SetUnsavedViewName(sublime_plugin.EventListener):
setting_name = False
dropped_chars = string.whitespace
pending = 0
def on_modified_async(self, view):
if view.file_name() or view.is_loading():
return
if self.setting_name:
return
self.pending += 1
sublime.set_timeout_async(functools.partial(self.update_title, view), 20)
def update_title(self, view):
self.pending -= 1
if self.pending != 0:
return
if view.settings().get('set_unsaved_view_name') is False:
return
cur_name = view.settings().get('auto_name')
view_name = view.name()
# Only set the name for plain text files
syntax = view.settings().get('syntax')
if syntax != 'Packages/Text/Plain text.tmLanguage':
if cur_name:
# Undo any previous name that was set
view.settings().erase('auto_name')
if cur_name == view_name:
view.set_name("")
return
# Name has been explicitly set, don't override it
if not cur_name and view_name:
return
# Name has been explicitly changed, don't override it
if cur_name and cur_name != view.name():
view.settings().erase('auto_name')
return
# Don't set the names on widgets, it'll just trigger spurious
# on_modified callbacks
if view.settings().get('is_widget'):
return
line = view.line(0)
if line.size() > 50:
line = sublime.Region(0, 50)
first_line = view.substr(line)
first_line = first_line.strip(self.dropped_chars)
# Filter non-printable characters. Without this the save dialog on
# windows fails to open.
first_line = ''.join(c for c in first_line if unicodedata.category(c)[0] != 'C')
self.setting_name = True
view.set_name(first_line)
self.setting_name = False
view.settings().set('auto_name', first_line)