Re: [gedit-list] Snapopen plugin updated with filebrowser support



Hi Warlock,

Cool! I think I will post your findings on my page, to help 2.14 users
use my plugin. Now that you mention it, I saw a deprecation warning when
running the eddt about get_encoding stuff.., avoiding this in my plugin
I probably stepped into 2.16 land.

>From reading your link, the 'get_tab_from_uri' you should be homemade by
something like this:

-------------------

#gedit < 2.16 version
def get_tab_from_uri(self, window, uri):
	docs = window.get_documents()
	for doc in docs:
		if doc.get_uri() == uri:
			return gedit.tab_get_from_document(doc)
	return None

--------------------

I have attached a 2.14 version of __init__.py that you can use (with the
above two changes (commented). I have not tried it on 2.14, but I have
succesfully tested homemade reimpl of get_tab_from_uri on 2.16


Looking forward to hearing if it helps you.

Regards,
Mads




On Fri, 2006-12-01 at 13:37 -0800, Warlock-G wrote:
> Hi,
> 
> for the first error I fix it with gedit_encoding_get_current(), and run ok
> 
> 
> for the second error, I found this 
> 
> http://www.archivesat.com/discussion_about_usage_and_developement_of_gedit/thread999490.htm
> get_tab_from_uri 
> 
> This one has been binded after the 2.15.3 release
> 
> 
> bye
> 
> 
> Warlock-G wrote:
> > 
> > Hi,
> > 
> > I did do what you say, and gedit into console send that messages
> > 
> > sguaman sg-ubuntu:~$ gedit
> > Traceback (most recent call last):
> >   File "/home/sguaman/.gnome2/gedit/plugins/snapopen/__init__.py", line
> > 213, in activate
> >     self._set_instance( window, SnapOpenPluginInstance( self, window ) )
> >   File "/home/sguaman/.gnome2/gedit/plugins/snapopen/__init__.py", line
> > 27, in __init__
> >     self._encoding = gedit.encoding_get_current()
> > AttributeError: 'module' object has no attribute 'encoding_get_current'
> > Traceback (most recent call last):
> >   File "/home/sguaman/.gnome2/gedit/plugins/snapopen/__init__.py", line
> > 220, in update_ui
> >     self._get_instance( window ).update_ui()
> > AttributeError: 'NoneType' object has no attribute 'update_ui'
> > 
> > 
> > I commented that line and the plugin run, but when I press Ctrl+Alt+o and
> > open the dialog and choose open a file happen this
> > 
> > Traceback (most recent call last):
> >   File "/home/sguaman/.gnome2/gedit/plugins/snapopen/__init__.py", line
> > 97, in on_select_from_list
> >     self.open_selected_item(event)
> >   File "/home/sguaman/.gnome2/gedit/plugins/snapopen/__init__.py", line
> > 158, in open_selected_item
> >     self._open_file ( selected_file )
> >   File "/home/sguaman/.gnome2/gedit/plugins/snapopen/__init__.py", line
> > 164, in _open_file
> >     tab = self._window.get_tab_from_uri(uri)
> > AttributeError: 'gedit.Window' object has no attribute 'get_tab_from_uri'
> > Traceback (most recent call last):
> >   File "/home/sguaman/.gnome2/gedit/plugins/snapopen/__init__.py", line
> > 93, in on_list_mouse
> >     self.open_selected_item( event )
> >   File "/home/sguaman/.gnome2/gedit/plugins/snapopen/__init__.py", line
> > 158, in open_selected_item
> >     self._open_file ( selected_file )
> >   File "/home/sguaman/.gnome2/gedit/plugins/snapopen/__init__.py", line
> > 164, in _open_file
> >     tab = self._window.get_tab_from_uri(uri)
> > AttributeError: 'gedit.Window' object has no attribute 'get_tab_from_uri'
> > Traceback (most recent call last):
> >   File "/home/sguaman/.gnome2/gedit/plugins/snapopen/__init__.py", line
> > 158, in open_selected_item
> >     self._open_file ( selected_file )
> >   File "/home/sguaman/.gnome2/gedit/plugins/snapopen/__init__.py", line
> > 164, in _open_file
> >     tab = self._window.get_tab_from_uri(uri)
> > AttributeError: 'gedit.Window' object has no attribute 'get_tab_from_uri'
> > 
> > 
> > I hoope this help you.
> > 
> > thank's
> > 
> > 
> > Mads Buus Jensen wrote:
> >> 
> >> Hi,
> >> 
> >> On Tue, 2006-11-28 at 04:28 -0800, Warlock-G wrote:
> >>> well, but this not work for me, I'm a Ubuntu Dapper 6.06 with gedit
> >>> 2.14,
> >>> this plug-ins need an early version? or what?
> >> No It has no dependency on earlier versions. In fact, 
> >> since I changed the dir structure a bit, earlier versions should be
> >> removed first. It should work fine on 2.14 (I use edgy/gedit 2.16), but
> >> I do not think I use any 2.16 specific stuff. 
> >> 
> >>> 
> >>> And I download this file 3 times and nothing change, Ctrl+Alt+o not
> >>> work. 
> >>> Please can you help me? need more information?
> >> Have you followed the installation instructions?
> >> Quick help/troubleshooting:
> >> 
> >> 1. Untar the download (preserving paths)
> >> into /home/<you>/.gnome2/gedit/plugins
> >> 
> >> 2. start gedit from a console, choose 'Edit - Preferences - Plugins' and
> >> enable the 'Snapopen' plugin.
> >> 
> >> 
> >> 3. watch the console out. If snapopen plugin fails you should see some
> >> output here.
> >> 
> >> 
> >> Hmm, come to think of it you might need to apt-get some python-dev if
> >> my .py files font compile (I havent bundled *.pyc files). Check for .pyc
> >> files in /home/<you>/.gnome2/gedit/plugins/snapopen/ after you've run
> >> gedit once with the plugin enabled.
> >> 
> >> 
> >> good luck, submit me your findings.
> >> 
> >> 
> >> -Mads
> >> 
> >> ______________________________________________
> >> gedit-list mailing list
> >> gedit-list gnome org
> >> http://mail.gnome.org/mailman/listinfo/gedit-list
> >> 
> >> 
> > 
> > 
> 
import gedit, gtk, gtk.glade
import gconf
import gnomevfs

import pygtk
pygtk.require('2.0')
import os, os.path, gobject
#from xml.dom import minidom
max_result = 50

ui_str="""<ui>
<menubar name="MenuBar">
	<menu name="SnapOpenMenu" action="SnapOpenMenuAction">
		<placeholder name="SnapOpen Options">
			<menuitem name="SnapOpen" action="SnapOpenAction"/>
		</placeholder>
	</menu>
</menubar>
</ui>
"""

# essential interface
class SnapOpenPluginInstance:
	def __init__( self, plugin, window ):
		self._window = window
		self._plugin = plugin
		self._encoding = gedit.gedit_encoding_get_current() 	#gedit < 2.16 
		#self._encoding = gedit.encoding_get_current() 		    #gedit > 2.16 
		self._rootdir = "file://" + os.getcwd()
		self._show_hidden = False
		self._liststore = None;
		self._init_glade()
		self._insert_menu()

	def deactivate( self ):
		self._remove_menu()
		self._action_group = None
		self._window = None
		self._plugin = None
		self._liststore = None;

	def update_ui( self ):
		return

	# MENU STUFF
	def _insert_menu( self ):
		manager = self._window.get_ui_manager()
		self._action_group = gtk.ActionGroup( "SnapOpenPluginActions" )
		snapopen_menu_action = gtk.Action( name="SnapOpenMenuAction", label="Snap", tooltip="Snap tools", stock_id=None )
		self._action_group.add_action( snapopen_menu_action )
		snapopen_action = gtk.Action( name="SnapOpenAction", label="Open...\t", tooltip="Open a file", stock_id=gtk.STOCK_OPEN )
		snapopen_action.connect( "activate", lambda a: self.on_snapopen_action() )
		self._action_group.add_action_with_accel( snapopen_action, "<Ctrl><Alt>o" )
		manager.insert_action_group( self._action_group, 0 )
		self._ui_id = manager.new_merge_id()
		manager.add_ui_from_string( ui_str )
		manager.ensure_update()

	def _remove_menu( self ):
		manager = self._window.get_ui_manager()
		manager.remove_ui( self._ui_id )
		manager.remove_action_group( self._action_group )
		manager.ensure_update()

  # UI DIALOGUES
	def _init_glade( self ):
		self._snapopen_glade = gtk.glade.XML( os.path.dirname( __file__ ) + "/snapopen.glade" )
		#setup window
		self._snapopen_window = self._snapopen_glade.get_widget( "SnapOpenWindow" )		
		self._snapopen_window.connect("key-release-event", self.on_window_key)		
		self._snapopen_window.set_transient_for(self._window)
		#setup buttons
		self._snapopen_glade.get_widget( "ok_button" ).connect( "clicked", self.open_selected_item )
		self._snapopen_glade.get_widget( "cancel_button" ).connect( "clicked", lambda a: self._snapopen_window.hide())
		#setup entry field
		self._glade_entry_name = self._snapopen_glade.get_widget( "entry_name" )
		self._glade_entry_name.connect("key-release-event", self.on_pattern_entry)		
		#setup list field
		self._hit_list = self._snapopen_glade.get_widget( "hit_list" )
		self._hit_list.connect("select-cursor-row", self.on_select_from_list)
		self._hit_list.connect("button_press_event", self.on_list_mouse)
		self._liststore = gtk.ListStore(str, str)
		self._hit_list.set_model(self._liststore)
		column = gtk.TreeViewColumn("Name" , gtk.CellRendererText(), text=0)
		column.set_sizing(gtk.TREE_VIEW_COLUMN_AUTOSIZE)
		column2 = gtk.TreeViewColumn("File", gtk.CellRendererText(), text=1)
		column2.set_sizing(gtk.TREE_VIEW_COLUMN_AUTOSIZE)
		self._hit_list.append_column(column)
		self._hit_list.append_column(column2)
		self._hit_list.get_selection().set_mode(gtk.SELECTION_MULTIPLE)

	#mouse event on list
	def on_list_mouse( self, widget, event ):
		if event.type == gtk.gdk._2BUTTON_PRESS:
			self.open_selected_item( event )			

	#key selects from list (passthrough 3 args)
	def on_select_from_list(self, widget, event):
		self.open_selected_item(event)

	#keyboard event on entry field
	def on_pattern_entry( self, widget, event ):
		if event.keyval == gtk.keysyms.Return:
			self.open_selected_item( event )
			return
		pattern = self._glade_entry_name.get_text()
		#modify line below as needed, these defaults work pretty well
		rawpath = self._rootdir.replace("file://", "")
		filefilter = "| grep -v \"/\.\""
		if self._show_hidden:
			filefilter = ""
		cmd = "find " + rawpath + " -maxdepth 5 -depth -type f -wholename \"*" + pattern + "*\" " + filefilter + " | grep -v \"~$\" | sort"
		self._liststore.clear()
		maxcount = 0
		for file in os.popen(cmd).readlines():
			file = file.rstrip().replace(rawpath, "")
			name = os.path.basename(file)
			self._liststore.append([name, file])
			maxcount = maxcount + 1
			if maxcount > max_result:
				break						
		title = self._snapopen_window.get_title().replace(" * too many hits", "")
		if maxcount > max_result:
			title = (title + " * too many hits")
		self._snapopen_window.set_title(title)
		
		selected = []
		self._hit_list.get_selection().selected_foreach(self.foreach, selected)

		if len(selected) == 0:
			iter = self._liststore.get_iter_first()
			if iter != None:
				self._hit_list.get_selection().select_iter(iter)

	#on menuitem activation (incl. shortcut)
	def on_snapopen_action( self ):
		fbroot = self.get_filebrowser_root()
		if fbroot != "" and fbroot is not None:
			self._rootdir = fbroot
			self._snapopen_window.set_title("Snap open (Filebrowser integration)")
		else:
			eddtroot = self.get_eddt_root()
			if eddtroot != "" and eddtroot is not None:
				self._rootdir = eddtroot
				self._snapopen_window.set_title("Snap open (EDDT integration)")
			else:
				self._snapopen_window.set_title("Snap open (cwd): " + self._rootdir)		
		self._snapopen_window.show()
		self._glade_entry_name.select_region(0,-1)
		self._glade_entry_name.grab_focus()

	#on any keyboard event in main window
	def on_window_key( self, widget, event ):
		if event.keyval == gtk.keysyms.Escape:
			self._snapopen_window.hide()

	def foreach(self, model, path, iter, selected):
		selected.append(model.get_value(iter, 1))

	#open file in selection and hide window
	def open_selected_item( self, event ):
		selected = []
		self._hit_list.get_selection().selected_foreach(self.foreach, selected)
		for selected_file in	selected:
			self._open_file ( selected_file )
		self._snapopen_window.hide()
	
	#gedit < 2.16 version (get_tab_from_uri)
	def get_tab_from_uri(self, window, uri):
          docs = window.get_documents()
	  for doc in docs:
            if doc.get_uri() == uri:
              return gedit.tab_get_from_document(doc)
          return None
	
	#opens (or switches to) the given file
	def _open_file( self, filename ):
 		uri = self._rootdir + "/" + filename
		#tab = self._window.get_tab_from_uri(uri)  #gedit > 2.16 
		tab = self.get_tab_from_uri(self._window, uri) # gedit < 2.16
		if tab == None:
			tab = self._window.create_tab_from_uri( uri, self._encoding, 0, False, False )
		self._window.set_active_tab( tab )

# EDDT integration
	def get_eddt_root(self):
	  base = u'/apps/gedit-2/plugins/eddt'
	  client = gconf.client_get_default()
	  client.add_dir(base, gconf.CLIENT_PRELOAD_NONE)
	  path = os.path.join(base, u'repository')
	  val = client.get(path)
	  if val is not None:
	  	return val.get_string()

# FILEBROWSER integration
	def get_filebrowser_root(self):
	  base = u'/apps/gedit-2/plugins/filebrowser/on_load'
	  client = gconf.client_get_default()
	  client.add_dir(base, gconf.CLIENT_PRELOAD_NONE)
	  path = os.path.join(base, u'virtual_root')
	  val = client.get(path)
	  if val is not None:
	  	#also read hidden files setting
		  base = u'/apps/gedit-2/plugins/filebrowser'
		  client = gconf.client_get_default()
		  client.add_dir(base, gconf.CLIENT_PRELOAD_NONE)
		  path = os.path.join(base, u'filter_mode')
		  fbfilter = client.get(path).get_string()
		  if fbfilter.find("hidden") == -1:
		  	self._show_hidden = True
		  else:
		  	self._show_hidden = False		  	
		  return val.get_string()

# STANDARD PLUMMING
class SnapOpenPlugin( gedit.Plugin ):
	DATA_TAG = "SnapOpenPluginInstance"

	def __init__( self ):
		gedit.Plugin.__init__( self )

	def _get_instance( self, window ):
		return window.get_data( self.DATA_TAG )

	def _set_instance( self, window, instance ):
		window.set_data( self.DATA_TAG, instance )

	def activate( self, window ):
		self._set_instance( window, SnapOpenPluginInstance( self, window ) )

	def deactivate( self, window ):
		self._get_instance( window ).deactivate()
		self._set_instance( window, None )

	def update_ui( self, window ):
		self._get_instance( window ).update_ui()


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]