Attachment 'advanced_editing.py'
Download 1 # -*- coding: utf8 -*-
2 # Advanced editing plugin
3 #
4 # Copyright (C) 2007 Shaddy Zeineddine <shaddyz@users.sourceforge.net>
5 # Copyright (C) 2005 Marcus Lunzenauer <mlunzena@uos.de>
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 2 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, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330,
20 # Boston, MA 02111-1307, USA.
21
22 import gedit
23 import gtk
24
25 adv_edit_str = """
26 <ui>
27 <menubar name="MenuBar">
28 <menu name="EditMenu" action="Edit">
29 <placeholder name="EditOps_3">
30 <separator name="AdvancedEditingSep1"/>
31 <menuitem name="DeleteLine" action="DeleteLine"/>
32 <menuitem name="DeleteLineBackwards" action="DeleteLineBackwards"/>
33 <menuitem name="DuplicateLine" action="DuplicateLine"/>
34 <separator name="AdvancedEditingSep2"/>
35 <menuitem name="RemoveWhitespace" action="RemoveWhitespace"/>
36 <menuitem name="ReduceWhitespace" action="ReduceWhitespace"/>
37 </placeholder>
38 </menu>
39 </menubar>
40 </ui>
41 """
42
43 class AdvancedEditingPlugin(gedit.Plugin):
44 def __init__(self):
45 gedit.Plugin.__init__(self)
46
47 def delete_line(self, action, window):
48 view = window.get_active_view()
49 view.do_delete_from_cursor(view, gtk.DELETE_PARAGRAPH_ENDS, 1)
50
51 def delete_line_bw(self, action, window):
52 view = window.get_active_view()
53 doc = window.get_active_document()
54 doc.begin_user_action()
55 view.do_move_cursor(view, gtk.MOVEMENT_PARAGRAPH_ENDS, -1, 0)
56 itstart = doc.get_iter_at_mark(doc.get_insert())
57 itend = doc.get_iter_at_mark(doc.get_insert())
58 itend.forward_line();
59 line = doc.get_slice(itstart, itend, True)
60 doc.delete(itstart, itend);
61 doc.end_user_action()
62
63 def duplicate_line(self, action, window):
64 view = window.get_active_view()
65 doc = window.get_active_document()
66 doc.begin_user_action()
67 view.do_move_cursor(view, gtk.MOVEMENT_PARAGRAPH_ENDS, -1, 0)
68 itstart = doc.get_iter_at_mark(doc.get_insert())
69 itend = doc.get_iter_at_mark(doc.get_insert())
70 itend.forward_line();
71 line = doc.get_slice(itstart, itend, True)
72 doc.insert_at_cursor(line)
73 view.do_move_cursor(view, gtk.MOVEMENT_PARAGRAPHS, -1, 0)
74 doc.end_user_action()
75
76 def remove_whitespace(self, action, window):
77 view = window.get_active_view()
78 view.do_delete_from_cursor(view, gtk.DELETE_WHITESPACE, 1)
79
80 def reduce_whitespace(self, action, window):
81 view = window.get_active_view()
82 view.do_delete_from_cursor(view, gtk.DELETE_WHITESPACE, 1)
83 view.do_insert_at_cursor(view, ' ')
84
85 def activate(self, window):
86 actions = [
87 ('DeleteLine', None, 'Delete To End Of Line', '<Shift><Control>j', "Delete To End Of Line", self.delete_line),
88 ('DeleteLineBackwards', None, 'Kill Line', '<Control>j', "Kill Line", self.delete_line_bw),
89 ('DuplicateLine', None, 'Duplicate Line', '<Control>d', "Duplicate Line", self.duplicate_line),
90 ('RemoveWhitespace', None, 'Remove Whitespace', '<Shift><Alt>j', "Remove Whitespace", self.remove_whitespace),
91 ('ReduceWhitespace', None, 'Reduce Whitespace', '<Alt>j', "Reduce Whitespace", self.reduce_whitespace)
92 ]
93
94 # store per window data in the window object
95 windowdata = dict()
96 window.set_data("AdvancedEditingPluginWindowDataKey", windowdata)
97
98 windowdata["action_group"] = gtk.ActionGroup("GeditAdvancedEditingPluginActions")
99 windowdata["action_group"].add_actions(actions, window)
100
101 manager = window.get_ui_manager()
102 manager.insert_action_group(windowdata["action_group"], -1)
103
104 windowdata["ui_id"] = manager.add_ui_from_string(adv_edit_str)
105
106 window.set_data("AdvancedEditingPluginInfo", windowdata)
107
108 def deactivate(self, window):
109 windowdata = window.get_data("AdvancedEditingPluginWindowDataKey")
110 manager = window.get_ui_manager()
111 manager.remove_ui(windowdata["ui_id"])
112 manager.remove_action_group(windowdata["action_group"])
113
114 def update_ui(self, window):
115 view = window.get_active_view()
116 windowdata = window.get_data("AdvancedEditingPluginWindowDataKey")
117 windowdata["action_group"].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.You are not allowed to attach a file to this page.