text:
#include "ce_syntax.h"

#include <stdlib.h>
#include <ncurses.h>
#include <string.h>
#include <ctype.h>

#define SEARCH_BACK_LINES 1024

int ce_syntax_def_get_fg(CeSyntaxDef_t* syntax_defs, CeSyntaxColor_t syntax_color, int current_fg){
     int new_color = syntax_defs[syntax_color].fg;
     if(new_color == CE_SYNTAX_USE_CURRENT_COLOR) return current_fg;
     return new_color;
}

int ce_syntax_def_get_bg(CeSyntaxDef_t* syntax_defs, CeSyntaxColor_t syntax_color, int current_bg){
     int new_color = syntax_defs[syntax_color].bg;
     if(new_color == CE_SYNTAX_USE_CURRENT_COLOR) return current_bg;
     return new_color;
}

bool ce_draw_color_list_insert(CeDrawColorList_t* list, int fg, int bg, CePoint_t point){
     if(list->tail && list->tail->fg == fg && list->tail->bg == bg && list->tail->point.y == point.y) return true;
     CeDrawColorNode_t* node = malloc(sizeof(*node));
     if(!node) return false;
     node->fg = fg;
     node->bg = bg;
     node->point = point;
     node->next = NULL;
     if(list->tail) list->tail->next = node;
     list->tail = node;
     if(!list->head) list->head = node;
     return true;
}

void ce_draw_color_list_free(CeDrawColorList_t* list){
     CeDrawColorNode_t* itr = list->head;
     while(itr){
          CeDrawColorNode_t* tmp = itr;
          itr = itr->next;
          free(tmp);
     }

     list->head = NULL;
     list->tail = NULL;
}

bool ce_range_list_insert(CeRangeList_t* list, CePoint_t start, CePoint_t end){
     CeRangeNode_t* node = malloc(sizeof(*node));
     if(!node) return false;
     node->range.start = start;
     node->range.end = end;
     node->next = NULL;
     if(list->tail) list->tail->next = node;
     list->tail = node;
     if(!list->head) list->head = node;
     return true;
}

bool ce_range_list_insert_sorted(CeRangeList_t* list, CePoint_t start, CePoint_t end){
     CeRangeNode_t* itr = list->head;
     CeRangeNode_t* prev = NULL;
     while(itr){
          if(prev){
               if(ce_point_after(itr->range.start, end) &&
                  ce_point_after(end, prev->range.end)){
                    break;
               }
          }else{
               if(ce_point_after(itr->range.start, end)){
                    break;
               }
          }

          prev = itr;
          itr = itr->next;
     }

     if(itr){
          if(!ce_point_after(end, itr->range.start)) return false;
     }

     CeRangeNode_t* node = malloc(sizeof(*node));
     if(!node) return false;
     node->range.start = start;
     node->range.end = end;

     if(!itr){
          node->next = NULL;
     }else{
          node->next = itr;
     }

     if(prev){
          prev->next = node;
     }else{
          list->head = node;
     }

     return true;
}

void ce_range_list_free(CeRangeList_t* list){
     CeRangeNode_t* itr = list->head;
     while(itr){
          CeRangeNode_t* tmp = itr;
          itr = itr->next;
          free(tmp);
     }

     list->head = NULL;
     list->tail = NULL;
}

int ce_draw_color_list_last_fg_color(CeDrawColorList_t* draw_color_list){
     int fg = COLOR_DEFAULT;
     if(draw_color_list->tail) fg = draw_color_list->tail->fg;
     return fg;
}

int ce_draw_color_list_last_bg_color(CeDrawColorList_t* draw_color_list){
     int bg = COLOR_DEFAULT;
     if(draw_color_list->tail) bg = draw_color_list->tail->bg;
     return bg;
}

int ce_draw_color_list_next_to_last_fg_color(CeDrawColorList_t* draw_color_list){
     int fg = COLOR_DEFAULT;
     if(draw_color_list->tail && (draw_color_list->head != draw_color_list->tail)){
          CeDrawColorNode_t* itr = draw_color_list->head;
          while(itr->next != draw_color_list->tail) itr = itr->next;
          fg = itr->fg;
     }
     return fg;
}

int ce_color_def_get(CeColorDefs_t* color_defs, int fg, int bg){
     // search for the already defined color
     for(int32_t i = 0; i < color_defs->count; ++i){
          if(color_defs->pairs[i].fg == fg && color_defs->pairs[i].bg == bg){
               return i;
          }
     }

     // increment the color pair we are going to define, but make sure it wraps around to 0 at the max
     color_defs->current++;
     color_defs->current %= 256;
     if(color_defs->current <= 0) color_defs->current = 1; // when we wrap around, start at 1, because curses doesn't like 0 index color pairs

     // create the pair definition
     init_pair(color_defs->current, fg, bg);

     // set our internal definition
     color_defs->pairs[color_defs->current].fg = fg;
     color_defs->pairs[color_defs->current].bg = bg;

     if(color_defs->current >= color_defs->count){
          color_defs->count = color_defs->current + 1;
     }

     return color_defs->current;
}

static bool is_c_type_char(int ch){
     return isalnum(ch) || ch == '_';
}

static int64_t match_words(const char* str, const char* beginning_of_line, const char** words, int64_t word_count){
     for(int64_t i = 0; i < word_count; ++i){
          int64_t word_len = strlen(words[i]);
          if(strncmp(words[i], str, word_len) == 0){
               // make sure word isn't in the middle of an identifier
               char post_char = str[word_len];
               if(is_c_type_char(post_char)) return 0;
               if(str > beginning_of_line){
                    char pre_char = *(str - 1);
                    if(is_c_type_char(pre_char)) return 0;
               }

               return word_len;
          }
     }

     return 0;
}

static int64_t match_c_type(const char* str, const char* beginning_of_line, bool cpp){
     if(!isalpha(*str)) return false;

     const char* itr = str;
     while(*itr){
          if(!is_c_type_char(*itr)) break;
          itr++;
     }

     int64_t len = itr - str;
     if(len > 1){
          if(strncmp((itr - 2), "_t", 2) == 0) return len;
          if(strncmp((itr - 2), "_h", 2) == 0) return len;
          if(cpp && isupper(*str)) return len;
     }

     static const char* keywords[] = {
          "bool",
          "char",
          "double",
          "float",
          "int",
          "long",
          "short",
          "signed",
          "unsigned",
          "void",
          "S8",
          "S16",
          "S32",
          "S64",
          "U8",
          "U16",
          "U32",
          "U64",
          "F32",
          "F64",
     };

     static const int64_t keyword_count = sizeof(keywords) / sizeof(keywords[0]);

     return match_words(str, beginning_of_line, keywords, keyword_count);
}

static int64_t match_c_keyword(const char* str, const char* beginning_of_line){
     static const char* keywords[] = {
          "__thread",
          "auto",
          "case",
          "default",
          "do",
          "else",
          "enum",
          "extern",
          "false",
          "for",
          "if",
          "inline",
          "register",
          "sizeof",
          "static",
          "struct",
          "switch",
          "true",
          "typedef",
          "typeof",
          "union",
          "volatile",
          "while",
     };

     static const int64_t keyword_count = sizeof(keywords) / sizeof(keywords[0]);

     return match_words(str, beginning_of_line, keywords, keyword_count);
}

static int64_t match_c_control(const char* str, const char* beginning_of_line){
     static const char* keywords [] = {
          "break",
          "const",
          "continue",
          "goto",
          "return",
     };

     static const int64_t keyword_count = sizeof(keywords) / sizeof(keywords[0]);

     return match_words(str, beginning_of_line, keywords, keyword_count);
}

static bool is_caps_var_char(int ch){
     return (ch >= 'A' && ch <= 'Z') || ch == '_' || (ch >= '0' && ch <= '9');
}

static bool is_starting_caps_var_char(int ch){
     return (ch >= 'A' && ch <= 'Z') || ch == '_';
}

static int64_t match_caps_var(const char* str, const char* beginning_of_line){
     const char* itr = str;
     if(!is_starting_caps_var_char(*itr)) return 0; // make sure the first char is not a number
     itr++;
     while(*itr){
          if(!is_caps_var_char(*itr)) break;
          itr++;
     }

     // check before and after the match for non c type chars
     if(str > beginning_of_line){
          if(is_c_type_char(*(str - 1))) return 0;
     }
     if(is_c_type_char(*itr)) return 0;

     int64_t len = itr - str;
     if(len > 1) return len;
     return 0;
}

static int64_t match_c_preproc(const char* str){
     if(*str == '#'){
          const char* itr = str + 1;
          while(*itr){
               if(!isalpha(*itr)) break;
               itr++;
          }

          return itr - str;
     }

     return 0;
}

static int64_t utf8_strlen_until_trailing_whitespace(const char* str){
     const char* itr = str;
     while(*itr) itr++;
     itr--;
     while(itr > str && *itr && isblank(*itr)) itr--;
     return ce_utf8_strlen_between(str, itr);
}

static int64_t match_c_comment(const char* str){
     if(strncmp("//", str, 2) == 0) return utf8_strlen_until_trailing_whitespace(str);
     return 0;
}

static int64_t match_c_multiline_comment(const char* str){
     if(strncmp("/*", str, 2) == 0){
          char* matching_comment = strstr(str, "*/");
          if(matching_comment) return (matching_comment - str);
          return utf8_strlen_until_trailing_whitespace(str);
     }

     return 0;
}

static int64_t match_c_multiline_comment_end(const char* str){
     if(strncmp("*/", str, 2) == 0) return 2;

     return 0;
}

static int64_t match_c_string(const char* str){
     if(*str == '"'){
          const char* match = str;
          while(match){
               match = strchr(match + 1, '"');
               if(match && *(match - 1) != '\\'){
                    return (match - str) + 1;
               }
          }
     }

     return 0;
}

static int64_t match_c_character_literal(const char* str){
     // c character literals are one character long unless that character is escaped
     // in which case the literal will be 2 characters long before we see an end '
     if(*str == '\''){
          switch(str[1]){
          case 0: return 0;
          case '\'': return 2;
          case '\\':
               // our next character is escaped. skip it unless its the end of the string
               if(!str[2]) return 0;

               // our next character must be an end quote or we are not in a character literal
               if(str[3] == '\'') return 4;
          default:
               if(str[2] == '\'') return 3;
          }
     }

     return 0;
}

static int64_t match_c_literal(const char* str, const char* beginning_of_line){
     const char* itr = str;
     int64_t count = 0;
     char ch = *itr;
     bool seen_decimal = false;
     bool seen_hex = false;
     bool seen_u = false;
     bool seen_digit = false;
     int seen_l = 0;

     while(ch != 0){
          if(isdigit(ch)){
               if(seen_u || seen_l) break;
               seen_digit = true;
               count++;
          }else if(!seen_decimal && ch == '.'){
               if(seen_u || seen_l) break;
               seen_decimal = true;
               count++;
          }else if(ch == 'f' && seen_decimal){
               if(seen_u || seen_l) break;
               count++;
               break;
          }else if(ch == '-' && itr == str){
               count++;
          }else if(ch == 'x' && itr == (str + 1)){
               seen_hex = true;
               count++;
          }else if((ch == 'u' || ch == 'U') && !seen_u){
               seen_u = true;
               count++;
          }else if((ch == 'l' || ch == 'L') && seen_l < 2){
               seen_l++;
               count++;
          }else if(seen_hex){
               if(seen_u || seen_l) break;

               bool valid_hex_char = false;

               switch(ch){
               default:
                    break;
               case 'a':
               case 'b':
               case 'c':
               case 'd':
               case 'e':
               case 'f':
               case 'A':
               case 'B':
               case 'C':
               case 'D':
               case 'E':
               case 'F':
                    count++;
                    valid_hex_char = true;
                    break;
               }

               if(!valid_hex_char) break;
          }else{
               break;
          }

          itr++;
          ch = *itr;
     }

     if(count == 1 && (str[0] == '-' || str[0] == '.')) return 0;
     if(!seen_digit) return 0;

     // check if the previous character is not a delimiter
     if(str > beginning_of_line){
          const char* prev = str - 1;
          if(is_caps_var_char(*prev) || isalpha(*prev)) return 0;
     }

     return count;
}

static int64_t match_trailing_whitespace(const char* str){
     const char* itr = str;
     while(*itr){
          if(!isspace(*itr)) return 0;
          itr++;
     }

     return (itr - str);
}

static void change_draw_color(CeDrawColorList_t* draw_color_list, CeSyntaxDef_t* syntax_defs, CeSyntaxColor_t syntax_color, CePoint_t point){
     int fg = ce_syntax_def_get_fg(syntax_defs, syntax_color, ce_draw_color_list_last_fg_color(draw_color_list));
     int bg = ce_syntax_def_get_bg(syntax_defs, syntax_color, ce_draw_color_list_last_bg_color(draw_color_list));
     ce_draw_color_list_insert(draw_color_list, fg, bg, point);
}

void check_visual_start(CeRangeNode_t* range_node, int64_t line, CeDrawColorList_t* draw_color_list,
                        CeSyntaxDef_t* syntax_defs, bool* in_visual){
     if(range_node){
          CePoint_t start = {0, line};
          if(ce_point_after(start, range_node->range.start) && !ce_point_after(start, range_node->range.end)){
               int bg = ce_syntax_def_get_bg(syntax_defs, CE_SYNTAX_COLOR_VISUAL, ce_draw_color_list_last_bg_color(draw_color_list));
               int fg = ce_syntax_def_get_fg(syntax_defs, CE_SYNTAX_COLOR_VISUAL, ce_draw_color_list_last_fg_color(draw_color_list));
               ce_draw_color_list_insert(draw_color_list, fg, bg, start);
               *in_visual = true;
          }
     }
}

void ce_syntax_highlight_visual(CeRangeNode_t** range_node, bool* in_visual, CePoint_t point, CeDrawColorList_t* draw_color_list,
                                CeSyntaxDef_t* syntax_defs){
     if(*range_node){
          if(*in_visual){
               if(ce_point_after(point, (*range_node)->range.end)){
                    if(syntax_defs[CE_SYNTAX_COLOR_VISUAL].fg == CE_SYNTAX_USE_CURRENT_COLOR){
                         // if the syntax def for visual fg is use current color, then keep that one
                         ce_draw_color_list_insert(draw_color_list, ce_draw_color_list_last_fg_color(draw_color_list),
                                                   COLOR_DEFAULT, point);
                    }else{
                         // use the 2nd to last color, because we have been overriding each color with visual colors
                         ce_draw_color_list_insert(draw_color_list, ce_draw_color_list_next_to_last_fg_color(draw_color_list),
                                                   COLOR_DEFAULT, point);
                    }

                    *range_node = (*range_node)->next;
                    *in_visual = false;
               }
          }else{
               if(ce_points_equal(point, (*range_node)->range.start)){
                    int bg = ce_syntax_def_get_bg(syntax_defs, CE_SYNTAX_COLOR_VISUAL, ce_draw_color_list_last_bg_color(draw_color_list));
                    int fg = ce_syntax_def_get_fg(syntax_defs, CE_SYNTAX_COLOR_VISUAL, ce_draw_color_list_last_fg_color(draw_color_list));
                    ce_draw_color_list_insert(draw_color_list, fg, bg, point);
                    *in_visual = true;
               }else if(ce_point_after(point, (*range_node)->range.end)){
                    *range_node = (*range_node)->next;
               }
          }
     }
}

void check_visual_mode_end(CeRangeNode_t* range_node, bool* in_visual, int64_t line, int64_t line_len,
                           CeDrawColorList_t* draw_color_list){
     if(!range_node) return;

     CePoint_t point = {line_len, line};
     if(in_visual){
          if(ce_point_after(point, range_node->range.end)){
               ce_draw_color_list_insert(draw_color_list, ce_draw_color_list_last_fg_color(draw_color_list),
                                         COLOR_DEFAULT, point);
               range_node = range_node->next;
               *in_visual = false;
          }
     }
}

void ce_syntax_highlight_c(CeView_t* view, CeRangeList_t* highlight_range_list, CeDrawColorList_t* draw_color_list,
                           CeSyntaxDef_t* syntax_defs, void* user_data){
     if(!view->buffer) return;
     if(view->buffer->line_count <= 0) return;
     int64_t min = view->scroll.y;
     int64_t max = min + (view->rect.bottom - view->rect.top);
     int64_t clamp_max = (view->buffer->line_count - 1);
     if(clamp_max < 0) clamp_max = 0;
     CE_CLAMP(min, 0, clamp_max);
     CE_CLAMP(max, 0, clamp_max);
     int64_t match_len = 0;
     bool multiline_comment = false;
     bool in_visual = false;
     CeRangeNode_t* range_node = highlight_range_list->head;

     check_visual_start(range_node, min, draw_color_list, syntax_defs, &in_visual);

     int prepass_min = min - SEARCH_BACK_LINES;
     if(prepass_min < 0) prepass_min = 0;

     // pre-pass to check if we are in a multiline comment
     for(int64_t y = prepass_min; y < min; y++){
          char* line = view->buffer->lines[y];
          int64_t line_len = ce_utf8_strlen(line);

          for(int64_t x = 0; x < line_len; ++x){
               char* str = ce_utf8_iterate_to(line, x);

               if(multiline_comment){
                    if(match_c_multiline_comment_end(str)){
                         multiline_comment = false;
                    }
               }else{
                    if((match_len = match_c_multiline_comment(str))){
                         multiline_comment = true;
                         x += (match_len - 1);
                    }
               }
          }
     }

     for(int64_t y = min; y <= max; ++y){
          char* line = view->buffer->lines[y];
          int64_t line_len = ce_utf8_strlen(line);
          int64_t current_match_len = 1;
          CePoint_t match_point = {0, y};

          if(multiline_comment){
               change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_COMMENT, match_point);
          }

          ce_syntax_highlight_visual(&range_node, &in_visual, match_point, draw_color_list, syntax_defs);

          if(in_visual && line_len == 0) change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_VISUAL, match_point);

          for(int64_t x = 0; x < line_len; ++x){
               char* str = ce_utf8_iterate_to(line, x);
               match_point.x = x;

               ce_syntax_highlight_visual(&range_node, &in_visual, match_point, draw_color_list, syntax_defs);

               if(current_match_len <= 1){
                    if(multiline_comment){
                         if((match_len = match_c_multiline_comment_end(str))){
                              multiline_comment = false;
                         }else if(((view->cursor.y != y) || (x > view->cursor.x)) && (match_len = match_trailing_whitespace(str))){
                              change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_TRAILING_WHITESPACE, match_point);
                              ce_draw_color_list_insert(draw_color_list, ce_syntax_def_get_fg(syntax_defs, CE_SYNTAX_COLOR_NORMAL, COLOR_DEFAULT),
                                                        ce_syntax_def_get_bg(syntax_defs, CE_SYNTAX_COLOR_NORMAL, COLOR_DEFAULT),
                                                        (CePoint_t){0, match_point.y + 1});
                         }
                    }else{
                         if((match_len = match_c_type(str, line, false))){
                              change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_TYPE, match_point);
                         }else if((match_len = match_c_keyword(str, line))){
                              change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_KEYWORD, match_point);
                         }else if((match_len = match_c_control(str, line))){
                              change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_CONTROL, match_point);
                         }else if((match_len = match_caps_var(str, line))){
                              change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_CAPS_VAR, match_point);
                         }else if((match_len = match_c_comment(str))){
                              change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_COMMENT, match_point);
                         }else if((match_len = match_c_string(str))){
                              change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_STRING, match_point);
                         }else if((match_len = match_c_character_literal(str))){
                              change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_CHAR_LITERAL, match_point);
                         }else if((match_len = match_c_literal(str, line))){
                              change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_NUMBER_LITERAL, match_point);
                         }else if((match_len = match_c_preproc(str))){
                              change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_PREPROCESSOR, match_point);
                         }else if((match_len = match_c_multiline_comment(str))){
                              change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_COMMENT, match_point);
                              multiline_comment = true;
                         }else if(((view->cursor.y != y) || (x > view->cursor.x)) && (match_len = match_trailing_whitespace(str))){
                              change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_TRAILING_WHITESPACE, match_point);
                              ce_draw_color_list_insert(draw_color_list, ce_syntax_def_get_fg(syntax_defs, CE_SYNTAX_COLOR_NORMAL, COLOR_DEFAULT),
                                                        ce_syntax_def_get_bg(syntax_defs, CE_SYNTAX_COLOR_NORMAL, COLOR_DEFAULT),
                                                        (CePoint_t){0, match_point.y + 1});
                         }else if(!draw_color_list->tail || (draw_color_list->tail->fg != COLOR_DEFAULT || draw_color_list->tail->bg != COLOR_DEFAULT)){
                              change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_NORMAL, match_point);
                         }
                    }

                    if(match_len) current_match_len = match_len;
               }else{
                    current_match_len--;
               }

               if(in_visual) change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_VISUAL, match_point);
          }

          check_visual_mode_end(range_node, &in_visual, match_point.y, line_len, draw_color_list);
     }
}

static int64_t match_cpp_keyword(const char* str, const char* beginning_of_line){
     static const char* keywords[] = {
          "Asm",
          "auto",
          "bool",
          "case",
          "char",
          "class",
          "const_cast",
          "constexpr",
          "default",
          "delete",
          "do",
          "double",
          "else",
          "enum",
          "dynamic_cast",
          "extern",
          "false",
          "float",
          "for",
          "union",
          "unsigned",
          "using",
          "friend",
          "if",
          "inline",
          "int",
          "long",
          "mutable",
          "virtual",
          "namespace",
          "new",
          "nullptr",
          "operator",
          "private",
          "protected",
          "public",
          "register",
          "void",
          "reinterpret_cast",
          "short",
          "signed",
          "sizeof",
          "static",
          "static_cast",
          "volatile",
          "struct",
          "switch",
          "template",
          "this",
          "true",
          "typedef",
          "typeid",
          "typename",
          "unsigned",
          "wchar_t",
          "while",
     };

     static const int64_t keyword_count = sizeof(keywords) / sizeof(keywords[0]);

     return match_words(str, beginning_of_line, keywords, keyword_count);
}

static int64_t match_cpp_control(const char* str, const char* beginning_of_line){
     static const char* keywords [] = {
          "break",
          "catch",
          "const",
          "constexpr",
          "continue",
          "goto",
          "return",
          "throw",
          "try",
     };

     static const int64_t keyword_count = sizeof(keywords) / sizeof(keywords[0]);

     return match_words(str, beginning_of_line, keywords, keyword_count);
}

static int64_t match_cpp_namespace(const char* str, const char* beginning_of_line){
     bool saw_colon = false;
     bool saw_alpha = false;
     const char* itr = str;
     while(*itr){
          if(isalpha(*itr) || *itr == '_'){
               if(saw_colon) break;
               saw_alpha = true;
          }else if(*itr == ':'){
               if(!saw_alpha) break;

               if(!saw_colon){
                    saw_colon = true;
               }else{
                    return (itr - str) - 1;
               }
          }else{
               break;
          }
          itr++;
     }

     return 0;
}

void ce_syntax_highlight_cpp(CeView_t* view, CeRangeList_t* highlight_range_list, CeDrawColorList_t* draw_color_list,
                             CeSyntaxDef_t* syntax_defs, void* user_data){
     if(!view->buffer) return;
     if(view->buffer->line_count <= 0) return;
     int64_t min = view->scroll.y;
     int64_t max = min + (view->rect.bottom - view->rect.top);
     int64_t clamp_max = (view->buffer->line_count - 1);
     if(clamp_max < 0) clamp_max = 0;
     CE_CLAMP(min, 0, clamp_max);
     CE_CLAMP(max, 0, clamp_max);
     int64_t match_len = 0;
     bool multiline_comment = false;
     bool in_visual = false;
     CeRangeNode_t* range_node = highlight_range_list->head;

     check_visual_start(range_node, min, draw_color_list, syntax_defs, &in_visual);

     int prepass_min = min - SEARCH_BACK_LINES;
     if(prepass_min < 0) prepass_min = 0;

     // pre-pass to check if we are in a multiline comment
     for(int64_t y = prepass_min; y < min; y++){
          char* line = view->buffer->lines[y];
          int64_t line_len = ce_utf8_strlen(line);

          for(int64_t x = 0; x < line_len; ++x){
               char* str = ce_utf8_iterate_to(line, x);

               if(multiline_comment){
                    if(match_c_multiline_comment_end(str)){
                         multiline_comment = false;
                    }
               }else{
                    if((match_len = match_c_multiline_comment(str))){
                         multiline_comment = true;
                         x += (match_len - 1);
                    }
               }
          }
     }

     for(int64_t y = min; y <= max; ++y){
          char* line = view->buffer->lines[y];
          int64_t line_len = ce_utf8_strlen(line);
          int64_t current_match_len = 1;
          CePoint_t match_point = {0, y};

          if(multiline_comment){
               change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_COMMENT, match_point);
          }

          ce_syntax_highlight_visual(&range_node, &in_visual, match_point, draw_color_list, syntax_defs);

          if(in_visual && line_len == 0) change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_VISUAL, match_point);

          for(int64_t x = 0; x < line_len; ++x){
               char* str = ce_utf8_iterate_to(line, x);
               match_point.x = x;

               ce_syntax_highlight_visual(&range_node, &in_visual, match_point, draw_color_list, syntax_defs);

               if(current_match_len <= 1){
                    if(multiline_comment){
                         if((match_len = match_c_multiline_comment_end(str))){
                              multiline_comment = false;
                         }else if(((view->cursor.y != y) || (x > view->cursor.x)) && (match_len = match_trailing_whitespace(str))){
                              change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_TRAILING_WHITESPACE, match_point);
                              ce_draw_color_list_insert(draw_color_list, ce_syntax_def_get_fg(syntax_defs, CE_SYNTAX_COLOR_NORMAL, COLOR_DEFAULT),
                                                        ce_syntax_def_get_bg(syntax_defs, CE_SYNTAX_COLOR_NORMAL, COLOR_DEFAULT),
                                                        (CePoint_t){0, match_point.y + 1});
                         }
                    }else{
                         if((match_len = match_caps_var(str, line))){
                              change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_CAPS_VAR, match_point);
                         }else if((match_len = match_c_type(str, line, true))){
                              change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_TYPE, match_point);
                         }else if((match_len = match_cpp_keyword(str, line))){
                              change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_KEYWORD, match_point);
                         }else if((match_len = match_cpp_control(str, line))){
                              change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_CONTROL, match_point);
                         }else if((match_len = match_c_comment(str))){
                              change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_COMMENT, match_point);
                         }else if((match_len = match_c_string(str))){
                              change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_STRING, match_point);
                         }else if((match_len = match_c_character_literal(str))){
                              change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_CHAR_LITERAL, match_point);
                         }else if((match_len = match_c_literal(str, line))){
                              change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_NUMBER_LITERAL, match_point);
                         }else if((match_len = match_c_preproc(str))){
                              change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_PREPROCESSOR, match_point);
                         }else if((match_len = match_cpp_namespace(str, line))){
                              change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_TYPE, match_point);
                         }else if((match_len = match_c_multiline_comment(str))){
                              change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_COMMENT, match_point);
                              multiline_comment = true;
                         }else if(((view->cursor.y != y) || (x > view->cursor.x)) && (match_len = match_trailing_whitespace(str))){
                              change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_TRAILING_WHITESPACE, match_point);
                              ce_draw_color_list_insert(draw_color_list, ce_syntax_def_get_fg(syntax_defs, CE_SYNTAX_COLOR_NORMAL, COLOR_DEFAULT),
                                                        ce_syntax_def_get_bg(syntax_defs, CE_SYNTAX_COLOR_NORMAL, COLOR_DEFAULT),
                                                        (CePoint_t){0, match_point.y + 1});
                         }else if(!draw_color_list->tail || (draw_color_list->tail->fg != COLOR_DEFAULT || draw_color_list->tail->bg != COLOR_DEFAULT)){
                              change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_NORMAL, match_point);
                         }
                    }

                    if(match_len) current_match_len = match_len;
               }else{
                    current_match_len--;
               }

               if(in_visual) change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_VISUAL, match_point);
          }

          check_visual_mode_end(range_node, &in_visual, match_point.y, line_len, draw_color_list);
     }
}

static int64_t match_java_type(const char* str, const char* beginning_of_line){
     static const char* keywords[] = {
          "boolean",
          "byte",
          "float",
          "int",
          "long",
          "short",
          "void",
     };

     static const int64_t keyword_count = sizeof(keywords) / sizeof(keywords[0]);

     return match_words(str, beginning_of_line, keywords, keyword_count);
}

static int64_t match_java_keyword(const char* str, const char* beginning_of_line){
     static const char* keywords[] = {
          "abstract",
          "for",
          "new",
          "switch",
          "assert",
          "default",
          "package",
          "synchronized",
          "do",
          "if",
          "private",
          "this",
          "double",
          "implements",
          "protected",
          "else",
          "import",
          "public",
          "case",
          "enum",
          "instanceof",
          "transient",
          "extends",
          "char",
          "final",
          "interface",
          "static",
          "class",
          "strictfp",
          "volatile",
          "native",
          "super",
          "while",
     };

     static const int64_t keyword_count = sizeof(keywords) / sizeof(keywords[0]);

     return match_words(str, beginning_of_line, keywords, keyword_count);
}

static int64_t match_java_control(const char* str, const char* beginning_of_line){
     static const char* keywords [] = {
          "try",
          "continue",
          "goto",
          "break",
          "throw",
          "throws",
          "return",
          "catch",
          "finally",
          "const",

          "yield",
          "break",
          "except",
          "raise",
          "continue",
          "finally",
          "return",
          "try",
     };

     static const int64_t keyword_count = sizeof(keywords) / sizeof(keywords[0]);

     return match_words(str, beginning_of_line, keywords, keyword_count);
}

void ce_syntax_highlight_java(CeView_t* view, CeRangeList_t* highlight_range_list, CeDrawColorList_t* draw_color_list,
                              CeSyntaxDef_t* syntax_defs, void* user_data){
     if(!view->buffer) return;
     if(view->buffer->line_count <= 0) return;
     int64_t min = view->scroll.y;
     int64_t max = min + (view->rect.bottom - view->rect.top);
     int64_t clamp_max = (view->buffer->line_count - 1);
     if(clamp_max < 0) clamp_max = 0;
     CE_CLAMP(min, 0, clamp_max);
     CE_CLAMP(max, 0, clamp_max);
     int64_t match_len = 0;
     bool multiline_comment = false;
     bool in_visual = false;
     CeRangeNode_t* range_node = highlight_range_list->head;

     check_visual_start(range_node, min, draw_color_list, syntax_defs, &in_visual);

     for(int64_t y = min; y <= max; ++y){
          char* line = view->buffer->lines[y];
          int64_t line_len = ce_utf8_strlen(line);
          int64_t current_match_len = 1;
          CePoint_t match_point = {0, y};

          if(multiline_comment){
               change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_COMMENT, match_point);
          }

          ce_syntax_highlight_visual(&range_node, &in_visual, match_point, draw_color_list, syntax_defs);

          if(in_visual && line_len == 0) change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_VISUAL, match_point);

          for(int64_t x = 0; x < line_len; ++x){
               char* str = ce_utf8_iterate_to(line, x);
               match_point.x = x;

               ce_syntax_highlight_visual(&range_node, &in_visual, match_point, draw_color_list, syntax_defs);

               if(current_match_len <= 1){
                    if(multiline_comment){
                         if((match_len = match_c_multiline_comment_end(str))){
                              multiline_comment = false;
                         }
                    }else{
                         if((match_len = match_java_type(str, line))){
                              change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_TYPE, match_point);
                         }else if((match_len = match_java_keyword(str, line))){
                              change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_KEYWORD, match_point);
                         }else if((match_len = match_java_control(str, line))){
                              change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_CONTROL, match_point);
                         }else if((match_len = match_caps_var(str, line))){
                              change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_CAPS_VAR, match_point);
                         }else if((match_len = match_c_comment(str))){
                              change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_COMMENT, match_point);
                         }else if((match_len = match_c_string(str))){
                              change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_STRING, match_point);
                         }else if((match_len = match_c_character_literal(str))){
                              change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_CHAR_LITERAL, match_point);
                         }else if((match_len = match_c_literal(str, line))){
                              change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_NUMBER_LITERAL, match_point);
                         }else if((match_len = match_c_multiline_comment(str))){
                              change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_COMMENT, match_point);
                              multiline_comment = true;
                         }else if(((view->cursor.y != y) || (x > view->cursor.x)) && (match_len = match_trailing_whitespace(str))){
                              change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_TRAILING_WHITESPACE, match_point);
                              ce_draw_color_list_insert(draw_color_list, ce_syntax_def_get_fg(syntax_defs, CE_SYNTAX_COLOR_NORMAL, COLOR_DEFAULT),
                                                        ce_syntax_def_get_bg(syntax_defs, CE_SYNTAX_COLOR_NORMAL, COLOR_DEFAULT),
                                                        (CePoint_t){0, match_point.y + 1});
                         }else if(!draw_color_list->tail || (draw_color_list->tail->fg != COLOR_DEFAULT || draw_color_list->tail->bg != COLOR_DEFAULT)){
                              change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_NORMAL, match_point);
                         }
                    }

                    if(match_len) current_match_len = match_len;
               }else{
                    current_match_len--;
               }

               if(in_visual) change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_VISUAL, match_point);
          }

          check_visual_mode_end(range_node, &in_visual, match_point.y, line_len, draw_color_list);
     }
}

static int64_t match_python_keyword(const char* str, const char* beginning_of_line){
     static const char* keywords[] = {
          "and",
          "del",
          "from",
          "not",
          "while",
          "as",
          "elif",
          "global",
          "or",
          "with",
          "assert",
          "else",
          "if",
          "pass",
          "import",
          "print",
          "class",
          "exec",
          "in",
          "is",
          "def",
          "for",
          "lambda",
          "self",
     };

     static const int64_t keyword_count = sizeof(keywords) / sizeof(keywords[0]);

     return match_words(str, beginning_of_line, keywords, keyword_count);
}

static int64_t match_python_control(const char* str, const char* beginning_of_line){
     static const char* keywords[] = {
          "yield",
          "break",
          "except",
          "raise",
          "continue",
          "finally",
          "return",
          "try",
     };

     static const int64_t keyword_count = sizeof(keywords) / sizeof(keywords[0]);

     return match_words(str, beginning_of_line, keywords, keyword_count);
}

static int64_t match_python_comment(const char* str){
     if(*str == '#') return ce_utf8_strlen(str);

     return 0;
}

static int64_t match_python_string(const char* str){
     if(*str == '\'' || *str == '"'){
          const char* match = str;
          while(match){
               match = strchr(match + 1, *str);
               if(match && *(match - 1) != '\\'){
                    return ce_utf8_strlen_between(str, match);
               }
          }
     }

     return 0;
}

typedef enum{
     PYTHON_DOCSTRING_NONE,
     PYTHON_DOCSTRING_SINGLE_QUOTE,
     PYTHON_DOCSTRING_DOUBLE_QUOTE,
}PythonDocstring_t;

static int64_t match_python_docstring(const char* str, PythonDocstring_t* python_docstring){
     if(strncmp(str, "\"\"\"", 3) == 0){
          *python_docstring = PYTHON_DOCSTRING_DOUBLE_QUOTE;
          char* match = strstr(str, "\"\"\"");
          if(match) return ce_utf8_strlen_between(str, match);
          return ce_utf8_strlen(str);
     }

     if(strncmp(str, "'''", 3) == 0){
          *python_docstring = PYTHON_DOCSTRING_SINGLE_QUOTE;
          char* match = strstr(str, "'''");
          if(match) return ce_utf8_strlen_between(str, match);
          return ce_utf8_strlen(str);
     }

     return 0;
}

void ce_syntax_highlight_python(CeView_t* view, CeRangeList_t* highlight_range_list, CeDrawColorList_t* draw_color_list,
                                CeSyntaxDef_t* syntax_defs, void* user_data){
     if(!view->buffer) return;
     if(view->buffer->line_count <= 0) return;
     int64_t min = view->scroll.y;
     int64_t max = min + (view->rect.bottom - view->rect.top);
     int64_t clamp_max = (view->buffer->line_count - 1);
     if(clamp_max < 0) clamp_max = 0;
     CE_CLAMP(min, 0, clamp_max);
     CE_CLAMP(max, 0, clamp_max);
     int64_t match_len = 0;
     bool in_visual = false;
     PythonDocstring_t docstring = PYTHON_DOCSTRING_NONE;
     CeRangeNode_t* range_node = highlight_range_list->head;

     check_visual_start(range_node, min, draw_color_list, syntax_defs, &in_visual);

     int prepass_min = min - SEARCH_BACK_LINES;
     if(prepass_min < 0) prepass_min = 0;

     // pre-pass to check if we are in a multiline comment
     for(int64_t y = prepass_min; y < min; y++){
          char* line = view->buffer->lines[y];
          int64_t line_len = ce_utf8_strlen(line);

          for(int64_t x = 0; x < line_len; ++x){
               char* str = ce_utf8_iterate_to(line, x);

               switch(docstring){
               default:
                    if((match_len = match_python_docstring(str, &docstring))){
                         x += match_len;
                    }
                    break;
               case PYTHON_DOCSTRING_DOUBLE_QUOTE:
                    if(strncmp(str, "\"\"\"", 3) == 0){
                         docstring = PYTHON_DOCSTRING_NONE;
                         x += 3;
                    }else if(match_len == 0){
                         docstring = PYTHON_DOCSTRING_NONE;
                    }
                    break;
               case PYTHON_DOCSTRING_SINGLE_QUOTE:
                    if(strncmp(str, "'''", 3) == 0){
                         docstring = PYTHON_DOCSTRING_NONE;
                         x += 3;
                    }else if(match_len == 0){
                         docstring = PYTHON_DOCSTRING_NONE;
                    }
                    break;
               }
          }
     }

     for(int64_t y = min; y <= max; ++y){
          char* line = view->buffer->lines[y];
          int64_t line_len = ce_utf8_strlen(line);
          int64_t current_match_len = 1;
          CePoint_t match_point = {0, y};

          if(docstring){
               change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_STRING, match_point);
          }

          ce_syntax_highlight_visual(&range_node, &in_visual, match_point, draw_color_list, syntax_defs);

          if(in_visual && line_len == 0) change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_VISUAL, match_point);

          for(int64_t x = 0; x < line_len; ++x){
               char* str = ce_utf8_iterate_to(line, x);
               match_point.x = x;

               ce_syntax_highlight_visual(&range_node, &in_visual, match_point, draw_color_list, syntax_defs);

               if(current_match_len <= 1){
                    if(docstring){
                         if(docstring == PYTHON_DOCSTRING_DOUBLE_QUOTE && strncmp(str, "\"\"\"", 3) == 0){
                              docstring = PYTHON_DOCSTRING_NONE;
                              match_len = 3;
                         }else if(docstring == PYTHON_DOCSTRING_SINGLE_QUOTE && strncmp(str, "'''", 3) == 0){
                              docstring = PYTHON_DOCSTRING_NONE;
                              match_len = 3;
                         }
                    }else{
                         if((match_len = match_c_type(str, line, false))){
                              change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_TYPE, match_point);
                         }else if((match_len = match_python_keyword(str, line))){
                              change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_KEYWORD, match_point);
                         }else if((match_len = match_python_control(str, line))){
                              change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_CONTROL, match_point);
                         }else if((match_len = match_caps_var(str, line))){
                              change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_CAPS_VAR, match_point);
                         }else if((match_len = match_python_comment(str))){
                              change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_COMMENT, match_point);
                         }else if((match_len = match_python_docstring(str, &docstring))){
                              change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_STRING, match_point);
                         }else if((match_len = match_python_string(str))){
                              change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_STRING, match_point);
                         }else if((match_len = match_c_literal(str, line))){
                              change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_NUMBER_LITERAL, match_point);
                         }else if(((view->cursor.y != y) || (x > view->cursor.x)) && (match_len = match_trailing_whitespace(str))){
                              change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_TRAILING_WHITESPACE, match_point);
                              ce_draw_color_list_insert(draw_color_list, ce_syntax_def_get_fg(syntax_defs, CE_SYNTAX_COLOR_NORMAL, COLOR_DEFAULT),
                                                        ce_syntax_def_get_bg(syntax_defs, CE_SYNTAX_COLOR_NORMAL, COLOR_DEFAULT),
                                                        (CePoint_t){0, match_point.y + 1});
                         }else if(!draw_color_list->tail || (draw_color_list->tail->fg != COLOR_DEFAULT || draw_color_list->tail->bg != COLOR_DEFAULT)){
                              change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_NORMAL, match_point);
                         }
                    }

                    if(match_len) current_match_len = match_len;
               }else{
                    current_match_len--;
               }

               if(in_visual) change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_VISUAL, match_point);
          }

          check_visual_mode_end(range_node, &in_visual, match_point.y, line_len, draw_color_list);
     }
}

static int64_t match_bash_keyword(const char* str, const char* beginning_of_line){
     static const char* keywords [] = {
          "if",
          "then",
          "else",
          "elif",
          "fi",
          "case",
          "esac",
          "for",
          "select",
          "while",
          "until",
          "do",
          "done",
          "in",
          "function",
          "time",
          "coproc",
     };

     static const int64_t keyword_count = sizeof(keywords) / sizeof(keywords[0]);

     return match_words(str, beginning_of_line, keywords, keyword_count);
}

void ce_syntax_highlight_bash(CeView_t* view, CeRangeList_t* highlight_range_list, CeDrawColorList_t* draw_color_list,
                              CeSyntaxDef_t* syntax_defs, void* user_data){
     if(!view->buffer) return;
     if(view->buffer->line_count <= 0) return;
     int64_t min = view->scroll.y;
     int64_t max = min + (view->rect.bottom - view->rect.top);
     int64_t clamp_max = (view->buffer->line_count - 1);
     if(clamp_max < 0) clamp_max = 0;
     CE_CLAMP(min, 0, clamp_max);
     CE_CLAMP(max, 0, clamp_max);
     int64_t match_len = 0;
     bool in_visual = false;
     CeRangeNode_t* range_node = highlight_range_list->head;

     check_visual_start(range_node, min, draw_color_list, syntax_defs, &in_visual);

     for(int64_t y = min; y <= max; ++y){
          char* line = view->buffer->lines[y];
          int64_t line_len = ce_utf8_strlen(line);
          int64_t current_match_len = 1;
          CePoint_t match_point = {0, y};

          ce_syntax_highlight_visual(&range_node, &in_visual, match_point, draw_color_list, syntax_defs);

          if(in_visual && line_len == 0) change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_VISUAL, match_point);

          for(int64_t x = 0; x < line_len; ++x){
               char* str = ce_utf8_iterate_to(line, x);
               match_point.x = x;

               ce_syntax_highlight_visual(&range_node, &in_visual, match_point, draw_color_list, syntax_defs);

               if(current_match_len <= 1){
                    if((match_len = match_bash_keyword(str, line))){
                         change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_KEYWORD, match_point);
                    }else if((match_len = match_caps_var(str, line))){
                         change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_CAPS_VAR, match_point);
                    }else if((match_len = match_python_comment(str))){
                         change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_COMMENT, match_point);
                    }else if((match_len = match_python_string(str))){
                         change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_STRING, match_point);
                    }else if((match_len = match_c_literal(str, line))){
                         change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_NUMBER_LITERAL, match_point);
                    }else if(((view->cursor.y != y) || (x > view->cursor.x)) && (match_len = match_trailing_whitespace(str))){
                         change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_TRAILING_WHITESPACE, match_point);
                         ce_draw_color_list_insert(draw_color_list, ce_syntax_def_get_fg(syntax_defs, CE_SYNTAX_COLOR_NORMAL, COLOR_DEFAULT),
                                                   ce_syntax_def_get_bg(syntax_defs, CE_SYNTAX_COLOR_NORMAL, COLOR_DEFAULT),
                                                   (CePoint_t){0, match_point.y + 1});
                    }else if(!draw_color_list->tail || (draw_color_list->tail->fg != COLOR_DEFAULT || draw_color_list->tail->bg != COLOR_DEFAULT)){
                         change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_NORMAL, match_point);
                    }

                    if(match_len) current_match_len = match_len;
               }else{
                    current_match_len--;
               }

               if(in_visual) change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_VISUAL, match_point);
          }

          check_visual_mode_end(range_node, &in_visual, match_point.y, line_len, draw_color_list);
     }
}

static int64_t match_config_keyword(const char* str, const char* beginning_of_line){
     static const char* keywords [] = {
          "true",
          "false",
     };

     static const int64_t keyword_count = sizeof(keywords) / sizeof(keywords[0]);

     return match_words(str, beginning_of_line, keywords, keyword_count);
}

void ce_syntax_highlight_config(CeView_t* view, CeRangeList_t* highlight_range_list, CeDrawColorList_t* draw_color_list,
                                CeSyntaxDef_t* syntax_defs, void* user_data){
     if(!view->buffer) return;
     if(view->buffer->line_count <= 0) return;
     int64_t min = view->scroll.y;
     int64_t max = min + (view->rect.bottom - view->rect.top);
     int64_t clamp_max = (view->buffer->line_count - 1);
     if(clamp_max < 0) clamp_max = 0;
     CE_CLAMP(min, 0, clamp_max);
     CE_CLAMP(max, 0, clamp_max);
     int64_t match_len = 0;
     bool in_visual = false;
     CeRangeNode_t* range_node = highlight_range_list->head;

     check_visual_start(range_node, min, draw_color_list, syntax_defs, &in_visual);

     for(int64_t y = min; y <= max; ++y){
          char* line = view->buffer->lines[y];
          int64_t line_len = ce_utf8_strlen(line);
          int64_t current_match_len = 1;
          CePoint_t match_point = {0, y};

          ce_syntax_highlight_visual(&range_node, &in_visual, match_point, draw_color_list, syntax_defs);

          if(in_visual && line_len == 0) change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_VISUAL, match_point);

          for(int64_t x = 0; x < line_len; ++x){
               char* str = ce_utf8_iterate_to(line, x); // TODO: decode rather than iterating to each time
               match_point.x = x;

               ce_syntax_highlight_visual(&range_node, &in_visual, match_point, draw_color_list, syntax_defs);

               if(current_match_len <= 1){
                    if((match_len = match_config_keyword(str, line))){
                         change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_KEYWORD, match_point);
                    }else if((match_len = match_caps_var(str, line))){
                         change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_CAPS_VAR, match_point);
                    }else if((match_len = match_python_comment(str))){
                         change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_COMMENT, match_point);
                    }else if((match_len = match_python_string(str))){
                         change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_STRING, match_point);
                    }else if((match_len = match_c_literal(str, line))){
                         change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_NUMBER_LITERAL, match_point);
                    }else if(((view->cursor.y != y) || (x > view->cursor.x)) && (match_len = match_trailing_whitespace(str))){
                         change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_TRAILING_WHITESPACE, match_point);
                         ce_draw_color_list_insert(draw_color_list, ce_syntax_def_get_fg(syntax_defs, CE_SYNTAX_COLOR_NORMAL, COLOR_DEFAULT),
                                                   ce_syntax_def_get_bg(syntax_defs, CE_SYNTAX_COLOR_NORMAL, COLOR_DEFAULT),
                                                   (CePoint_t){0, match_point.y + 1});
                    }else if(!draw_color_list->tail || (draw_color_list->tail->fg != COLOR_DEFAULT || draw_color_list->tail->bg != COLOR_DEFAULT)){
                         change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_NORMAL, match_point);
                    }

                    if(match_len) current_match_len = match_len;
               }else{
                    current_match_len--;
               }

               if(in_visual) change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_VISUAL, match_point);
          }

          check_visual_mode_end(range_node, &in_visual, match_point.y, line_len, draw_color_list);
     }
}

void ce_syntax_highlight_diff(CeView_t* view, CeRangeList_t* highlight_range_list, CeDrawColorList_t* draw_color_list,
                              CeSyntaxDef_t* syntax_defs, void* user_data){
     if(!view->buffer) return;
     if(view->buffer->line_count <= 0) return;
     int64_t min = view->scroll.y;
     int64_t max = min + (view->rect.bottom - view->rect.top);
     int64_t clamp_max = (view->buffer->line_count - 1);
     if(clamp_max < 0) clamp_max = 0;
     CE_CLAMP(min, 0, clamp_max);
     CE_CLAMP(max, 0, clamp_max);
     bool in_visual = false;
     CeRangeNode_t* range_node = highlight_range_list->head;

     check_visual_start(range_node, min, draw_color_list, syntax_defs, &in_visual);

     for(int64_t y = min; y <= max; ++y){
          char* line = view->buffer->lines[y];
          int64_t line_len = ce_utf8_strlen(line);
          int64_t current_match_len = 1;
          CePoint_t match_point = {0, y};

          ce_syntax_highlight_visual(&range_node, &in_visual, match_point, draw_color_list, syntax_defs);

          if(*line == '+'){
               change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_DIFF_ADD, match_point);
               current_match_len = line_len + 1;
          }else if(*line == '-'){
               change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_DIFF_REMOVE, match_point);
               current_match_len = line_len + 1;
          }else if(*line == '#'){
               change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_DIFF_COMMENT, match_point);
               current_match_len = line_len + 1;
          }else if(strncmp(line, "@@", 2) == 0){
               char* end = strstr(line + 2, "@@");
               if(end){
                    change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_DIFF_HEADER, match_point);
                    current_match_len = ce_utf8_strlen_between(line, end + 2);
               }
          }else{
               change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_NORMAL, match_point);
          }

          if(in_visual && line_len == 0) change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_VISUAL, match_point);

          for(int64_t x = 0; x < line_len; ++x){
               match_point.x = x;

               ce_syntax_highlight_visual(&range_node, &in_visual, match_point, draw_color_list, syntax_defs);

               if(current_match_len <= 1){
                    change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_NORMAL, match_point);
               }else{
                    current_match_len--;
               }

               if(in_visual) change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_VISUAL, match_point);
          }

          check_visual_mode_end(range_node, &in_visual, match_point.y, line_len, draw_color_list);
     }
}

void ce_syntax_highlight_plain(CeView_t* view, CeRangeList_t* highlight_range_list, CeDrawColorList_t* draw_color_list,
                               CeSyntaxDef_t* syntax_defs, void* user_data){
     if(!view->buffer) return;
     if(view->buffer->line_count <= 0) return;
     int64_t min = view->scroll.y;
     int64_t max = min + (view->rect.bottom - view->rect.top);
     int64_t clamp_max = (view->buffer->line_count - 1);
     if(clamp_max < 0) clamp_max = 0;
     CE_CLAMP(min, 0, clamp_max);
     CE_CLAMP(max, 0, clamp_max);
     bool in_visual = false;
     CeRangeNode_t* range_node = highlight_range_list->head;

     check_visual_start(range_node, min, draw_color_list, syntax_defs, &in_visual);

     for(int64_t y = min; y <= max; ++y){
          char* line = view->buffer->lines[y];
          int64_t line_len = ce_utf8_strlen(line);
          CePoint_t match_point = {0, y};

          if(in_visual && line_len == 0) change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_VISUAL, match_point);

          if(in_visual){
               int bg = ce_syntax_def_get_bg(syntax_defs, CE_SYNTAX_COLOR_VISUAL, ce_draw_color_list_last_bg_color(draw_color_list));
               ce_draw_color_list_insert(draw_color_list, ce_draw_color_list_last_fg_color(draw_color_list), bg, match_point);
          }

          for(int64_t x = 0; x < line_len; ++x){
               match_point.x = x;
               ce_syntax_highlight_visual(&range_node, &in_visual, match_point, draw_color_list, syntax_defs);
               if(in_visual) change_draw_color(draw_color_list, syntax_defs, CE_SYNTAX_COLOR_VISUAL, match_point);
          }

          check_visual_mode_end(range_node, &in_visual, match_point.y, line_len, draw_color_list);
     }
}


text:
#ifdef __cplusplus
#   define EXTERN extern "C"
#else
#   define EXTERN extern
#endif

#include "elg_pform_defs.h"
#include "../epik_topol.h"

EXTERN EPIK_TOPOL * EPIK_Pform_hw_topol();

EXTERN void EPIK_Pform_hw_topol_getcoords(EPIK_TOPOL * t);

redpajama_set_name: RedPajamaGithub


text:
Get rid of all the clutter on Ixquick's [Startpage](http://startpage.com) search results.

## Before:
![screenshot](https://github.com/lhagan/Ixquick-Cleaner/raw/master/before.jpg)

## After:
![screenshot](https://github.com/lhagan/Ixquick-Cleaner/raw/master/after.jpg)

## Download

Either [download the extension](https://github.com/lhagan/Ixquick-Cleaner/blob/master/Ixquick Cleaner.safariextz?raw=true) or just clone this repository.


text:
using System;
using System.Drawing;
using System.Windows.Forms;

namespace NAudio.Utils
{
    /// <summary>
    ///     A thread-safe Progress Log Control
    /// </summary>
    public partial class ProgressLog : UserControl
    {
        /// <summary>
        ///     Creates a new progress log control
        /// </summary>
        public ProgressLog()
        {
            InitializeComponent();
        }

        /// <summary>
        ///     The contents of the log as text
        /// </summary>
        public new string Text
        {
            get { return richTextBoxLog.Text; }
        }

        /// <summary>
        ///     Report progress from a progress event
        /// </summary>
        public void ReportProgress(ProgressEventArgs e)
        {
            Color color = Color.Black;
            if (e.MessageType == ProgressMessageType.Warning)
            {
                color = Color.Blue;
            }
            else if (e.MessageType == ProgressMessageType.Error)
            {
                color = Color.Red;
            }
            else if (e.MessageType == ProgressMessageType.Trace)
            {
                color = Color.Purple;
            }

            LogMessage(color, e.Message);
        }

        /// <summary>
        ///     Log a message
        /// </summary>
        public void LogMessage(Color color, string message)
        {
            if (richTextBoxLog.InvokeRequired)
            {
                Invoke(new LogMessageDelegate(LogMessage), new object[] {color, message});
            }
            else
            {
                richTextBoxLog.SelectionStart = richTextBoxLog.TextLength;
                richTextBoxLog.SelectionColor = color;
                richTextBoxLog.AppendText(message);
                richTextBoxLog.AppendText(Environment.NewLine);
            }
        }

        /// <summary>
        ///     Clear the log
        /// </summary>
        public void ClearLog()
        {
            if (richTextBoxLog.InvokeRequired)
            {
                Invoke(new ClearLogDelegate(ClearLog), new object[] {});
            }
            else
            {
                richTextBoxLog.Clear();
            }
        }

        private delegate void ClearLogDelegate();

        private delegate void LogMessageDelegate(Color color, string message);
    }
}


text:
"""Package contenant les différentes données de configuration."""

from secondaires.navigation.equipage.donnees.nb_abandon import NbAbandon


text:
class SkPicture;

namespace sky {
namespace shell {

class GPUDelegate {
 public:
  virtual void OnAcceleratedWidgetAvailable(gfx::AcceleratedWidget widget) = 0;
  virtual void OnOutputSurfaceDestroyed() = 0;
  virtual void Draw(scoped_ptr<compositor::LayerTree> layer_tree) = 0;

 protected:
  virtual ~GPUDelegate();
};

}  // namespace shell
}  // namespace sky

#endif  // SKY_SHELL_GPU_DELEGATE_H_


text:
//=================================================================================================

//=================================================================================================

//*************************************************************************************************
/*!\brief Cache size of the target architecture.
// \ingroup config
//
// This setting specifies the available cache size in Byte of the used target architecture.
// Several algorithms use this setting for an optimized evaluation.
//
// The size of the cache is specified in Byte. For instance, a cache of 3 MiByte must therefore
// be specified as 3145728.
//
// \note It is possible to specify the cache size via command line or by defining this symbol
// manually before including any Blaze header file:

   \code
   #define BLAZE_CACHE_SIZE 3145728UL
   #include <blaze/Blaze.h>
   \endcode
*/
#ifndef BLAZE_CACHE_SIZE
#define BLAZE_CACHE_SIZE 3145728UL;
#endif
//*************************************************************************************************


text:
<?php

use yii\helpers\Html;

/* @var $this yii\web\View */
/* @var $model backend\models\ClubBookingNames */

$this->title = 'Create Club Booking Names';
$this->params['breadcrumbs'][] = ['label' => 'Club Booking Names', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>

<?= Html::encode($this->title) ?>

    <?= $this->render('_form', [
        'model' => $model,
    ]) ?>


text:
layout: page
title: error-correction
permalink: /tag/error-correction
istag: 1
excerpt: "error-correction"
noindex: true
sitemap: false
---

{% include tag.html tag=page.title %}


text:
import { Component, QueryList, Input, ContentChildren, ElementRef, Renderer, AfterContentInit } from '@angular/core';
import { GridStackOptions } from './grid-stack-options.model';
import { GridStackItem } from './grid-stack-item.model';
import { GridStackItemComponent } from './grid-stack-item.component';
declare var jQuery: any;
declare var _: any;

@Component({
    selector: 'grid-stack',
    template: `<ng-content></ng-content>`,
    styles: [":host { display: block; }"]
})
export class GridStackComponent implements AfterContentInit {

    @Input() options: GridStackOptions = new GridStackOptions();
    @ContentChildren(GridStackItemComponent) items: QueryList<GridStackItemComponent>;
    private gridStack: any = null;
    private grid: any = null;
    private defaultOptions = {
      cellHeight: '60px',
      width: 12,
      height: 0,
      animate: true,
      float: false,
      resizable: true
    };

    constructor(private el: ElementRef, private renderer: Renderer) {
    }

    public makeWidget(item: GridStackItemComponent) {
        item.jGridRef = this.grid;
        if (item.option != null && item.option.noResize != null && item.option.noResize) {
          return;
        }

        this.grid.resizable(item.nativeElement, true);
        this.grid.move(item.nativeElement, item.option.x, item.option.y);
        this.grid.resize(item.nativeElement, item.option.width, item.option.height);
    };

    public updateWidget(item: GridStackItemComponent) {
        this.grid.resizable(item.nativeElement, true);
        this.grid.move(item.nativeElement, item.option.x, item.option.y);
        this.grid.resize(item.nativeElement, item.option.width, item.option.height);
    }

    public AddWidget(item: GridStackItemComponent) {
        item.jGridRef = this.grid;
        if (item.option != null && item.option.noResize != null && item.option.noResize) {
          return;
        }

        this.grid.resizable(item.nativeElement, true);
        this.grid.move(item.nativeElement, item.option.x, item.option.y);
        this.grid.resize(item.nativeElement, item.option.width, item.option.height);
    }

    public RemoveWidget(item: GridStackItemComponent) {
        this.grid.removeWidget(item.nativeElement, false);
    }

    ngAfterContentInit(): void {
        const that = this;
        let nativeElement = this.el.nativeElement;
        if (this.options == null) {
          this.options = new GridStackOptions();
        }

        for (const key of Object.keys(this.defaultOptions)) {
          if (!this.options.hasOwnProperty(key)) {
            this.options = this.defaultOptions[key];
          }
        }

        this.renderer.setElementAttribute(nativeElement, 'data-gs-width', String(this.options.width));
        this.renderer.setElementAttribute(nativeElement, 'data-gs-height', String(this.options.height));

        this.gridStack = jQuery(nativeElement).gridstack(this.options);
        this.grid = this.gridStack.data('gridstack');

        this.gridStack.on('change', (e: any, items: any) => {
            _.each(items, (item: any) => this.widgetChanged(item));
        });

        // Initialize widgets
        this.items.forEach(item => that.makeWidget(item));
    }

    private widgetChanged(change: GridStackItem): void {
        const jWidget = change.el;
        const gridStackItem = this.items.find(item => item.jWidgetRef !== null ? item.jWidgetRef === jWidget[0] : false);
        if (!gridStackItem) {
          return;
        }

        gridStackItem.update(change.x, change.y, change.width, change.height);
    }
}


text:
'''
    @brief:
    @author:
        Henry Zhou, March 31st, 2018
    @update:
'''

import init_path
import numpy as np
from util import model_gen_util
from env import model_gen

ROOT_PATH = init_path.get_abs_base_dir()
'''
    definition of nodes:
    @root:
        the 'root' node

'''
_NODE_TYPE = ['root', 'joint', 'body']  # geom is removed
EDGE_TYPE = {'self_loop': 0, 'root-root': 0,  # root-root is loop, also 0
             'joint-joint': 1, 'geom-geom': 2, 'body-body': 3, 'tendon': 4,
             'joint-geom': 5, 'geom-joint': -5,  # pc-relationship
             'joint-body': 6, 'body-joint': -6,
             'body-geom': 7, 'geom-body': -7,
             'root-geom': 8, 'geom-root': -8,
             'root-joint': 9, 'joint-root': -9,
             'root-body': 10, 'body-root': -10}

def gen_gnn_param(task_name,
                  adj_mat, node_attr_list,
                  gnn_node_option='nG,yB',
                  root_connection_option='nN, Rn, sE',
                  gnn_output_option='shared',
                  gnn_embedding_option='parameter'):
    ''' this function resembles the functionality of parse_mujoco_graph
        in mujoco_parser.py
        WARNING: this is subject to evolution fish3d environment only
        which has the following features
            1. the mujoco model is deterministically generated from
               adj_mat and node_attr_list
            2. the structure of the model follows
                a. body-body connection is through 3 joints
                b. the root body has 3 joints
    '''
    def actuator_order(adj_mat):
        ''' given the connection information,
            generate the order of the actuator
            NOTE: this should be consistant with model_gen.py -> add_mujoco_actuator()
        '''
        N, _ = adj_mat.shape

        connection_list = []
        for i in range(N):
            for j in range(i, N):
                node_edge = '%d-%d' % (i, j)
                connection_list.append(node_edge)
        return connection_list

    # step 1: directly adopt the relation_matrix from the current adj_matrix
    connection_list = model_gen_util.dfs_order(adj_mat)
    relation_matrix = np.zeros(adj_mat.shape)
    relation_matrix[adj_mat > 0] = 2

    # step 2: tree structure parsed from the adj_mat
    tree, node_type_dict = _get_tree_struct(adj_mat, node_attr_list)

    # step 3: map the input_list
    input_dict, ob_size = _get_input_dict(tree, adj_mat, task_name)

    # step 4: map the action_list
    output_list, output_type_dict, action_size = _get_output_dict(
        tree, adj_mat, gnn_output_option, task_name
    )

    node_param, param_size_dict = _append_node_attr(tree, adj_mat, node_attr_list)

    # step 5: get the node parameters
    debug_info = {'ob_size': ob_size, 'action_size': action_size}

    # post_process, if using noninput_embedding

    if gnn_embedding_option == 'noninput_separate':
        node_param['root'] = np.reshape(np.array(0, dtype=np.int), [1, 1])
        node_param['joint'] = np.reshape(
            np.array(range(1, 1 + len(node_type_dict['joint'])), dtype=np.int),
            [-1, 1]
        )
        param_size_dict = {'joint': 1, 'root': 1}
        # from util import fpdb; fpdb.fpdb().set_trace()

    elif gnn_embedding_option == 'noninput_shared':
        assert False
        node_param['root'] = np.reshape(np.array(0, dtype=np.int), [1, 1])
        node_param['joint'] = np.reshape(
            np.ones(len(node_type_dict['joint']), dtype=np.int),
            [-1, 1]
        )
        param_size_dict = {'joint': 1, 'root': 1}

    else:
        assert gnn_embedding_option == 'parameter'
    # from util import fpdb; fpdb = fpdb.fpdb(); fpdb.set_trace()

    return dict(tree=tree,
                relation_matrix=relation_matrix,
                node_type_dict=node_type_dict,
                output_type_dict=output_type_dict,
                input_dict=input_dict,
                output_list=output_list,
                debug_info=debug_info,
                node_parameters=node_param,
                para_size_dict=param_size_dict,
                num_nodes=len(tree))

def _get_tree_struct(adj_mat, node_attr_list):
    ''' parse the adj_mat and log the information from node_attr_list
        to its corresponding node_id
        output:
            tree: a dictionary indexed by the node_id
                tree[i]['name'] -> node_id
                tree[i]['neighbour'] -> list of parents and child
                tree[i]['parent'] -> list of parent (at most 1, root has zero parent)
                tree[i]['child'] -> 0 for leaf, reasonable amount for all the others
                tree[i]['info'] -> whatever attributes given by node_attr_list
                tree[i]['is_output_node'] -> False if root True otherwise
                tree[i]['output_type'] -> list of length 3
                                          index 0 1 2 corresponds to existance of x y z
                                          joints
                tree[i]['joint_num'] -> sum(tree[i]['output_type']) for ease of access
            node_type_dict:
                historic reason, preserving from mujoco_parser.py
    '''
    N, _ = adj_mat.shape

    tree = {}

    # give name and find neighbour
    for i in range(N):
        # initialize tree struct
        tree[i] = {}
        # check child
        child = adj_mat[i][i + 1:]
        child = list(set(np.where(child > 0)[0]))
        # check its parent
        parent = adj_mat[:, i][0: i]
        parent = list(set(np.where(parent > 0)[0]))
        # remove duplicate elements
        neighbour = child + parent
        # fill the node attribute
        tree[i]['name'] = str(i)
        tree[i]['neighbour'] = neighbour
        tree[i]['parent'] = parent  # can have at most 1 parent
        tree[i]['child'] = child

    # add debug info
    for i in range(N):
        tree[i]['info'] = node_attr_list[i]

    # add output and type of output
    tree[0]['is_output_node'] = False
    tree[0]['joint_num'] = 3
    for i in range(1, N):
        tree[i]['is_output_node'] = True
        parent_node = tree[i]['parent'][0]
        joint_type = adj_mat[parent_node, i]
        joint_axis = [int(item) for item in list(bin(joint_type)[2:])]
        joint_axis = [0 for i in range(3 - len(joint_axis))] + joint_axis
        joint_axis = list(reversed(joint_axis))
        tree[i]['output_type'] = joint_axis
        tree[i]['joint_num'] = int(np.array(joint_axis).sum())
        pass

    # all node types should be of joint
    tree[0]['type'] = 'root'
    for i in range(1, N):
        tree[i]['type'] = 'joint'

    # create node type dict
    node_type_dict = {}
    node_type_dict['root'] = [0]
    node_type_dict['joint'] = list(range(1, N))

    return tree, node_type_dict

def _get_input_dict(tree, adj_mat, task_name):
    '''
        output:
            input_dict (for fish only): dm_control returns a
                                        concatenation of 'joint_angles', 'velocity'
                                        and optionally,
                                        'target', depending on the task
                maps index of the tree to index of the observation vector corresponding
                to the node
                eg. input_dict[0] == [0, 1, 2, 31, 32, 33, 34, 35, 36]
                    root's observation corresponds to the specific indeces inside the
                    overall ob vector
            input_dict (for planar creatures such as walker, cheetah, hopper):
                our customized environment returns a concatenation of
                (assuming the creature contains N nodes, including the torso)
                'orientation': 2 * N
                'height': 1
                'velocity': 3 (for the rootxyz) + (N - 1)
                'touch': 2 * (N - 1) -- all body nodes
                         except the root will have 2 seonsors

    '''
    # from util.fpdb import fpdb; fpdb().set_trace()
    N, _ = adj_mat.shape
    input_dict = {}
    ob_size = 0

    dfs_order = model_gen_util.dfs_order(adj_mat)
    node_order = [0] + [int(item.split('-')[-1]) for item in dfs_order]
    node_order = np.array(node_order)

    # ### map the node id to observation entry in the observation dict
    if 'fish' in task_name:
        joint_ptr = 0
        velocity_ptr = (N - 1) * 3
        if 'speed' in task_name:
            velocity_ptr += 1

        for i, node_id in enumerate(node_order):
            node_joint_num = tree[i]['joint_num']

            # add observation for joint angles
            if i == 0:  # root node
                if 'speed' in task_name:  # add the observation for rootz
                    input_dict[node_id] = [0]
                    joint_ptr += 1
                    ob_size += 1
                else:
                    input_dict[node_id] = []
            else:
                input_dict[node_id] = list(range(joint_ptr, joint_ptr + node_joint_num))
                joint_ptr += node_joint_num
                ob_size += node_joint_num

            # add observation for velocity
            input_dict[node_id] += list(range(velocity_ptr, velocity_ptr + node_joint_num))
            velocity_ptr += node_joint_num
            ob_size += node_joint_num

        # if the task is 'fish3d-swim', process target
        if 'swim' in task_name:
            input_dict[0] = input_dict[0] + list(range(ob_size, ob_size + 3))
            ob_size += 3

    elif 'walker' in task_name or 'hopper' in task_name or 'cheetah' in task_name:
        orient_ptr = 1
        velocity_ptr = orient_ptr + 2 * N
        touch_ptr = velocity_ptr + 3 + (N - 1)

        for i, node_id in enumerate(node_order):
            # print tree[i]
            node_joint_num = tree[i]['joint_num']
            # print 'success', i, node_id

            # process observations that are different between root and body
            # process height, velocity, touch
            if i == 0: # the root node
                assert 'speed' in task_name
                # process height
                input_dict[node_id] = [0]
                ob_size += 1
                # process velocity
                input_dict[node_id] += list(range(velocity_ptr,
                                                  velocity_ptr + 3))
                velocity_ptr += 3
                ob_size += 3
            else:
                # process velocity
                input_dict[node_id] =  list(range(velocity_ptr,
                                                  velocity_ptr + node_joint_num))
                velocity_ptr += node_joint_num
                ob_size += node_joint_num
                # process touch
                input_dict[node_id] += list(range(touch_ptr,
                                                  touch_ptr + 2))
                touch_ptr += 2
                ob_size += 2

            # process orientations
            input_dict[node_id] += list(range(orient_ptr,
                                              orient_ptr + 2))
            orient_ptr += 2
            ob_size += 2

    return input_dict, ob_size

def _get_output_dict(tree, adj_mat, gnn_output_option, task_name):
    '''
        how to order the output of GGNN to the corresponding mujoco xml file
        the order is stored in output_order
    '''
    if 'fish' in task_name:
        # the order of apperence for each node in the xml file
        dfs_order = model_gen_util.dfs_order(adj_mat)
        output_order = [int(item.split('-')[-1]) for item in dfs_order]

        # output_type_dict
        output_type_dict = {}
        output_type_dict[gnn_output_option] = output_order

        # how many actions should each 1 node output
        # output size can be infered from the tree structure
        # for each node_i in output_order, it should output tree[node_i]['output_type']
        # tree[node_i]['output_type'] is a list of 3 elements
        # index 0 - exists x_axis joint output
        # index 1 - exists y_axis joint output
        # index 2 - exists z_axis joint output

        # all the actuators need one output
        N, _ = adj_mat.shape
        # the joints on the root are not actuators
        # action_size = joint_num =
        # sum([v['joint_num'] for k, v in tree.items()]) - tree[0]['joint_num']
        action_size = sum([v['joint_num'] for k, v in tree.items()]) - tree[0]['joint_num']
    elif 'walker' in task_name or 'cheetah' in task_name or 'hopper' in task_name:
        # NOTE: seemingly the same thing as the fish
        # the order of apperence for each node in the xml file
        dfs_order = model_gen_util.dfs_order(adj_mat)
        output_order = [int(item.split('-')[-1]) for item in dfs_order]

        # output_type_dict
        output_type_dict = {}
        output_type_dict[gnn_output_option] = output_order

        # how many actions should each 1 node output
        # output size can be infered from the tree structure
        # for each node_i in output_order, it should output tree[node_i]['output_type']
        # tree[node_i]['output_type'] is a list of 3 elements
        # index 0 - exists x_axis joint output
        # index 1 - exists y_axis joint output
        # index 2 - exists z_axis joint output

        # all the actuators need one output
        N, _ = adj_mat.shape
        # the joints on the root are not actuators
        # action_size = joint_num =
        # sum([v['joint_num'] for k, v in tree.items()]) - tree[0]['joint_num']
        action_size = sum([v['joint_num'] for k, v in tree.items()]) - tree[0]['joint_num']

    return output_order, output_type_dict, action_size

def _append_node_attr(tree, adj_mat, node_attr_list):
    ''' output:
            node_param: node parameters that can fully re-construct the
                        mujoco model structure
                        key: node_type
                        value: a numpy array of size
                               (num_of_node_type x number of features)
            param_size_dict:
                        number of features for each type of node
                        key: node_type
                        value: number of features of the node type in int
        NOTE: in nervenet we use a fixed number of features,
              and a fixed type of nodes
    '''
    N, _ = adj_mat.shape
    n_root_feature = 2
    n_joint_feature = 2 + 2 + 3 + 1  # see below for the definition details

    node_param = {'root': np.zeros(n_root_feature),
                  'joint': np.zeros((N - 1, n_joint_feature))}
    param_size_dict = {'root': n_root_feature, 'joint': n_joint_feature}

    # need to be consistant with the dfs order
    dfs_order = model_gen_util.dfs_order(adj_mat)
    node_order = [0] + [int(item.split('-')[-1]) for item in dfs_order]

    for i, node_id in enumerate(list(range(len(node_order)))):
        node_attr = node_attr_list[node_id]
        if i == 0:
            node_param['root'] = np.array([node_attr['a_size'], node_attr['b_size']])
        else:
            node_param['joint'][i - 1] = np.array(
                [node_attr['a_size'], node_attr['b_size'], node_attr['c_size'],
                 node_attr['u'], node_attr['v'], node_attr['axis_x'],
                 node_attr['axis_y'], node_attr['joint_range']]
            )

    return node_param, param_size_dict

if __name__ == '__main__':
    pass

    N = 5
    from config.config import get_config
    args = get_config(evolution=True)

    # get the
    adj_mat = model_gen.gen_test_adj_mat(task=args.task, shape=(N, N))
    node_attr = model_gen.gen_test_node_attr(task=args.task, node_num=N)
    if 'fish' in args.task:
        xml_struct = model_gen.fish_xml_generator(
            adj_mat, node_attr, options=None, filename='debug_gnn_param'
        )
    elif 'walker' in args.task:
        xml_struct = model_gen.walker_xml_generator(
            adj_mat, node_attr, options=None, filename='debug_gnn_param'
        )
    res = gen_gnn_param(args.task, adj_mat, node_attr)

    # create an environment based on the adj_mat and node_attr
    from env import test_model_gen
    import lxml.etree as etree
    xml_str = etree.tostring(xml_struct)
    import pdb; pdb.set_trace()
    test_model_gen.run_one_ep_given_model(
        args, adj_mat, xml_str, max_time_step=5
    )


text:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<TITLE>
Uses of Package org.hsqldb.jdbc (HSQLDB 2.2.7 API)
</TITLE>

<META NAME="date" CONTENT="2012-01-15">

<LINK REL ="stylesheet" TYPE="text/css" HREF="../../../javadoc.css" TITLE="Style">

      Overview
      Package
      Class
    Use
      Tree
      Deprecated
      Index
      Help

 PREV
 NEXT

  FRAMES
 NO FRAMES

  All Classes

Uses of Package
org.hsqldb.jdbc

Packages that use org.hsqldb.jdbc

org.hsqldb.jdbc
Contains the HyperSQL JDBC Driver, and other classes providing JDBC
  functionality and support.

Classes in org.hsqldb.jdbc used by org.hsqldb.jdbc

JDBCClob

          The mapping in the JavaTM programming language
 for the SQL CLOB type.

JDBCCommonDataSource

          Common base for DataSource implementations.

JDBCConnection

JDBCConnectionEventListener

JDBCDriver

          Provides the java.sql.Driver interface implementation required by
 the JDBC specification.

JDBCPreparedStatement

          An object that represents a precompiled SQL statement.

JDBCResultSet

          A table of data representing a database result set, which
 is usually generated by executing a statement that queries the database.

      Overview
      Package
      Class
    Use
      Tree
      Deprecated
      Index
      Help

 PREV
 NEXT

  FRAMES
 NO FRAMES

  All Classes

Copyright © 2001 - 2010 HSQL Development Group.


text:
require 'rails_helper'

describe "documents/index" do

end


text:
import numpy as np
import aesara.tensor as at

def probit(x):
    """Probit function that ensures result is in (0, 1)"""
    eps = np.finfo(float).eps
    result = 0.5 + 0.5 * at.erf(x / at.sqrt(2))
    result = at.switch(at.eq(result, 0), eps, result)
    result = at.switch(at.eq(result, 1), 1 - eps, result)

    return result

def cloglog(x):
    """Cloglog function that ensures result is in (0, 1)"""
    eps = np.finfo(float).eps
    result = 1 - at.exp(-at.exp(x))
    result = at.switch(at.eq(result, 0), eps, result)
    result = at.switch(at.eq(result, 1), 1 - eps, result)

    return result

def logit(x):
    """Logit function that ensures result is in (0, 1)"""
    eps = np.finfo(float).eps
    result = at.sigmoid(x)
    result = at.switch(at.eq(result, 0), eps, result)
    result = at.switch(at.eq(result, 1), 1 - eps, result)
    return result

def identity(x):
    return x

def inverse_squared(x):
    return at.reciprocal(at.sqrt(x))

def arctan_2(x):
    return 2 * at.arctan(x)


text:
<?php namespace Echosign\Responses;

class AgreementStatusUpdateResponse {

    public $result;

    public function __construct( array $response )
    {
        $this->result = $response['result'];
    }

}


text:
hqDefine("cloudcare/js/formplayer/constants", function () {
    return {
        ALLOWED_SAVED_OPTIONS: ['oneQuestionPerScreen', 'language'],

        // These should match corehq/apps/cloudcare/const.py
        WEB_APPS_ENVIRONMENT: 'web-apps',
        PREVIEW_APP_ENVIRONMENT: 'preview-app',
        GENERIC_ERROR: gettext(
            'An unexpected error occurred. ' +
            'Please report an issue if you continue to see this message.'
        ),

        LayoutStyles: {
            GRID: 'grid',
            LIST: 'list',
        },
        DEFAULT_INCOMPLETE_FORMS_PAGE_SIZE: 20,

        MULTI_SELECT_ADD: 'add',
        MULTI_SELECT_REMOVE: 'remove',
        MULTI_SELECT_MAX_SELECT_VALUE: 100,
    };
});


text:
<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

	<parent>
		<groupId>ch.qos.logback</groupId>
		<artifactId>audit-parent</artifactId>
		<version>0.6</version>
	</parent>

	<modelVersion>4.0.0</modelVersion>

	<groupId>ch.qos.logback</groupId>
	<artifactId>audit-examples</artifactId>
	<packaging>jar</packaging>
	<name>Logback Audit Examples</name>

	<dependencies>
    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>audit-client</artifactId>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-simple</artifactId>
      <scope>test</scope>
    </dependency>
	</dependencies>

	<build>
		<plugins>
      <plugin>
        <artifactId>maven-antrun-plugin</artifactId>
        <executions>
          <execution>
            <phase>compile</phase>
            <configuration>
              <tasks>
                <mkdir dir="../target/site/audit-examples/resources"/>
                <copy todir="../target/site/audit-examples/resources">
                  <fileset dir="src/main/resources/">
                    <include name="**/*.xml"/>
                  </fileset>
                </copy>
              </tasks>
            </configuration>
            <goals>
              <goal>run</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
		</plugins>

		<pluginManagement>
			<plugins>
				<!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->
				<plugin>
					<groupId>org.eclipse.m2e</groupId>
					<artifactId>lifecycle-mapping</artifactId>
					<version>1.0.0</version>
					<configuration>
						<lifecycleMappingMetadata>
							<pluginExecutions>
								<pluginExecution>
									<pluginExecutionFilter>
										<groupId>
											org.apache.maven.plugins
										</groupId>
										<artifactId>
											maven-antrun-plugin
										</artifactId>
										<versionRange>
											[1.3,)
										</versionRange>
										<goals>
											<goal>run</goal>
										</goals>
									</pluginExecutionFilter>
									<action>
										<ignore></ignore>
									</action>
								</pluginExecution>
							</pluginExecutions>
						</lifecycleMappingMetadata>
					</configuration>
				</plugin>
			</plugins>
		</pluginManagement>
	</build>

</project>


text:
using Nucleus.Base;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Nucleus.Model
{
    /// <summary>
    /// An interface for sets of Model Objects
    /// </summary>
    public interface IModelObjectSet : IMutableNamed, IUnique
    {
        /// <summary>
        /// Get the final set of items contained within this set, consisting of all items in the base collection
        /// and any subsets (or all items in the model if 'All' is true) that pass all filters specified via the
        /// Filters property.
        /// </summary>
        IList GetItems();

        /// <summary>
        /// Add an item to the base collection of this set.
        /// If the specified item is not a valid type for this set, adding it will
        /// fail and this function will return false.
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        bool Add(ModelObject item);

        /// <summary>
        /// Does this set contain the specified item?
        /// (or, would it, if they were part of the same model?)
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        bool Contains(ModelObject item);
    }
}


text:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="me.gurpreetsk.rxpagination.MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="1"
        android:textSize="24sp"/>

</LinearLayout>


text:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Asylo: asylo/identity/sealing/sgx -> util Relation</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="/favicons/favicon.ico" rel="shortcut icon" type="image/x-icon"/>

<link href="search/search.css" rel="stylesheet" type="text/css"/>

<link href="doxygen.css" rel="stylesheet" type="text/css" />

Asylo

asylo

identity

sealing

sgx

sgx → util Relation

File in asylo/identity/sealing/sgx	Includes file in asylo/util

sgx_local_secret_sealer.h	cleansing_types.h

sgx_local_secret_sealer.h	status.h


text: cp templates/* /usr/share/git-core/templates/hooks


text:
'use strict';

module.exports = function (grunt) {

  // Project configuration.
  grunt.initConfig({
    jshint: {
      all: [
        'Gruntfile.js',
        'tasks/*.js',
        '<%= nodeunit.tests %>'
      ],
      options: {
        jshintrc: '.jshintrc'
      }
    },

    // Before generating any new files, remove any previously-created files.
    clean: {
      tests: ['tmp']
    },

    // Configuration to be run (and then tested).
    batch: {
      cat: {
        options: {
          cmd: function (f) {
            var cmd = 'cat ' + f.src.join(' ') + ' > ' + f.dest;
            grunt.log.writeln(cmd);
            return cmd;
          },
          setup: function (done) {
            grunt.file.mkdir('tmp');
            done();
          }
        },
        files: {
          'tmp/cat': ['test/fixtures/testing', 'test/fixtures/123']
        }
      }
    },

    // Unit tests.
    nodeunit: {
      tests: ['test/*_test.js']
    }

  });

  // Actually load this plugin's task(s).
  grunt.loadTasks('tasks');

  // These plugins provide necessary tasks.
  grunt.loadNpmTasks('grunt-contrib-jshint');
  grunt.loadNpmTasks('grunt-contrib-clean');
  grunt.loadNpmTasks('grunt-contrib-nodeunit');

  // Whenever the "test" task is run, first clean the "tmp" dir, then run this
  // plugin's task(s), then test the result.
  grunt.registerTask('test', ['clean', 'batch', 'nodeunit']);

  // By default, lint and run all tests.
  grunt.registerTask('default', ['jshint', 'test']);

};


text:
namespace CanvasToy {
    var version = 2;
    export var engine: Renderer;
    export var gl: WebGLRenderingContext = null;
    export var debug: boolean = true;
    export interface Vec2Array extends GLM.IArray { }
    export interface Vec3Array extends GLM.IArray { }
    export interface Vec4Array extends GLM.IArray { }
    export interface Mat2Array extends GLM.IArray { }
    export interface Mat2dArray extends GLM.IArray { }
    export interface Mat3Array extends GLM.IArray { }
    export interface Mat4Array extends GLM.IArray { }
    export interface QuatArray extends GLM.IArray { }

    export enum DataType {
        float,
        int,
        vec2,
        vec3,
        vec4,
        mat2,
        mat3,
        mat4
    }
}