editor.cxx 27 KB

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