/usr/share/pyshared/fltk/test/editor.py is in python-fltk 1.3.0-1.
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 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 | #!/usr/bin/env python
# $Id: editor.py 198 2006-02-16 07:22:28Z andreasheld $
# Author: Anderas Held
# Purpose: test text editor
# Created: Thu Jan 22 14:06:24 EST 2004
#
#
# Text Editor test program for pyFLTK the Python bindings
# for the Fast Light Tool Kit (FLTK).
#
# FLTK copyright 1998-1999 by Bill Spitzak and others.
# pyFLTK copyright 2003-2006 by Andreas Held and others.
#
# This library is free software you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public
# License along with this library if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
# USA.
#
# Please report all bugs and problems to "pyfltk-user@lists.sourceforge.net".
#
from fltk import *
import string, os.path, sys
from keyword import iskeyword
changed = False
filename = ""
title = ""
textbuf = None
editor = None
window = None
# Syntax highlighting stuff...
stylebuf = None
styletable = [ # Style table
[ FL_BLACK, FL_COURIER, 14 ], # A - Plain
[ FL_DARK_GREEN, FL_COURIER_ITALIC, 14 ], # B - Line comments
[ FL_DARK_GREEN, FL_COURIER_ITALIC, 14 ], # C - Block comments
[ FL_BLUE, FL_COURIER, 14 ], # D - Strings
[ FL_DARK_RED, FL_COURIER, 14 ], # E - Directives
[ FL_DARK_RED, FL_COURIER_BOLD, 14 ], # F - Types
[ FL_BLUE, FL_COURIER_BOLD, 14 ] # G - Keywords
]
# 'compare_keywords()' - Compare two keywords...
def compare_keywords(a, b):
return String.compare(a,b)
#
# 'style_parse()' - Parse text and produce style data.
#
def style_parse(text, style, length):
# Style letters:
#
# A - Plain
# B - Line comments
# C - Block comments
# D - Strings
# E - Directives
# F - Types
# G - Keywords
col = 0
new_style = ""
isComment = False
isString = False
pos = 0
last = False
while pos < length:
current = style[pos]
if isComment:
current = 'B'
if text[pos] == '\n':
isComment = False
elif isString:
current = 'D'
if text[pos] == '"' or text[pos] == "'":
isString = False
elif current in ['B', 'F', 'G']:
current = 'A'
if current == 'A':
# Check for directives, comments, strings, and keywords...
if text[pos] == '#':
current = 'B'
isComment = True
elif text[pos] == '"' or text[pos] == "'":
current = 'D'
isString = True
elif not last and text[pos].islower() or text[pos] == '_':
pos1 = pos
while pos1 < length:
if text[pos1].islower() or text[pos1] == '_':
pos1 += 1
else:
break
kw = text[pos:pos1]
if iskeyword(kw):
new_style = new_style+'F'*(pos1-pos)
current = 'F'
if pos1 >= length:
pos1 -= 1
pos = pos1
last = True
last = text[pos].isalnum() or text[pos] == '_' or text[pos] == '.'
if text[pos] == '\n':
col = 0
if current == 'B' or current == 'E':
current = 'A'
new_style = new_style+current
pos += 1
#print "new style: ", new_style
return new_style
def style_init():
global stylebuf
style = "A"*(textbuf.length())
text = textbuf.text()
if stylebuf == None:
stylebuf = Fl_Text_Buffer(textbuf.length())
style = style_parse(text, style, textbuf.length())
stylebuf.text(style)
def style_unfinished_cb(arg1, arg2):
#print "style_unfinished_cb"
pass
#
# 'style_update()' - Update the style buffer...
#
def style_update(pos, nInserted, nDeleted, nRestyled, deletedText, e):
global stylebuf, editor
if nInserted == 0 and nDeleted == 0:
stylebuf.unselect()
else:
if nInserted > 0:
style = 'A'*nInserted
stylebuf.replace(pos, pos+nDeleted, style)
else:
# Just delete characters in the style buffer...
stylebuf.remove(pos, pos+nDeleted)
# Select the area that was just updated to avoid unnecessary
# callbacks...
stylebuf.select(pos, pos + nInserted - nDeleted)
# Re-parse the changed region; we do this by parsing from the
# beginning of the previous line of the changed region to the end of
# the line of the changed region... Then we check the last
# style character and keep updating if we have a multi-line
# comment character...
start = textbuf.line_start(pos)
end = textbuf.line_end(pos + nInserted)
text = textbuf.text_range(start, end)
style = stylebuf.text_range(start, end)
if start==end:
last = 0
else:
last = style[end - start - 1]
style = style_parse(text, style, end - start)
stylebuf.replace(start, end, style)
editor.editor.redisplay_range(start, end)
if start == end or last != style[end - start - 1]:
# Either the user deleted some text, or the last character
# on the line changed styles, so reparse the
# remainder of the buffer...
end = textbuf.length()
text = textbuf.text_range(start, end)
style = stylebuf.text_range(start, end)
style_parse(text, style, end - start)
stylebuf.replace(start, end, style)
editor.editor.redisplay_range(start, end)
class EditorWindow(Fl_Double_Window):
search = ""
def __init__(self, w, h, label):
Fl_Double_Window.__init__(self, w, h, label)
self.replace_dlg = Fl_Window(300, 105, "Replace")
self.replace_find = Fl_Input(80, 10, 210, 25, "Find:")
self.replace_find.align(FL_ALIGN_LEFT)
self.replace_with = Fl_Input(80, 40, 210, 25, "Replace:")
self.replace_with.align(FL_ALIGN_LEFT)
self.replace_all = Fl_Button(10, 70, 90, 25, "Replace All")
self.replace_all.callback(replall_cb, self)
self.replace_next = Fl_Return_Button(105, 70, 120, 25, "Replace Next")
self.replace_next.callback(replace2_cb, self)
self.replace_cancel = Fl_Button(230, 70, 60, 25, "Cancel")
self.replace_cancel.callback(replcan_cb, self)
self.replace_dlg.end()
self.replace_dlg.set_non_modal()
self.editor = 0
def check_save():
global changed
if not changed:
return
r = fl_choice("The current file has not been saved.\n"
"Would you like to save it now?",
"Cancel", "Save", "Don't Save")
if r == 1:
save_cb(None)
return not changed
if r == 2:
return 1
else:
return 0
loading = False
def load_file(newfile, ipos):
global changed, loading, filename
loading = True
if ipos != -1:
insert = 1
changed = True
else:
insert = 0
changed = False
if insert == 0:
filename = ""
r = textbuf.loadfile(newfile)
else:
r = textbuf.insertfile(newfile, ipos)
if r != 0:
fl_alert("Error reading from file %s."%newfile)
else:
if insert == 0:
filename = newfile
loading = False
textbuf.call_modify_callbacks()
def save_file(newfile):
global changed, filename
if textbuf.savefile(newfile) != 0:
fl_alert("Error writing to file %s."%newfile)
else:
filename = newfile
changed = False
textbuf.call_modify_callbacks()
def copy_cb(widget, editor):
Fl_Text_Editor.kf_copy(0, editor.editor)
def cut_cb(widget, editor):
Fl_Text_Editor.kf_cut(0, editor.editor)
def delete_cb(widget):
global textbuf
textbuf.remove_selection()
def find_cb(widget, editor):
val = fl_input("Search String:", editor.search)
if val != None:
# User entered a string - go find it!
editor.search = val
find2_cb(widget)
def find2_cb(widget):
if editor.search[0] == 0:
# Search string is blank; get a new one...
find_cb(widget, editor)
return
pos = editor.editor.insert_position();
(found, pos) = textbuf.search_forward(pos, editor.search);
if found!= 0:
# Found a match; select and update the position...
textbuf.select(pos, pos+len(editor.search))
editor.editor.insert_position(pos+len(editor.search))
editor.editor.show_insert_position()
else:
fl_alert("No occurrences of %s found!"%editor.search)
def set_title(win):
global filename, title
if len(filename) == 0:
title = "Untitled"
else:
title = os.path.basename(filename)
if changed:
title = title+" (modified)"
win.label(title)
def changed_cb(i1, nInserted, nDeleted, i2, c1, editor):
global changed, loading
if (nInserted != 0 or nDeleted != 0) and loading == False:
changed = True
set_title(editor);
if loading:
editor.editor.show_insert_position()
def new_cb(widget):
global filename, changed
if check_save() == 0:
return
filename = ""
textbuf.select(0, textbuf.length())
textbuf.remove_selection()
changed = False
textbuf.call_modify_callbacks()
def open_cb(widget):
global filename
if check_save() == 0:
return
newfile = fl_file_chooser("Open File?", "*", filename)
if newfile != None:
load_file(newfile, -1)
def insert_cb(widget, editor):
global filename
newfile = fl_file_chooser("Open File?", "*", filename)
if newfile != None:
load_file(newfile, editor.editor.insert_position())
def paste_cb(widget, editor):
Fl_Text_Editor.kf_paste(0, editor.editor)
num_windows = 0
def close_cb(window, data):
global num_windows
if num_windows == 1 and check_save() == 0:
return
window.hide()
textbuf.remove_modify_callback(changed_cb, window, window)
num_windows -= 1;
if num_windows == 0:
sys.exit(0)
def quit_cb(widget, data):
global changed
if changed and check_save() == 0:
return
sys.exit(0)
def replace_cb(widget, editor):
editor.replace_dlg.show()
def replace2_cb(widget, editor):
find = editor.replace_find.value()
replace = editor.replace_with.value()
if len(find) == 0:
editor.replace_dlg.show()
return
editor.replace_dlg.hide()
pos = editor.editor.insert_position()
(found, pos) = textbuf.search_forward(pos, find)
if found != 0:
# Found a match; update the position and replace text...
textbuf.select(pos, pos+len(find))
textbuf.remove_selection()
textbuf.insert(pos, replace)
textbuf.select(pos, pos+len(replace))
editor.editor.insert_position(pos+len(replace))
editor.editor.show_insert_position()
else:
fl_alert("No occurrences of %s found!"%find)
def replall_cb(widget, editor):
find = editor.replace_find.value()
replace = editor.replace_with.value()
if len(find) == 0:
editor.replace_dlg.show()
return
editor.replace_dlg.hide()
editor.editor.insert_position(0)
times = 0
found = 1
while found != 0:
pos = editor.editor.insert_position()
(found, pos) = textbuf.search_forward(pos, find)
if found != 0:
# Found a match; update the position and replace text...
textbuf.select(pos, pos+len(find))
textbuf.remove_selection()
textbuf.insert(pos, replace)
editor.editor.insert_position(pos+len(replace))
editor.editor.show_insert_position()
times += 1
if times > 0:
fl_message("Replaced %d occurrences."%times)
else:
fl_alert("No occurrences of %s found!"%find)
def replcan_cb(widget, editor):
editor.replace_dlg.hide()
def save_cb(widget):
global filename
if len(filename) == 0:
# No filename - get one!
saveas_cb(widget, 0)
return
else:
save_file(filename)
def saveas_cb(widget, data):
global filename
newfile = fl_file_chooser("Save File As?", "*", filename)
if newfile != None:
save_file(newfile)
def view_cb(widget, data):
win = new_view()
win.show()
def new_view():
global num_windows, editor, stylebuf, styletable
w = EditorWindow(660, 400, title)
editor = w
w.begin()
m = Fl_Menu_Bar(0, 0, 660, 30);
menuitems = (( "&File", 0, 0, 0, FL_SUBMENU ),
( "&New File", 0, new_cb ),
( "&Open File...", FL_CTRL + ord('o'), open_cb ),
( "&Insert File...", FL_CTRL + ord('i'), insert_cb, editor, FL_MENU_DIVIDER ),
( "&Save File", FL_CTRL + ord('s'), save_cb ),
( "Save File &As...", FL_CTRL + FL_SHIFT + ord('s'), saveas_cb, 0, FL_MENU_DIVIDER ),
( "New &View", FL_ALT + ord('v'),view_cb, 0 ),
( "&Close View", FL_CTRL + ord('w'), close_cb, window, FL_MENU_DIVIDER ),
( "E&xit", FL_CTRL + ord('q'), quit_cb, 0 ),
( None, 0 ),
( "&Edit", 0, 0, 0, FL_SUBMENU ),
( "Cu&t", FL_CTRL + ord('x'), cut_cb, editor ),
( "&Copy", FL_CTRL + ord('c'), copy_cb, editor ),
( "&Paste", FL_CTRL + ord('v'), paste_cb, editor ),
( "&Delete", 0, delete_cb ),
( None, 0 ),
( "&Search", 0, 0, 0, FL_SUBMENU ),
( "&Find...", FL_CTRL + ord('f'), find_cb, editor ),
( "F&ind Again", FL_CTRL + ord('g'), find2_cb ),
( "&Replace...", FL_CTRL + ord('r'), replace_cb, editor ),
( "Re&place Again", FL_CTRL + ord('t'), replace2_cb, editor ),
( None, 0 ),
( None, 0 )
)
m.copy(menuitems)
w.editor = Fl_Text_Editor(0, 30, 660, 370)
w.editor.buffer(textbuf)
w.editor.highlight_data(stylebuf, styletable,
len(styletable),
# 'A', None, None)
'A', style_unfinished_cb, w.editor)
w.editor.textfont(FL_COURIER)
w.end()
w.resizable(w.editor)
w.callback(close_cb, w)
textbuf.add_modify_callback(style_update, w.editor)
textbuf.add_modify_callback(changed_cb, w)
textbuf.call_modify_callbacks()
num_windows += 1
return w
# main
textbuf = Fl_Text_Buffer()
style_init()
window = new_view()
window.show(1, sys.argv)
if len(sys.argv) > 1:
load_file(sys.argv[1], -1)
Fl.run()
|