-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathPastebin.py
102 lines (84 loc) · 3.24 KB
/
Pastebin.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
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
# -*- coding: utf-8 -*-
"""
"""
import sublime_plugin
import sublime
from src import api
api.load()
### THE COMMANDS ###
class PasterCommand(sublime_plugin.TextCommand):
def __init__(self, view):
super(PasterCommand, self).__init__(view)
# self.window = self.view.window()
self.window = sublime.active_window()
self.settings = self.view.settings().get('pastebin')
self.Paster = self.get_pastebin_implementation()
def get_pastebin_implementation(self):
mode = self.settings.get('mode')
for impl in api.PastebinImplementation.plugins:
if mode == impl._name:
return impl
raise ValueError('Unknown pastebin mode `%s`' % mode)
def status(self, msg, clipboard=None):
"""Display message and optionally set clipboard contents"""
if clipboard:
sublime.set_clipboard(clipboard)
sublime.status_message(msg)
class PastebinPostCommand(PasterCommand):
""""""
def run(self, edit):
try:
self.paster = self.Paster(self.view)
self.content = self.selected_content()
if not self.content:
raise ValueError('No content to post')
if not self.settings.get('prompt_on_post', True):
self.go()
else:
msg = 'post snippet to %s? [y/n]' % self.paster.url()
self.window.show_input_panel(
msg, 'y', self.go, None, None)
except Exception, exc:
self.status(str(exc))
def go(self, confirm_choice='y'):
if confirm_choice.lower() == 'y':
paste_url = self.paster.upload(self.content)
if self.settings.get('copy_to_clipboard', True):
self.status("%s. URL has been copied to the clipboard." % paste_url, paste_url)
else:
self.status(paste_url)
else:
self.status('Cancelled')
def selected_content(self):
"""
Return the selected region or the entire buffer contents
if no region is selected.
"""
content = u'\n'.join([self.view.substr(region) for region in self.view.sel()])
if not content:
content = self.view.substr(sublime.Region(0, self.view.size()))
return content
class PastebinFetchCommand(PasterCommand):
""""""
def run(self, edit):
self.edit = edit
self.window.show_input_panel('Paste id', '',
self.on_paste_id, None, None)
def on_paste_id(self, paste_id):
try:
paste_id = paste_id.strip()
paster = self.Paster(self.view)
data, lang, url = paster.fetch(paste_id)
## If view is empty set the syntax
## TODO: How to derive the syntax_file?
# if not self.view.size() and lang:
# self.view.set_syntax_file()
## Insert
for region in self.view.sel():
self.view.erase(self.edit, region)
self.view.insert(self.edit, region.begin(), data)
self.status("Fetched from %s" % url)
except api.TransportError, exc:
self.status(str(exc))
except Exception, exc:
self.status("Unable to fetch paste (%s)" % exc)