Sublime Forum

Small script for man page

#1

Hi everyone,

I’ve done a little python script (duh!) for showing man page with Sublime 3. Although is not the best code, it is simple. The code I post now is working. If you have any suggestions, you’re welcome. And if you want to use the code for another just give me a little credit.

Link to gish code.
gist.github.com/Spationaute/5120104

Edit: Remove old code to avoid confusion.

0 Likes

#2

For download with keymap for linux (I think that script work on mac to, but not on windows).
stellaire.ca/outils/fast-man.tar.gz

Just do

 tar -C ~/.config/sublime-text-3/Packages/ -xvf fast-man.tar.gz; 

to install

To use, select a word to search in the man and press ctrl+. (ctrl+dot).
You can also just press ctrl+. (ctrl+dot) and type the search.
man have the instruction to search all man section and send you the first section found (not good, I must find an idea)

Edit: Update link and instructions.

0 Likes

#3

I don’t know if you thought about this, but have you considered using view.word() on the selection rather than taking the specific region? It may save you some time so you don’t have to actually select the word you want to man. If you want to use the selection if it is not empty, perhaps you can use something like this.

if view.sel()[0].empty(): word = view.word(view.sel()[0]) else: word = view.sel()[0]

Though depending on where the cursor is, that might return spaces (using view.word()), so you would have to do some additional checking. If you trim and find the string is empty, you could open an input panel and request a term to search for. Anyways, just throwing things out there that may or may not be useful to you.

0 Likes

#4

Thank you,
I’ll add trimming as you suggested and try implement the window asking which page is request if no selection is made.

edit:substracting a “the”

0 Likes

#5

I don’t understand why my panel is blank when I search from the input panel text and not when I search from the selected text…
Here the new code:


import sublime, sublime_plugin
from subprocess import *

class man(sublime_plugin.TextCommand):


	def run(self, edit):
		#Set some global to work whit in
		#other functions 
		global aedit
		global window
		global view

		#Get the windows and the view
		aedit=edit
		window = self.view.window()
		view = self.view

		#if the selection is empty, ask for the page
		if view.sel()[0].empty():
			window.show_input_panel("Man search :", "", self.input_ret,None,None)
		else:
			#if search is not empty
			word = view.substr(view.sel()[0])
			self.man_search(aedit,word)


	def input_ret(self,text):
		#if an text is set, search it
		if text:
			self.man_search(aedit,text)

	def man_search(self,edit,to_search):
		text = str(to_search)
		#Open the man panel
		man_panel = window.get_output_panel("man")

		#Wait that the panel is ready
		while man_panel.is_loading():
			pass

		#print the search in console
		print("searching in man: {}".format(text))

		#Command to send
		man_command="man","-P","cat","-7","-a", text]

		#Open the pipe and redirect stdout to the pipe
		pipe = Popen(man_command,stdout=PIPE,stderr=STDOUT)
		(in_pipe,out_pipe) = pipe.communicate()

		#Show the panel
		window.run_command("show_panel",{"panel":"output.man"})

		#insert text in the man panel
		man_panel.insert(edit, 0, in_pipe.decode("utf-8"))
0 Likes

#6

When I try to bring up a man page from the input panel, I don’t get anything. It is giving me an error on the following line

      print("searching in man: {}".format(text))

I believe you need to specify an index. Is that the same behavior you were seeing?

Side note, have you consider putting this on a gist or something similar. Would make it easier to share and for me to view. If not, that’s fine too.

0 Likes

#7

I’ve published a gish at gist.github.com/Spationaute/5120104
It’s the first time a try this site. :smile:

0 Likes

#8

Mine, don’t show any error. It print in the console (and I try print the man, both worked) But it doesn’t print in the panel.

0 Likes

#9

I think I understand why. When using the input, the are a new edit context created…
I can write in the output whit run_command insert, but it’s really slow.

So,for now, the best, is to print it in console…

0 Likes

#10

Hmm, I just realized (through your gist description), I was testing in ST2 not ST3. Yes it’s a change in the API. You might want to take a look at sublimetext.com/docs/3/porting_guide.html. As a work around you can do something like

class SimpleInsertCommand(sublime_plugin.TextCommand):
    def run(self, edit, content):
        self.view.insert(edit, 0, content)

Then, at gist.github.com/Spationaute/512 … man-py-L56 do something like

man_panel.run_command("simple_insert", {"content": in_pipe.decode("utf-8")})

Though I don’t know how good/bad the performance would be for that. It may in fact be the same, but it might be worth a try.

0 Likes

#11

That doesn’t work at all, sorry. I think it’s not really my code that doesn’t work. I think the “edit” object just change to the input_box and I lose control of it.

0 Likes

#12

Odd it worked for me. Here is what I tested with.

[code]import sublime, sublime_plugin
from subprocess import *

class man(sublime_plugin.TextCommand):
def run(self, edit):
#Set some global to work whit in
#other functions
global aedit
global window
global view

    #Get the windows and the view
    aedit=edit
    window = self.view.window()
    view = self.view

    #if the selection is empty, ask for the page
    if view.sel()[0].empty():
        
        window.show_input_panel("Man search :", "", self.input_ret,None,None)
    else:
        #if search is not empty
        word = view.substr(view.sel()[0])
        self.man_search(aedit,word)

def input_ret(self,text):
    #if an text is set, search it
    if text:
        self.man_search(aedit,text)

def man_search(self,edit,to_search):
    text = "{}".format(to_search)
    #Open the man panel
    man_panel = window.get_output_panel("man")
    print(man_panel)
    #Wait that the panel is ready
    while man_panel.is_loading():
        pass

    #print the search in console
    print("searching in man: {}".format(text))

    #Command to send
    man_command="man","-P","cat","-7","-a", text]

    #Open the pipe and redirect stdout to the pipe
    pipe = Popen(man_command,stdout=PIPE,stderr=STDOUT)
    (in_pipe,out_pipe) = pipe.communicate()

    #insert text in the man panel
    man_panel.run_command("simple_insert", {"content": in_pipe.decode("utf-8")})

    #Show the panel
    window.run_command("show_panel",{"panel":"output.man"})

class SimpleInsertCommand(sublime_plugin.TextCommand):
def run(self, edit, content):
print(self.view)
self.view.insert(edit, 0, content)

[/code]

0 Likes

#13

Hey! Yes, it’s working! It’s seem that I was missing the lass class. Thank you :smile:
I’m happy whit my new script!

0 Likes

#14

Glad to hear. Hope you removed those prints I forgot to take out. :smiley:

0 Likes