editor.cxx 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  1. //
  2. // "$Id: editor.cxx 9718 2012-11-13 13:03:20Z manolo $"
  3. //
  4. // A simple text editor program for the Fast Light Tool Kit (FLTK).
  5. //
  6. // This program is described in Chapter 4 of the FLTK Programmer's Guide.
  7. //
  8. // Copyright 1998-2010 by Bill Spitzak and others.
  9. //
  10. // This library is free software. Distribution and use rights are outlined in
  11. // the file "COPYING" which should have been included with this file. If this
  12. // file is missing or damaged, see the license at:
  13. //
  14. // http://www.fltk.org/COPYING.php
  15. //
  16. // Please report all bugs and problems on the following page:
  17. //
  18. // http://www.fltk.org/str.php
  19. //
  20. //
  21. // Include necessary headers...
  22. //
  23. #include <stdarg.h>
  24. #include <squirrel.h>
  25. #include <sqstdblob.h>
  26. #include <sqstdsystem.h>
  27. #include <sqstdio.h>
  28. #include <sqstdmath.h>
  29. #include <sqstdstring.h>
  30. #include <sqstdaux.h>
  31. #ifdef SQUNICODE
  32. #define scfprintf fwprintf
  33. #define scfopen _wfopen
  34. #define scvprintf vfwprintf
  35. #else
  36. #define scfprintf fprintf
  37. #define scfopen fopen
  38. #define scvprintf vfprintf
  39. #endif
  40. static HSQUIRRELVM v;
  41. static bool _debug_wait = false;
  42. static bool _stop_debug = false;
  43. static int _debug_pos = 0;
  44. static void sq_debug_hook(HSQUIRRELVM v, SQInteger type, const SQChar *sourcename, SQInteger line, const SQChar *funcname);
  45. #include <stdio.h>
  46. #include <stdlib.h>
  47. #include <string.h>
  48. #include <ctype.h>
  49. #include <errno.h>
  50. #ifdef __MWERKS__
  51. # define FL_DLL
  52. #endif
  53. #include <FL/Fl.H>
  54. #include <FL/Fl_Group.H>
  55. #include <FL/Fl_Double_Window.H>
  56. #include <FL/fl_ask.H>
  57. #include <FL/Fl_Native_File_Chooser.H>
  58. #include <FL/Fl_Menu_Bar.H>
  59. #include <FL/Fl_Input.H>
  60. #include <FL/Fl_Button.H>
  61. #include <FL/Fl_Return_Button.H>
  62. #include <FL/Fl_Text_Buffer.H>
  63. #include <FL/Fl_Text_Editor.H>
  64. #include <FL/filename.H>
  65. int changed = 0;
  66. char filename[FL_PATH_MAX] = "";
  67. char title[FL_PATH_MAX];
  68. Fl_Text_Buffer *textbuf = 0;
  69. // Syntax highlighting stuff...
  70. #define TS 14 // default editor textsize
  71. Fl_Text_Buffer *stylebuf = 0;
  72. Fl_Text_Display::Style_Table_Entry
  73. styletable[] = { // Style table
  74. { FL_BLACK, FL_COURIER, TS }, // A - Plain
  75. { FL_DARK_GREEN, FL_HELVETICA_ITALIC, TS }, // B - Line comments
  76. { FL_DARK_GREEN, FL_HELVETICA_ITALIC, TS }, // C - Block comments
  77. { FL_BLUE, FL_COURIER, TS }, // D - Strings
  78. { FL_DARK_RED, FL_COURIER, TS }, // E - Directives
  79. { FL_DARK_RED, FL_COURIER_BOLD, TS }, // F - Types
  80. { FL_BLUE, FL_COURIER_BOLD, TS }, // G - Keywords
  81. };
  82. const char *code_keywords[] = { // List of known C/C++ keywords...
  83. "and",
  84. "and_eq",
  85. "array",
  86. "asm",
  87. "bitand",
  88. "bitor",
  89. "break",
  90. "case",
  91. "catch",
  92. "compl",
  93. "continue",
  94. "default",
  95. "delete",
  96. "do",
  97. "else",
  98. "false",
  99. "for",
  100. "foreach",
  101. "function",
  102. "goto",
  103. "if",
  104. "new",
  105. "not",
  106. "not_eq",
  107. "operator",
  108. "or",
  109. "or_eq",
  110. "return",
  111. "switch",
  112. "table",
  113. "template",
  114. "this",
  115. "throw",
  116. "true",
  117. "try",
  118. "while",
  119. "xor",
  120. "xor_eq"
  121. };
  122. const char *code_types[] = { // List of known C/C++ types...
  123. "auto",
  124. "bool",
  125. "char",
  126. "class",
  127. "const",
  128. "const_cast",
  129. "double",
  130. "dynamic_cast",
  131. "enum",
  132. "explicit",
  133. "extern",
  134. "float",
  135. "friend",
  136. "inline",
  137. "int",
  138. "local",
  139. "long",
  140. "mutable",
  141. "namespace",
  142. "private",
  143. "protected",
  144. "public",
  145. "register",
  146. "short",
  147. "signed",
  148. "sizeof",
  149. "static",
  150. "static_cast",
  151. "struct",
  152. "template",
  153. "typedef",
  154. "typename",
  155. "union",
  156. "unsigned",
  157. "virtual",
  158. "void",
  159. "volatile"
  160. };
  161. //
  162. // 'compare_keywords()' - Compare two keywords...
  163. //
  164. extern "C" {
  165. int
  166. compare_keywords(const void *a,
  167. const void *b) {
  168. return (strcmp(*((const char **)a), *((const char **)b)));
  169. }
  170. }
  171. //
  172. // 'style_parse()' - Parse text and produce style data.
  173. //
  174. void
  175. style_parse(const char *text,
  176. char *style,
  177. int length) {
  178. char current;
  179. int col;
  180. int last;
  181. char buf[255],
  182. *bufptr;
  183. const char *temp;
  184. // Style letters:
  185. //
  186. // A - Plain
  187. // B - Line comments
  188. // C - Block comments
  189. // D - Strings
  190. // E - Directives
  191. // F - Types
  192. // G - Keywords
  193. for (current = *style, col = 0, last = 0; length > 0; length --, text ++) {
  194. if (current == 'B' || current == 'F' || current == 'G') current = 'A';
  195. if (current == 'A') {
  196. // Check for directives, comments, strings, and keywords...
  197. if (col == 0 && *text == '#') {
  198. // Set style to directive
  199. current = 'E';
  200. } else if (strncmp(text, "//", 2) == 0) {
  201. current = 'B';
  202. for (; length > 0 && *text != '\n'; length --, text ++) *style++ = 'B';
  203. if (length == 0) break;
  204. } else if (strncmp(text, "/*", 2) == 0) {
  205. current = 'C';
  206. } else if (strncmp(text, "\\\"", 2) == 0) {
  207. // Quoted quote...
  208. *style++ = current;
  209. *style++ = current;
  210. text ++;
  211. length --;
  212. col += 2;
  213. continue;
  214. } else if (*text == '\"') {
  215. current = 'D';
  216. } else if (!last && (islower((*text)&255) || *text == '_')) {
  217. // Might be a keyword...
  218. for (temp = text, bufptr = buf;
  219. (islower((*temp)&255) || *temp == '_') && bufptr < (buf + sizeof(buf) - 1);
  220. *bufptr++ = *temp++);
  221. if (!islower((*temp)&255) && *temp != '_') {
  222. *bufptr = '\0';
  223. bufptr = buf;
  224. if (bsearch(&bufptr, code_types,
  225. sizeof(code_types) / sizeof(code_types[0]),
  226. sizeof(code_types[0]), compare_keywords)) {
  227. while (text < temp) {
  228. *style++ = 'F';
  229. text ++;
  230. length --;
  231. col ++;
  232. }
  233. text --;
  234. length ++;
  235. last = 1;
  236. continue;
  237. } else if (bsearch(&bufptr, code_keywords,
  238. sizeof(code_keywords) / sizeof(code_keywords[0]),
  239. sizeof(code_keywords[0]), compare_keywords)) {
  240. while (text < temp) {
  241. *style++ = 'G';
  242. text ++;
  243. length --;
  244. col ++;
  245. }
  246. text --;
  247. length ++;
  248. last = 1;
  249. continue;
  250. }
  251. }
  252. }
  253. } else if (current == 'C' && strncmp(text, "*/", 2) == 0) {
  254. // Close a C comment...
  255. *style++ = current;
  256. *style++ = current;
  257. text ++;
  258. length --;
  259. current = 'A';
  260. col += 2;
  261. continue;
  262. } else if (current == 'D') {
  263. // Continuing in string...
  264. if (strncmp(text, "\\\"", 2) == 0) {
  265. // Quoted end quote...
  266. *style++ = current;
  267. *style++ = current;
  268. text ++;
  269. length --;
  270. col += 2;
  271. continue;
  272. } else if (*text == '\"') {
  273. // End quote...
  274. *style++ = current;
  275. col ++;
  276. current = 'A';
  277. continue;
  278. }
  279. }
  280. // Copy style info...
  281. if (current == 'A' && (*text == '{' || *text == '}')) *style++ = 'G';
  282. else *style++ = current;
  283. col ++;
  284. last = isalnum((*text)&255) || *text == '_' || *text == '.';
  285. if (*text == '\n') {
  286. // Reset column and possibly reset the style
  287. col = 0;
  288. if (current == 'B' || current == 'E') current = 'A';
  289. }
  290. }
  291. }
  292. //
  293. // 'style_init()' - Initialize the style buffer...
  294. //
  295. void
  296. style_init(void) {
  297. char *style = new char[textbuf->length() + 1];
  298. char *text = textbuf->text();
  299. memset(style, 'A', textbuf->length());
  300. style[textbuf->length()] = '\0';
  301. if (!stylebuf) stylebuf = new Fl_Text_Buffer(textbuf->length());
  302. style_parse(text, style, textbuf->length());
  303. stylebuf->text(style);
  304. delete[] style;
  305. free(text);
  306. }
  307. //
  308. // 'style_unfinished_cb()' - Update unfinished styles.
  309. //
  310. void
  311. style_unfinished_cb(int, void*) {
  312. }
  313. //
  314. // 'style_update()' - Update the style buffer...
  315. //
  316. void
  317. style_update(int pos, // I - Position of update
  318. int nInserted, // I - Number of inserted chars
  319. int nDeleted, // I - Number of deleted chars
  320. int /*nRestyled*/, // I - Number of restyled chars
  321. const char * /*deletedText*/,// I - Text that was deleted
  322. void *cbArg) { // I - Callback data
  323. int start, // Start of text
  324. end; // End of text
  325. char last, // Last style on line
  326. *style, // Style data
  327. *text; // Text data
  328. // If this is just a selection change, just unselect the style buffer...
  329. if (nInserted == 0 && nDeleted == 0) {
  330. stylebuf->unselect();
  331. return;
  332. }
  333. // Track changes in the text buffer...
  334. if (nInserted > 0) {
  335. // Insert characters into the style buffer...
  336. style = new char[nInserted + 1];
  337. memset(style, 'A', nInserted);
  338. style[nInserted] = '\0';
  339. stylebuf->replace(pos, pos + nDeleted, style);
  340. delete[] style;
  341. } else {
  342. // Just delete characters in the style buffer...
  343. stylebuf->remove(pos, pos + nDeleted);
  344. }
  345. // Select the area that was just updated to avoid unnecessary
  346. // callbacks...
  347. stylebuf->select(pos, pos + nInserted - nDeleted);
  348. // Re-parse the changed region; we do this by parsing from the
  349. // beginning of the previous line of the changed region to the end of
  350. // the line of the changed region... Then we check the last
  351. // style character and keep updating if we have a multi-line
  352. // comment character...
  353. start = textbuf->line_start(pos);
  354. // if (start > 0) start = textbuf->line_start(start - 1);
  355. end = textbuf->line_end(pos + nInserted);
  356. text = textbuf->text_range(start, end);
  357. style = stylebuf->text_range(start, end);
  358. if (start==end)
  359. last = 0;
  360. else
  361. last = style[end - start - 1];
  362. // printf("start = %d, end = %d, text = \"%s\", style = \"%s\", last='%c'...\n",
  363. // start, end, text, style, last);
  364. style_parse(text, style, end - start);
  365. // printf("new style = \"%s\", new last='%c'...\n",
  366. // style, style[end - start - 1]);
  367. stylebuf->replace(start, end, style);
  368. ((Fl_Text_Editor *)cbArg)->redisplay_range(start, end);
  369. if (start==end || last != style[end - start - 1]) {
  370. // printf("Recalculate the rest of the buffer style\n");
  371. // Either the user deleted some text, or the last character
  372. // on the line changed styles, so reparse the
  373. // remainder of the buffer...
  374. free(text);
  375. free(style);
  376. end = textbuf->length();
  377. text = textbuf->text_range(start, end);
  378. style = stylebuf->text_range(start, end);
  379. style_parse(text, style, end - start);
  380. stylebuf->replace(start, end, style);
  381. ((Fl_Text_Editor *)cbArg)->redisplay_range(start, end);
  382. }
  383. free(text);
  384. free(style);
  385. }
  386. // Editor window functions and class...
  387. void save_cb();
  388. void saveas_cb();
  389. void find2_cb(Fl_Widget*, void*);
  390. void replall_cb(Fl_Widget*, void*);
  391. void replace2_cb(Fl_Widget*, void*);
  392. void replcan_cb(Fl_Widget*, void*);
  393. class EditorWindow : public Fl_Double_Window {
  394. public:
  395. EditorWindow(int w, int h, const char* t);
  396. ~EditorWindow();
  397. Fl_Window *replace_dlg;
  398. Fl_Input *replace_find;
  399. Fl_Input *replace_with;
  400. Fl_Button *replace_all;
  401. Fl_Return_Button *replace_next;
  402. Fl_Button *replace_cancel;
  403. Fl_Text_Editor *editor;
  404. char search[256];
  405. };
  406. EditorWindow::EditorWindow(int w, int h, const char* t) : Fl_Double_Window(w, h, t) {
  407. replace_dlg = new Fl_Window(300, 105, "Replace");
  408. replace_find = new Fl_Input(80, 10, 210, 25, "Find:");
  409. replace_find->align(FL_ALIGN_LEFT);
  410. replace_with = new Fl_Input(80, 40, 210, 25, "Replace:");
  411. replace_with->align(FL_ALIGN_LEFT);
  412. replace_all = new Fl_Button(10, 70, 90, 25, "Replace All");
  413. replace_all->callback((Fl_Callback *)replall_cb, this);
  414. replace_next = new Fl_Return_Button(105, 70, 120, 25, "Replace Next");
  415. replace_next->callback((Fl_Callback *)replace2_cb, this);
  416. replace_cancel = new Fl_Button(230, 70, 60, 25, "Cancel");
  417. replace_cancel->callback((Fl_Callback *)replcan_cb, this);
  418. replace_dlg->end();
  419. replace_dlg->set_non_modal();
  420. editor = 0;
  421. *search = (char)0;
  422. }
  423. EditorWindow::~EditorWindow() {
  424. delete replace_dlg;
  425. }
  426. int check_save(void) {
  427. if (!changed) return 1;
  428. int r = fl_choice("The current file has not been saved.\n"
  429. "Would you like to save it now?",
  430. "Cancel", "Save", "Don't Save");
  431. if (r == 1) {
  432. save_cb(); // Save the file...
  433. return !changed;
  434. }
  435. return (r == 2) ? 1 : 0;
  436. }
  437. int loading = 0;
  438. void load_file(const char *newfile, int ipos) {
  439. loading = 1;
  440. int insert = (ipos != -1);
  441. changed = insert;
  442. if (!insert) strcpy(filename, "");
  443. int r;
  444. if (!insert) r = textbuf->loadfile(newfile);
  445. else r = textbuf->insertfile(newfile, ipos);
  446. changed = changed || textbuf->input_file_was_transcoded;
  447. if (r)
  448. fl_alert("Error reading from file \'%s\':\n%s.", newfile, strerror(errno));
  449. else
  450. if (!insert) strcpy(filename, newfile);
  451. loading = 0;
  452. textbuf->call_modify_callbacks();
  453. }
  454. void save_file(const char *newfile) {
  455. if (textbuf->savefile(newfile))
  456. fl_alert("Error writing to file \'%s\':\n%s.", newfile, strerror(errno));
  457. else
  458. strcpy(filename, newfile);
  459. changed = 0;
  460. textbuf->call_modify_callbacks();
  461. }
  462. void copy_cb(Fl_Widget*, void* v) {
  463. EditorWindow* e = (EditorWindow*)v;
  464. Fl_Text_Editor::kf_copy(0, e->editor);
  465. }
  466. void cut_cb(Fl_Widget*, void* v) {
  467. EditorWindow* e = (EditorWindow*)v;
  468. Fl_Text_Editor::kf_cut(0, e->editor);
  469. }
  470. void delete_cb(Fl_Widget*, void*) {
  471. textbuf->remove_selection();
  472. }
  473. void find_cb(Fl_Widget* w, void* v) {
  474. EditorWindow* e = (EditorWindow*)v;
  475. const char *val;
  476. val = fl_input("Search String:", e->search);
  477. if (val != NULL) {
  478. // User entered a string - go find it!
  479. strcpy(e->search, val);
  480. find2_cb(w, v);
  481. }
  482. }
  483. void find2_cb(Fl_Widget* w, void* v) {
  484. EditorWindow* e = (EditorWindow*)v;
  485. if (e->search[0] == '\0') {
  486. // Search string is blank; get a new one...
  487. find_cb(w, v);
  488. return;
  489. }
  490. int pos = e->editor->insert_position();
  491. int found = textbuf->search_forward(pos, e->search, &pos);
  492. if (found) {
  493. // Found a match; select and update the position...
  494. textbuf->select(pos, pos+strlen(e->search));
  495. e->editor->insert_position(pos+strlen(e->search));
  496. e->editor->show_insert_position();
  497. }
  498. else fl_alert("No occurrences of \'%s\' found!", e->search);
  499. }
  500. void set_title(Fl_Window* w) {
  501. if (filename[0] == '\0') strcpy(title, "Untitled");
  502. else {
  503. char *slash;
  504. slash = strrchr(filename, '/');
  505. #ifdef WIN32
  506. if (slash == NULL) slash = strrchr(filename, '\\');
  507. #endif
  508. if (slash != NULL) strcpy(title, slash + 1);
  509. else strcpy(title, filename);
  510. }
  511. if (changed) strcat(title, " (modified)");
  512. w->label(title);
  513. }
  514. void changed_cb(int, int nInserted, int nDeleted,int, const char*, void* v) {
  515. if ((nInserted || nDeleted) && !loading) changed = 1;
  516. EditorWindow *w = (EditorWindow *)v;
  517. set_title(w);
  518. if (loading) w->editor->show_insert_position();
  519. }
  520. void new_cb(Fl_Widget*, void*) {
  521. if (!check_save()) return;
  522. filename[0] = '\0';
  523. textbuf->select(0, textbuf->length());
  524. textbuf->remove_selection();
  525. changed = 0;
  526. textbuf->call_modify_callbacks();
  527. }
  528. void open_cb(Fl_Widget*, void*) {
  529. if (!check_save()) return;
  530. Fl_Native_File_Chooser fnfc;
  531. fnfc.title("Open file");
  532. fnfc.type(Fl_Native_File_Chooser::BROWSE_FILE);
  533. if ( fnfc.show() ) return;
  534. load_file(fnfc.filename(), -1);
  535. }
  536. void insert_cb(Fl_Widget*, void *v) {
  537. Fl_Native_File_Chooser fnfc;
  538. fnfc.title("Insert file");
  539. fnfc.type(Fl_Native_File_Chooser::BROWSE_FILE);
  540. if ( fnfc.show() ) return;
  541. EditorWindow *w = (EditorWindow *)v;
  542. load_file(fnfc.filename(), w->editor->insert_position());
  543. }
  544. void paste_cb(Fl_Widget*, void* v) {
  545. EditorWindow* e = (EditorWindow*)v;
  546. Fl_Text_Editor::kf_paste(0, e->editor);
  547. }
  548. int num_windows = 0;
  549. void close_cb(Fl_Widget*, void* v) {
  550. EditorWindow* w = (EditorWindow*)v;
  551. if (num_windows == 1) {
  552. if (!check_save())
  553. return;
  554. }
  555. w->hide();
  556. w->editor->buffer(0);
  557. textbuf->remove_modify_callback(style_update, w->editor);
  558. textbuf->remove_modify_callback(changed_cb, w);
  559. Fl::delete_widget(w);
  560. num_windows--;
  561. if (!num_windows) exit(0);
  562. }
  563. void quit_cb(Fl_Widget*, void*) {
  564. if (changed && !check_save())
  565. return;
  566. exit(0);
  567. }
  568. void replace_cb(Fl_Widget*, void* v) {
  569. EditorWindow* e = (EditorWindow*)v;
  570. e->replace_dlg->show();
  571. }
  572. void replace2_cb(Fl_Widget*, void* v) {
  573. EditorWindow* e = (EditorWindow*)v;
  574. const char *find = e->replace_find->value();
  575. const char *replace = e->replace_with->value();
  576. if (find[0] == '\0') {
  577. // Search string is blank; get a new one...
  578. e->replace_dlg->show();
  579. return;
  580. }
  581. e->replace_dlg->hide();
  582. int pos = e->editor->insert_position();
  583. int found = textbuf->search_forward(pos, find, &pos);
  584. if (found) {
  585. // Found a match; update the position and replace text...
  586. textbuf->select(pos, pos+strlen(find));
  587. textbuf->remove_selection();
  588. textbuf->insert(pos, replace);
  589. textbuf->select(pos, pos+strlen(replace));
  590. e->editor->insert_position(pos+strlen(replace));
  591. e->editor->show_insert_position();
  592. }
  593. else fl_alert("No occurrences of \'%s\' found!", find);
  594. }
  595. void replall_cb(Fl_Widget*, void* v) {
  596. EditorWindow* e = (EditorWindow*)v;
  597. const char *find = e->replace_find->value();
  598. const char *replace = e->replace_with->value();
  599. find = e->replace_find->value();
  600. if (find[0] == '\0') {
  601. // Search string is blank; get a new one...
  602. e->replace_dlg->show();
  603. return;
  604. }
  605. e->replace_dlg->hide();
  606. e->editor->insert_position(0);
  607. int times = 0;
  608. // Loop through the whole string
  609. for (int found = 1; found;) {
  610. int pos = e->editor->insert_position();
  611. found = textbuf->search_forward(pos, find, &pos);
  612. if (found) {
  613. // Found a match; update the position and replace text...
  614. textbuf->select(pos, pos+strlen(find));
  615. textbuf->remove_selection();
  616. textbuf->insert(pos, replace);
  617. e->editor->insert_position(pos+strlen(replace));
  618. e->editor->show_insert_position();
  619. times++;
  620. }
  621. }
  622. if (times) fl_message("Replaced %d occurrences.", times);
  623. else fl_alert("No occurrences of \'%s\' found!", find);
  624. }
  625. void replcan_cb(Fl_Widget*, void* v) {
  626. EditorWindow* e = (EditorWindow*)v;
  627. e->replace_dlg->hide();
  628. }
  629. void save_cb() {
  630. if (filename[0] == '\0') {
  631. // No filename - get one!
  632. saveas_cb();
  633. return;
  634. }
  635. else save_file(filename);
  636. }
  637. void saveas_cb() {
  638. Fl_Native_File_Chooser fnfc;
  639. fnfc.title("Save File As?");
  640. fnfc.type(Fl_Native_File_Chooser::BROWSE_SAVE_FILE);
  641. if ( fnfc.show() ) return;
  642. save_file(fnfc.filename());
  643. }
  644. void run_cb() {
  645. if (filename[0]) {
  646. sq_setnativedebughook(v, NULL);
  647. sq_enabledebuginfo(v, 0);
  648. sqstd_dofile(v, filename, SQFalse, SQTrue);
  649. }
  650. }
  651. #define DBG_EDITOR_WINDOW_KEY "DebugEditorWindow"
  652. void debug_cb(Fl_Widget*, void* ew) {
  653. if(_debug_wait){
  654. _debug_wait = false;
  655. return;
  656. }
  657. if (filename[0]) {
  658. _debug_wait = true;
  659. _stop_debug = false;
  660. _debug_pos = 0;
  661. sq_pushregistrytable(v);
  662. sq_pushstring(v, DBG_EDITOR_WINDOW_KEY, -1);
  663. sq_pushuserpointer(v, ew);
  664. sq_rawset(v, -3);
  665. sq_poptop(v);
  666. sq_setnativedebughook(v, sq_debug_hook);
  667. sq_enabledebuginfo(v, 1);
  668. sqstd_dofile(v, filename, SQFalse, SQTrue);
  669. }
  670. }
  671. void stop_debug_cb(Fl_Widget*, void* ew) {
  672. _stop_debug = true;
  673. }
  674. Fl_Window* new_view();
  675. void view_cb(Fl_Widget*, void*) {
  676. Fl_Window* w = new_view();
  677. w->show();
  678. }
  679. Fl_Menu_Item menuitems[] = {
  680. { "&File", 0, 0, 0, FL_SUBMENU },
  681. { "&New File", 0, (Fl_Callback *)new_cb },
  682. { "&Open File...", FL_COMMAND + 'o', (Fl_Callback *)open_cb },
  683. { "&Insert File...", FL_COMMAND + 'i', (Fl_Callback *)insert_cb, 0, FL_MENU_DIVIDER },
  684. { "&Save File", FL_COMMAND + 's', (Fl_Callback *)save_cb },
  685. { "Save File &As...", FL_COMMAND + FL_SHIFT + 's', (Fl_Callback *)saveas_cb, 0, FL_MENU_DIVIDER },
  686. { "New &View", FL_ALT
  687. #ifdef __APPLE__
  688. + FL_COMMAND
  689. #endif
  690. + 'v', (Fl_Callback *)view_cb, 0 },
  691. { "&Close View", FL_COMMAND + 'w', (Fl_Callback *)close_cb, 0, FL_MENU_DIVIDER },
  692. { "E&xit", FL_COMMAND + 'q', (Fl_Callback *)quit_cb, 0 },
  693. { 0 },
  694. { "&Edit", 0, 0, 0, FL_SUBMENU },
  695. { "Cu&t", FL_COMMAND + 'x', (Fl_Callback *)cut_cb },
  696. { "&Copy", FL_COMMAND + 'c', (Fl_Callback *)copy_cb },
  697. { "&Paste", FL_COMMAND + 'v', (Fl_Callback *)paste_cb },
  698. { "&Delete", 0, (Fl_Callback *)delete_cb },
  699. { 0 },
  700. { "&Search", 0, 0, 0, FL_SUBMENU },
  701. { "&Find...", FL_COMMAND + 'f', (Fl_Callback *)find_cb },
  702. { "F&ind Again", FL_COMMAND + 'g', find2_cb },
  703. { "&Replace...", FL_COMMAND + 'r', replace_cb },
  704. { "Re&place Again", FL_COMMAND + 't', replace2_cb },
  705. { 0 },
  706. { "Run F5", FL_F+5, (Fl_Callback *)run_cb },
  707. { "Debug F8", FL_F+8, debug_cb },
  708. { "Stop Debug", 0, stop_debug_cb },
  709. { 0 }
  710. };
  711. Fl_Window* new_view() {
  712. EditorWindow* w = new EditorWindow(660, 400, title);
  713. w->begin();
  714. Fl_Menu_Bar* m = new Fl_Menu_Bar(0, 0, 660, 30);
  715. m->copy(menuitems, w);
  716. w->editor = new Fl_Text_Editor(0, 30, 660, 370);
  717. w->editor->textfont(FL_COURIER);
  718. w->editor->textsize(TS);
  719. //w->editor->wrap_mode(Fl_Text_Editor::WRAP_AT_BOUNDS, 250);
  720. w->editor->buffer(textbuf);
  721. w->editor->highlight_data(stylebuf, styletable,
  722. sizeof(styletable) / sizeof(styletable[0]),
  723. 'A', style_unfinished_cb, 0);
  724. textbuf->text();
  725. style_init();
  726. w->end();
  727. w->resizable(w->editor);
  728. w->callback((Fl_Callback *)close_cb, w);
  729. textbuf->add_modify_callback(style_update, w->editor);
  730. textbuf->add_modify_callback(changed_cb, w);
  731. textbuf->call_modify_callbacks();
  732. num_windows++;
  733. return w;
  734. }
  735. void cb(const char *fname) {
  736. load_file(fname, -1);
  737. }
  738. void printfunc(HSQUIRRELVM v,const SQChar *s,...)
  739. {
  740. va_list vl;
  741. va_start(vl, s);
  742. scvprintf(stdout, s, vl);
  743. va_end(vl);
  744. }
  745. void errorfunc(HSQUIRRELVM v,const SQChar *s,...)
  746. {
  747. va_list vl;
  748. va_start(vl, s);
  749. scvprintf(stderr, s, vl);
  750. va_end(vl);
  751. }
  752. extern "C" {
  753. SQRESULT sqext_register_sqfs(HSQUIRRELVM v);
  754. SQRESULT sqext_register_sq_zmq3(HSQUIRRELVM v);
  755. SQRESULT sqext_register_sq_socket(HSQUIRRELVM v);
  756. SQRESULT sqext_register_sq_zlib(HSQUIRRELVM v);
  757. SQRESULT sqext_register_mongoose(HSQUIRRELVM v);
  758. SQRESULT sqrat_register_importlib(HSQUIRRELVM v);
  759. SQRESULT sqext_register_sq_slave_vm(HSQUIRRELVM v);
  760. SQRESULT sqext_register_axtls (HSQUIRRELVM v);
  761. SQRESULT sqext_register_base64(HSQUIRRELVM v);
  762. SQRESULT sqext_register_Sq_Fpdf(HSQUIRRELVM v);
  763. SQRESULT sqext_register_SQLite3(HSQUIRRELVM v);
  764. SQRESULT sqext_register_mix (HSQUIRRELVM sqvm);
  765. SQRESULT sqext_register_rs232(HSQUIRRELVM v);
  766. SQRESULT sqext_register_tinyxml2(HSQUIRRELVM v);
  767. SQRESULT sqext_register_decimal(HSQUIRRELVM v);
  768. SQRESULT sqext_register_fltklib(HSQUIRRELVM v);
  769. SQRESULT sqext_register_markdown(HSQUIRRELVM v);
  770. }
  771. int sq_main_argc = 0;
  772. char** sq_main_argv = 0;
  773. static void sq_debug_hook(HSQUIRRELVM v, SQInteger ev_type, const SQChar *sourcename, SQInteger line, const SQChar *funcname){
  774. if(_stop_debug) return;
  775. const SQChar *fname = funcname ? funcname : "unknown";
  776. printf("evt %d %c\n", ev_type, ev_type);
  777. switch(ev_type){
  778. case 'l': {//called every line(that contains some code)
  779. printf("LINE line [%d] func [%s] l\n", line, fname);
  780. EditorWindow *w=0;
  781. SQInteger top = sq_gettop(v);
  782. sq_pushstring(v, DBG_EDITOR_WINDOW_KEY, -1);
  783. if(sq_getonregistrytable(v) == SQ_OK){
  784. sq_getuserpointer(v, -1, (SQUserPointer*)&w);
  785. }
  786. sq_settop(v, top);
  787. int pos = textbuf->skip_lines(0, line-1);
  788. textbuf->select(pos, textbuf->line_end(pos));
  789. if(w){
  790. w->editor->insert_position(pos);
  791. w->editor->show_insert_position();
  792. }
  793. sqstd_printcallstack(v);
  794. }
  795. break;
  796. case 'c': //called when a function has been called
  797. printf("LINE line [%d] func [%s] c\n", line, fname);
  798. break;
  799. case 'r': //called when a function returns
  800. printf("LINE line [%d] func [%s] r\n", line, fname);
  801. break;
  802. }
  803. while(_debug_wait && !_stop_debug){
  804. Fl::wait(0.01);
  805. }
  806. _debug_wait = true;
  807. }
  808. int main(int argc, char **argv) {
  809. sq_main_argc = argc;
  810. sq_main_argv = argv;
  811. v=sq_open(1024);
  812. sq_setprintfunc(v,printfunc,errorfunc);
  813. sq_pushroottable(v);
  814. sqstd_register_bloblib(v);
  815. sqstd_register_iolib(v);
  816. sqstd_register_systemlib(v);
  817. sqstd_register_mathlib(v);
  818. sqstd_register_stringlib(v);
  819. #ifdef WITH_DAD_EXTRAS
  820. sqext_register_base64(v);
  821. sqext_register_Sq_Fpdf(v);
  822. sqext_register_SQLite3(v);
  823. //sqext_register_dad_utils(v);
  824. sqext_register_mix(v);
  825. sqext_register_sqfs(v);
  826. sqext_register_sq_socket(v);
  827. sqext_register_axtls(v);
  828. sqext_register_sq_zlib(v);
  829. sqext_register_mongoose(v);
  830. sqrat_register_importlib(v);
  831. sqext_register_sq_slave_vm(v);
  832. sqext_register_tinyxml2(v);
  833. sqext_register_decimal(v);
  834. sqext_register_markdown(v);
  835. sqext_register_sq_zmq3(v);
  836. sqext_register_rs232(v);
  837. #ifdef WITH_FLTK
  838. sqext_register_fltklib(v);
  839. #endif
  840. #endif
  841. //aux library
  842. //sets error handlers
  843. sqstd_seterrorhandlers(v);
  844. textbuf = new Fl_Text_Buffer;
  845. //textbuf->transcoding_warning_action = NULL;
  846. style_init();
  847. fl_open_callback(cb);
  848. Fl_Window* window = new_view();
  849. window->show(1, argv);
  850. #ifndef __APPLE__
  851. if (argc > 1) load_file(argv[1], -1);
  852. #endif
  853. int rc = Fl::run();
  854. sq_close(v);
  855. return rc;
  856. }
  857. //
  858. // End of "$Id: editor.cxx 9718 2012-11-13 13:03:20Z manolo $".
  859. //