forked from apophys/SublimeManpage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSublimeManpage.py
232 lines (181 loc) · 8.08 KB
/
SublimeManpage.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# -*- coding: utf-8 –*–
import locale
import logging
import re
import threading
import webbrowser
from subprocess import Popen, PIPE
from urllib.request import urlopen, HTTPError
import sublime
import sublime_plugin
def manpage_api_factory(window, word):
"""Generate manpage api object"""
if sublime.platform() in ["osx", "linux"]:
return ManpageApiCall(window, word)
else:
return ManpageWebCall(window, word)
class ExactMatchSettings(sublime_plugin.WindowCommand):
""" This writes changes to User Settings """
def run(self, mode=False):
settings = sublime.load_settings("SublimeManpage.sublime-settings")
if type(mode) is bool:
settings.set("exact_match", mode)
sublime.save_settings("SublimeManpage.sublime-settings")
else:
sublime.status_message("Unexpected parameter type.")
class ManpageCommand(sublime_plugin.WindowCommand):
def run(self):
self.window.show_input_panel("Type function name or command:", "",
self.on_done, None, None)
def on_done(self, line):
command = manpage_api_factory(self.window, line)
command.start()
class FindManpageFromSelectionCommand(sublime_plugin.WindowCommand):
def run(self):
currentView = self.window.active_view()
wordEnd = currentView.sel()[0].end()
if currentView.sel()[0].empty():
word = currentView.substr(currentView.word(wordEnd)).lower()
else:
word = currentView.substr(currentView.sel()[0]).lower()
if word is None or len(word) <= 1:
sublime.status_message('No word selected')
return
sublime.status_message("Selected word is: " + word)
command = manpage_api_factory(self.window, word)
command.start()
class ManpageApiCall(threading.Thread):
def __init__(self, window, func):
WHATIS_RE = "^(?P<func>[\w-]+)\s*\((?P<sect>[^\)+])\)\s+-\s+(?P<desc>.*)$"
self.window = window
self.req_function = func
self.function_list = []
self.settings = sublime.load_settings("SublimeManpage.sublime-settings")
self.whatis_re = re.compile(WHATIS_RE)
threading.Thread.__init__(self)
def run(self):
def show_panel():
if not self.req_function:
return
func_list = self._get_function_list()
if not func_list:
return
if len(func_list) is not 1:
self.window.show_quick_panel(func_list, self.on_done)
else:
self.on_done(0)
sublime.set_timeout(show_panel, 10)
def on_done(self, picked):
if picked == -1:
return
logging.debug("[sublime_manpage:on_done] (%d, %d)"
% (len(self.function_list), picked))
manpage, title = self._call_man(self.function_list[picked])
self._render_manpage(manpage, title)
def _get_function_list(self):
def split_whatis(lines):
splited = []
for line in lines:
if ',' in line:
func_lst, desc = line.split('-', 1)
for f in func_lst.split(','):
splited.append("%s - %s" % (f.strip(), desc))
else:
splited.append(line)
return splited
whatis = Popen(["whatis", self.req_function], stdin=None,
stdout=PIPE, stderr=PIPE)
whatis_output = str(whatis.communicate()[0], encoding='utf8')
function_descriptions = whatis_output.rstrip().split('\n')
function_descriptions = split_whatis(function_descriptions)
sections = self.settings.get("sections", ["2", "3"])
exact_match = self.settings.get("exact_match", False)
if len(function_descriptions) is 1 and not function_descriptions[0]:
sublime.status_message("Manpage: Function '%s' not found"
% self.req_function)
return []
func_found_global = False
for item in function_descriptions:
match = re.search(self.whatis_re, item)
if match:
dct = match.groupdict()
func_found = dct["func"].find(self.req_function) != -1
func_found_global = func_found_global or func_found
if dct["sect"] in sections and func_found:
func_desc = "(%s) - %s" % (dct["sect"], dct["desc"],)
entry = [dct["func"], func_desc]
logging.debug("[sublime_manpage] Exact match: [%s]; \
searched function: [%s]; \
parsed function: [%s]"
% (exact_match, self.req_function, dct["func"]))
if exact_match and dct["func"] == self.req_function:
# Returning *first* exact match.
logging.debug("[sublime_manpage] Match for [%s]"
% dct["func"])
self.function_list = [entry]
return self.function_list
else:
self.function_list.append(entry)
elif len(function_descriptions) is 1:
# temporary hack: better detect in some more clever way
sublime.status_message("Function %s not found in whatis database." % self.req_function)
else:
if func_found_global and not self.function_list:
sublime.status_message("Manpage: function %s found but its section is not in configuration file."
% self.req_function)
return self.function_list
def _call_man(self, function):
""" function = ['func', '(sect) - desc']"""
# screw it!
section = function[1].split('-')[0].strip(" ()")
logging.debug("[sublime_manpage] Calling man for [%s] in section [%s]" %
(function[0], section))
cmd_man = ["man", section, function[0]]
cmd_col = ["col", "-b"]
man = Popen(cmd_man, stdin=None, stdout=PIPE, stderr=PIPE)
col = Popen(cmd_col, stdin=man.stdout, stdout=PIPE, stderr=PIPE)
result = col.communicate()[0]
return (result, {"func": function[0], "sect": section})
def _render_manpage(self, manpage, desc):
view = self.window.new_file()
view.set_name("%s (%s)" % (desc["func"], desc["sect"]))
view.set_scratch(True)
if sublime.platform() == "linux":
loc = locale.getdefaultlocale()[-1]
data = manpage.decode(loc)
else:
# python bundled with Sublime Text raises ValueError for UTF-8
# on OS X
data = str(manpage, encoding='utf8')
view.run_command('append', {'characters':data})
view.set_read_only(True)
class ManpageWebCall(threading.Thread):
URL = "http://www.linuxmanpages.com/man%(section)s/%(function)s.%(section)s.php"
def __init__(self, window, func):
self.window = window
self.function = func
self.settings = sublime.load_settings("SublimeManpage.sublime-settings")
threading.Thread.__init__(self)
def run(self):
def get_manpage_url(url_data):
for item in url_data:
try:
url = ManpageWebCall.URL % item
f = urlopen(url)
f.close()
if f.getcode() is 200:
return url
else:
pass # potentially ignoring redirects
except HTTPError:
pass
else:
return None
sections = self.settings.get("sections", ["2", "3"])
url_data = ({ "function" : self.function, "section" : s} for s in sections)
url = get_manpage_url(url_data)
if url:
webbrowser.open(url)
else:
sublime.error_message("Manpage for '%s' not found in sections [%s]."
% (self.function, ', '.join(sections)))