Attachment 'toggletextwrap.py'

Download

   1 # -*- coding: utf8 -*-
   2 # Text Wrap Gedit Plugin
   3 #
   4 # This file is part of the Text Wrap Plugin for Gedit
   5 # Copyright (C) 2011 Francisco Franchetti <nixahn@gmail.com>
   6 #
   7 # This program is free software: you can redistribute it and/or modify
   8 # it under the terms of the GNU General Public License as published by
   9 # the Free Software Foundation, either version 3 of the License, or
  10 # (at your option) any later version.
  11 #
  12 # This program is distributed in the hope that it will be useful,
  13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15 # GNU General Public License for more details.
  16 #
  17 # You should have received a copy of the GNU General Public License
  18 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
  19 
  20 # This plugin is intended to ease the setting of Text Wrap (aka Line Wrap,
  21 # Word Wrap) by either a Keyboard Shortcurt (currently sticked to Shift-Ctrl-B),
  22 # a new entry in the View Menu or by an Icon in the Toolbar. The use of either 
  23 # option works as a toggle (de- or activate text wrap). The initial setting for 
  24 # new or new opened files is taken from the setting in the Preferences dialog 
  25 # and remembered per file as long thew file is open. 
  26 
  27 # This plugin was developed for gedit 2 by Christian Hartmann at <christian.hartmann@berlin.de>. Parts of this plugin are based on the work of Mike Doty <mike@psyguygames.com>
  28 # who wrote the infamous SplitView plugin. The rest is inspired from the Python
  29 # Plugin Howto document and the Python-GTK documentation.
  30 
  31 # CHANGELOG
  32 # =========
  33 # * 2008-10-10:
  34 #   0.1 initial release for private use only
  35 # * 2009-04-26:
  36 #   0.2 changed filenames from textwrap to TextWrap as it conflicts with 
  37 #   /usr/lib/python2.6/textwrap.py when loading the plugin. Unfortunately
  38 #   i have no real clue what actualy is causing this conflict. This might
  39 #   be reasoned by a change in the Gedit Python Plugin Loader, as this has
  40 #   not been happening before upgrading gedit or a prerequisite of it through
  41 #   an upgrade of my Ubuntu to 8.10 or 9.04. Added a couple documentst mainly
  42 #   to ease the burdon of installation for gedit plugin beginners and made it
  43 #   public available on my company website: http://hartmann-it-design.de/gedit
  44 # * 2011-11-05:
  45 #   migration to gedit 3 by Francisco Franchetti. things changed are
  46 #   in the .py file
  47 #   _ dont use the enums in capital letters; look up the numbers online and use those
  48 #   _ gtk -> Gtk
  49 #	_ add the do_ in front of activate, deactivate, update_state (not update_ui); a good practice would be to add it to the custom methods too
  50 #   _ def statements don't have the window argument (defined for the class in the beginning)
  51 #   _ import statements are different
  52 #	_ class definition, init are different
  53 #   _ delete first line with the !/dev...
  54 #   in the .plugin file
  55 #   _ [Gedit plugin] -> [Plugin]
  56 #   _ IAge=3
  57 
  58 # import basic requisites
  59 from gi.repository import GObject, Gtk, Gdk, Gedit
  60 
  61 # just a constant used in several places herein
  62 prefix = "Plugin TextWrap "
  63 
  64 # a common ui definition for menu and toolbar additions
  65 ui_str = """<ui>
  66   <menubar name="MenuBar">
  67 	<menu name="ViewMenu" action="View">
  68 	  <placeholder name="ViewOps_2">
  69 		<menuitem name="ToggleTextWrap" action="TTextWrap" />
  70 	  </placeholder>
  71 	</menu>
  72   </menubar>
  73   <toolbar name="ToolBar">
  74 	<separator />
  75 	<toolitem name="ToggleTextWrap" action="TTextWrap" />
  76   </toolbar>
  77 </ui>
  78 """
  79 
  80 # define the plugin class (helper class is not needed anymore cause gtk3 has windowactivatable)
  81 class ToggleTextWrap(GObject.Object,Gedit.WindowActivatable):
  82 	__gtype_name__= "ToggleTextWrap"
  83 	window=GObject.property(type=Gedit.Window)
  84 
  85 	def __init__(self):
  86 		GObject.Object.__init__(self)
  87 
  88 	def do_toggle_text_wrap(self, action):
  89 		view = self.window.get_active_view()
  90 		current_action = self._action_group.get_action("TTextWrap")
  91 		if current_action.get_active():
  92 			view.set_wrap_mode(2)
  93 		else:
  94 			view.set_wrap_mode(0)
  95 
  96 	def do_activate(self):
  97 		
  98 		# Get initial state from text wrapping in this view (not available
  99 		# on gedit startup but if plugin is enabled during the gedit session
 100 		# and for what ever reason we do not have an update ui signal on init)
 101 		view = self.window.get_active_view()
 102 		try:
 103 			current_wrap_mode = view.get_wrap_mode()
 104 			# the order gives the numbers, starting at 0 for WRAP_NONE
 105 			# typedef enum {
 106 			# 	GTK_WRAP_NONE,
 107 			# 	GTK_WRAP_CHAR,
 108 			# 	GTK_WRAP_WORD,
 109 			# 	GTK_WRAP_WORD_CHAR
 110 			# } GtkWrapMode;
 111 			if current_wrap_mode == 0:
 112 				self._initial_toggle_state = False
 113 			else:
 114 				self._initial_toggle_state = True
 115 		except:
 116 			# Define default initial state for the plugin (should read this from the preferences file)
 117 			self._initial_toggle_state = False
 118 			# view.set_wrap_mode(0)	
 119 
 120 		# Add "Toggle Text Wrap" to the View menu and to the Toolbar
 121 		# Get the GtkUIManager
 122 		self._manager = self.window.get_ui_manager()
 123 		# Create a new action group
 124 		self._action_group = Gtk.ActionGroup("GeditTextWrapPluginActions")
 125 		self._action_group.add_toggle_actions([(
 126 				"TTextWrap", 
 127 				"gtk-ok", 
 128 				_("Text Wrap"), 
 129 				"<Ctrl><Shift>B", 
 130 				_("Toggle Current Text Wrap Setting"), 
 131 				self.do_toggle_text_wrap,
 132 				self._initial_toggle_state)])
 133 		# Insert the action group
 134 		self._manager.insert_action_group(self._action_group)
 135 		# Add my item to the "Views" menu and to the Toolbar
 136 		self._ui_id = self._manager.add_ui_from_string(ui_str)
 137 		# Debug merged ui
 138 		self._manager.ensure_update()
 139 
 140 	def do_deactivate(self):
 141 		# Remove the ui
 142 		self._manager.remove_ui(self._ui_id)
 143 		self._ui_id = None
 144 		# Remove action group
 145 		self._manager.remove_action_group(self._action_group)
 146 		self._action_group = None
 147 		# ensure that manager updates
 148 		self._manager.ensure_update()
 149 
 150 	def do_update_state(self):
 151 		view = self.window.get_active_view()
 152 		self._action_group.set_sensitive(self.window.get_active_document() != None)
 153 		# self._action_group.set_sensitive(bool(view and view.get_editable()))
 154 		try:
 155 			# Get initial state from word wrapping in this view (if any)
 156 			current_wrap_mode = view.get_wrap_mode()
 157 			# Get our action and set state according to current wrap mode
 158 			current_action = self._action_group.get_action("TTextWrap")
 159 			if current_wrap_mode == 0:
 160 				current_action.set_active(False)
 161 			else:
 162 				current_action.set_active(True)
 163 		except:
 164 			return
 165 
 166 		
 167 
 168 #	def _console(self, vartext):
 169 #		if self._DEBUG:
 170 #			print prefix, vartext	

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2021-02-25 09:43:41, 3.4 KB) [[attachment:duplicate_line.new.py]]
  • [get | view] (2021-02-25 09:43:41, 0.4 KB) [[attachment:duplicate_line.plugin]]
  • [get | view] (2021-02-25 09:43:41, 3.4 KB) [[attachment:duplicate_line.py]]
  • [get | view] (2021-02-25 09:43:41, 3.3 KB) [[attachment:duplicate_line_old.py]]
  • [get | view] (2021-02-25 09:43:41, 4.7 KB) [[attachment:regex_replace-gedit3.tar.gz]]
  • [get | view] (2021-02-25 09:43:41, 0.4 KB) [[attachment:toggletextwrap.plugin]]
  • [get | view] (2021-02-25 09:43:41, 6.3 KB) [[attachment:toggletextwrap.py]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.