<commit_before>/*
 * LaTeX mode for QEmacs.
 *
 * Copyright (c) 2003 <NAME> <<EMAIL>>
 * Based on c-mode by <NAME>
 * Requires the shell mode
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "qe.h"

#define MAX_BUF_SIZE    512

static ModeDef latex_mode;

/* TODO: add state handling to allow colorization of elements longer
 * than one line (eg, multi-line functions and strings)
 */
static void latex_colorize_line(unsigned int *buf, __unused__ int len,
                                int *colorize_state_ptr,
                                __unused__ int state_only)
{
    int c, state;
    unsigned int *p, *p_start;

    state = *colorize_state_ptr;
    p = buf;
    p_start = p;

    for (;;) {
        p_start = p;
        c = *p;
        switch (c) {
        case '\n':
            goto the_end;
        case '`':
            p++;
            /* a ``string'' */
            if (*p == '`') {
                while (1) {
                    p++;
                    if (*p == '\n' || (*p == '\'' && *(p+1) == '\''))
                        break;
                }
                if (*p == '\'' && *++p == '\'')
                    p++;
                set_color(p_start, p, QE_STYLE_STRING);
            }
            break;
        case '\\':
            p++;
            /* \function[keyword]{variable} */
            if (*p == '\'' || *p == '\"' || *p == '~' || *p == '%' || *p == '\\') {
                p++;
            } else {
                while (*p != '{' && *p != '[' && *p != '\n' && *p != ' ' && *p != '\\')
                    p++;
            }
            set_color(p_start, p, QE_STYLE_FUNCTION);
            while (*p == ' ' || *p == '\t') {
                /* skip space */
                p++;
            }
            while (*p == '{' || *p == '[') {
                if (*p++ == '[') {
                    /* handle [keyword] */
                    p_start = p;
                    while (*p != ']' && *p != '\n')
                        p++;
                    set_color(p_start, p, QE_STYLE_KEYWORD);
                    if (*p == ']')
                        p++;
                } else {
                    int braces = 0;
                    /* handle {variable} */
                    p_start = p;
                    while (*p != '\n') {
                        if (*p == '{') {
                            braces++;
                        } else
                        if (*p == '}') {
                            if (braces-- == 0)
                                break;
                        }
                        p++;
                    }
                    set_color(p_start, p, QE_STYLE_VARIABLE);
                    if (*p == '}')
                        p++;
                }
                while (*p == ' ' || *p == '\t') {
                    /* skip space */
                    p++;
                }
            }
            break;
        case '%':
            p++;
            /* line comment */
            while (*p != '\n')
                p++;
            set_color(p_start, p, QE_STYLE_COMMENT);
            break;
        default:
            p++;
            break;
        }
    }
 the_end:
    *colorize_state_ptr = state;
}

static int latex_mode_probe(ModeProbeData *p)
{
    /* currently, only use the file extension */
    if (match_extension(p->filename, "tex"))
        return 100;

    return 0;
}

static int latex_mode_init(EditState *s, ModeSavedData *saved_data)
{
    int ret;

    ret = text_mode_init(s, saved_data);
    if (ret)
        return ret;<commit_msg>recognise Halibut files<commit_after>/*
 * LaTeX mode for QEmacs.
 *
 * Copyright (c) 2003 <NAME> <<EMAIL>>
 * Based on c-mode by <NAME>
 * Requires the shell mode
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "qe.h"

#define MAX_BUF_SIZE    512

static ModeDef latex_mode;

/* TODO: add state handling to allow colorization of elements longer
 * than one line (eg, multi-line functions and strings)
 */
static void latex_colorize_line(unsigned int *buf, __unused__ int len,
                                int *colorize_state_ptr,
                                __unused__ int state_only)
{
    int c, state;
    unsigned int *p, *p_start;

    state = *colorize_state_ptr;
    p = buf;
    p_start = p;

    for (;;) {
        p_start = p;
        c = *p;
        switch (c) {
        case '\n':
            goto the_end;
        case '`':
            p++;
            /* a ``string'' */
            if (*p == '`') {
                while (1) {
                    p++;
                    if (*p == '\n' || (*p == '\'' && *(p+1) == '\''))
                        break;
                }
                if (*p == '\'' && *++p == '\'')
                    p++;
                set_color(p_start, p, QE_STYLE_STRING);
            }
            break;
        case '\\':
            p++;
            /* \function[keyword]{variable} */
            if (*p == '\'' || *p == '\"' || *p == '~' || *p == '%' || *p == '\\') {
                p++;
            } else {
                while (*p != '{' && *p != '[' && *p != '\n' && *p != ' ' && *p != '\\')
                    p++;
            }
            set_color(p_start, p, QE_STYLE_FUNCTION);
            while (*p == ' ' || *p == '\t') {
                /* skip space */
                p++;
            }
            while (*p == '{' || *p == '[') {
                if (*p++ == '[') {
                    /* handle [keyword] */
                    p_start = p;
                    while (*p != ']' && *p != '\n')
                        p++;
                    set_color(p_start, p, QE_STYLE_KEYWORD);
                    if (*p == ']')
                        p++;
                } else {
                    int braces = 0;
                    /* handle {variable} */
                    p_start = p;
                    while (*p != '\n') {
                        if (*p == '{') {
                            braces++;
                        } else
                        if (*p == '}') {
                            if (braces-- == 0)
                                break;
                        }
                        p++;
                    }
                    set_color(p_start, p, QE_STYLE_VARIABLE);
                    if (*p == '}')
                        p++;
                }
                while (*p == ' ' || *p == '\t') {
                    /* skip space */
                    p++;
                }
            }
            break;
        case '%':
            p++;
            /* line comment */
            while (*p != '\n')
                p++;
            set_color(p_start, p, QE_STYLE_COMMENT);
            break;
        default:
            p++;
            break;
        }
    }
 the_end:
    *colorize_state_ptr = state;
}

static int latex_mode_probe(ModeProbeData *p)
{
    /* currently, only use the file extension */
    /* Halibut (by Simon Tatham) has a syntax similar to TeX and uses
     * .but extension */
    if (match_extension(p->filename, "tex|but"))
        return 100;

    return 0;
}

static int latex_mode_init(EditState *s, ModeSavedData *saved_data)
{
    int ret;

    ret = text_mode_init(s, saved_data);
    if (ret)
        return ret;
{'fraction_non_alphanumeric': 0.1378870462715992, 'fraction_numerical': 0.006958135219761104, 'mean_word_length': 1.1474103585657371, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6664341', 'n_tokens_mistral': 2527, 'n_tokens_neox': 2366, 'n_words': 727}