syntax.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  1. /* editor syntax highlighting.
  2. Copyright (C) 1996, 1997, 1998 the Free Software Foundation
  3. Authors: 1998 Paul Sheer
  4. $Id$
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  16. 02111-1307, USA.
  17. */
  18. #include <config.h>
  19. #include "edit.h"
  20. /* bytes */
  21. #define SYNTAX_MARKER_DENSITY 512
  22. /*
  23. Mispelled words are flushed from the syntax highlighting rules
  24. when they have been around longer than
  25. TRANSIENT_WORD_TIME_OUT seconds. At a cursor rate of 30
  26. chars per second and say 3 chars + a space per word, we can
  27. accumulate 450 words absolute max with a value of 60. This is
  28. below this limit of 1024 words in a context.
  29. */
  30. #define TRANSIENT_WORD_TIME_OUT 60
  31. #define UNKNOWN_FORMAT "unknown"
  32. #ifdef HAVE_SYNTAXH
  33. int option_syntax_highlighting = 1;
  34. int option_auto_spellcheck = 1;
  35. static inline void *syntax_malloc (size_t x)
  36. {
  37. void *p;
  38. p = malloc (x);
  39. memset (p, 0, x);
  40. return p;
  41. }
  42. #define syntax_free(x) {if(x){free(x);(x)=0;}}
  43. #define syntax_g_free(x) {if(x){g_free(x);(x)=0;}}
  44. static long compare_word_to_right (WEdit * edit, long i, char *text, char *whole_left, char *whole_right, int line_start)
  45. {
  46. unsigned char *p, *q;
  47. int c, d, j;
  48. if (!*text)
  49. return -1;
  50. c = edit_get_byte (edit, i - 1);
  51. if (line_start)
  52. if (c != '\n')
  53. return -1;
  54. if (whole_left)
  55. if (strchr (whole_left, c))
  56. return -1;
  57. for (p = (unsigned char *) text, q = p + strlen ((char *) p); p < q; p++, i++) {
  58. switch (*p) {
  59. case '\001':
  60. p++;
  61. for (;;) {
  62. c = edit_get_byte (edit, i);
  63. if (!*p)
  64. if (whole_right)
  65. if (!strchr (whole_right, c))
  66. break;
  67. if (c == *p)
  68. break;
  69. if (c == '\n')
  70. return -1;
  71. i++;
  72. }
  73. break;
  74. case '\002':
  75. p++;
  76. j = 0;
  77. for (;;) {
  78. c = edit_get_byte (edit, i);
  79. if (c == *p) {
  80. j = i;
  81. if (*p == *text && !p[1]) /* handle eg '+' and @+@ keywords properly */
  82. break;
  83. }
  84. if (j && strchr ((char *) p + 1, c)) /* c exists further down, so it will get matched later */
  85. break;
  86. if (c == '\n' || c == '\t' || c == ' ') {
  87. if (!*p) {
  88. i--;
  89. break;
  90. }
  91. if (!j)
  92. return -1;
  93. i = j;
  94. break;
  95. }
  96. if (whole_right)
  97. if (!strchr (whole_right, c)) {
  98. if (!*p) {
  99. i--;
  100. break;
  101. }
  102. if (!j)
  103. return -1;
  104. i = j;
  105. break;
  106. }
  107. i++;
  108. }
  109. break;
  110. case '\003':
  111. p++;
  112. c = -1;
  113. for (;; i++) {
  114. d = c;
  115. c = edit_get_byte (edit, i);
  116. for (j = 0; p[j] != '\003'; j++)
  117. if (c == p[j])
  118. goto found_char2;
  119. break;
  120. found_char2:
  121. j = c; /* dummy command */
  122. }
  123. i--;
  124. while (*p != '\003')
  125. p++;
  126. if (p[1] == d)
  127. i--;
  128. break;
  129. case '\004':
  130. p++;
  131. c = edit_get_byte (edit, i);
  132. for (; *p != '\004'; p++)
  133. if (c == *p)
  134. goto found_char3;
  135. return -1;
  136. found_char3:
  137. for (; *p != '\004'; p++);
  138. break;
  139. default:
  140. if (*p != edit_get_byte (edit, i))
  141. return -1;
  142. }
  143. }
  144. if (whole_right)
  145. if (strchr (whole_right, edit_get_byte (edit, i)))
  146. return -1;
  147. return i;
  148. }
  149. #define XXX \
  150. if (*s < '\005' || *s == (unsigned char) c) \
  151. goto done; \
  152. s++;
  153. static inline char *xx_strchr (const unsigned char *s, int c)
  154. {
  155. repeat:
  156. XXX XXX XXX XXX XXX XXX XXX XXX;
  157. XXX XXX XXX XXX XXX XXX XXX XXX;
  158. goto repeat;
  159. done:
  160. return (char *) s;
  161. }
  162. static inline struct syntax_rule apply_rules_going_right (WEdit * edit, long i, struct syntax_rule rule)
  163. {
  164. struct context_rule *r;
  165. int contextchanged = 0, c;
  166. int found_right = 0, found_left = 0, keyword_foundleft = 0, keyword_foundright = 0;
  167. int is_end;
  168. long end = 0;
  169. struct syntax_rule _rule = rule;
  170. if (!(c = edit_get_byte (edit, i)))
  171. return rule;
  172. is_end = (rule.end == (unsigned char) i);
  173. /* check to turn off a keyword */
  174. if (_rule.keyword) {
  175. struct key_word *k;
  176. k = edit->rules[_rule.context]->keyword[_rule.keyword];
  177. if (edit_get_byte (edit, i - 1) == '\n')
  178. _rule.keyword = 0;
  179. if (is_end) {
  180. _rule.keyword = 0;
  181. keyword_foundleft = 1;
  182. }
  183. }
  184. /* check to turn off a context */
  185. if (_rule.context && !_rule.keyword) {
  186. long e;
  187. r = edit->rules[_rule.context];
  188. if (r->first_right == c && !(rule.border & RULE_ON_RIGHT_BORDER) && (e = compare_word_to_right (edit, i, r->right, r->whole_word_chars_left, r->whole_word_chars_right, r->line_start_right)) > 0) {
  189. _rule.end = e;
  190. found_right = 1;
  191. _rule.border = RULE_ON_RIGHT_BORDER;
  192. if (r->between_delimiters)
  193. _rule.context = 0;
  194. } else if (is_end && rule.border & RULE_ON_RIGHT_BORDER) {
  195. /* always turn off a context at 4 */
  196. found_left = 1;
  197. _rule.border = 0;
  198. if (!keyword_foundleft)
  199. _rule.context = 0;
  200. } else if (is_end && rule.border & RULE_ON_LEFT_BORDER) {
  201. /* never turn off a context at 2 */
  202. found_left = 1;
  203. _rule.border = 0;
  204. }
  205. }
  206. /* check to turn on a keyword */
  207. if (!_rule.keyword) {
  208. char *p;
  209. p = (r = edit->rules[_rule.context])->keyword_first_chars;
  210. if (p)
  211. while (*(p = xx_strchr ((unsigned char *) p + 1, c))) {
  212. struct key_word *k;
  213. int count;
  214. long e;
  215. count = p - r->keyword_first_chars;
  216. k = r->keyword[count];
  217. e = compare_word_to_right (edit, i, k->keyword, k->whole_word_chars_left, k->whole_word_chars_right, k->line_start);
  218. if (e > 0) {
  219. end = e;
  220. _rule.end = e;
  221. _rule.keyword = count;
  222. keyword_foundright = 1;
  223. break;
  224. }
  225. }
  226. }
  227. /* check to turn on a context */
  228. if (!_rule.context) {
  229. if (!found_left && is_end) {
  230. if (rule.border & RULE_ON_RIGHT_BORDER) {
  231. _rule.border = 0;
  232. _rule.context = 0;
  233. contextchanged = 1;
  234. _rule.keyword = 0;
  235. } else if (rule.border & RULE_ON_LEFT_BORDER) {
  236. r = edit->rules[_rule._context];
  237. _rule.border = 0;
  238. if (r->between_delimiters) {
  239. long e;
  240. _rule.context = _rule._context;
  241. contextchanged = 1;
  242. _rule.keyword = 0;
  243. if (r->first_right == c && (e = compare_word_to_right (edit, i, r->right, r->whole_word_chars_left, r->whole_word_chars_right, r->line_start_right)) >= end) {
  244. _rule.end = e;
  245. found_right = 1;
  246. _rule.border = RULE_ON_RIGHT_BORDER;
  247. _rule.context = 0;
  248. }
  249. }
  250. }
  251. }
  252. if (!found_right) {
  253. int count;
  254. struct context_rule **rules = edit->rules;
  255. for (count = 1; rules[count]; count++) {
  256. r = rules[count];
  257. if (r->first_left == c) {
  258. long e;
  259. e = compare_word_to_right (edit, i, r->left, r->whole_word_chars_left, r->whole_word_chars_right, r->line_start_left);
  260. if (e >= end && (!_rule.keyword || keyword_foundright)) {
  261. _rule.end = e;
  262. found_right = 1;
  263. _rule.border = RULE_ON_LEFT_BORDER;
  264. _rule._context = count;
  265. if (!r->between_delimiters)
  266. if (!_rule.keyword) {
  267. _rule.context = count;
  268. contextchanged = 1;
  269. }
  270. break;
  271. }
  272. }
  273. }
  274. }
  275. }
  276. /* check again to turn on a keyword if the context switched */
  277. if (contextchanged && !_rule.keyword) {
  278. char *p;
  279. p = (r = edit->rules[_rule.context])->keyword_first_chars;
  280. while (*(p = xx_strchr ((unsigned char *) p + 1, c))) {
  281. struct key_word *k;
  282. int count;
  283. long e;
  284. count = p - r->keyword_first_chars;
  285. k = r->keyword[count];
  286. e = compare_word_to_right (edit, i, k->keyword, k->whole_word_chars_left, k->whole_word_chars_right, k->line_start);
  287. if (e > 0) {
  288. _rule.end = e;
  289. _rule.keyword = count;
  290. break;
  291. }
  292. }
  293. }
  294. return _rule;
  295. }
  296. static struct syntax_rule edit_get_rule (WEdit * edit, long byte_index)
  297. {
  298. long i;
  299. if (byte_index > edit->last_get_rule) {
  300. for (i = edit->last_get_rule + 1; i <= byte_index; i++) {
  301. edit->rule = apply_rules_going_right (edit, i, edit->rule);
  302. if (i > (edit->syntax_marker ? edit->syntax_marker->offset + SYNTAX_MARKER_DENSITY : SYNTAX_MARKER_DENSITY)) {
  303. struct _syntax_marker *s;
  304. s = edit->syntax_marker;
  305. edit->syntax_marker = syntax_malloc (sizeof (struct _syntax_marker));
  306. edit->syntax_marker->next = s;
  307. edit->syntax_marker->offset = i;
  308. edit->syntax_marker->rule = edit->rule;
  309. }
  310. }
  311. } else if (byte_index < edit->last_get_rule) {
  312. struct _syntax_marker *s;
  313. for (;;) {
  314. if (!edit->syntax_marker) {
  315. memset (&edit->rule, 0, sizeof (edit->rule));
  316. for (i = -1; i <= byte_index; i++)
  317. edit->rule = apply_rules_going_right (edit, i, edit->rule);
  318. break;
  319. }
  320. if (byte_index >= edit->syntax_marker->offset) {
  321. edit->rule = edit->syntax_marker->rule;
  322. for (i = edit->syntax_marker->offset + 1; i <= byte_index; i++)
  323. edit->rule = apply_rules_going_right (edit, i, edit->rule);
  324. break;
  325. }
  326. s = edit->syntax_marker->next;
  327. syntax_free (edit->syntax_marker);
  328. edit->syntax_marker = s;
  329. }
  330. }
  331. edit->last_get_rule = byte_index;
  332. return edit->rule;
  333. }
  334. static void translate_rule_to_color (WEdit * edit, struct syntax_rule rule, int *color)
  335. {
  336. struct key_word *k;
  337. k = edit->rules[rule.context]->keyword[rule.keyword];
  338. *color = k->color;
  339. }
  340. void edit_get_syntax_color (WEdit * edit, long byte_index, int *color)
  341. {
  342. if (edit->rules && byte_index < edit->last_byte &&
  343. option_syntax_highlighting && use_colors) {
  344. translate_rule_to_color (edit, edit_get_rule (edit, byte_index), color);
  345. } else {
  346. *color = use_colors ? EDITOR_NORMAL_COLOR_INDEX : 0;
  347. }
  348. }
  349. /*
  350. Returns 0 on error/eof or a count of the number of bytes read
  351. including the newline. Result must be free'd.
  352. */
  353. #ifdef HAVE_MAD
  354. static int mad_read_one_line (char **line, FILE * f, char *file, int line_)
  355. #define read_one_line(a,b) mad_read_one_line(a,b,__FILE__,__LINE__)
  356. #else
  357. static int read_one_line (char **line, FILE * f)
  358. #endif
  359. {
  360. char *p;
  361. int len = 256, c, r = 0, i = 0;
  362. p = syntax_malloc (len);
  363. for (;;) {
  364. c = fgetc (f);
  365. if (c == EOF) {
  366. if (ferror (f)) {
  367. if (errno == EINTR)
  368. continue;
  369. r = 0;
  370. }
  371. break;
  372. } else if (c == '\n') {
  373. r = i + 1; /* extra 1 for the newline just read */
  374. break;
  375. } else {
  376. if (i >= len - 1) {
  377. char *q;
  378. q = syntax_malloc (len * 2);
  379. memcpy (q, p, len);
  380. syntax_free (p);
  381. p = q;
  382. len *= 2;
  383. }
  384. p[i++] = c;
  385. }
  386. }
  387. p[i] = 0;
  388. *line = p;
  389. return r;
  390. }
  391. static char *convert (char *s)
  392. {
  393. char *r, *p;
  394. p = r = s;
  395. while (*s) {
  396. switch (*s) {
  397. case '\\':
  398. s++;
  399. switch (*s) {
  400. case ' ':
  401. *p = ' ';
  402. s--;
  403. break;
  404. case 'n':
  405. *p = '\n';
  406. break;
  407. case 'r':
  408. *p = '\r';
  409. break;
  410. case 't':
  411. *p = '\t';
  412. break;
  413. case 's':
  414. *p = ' ';
  415. break;
  416. case '*':
  417. *p = '*';
  418. break;
  419. case '\\':
  420. *p = '\\';
  421. break;
  422. case '[':
  423. case ']':
  424. *p = '\003';
  425. break;
  426. case '{':
  427. case '}':
  428. *p = '\004';
  429. break;
  430. case 0:
  431. *p = *s;
  432. return r;
  433. default:
  434. *p = *s;
  435. break;
  436. }
  437. break;
  438. case '*':
  439. *p = '\001';
  440. break;
  441. case '+':
  442. *p = '\002';
  443. break;
  444. default:
  445. *p = *s;
  446. break;
  447. }
  448. s++;
  449. p++;
  450. }
  451. *p = '\0';
  452. return r;
  453. }
  454. #define whiteness(x) ((x) == '\t' || (x) == '\n' || (x) == ' ')
  455. static void get_args (char *l, char **args, int *argc)
  456. {
  457. *argc = 0;
  458. for (;;) {
  459. char *p = l;
  460. while (*p && whiteness (*p))
  461. p++;
  462. if (!*p)
  463. break;
  464. for (l = p + 1; *l && !whiteness (*l); l++);
  465. if (*l)
  466. *l++ = '\0';
  467. *args = convert (p);
  468. (*argc)++;
  469. args++;
  470. }
  471. *args = 0;
  472. }
  473. #define free_args(x)
  474. #define break_a {result=line;break;}
  475. #define check_a {if(!*a){result=line;break;}}
  476. #define check_not_a {if(*a){result=line;break;}}
  477. int try_alloc_color_pair (char *fg, char *bg);
  478. int this_try_alloc_color_pair (char *fg, char *bg)
  479. {
  480. char f[80], b[80], *p;
  481. if (bg)
  482. if (!*bg)
  483. bg = 0;
  484. if (fg)
  485. if (!*fg)
  486. fg = 0;
  487. if (fg) {
  488. strcpy (f, fg);
  489. p = strchr (f, '/');
  490. if (p)
  491. *p = '\0';
  492. fg = f;
  493. }
  494. if (bg) {
  495. strcpy (b, bg);
  496. p = strchr (b, '/');
  497. if (p)
  498. *p = '\0';
  499. bg = b;
  500. }
  501. return try_alloc_color_pair (fg, bg);
  502. }
  503. static char *error_file_name = 0;
  504. static FILE *open_include_file (char *filename)
  505. {
  506. FILE *f;
  507. syntax_g_free (error_file_name);
  508. error_file_name = g_strdup (filename);
  509. if (*filename == PATH_SEP)
  510. return fopen (filename, "r");
  511. g_free (error_file_name);
  512. error_file_name = g_strconcat (home_dir, EDIT_DIR PATH_SEP_STR,
  513. filename, NULL);
  514. f = fopen (error_file_name, "r");
  515. if (f)
  516. return f;
  517. g_free (error_file_name);
  518. error_file_name = g_strconcat (mc_home, PATH_SEP_STR "syntax" PATH_SEP_STR,
  519. filename, NULL);
  520. return fopen (error_file_name, "r");
  521. }
  522. /* returns line number on error */
  523. static int edit_read_syntax_rules (WEdit * edit, FILE * f)
  524. {
  525. FILE *g = 0;
  526. char *fg, *bg;
  527. char last_fg[32] = "", last_bg[32] = "";
  528. char whole_right[512];
  529. char whole_left[512];
  530. char *args[1024], *l = 0;
  531. int save_line = 0, line = 0;
  532. struct context_rule **r, *c = 0;
  533. int num_words = -1, num_contexts = -1;
  534. int argc, result = 0;
  535. int i, j;
  536. args[0] = 0;
  537. strcpy (whole_left, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_01234567890");
  538. strcpy (whole_right, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_01234567890");
  539. r = edit->rules = syntax_malloc (MAX_CONTEXTS * sizeof (struct context_rule *));
  540. for (;;) {
  541. char **a;
  542. line++;
  543. l = 0;
  544. if (!read_one_line (&l, f)) {
  545. if (g) {
  546. fclose (f);
  547. f = g;
  548. g = 0;
  549. line = save_line + 1;
  550. syntax_g_free (error_file_name);
  551. if (l)
  552. syntax_free (l);
  553. if (!read_one_line (&l, f))
  554. break;
  555. } else {
  556. break;
  557. }
  558. }
  559. get_args (l, args, &argc);
  560. a = args + 1;
  561. if (!args[0]) {
  562. /* do nothing */
  563. } else if (!strcmp (args[0], "include")) {
  564. if (g || argc != 2) {
  565. result = line;
  566. break;
  567. }
  568. g = f;
  569. f = open_include_file (args[1]);
  570. if (!f) {
  571. syntax_g_free (error_file_name);
  572. result = line;
  573. break;
  574. }
  575. save_line = line;
  576. line = 0;
  577. } else if (!strcmp (args[0], "wholechars")) {
  578. check_a;
  579. if (!strcmp (*a, "left")) {
  580. a++;
  581. strcpy (whole_left, *a);
  582. } else if (!strcmp (*a, "right")) {
  583. a++;
  584. strcpy (whole_right, *a);
  585. } else {
  586. strcpy (whole_left, *a);
  587. strcpy (whole_right, *a);
  588. }
  589. a++;
  590. check_not_a;
  591. } else if (!strcmp (args[0], "context")) {
  592. check_a;
  593. if (num_contexts == -1) {
  594. if (strcmp (*a, "default")) { /* first context is the default */
  595. break_a;
  596. }
  597. a++;
  598. c = r[0] = syntax_malloc (sizeof (struct context_rule));
  599. c->left = (char *) strdup (" ");
  600. c->right = (char *) strdup (" ");
  601. num_contexts = 0;
  602. } else {
  603. c = r[num_contexts] = syntax_malloc (sizeof (struct context_rule));
  604. if (!strcmp (*a, "exclusive")) {
  605. a++;
  606. c->between_delimiters = 1;
  607. }
  608. check_a;
  609. if (!strcmp (*a, "whole")) {
  610. a++;
  611. c->whole_word_chars_left = (char *) strdup (whole_left);
  612. c->whole_word_chars_right = (char *) strdup (whole_right);
  613. } else if (!strcmp (*a, "wholeleft")) {
  614. a++;
  615. c->whole_word_chars_left = (char *) strdup (whole_left);
  616. } else if (!strcmp (*a, "wholeright")) {
  617. a++;
  618. c->whole_word_chars_right = (char *) strdup (whole_right);
  619. }
  620. check_a;
  621. if (!strcmp (*a, "linestart")) {
  622. a++;
  623. c->line_start_left = 1;
  624. }
  625. check_a;
  626. c->left = (char *) strdup (*a++);
  627. check_a;
  628. if (!strcmp (*a, "linestart")) {
  629. a++;
  630. c->line_start_right = 1;
  631. }
  632. check_a;
  633. c->right = (char *) strdup (*a++);
  634. c->first_left = *c->left;
  635. c->first_right = *c->right;
  636. #if 0
  637. c->single_char = (strlen (c->right) == 1);
  638. #endif
  639. }
  640. c->keyword = syntax_malloc (MAX_WORDS_PER_CONTEXT * sizeof (struct key_word *));
  641. #if 0
  642. c->max_words = MAX_WORDS_PER_CONTEXT;
  643. #endif
  644. num_words = 1;
  645. c->keyword[0] = syntax_malloc (sizeof (struct key_word));
  646. fg = *a;
  647. if (*a)
  648. a++;
  649. bg = *a;
  650. if (*a)
  651. a++;
  652. strcpy (last_fg, fg ? fg : "");
  653. strcpy (last_bg, bg ? bg : "");
  654. c->keyword[0]->color = this_try_alloc_color_pair (fg, bg);
  655. c->keyword[0]->keyword = (char *) strdup (" ");
  656. check_not_a;
  657. num_contexts++;
  658. } else if (!strcmp (args[0], "spellcheck")) {
  659. if (!c) {
  660. result = line;
  661. break;
  662. }
  663. c->spelling = 1;
  664. } else if (!strcmp (args[0], "keyword")) {
  665. struct key_word *k;
  666. if (num_words == -1)
  667. break_a;
  668. check_a;
  669. k = r[num_contexts - 1]->keyword[num_words] = syntax_malloc (sizeof (struct key_word));
  670. if (!strcmp (*a, "whole")) {
  671. a++;
  672. k->whole_word_chars_left = (char *) strdup (whole_left);
  673. k->whole_word_chars_right = (char *) strdup (whole_right);
  674. } else if (!strcmp (*a, "wholeleft")) {
  675. a++;
  676. k->whole_word_chars_left = (char *) strdup (whole_left);
  677. } else if (!strcmp (*a, "wholeright")) {
  678. a++;
  679. k->whole_word_chars_right = (char *) strdup (whole_right);
  680. }
  681. check_a;
  682. if (!strcmp (*a, "linestart")) {
  683. a++;
  684. k->line_start = 1;
  685. }
  686. check_a;
  687. if (!strcmp (*a, "whole")) {
  688. break_a;
  689. }
  690. k->keyword = (char *) strdup (*a++);
  691. k->first = *k->keyword;
  692. fg = *a;
  693. if (*a)
  694. a++;
  695. bg = *a;
  696. if (*a)
  697. a++;
  698. if (!fg)
  699. fg = last_fg;
  700. if (!bg)
  701. bg = last_bg;
  702. k->color = this_try_alloc_color_pair (fg, bg);
  703. check_not_a;
  704. num_words++;
  705. } else if (!strncmp (args[0], "#", 1)) {
  706. /* do nothing for comment */
  707. } else if (!strcmp (args[0], "file")) {
  708. break;
  709. } else { /* anything else is an error */
  710. break_a;
  711. }
  712. free_args (args);
  713. syntax_free (l);
  714. }
  715. free_args (args);
  716. syntax_free (l);
  717. if (!edit->rules[0])
  718. syntax_free (edit->rules);
  719. if (result)
  720. return result;
  721. if (num_contexts == -1) {
  722. result = line;
  723. return result;
  724. }
  725. {
  726. char first_chars[MAX_WORDS_PER_CONTEXT + 2], *p;
  727. for (i = 0; edit->rules[i]; i++) {
  728. c = edit->rules[i];
  729. p = first_chars;
  730. *p++ = (char) 1;
  731. for (j = 1; c->keyword[j]; j++)
  732. *p++ = c->keyword[j]->first;
  733. *p = '\0';
  734. c->keyword_first_chars = syntax_malloc (strlen (first_chars) + 2);
  735. strcpy (c->keyword_first_chars, first_chars);
  736. }
  737. }
  738. return result;
  739. }
  740. int edit_check_spelling (WEdit * edit)
  741. {
  742. return 0;
  743. }
  744. void (*syntax_change_callback) (CWidget *) = 0;
  745. void edit_set_syntax_change_callback (void (*callback) (CWidget *))
  746. {
  747. syntax_change_callback = callback;
  748. }
  749. void edit_free_syntax_rules (WEdit * edit)
  750. {
  751. int i, j;
  752. if (!edit)
  753. return;
  754. if (!edit->rules)
  755. return;
  756. edit_get_rule (edit, -1);
  757. syntax_free (edit->syntax_type);
  758. edit->syntax_type = 0;
  759. if (syntax_change_callback)
  760. (*syntax_change_callback) (&edit->widget);
  761. for (i = 0; edit->rules[i]; i++) {
  762. if (edit->rules[i]->keyword) {
  763. for (j = 0; edit->rules[i]->keyword[j]; j++) {
  764. syntax_free (edit->rules[i]->keyword[j]->keyword);
  765. syntax_free (edit->rules[i]->keyword[j]->whole_word_chars_left);
  766. syntax_free (edit->rules[i]->keyword[j]->whole_word_chars_right);
  767. syntax_free (edit->rules[i]->keyword[j]);
  768. }
  769. }
  770. syntax_free (edit->rules[i]->left);
  771. syntax_free (edit->rules[i]->right);
  772. syntax_free (edit->rules[i]->whole_word_chars_left);
  773. syntax_free (edit->rules[i]->whole_word_chars_right);
  774. syntax_free (edit->rules[i]->keyword);
  775. syntax_free (edit->rules[i]->keyword_first_chars);
  776. syntax_free (edit->rules[i]);
  777. }
  778. while (edit->syntax_marker) {
  779. struct _syntax_marker *s = edit->syntax_marker->next;
  780. syntax_free (edit->syntax_marker);
  781. edit->syntax_marker = s;
  782. }
  783. syntax_free (edit->rules);
  784. }
  785. /* returns -1 on file error, line number on error in file syntax */
  786. static int edit_read_syntax_file (WEdit * edit, char **names, char *syntax_file, char *editor_file, char *first_line, char *type)
  787. {
  788. FILE *f;
  789. regex_t r;
  790. regmatch_t pmatch[1];
  791. char *args[1024], *l = 0;
  792. int line = 0;
  793. int argc;
  794. int result = 0;
  795. int count = 0;
  796. char *lib_file;
  797. f = fopen (syntax_file, "r");
  798. if (!f){
  799. lib_file = concat_dir_and_file (mc_home, "syntax" PATH_SEP_STR "Syntax");
  800. f = fopen (lib_file, "r");
  801. g_free (lib_file);
  802. if (!f)
  803. return -1;
  804. }
  805. args[0] = 0;
  806. for (;;) {
  807. line++;
  808. syntax_free (l);
  809. if (!read_one_line (&l, f))
  810. break;
  811. get_args (l, args, &argc);
  812. if (!args[0])
  813. continue;
  814. /* looking for `file ...' lines only */
  815. if (strcmp (args[0], "file")) {
  816. free_args (args);
  817. continue;
  818. }
  819. /* must have two args or report error */
  820. if (!args[1] || !args[2]) {
  821. result = line;
  822. break;
  823. }
  824. if (names) {
  825. /* 1: just collecting a list of names of rule sets */
  826. names[count++] = (char *) strdup (args[2]);
  827. names[count] = 0;
  828. } else if (type) {
  829. /* 2: rule set was explicitly specified by the caller */
  830. if (!strcmp (type, args[2]))
  831. goto found_type;
  832. } else if (editor_file && edit) {
  833. /* 3: auto-detect rule set from regular expressions */
  834. int q;
  835. if (regcomp (&r, args[1], REG_EXTENDED)) {
  836. result = line;
  837. break;
  838. }
  839. /* does filename match arg 1 ? */
  840. q = !regexec (&r, editor_file, 1, pmatch, 0);
  841. regfree (&r);
  842. if (!q && args[3]) {
  843. if (regcomp (&r, args[3], REG_EXTENDED)) {
  844. result = line;
  845. break;
  846. }
  847. /* does first line match arg 3 ? */
  848. q = !regexec (&r, first_line, 1, pmatch, 0);
  849. regfree (&r);
  850. }
  851. if (q) {
  852. int line_error;
  853. found_type:
  854. line_error = edit_read_syntax_rules (edit, f);
  855. if (line_error) {
  856. if (!error_file_name) /* an included file */
  857. result = line + line_error;
  858. else
  859. result = line_error;
  860. } else {
  861. syntax_free (edit->syntax_type);
  862. edit->syntax_type = (char *) strdup (args[2]);
  863. /* if there are no rules then turn off syntax highlighting for speed */
  864. if (!edit->rules[1])
  865. if (!edit->rules[0]->keyword[1] && !edit->rules[0]->spelling) {
  866. edit_free_syntax_rules (edit);
  867. break;
  868. }
  869. /* notify the callback of a change in rule set */
  870. if (syntax_change_callback)
  871. (*syntax_change_callback) (&edit->widget);
  872. }
  873. break;
  874. }
  875. }
  876. free_args (args);
  877. }
  878. free_args (args);
  879. syntax_free (l);
  880. fclose (f);
  881. return result;
  882. }
  883. static char *get_first_editor_line (WEdit * edit)
  884. {
  885. int i;
  886. static char s[256];
  887. s[0] = '\0';
  888. if (!edit)
  889. return s;
  890. for (i = 0; i < 255; i++) {
  891. s[i] = edit_get_byte (edit, i);
  892. if (s[i] == '\n') {
  893. s[i] = '\0';
  894. break;
  895. }
  896. }
  897. s[255] = '\0';
  898. return s;
  899. }
  900. /* loads rules into edit struct. one of edit or names must be zero. if
  901. edit is zero, a list of types will be stored into name. type may be zero
  902. in which case the type will be selected according to the filename. */
  903. void edit_load_syntax (WEdit * edit, char **names, char *type)
  904. {
  905. int r;
  906. char *f;
  907. edit_free_syntax_rules (edit);
  908. if (!use_colors)
  909. return;
  910. if (!option_syntax_highlighting)
  911. return;
  912. if (edit) {
  913. if (!edit->filename)
  914. return;
  915. if (!*edit->filename && !type)
  916. return;
  917. }
  918. f = catstrs (home_dir, SYNTAX_FILE, 0);
  919. r = edit_read_syntax_file (edit, names, f, edit ? edit->filename : 0, get_first_editor_line (edit), type);
  920. if (r == -1) {
  921. edit_free_syntax_rules (edit);
  922. edit_error_dialog (_ (" Load syntax file "), _ (" File access error "));
  923. return;
  924. }
  925. if (r) {
  926. edit_free_syntax_rules (edit);
  927. message (0, _(" Load syntax file "),
  928. _(" Error in file %s on line %d "),
  929. error_file_name ? error_file_name : f, r);
  930. syntax_g_free (error_file_name);
  931. return;
  932. }
  933. }
  934. #else
  935. int option_syntax_highlighting = 0;
  936. void edit_load_syntax (WEdit * edit, char **names, char *type)
  937. {
  938. return;
  939. }
  940. void edit_free_syntax_rules (WEdit * edit)
  941. {
  942. return;
  943. }
  944. void edit_get_syntax_color (WEdit * edit, long byte_index, int *color)
  945. {
  946. *color = use_colors ? EDITOR_NORMAL_COLOR_INDEX : 0;
  947. }
  948. int edit_check_spelling (WEdit * edit)
  949. {
  950. return 0;
  951. }
  952. #endif /* HAVE_SYNTAXH */