| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444 |
- /******************************************************************************
- * This file is part of TinTin++ *
- * *
- * Copyright (C) 2004-2020 Igor van den Hoven *
- * *
- * TinTin++ is free software; you can redistribute it and/or modify *
- * it under the terms of the GNU General Public License as published by *
- * the Free Software Foundation; either version 3 of the License, or *
- * (at your option) any later version. *
- * *
- * This program 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 General Public License for more details. *
- * *
- * You should have received a copy of the GNU General Public License *
- * along with TinTin++. If not, see https://www.gnu.org/licenses. *
- ******************************************************************************/
- /******************************************************************************
- * T I N T I N + + *
- * *
- * coded by Igor van den Hoven 2020 *
- ******************************************************************************/
- #include "tintin.h"
- DO_COMMAND(do_edit)
- {
- int cnt;
- arg = sub_arg_in_braces(ses, arg, arg1, GET_ONE, SUB_VAR|SUB_FUN);
- if (*arg1 == 0)
- {
- tintin_header(ses, 80, " EDIT OPTIONS ");
- for (cnt = 0 ; edit_table[cnt].fun ; cnt++)
- {
- if (*edit_table[cnt].desc)
- {
- tintin_printf2(ses, " [%-18s] %s", edit_table[cnt].name, edit_table[cnt].desc);
- }
- }
- tintin_header(ses, 80, "");
- }
- else
- {
- for (cnt = 0 ; edit_table[cnt].fun ; cnt++)
- {
- if (is_abbrev(arg1, edit_table[cnt].name))
- {
- edit_table[cnt].fun(ses, arg, arg1, arg2);
- return ses;
- }
- }
- show_error(ses, LIST_COMMAND, "#ERROR: #EDIT {%s} IS NOT A VALID OPTION.", arg1);
- }
- return ses;
- }
- DO_EDIT(edit_create)
- {
- struct edit_data *edit = gtd->ses->input->edit;
- char *pta, *ptn;
- int index;
- clear_editor(edit);
- arg = sub_arg_in_braces(ses, arg, arg1, GET_ONE, SUB_VAR|SUB_FUN);
- str_cpy(>d->ses->input->edit_name, arg1);
- index = 0;
- while (*arg)
- {
- arg = sub_arg_in_braces(ses, arg, arg1, GET_ONE, SUB_VAR|SUB_FUN);
- pta = arg1;
- ptn = strchr(pta, '\n');
- while (ptn)
- {
- *ptn++ = 0;
- if (*ptn)
- {
- insert_line(edit, index++, pta);
- pta = ptn;
- }
- ptn = strchr(pta, '\n');
- }
- insert_line(edit, index++, pta);
- }
- show_message(ses, LIST_COMMAND, "#EDIT CREATE: CREATED %d LINES.", edit->used);
- enable_editor(edit);
- return ses;
- }
- DO_EDIT(edit_load)
- {
- struct edit_data *edit = gtd->ses->input->edit;
- struct listnode *node;
- int index;
- arg = sub_arg_in_braces(ses, arg, arg1, GET_ONE, SUB_VAR|SUB_FUN);
- if ((node = search_nest_node_ses(ses, arg1)) == NULL)
- {
- show_error(ses, LIST_COMMAND, "#EDIT LOAD: VARIABLE {%s} NOT FOUND.", arg1);
-
- return ses;
- }
- if (node->root == NULL)
- {
- return edit_create(ses, node->arg1, arg1, arg2);
- }
- clear_editor(edit);
- for (index = 0 ; index < node->root->used ; index++)
- {
- substitute(ses, node->root->list[index]->arg1, arg2, SUB_ESC);
- insert_line(edit, index, arg2);
- }
- show_message(ses, LIST_COMMAND, "#EDIT LOAD: LOADED %d LINES FROM {%s}.", edit->used, arg1);
- enable_editor(edit);
- return ses;
- }
- DO_EDIT(edit_save)
- {
- struct edit_data *edit = gtd->ses->input->edit;
- struct listnode *node;
- int index;
- arg = sub_arg_in_braces(ses, arg, arg1, GET_ONE, SUB_VAR|SUB_FUN);
- if (!valid_variable(ses, arg1))
- {
- show_error(ses, LIST_VARIABLE, "#EDIT SAVE: INVALID VARIABLE NAME {%s}.", arg1);
- return ses;
- }
- node = set_nest_node_ses(ses, arg1, "");
- node->root = init_list(gtd->ses, LIST_VARIABLE, LIST_SIZE);
- for (index = 0 ; index < edit->used ; index++)
- {
- substitute(ses, edit->line[index]->str, arg2, SUB_SEC);
- create_node_list(node->root, ntos(index), arg2, "", "");
- }
- show_message(ses, LIST_COMMAND, "#EDIT SAVE: SAVED %d LINES TO {%s}.", edit->used, arg1);
- return ses;
- }
- DO_EDIT(edit_read)
- {
- struct edit_data *edit = gtd->ses->input->edit;
- FILE *file;
- int index, size;
- arg = sub_arg_in_braces(ses, arg, arg1, GET_ONE, SUB_VAR|SUB_FUN);
- if (*arg1 == 0)
- {
- show_error(ses, LIST_COMMAND, "#SYNTAX: #EDIT READ <FILENAME>");
- return ses;
- }
- if ((file = fopen(arg1, "r")) == NULL)
- {
- show_error(ses, LIST_COMMAND, "#ERROR: #EDIT READ: FILE {%s} NOT FOUND.", arg1);
- return ses;
- }
- clear_editor(edit);
- str_cpy(>d->ses->input->edit_name, arg1);
- index = 0;
- size = 0;
- while (fread_one_line(&arg2, file))
- {
- size += str_len(arg2);
- insert_line(edit, index++, arg2);
- }
- show_message(ses, LIST_COMMAND, "#EDIT READ: READ %d LINES FROM {%s}.", edit->used, arg1);
- enable_editor(edit);
- fclose(file);
- return ses;
- }
- DO_EDIT(edit_write)
- {
- struct edit_data *edit = gtd->ses->input->edit;
- FILE *file;
- int index;
- arg = sub_arg_in_braces(ses, arg, arg1, GET_ONE, SUB_VAR|SUB_FUN);
- if (*arg1 == 0)
- {
- str_cpy(&arg1, gtd->ses->input->edit_name);
- }
- if ((file = fopen(arg1, "w")) == NULL)
- {
- show_error(ses, LIST_COMMAND, "#ERROR: #EDIT WRITE: COULDN'T OPEN {%s} TO WRITE.", arg1);
- return ses;
- }
- for (index = 0 ; index < edit->used ; index++)
- {
- fputs(edit->line[index]->str, file);
- putc(ASCII_LF, file);
- }
- show_message(ses, LIST_COMMAND, "#EDIT WRITE: WROTE %d LINES TO {%s}.", index, arg1);
- return ses;
- }
- DO_EDIT(edit_resume)
- {
- struct edit_data *edit = gtd->ses->input->edit;
- SET_BIT(gtd->ses->input->flags, INPUT_FLAG_EDIT);
- if (edit->size == edit->update)
- {
- insert_line(edit, edit->update, "");
- }
- inputline_set(edit->line[edit->update]->str, 0);
- cursor_redraw_edit(ses, "");
- return ses;
- }
- DO_EDIT(edit_suspend)
- {
- struct edit_data *edit = gtd->ses->input->edit;
- if (!HAS_BIT(gtd->ses->input->flags, INPUT_FLAG_EDIT))
- {
- tintin_printf2(gtd->ses, "edit_suspend: not currently editing");
- return ses;
- }
- DEL_BIT(gtd->ses->input->flags, INPUT_FLAG_EDIT);
- str_cpy(&edit->line[edit->update]->str, gtd->ses->input->buf);
- cursor_clear_line(ses, "");
- cursor_redraw_edit(ses, "");
- return ses;
- }
- struct edit_data *create_editor(void)
- {
- return calloc(1, sizeof(struct edit_data));
- }
- void delete_editor(struct edit_data *edit)
- {
- if (edit == NULL)
- {
- tintin_printf2(gtd->ses, "destroy_edit: NULL");
- return;
- }
- clear_editor(edit);
- free(edit);
- }
- void resize_editor(struct edit_data *edit, int size)
- {
- if (edit->size < size)
- {
- edit->size = size;
- edit->line = (struct row_data **) realloc(edit->line, edit->size * sizeof(struct row_data *));
- }
- }
- void clear_editor(struct edit_data *edit)
- {
- int index;
- edit->update = 0;
- for (index = edit->used - 1 ; index >= 0 ; index--)
- {
- delete_line(edit, index);
- }
- }
- void enable_editor(struct edit_data *edit)
- {
- char *str1;
- int filesize;
- SET_BIT(gtd->ses->input->flags, INPUT_FLAG_EDIT);
- if (edit->used == 0)
- {
- create_line(edit, 0, "");
- }
- inputline_set(edit->line[edit->update]->str, 0);
- str1 = str_alloc_stack(0);
- filesize = str_save_editor(gtd->ses->input->edit, &str1);
- cursor_redraw_edit(gtd->ses, "");
- check_all_events(gtd->ses, EVENT_FLAG_INPUT, 0, 4, "STARTED EDITING", gtd->ses->input->edit_name, ntos(edit->used), ntos(filesize), str1);
- if (*gtd->ses->input->edit_name)
- {
- check_all_events(gtd->ses, EVENT_FLAG_INPUT, 1, 4, "STARTED EDITING %s", gtd->ses->input->edit_name, gtd->ses->input->edit_name, ntos(edit->used), ntos(filesize), str1);
- }
- }
- int str_save_editor(struct edit_data *edit, char **str)
- {
- int index, size;
- size = 0;
- str_cpy(str, "");
- for (index = 0 ; index < edit->used ; index++)
- {
- str_cat_printf(str, "%s\n", edit->line[index]->str);
- size += str_len(edit->line[index]->str);
- }
- return size;
- }
- int var_save_editor(struct edit_data *edit, char **str)
- {
- int index, size;
- size = 0;
- for (index = 0 ; index < edit->used ; index++)
- {
- str_cat_printf(str, "{%d}{%s}", index + 1, edit->line[index]->str);
- }
- return size;
- }
- void create_line(struct edit_data *edit, int index, char *str)
- {
- resize_editor(edit, index + 2);
- edit->line[index] = (struct row_data *) calloc(1, sizeof(struct row_data));
- edit->line[index]->str = str_dup(str);
- edit->used++;
- }
- void delete_line(struct edit_data *edit, int index)
- {
- str_free(edit->line[index]->str);
- free(edit->line[index]);
- edit->used--;
- }
- void insert_line(struct edit_data *edit, int index, char *str)
- {
- int cnt;
- if (index >= edit->used)
- {
- while (edit->used < index)
- {
- create_line(edit, edit->used, "");
- }
- create_line(edit, edit->used, str);
- return;
- }
- resize_editor(edit, edit->used + 2);
- for (cnt = edit->used ; cnt > index ; cnt--)
- {
- edit->line[cnt] = edit->line[cnt - 1];
- }
- create_line(edit, index, str);
- }
- void remove_line(struct edit_data *edit, int index)
- {
- int cnt;
- delete_line(edit, index);
- for (cnt = index ; cnt < edit->used ; cnt++)
- {
- edit->line[cnt] = edit->line[cnt + 1];
- }
- }
|