Attachment 'line_tools.py'
Download 1 # -*- coding: utf8 -*-
2 # Line Tools 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 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 import gedit
21 import gtk
22
23 class LineToolsPlugin(gedit.Plugin):
24
25 line_tools_str = """
26 <ui>
27 <menubar name="MenuBar">
28 <menu name="EditMenu" action="Edit">
29 <placeholder name="EditOps_6">
30 <menu action="LineTools">
31 <menuitem action="TrimLine"/>
32 <menuitem action="ClearLine"/>
33 <menuitem action="DeleteLine"/>
34 <menuitem action="DeleteLine2"/>
35 <menuitem action="DuplicateLine"/>
36 <menuitem action="RaiseLine"/>
37 <menuitem action="LowerLine"/>
38 <menuitem action="CopyLine"/>
39 <menuitem action="CutLine"/>
40 <menuitem action="PasteLine"/>
41 <menuitem action="ReplaceLine"/>
42 </menu>
43 </placeholder>
44 </menu>
45 <menu name="SearchMenu" action="Search">
46 <placeholder name="SearchOps_6">
47 <menu action="SetBookmark">
48 <menuitem action="SetBookmark1"/>
49 <menuitem action="SetBookmark2"/>
50 <menuitem action="SetBookmark3"/>
51 <menuitem action="SetBookmark4"/>
52 <menuitem action="SetBookmark5"/>
53 <menuitem action="SetBookmark6"/>
54 <menuitem action="SetBookmark7"/>
55 <menuitem action="SetBookmark8"/>
56 <menuitem action="SetBookmark9"/>
57 <menuitem action="SetBookmark0"/>
58 </menu>
59 <menu action="RecallBookmark">
60 <menuitem action="RecallBookmark1"/>
61 <menuitem action="RecallBookmark2"/>
62 <menuitem action="RecallBookmark3"/>
63 <menuitem action="RecallBookmark4"/>
64 <menuitem action="RecallBookmark5"/>
65 <menuitem action="RecallBookmark6"/>
66 <menuitem action="RecallBookmark7"/>
67 <menuitem action="RecallBookmark8"/>
68 <menuitem action="RecallBookmark9"/>
69 <menuitem action="RecallBookmark0"/>
70 </menu>
71 </placeholder>
72 </menu>
73 </menubar>
74 </ui>
75 """
76 bookmarks = {}
77
78 def __init__(self):
79 gedit.Plugin.__init__(self)
80
81 def activate(self, window):
82 actions = [
83 ('LineTools', None, 'Line Tools'),
84 ('TrimLine', None, 'Trim Line', '<Control>t', 'Remove characters between the cursor and the line end', self.trim_line),
85 ('ClearLine', None, 'Clear Line', '<Control>b', 'Remove all the characters on the current line', self.clear_line),
86 ('DeleteLine', None, 'Kill Line Alt.', '', 'Completely remove the current line and resets cursor offset', self.delete_line),
87 ('DeleteLine2', None, 'Kill Line', '<Shift><Control>d', 'Completely remove the current line and retains cursor offset', self.delete_line2),
88 ('DuplicateLine', None, 'Duplicate Line', '<Control>d', 'Create a duplicate of the current line below the current line', self.duplicate_line),
89 ('RaiseLine', None, 'Raise Line', '<Control>m', 'Transpose the current line with the line above it', self.raise_line),
90 ('LowerLine', None, 'Lower Line', '<Shift><Control>m', 'Transpose the current line with the line below it', self.lower_line),
91 ('CopyLine', None, 'Copy Line', '<Shift><Control>c', 'Copy the contents of the current line to the clipboard', self.copy_line),
92 ('CutLine', None, 'Cut Line', '<Shift><Control>x', 'Copy the contents of the current line to the clipboard and then remove it', self.cut_line),
93 ('PasteLine', None, 'Paste Line', '<Shift><Control>v', 'Paste the contents of the clipboard to the current line', self.paste_line),
94 ('ReplaceLine', None, 'Replace Line', '', 'Paste the contents of the clipboard replacing the current line', self.replace_line),
95 ('SetBookmark', None, 'Set Bookmark'),
96 ('SetBookmark1', None, 'Set Bookmark 1', '<Control>!', 'Set a bookmark at the current line', self.set_bookmark_1),
97 ('SetBookmark2', None, 'Set Bookmark 2', '<Control>@', 'Set a bookmark at the current line', self.set_bookmark_2),
98 ('SetBookmark3', None, 'Set Bookmark 3', '<Control>#', 'Set a bookmark at the current line', self.set_bookmark_3),
99 ('SetBookmark4', None, 'Set Bookmark 4', '<Control>$', 'Set a bookmark at the current line', self.set_bookmark_4),
100 ('SetBookmark5', None, 'Set Bookmark 5', '<Control>%', 'Set a bookmark at the current line', self.set_bookmark_5),
101 ('SetBookmark6', None, 'Set Bookmark 6', '<Control>^', 'Set a bookmark at the current line', self.set_bookmark_6),
102 ('SetBookmark7', None, 'Set Bookmark 7', '<Control>&', 'Set a bookmark at the current line', self.set_bookmark_7),
103 ('SetBookmark8', None, 'Set Bookmark 8', '<Control>*', 'Set a bookmark at the current line', self.set_bookmark_8),
104 ('SetBookmark9', None, 'Set Bookmark 9', '<Control>(', 'Set a bookmark at the current line', self.set_bookmark_9),
105 ('SetBookmark0', None, 'Set Bookmark 0', '<Control>)', 'Set a bookmark at the current line', self.set_bookmark_0),
106 ('RecallBookmark', None, 'Go to Bookmark'),
107 ('RecallBookmark1', None, 'Go to Bookmark 1', '<Control>1', 'Go to this bookmark', self.recall_bookmark_1),
108 ('RecallBookmark2', None, 'Go to Bookmark 2', '<Control>2', 'Go to this bookmark', self.recall_bookmark_2),
109 ('RecallBookmark3', None, 'Go to Bookmark 3', '<Control>3', 'Go to this bookmark', self.recall_bookmark_3),
110 ('RecallBookmark4', None, 'Go to Bookmark 4', '<Control>4', 'Go to this bookmark', self.recall_bookmark_4),
111 ('RecallBookmark5', None, 'Go to Bookmark 5', '<Control>5', 'Go to this bookmark', self.recall_bookmark_5),
112 ('RecallBookmark6', None, 'Go to Bookmark 6', '<Control>6', 'Go to this bookmark', self.recall_bookmark_6),
113 ('RecallBookmark7', None, 'Go to Bookmark 7', '<Control>7', 'Go to this bookmark', self.recall_bookmark_7),
114 ('RecallBookmark8', None, 'Go to Bookmark 8', '<Control>8', 'Go to this bookmark', self.recall_bookmark_8),
115 ('RecallBookmark9', None, 'Go to Bookmark 9', '<Control>9', 'Go to this bookmark', self.recall_bookmark_9),
116 ('RecallBookmark0', None, 'Go to Bookmark 0', '<Control>0', 'Go to this bookmark', self.recall_bookmark_0)
117 ]
118 windowdata = dict()
119 window.set_data("LineToolsPluginWindowDataKey", windowdata)
120 windowdata["action_group"] = gtk.ActionGroup("GeditLineToolsPluginActions")
121 windowdata["action_group"].add_actions(actions, window)
122 manager = window.get_ui_manager()
123 manager.insert_action_group(windowdata["action_group"], -1)
124 windowdata["ui_id"] = manager.add_ui_from_string(self.line_tools_str)
125 window.set_data("LineToolsPluginInfo", windowdata)
126
127 def deactivate(self, window):
128 windowdata = window.get_data("LineToolsPluginWindowDataKey")
129 manager = window.get_ui_manager()
130 manager.remove_ui(windowdata["ui_id"])
131 manager.remove_action_group(windowdata["action_group"])
132
133 def update_ui(self, window):
134 view = window.get_active_view()
135 windowdata = window.get_data("LineToolsPluginWindowDataKey")
136 windowdata["action_group"].set_sensitive(bool(view and view.get_editable()))
137
138 def trim_line(self, action, window):
139 doc = window.get_active_document()
140 doc.begin_user_action()
141 itstart = doc.get_iter_at_mark(doc.get_insert())
142 is_end = itstart.ends_line()
143 if is_end == False:
144 itend = doc.get_iter_at_mark(doc.get_insert())
145 itend.forward_to_line_end()
146 doc.delete(itstart, itend)
147 doc.end_user_action()
148
149 def clear_line(self, action, window):
150 doc = window.get_active_document()
151 doc.begin_user_action()
152 itstart = doc.get_iter_at_mark(doc.get_insert())
153 itstart.set_line_offset(0);
154 is_end = itstart.ends_line()
155 if is_end == False:
156 itend = doc.get_iter_at_mark(doc.get_insert())
157 is_end = itend.ends_line()
158 if is_end == False:
159 itend.forward_to_line_end()
160 doc.delete(itstart, itend)
161 doc.end_user_action()
162
163 def delete_line(self, action, window):
164 doc = window.get_active_document()
165 doc.begin_user_action()
166 itstart = doc.get_iter_at_mark(doc.get_insert())
167 itstart.set_line_offset(0)
168 itend = doc.get_iter_at_mark(doc.get_insert())
169 itend.forward_line()
170 doc.delete(itstart, itend)
171 doc.end_user_action()
172
173 def delete_line2(self, action, window):
174 doc = window.get_active_document()
175 doc.begin_user_action()
176 itstart = doc.get_iter_at_mark(doc.get_insert())
177 offset = itstart.get_line_offset()
178 itstart.set_line_offset(0)
179 itend = doc.get_iter_at_mark(doc.get_insert())
180 itend.forward_line()
181 doc.delete(itstart, itend)
182 itstart.set_line_offset(offset)
183 doc.end_user_action()
184 doc.place_cursor(itstart)
185
186 def duplicate_line(self, action, window):
187 doc = window.get_active_document()
188 doc.begin_user_action()
189 itstart = doc.get_iter_at_mark(doc.get_insert())
190 itstart.set_line_offset(0);
191 itend = doc.get_iter_at_mark(doc.get_insert())
192 itend.forward_line()
193 line = doc.get_slice(itstart, itend, True)
194 doc.insert(itend, line)
195 doc.end_user_action()
196
197 def raise_line(self, action, window):
198 doc = window.get_active_document()
199 doc.begin_user_action()
200 itstart = doc.get_iter_at_mark(doc.get_insert())
201 itstart.set_line_offset(0);
202 itstart.backward_line()
203 itend = doc.get_iter_at_mark(doc.get_insert())
204 itend.set_line_offset(0);
205 line = doc.get_slice(itstart, itend, True)
206 doc.delete(itstart, itend)
207 itend.forward_line()
208 doc.insert(itend, line)
209 doc.end_user_action()
210
211 def lower_line(self, action, window):
212 doc = window.get_active_document()
213 doc.begin_user_action()
214 itstart = doc.get_iter_at_mark(doc.get_insert())
215 itstart.forward_line()
216 itend = doc.get_iter_at_mark(doc.get_insert())
217 itend.forward_line()
218 itend.forward_line()
219 line = doc.get_slice(itstart, itend, True)
220 doc.delete(itstart, itend)
221 itstart.backward_line()
222 doc.insert(itstart, line)
223 doc.end_user_action()
224
225 def copy_line(self, action, window):
226 view = window.get_active_view()
227 doc = window.get_active_document()
228 itstart = doc.get_iter_at_mark(doc.get_insert())
229 offset = itstart.get_line_offset()
230 itstart.set_line_offset(0)
231 itend = doc.get_iter_at_mark(doc.get_insert())
232 itend.forward_line()
233 doc.begin_user_action()
234 doc.select_range(itstart, itend)
235 doc.copy_clipboard(view.get_clipboard(gtk.gdk.SELECTION_CLIPBOARD))
236 itstart.set_line_offset(offset)
237 doc.end_user_action()
238 doc.place_cursor(itstart)
239
240 def cut_line(self, action, window):
241 view = window.get_active_view()
242 doc = window.get_active_document()
243 itstart = doc.get_iter_at_mark(doc.get_insert())
244 offset = itstart.get_line_offset()
245 itstart.set_line_offset(0)
246 itend = doc.get_iter_at_mark(doc.get_insert())
247 itend.forward_line()
248 doc.begin_user_action()
249 doc.select_range(itstart, itend)
250 doc.cut_clipboard(view.get_clipboard(gtk.gdk.SELECTION_CLIPBOARD), True)
251 itstart = doc.get_iter_at_mark(doc.get_insert())
252 itstart.set_line_offset(offset)
253 doc.end_user_action()
254 doc.place_cursor(itstart)
255
256 def paste_line(self, action, window):
257 view = window.get_active_view()
258 doc = window.get_active_document()
259 itstart = doc.get_iter_at_mark(doc.get_insert())
260 offset = itstart.get_line_offset()
261 itstart.set_line_offset(0)
262 doc.begin_user_action()
263 doc.paste_clipboard(view.get_clipboard(gtk.gdk.SELECTION_CLIPBOARD), itstart, True)
264 doc.end_user_action()
265 itstart = doc.get_iter_at_mark(doc.get_insert())
266 itstart.backward_line()
267 itstart.set_line_offset(offset)
268 doc.place_cursor(itstart)
269
270 def replace_line(self, action, window):
271 view = window.get_active_view()
272 doc = window.get_active_document()
273 itstart = doc.get_iter_at_mark(doc.get_insert())
274 offset = itstart.get_line_offset()
275 itstart.set_line_offset(0)
276 itend = doc.get_iter_at_mark(doc.get_insert())
277 itend.forward_line()
278 doc.begin_user_action()
279 doc.delete(itstart, itend)
280 doc.paste_clipboard(view.get_clipboard(gtk.gdk.SELECTION_CLIPBOARD), itstart, True)
281 doc.end_user_action()
282 itstart = doc.get_iter_at_mark(doc.get_insert())
283 itstart.backward_line()
284 itstart.set_line_offset(offset)
285 doc.place_cursor(itstart)
286
287 def set_bookmark(self, action, window, position):
288 doc = window.get_active_document()
289 iterold = doc.get_iter_at_mark(doc.get_insert())
290 try:
291 markold = doc.get_mark(self.bookmarks[position])
292 doc.move_mark(markold, iterold)
293 except KeyError:
294 self.bookmarks[position] = 'LineToolsBookmark' + `position`
295 doc.create_mark(self.bookmarks[position], iterold, True)
296
297 def recall_bookmark(self, action, window, position):
298 try:
299 doc = window.get_active_document()
300 markold = doc.get_mark(self.bookmarks[position])
301 iterold = doc.get_iter_at_mark(markold)
302 view = window.get_active_view()
303 view.scroll_to_iter(iterold, 0, True)
304 doc.place_cursor(iterold)
305 except KeyError:
306 return
307
308 def set_bookmark_1(self, action, window):
309 self.set_bookmark(action, window, 1)
310
311 def set_bookmark_2(self, action, window):
312 self.set_bookmark(action, window, 2)
313
314 def set_bookmark_3(self, action, window):
315 self.set_bookmark(action, window, 3)
316
317 def set_bookmark_4(self, action, window):
318 self.set_bookmark(action, window, 4)
319
320 def set_bookmark_5(self, action, window):
321 self.set_bookmark(action, window, 5)
322
323 def set_bookmark_6(self, action, window):
324 self.set_bookmark(action, window, 6)
325
326 def set_bookmark_7(self, action, window):
327 self.set_bookmark(action, window, 7)
328
329 def set_bookmark_8(self, action, window):
330 self.set_bookmark(action, window, 8)
331
332 def set_bookmark_9(self, action, window):
333 self.set_bookmark(action, window, 9)
334
335 def set_bookmark_0(self, action, window):
336 self.set_bookmark(action, window, 0)
337
338 def recall_bookmark_1(self, action, window):
339 self.recall_bookmark(action, window, 1)
340
341 def recall_bookmark_2(self, action, window):
342 self.recall_bookmark(action, window, 2)
343
344 def recall_bookmark_3(self, action, window):
345 self.recall_bookmark(action, window, 3)
346
347 def recall_bookmark_4(self, action, window):
348 self.recall_bookmark(action, window, 4)
349
350 def recall_bookmark_5(self, action, window):
351 self.recall_bookmark(action, window, 5)
352
353 def recall_bookmark_6(self, action, window):
354 self.recall_bookmark(action, window, 6)
355
356 def recall_bookmark_7(self, action, window):
357 self.recall_bookmark(action, window, 7)
358
359 def recall_bookmark_8(self, action, window):
360 self.recall_bookmark(action, window, 8)
361
362 def recall_bookmark_9(self, action, window):
363 self.recall_bookmark(action, window, 9)
364
365 def recall_bookmark_0(self, action, window):
366 self.recall_bookmark(action, window, 0)
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.