/usr/share/gps/plug-ins/rectangles.py is in gnat-gps-common 5.0-16.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 | # for Emacs: -*- mode: python; python-indent: 3 -*-
"""This file provides support for rectangle-mode in an editor.
In particular, it is possible to select a rectangular area,
cut it, and paste it elsewhere.
To perform a selection, use the standard selection mechanisms (e.g.
mouse+click selection, or keyboard shift+arrows).
The highlighting of the selection itself is done on the whole
lines, not on the rectangle itself, but the rectangle is the
part between the column of the start of the selection, and the
column of the end of the selection. For instance:
ABCD
EFGH
IJKL
If the selection goes from B to K, the rectangle will be:
BC
FG
JK
However, the highlighting will include:
BCD
EFGH
IJK
Cutting the rectangle will result in:
AD
EH
IL
If you move the cursor before "A", and paste the rectangle:
BCAD
FGEH
JKIL
"""
############################################################################
## No user customization below this line
############################################################################
from GPS import *
import traceback
def rectangle_delete (menu):
"""Delete the selected rectangle"""
Rectangle.from_buffer (EditorBuffer.get ()).delete ()
def rectangle_cut (menu):
"""Cut the selected rectangle into the clipboard"""
Rectangle.from_buffer (EditorBuffer.get ()).cut ()
def rectangle_copy (menu):
"""Copy the selected rectangle into the clipboard"""
Rectangle.from_buffer (EditorBuffer.get ()).copy ()
def rectangle_paste (menu):
"""Paste the last entry in the clipboard as a rectangle in the current editor"""
Rectangle.paste (loc = EditorBuffer.get ().current_view ().cursor ())
def rectangle_open (menu):
"""Insert blank spaces to fill the selected rectangle.
This pushes its text to the right"""
Rectangle.from_buffer (EditorBuffer.get ()).open ()
def rectangle_insert (menu, text=None):
"""Insert TEXT at the beginning of each line of the selected rectangle.
If TEXT is unspecified, an interactive dialog is open"""
if not text:
text = MDI.input_dialog ("Text to insert before each line:", "")
if not text: return
text = text [0]
Rectangle.from_buffer (EditorBuffer.get ()).insert (text)
def rectangle_clear (menu):
"""Replaces the contents of the rectangle with spaces"""
Rectangle.from_buffer (EditorBuffer.get ()).clear ()
def rectangle_string (menu, text=None):
"""Replaces the contents of the rectangle with TEXT on each line.
If TEXT is narrower or wider than the rectangle, the text is shifted
right or left as appropriate.
If TEXT is unspecified, an interactive dialog is open."""
if not text:
text = MDI.input_dialog ("Text to replace each line with:", "")
if not text: return
text = text [0]
Rectangle.from_buffer (EditorBuffer.get ()).string (text)
def on_gps_started (hook_name):
"""Create the menus associated with this module"""
Menu.create ("/Edit/Rectangle",
ref = "Redo",
add_before = False)
Menu.create ("/Edit/Rectangle/Cut", on_activate = rectangle_cut)
Menu.create ("/Edit/Rectangle/Copy", on_activate = rectangle_copy)
Menu.create ("/Edit/Rectangle/Paste", on_activate = rectangle_paste)
Menu.create ("/Edit/Rectangle/-")
Menu.create ("/Edit/Rectangle/Delete", on_activate = rectangle_delete)
Menu.create ("/Edit/Rectangle/Clear", on_activate = rectangle_clear)
Menu.create ("/Edit/Rectangle/Open", on_activate = rectangle_open)
Menu.create ("/Edit/Rectangle/Replace with Text", on_activate = rectangle_string)
Menu.create ("/Edit/Rectangle/Insert Text", on_activate = rectangle_insert)
##############################################################################
## No public function below this
##############################################################################
class Rectangle (object):
@staticmethod
def from_buffer (buffer):
start = buffer.selection_start ()
end = buffer.selection_end () - 1
return Rectangle (buffer = buffer,
start_line = start.line (),
start_col = start.column (),
end_line = end.line (),
end_col = end.column ())
def __init__ (self, buffer, start_line, start_col, end_line, end_col):
"""Create a new rectangle.
Internally, ensures that start_line <= end_line and
start_col <= end_col
"""
self.buffer = buffer
self.start_line = min (start_line, end_line)
self.end_line = max (start_line, end_line)
self.start_col = min (start_col, end_col)
self.end_col = max (start_col, end_col)
def insert (self, text):
"""Insert TEXT at the beginning of each line of the rectangle."""
self.__apply (self.__insert_func, self.__open_line_func, text)
def open (self):
"""Insert blank spaces to fill the selected rectangle.
This pushes its text to the right"""
self.__apply (self.__insert_func, self.__open_line_func,
" " * (self.end_col - self.start_col))
def copy (self):
"""Copy the selected rectangle into the clipboard"""
start = EditorLocation (
self.buffer, self.start_line, self.start_col).create_mark ()
end = EditorLocation (
self.buffer, self.end_line, self.end_col).create_mark()
self.__apply (self.__cut_func, self.__copy_empty_func, True, True)
self.buffer.select (start.location(), end.location())
def delete (self):
"""Delete the selected rectangle"""
self.__apply (self.__cut_func, None, False, False)
def cut (self):
"""Cut the selected rectangle into the clipboard"""
self.__apply (self.__cut_func, self.__copy_empty_func, True, False)
def clear (self):
"""Replaces the contents of the rectangle with spaces"""
self.__apply (self.__replace_func, None,
" " * (self.end_col - self.start_col))
def string (self,text):
"""Replaces the contents of the rectangle with TEXT on each line.
If TEXT is narrower or wider than the rectangle, the text is shifted
right or left as appropriate."""
self.__apply (self.__replace_func, self.__open_and_insert_func, text)
@staticmethod
def paste (loc):
"""Paste the last entry in the clipboard as a rectangle at LOC"""
try:
buffer = loc.buffer ()
start = loc
selection = Clipboard.contents () [Clipboard.current()]
buffer.start_undo_group ()
for line in selection.splitlines():
buffer.insert (start, line)
start = EditorLocation (buffer, start.line() + 1, start.column())
buffer.finish_undo_group ()
except:
Logger ("TESTSUITE").log (
"Unexpected exception: " + traceback.format_exc())
def __cut_func (self, start, end, in_clipboard, copy):
if in_clipboard:
append = (start.line () != self.start_line)
if start <= end:
if copy:
self.buffer.copy (start, end, append=append)
else:
self.buffer.cut (start, end, append=append)
if end.line() != self.end_line:
Clipboard.copy (text="\n", append=True)
else:
self.buffer.delete (start, end)
def __insert_func (self, start, end, text):
"""Fills the range START..END with spaces and moves the text rightward"""
self.buffer.insert (start, text)
def __replace_func (self, start, end, text):
"""Replaces the range START .. END with TEXT"""
self.buffer.delete (start, end)
self.buffer.insert (start, text)
def __open_line_func (self, func, eol, *args):
self.buffer.insert (eol, " " * (self.start_col - eol.column()))
current = EditorLocation (self.buffer, eol.line (), self.start_col)
func (current, current - 1, *args)
def __open_and_insert_func (self, func, eol, text):
self.buffer.insert (eol, " " * (self.start_col - eol.column()) + text)
def __copy_empty_func (self, func, eol, *args):
Clipboard.copy (text="\n", append=True)
def __apply (self, func, short_line_func, *args):
"""Applies FUNC for each line segment in SELF.
FUNC is called wth two parameters of type EditorLocation for the
range that FUNC should apply on.
SHORT_LINE_FUNC is called when the current line is shorter than self.start_col.
It receives a single parameter (+ args), the last char in the line.
Any additional parameter specified in ARGS is passed to FUNC."""
try:
line = self.start_line
self.buffer.start_undo_group()
while line <= self.end_line:
## Some lines might not include enough characters for the rectangle
eol = EditorLocation (self.buffer, line, 1).end_of_line ()
if eol.column () > self.end_col:
endcolumn = EditorLocation (self.buffer, line, self.end_col)
current = EditorLocation (self.buffer, line, self.start_col)
func (current, endcolumn, *args)
else:
if eol.column () < self.start_col:
if short_line_func:
short_line_func (func, eol, *args)
else:
current = EditorLocation (self.buffer, line, self.start_col)
eol = current.end_of_line ()
if eol.column() != 1:
func (current, eol - 1, *args)
else:
func (current, eol, *args)
line += 1
self.buffer.finish_undo_group()
except:
Logger ("TESTSUITE").log (
"Unexpected exception: " + traceback.format_exc())
Hook ("gps_started").add (on_gps_started)
|