Attachment 'duplicate_line.py'

Download

   1 # -*- coding: utf8 -*-
   2 #  Duplicate Line plugin
   3 #
   4 #  spetial thanks to Micah:
   5 #  http://www.micahcarrick.com/writing-plugins-for-gedit-3-in-python.html
   6 #
   7 #  Copyright (C) 2011 Nixahn <nixahn@gmail.com>
   8 #  Copyright (C) 2007 Shaddy Zeineddine <shaddyz@users.sourceforge.net>
   9 #  Copyright (C) 2005 Marcus Lunzenauer <mlunzena@uos.de>
  10 #
  11 #  This program is free software; you can redistribute it and/or modify
  12 #  it under the terms of the GNU General Public License as published by
  13 #  the Free Software Foundation; either version 2 of the License, or
  14 #  (at your option) any later version.
  15 #
  16 #  This program is distributed in the hope that it will be useful,
  17 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
  18 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19 #  GNU General Public License for more details.
  20 #
  21 #  You should have received a copy of the GNU General Public License
  22 #  along with this program; if not, write to the Free Software
  23 #  Foundation, Inc., 59 Temple Place, Suite 330,
  24 #  Boston, MA 02111-1307, USA.
  25 
  26 from gi.repository import GObject, Gtk, Gdk, Gedit
  27 
  28 dup_line_xml = """
  29 <ui>
  30 	<menubar name="MenuBar">
  31 		<menu name="EditMenu" action="Edit">
  32 			<placeholder name="EditOps_3">
  33 				<separator name="DuplicateLineSep1"/>
  34 				<menuitem name="DeleteLine"          action="DeleteLine"/>
  35 				<menuitem name="DuplicateLine"       action="DuplicateLine"/>
  36 			</placeholder>
  37 		</menu>
  38 	</menubar>
  39 </ui>
  40 """
  41 
  42 class DuplicateLine(GObject.Object, Gedit.WindowActivatable):
  43 	__gtype_name__ = "DuplicateLine"
  44 	window = GObject.property(type=Gedit.Window)
  45 
  46 	def __init__(self):
  47 		GObject.Object.__init__(self)
  48 
  49 	def delete_line(self, action):
  50 		view = self.window.get_active_view()
  51 		# GTK_DELETE_PARAGRAPH_ENDS=5
  52 		view.do_delete_from_cursor(5,1)
  53 
  54 	def duplicate_line(self, action):
  55 		view = self.window.get_active_view()
  56 		doc	= self.window.get_active_document()
  57 		doc.begin_user_action()
  58 		itorig=doc.get_iter_at_mark(doc.get_insert())
  59 		offset=itorig.get_line_offset()
  60 		# GTK_MOVEMENT_PARAGRAPH_ENDS=6
  61 		if offset:
  62 			view.do_move_cursor(view,6,-1,0)
  63 		itstart = doc.get_iter_at_mark(doc.get_insert())
  64 		itend = doc.get_iter_at_mark(doc.get_insert())
  65 		itend.forward_line();
  66 		line = doc.get_slice(itstart, itend, True)
  67 		doc.insert_at_cursor(line)
  68 		# GTK_MOVEMENT_PARAGRAPHS=5		
  69 		# view.do_move_cursor(view,5,-1,0)
  70 		# GTK_MOVEMENT_DISPLAY_LINE_ENDS = 4
  71 		# view.do_move_cursor(view,4,1,0)
  72 		# GTK_MOVEMENT_LOGICAL_POSITIONS = 0
  73 		if offset:
  74 			view.do_move_cursor(view,0,offset,0)
  75 		doc.end_user_action()
  76 
  77 	def do_activate(self):
  78 		actions = [
  79 			('DeleteLine',          None, 'Delete To End Of Line', '<Shift><Control>d', "Delete To End Of Line", self.delete_line),
  80 			('DuplicateLine',       None, 'Duplicate Line',        '<Control>j',        "Duplicate Line",        self.duplicate_line)
  81 		]
  82 
  83 		self._actions = Gtk.ActionGroup("GeditDuplicateLinePluginActions")
  84 		self._actions.add_actions(actions)
  85 
  86 		manager = self.window.get_ui_manager()
  87 		manager.insert_action_group(self._actions)
  88 
  89 		self._ui_merge_id = manager.add_ui_from_string(dup_line_xml)
  90 		manager.ensure_update()
  91 
  92 	def do_deactivate(self):
  93 		manager = self.window.get_ui_manager()
  94 		manager.remove_ui(self._ui_merge_id)
  95 		manager.remove_action_group(self._actions)
  96 		manager.ensure_update()
  97 		
  98 	def do_update_state(self):
  99 		view = self.window.get_active_view()
 100 		self._actions.set_sensitive(bool(view and view.get_editable()))

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.