text.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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 Igor van den Hoven 2004 *
  24. ******************************************************************************/
  25. #include "tintin.h"
  26. void print_line(struct session *ses, char **str, int prompt)
  27. {
  28. char *out;
  29. push_call("print_line(%p,%p,%d)",ses,*str,prompt);
  30. if (ses->scroll->line != -1 && HAS_BIT(ses->flags, SES_FLAG_SCROLLLOCK))
  31. {
  32. pop_call();
  33. return;
  34. }
  35. if (HAS_BIT(ses->flags, SES_FLAG_SCAN) && gtd->verbose_level == 0)
  36. {
  37. pop_call();
  38. return;
  39. }
  40. out = str_alloc(100 + strlen(*str) * 2 + 2);
  41. if (HAS_BIT(ses->flags, SES_FLAG_CONVERTMETA))
  42. {
  43. convert_meta(*str, out, TRUE);
  44. str_cpy(str, out);
  45. }
  46. if (HAS_BIT(ses->flags, SES_FLAG_WORDWRAP))
  47. {
  48. word_wrap(ses, *str, out, TRUE);
  49. }
  50. else
  51. {
  52. strcpy(out, *str);
  53. }
  54. if (prompt)
  55. {
  56. printf("%s", out);
  57. }
  58. else
  59. {
  60. printf("%s\n", out);
  61. }
  62. add_line_screen(out);
  63. str_free(out);
  64. pop_call();
  65. return;
  66. }
  67. /*
  68. Word wrapper, only wraps scrolling region
  69. */
  70. int word_wrap(struct session *ses, char *textin, char *textout, int display)
  71. {
  72. char *pti, *pto, *lis, *los, *chi, *cho;
  73. int width, size, skip = 0, cnt = 0;
  74. push_call("word_wrap(%s,%p,%p)",ses->name, textin,textout);
  75. pti = chi = lis = textin;
  76. pto = cho = los = textout;
  77. ses->cur_col = 1;
  78. while (*pti != 0)
  79. {
  80. if (skip_vt102_codes(pti))
  81. {
  82. if (display)
  83. {
  84. interpret_vt102_codes(ses, pti, TRUE);
  85. }
  86. for (skip = skip_vt102_codes(pti) ; skip > 0 ; skip--)
  87. {
  88. *pto++ = *pti++;
  89. }
  90. continue;
  91. }
  92. if (*pti == '\n')
  93. {
  94. *pto++ = *pti++;
  95. cnt = cnt + 1;
  96. los = pto;
  97. lis = pti;
  98. ses->cur_col = 1;
  99. continue;
  100. }
  101. if (*pti == ' ')
  102. {
  103. los = pto;
  104. lis = pti;
  105. }
  106. if (ses->cur_col > gtd->screen->cols)
  107. {
  108. cnt++;
  109. ses->cur_col = 1;
  110. if (HAS_BIT(ses->flags, SES_FLAG_WORDWRAP))
  111. {
  112. if (pto - los > 15 || !SCROLL(ses))
  113. {
  114. *pto++ = '\n';
  115. los = pto;
  116. lis = pti;
  117. }
  118. else if (lis != chi) // infinite VT loop detection
  119. {
  120. pto = los;
  121. *pto++ = '\n';
  122. pti = chi = lis;
  123. pti++;
  124. }
  125. else if (los != cho)
  126. {
  127. pto = cho = los;
  128. pto++;
  129. pti = chi = lis;
  130. pti++;
  131. }
  132. }
  133. }
  134. else
  135. {
  136. if (HAS_BIT(ses->flags, SES_FLAG_BIG5) && *pti & 128 && pti[1] != 0)
  137. {
  138. *pto++ = *pti++;
  139. *pto++ = *pti++;
  140. ses->cur_col++;
  141. }
  142. else if (HAS_BIT(ses->flags, SES_FLAG_UTF8) && is_utf8_head(pti))
  143. {
  144. size = get_utf8_width(pti, &width);
  145. while (size--)
  146. {
  147. *pto++ = *pti++;
  148. }
  149. ses->cur_col += width;
  150. }
  151. else
  152. {
  153. *pto++ = *pti++;
  154. ses->cur_col++;
  155. }
  156. }
  157. }
  158. *pto = 0;
  159. pop_call();
  160. return (cnt + 1);
  161. }
  162. // store whatever falls inbetween skip and keep. Used by #buffer
  163. int word_wrap_split(struct session *ses, char *textin, char *textout, int display, int start, int end)
  164. {
  165. char *pti, *pto, *lis, *los, *chi, *cho, *ptb;
  166. int width, size, i = 0, cnt = 0;
  167. push_call("word_wrap_split(%s,%p,%p,%d,%d)",ses->name, textin,textout, start, end);
  168. pti = chi = lis = textin;
  169. pto = cho = los = textout;
  170. ses->cur_col = 1;
  171. while (*pti != 0)
  172. {
  173. if (skip_vt102_codes(pti))
  174. {
  175. // if (cnt >= start && cnt < end)
  176. {
  177. for (i = skip_vt102_codes(pti) ; i > 0 ; i--)
  178. {
  179. *pto++ = *pti++;
  180. }
  181. }
  182. /* else
  183. {
  184. pti += skip_vt102_codes(pti);
  185. }*/
  186. continue;
  187. }
  188. if (*pti == '\n')
  189. {
  190. if (cnt++ >= start && cnt < end)
  191. {
  192. *pto++ = *pti++;
  193. }
  194. else
  195. {
  196. pti++;
  197. }
  198. los = pto;
  199. lis = pti;
  200. ses->cur_col = 1;
  201. continue;
  202. }
  203. if (*pti == ' ')
  204. {
  205. los = pto;
  206. lis = pti;
  207. }
  208. if (ses->cur_col > gtd->screen->cols)
  209. {
  210. cnt++;
  211. ses->cur_col = 1;
  212. if (HAS_BIT(ses->flags, SES_FLAG_WORDWRAP))
  213. {
  214. if (pto - los > 15 || !SCROLL(ses))
  215. {
  216. if (cnt >= start && cnt < end)
  217. {
  218. *pto++ = '\n';
  219. }
  220. los = pto;
  221. lis = pti;
  222. }
  223. else if (lis != chi) // infinite VT loop detection
  224. {
  225. if (cnt >= start && cnt < end)
  226. {
  227. pto = los;
  228. *pto++ = '\n';
  229. }
  230. pti = chi = lis;
  231. pti++;
  232. }
  233. else if (los != cho)
  234. {
  235. if (cnt >= start && cnt < end)
  236. {
  237. pto = cho = los;
  238. pto++;
  239. }
  240. else
  241. {
  242. cho = los;
  243. }
  244. pti = chi = lis;
  245. pti++;
  246. }
  247. }
  248. }
  249. else
  250. {
  251. ptb = pto;
  252. if (HAS_BIT(ses->flags, SES_FLAG_BIG5) && is_big5(pti))
  253. {
  254. *pto++ = *pti++;
  255. *pto++ = *pti++;
  256. ses->cur_col++;
  257. }
  258. else if (HAS_BIT(ses->flags, SES_FLAG_UTF8) && is_utf8_head(pti))
  259. {
  260. size = get_utf8_width(pti, &width);
  261. while (size--)
  262. {
  263. *pto++ = *pti++;
  264. }
  265. ses->cur_col += width;
  266. }
  267. else
  268. {
  269. *pto++ = *pti++;
  270. ses->cur_col++;
  271. }
  272. if (cnt < start || cnt >= end)
  273. {
  274. pto = ptb;
  275. }
  276. }
  277. }
  278. *pto = 0;
  279. pop_call();
  280. return (cnt + 1);
  281. }