| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658 |
- /******************************************************************************
- * This file is part of TinTin++ *
- * *
- * Copyright 2004-2019 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)he K(I)cki(N) (T)ickin D(I)kumud Clie(N)t *
- * *
- * coded by Igor van den Hoven 2006 *
- ******************************************************************************/
- #include "tintin.h"
- DO_COMMAND(do_cursor)
- {
- char all[BUFFER_SIZE], arg1[BUFFER_SIZE], right[BUFFER_SIZE], temp[BUFFER_SIZE];
- int cnt;
- get_arg_in_braces(ses, arg, all, GET_ALL);
- arg = get_arg_in_braces(ses, arg, arg1, GET_ONE);
- arg = sub_arg_in_braces(ses, arg, right, GET_ALL, SUB_VAR|SUB_FUN);
- if (*arg1 == 0)
- {
- tintin_header(ses, " CURSOR OPTIONS ");
- for (cnt = 0 ; *cursor_table[cnt].fun ; cnt++)
- {
- if (*cursor_table[cnt].name)
- {
- convert_meta(cursor_table[cnt].code, temp, FALSE);
- tintin_printf2(ses, " [%-18s] [%-6s] %s", cursor_table[cnt].name, temp, cursor_table[cnt].desc);
- }
- }
- tintin_header(ses, "");
- }
- else
- {
- for (cnt = 0 ; ; cnt++)
- {
- if (HAS_BIT(cursor_table[cnt].flags, CURSOR_FLAG_GET_ALL))
- {
- if (is_abbrev(all, cursor_table[cnt].name))
- {
- cursor_table[cnt].fun(ses, right);
- return ses;
- }
- }
- else if (HAS_BIT(cursor_table[cnt].flags, CURSOR_FLAG_GET_ONE))
- {
- if (is_abbrev(arg1, cursor_table[cnt].name))
- {
- cursor_table[cnt].fun(ses, right);
- return ses;
- }
- }
- else
- {
- break;
- }
- }
- show_error(ses, LIST_COMMAND, "#ERROR: #CURSOR {%s} IS NOT A VALID OPTION.", capitalize(all));
- }
- return ses;
- }
- int inputline_str_str_len(int start, int end)
- {
- int raw_cnt, str_cnt, ret_cnt, width;
- raw_cnt = str_cnt = ret_cnt = 0;
- while (raw_cnt < gtd->input_len)
- {
- if (str_cnt >= end)
- {
- break;
- }
- if (HAS_BIT(gtd->ses->charset, CHARSET_FLAG_UTF8) && is_utf8_head(>d->input_buf[raw_cnt]))
- {
- raw_cnt += get_utf8_width(>d->input_buf[raw_cnt], &width);
- if (str_cnt >= start)
- {
- ret_cnt += width;
- }
- str_cnt += width;
- }
- else
- {
- if (str_cnt >= start)
- {
- ret_cnt++;
- }
- raw_cnt++;
- str_cnt++;
- }
- }
- return ret_cnt;
- }
- // raw range
- int inputline_raw_str_len(int start, int end)
- {
- int raw_cnt, ret_cnt, width;
- raw_cnt = start;
- ret_cnt = 0;
- while (raw_cnt < gtd->input_len)
- {
- if (raw_cnt >= end)
- {
- break;
- }
- if (HAS_BIT(gtd->ses->charset, CHARSET_FLAG_UTF8) && is_utf8_head(>d->input_buf[raw_cnt]))
- {
- raw_cnt += get_utf8_width(>d->input_buf[raw_cnt], &width);
- ret_cnt += width;
- }
- else
- {
- raw_cnt++;
- ret_cnt++;
- }
- }
- return ret_cnt;
- }
- // display range
- int inputline_str_raw_len(int start, int end)
- {
- int raw_cnt, str_cnt, ret_cnt, width, tmp_cnt;
- raw_cnt = str_cnt = ret_cnt = 0;
- while (raw_cnt < gtd->input_len)
- {
- if (str_cnt >= end)
- {
- break;
- }
- if (HAS_BIT(gtd->ses->charset, CHARSET_FLAG_UTF8) && is_utf8_head(>d->input_buf[raw_cnt]))
- {
- tmp_cnt = get_utf8_width(>d->input_buf[raw_cnt], &width);
- if (str_cnt >= start)
- {
- ret_cnt += tmp_cnt;
- }
- raw_cnt += tmp_cnt;
- str_cnt += width;
- }
- else
- {
- if (str_cnt >= start)
- {
- ret_cnt++;
- }
- raw_cnt++;
- str_cnt++;
- }
- }
- return ret_cnt;
- }
- int inputline_raw_raw_len(int start, int end)
- {
- if (start > end)
- {
- return 0;
- }
- return end - start;
- }
- // Get string length of the input area
- int inputline_max_str_len(void)
- {
- return gtd->screen->cols + 1 - gtd->input_off - (HAS_BIT(gtd->flags, TINTIN_FLAG_HISTORYSEARCH) ? 11 : 0);
- }
- int inputline_cur_str_len(void)
- {
- return inputline_str_str_len(gtd->input_hid, gtd->input_hid + inputline_max_str_len());
- }
- // Get the position of the cursor
- int inputline_cur_pos(void)
- {
- return gtd->input_off + gtd->input_pos - gtd->input_hid;
- }
- // Check for invalid characters.
- int inputline_str_chk(int offset, int totlen)
- {
- int size;
- while (offset < totlen)
- {
- if (HAS_BIT(gtd->ses->charset, CHARSET_FLAG_BIG5))
- {
- if (HAS_BIT(gtd->input_buf[offset], 128) && (unsigned char) gtd->input_buf[offset] < 255)
- {
- if (offset + 1 >= totlen)
- {
- return FALSE;
- }
- if (is_big5(>d->input_buf[offset]))
- {
- offset += 2;
- }
- else
- {
- offset += 1;
- }
- }
- else
- {
- offset += 1;
- }
- }
- else if (HAS_BIT(gtd->ses->charset, CHARSET_FLAG_UTF8))
- {
- if (is_utf8_head(>d->input_buf[offset]))
- {
- size = get_utf8_size(>d->input_buf[offset]);
- if (size == 1 || offset + size > totlen)
- {
- return FALSE;
- }
- offset += size;
- }
- else
- {
- offset += 1;
- }
- }
- else
- {
- return TRUE;
- }
- }
- return TRUE;
- }
- DO_CURSOR(cursor_backspace)
- {
- if (gtd->input_cur == 0)
- {
- return;
- }
- cursor_left(ses, "");
- cursor_delete(ses, "");
- modified_input();
- }
- DO_CURSOR(cursor_brace_open)
- {
- ins_sprintf(>d->input_buf[gtd->input_cur], "{");
- gtd->input_len++;
- gtd->input_cur++;
- gtd->input_pos += inputline_raw_str_len(gtd->input_cur - 1, gtd->input_cur);
- cursor_redraw_line(ses, "");
- modified_input();
- }
- DO_CURSOR(cursor_brace_close)
- {
- ins_sprintf(>d->input_buf[gtd->input_cur], "}");
- gtd->input_len++;
- gtd->input_cur++;
- gtd->input_pos += inputline_raw_str_len(gtd->input_cur - 1, gtd->input_cur);
- cursor_redraw_line(ses, "");
- modified_input();
- }
- DO_CURSOR(cursor_buffer_down)
- {
- do_buffer(ses, "DOWN");
- }
- DO_CURSOR(cursor_buffer_end)
- {
- do_buffer(ses, "END");
- }
- DO_CURSOR(cursor_buffer_home)
- {
- do_buffer(ses, "HOME");
- }
- DO_CURSOR(cursor_buffer_lock)
- {
- do_buffer(ses, "LOCK");
- }
- DO_CURSOR(cursor_buffer_up)
- {
- do_buffer(ses, "UP");
- }
- DO_CURSOR(cursor_check_line)
- {
- if (gtd->input_pos - gtd->input_hid > inputline_max_str_len() - 3)
- {
- return cursor_redraw_line(ses, "");
- }
- if (gtd->input_hid && gtd->input_pos - gtd->input_hid < 2)
- {
- return cursor_redraw_line(ses, "");
- }
- }
- DO_CURSOR(cursor_check_line_modified)
- {
- if (gtd->input_hid + inputline_max_str_len() < inputline_raw_str_len(0, gtd->input_len))
- {
- return cursor_redraw_line(ses, "");
- }
- return cursor_check_line(ses, "");
- }
- DO_CURSOR(cursor_clear_left)
- {
- if (gtd->input_cur == 0)
- {
- return;
- }
- sprintf(gtd->paste_buf, "%.*s", gtd->input_cur, gtd->input_buf);
- input_printf("\e[%dG\e[%dP", gtd->input_off, gtd->input_pos - gtd->input_hid);
- memmove(>d->input_buf[0], >d->input_buf[gtd->input_cur], gtd->input_len - gtd->input_cur);
- gtd->input_len -= gtd->input_cur;
- gtd->input_buf[gtd->input_len] = 0;
- gtd->input_cur = 0;
- gtd->input_pos = 0;
- cursor_check_line_modified(ses, "");
- modified_input();
- }
- DO_CURSOR(cursor_clear_line)
- {
- if (gtd->input_len == 0)
- {
- return;
- }
- sprintf(gtd->paste_buf, "%s", gtd->input_buf);
- input_printf("\e[%dG\e[%dP", gtd->input_off, inputline_cur_str_len());
- gtd->input_len = 0;
- gtd->input_cur = 0;
- gtd->input_hid = 0;
- gtd->input_pos = 0;
- gtd->input_buf[0] = 0;
- modified_input();
- }
- DO_CURSOR(cursor_clear_right)
- {
- if (gtd->input_cur == gtd->input_len)
- {
- return;
- }
- strcpy(gtd->paste_buf, >d->input_buf[gtd->input_cur]);
- input_printf("\e[%dP", inputline_max_str_len() - inputline_str_str_len(gtd->input_hid, gtd->input_pos));
- gtd->input_buf[gtd->input_cur] = 0;
- gtd->input_len = gtd->input_cur;
- modified_input();
- }
- DO_CURSOR(cursor_convert_meta)
- {
- SET_BIT(gtd->flags, TINTIN_FLAG_CONVERTMETACHAR);
- }
- DO_CURSOR(cursor_delete_or_exit)
- {
- if (gtd->input_len == 0)
- {
- cursor_exit(ses, "");
- }
- else
- {
- cursor_delete(ses, "");
- }
- }
- DO_CURSOR(cursor_delete)
- {
- int size, width;
- if (gtd->input_len == 0)
- {
- return;
- }
- if (gtd->input_len == gtd->input_cur)
- {
- return;
- }
- if (HAS_BIT(ses->charset, CHARSET_FLAG_BIG5) && is_big5(>d->input_buf[gtd->input_cur]))
- {
- gtd->input_len--;
- memmove(>d->input_buf[gtd->input_cur+1], >d->input_buf[gtd->input_cur+2], gtd->input_len - gtd->input_cur + 1);
- input_printf("\e[2P");
- }
- else if (HAS_BIT(ses->charset, CHARSET_FLAG_UTF8))
- {
- size = get_utf8_width(>d->input_buf[gtd->input_cur], &width);
- gtd->input_len -= size;
- memmove(>d->input_buf[gtd->input_cur], >d->input_buf[gtd->input_cur + size], gtd->input_len - gtd->input_cur + 1);
- if (width)
- {
- input_printf("\e[%dP", width);
- }
- while (gtd->input_len > gtd->input_cur)
- {
- size = get_utf8_width(>d->input_buf[gtd->input_cur], &width);
- if (width)
- {
- break;
- }
- gtd->input_len -= size;
- memmove(>d->input_buf[gtd->input_cur], >d->input_buf[gtd->input_cur + size], gtd->input_len - gtd->input_cur + 1);
- }
- }
- else
- {
- gtd->input_len--;
- memmove(>d->input_buf[gtd->input_cur], >d->input_buf[gtd->input_cur+1], gtd->input_len - gtd->input_cur + 1);
- input_printf("\e[1P");
- }
- if (gtd->input_hid + inputline_max_str_len() <= inputline_raw_str_len(0, gtd->input_len))
- {
- cursor_redraw_line(ses, "");
- }
- modified_input();
- }
- DO_CURSOR(cursor_delete_word_left)
- {
- int index_cur, width;
- if (gtd->input_cur == 0)
- {
- return;
- }
- index_cur = gtd->input_cur;
- while (gtd->input_cur > 0 && gtd->input_buf[gtd->input_cur - 1] == ' ')
- {
- gtd->input_pos--;
- gtd->input_cur--;
- }
- while (gtd->input_cur > 0 && gtd->input_buf[gtd->input_cur - 1] != ' ')
- {
- if (HAS_BIT(ses->charset, CHARSET_FLAG_UTF8))
- {
- if (is_utf8_tail(>d->input_buf[gtd->input_cur]))
- {
- gtd->input_cur--;
- }
- else if (is_utf8_head(>d->input_buf[gtd->input_cur]))
- {
- get_utf8_width(>d->input_buf[gtd->input_cur], &width);
- gtd->input_cur -= 1;
- gtd->input_pos -= width;
- }
- else
- {
- gtd->input_cur--;
- gtd->input_pos--;
- }
- }
- else
- {
- gtd->input_pos--;
- gtd->input_cur--;
- }
- }
- sprintf(gtd->paste_buf, "%.*s", index_cur - gtd->input_cur, >d->input_buf[gtd->input_cur]);
- memmove(>d->input_buf[gtd->input_cur], >d->input_buf[index_cur], gtd->input_len - index_cur + 1);
- // input_printf("\e[%dD\e[%dP", index_cur - gtd->input_cur, index_cur - gtd->input_cur);
- gtd->input_len -= index_cur - gtd->input_cur;
- cursor_redraw_line(ses, "");
- modified_input();
- }
- DO_CURSOR(cursor_delete_word_right)
- {
- int index_cur;
- if (gtd->input_cur == gtd->input_len)
- {
- return;
- }
- index_cur = gtd->input_cur;
- while (gtd->input_cur != gtd->input_len && gtd->input_buf[gtd->input_cur] == ' ')
- {
- gtd->input_cur++;
- }
- while (gtd->input_cur != gtd->input_len && gtd->input_buf[gtd->input_cur] != ' ')
- {
- gtd->input_cur++;
- }
- sprintf(gtd->paste_buf, "%.*s", gtd->input_cur - index_cur, >d->input_buf[gtd->input_cur]);
- memmove(>d->input_buf[index_cur], >d->input_buf[gtd->input_cur], gtd->input_len - gtd->input_cur + 1);
- // input_printf("\e[%dP", gtd->input_cur - index_cur);
- gtd->input_len -= gtd->input_cur - index_cur;
- gtd->input_cur = index_cur;
- cursor_redraw_line(ses, "");
- modified_input();
- }
- DO_CURSOR(cursor_echo)
- {
- if (*arg == 0)
- {
- TOG_BIT(ses->telopts, TELOPT_FLAG_ECHO);
- }
- else if (!strcasecmp(arg, "ON"))
- {
- SET_BIT(ses->telopts, TELOPT_FLAG_ECHO);
- }
- else if (!strcasecmp(arg, "OFF"))
- {
- DEL_BIT(ses->telopts, TELOPT_FLAG_ECHO);
- }
- else
- {
- show_error(gtd->ses, LIST_COMMAND, "#SYNTAX: #CURSOR {ECHO} {ON|OFF}.");
- }
- }
- DO_CURSOR(cursor_end)
- {
- gtd->input_cur = gtd->input_len;
- gtd->input_pos = inputline_raw_str_len(0, gtd->input_len);
- cursor_redraw_line(ses, "");
- }
- DO_CURSOR(cursor_enter)
- {
- input_printf("\n");
- gtd->input_buf[gtd->input_len] = 0;
- gtd->input_len = 0;
- gtd->input_cur = 0;
- gtd->input_pos = 0;
- gtd->input_off = 1;
- gtd->input_hid = 0;
- gtd->macro_buf[0] = 0;
- gtd->input_tmp[0] = 0;
- if (HAS_BIT(gtd->flags, TINTIN_FLAG_HISTORYSEARCH))
- {
- struct listroot *root = ses->list[LIST_HISTORY];
- if (root->update >= 0 && root->update < root->used)
- {
- strcpy(gtd->input_buf, root->list[root->update]->arg1);
- }
- DEL_BIT(gtd->flags, TINTIN_FLAG_HISTORYSEARCH);
- gtd->input_off = 1;
- }
- SET_BIT(gtd->flags, TINTIN_FLAG_PROCESSINPUT);
- modified_input();
- }
- DO_CURSOR(cursor_exit)
- {
- if (ses == gts)
- {
- do_end(ses, "");
- }
- else
- {
- cleanup_session(ses);
- }
- }
- DO_CURSOR(cursor_get)
- {
- if (*arg == 0)
- {
- show_error(ses, LIST_COMMAND, "#SYNTAX: #CURSOR GET {variable}");
- }
- else
- {
- set_nest_node(ses->list[LIST_VARIABLE], arg, "%s", gtd->input_buf);
- }
- }
- DO_CURSOR(cursor_history_next)
- {
- struct listroot *root = ses->list[LIST_HISTORY];
- if (HAS_BIT(gtd->flags, TINTIN_FLAG_HISTORYSEARCH))
- {
- if (root->update == root->used)
- {
- return;
- }
- for (root->update++ ; root->update < root->used ; root->update++)
- {
- if (*gtd->input_buf && find(ses, root->list[root->update]->arg1, gtd->input_buf, SUB_NONE, REGEX_FLAG_NONE))
- {
- break;
- }
- }
- if (root->update < root->used)
- {
- input_printf("\e[%dG \e[0K%.*s\e[%dG", gtd->input_off + inputline_cur_str_len() + 2, gtd->input_off + inputline_max_str_len() - inputline_cur_str_len() - 4, root->list[root->update]->arg1, gtd->input_off + gtd->input_pos - gtd->input_hid);
- }
- return;
- }
- if (root->list[0] == NULL)
- {
- return;
- }
- if (!HAS_BIT(gtd->flags, TINTIN_FLAG_HISTORYBROWSE))
- {
- return;
- }
- else if (root->update < root->used)
- {
- for (root->update++ ; root->update < root->used ; root->update++)
- {
- if (!strncmp(gtd->input_tmp, root->list[root->update]->arg1, strlen(gtd->input_tmp)))
- {
- break;
- }
- }
- }
- cursor_clear_line(ses, "");
- if (root->update == root->used)
- {
- strcpy(gtd->input_buf, gtd->input_tmp);
- }
- else
- {
- strcpy(gtd->input_buf, root->list[root->update]->arg1);
- }
- gtd->input_len = strlen(gtd->input_buf);
- cursor_end(ses, "");
- SET_BIT(gtd->flags, TINTIN_FLAG_HISTORYBROWSE);
- }
- DO_CURSOR(cursor_history_prev)
- {
- struct listroot *root = ses->list[LIST_HISTORY];
- if (HAS_BIT(gtd->flags, TINTIN_FLAG_HISTORYSEARCH))
- {
- if (root->update <= 0)
- {
- return;
- }
- for (root->update-- ; root->update >= 0 ; root->update--)
- {
- if (*gtd->input_buf && find(ses, root->list[root->update]->arg1, gtd->input_buf, SUB_NONE, REGEX_FLAG_NONE))
- {
- break;
- }
- }
- if (root->update >= 0)
- {
- input_printf("\e[%dG \e[0K%.*s\e[%dG", gtd->input_off + inputline_cur_str_len() + 2, gtd->input_off + inputline_max_str_len() - inputline_cur_str_len() - 4, root->list[root->update]->arg1, gtd->input_off + gtd->input_pos - gtd->input_hid);
- }
- return;
- }
- if (root->list[0] == NULL)
- {
- return;
- }
- if (!HAS_BIT(gtd->flags, TINTIN_FLAG_HISTORYBROWSE))
- {
- strcpy(gtd->input_tmp, gtd->input_buf);
- for (root->update = root->used - 1 ; root->update >= 0 ; root->update--)
- {
- if (!strncmp(gtd->input_tmp, root->list[root->update]->arg1, strlen(gtd->input_tmp)))
- {
- break;
- }
- }
- }
- else if (root->update >= 0)
- {
- for (root->update-- ; root->update >= 0 ; root->update--)
- {
- if (!strncmp(gtd->input_tmp, root->list[root->update]->arg1, strlen(gtd->input_tmp)))
- {
- break;
- }
- }
- }
- cursor_clear_line(ses, "");
- if (root->update == -1)
- {
- strcpy(gtd->input_buf, gtd->input_tmp);
- }
- else
- {
- strcpy(gtd->input_buf, root->list[root->update]->arg1);
- }
- gtd->input_len = strlen(gtd->input_buf);
- cursor_end(ses, "");
- SET_BIT(gtd->flags, TINTIN_FLAG_HISTORYBROWSE);
- }
- DO_CURSOR(cursor_history_search)
- {
- struct listroot *root = ses->list[LIST_HISTORY];
- if (root->list[0] == NULL)
- {
- return;
- }
- if (!HAS_BIT(gtd->flags, TINTIN_FLAG_HISTORYSEARCH))
- {
- strcpy(gtd->input_tmp, gtd->input_buf);
- cursor_clear_line(ses, "");
- SET_BIT(gtd->flags, TINTIN_FLAG_HISTORYSEARCH);
- gtd->input_off = 11;
- root->update = root->used - 1;
- input_printf("(search) [ ] \e[3D");
- }
- else
- {
- if (root->update >= 0 && root->update < root->used)
- {
- strcpy(gtd->input_buf, root->list[root->update]->arg1);
- }
- input_printf("\e[1G\e[0K");
- gtd->input_len = strlen(gtd->input_buf);
- gtd->input_cur = gtd->input_len;
- gtd->input_pos = 0;
- root->update = -1;
- DEL_BIT(gtd->flags, TINTIN_FLAG_HISTORYSEARCH);
- gtd->input_off = 1;
- cursor_redraw_line(ses, "");
- }
- }
- DO_CURSOR(cursor_history_find)
- {
- struct listroot *root = ses->list[LIST_HISTORY];
- push_call("cursor_history_find(%s)", gtd->input_buf);
- if (HAS_BIT(ses->charset, CHARSET_FLAG_UTF8|CHARSET_FLAG_BIG5))
- {
- if (inputline_str_chk(0, gtd->input_len) == FALSE)
- {
- pop_call();
- return;
- }
- }
- gtd->level->quiet++;
- for (root->update = root->used - 1 ; root->update >= 0 ; root->update--)
- {
- if (*gtd->input_buf && find(ses, root->list[root->update]->arg1, gtd->input_buf, SUB_NONE, REGEX_FLAG_NONE))
- {
- break;
- }
- }
- gtd->level->quiet--;
- if (root->update >= 0)
- {
- input_printf("\e[%dG ] %.*s\e[%dG", gtd->input_off + inputline_cur_str_len(), gtd->input_off + inputline_max_str_len() - inputline_cur_str_len() - 4, root->list[root->update]->arg1, gtd->input_off + gtd->input_pos - gtd->input_hid);
- }
- else
- {
- input_printf("\e[%dG ] \e[0K\e[%dG", gtd->input_off + inputline_cur_str_len(), gtd->input_off + gtd->input_pos - gtd->input_hid);
- }
- pop_call();
- return;
- }
- DO_CURSOR(cursor_home)
- {
- if (gtd->input_cur == 0)
- {
- return;
- }
- input_printf("\e[%dD", gtd->input_pos - gtd->input_hid);
- gtd->input_cur = 0;
- gtd->input_pos = 0;
- if (gtd->input_hid)
- {
- gtd->input_hid = 0;
- cursor_redraw_line(ses, "");
- }
- }
- DO_CURSOR(cursor_insert)
- {
- if (*arg == 0)
- {
- TOG_BIT(gtd->flags, TINTIN_FLAG_INSERTINPUT);
- }
- else if (!strcasecmp(arg, "ON"))
- {
- SET_BIT(gtd->flags, TINTIN_FLAG_INSERTINPUT);
- }
- else if (!strcasecmp(arg, "OFF"))
- {
- DEL_BIT(gtd->flags, TINTIN_FLAG_INSERTINPUT);
- }
- else
- {
- show_error(gtd->ses, LIST_COMMAND, "#SYNTAX: #CURSOR {INSERT} {ON|OFF}.");
- }
- }
- DO_CURSOR(cursor_left)
- {
- int width;
- if (gtd->input_cur > 0)
- {
- if (HAS_BIT(ses->charset, CHARSET_FLAG_BIG5))
- {
- gtd->input_cur--;
- gtd->input_pos--;
- input_printf("\e[1D");
- if (inputline_str_chk(0, gtd->input_cur) == FALSE)
- {
- gtd->input_cur--;
- gtd->input_pos--;
- input_printf("\e[1D");
- }
- }
- else if (HAS_BIT(ses->charset, CHARSET_FLAG_UTF8))
- {
- gtd->input_cur--;
- if (gtd->input_cur > 0 && is_utf8_tail(>d->input_buf[gtd->input_cur]))
- {
- do
- {
- gtd->input_cur--;
- }
- while (gtd->input_cur > 0 && is_utf8_tail(>d->input_buf[gtd->input_cur]));
- get_utf8_width(>d->input_buf[gtd->input_cur], &width);
- if (width == 0)
- {
- return cursor_left(ses, "");
- }
- input_printf("\e[%dD", width);
- gtd->input_pos -= width;
- }
- else
- {
- gtd->input_pos--;
- input_printf("\e[1D");
- }
- }
- else
- {
- gtd->input_cur--;
- gtd->input_pos--;
- input_printf("\e[1D");
- }
- cursor_check_line(ses, "");
- }
- }
- DO_CURSOR(cursor_left_word)
- {
- int width;
- // int index_pos;
- if (gtd->input_cur == 0)
- {
- return;
- }
- // index_pos = gtd->input_pos;
- while (gtd->input_cur > 0 && gtd->input_buf[gtd->input_cur - 1] == ' ')
- {
- gtd->input_pos--;
- gtd->input_cur--;
- }
- while (gtd->input_cur > 0 && gtd->input_buf[gtd->input_cur - 1] != ' ')
- {
- gtd->input_cur--;
- if (HAS_BIT(ses->charset, CHARSET_FLAG_UTF8))
- {
- if (!is_utf8_tail(>d->input_buf[gtd->input_cur]))
- {
- get_utf8_width(>d->input_buf[gtd->input_cur], &width);
- gtd->input_pos -= width;
- }
- }
- else
- {
- gtd->input_pos--;
- }
- }
- // input_printf("\e[%dD", index_pos - gtd->input_pos);
- cursor_redraw_line(ses, "");
- }
- DO_CURSOR(cursor_paste_buffer)
- {
- if (*gtd->paste_buf == 0)
- {
- return;
- }
- ins_sprintf(>d->input_buf[gtd->input_cur], "%s", gtd->paste_buf);
- gtd->input_len += strlen(gtd->paste_buf);
- gtd->input_cur += strlen(gtd->paste_buf);
- gtd->input_pos += inputline_raw_str_len(gtd->input_cur - strlen(gtd->paste_buf), gtd->input_cur);
- cursor_redraw_line(ses, "");
- modified_input();
- }
- DO_CURSOR(cursor_redraw_input)
- {
- if (IS_SPLIT(ses))
- {
- cursor_redraw_line(ses, "");
- }
- else
- {
- /* if (*gtd->ses->scroll->input)
- {
- input_printf("\e[G%s", gtd->ses->scroll->input);
- }*/
- cursor_redraw_line(ses, "");
- /*
- gtd->input_cur = gtd->input_len;
- gtd->input_pos = gtd->input_len % gtd->screen->cols;
- */
- }
- }
- DO_CURSOR(cursor_redraw_line)
- {
- if (HAS_BIT(ses->charset, CHARSET_FLAG_UTF8|CHARSET_FLAG_BIG5))
- {
- if (inputline_str_chk(0, gtd->input_len) == FALSE)
- {
- return;
- }
- }
- // Erase current input
- input_printf("\e[%dG\e[%dP", gtd->input_off, inputline_max_str_len());
- // Center long lines of input
- if (gtd->input_pos > inputline_max_str_len() - 3)
- {
- while (gtd->input_pos - gtd->input_hid > inputline_max_str_len() - 3)
- {
- gtd->input_hid += inputline_max_str_len() / 2;
- }
- while (gtd->input_pos - gtd->input_hid < 2)
- {
- gtd->input_hid -= inputline_max_str_len() / 2;
- }
- }
- else
- {
- if (gtd->input_hid && gtd->input_pos - gtd->input_hid < 2)
- {
- gtd->input_hid = 0;
- }
- }
- // Print the entire thing
- if (gtd->input_hid)
- {
- if (gtd->input_hid + inputline_max_str_len() >= inputline_raw_str_len(0, gtd->input_len))
- {
- input_printf("<%.*s\e[%dG", inputline_str_raw_len(gtd->input_hid + 1, gtd->input_hid + inputline_max_str_len() - 0), >d->input_buf[inputline_str_raw_len(0, gtd->input_hid + 1)], gtd->input_off + gtd->input_pos - gtd->input_hid);
- }
- else
- {
- input_printf("<%.*s>\e[%dG", inputline_str_raw_len(gtd->input_hid + 1, gtd->input_hid + inputline_max_str_len() - 1), >d->input_buf[inputline_str_raw_len(0, gtd->input_hid + 1)], gtd->input_off + gtd->input_pos - gtd->input_hid);
- }
- }
- else
- {
- if (gtd->input_hid + inputline_max_str_len() >= inputline_raw_str_len(0, gtd->input_len))
- {
- input_printf("%.*s\e[%dG", inputline_str_raw_len(gtd->input_hid + 0, gtd->input_hid + inputline_max_str_len() - 0), >d->input_buf[inputline_str_raw_len(0, gtd->input_hid + 0)], gtd->input_off + gtd->input_pos - gtd->input_hid);
- }
- else
- {
- input_printf("%.*s>\e[%dG", inputline_str_raw_len(gtd->input_hid + 0, gtd->input_hid + inputline_max_str_len() - 1), >d->input_buf[inputline_str_raw_len(0, gtd->input_hid + 0)], gtd->input_off + gtd->input_pos - gtd->input_hid);
- }
- }
- }
- DO_CURSOR(cursor_right)
- {
- int size, width;
- if (gtd->input_cur < gtd->input_len)
- {
- if (HAS_BIT(ses->charset, CHARSET_FLAG_BIG5) && (gtd->input_buf[gtd->input_cur] & 128) == 128)
- {
- if (gtd->input_cur + 1 < gtd->input_len && gtd->input_buf[gtd->input_cur+1])
- {
- gtd->input_cur += 2;
- gtd->input_pos += 2;
- input_printf("\e[2C");
- }
- }
- else if (HAS_BIT(ses->charset, CHARSET_FLAG_UTF8))
- {
- gtd->input_cur += get_utf8_width(>d->input_buf[gtd->input_cur], &width);
- if (width == 0)
- {
- return cursor_right(ses, "");
- }
- input_printf("\e[%dC", width);
- gtd->input_pos += width;
- while (gtd->input_cur < gtd->input_len)
- {
- size = get_utf8_width(>d->input_buf[gtd->input_cur], &width);
- if (width)
- {
- break;
- }
- gtd->input_cur += size;
- }
- }
- else
- {
- input_printf("\e[1C");
- gtd->input_cur++;
- gtd->input_pos++;
- }
- }
- cursor_check_line(ses, "");
- }
- DO_CURSOR(cursor_right_word)
- {
- if (gtd->input_cur == gtd->input_len)
- {
- return;
- }
- while (gtd->input_cur < gtd->input_len && gtd->input_buf[gtd->input_cur] == ' ')
- {
- gtd->input_cur++;
- gtd->input_pos++;
- }
- while (gtd->input_cur < gtd->input_len && gtd->input_buf[gtd->input_cur] != ' ')
- {
- if (!HAS_BIT(ses->charset, CHARSET_FLAG_UTF8) || (gtd->input_buf[gtd->input_cur] & 192) != 128)
- {
- gtd->input_pos++;
- }
- gtd->input_cur++;
- }
- cursor_redraw_line(ses, "");
- }
- DO_CURSOR(cursor_set)
- {
- if (*arg == 0)
- {
- return;
- }
- ins_sprintf(>d->input_buf[gtd->input_cur], "%s", arg);
- gtd->input_len += strlen(arg);
- gtd->input_cur += strlen(arg);
- gtd->input_pos += inputline_raw_str_len(gtd->input_cur - strlen(arg), gtd->input_cur);
- cursor_redraw_line(ses, "");
- modified_input();
- }
- DO_CURSOR(cursor_suspend)
- {
- do_suspend(ses, "");
- }
- DO_CURSOR(cursor_info)
- {
- tintin_printf2(ses, "Width of input bar: %10d", inputline_max_str_len());
- tintin_printf2(ses, "Offset of input bar: %10d", gtd->input_off);
- tintin_printf2(ses, "Width of hidden text on left: %10d", gtd->input_hid);
- tintin_printf2(ses, "VT100 position of cursor: %10d", gtd->input_pos);
- tintin_printf2(ses, "internal position of cursor: %10d", gtd->input_cur);
- tintin_printf2(ses, "internal length of input line: %10d", gtd->input_len);
- tintin_printf2(ses, "VT100 length of displayed line: %10d", inputline_cur_str_len());
- }
- /*
- Improved tab handling by Ben Love
- */
- int cursor_tab_add(int input_now, int stop_after_first)
- {
- struct listroot *root = gtd->ses->list[LIST_TAB];
- struct listnode *node;
- char tab[BUFFER_SIZE];
- if (!HAS_BIT(root->flags, LIST_FLAG_IGNORE))
- {
- for (root->update = 0 ; root->update < root->used ; root->update++)
- {
- node = root->list[root->update];
- substitute(gtd->ses, node->arg1, tab, SUB_VAR|SUB_FUN);
- if (!strncmp(tab, >d->input_buf[input_now], strlen(>d->input_buf[input_now])))
- {
- if (search_node_list(gtd->ses->list[LIST_COMMAND], tab))
- {
- continue;
- }
- insert_node_list(gtd->ses->list[LIST_COMMAND], tab, "", "", "");
- if (HAS_BIT(node->flags, NODE_FLAG_ONESHOT))
- {
- delete_node_list(gtd->ses, LIST_TAB, node);
- }
- if (stop_after_first)
- {
- return TRUE;
- }
- }
- }
- }
- return FALSE;
- }
- int cursor_auto_tab_add(int input_now, int stop_after_first)
- {
- char tab[BUFFER_SIZE], buf[BUFFER_SIZE];
- int scroll_cnt, line_cnt, tab_len;
- char *arg;
- line_cnt = 0;
- for (scroll_cnt = gtd->ses->scroll->used - 1 ; scroll_cnt > 0 ; scroll_cnt--)
- {
- if (HAS_BIT(gtd->ses->scroll->buffer[scroll_cnt]->flags, BUFFER_FLAG_GREP))
- {
- continue;
- }
- if (line_cnt++ >= gtd->ses->auto_tab)
- {
- break;
- }
- strip_vt102_codes(gtd->ses->scroll->buffer[scroll_cnt]->str, buf);
- arg = buf;
- while (*arg)
- {
- arg = get_arg_in_braces(gtd->ses, arg, tab, GET_ONE);
- if (!strncmp(tab, >d->input_buf[input_now], strlen(>d->input_buf[input_now])))
- {
- tab_len = strlen(tab) -1;
- if (tab_len > 0)
- {
- if (tab[tab_len] == '.' || tab[tab_len] == ',' || tab[tab_len] == ';')
- {
- tab[tab_len] = 0;
- }
- }
- if (search_node_list(gtd->ses->list[LIST_COMMAND], tab))
- {
- continue;
- }
- insert_node_list(gtd->ses->list[LIST_COMMAND], tab, "", "", "");
- if (stop_after_first)
- {
- return TRUE;
- }
- }
- if (*arg == COMMAND_SEPARATOR)
- {
- arg++;
- }
- }
- }
- return FALSE;
- }
- void cursor_hide_completion(int input_now)
- {
- struct listroot *root = gtd->ses->list[LIST_COMMAND];
- struct listnode *f_node;
- struct listnode *l_node;
- int len_change;
- f_node = root->list[0];
- l_node = root->list[root->used - 1];
- if (root->used && !strcmp(l_node->arg1, gtd->input_buf + input_now))
- {
- len_change = strlen(l_node->arg1) - strlen(f_node->arg1);
- if (len_change > 0)
- {
- /*
- if (gtd->input_cur < gtd->input_len)
- {
- input_printf("\e[%dC", gtd->input_len - gtd->input_cur);
- }
- input_printf("\e[%dD\e[%dP", len_change, len_change);
- */
- gtd->input_len = gtd->input_len - len_change;
- gtd->input_buf[gtd->input_len] = 0;
- gtd->input_cur = gtd->input_len;
- gtd->input_pos = gtd->input_pos;
- }
- }
- return;
- }
- void cursor_show_completion(int input_now, int show_last_node)
- {
- struct listroot *root = gtd->ses->list[LIST_COMMAND];
- struct listnode *node;
- if (!root->used)
- {
- return;
- }
- node = show_last_node ? root->list[root->used - 1] : root->list[0];
- /*
- if (gtd->input_cur < gtd->input_len)
- {
- input_printf("\e[%dC", gtd->input_len - gtd->input_cur);
- }
- if (gtd->input_len > input_now)
- {
- input_printf("\e[%dD\e[%dP", gtd->input_len - input_now, gtd->input_len - input_now);
- }
- if (input_now + (int) strlen(node->arg1) < gtd->ses->cols - 2)
- {
- input_printf("%s", node->arg1);
- }
- */
- strcpy(>d->input_buf[input_now], node->arg1);
- gtd->input_len = input_now + strlen(node->arg1);
- gtd->input_cur = gtd->input_len;
- cursor_end(gtd->ses, "");
- if (HAS_BIT(gtd->flags, TINTIN_FLAG_HISTORYSEARCH))
- {
- cursor_history_find(gtd->ses, "");
- }
- if (node == root->list[0])
- {
- kill_list(root);
- }
- return;
- }
- int cursor_calc_input_now(void)
- {
- int input_now;
- if (gtd->input_len == 0 || gtd->input_buf[gtd->input_len - 1] == ' ')
- {
- return -1;
- }
- for (input_now = gtd->input_len - 1 ; input_now ; input_now--)
- {
- if (gtd->input_buf[input_now] == ' ')
- {
- return input_now + 1;
- }
- }
- return input_now;
- }
- DO_CURSOR(cursor_tab_forward)
- {
- struct listroot *root = ses->list[LIST_COMMAND];
- int tab_found;
- if (!root->list[0])
- {
- gtd->input_tab = cursor_calc_input_now();
- }
- if (gtd->input_tab == -1)
- {
- return;
- }
- cursor_hide_completion(gtd->input_tab);
- if (!root->list[0])
- {
- insert_node_list(root, >d->input_buf[gtd->input_tab], "", "", "");
- }
- tab_found = cursor_tab_add(gtd->input_tab, TRUE);
- cursor_show_completion(gtd->input_tab, tab_found);
- }
- DO_CURSOR(cursor_tab_backward)
- {
- struct listroot *root = ses->list[LIST_COMMAND];
- if (!root->list[0])
- {
- gtd->input_tab = cursor_calc_input_now();
- }
- if (gtd->input_tab == -1)
- {
- return;
- }
- cursor_hide_completion(gtd->input_tab);
- if (root->used)
- {
- delete_index_list(root, root->used - 1);
- }
- if (!root->list[0])
- {
- insert_node_list(root, >d->input_buf[gtd->input_tab], "", "", "");
- cursor_tab_add(gtd->input_tab, FALSE);
- }
- cursor_show_completion(gtd->input_tab, TRUE);
- }
- DO_CURSOR(cursor_auto_tab_forward)
- {
- struct listroot *root = ses->list[LIST_COMMAND];
- int tab_found;
- if (!root->list[0])
- {
- gtd->input_tab = cursor_calc_input_now();
- }
- if (gtd->input_tab == -1)
- {
- return;
- }
- cursor_hide_completion(gtd->input_tab);
- if (!root->list[0])
- {
- insert_node_list(root, >d->input_buf[gtd->input_tab], "", "", "");
- }
- tab_found = cursor_auto_tab_add(gtd->input_tab, TRUE);
- cursor_show_completion(gtd->input_tab, tab_found);
- }
- DO_CURSOR(cursor_auto_tab_backward)
- {
- struct listroot *root = ses->list[LIST_COMMAND];
- if (!root->list[0])
- {
- gtd->input_tab = cursor_calc_input_now();
- }
- if (gtd->input_tab == -1)
- {
- return;
- }
- cursor_hide_completion(gtd->input_tab);
- if (root->used)
- {
- delete_index_list(root, root->used - 1);
- }
- if (!root->list[0])
- {
- insert_node_list(root, >d->input_buf[gtd->input_tab], "", "", "");
- cursor_auto_tab_add(gtd->input_tab, FALSE);
- }
- cursor_show_completion(gtd->input_tab, TRUE);
- }
- DO_CURSOR(cursor_mixed_tab_forward)
- {
- struct listroot *root = ses->list[LIST_COMMAND];
- int tab_found;
- if (!root->list[0])
- {
- gtd->input_tab = cursor_calc_input_now();
- }
- if (gtd->input_tab == -1)
- {
- return;
- }
- cursor_hide_completion(gtd->input_tab);
- if (!root->list[0])
- {
- insert_node_list(root, >d->input_buf[gtd->input_tab], "", "", "");
- }
- tab_found = cursor_tab_add(gtd->input_tab, TRUE) || cursor_auto_tab_add(gtd->input_tab, TRUE);
- cursor_show_completion(gtd->input_tab, tab_found);
- }
- DO_CURSOR(cursor_mixed_tab_backward)
- {
- struct listroot *root = ses->list[LIST_COMMAND];
- if (!root->list[0])
- {
- gtd->input_tab = cursor_calc_input_now();
- }
- if (gtd->input_tab == -1)
- {
- return;
- }
- cursor_hide_completion(gtd->input_tab);
- if (root->used)
- {
- delete_index_list(root, root->used - 1);
- }
- if (!root->list[0])
- {
- insert_node_list(root, >d->input_buf[gtd->input_tab], "", "", "");
- cursor_tab_add(gtd->input_tab, FALSE);
- cursor_auto_tab_add(gtd->input_tab, FALSE);
- }
- cursor_show_completion(gtd->input_tab, TRUE);
- }
- DO_CURSOR(cursor_screen_focus_in)
- {
- gtd->screen->focus = 1;
- check_all_events(gtd->ses, SUB_ARG, 1, 1, "SCREEN FOCUS", ntos(gtd->screen->focus));
- msdp_update_all("SCREEN_FOCUS", "%d", gtd->screen->focus);
- }
- DO_CURSOR(cursor_screen_focus_out)
- {
- gtd->screen->focus = 0;
- check_all_events(gtd->ses, SUB_ARG, 0, 1, "SCREEN FOCUS", ntos(gtd->screen->focus));
- msdp_update_all("SCREEN_FOCUS", "%d", gtd->screen->focus);
- }
|