| 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 |
from fltk import * |
| 32 |
import string, os.path, sys |
| 33 |
from keyword import iskeyword |
| 34 |
|
| 35 |
changed = False |
| 36 |
filename = "" |
| 37 |
title = "" |
| 38 |
textbuf = None |
| 39 |
editor = None |
| 40 |
window = None |
| 41 |
|
| 42 |
|
| 43 |
stylebuf = None |
| 44 |
|
| 45 |
styletable = [ |
| 46 |
[ FL_BLACK, FL_COURIER, 14 ], |
| 47 |
[ FL_DARK_GREEN, FL_COURIER_ITALIC, 14 ], |
| 48 |
[ FL_DARK_GREEN, FL_COURIER_ITALIC, 14 ], |
| 49 |
[ FL_BLUE, FL_COURIER, 14 ], |
| 50 |
[ FL_DARK_RED, FL_COURIER, 14 ], |
| 51 |
[ FL_DARK_RED, FL_COURIER_BOLD, 14 ], |
| 52 |
[ FL_BLUE, FL_COURIER_BOLD, 14 ] |
| 53 |
] |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
def compare_keywords(a, b): |
| 58 |
return String.compare(a,b) |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
def style_parse(text, style, length): |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
col = 0 |
| 74 |
new_style = "" |
| 75 |
isComment = False |
| 76 |
isString = False |
| 77 |
|
| 78 |
pos = 0 |
| 79 |
last = False |
| 80 |
while pos < length: |
| 81 |
current = style[pos] |
| 82 |
if isComment: |
| 83 |
current = 'B' |
| 84 |
if text[pos] == '\n': |
| 85 |
isComment = False |
| 86 |
elif isString: |
| 87 |
current = 'D' |
| 88 |
if text[pos] == '"' or text[pos] == "'": |
| 89 |
isString = False |
| 90 |
elif current in ['B', 'F', 'G']: |
| 91 |
current = 'A' |
| 92 |
|
| 93 |
if current == 'A': |
| 94 |
|
| 95 |
if text[pos] == '#': |
| 96 |
current = 'B' |
| 97 |
isComment = True |
| 98 |
elif text[pos] == '"' or text[pos] == "'": |
| 99 |
current = 'D' |
| 100 |
isString = True |
| 101 |
elif not last and text[pos].islower() or text[pos] == '_': |
| 102 |
pos1 = pos |
| 103 |
while pos1 < length: |
| 104 |
if text[pos1].islower() or text[pos1] == '_': |
| 105 |
pos1 += 1 |
| 106 |
else: |
| 107 |
break |
| 108 |
|
| 109 |
kw = text[pos:pos1] |
| 110 |
if iskeyword(kw): |
| 111 |
new_style = new_style+'F'*(pos1-pos) |
| 112 |
current = 'F' |
| 113 |
if pos1 >= length: |
| 114 |
pos1 -= 1 |
| 115 |
pos = pos1 |
| 116 |
last = True |
| 117 |
|
| 118 |
last = text[pos].isalnum() or text[pos] == '_' or text[pos] == '.' |
| 119 |
if text[pos] == '\n': |
| 120 |
col = 0 |
| 121 |
if current == 'B' or current == 'E': |
| 122 |
current = 'A' |
| 123 |
new_style = new_style+current |
| 124 |
pos += 1 |
| 125 |
|
| 126 |
return new_style |
| 127 |
|
| 128 |
def style_init(): |
| 129 |
global stylebuf |
| 130 |
style = "A"*(textbuf.length()) |
| 131 |
text = textbuf.text() |
| 132 |
if stylebuf == None: |
| 133 |
stylebuf = Fl_Text_Buffer(textbuf.length()) |
| 134 |
style = style_parse(text, style, textbuf.length()) |
| 135 |
stylebuf.text(style) |
| 136 |
|
| 137 |
def style_unfinished_cb(arg1, arg2): |
| 138 |
|
| 139 |
pass |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
def style_update(pos, nInserted, nDeleted, nRestyled, deletedText, e): |
| 145 |
global stylebuf, editor |
| 146 |
|
| 147 |
if nInserted == 0 and nDeleted == 0: |
| 148 |
stylebuf.unselect() |
| 149 |
else: |
| 150 |
if nInserted > 0: |
| 151 |
style = 'A'*nInserted |
| 152 |
stylebuf.replace(pos, pos+nDeleted, style) |
| 153 |
else: |
| 154 |
|
| 155 |
stylebuf.remove(pos, pos+nDeleted) |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
stylebuf.select(pos, pos + nInserted - nDeleted) |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
start = textbuf.line_start(pos) |
| 166 |
end = textbuf.line_end(pos + nInserted) |
| 167 |
text = textbuf.text_range(start, end) |
| 168 |
style = stylebuf.text_range(start, end) |
| 169 |
if start==end: |
| 170 |
last = 0 |
| 171 |
else: |
| 172 |
last = style[end - start - 1] |
| 173 |
|
| 174 |
style = style_parse(text, style, end - start) |
| 175 |
stylebuf.replace(start, end, style) |
| 176 |
editor.editor.redisplay_range(start, end) |
| 177 |
if start == end or last != style[end - start - 1]: |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
end = textbuf.length() |
| 182 |
text = textbuf.text_range(start, end) |
| 183 |
style = stylebuf.text_range(start, end) |
| 184 |
|
| 185 |
style_parse(text, style, end - start) |
| 186 |
|
| 187 |
stylebuf.replace(start, end, style) |
| 188 |
editor.editor.redisplay_range(start, end) |
| 189 |
|
| 190 |
|
| 191 |
class EditorWindow(Fl_Double_Window): |
| 192 |
search = "" |
| 193 |
def __init__(self, w, h, label): |
| 194 |
Fl_Double_Window.__init__(self, w, h, label) |
| 195 |
self.replace_dlg = Fl_Window(300, 105, "Replace") |
| 196 |
self.replace_find = Fl_Input(80, 10, 210, 25, "Find:") |
| 197 |
self.replace_find.align(FL_ALIGN_LEFT) |
| 198 |
|
| 199 |
self.replace_with = Fl_Input(80, 40, 210, 25, "Replace:") |
| 200 |
self.replace_with.align(FL_ALIGN_LEFT) |
| 201 |
|
| 202 |
self.replace_all = Fl_Button(10, 70, 90, 25, "Replace All") |
| 203 |
self.replace_all.callback(replall_cb, self) |
| 204 |
|
| 205 |
self.replace_next = Fl_Return_Button(105, 70, 120, 25, "Replace Next") |
| 206 |
self.replace_next.callback(replace2_cb, self) |
| 207 |
|
| 208 |
self.replace_cancel = Fl_Button(230, 70, 60, 25, "Cancel") |
| 209 |
self.replace_cancel.callback(replcan_cb, self) |
| 210 |
self.replace_dlg.end() |
| 211 |
self.replace_dlg.set_non_modal() |
| 212 |
self.editor = 0 |
| 213 |
|
| 214 |
def check_save(): |
| 215 |
global changed |
| 216 |
if not changed: |
| 217 |
return |
| 218 |
|
| 219 |
r = fl_choice("The current file has not been saved.\n" |
| 220 |
"Would you like to save it now?", |
| 221 |
"Cancel", "Save", "Don't Save") |
| 222 |
|
| 223 |
if r == 1: |
| 224 |
save_cb(None) |
| 225 |
return not changed |
| 226 |
|
| 227 |
if r == 2: |
| 228 |
return 1 |
| 229 |
else: |
| 230 |
return 0 |
| 231 |
|
| 232 |
loading = False |
| 233 |
|
| 234 |
def load_file(newfile, ipos): |
| 235 |
global changed, loading, filename |
| 236 |
loading = True |
| 237 |
if ipos != -1: |
| 238 |
insert = 1 |
| 239 |
changed = True |
| 240 |
else: |
| 241 |
insert = 0 |
| 242 |
changed = False |
| 243 |
if insert == 0: |
| 244 |
filename = "" |
| 245 |
r = textbuf.loadfile(newfile) |
| 246 |
else: |
| 247 |
r = textbuf.insertfile(newfile, ipos) |
| 248 |
if r != 0: |
| 249 |
fl_alert(f"Error reading from file {newfile}.") |
| 250 |
else: |
| 251 |
if insert == 0: |
| 252 |
filename = newfile |
| 253 |
loading = False |
| 254 |
textbuf.call_modify_callbacks() |
| 255 |
|
| 256 |
def save_file(newfile): |
| 257 |
global changed, filename |
| 258 |
if textbuf.savefile(newfile) != 0: |
| 259 |
fl_alert(f"Error writing to file {newfile}.") |
| 260 |
else: |
| 261 |
filename = newfile |
| 262 |
changed = False |
| 263 |
textbuf.call_modify_callbacks() |
| 264 |
|
| 265 |
def copy_cb(widget, editor): |
| 266 |
Fl_Text_Editor.kf_copy(0, editor.editor) |
| 267 |
|
| 268 |
def cut_cb(widget, editor): |
| 269 |
Fl_Text_Editor.kf_cut(0, editor.editor) |
| 270 |
|
| 271 |
def delete_cb(widget): |
| 272 |
global textbuf |
| 273 |
textbuf.remove_selection() |
| 274 |
|
| 275 |
def find_cb(widget, editor): |
| 276 |
val = fl_input("Search String:", editor.search) |
| 277 |
if val != None: |
| 278 |
|
| 279 |
editor.search = val |
| 280 |
find2_cb(widget) |
| 281 |
|
| 282 |
def find2_cb(widget): |
| 283 |
if editor.search[0] == 0: |
| 284 |
|
| 285 |
find_cb(widget, editor) |
| 286 |
return |
| 287 |
|
| 288 |
pos = editor.editor.insert_position(); |
| 289 |
(found, pos) = textbuf.search_forward(pos, editor.search); |
| 290 |
if found!= 0: |
| 291 |
|
| 292 |
textbuf.select(pos, pos+len(editor.search)) |
| 293 |
editor.editor.insert_position(pos+len(editor.search)) |
| 294 |
editor.editor.show_insert_position() |
| 295 |
else: |
| 296 |
fl_alert(f"No occurrences of {editor.search} found!") |
| 297 |
|
| 298 |
def set_title(win): |
| 299 |
global filename, title |
| 300 |
if len(filename) == 0: |
| 301 |
title = "Untitled" |
| 302 |
else: |
| 303 |
title = os.path.basename(filename) |
| 304 |
if changed: |
| 305 |
title = title+" (modified)" |
| 306 |
win.label(title) |
| 307 |
|
| 308 |
def changed_cb(i1, nInserted, nDeleted, i2, c1, editor): |
| 309 |
global changed, loading |
| 310 |
|
| 311 |
if (nInserted != 0 or nDeleted != 0) and loading == False: |
| 312 |
changed = True |
| 313 |
set_title(editor); |
| 314 |
if loading: |
| 315 |
editor.editor.show_insert_position() |
| 316 |
|
| 317 |
def new_cb(widget): |
| 318 |
global filename, changed |
| 319 |
if check_save() == 0: |
| 320 |
return |
| 321 |
filename = "" |
| 322 |
textbuf.select(0, textbuf.length()) |
| 323 |
textbuf.remove_selection() |
| 324 |
changed = False |
| 325 |
textbuf.call_modify_callbacks() |
| 326 |
|
| 327 |
def open_cb(widget): |
| 328 |
global filename |
| 329 |
if check_save() == 0: |
| 330 |
return |
| 331 |
newfile = fl_file_chooser("Open File?", "*", filename) |
| 332 |
if newfile != None: |
| 333 |
load_file(newfile, -1) |
| 334 |
|
| 335 |
def insert_cb(widget, editor): |
| 336 |
global filename |
| 337 |
newfile = fl_file_chooser("Open File?", "*", filename) |
| 338 |
if newfile != None: |
| 339 |
load_file(newfile, editor.editor.insert_position()) |
| 340 |
|
| 341 |
def paste_cb(widget, editor): |
| 342 |
Fl_Text_Editor.kf_paste(0, editor.editor) |
| 343 |
|
| 344 |
num_windows = 0 |
| 345 |
|
| 346 |
def close_cb(window, data): |
| 347 |
global num_windows |
| 348 |
if num_windows == 1 and check_save() == 0: |
| 349 |
return |
| 350 |
window.hide() |
| 351 |
textbuf.remove_modify_callback(changed_cb, window, window) |
| 352 |
num_windows -= 1; |
| 353 |
if num_windows == 0: |
| 354 |
sys.exit(0) |
| 355 |
|
| 356 |
def quit_cb(widget, data): |
| 357 |
global changed |
| 358 |
if changed and check_save() == 0: |
| 359 |
return |
| 360 |
sys.exit(0) |
| 361 |
|
| 362 |
def replace_cb(widget, editor): |
| 363 |
editor.replace_dlg.show() |
| 364 |
|
| 365 |
def replace2_cb(widget, editor): |
| 366 |
find = editor.replace_find.value() |
| 367 |
replace = editor.replace_with.value() |
| 368 |
|
| 369 |
if len(find) == 0: |
| 370 |
editor.replace_dlg.show() |
| 371 |
return |
| 372 |
|
| 373 |
editor.replace_dlg.hide() |
| 374 |
|
| 375 |
pos = editor.editor.insert_position() |
| 376 |
(found, pos) = textbuf.search_forward(pos, find) |
| 377 |
|
| 378 |
if found != 0: |
| 379 |
|
| 380 |
textbuf.select(pos, pos+len(find)) |
| 381 |
textbuf.remove_selection() |
| 382 |
textbuf.insert(pos, replace) |
| 383 |
textbuf.select(pos, pos+len(replace)) |
| 384 |
editor.editor.insert_position(pos+len(replace)) |
| 385 |
editor.editor.show_insert_position() |
| 386 |
else: |
| 387 |
fl_alert(f"No occurrences of {find} found!") |
| 388 |
|
| 389 |
def replall_cb(widget, editor): |
| 390 |
find = editor.replace_find.value() |
| 391 |
replace = editor.replace_with.value() |
| 392 |
|
| 393 |
if len(find) == 0: |
| 394 |
editor.replace_dlg.show() |
| 395 |
return |
| 396 |
|
| 397 |
editor.replace_dlg.hide() |
| 398 |
editor.editor.insert_position(0) |
| 399 |
times = 0 |
| 400 |
|
| 401 |
found = 1 |
| 402 |
while found != 0: |
| 403 |
pos = editor.editor.insert_position() |
| 404 |
(found, pos) = textbuf.search_forward(pos, find) |
| 405 |
|
| 406 |
if found != 0: |
| 407 |
|
| 408 |
textbuf.select(pos, pos+len(find)) |
| 409 |
textbuf.remove_selection() |
| 410 |
textbuf.insert(pos, replace) |
| 411 |
editor.editor.insert_position(pos+len(replace)) |
| 412 |
editor.editor.show_insert_position() |
| 413 |
times += 1 |
| 414 |
|
| 415 |
if times > 0: |
| 416 |
fl_message(f"Replaced {times} occurrences.") |
| 417 |
else: |
| 418 |
fl_alert(f"No occurrences of {find} found!") |
| 419 |
|
| 420 |
def replcan_cb(widget, editor): |
| 421 |
editor.replace_dlg.hide() |
| 422 |
|
| 423 |
def save_cb(widget): |
| 424 |
global filename |
| 425 |
if len(filename) == 0: |
| 426 |
|
| 427 |
saveas_cb(widget, 0) |
| 428 |
return |
| 429 |
else: |
| 430 |
save_file(filename) |
| 431 |
|
| 432 |
def saveas_cb(widget, data): |
| 433 |
global filename |
| 434 |
newfile = fl_file_chooser("Save File As?", "*", filename) |
| 435 |
if newfile != None: |
| 436 |
save_file(newfile) |
| 437 |
|
| 438 |
def view_cb(widget, data): |
| 439 |
win = new_view() |
| 440 |
win.show() |
| 441 |
|
| 442 |
def new_view(): |
| 443 |
global num_windows, editor, stylebuf, styletable |
| 444 |
w = EditorWindow(660, 400, title) |
| 445 |
editor = w |
| 446 |
w.begin() |
| 447 |
m = Fl_Menu_Bar(0, 0, 660, 30); |
| 448 |
menuitems = (( "&File", 0, 0, 0, FL_SUBMENU ), |
| 449 |
( "&New File", 0, new_cb ), |
| 450 |
( "&Open File...", FL_CTRL + ord('o'), open_cb ), |
| 451 |
( "&Insert File...", FL_CTRL + ord('i'), insert_cb, editor, FL_MENU_DIVIDER ), |
| 452 |
( "&Save File", FL_CTRL + ord('s'), save_cb ), |
| 453 |
( "Save File &As...", FL_CTRL + FL_SHIFT + ord('s'), saveas_cb, 0, FL_MENU_DIVIDER ), |
| 454 |
( "New &View", FL_ALT + ord('v'),view_cb, 0 ), |
| 455 |
( "&Close View", FL_CTRL + ord('w'), close_cb, window, FL_MENU_DIVIDER ), |
| 456 |
( "E&xit", FL_CTRL + ord('q'), quit_cb, 0 ), |
| 457 |
( None, 0 ), |
| 458 |
|
| 459 |
( "&Edit", 0, 0, 0, FL_SUBMENU ), |
| 460 |
( "Cu&t", FL_CTRL + ord('x'), cut_cb, editor ), |
| 461 |
( "&Copy", FL_CTRL + ord('c'), copy_cb, editor ), |
| 462 |
( "&Paste", FL_CTRL + ord('v'), paste_cb, editor ), |
| 463 |
( "&Delete", 0, delete_cb ), |
| 464 |
( None, 0 ), |
| 465 |
|
| 466 |
( "&Search", 0, 0, 0, FL_SUBMENU ), |
| 467 |
( "&Find...", FL_CTRL + ord('f'), find_cb, editor ), |
| 468 |
( "F&ind Again", FL_CTRL + ord('g'), find2_cb ), |
| 469 |
( "&Replace...", FL_CTRL + ord('r'), replace_cb, editor ), |
| 470 |
( "Re&place Again", FL_CTRL + ord('t'), replace2_cb, editor ), |
| 471 |
( None, 0 ), |
| 472 |
|
| 473 |
( None, 0 ) |
| 474 |
) |
| 475 |
m.copy(menuitems) |
| 476 |
|
| 477 |
w.editor = Fl_Text_Editor(0, 30, 660, 370) |
| 478 |
w.editor.buffer(textbuf) |
| 479 |
w.editor.highlight_data(stylebuf, styletable, |
| 480 |
len(styletable), |
| 481 |
|
| 482 |
'A', style_unfinished_cb, w.editor) |
| 483 |
w.editor.textfont(FL_COURIER) |
| 484 |
w.end() |
| 485 |
w.resizable(w.editor) |
| 486 |
w.callback(close_cb, w) |
| 487 |
textbuf.add_modify_callback(style_update, w.editor) |
| 488 |
textbuf.add_modify_callback(changed_cb, w) |
| 489 |
textbuf.call_modify_callbacks() |
| 490 |
num_windows += 1 |
| 491 |
return w |
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
textbuf = Fl_Text_Buffer() |
| 496 |
style_init() |
| 497 |
|
| 498 |
window = new_view() |
| 499 |
window.show(1, sys.argv) |
| 500 |
|
| 501 |
if len(sys.argv) > 1: |
| 502 |
load_file(sys.argv[1], -1) |
| 503 |
|
| 504 |
Fl.run() |