split.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /******************************************************************************
  2. * This file is part of TinTin++ *
  3. * *
  4. * Copyright 2004-2019 Igor van den Hoven *
  5. * *
  6. * TinTin++ is free software; you can redistribute it and/or modify *
  7. * it under the terms of the GNU General Public License as published by *
  8. * the Free Software Foundation; either version 3 of the License, or *
  9. * (at your option) any later version. *
  10. * *
  11. * This program is distributed in the hope that it will be useful, *
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  14. * GNU General Public License for more details. *
  15. * *
  16. * *
  17. * You should have received a copy of the GNU General Public License *
  18. * along with TinTin++. If not, see https://www.gnu.org/licenses. *
  19. ******************************************************************************/
  20. /******************************************************************************
  21. * (T)he K(I)cki(N) (T)ickin D(I)kumud Clie(N)t *
  22. * *
  23. * coded by Bill Reiss 1993 *
  24. * recoded by Igor van den Hoven 2004 *
  25. ******************************************************************************/
  26. #include "tintin.h"
  27. DO_COMMAND(do_split)
  28. {
  29. int input;
  30. arg = sub_arg_in_braces(ses, arg, arg1, GET_ONE, SUB_VAR|SUB_FUN);
  31. arg = sub_arg_in_braces(ses, arg, arg2, GET_ONE, SUB_VAR|SUB_FUN);
  32. if ((*arg1 && !is_math(ses, arg1)) || (*arg2 && !is_math(ses, arg2)))
  33. {
  34. show_error(ses, LIST_COMMAND, "#SYNTAX: #SPLIT [TOP BAR] [BOTTOM BAR] [LEFT BAR] [RIGHT BAR] [INPUT BAR]");
  35. return ses;
  36. }
  37. ses->split->sav_top_row = *arg1 ? get_number(ses, arg1) : 0;
  38. ses->split->sav_bot_row = *arg2 ? get_number(ses, arg2) : 1;
  39. if (*arg == 0)
  40. {
  41. ses->split->sav_top_col = 0;
  42. ses->split->sav_bot_col = 0;
  43. }
  44. else
  45. {
  46. arg = sub_arg_in_braces(ses, arg, arg1, GET_ONE, SUB_VAR|SUB_FUN);
  47. arg = sub_arg_in_braces(ses, arg, arg2, GET_ONE, SUB_VAR|SUB_FUN);
  48. if ((*arg1 && !is_math(ses, arg1)) || (*arg2 && !is_math(ses, arg2)))
  49. {
  50. show_error(ses, LIST_COMMAND, "#SYNTAX: #SPLIT {TOP BAR} {BOT BAR} {LEFT BAR} {RIGHT BAR}");
  51. return ses;
  52. }
  53. ses->split->sav_top_col = *arg1 ? get_number(ses, arg1) : 1;
  54. ses->split->sav_bot_col = *arg2 ? get_number(ses, arg2) : 0;
  55. }
  56. if (*arg)
  57. {
  58. arg = sub_arg_in_braces(ses, arg, arg1, GET_ONE, SUB_VAR|SUB_FUN);
  59. input = get_number(ses, arg1);
  60. input *= -1;
  61. }
  62. else
  63. {
  64. input = -1;
  65. }
  66. init_input(ses, input, 1, -1, -1);
  67. ses->split->sav_bot_row += inputline_rows(ses) - 1;
  68. DEL_BIT(ses->flags, SES_FLAG_SCROLLSPLIT);
  69. SET_BIT(ses->flags, SES_FLAG_SPLIT);
  70. ses->input->str_off = 1;
  71. init_split(ses, ses->split->sav_top_row, ses->split->sav_top_col, ses->split->sav_bot_row, ses->split->sav_bot_col);
  72. return ses;
  73. }
  74. DO_COMMAND(do_unsplit)
  75. {
  76. memset(ses->split, 0, sizeof(struct split_data));
  77. ses->wrap = gtd->screen->cols;
  78. reset_screen(ses);
  79. SET_BIT(ses->scroll->flags, SCROLL_FLAG_RESIZE);
  80. if (HAS_BIT(ses->flags, SES_FLAG_SPLIT))
  81. {
  82. if (HAS_BIT(ses->telopts, TELOPT_FLAG_NAWS))
  83. {
  84. client_send_sb_naws(ses, 0, NULL);
  85. }
  86. DEL_BIT(ses->flags, SES_FLAG_SPLIT);
  87. DEL_BIT(ses->flags, SES_FLAG_SCROLLSPLIT);
  88. }
  89. check_all_events(ses, EVENT_FLAG_SCREEN, 0, 4, "SCREEN UNSPLIT", ntos(ses->split->top_row), ntos(ses->split->top_col), ntos(ses->split->bot_row), ntos(ses->split->bot_col));
  90. return ses;
  91. }
  92. void init_split(struct session *ses, int top_row, int top_col, int bot_row, int bot_col)
  93. {
  94. push_call("init_split(%p,%d,%d,%d,%d)",ses,top_row,top_col,bot_row,bot_col);
  95. SET_BIT(ses->scroll->flags, SCROLL_FLAG_RESIZE);
  96. init_input(ses, ses->input->sav_top_row, ses->input->sav_top_col, ses->input->sav_bot_row, ses->input->sav_bot_col);
  97. if (!HAS_BIT(ses->flags, SES_FLAG_SPLIT))
  98. {
  99. ses->split->top_row = 1;
  100. ses->split->top_col = 1;
  101. ses->split->bot_row = gtd->screen->rows;
  102. ses->split->bot_col = gtd->screen->cols;
  103. ses->wrap = gtd->screen->cols;
  104. init_pos(ses, gtd->screen->rows, 1);
  105. if (ses->map && HAS_BIT(ses->map->flags, MAP_FLAG_VTMAP))
  106. {
  107. SET_BIT(ses->flags, SES_FLAG_UPDATEVTMAP);
  108. }
  109. pop_call();
  110. return;
  111. }
  112. if (HAS_BIT(ses->flags, SES_FLAG_SCROLLSPLIT))
  113. {
  114. ses->split->top_row = top_row > 0 ? top_row : top_row < 0 ? gtd->screen->rows + top_row + 1 : 1;
  115. ses->split->top_col = top_col > 0 ? top_col : top_col < 0 ? gtd->screen->cols + top_col + 1 : 1;
  116. ses->split->bot_row = bot_row > 0 ? bot_row : bot_row < 0 ? gtd->screen->rows + bot_row + 1 : gtd->screen->rows - 2;
  117. ses->split->bot_col = bot_col > 0 ? bot_col : bot_col < 0 ? gtd->screen->cols + bot_col + 1 : gtd->screen->cols;
  118. }
  119. else
  120. {
  121. ses->split->top_row = top_row > 0 ? top_row + 1 : top_row < 0 ? gtd->screen->rows + top_row + 1 : 1;
  122. ses->split->top_col = top_col > 0 ? top_col + 1 : top_col < 0 ? gtd->screen->cols + top_col + 1 : 1;
  123. ses->split->bot_row = bot_row > 0 ? gtd->screen->rows - bot_row - 1 : bot_row < 0 ? bot_row * -1 : gtd->screen->rows - 1;
  124. ses->split->bot_col = bot_col > 0 ? gtd->screen->cols - bot_col : bot_col < 0 ? bot_col * -1 : gtd->screen->cols;
  125. }
  126. ses->split->top_row = URANGE(1, ses->split->top_row, gtd->screen->rows -3);
  127. ses->split->top_col = URANGE(1, ses->split->top_col, gtd->screen->cols - 2);
  128. ses->split->bot_row = URANGE(ses->split->top_row + 1, ses->split->bot_row, gtd->screen->rows - 1);
  129. ses->split->bot_col = URANGE(ses->split->top_col + 1, ses->split->bot_col, gtd->screen->cols);
  130. ses->split->sav_top_row = ses->split->top_row - 1;
  131. ses->split->sav_top_col = ses->split->top_col - 1;
  132. ses->split->sav_bot_row = gtd->screen->rows - ses->split->bot_row - 1;
  133. ses->split->sav_bot_col = gtd->screen->cols - ses->split->bot_col;
  134. ses->wrap = ses->split->bot_col - (ses->split->top_col - 1);
  135. scroll_region(ses, ses->split->top_row, ses->split->bot_row);
  136. init_pos(ses, ses->input->top_row, ses->input->top_col);
  137. if (HAS_BIT(ses->telopts, TELOPT_FLAG_NAWS))
  138. {
  139. SET_BIT(ses->telopts, TELOPT_FLAG_UPDATENAWS);
  140. }
  141. if (ses->map && HAS_BIT(ses->map->flags, MAP_FLAG_VTMAP))
  142. {
  143. SET_BIT(ses->flags, SES_FLAG_UPDATEVTMAP);
  144. }
  145. check_all_events(ses, EVENT_FLAG_SCREEN, 0, 4, "SCREEN SPLIT FILL", ntos(ses->split->top_row), ntos(ses->split->top_col), ntos(ses->split->bot_row), ntos(ses->split->bot_col), ntos(ses->split->sav_top_row), ntos(ses->split->sav_bot_row), ntos(ses->split->sav_top_col), ntos(ses->split->sav_bot_col));
  146. if (!check_all_events(ses, EVENT_FLAG_CATCH, 0, 8, "CATCH SCREEN SPLIT FILL", ntos(ses->split->top_row), ntos(ses->split->top_col), ntos(ses->split->bot_row), ntos(ses->split->bot_col), ntos(ses->split->sav_top_row), ntos(ses->split->sav_bot_row), ntos(ses->split->sav_top_col), ntos(ses->split->sav_bot_col)))
  147. {
  148. if (ses == gtd->ses && !HAS_BIT(ses->flags, SES_FLAG_SCROLLSPLIT))
  149. {
  150. if (HAS_BIT(ses->config_flags, CONFIG_FLAG_VERBOSE) || gtd->level->verbose || gtd->level->quiet == 0)
  151. {
  152. command(ses, do_screen, "FILL DEFAULT");
  153. }
  154. }
  155. }
  156. check_all_events(ses, EVENT_FLAG_SCREEN, 0, 4, "SCREEN SPLIT", ntos(ses->split->top_row), ntos(ses->split->top_col), ntos(ses->split->bot_row), ntos(ses->split->bot_col));
  157. pop_call();
  158. return;
  159. }
  160. /*
  161. unsplit
  162. */
  163. void reset_screen(struct session *ses)
  164. {
  165. reset_scroll_region(ses);
  166. init_pos(ses, gtd->screen->rows, 1);
  167. }
  168. /*
  169. refresh
  170. */
  171. void dirty_screen(struct session *ses)
  172. {
  173. push_call("dirty_screen(%p)",ses);
  174. refresh_session_terminal(ses);
  175. print_stdout(0, 0, "\e=");
  176. if (HAS_BIT(ses->flags, SES_FLAG_SPLIT))
  177. {
  178. init_split(ses, ses->split->sav_top_row, ses->split->sav_top_col, ses->split->sav_bot_row, ses->split->sav_bot_col);
  179. init_input(ses, ses->input->sav_top_row, ses->input->sav_top_col, ses->input->sav_bot_row, ses->input->sav_bot_col);
  180. }
  181. else if (IS_SPLIT(ses))
  182. {
  183. scroll_region(ses, ses->split->top_row, ses->split->bot_row);
  184. }
  185. else
  186. {
  187. reset_screen(ses);
  188. }
  189. if (IS_SPLIT(ses) && ses == gtd->ses)
  190. {
  191. init_pos(ses, ses->input->top_row, ses->input->top_col);
  192. }
  193. pop_call();
  194. return;
  195. }
  196. void split_show(struct session *ses, char *prompt, char *row_str, char *col_str)
  197. {
  198. char buf1[BUFFER_SIZE];
  199. int row, col, len, width, clear;
  200. row = 0;
  201. if (*row_str)
  202. {
  203. row = get_row_index_arg(ses, row_str);
  204. }
  205. if (row == 0)
  206. {
  207. row = URANGE(1, ses->input->top_row - 1, gtd->screen->rows);
  208. }
  209. col = 0;
  210. if (*col_str)
  211. {
  212. col = get_col_index_arg(ses, col_str);
  213. }
  214. if (col == 0)
  215. {
  216. col = 1;
  217. clear = 1;
  218. }
  219. else
  220. {
  221. clear = 0;
  222. }
  223. if (row < 1 || row > gtd->screen->rows)
  224. {
  225. show_error(ses, LIST_PROMPT, "#ERROR: PROMPT ROW IS OUTSIDE THE SCREEN: {%s} {%s} {%s} [%d].", prompt, row_str, col_str, row);
  226. return;
  227. }
  228. if (col < 0 || col > gtd->screen->cols)
  229. {
  230. show_error(ses, LIST_PROMPT, "#ERROR: PROMPT COLUMN IS OUTSIDE THE SCREEN: {%s} {%s} {%s} [%d].", prompt, row_str, col_str, col);
  231. return;
  232. }
  233. if (row != gtd->screen->rows && inside_scroll_region(ses, row, col))
  234. {
  235. show_error(ses, LIST_PROMPT, "#ERROR: PROMPT ROW IS INSIDE THE SCROLLING REGION: {%s} {%s} [%d].", prompt, row_str, row);
  236. return;
  237. }
  238. if (ses != gtd->ses)
  239. {
  240. return;
  241. }
  242. len = strip_vt102_width(ses, prompt, &width);
  243. if (col - 1 + width <= gtd->screen->cols)
  244. {
  245. sprintf(buf1, "%s", prompt);
  246. }
  247. else
  248. {
  249. show_debug(ses, LIST_PROMPT, "#DEBUG PROMPT {%s}", prompt);
  250. show_debug(ses, LIST_PROMPT, "#PROMPT WIDTH %d WITH OFFSET %d LONGER THAN ROW SIZE %d.", width, col, gtd->screen->cols);
  251. sprintf(buf1, "#PROMPT WIDTH %d WITH OFFSET %d LONGER THAN ROW SIZE %d.", len, col, gtd->screen->cols);
  252. }
  253. save_pos(ses);
  254. if (row == gtd->screen->rows)
  255. {
  256. gtd->ses->input->str_off = width + 1;
  257. goto_pos(ses, row, col);
  258. print_stdout(0, 0, "%s%s", buf1, gtd->ses->input->buf);
  259. // bit of a hack
  260. gtd->screen->sav_col[0] = inputline_cur_col();
  261. gtd->screen->sav_row[0] = inputline_cur_row();
  262. }
  263. else
  264. {
  265. goto_pos(ses, row, col);
  266. if (clear)
  267. {
  268. erase_cols(gtd->screen->cols);
  269. }
  270. print_stdout(0, 0, "%s", buf1);
  271. }
  272. set_line_screen(ses, buf1, row, col);
  273. restore_pos(ses);
  274. }