/*******************************************************************************
* Copyright (c) 2013-2017, Andrés Martinelli <andmarti@gmail.com *
* All rights reserved. *
* *
* This file is a part of SC-IM *
* *
* SC-IM is a spreadsheet program that is based on SC. The original authors *
* of SC are James Gosling and Mark Weiser, and mods were later added by *
* Chuck Martin. *
* *
* Redistribution and use in source and binary forms, with or without *
* modification, are permitted provided that the following conditions are met: *
* 1. Redistributions of source code must retain the above copyright *
* notice, this list of conditions and the following disclaimer. *
* 2. Redistributions in binary form must reproduce the above copyright *
* notice, this list of conditions and the following disclaimer in the *
* documentation and/or other materials provided with the distribution. *
* 3. All advertising materials mentioning features or use of this software *
* must display the following acknowledgement: *
* This product includes software developed by Andrés Martinelli *
* <andmarti@gmail.com>. *
* 4. Neither the name of the Andrés Martinelli nor the *
* names of other contributors may be used to endorse or promote products *
* derived from this software without specific prior written permission. *
* *
* THIS SOFTWARE IS PROVIDED BY ANDRES MARTINELLI ''AS IS'' AND ANY *
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED *
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE *
* DISCLAIMED. IN NO EVENT SHALL ANDRES MARTINELLI BE LIABLE FOR ANY *
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES *
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;*
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND *
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE *
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
*******************************************************************************/
/**
* \file cmds_command.c
* \author Andrés Martinelli <andmarti@gmail.com>
* \date 2017-07-18
* \brief TODO Write a tbrief file description.
*/
#include <string.h>
#include <wchar.h>
#include <stdlib.h>
#include <ctype.h> // for isprint()
#ifndef NO_WORDEXP
#include <wordexp.h>
#endif
#include "sc.h" // for rescol
#include "conf.h"
#include "cmds_command.h"
#include "cmds_edit.h"
#include "cmds.h"
#include "utils/string.h"
#include "utils/dictionary.h"
#include "tui.h"
#include "file.h"
#include "main.h"
#include "interp.h"
#include "hide_show.h"
#include "exec.h"
#include "help.h"
#include "marks.h"
#include "filter.h"
#include "maps.h"
#include "xls.h"
#include "xlsx.h"
#include "cmds_visual.h"
#include "plot.h"
#ifdef UNDO
#include "undo.h"
#endif
extern char * rev;
extern struct dictionary * user_conf_d;
wchar_t inputline[BUFFERSIZE];
extern wchar_t interp_line[BUFFERSIZE];
int inputline_pos; /**< Position in window. Some chars has 2 chars width */
// see https://en.wikipedia.org/wiki/Halfwidth_and_fullwidth_forms
int real_inputline_pos; /**< Real position in inputline */
static wchar_t * valid_commands[] = {
L"!",
L"addfilter",
L"autojus",
L"ccopy",
L"cellcolor",
L"color",
L"cpaste",
L"e csv",
L"e tab",
L"e txt",
L"e mkd",
L"e xlsx",
L"e! csv",
L"e! tab",
L"e! txt",
L"e! mkd",
L"e! xlsx",
L"datefmt",
L"delfilter",
L"delfilters",
L"file",
L"fill",
L"filteron",
L"filteroff",
L"format",
L"freeze",
L"h",
L"help",
L"hiddencols",
L"hiddenrows",
L"hidecol",
L"hiderow",
L"imap",
L"inoremap",
L"int",
L"iunmap",
L"load",
L"load!",
L"lock",
L"unfreeze",
L"unlock",
L"nmap",
L"nnoremap",
L"nunmap",
L"pad",
L"plot",
L"plotedit",
L"q",
L"q!",
L"quit!",
L"quit",
L"redefine_color",
L"refresh",
L"set",
L"showcol",
L"showfilters",
L"showmaps",
L"showrow",
L"showrows",
L"sort",
L"subtotal",
L"trigger",
L"untrigger",
L"unformat",
L"version",
L"w",
L"wq",
L"x",
L"valueize",
(wchar_t *) 0
};
/**
* \brief TODO Document do_commandmode()
*
* \param[in] sb
*
* \return none
*/
void do_commandmode(struct block * sb) {
// If a visual selected range exists
int p = is_range_selected();
struct srange * sr = NULL;
if (p != -1) sr = get_range_by_pos(p);
/*
* Normal KEY handlers for this MODE
*/
if (sb->value == OKEY_BS || sb->value == OKEY_BS2) { // BS
if ( ! wcslen(inputline) || ! real_inputline_pos ) return;
int l = wcwidth(inputline[real_inputline_pos - 1]);
real_inputline_pos--;
del_wchar(inputline, real_inputline_pos);
inputline_pos -= l;
ui_show_header();
#ifdef HISTORY_FILE
if (commandline_history->pos == 0)
del_wchar(get_line_from_history(commandline_history, commandline_history->pos), real_inputline_pos); // Clean history
#endif
ui_show_header();
return;
} else if (sb->value == OKEY_LEFT) { // LEFT
if (inputline_pos) {
real_inputline_pos--;
int l = wcwidth(inputline[real_inputline_pos]);
inputline_pos -= l