/************************************************************************ * awk.e * * "Epsilon" is a registered trademark licensed to Lugaru Software, Ltd. * * "EEL" and "Lugaru" are trademarks of Lugaru Software, Ltd. * * * * Copyright (C) 1996, 1997 Lugaru Software Ltd. All rights reserved. * * * * Limited permission is hereby granted to reproduce and modify this * * copyrighted material provided that the resulting code is used only in * * conjunction with Lugaru products and that this notice is retained in * * any such reproduction or modification. * * *********************************************************************** * Modified by Paul Neth, Jan 14, 1998 email:pneth@henge.com * * Modified the assembly language eel (asm.e) program * * from the lugaru web site * * Provides syntax highlighting for GNU awk programs * * Highlites the awk keywords in blue. * * Comments are in green and strings are in red. * ************************************************************************/ #include "eel.h" #include "proc.h" #include "colcode.h" char _awk_mode_name[] = "awk"; /* awk mode */ keytable awk_tab; /* key table for awk mode */ color_scheme "standard-gui" { /* define some of the colors */ color_class awk_keyword blue; color_class awk_identifier black; }; color_awk_range(from, to) // recolor just this section { // last colored region may go past to int t = -1, talk, s; if (from >= to) return to; save_var point, matchstart, matchend; c_init_color(from, to); point = from; talk = (to - from > 2000); // show status during long delays save_var case_fold = 0; while (point < to) { if (!re_search(1, "[0-9A-Za-z_.]+|[\"';#]")) { t = size(); break; } t = matchstart; switch (character(point - 1)) { // check last char case '#': // found a pound sign (starts a comment) nl_forward(); set_character_color(t, point, color_class c_comment); break; case '"': // found a string literal point = t; re_search(1, "\"([^\"\\\n]|\\(.|\n))*[\"\n]"); set_character_color(t, point, color_class c_string); if (get_character_color(point, (int *) 0, &s) == color_class c_string && s > to) // fix up after c_init_color(point, to = s); // quoted "'s break; default: // found identifier, kywd, or number set_character_color(t, point, awk_keyword_color(t)); break; } if (talk) note("Coloring awk program: %d%% complete...", (point - from) * 100 / (to - from)); } c_init_color(to, t); if (talk) note(""); return point; } awk_keyword_color(from) // return color for "identifier" from here to point { // (something with alpha or digits) char buf[500]; if (point - from > sizeof(buf) - 10) save_var point = from + sizeof(buf) - 10; buf[0] = '|'; // get identifier, between | chars grab(from, point, buf + 1); if (index("0123456789-.", buf[1])) return c_number_color(buf + 1); strcpy(buf + point - from + 1, "|"); if (is_awk_keyword(buf)) return color_class awk_keyword; return color_class awk_identifier; } // gnu awk keyword list is_awk_keyword(p) // is text in p (must be surrounded by |'s) a keyword? char *p; { if (strstr("|ARGC|ARGIND|ARGV" "|CONVFMT|FIELDWIDTHS|ENVIRON|" "|ERRNO|FILENAME|FNR|" "FS|IGNORECASE|NF|" "NR|OFMT|OFS|" "ORS|RS|RT|" "RSTART|RLENGTH|SUBSEP|" "BEGIN|END|" "if|else|while|" "do|for|break|" "continue|delete|exit|" "getline|next|nextfile|" "print|printf|" "atan2|cos|exp|" "int|log|rand|" "sin|sqrt|srand|" "gensub|gsub|index|" "length|match|split|" "sprintf|sub|substr|" "tolower|toupper|close|" "fflush|system|systime|" "strftime",p)) return 1; return 0; } // A recolor_from_here function good for line-based buffers. recolor_by_lines(safe) { safe = safe; // eliminate compiler warning return give_begin_line(); } command awk_mode() { mode_keys = awk_tab; /* Use these keys. */ major_mode = _awk_mode_name; strcpy(comment_start, "#"); // regex for start of comment strcpy(comment_pattern, ".*#.*$"); // regex pattern for comment area strcpy(comment_begin, "# "); // string used if epsilon automatically creates comment strcpy(comment_end, ""); // string used to indicate end of string recolor_range = color_awk_range; // set up coloring rules recolor_from_here = recolor_by_lines; if (want_code_coloring) // maybe turn on coloring when_setting_want_code_coloring(); try_calling("awk-mode-hook"); make_mode(); } suffix_awk() { awk_mode(); }