<commit_before>// Distributed under Mozilla Public Licence 2.0
// https://www.mozilla.org/en-US/MPL/2.0/
// (c) 2015 <NAME>
// imacs - possibly the worlds smallest emacs clone!

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <string.h>

#define BUFF_SIZE (1024*24)
int buff_size = BUFF_SIZE;
char buff[BUFF_SIZE] =
    "imacs - possibly the worlds smallest emacs clone!\n"
    "(imacs said in Swedish sounds like [eeemacs])\n"
    "(c) 2015 <NAME>\n"
    "\n"
    "Usage: ./imacs file.txt\n"
    "\n"
    "Help\n"
    "====\n"
    "lines: ^A, ^P, ^N, ^K, ^E\n"
    "chars: ^B, ^F, ^D, DEL/^H\n"
    "exit: ^C\n"
    "\n"
    "Limitations\n"
    "===========\n"
    "- can't handle files with longer lines that terminal width, or more lines than terminal\n"
    "- tabs... get's funny\n"
    "- vt100 only\n"
    "- each keystroke rewrite the screen!\n"
    "\n"
    "Why\n"
    "===\n"
    "Support simple editing on embedded systems ala ESP8266 wifi device, 115200 baud\n"
    ;

char* buff_end = buff + BUFF_SIZE;

int row = 0;
int col = 0;

int lines, columns;
char* filename = NULL;

char* getpos(int r, int c) {
    char* p = buff;
    while (r > 0 && *p && p < buff_end) if (*p++ == '\n') r--;
    while (c > 0 && *p && p < buff_end && *p++ != '\n') c--;
    return p < buff_end ? p : (char*)buff_end-1;
}

int currentLineLength() {
    char* start = getpos(row, 0);
    char* p = start;
    while (*p != '\n' && *p && p < buff_end) p++;
    return (p - start);
}

void f() { fflush(stdout); }

// http://wiki.bash-hackers.org/scripting/terminalcodes
// http://paulbourke.net/dataformats/ascii/
void clear() { printf("\x1b[2J\x1b[H%"); }
void gotorc(int r, int c) { printf("\x1b[%d;%dH", r+1, c+1); }
void inverse(int on) { printf(on ? "\x1b[7m" : "\x1b[m"); }
void insert(char c) {
    char* from = getpos(row, col);
    memmove(from+1, from, strlen(from)+1); // buff+len-from ?
    *from = c;

    // TODO: make function and use for ^F ?
    col++;
    if (col > columns) {
        col = 0;
        row++;
    }
}

void restoreTerminalAndExit(int dummy) {
    system("stty echo");
    system("stty sane");
    exit(0);
}

void error(char* msg, char* arg) {
    clear(); f();
    inverse(1);
    printf("%s %s\n", msg, arg);
    inverse(0);
    exit(1);<commit_msg>More help and clear screen on exit<commit_after>// Distributed under Mozilla Public Licence 2.0
// https://www.mozilla.org/en-US/MPL/2.0/
// (c) 2015 <NAME>
// imacs - possibly the worlds smallest emacs clone!

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <string.h>

// TODO: refactor to create a struct with all the states of a buffer?

#define BUFF_SIZE (1024*24)
int buff_size = BUFF_SIZE;
char buff[BUFF_SIZE] =
    "imacs - possibly the worlds smallest emacs clone!\n"
    "(imacs said in Swedish sounds like [eeemacs])\n"
    "(c) 2015 <NAME>\n"
    "\n"
    "Usage: ./imacs file.txt\n"
    "\n"
    "Help\n"
    "====\n"
    "lines: ^A, ^P, ^N, ^K, ^E\n"
    "chars: ^B, ^F, ^D, DEL/^H\n"
    "exit: ^C\n"
    "\n"
    "Limitations\n"
    "===========\n"
    "- no way to save a file!\n"
    "- can't handle files with longer lines that terminal width, or more lines than terminal\n"
    "- tabs... get's funny\n"
    "- vt100 only\n"
    "- each keystroke rewrite the screen!\n"
    "- when you move till after the file end, it gets funny\n"
    "\n"
    "Why\n"
    "===\n"
    "Support simple editing on embedded systems ala ESP8266 wifi device, 115200 baud\n"
    ;

char* buff_end = buff + BUFF_SIZE;

int row = 0;
int col = 0;

int lines, columns;
char* filename = NULL;

char* getpos(int r, int c) {
    char* p = buff;
    while (r > 0 && *p && p < buff_end) if (*p++ == '\n') r--;
    while (c > 0 && *p && p < buff_end && *p++ != '\n') c--;
    return p < buff_end ? p : (char*)buff_end-1;
}

int currentLineLength() {
    char* start = getpos(row, 0);
    char* p = start;
    while (*p != '\n' && *p && p < buff_end) p++;
    return (p - start);
}

void f() { fflush(stdout); }

// http://wiki.bash-hackers.org/scripting/terminalcodes
// http://paulbourke.net/dataformats/ascii/
void clear() { printf("\x1b[2J\x1b[H%"); }
void gotorc(int r, int c) { printf("\x1b[%d;%dH", r+1, c+1); }
void inverse(int on) { printf(on ? "\x1b[7m" : "\x1b[m"); }
void insert(char c) {
    char* from = getpos(row, col);
    memmove(from+1, from, strlen(from)+1); // buff+len-from ?
    *from = c;

    // TODO: make function and use for ^F ?
    col++;
    if (col > columns) {
        col = 0;
        row++;
    }
}

void restoreTerminalAndExit(int dummy) {
    clear();
    printf("imacs bids you farewell!\n");
    system("stty echo");
    system("stty sane");
    exit(0);
}

void error(char* msg, char* arg) {
    clear(); f();
    inverse(1);
    printf("%s %s\n", msg, arg);
    inverse(0);
    exit(1);
{'fraction_non_alphanumeric': 0.19680743584562538, 'fraction_numerical': 0.02141846837744999, 'mean_word_length': 2.6912751677852347, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 27, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7667223', 'n_tokens_mistral': 2026, 'n_tokens_neox': 1927, 'n_words': 554}