<commit_before>/**
 * @file   latex.c
 * @brief  
 *
 * Copyright (C) 2010-2011 Gummi-Dev Team <<EMAIL>>
 * All Rights reserved.
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

#include "latex.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include <gtk/gtk.h>
#include <glib.h>

#include "configfile.h"
#include "constants.h"
#include "editor.h"
#include "environment.h"
#include "gui/gui-preview.h"
#include "utils.h"

#include "compile/rubber.h"
#include "compile/latexmk.h"
#include "compile/texlive.h"



GuLatex* latex_init (void) {
    GuLatex* l = g_new0 (GuLatex, 1);
    l->errormessage = NULL;
    l->modified_since_compile = FALSE;
    
    texlive_init ();
    rubber_init ();
    latexmk_init ();
    
    /* TODO: Temp hard set of compilation options for migrating configs */
    if (strlen(config_get_value("typesetter")) == 0)
        config_set_value("typesetter", "pdflatex");
    if (strlen(config_get_value("compile_steps")) == 0)
        config_set_value("compile_steps", "texpdf");
    
    return l;
}



gboolean latex_method_active (gchar* method) {
    if (utils_strequal (config_get_value ("compile_steps"), method)) {
        return TRUE;
    }
    return FALSE;
}

gchar* latex_update_workfile (GuLatex* lc, GuEditor* ec) {
    GtkTextIter current, start, end;
    gchar *text;
    
    /* save selection */
    editor_get_current_iter (ec, &current);
    gtk_text_buffer_get_selection_bounds (GTK_TEXT_BUFFER (ec->buffer), &start,
            &end);
    text = editor_grab_buffer (ec);

    /* restore selection */
    gtk_text_buffer_place_cursor (GTK_TEXT_BUFFER (ec->buffer), &current);
    gtk_text_buffer_select_range (GTK_TEXT_BUFFER (ec->buffer), &end, &start);
    
    /* write buffer content to the workfile */
    utils_set_file_contents (ec->workfile, text, -1);
    
    return text;
}




gchar* latex_set_compile_cmd (GuEditor* ec) {
    
    const gchar* method = config_get_value ("compile_steps");
    const gchar* precommand = g_strdup_printf ("cd \"%s\"%s%s", 
                                                C_TMPDIR, C_CMDSEP, C_TEXSEC);
                                                
    gchar* texcmd = NULL;<commit_msg>fix: compiling from current dir<commit_after>/**
 * @file   latex.c
 * @brief  
 *
 * Copyright (C) 2010-2011 Gummi-Dev Team <<EMAIL>>
 * All Rights reserved.
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

#include "latex.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include <gtk/gtk.h>
#include <glib.h>

#include "configfile.h"
#include "constants.h"
#include "editor.h"
#include "environment.h"
#include "gui/gui-preview.h"
#include "utils.h"

#include "compile/rubber.h"
#include "compile/latexmk.h"
#include "compile/texlive.h"



GuLatex* latex_init (void) {
    GuLatex* l = g_new0 (GuLatex, 1);
    l->errormessage = NULL;
    l->modified_since_compile = FALSE;
    
    texlive_init ();
    rubber_init ();
    latexmk_init ();
    
    /* TODO: Temp hard set of compilation options for migrating configs */
    if (strlen(config_get_value("typesetter")) == 0)
        config_set_value("typesetter", "pdflatex");
    if (strlen(config_get_value("compile_steps")) == 0)
        config_set_value("compile_steps", "texpdf");
    
    return l;
}



gboolean latex_method_active (gchar* method) {
    if (utils_strequal (config_get_value ("compile_steps"), method)) {
        return TRUE;
    }
    return FALSE;
}

gchar* latex_update_workfile (GuLatex* lc, GuEditor* ec) {
    GtkTextIter current, start, end;
    gchar *text;
    
    /* save selection */
    editor_get_current_iter (ec, &current);
    gtk_text_buffer_get_selection_bounds (GTK_TEXT_BUFFER (ec->buffer), &start,
            &end);
    text = editor_grab_buffer (ec);

    /* restore selection */
    gtk_text_buffer_place_cursor (GTK_TEXT_BUFFER (ec->buffer), &current);
    gtk_text_buffer_select_range (GTK_TEXT_BUFFER (ec->buffer), &end, &start);
    
    /* write buffer content to the workfile */
    utils_set_file_contents (ec->workfile, text, -1);
    
    return text;
}




gchar* latex_set_compile_cmd (GuEditor* ec) {
    
    const gchar* method = config_get_value ("compile_steps");
    const gchar* curdir = g_path_get_dirname (ec->workfile);
    const gchar* precommand = g_strdup_printf ("cd \"%s\"%s%s", 
                                                curdir, C_CMDSEP, C_TEXSEC);
                                                
    gchar* texcmd = NULL;
{'fraction_non_alphanumeric': 0.10532706025193504, 'fraction_numerical': 0.003945970556988921, 'mean_word_length': 3.2903645833333335, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2517717', 'n_tokens_mistral': 2111, 'n_tokens_neox': 2005, 'n_words': 660}