window-copy.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326
  1. /* $OpenBSD$ */
  2. /*
  3. * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
  14. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
  15. * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #include <sys/types.h>
  18. #include <ctype.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include "tmux.h"
  22. #include "tmate.h"
  23. struct screen *window_copy_init(struct window_pane *);
  24. void window_copy_free(struct window_pane *);
  25. void window_copy_pagedown(struct window_pane *);
  26. void window_copy_resize(struct window_pane *, u_int, u_int);
  27. void window_copy_key(struct window_pane *, struct client *, struct session *,
  28. key_code, struct mouse_event *);
  29. int window_copy_key_input(struct window_pane *, key_code);
  30. int window_copy_key_numeric_prefix(struct window_pane *, key_code);
  31. void window_copy_redraw_selection(struct window_pane *, u_int);
  32. void window_copy_redraw_lines(struct window_pane *, u_int, u_int);
  33. void window_copy_redraw_screen(struct window_pane *);
  34. void window_copy_write_line(struct window_pane *, struct screen_write_ctx *,
  35. u_int);
  36. void window_copy_write_lines(struct window_pane *,
  37. struct screen_write_ctx *, u_int, u_int);
  38. void window_copy_scroll_to(struct window_pane *, u_int, u_int);
  39. int window_copy_search_compare(struct grid *, u_int, u_int, struct grid *,
  40. u_int, int);
  41. int window_copy_search_lr(struct grid *, struct grid *, u_int *, u_int,
  42. u_int, u_int, int);
  43. int window_copy_search_rl(struct grid *, struct grid *, u_int *, u_int,
  44. u_int, u_int, int);
  45. void window_copy_search_up(struct window_pane *, const char *);
  46. void window_copy_search_down(struct window_pane *, const char *);
  47. void window_copy_goto_line(struct window_pane *, const char *);
  48. void window_copy_update_cursor(struct window_pane *, u_int, u_int);
  49. void window_copy_start_selection(struct window_pane *);
  50. int window_copy_update_selection(struct window_pane *, int);
  51. void *window_copy_get_selection(struct window_pane *, size_t *);
  52. void window_copy_copy_buffer(struct window_pane *, const char *, void *,
  53. size_t);
  54. void window_copy_copy_pipe(struct window_pane *, struct session *,
  55. const char *, const char *);
  56. void window_copy_copy_selection(struct window_pane *, const char *);
  57. void window_copy_append_selection(struct window_pane *, const char *);
  58. void window_copy_clear_selection(struct window_pane *);
  59. void window_copy_copy_line(struct window_pane *, char **, size_t *, u_int,
  60. u_int, u_int);
  61. int window_copy_in_set(struct window_pane *, u_int, u_int, const char *);
  62. u_int window_copy_find_length(struct window_pane *, u_int);
  63. void window_copy_cursor_start_of_line(struct window_pane *);
  64. void window_copy_cursor_back_to_indentation(struct window_pane *);
  65. void window_copy_cursor_end_of_line(struct window_pane *);
  66. void window_copy_other_end(struct window_pane *);
  67. void window_copy_cursor_left(struct window_pane *);
  68. void window_copy_cursor_right(struct window_pane *);
  69. void window_copy_cursor_up(struct window_pane *, int);
  70. void window_copy_cursor_down(struct window_pane *, int);
  71. void window_copy_cursor_jump(struct window_pane *);
  72. void window_copy_cursor_jump_back(struct window_pane *);
  73. void window_copy_cursor_jump_to(struct window_pane *, int);
  74. void window_copy_cursor_jump_to_back(struct window_pane *, int);
  75. void window_copy_cursor_next_word(struct window_pane *, const char *);
  76. void window_copy_cursor_next_word_end(struct window_pane *, const char *);
  77. void window_copy_cursor_previous_word(struct window_pane *, const char *);
  78. void window_copy_scroll_up(struct window_pane *, u_int);
  79. void window_copy_scroll_down(struct window_pane *, u_int);
  80. void window_copy_rectangle_toggle(struct window_pane *);
  81. void window_copy_drag_update(struct client *, struct mouse_event *);
  82. void window_copy_drag_release(struct client *, struct mouse_event *);
  83. const struct window_mode window_copy_mode = {
  84. window_copy_init,
  85. window_copy_free,
  86. window_copy_resize,
  87. window_copy_key,
  88. };
  89. #include "window-copy.h"
  90. struct screen *
  91. window_copy_init(struct window_pane *wp)
  92. {
  93. struct window_copy_mode_data *data;
  94. struct screen *s;
  95. int keys;
  96. wp->modedata = data = xmalloc(sizeof *data);
  97. data->oy = 0;
  98. data->cx = 0;
  99. data->cy = 0;
  100. data->lastcx = 0;
  101. data->lastsx = 0;
  102. data->backing_written = 0;
  103. data->rectflag = 0;
  104. data->scroll_exit = 0;
  105. data->inputtype = WINDOW_COPY_OFF;
  106. data->inputprompt = NULL;
  107. data->inputstr = xstrdup("");
  108. data->numprefix = -1;
  109. data->searchtype = WINDOW_COPY_OFF;
  110. data->searchstr = NULL;
  111. if (wp->fd != -1)
  112. bufferevent_disable(wp->event, EV_READ|EV_WRITE);
  113. data->jumptype = WINDOW_COPY_OFF;
  114. data->jumpchar = '\0';
  115. s = &data->screen;
  116. screen_init(s, screen_size_x(&wp->base), screen_size_y(&wp->base), 0);
  117. keys = options_get_number(wp->window->options, "mode-keys");
  118. if (keys == MODEKEY_EMACS)
  119. mode_key_init(&data->mdata, &mode_key_tree_emacs_copy);
  120. else
  121. mode_key_init(&data->mdata, &mode_key_tree_vi_copy);
  122. s->sel.modekeys = keys;
  123. data->backing = NULL;
  124. #ifdef TMATE
  125. data->password_cb = NULL;
  126. #endif
  127. return (s);
  128. }
  129. void
  130. window_copy_init_from_pane(struct window_pane *wp, int scroll_exit)
  131. {
  132. struct window_copy_mode_data *data = wp->modedata;
  133. struct screen *s = &data->screen;
  134. struct screen_write_ctx ctx;
  135. u_int i;
  136. if (wp->mode != &window_copy_mode)
  137. fatalx("not in copy mode");
  138. data->backing = &wp->base;
  139. data->cx = data->backing->cx;
  140. data->cy = data->backing->cy;
  141. data->scroll_exit = scroll_exit;
  142. s->cx = data->cx;
  143. s->cy = data->cy;
  144. screen_write_start(&ctx, NULL, s);
  145. for (i = 0; i < screen_size_y(s); i++)
  146. window_copy_write_line(wp, &ctx, i);
  147. screen_write_cursormove(&ctx, data->cx, data->cy);
  148. screen_write_stop(&ctx);
  149. #ifdef TMATE
  150. tmate_sync_copy_mode(wp);
  151. #endif
  152. }
  153. void
  154. window_copy_init_for_output(struct window_pane *wp)
  155. {
  156. struct window_copy_mode_data *data = wp->modedata;
  157. data->backing = xmalloc(sizeof *data->backing);
  158. screen_init(data->backing, screen_size_x(&wp->base),
  159. screen_size_y(&wp->base), UINT_MAX);
  160. }
  161. void
  162. window_copy_free(struct window_pane *wp)
  163. {
  164. struct window_copy_mode_data *data = wp->modedata;
  165. if (wp->fd != -1)
  166. bufferevent_enable(wp->event, EV_READ|EV_WRITE);
  167. free(data->searchstr);
  168. free(data->inputstr);
  169. if (data->backing != &wp->base) {
  170. screen_free(data->backing);
  171. free(data->backing);
  172. }
  173. screen_free(&data->screen);
  174. free(data);
  175. }
  176. void
  177. window_copy_add(struct window_pane *wp, const char *fmt, ...)
  178. {
  179. va_list ap;
  180. va_start(ap, fmt);
  181. window_copy_vadd(wp, fmt, ap);
  182. va_end(ap);
  183. }
  184. void
  185. window_copy_vadd(struct window_pane *wp, const char *fmt, va_list ap)
  186. {
  187. struct window_copy_mode_data *data = wp->modedata;
  188. struct screen *backing = data->backing;
  189. struct screen_write_ctx back_ctx, ctx;
  190. struct grid_cell gc;
  191. u_int old_hsize, old_cy;
  192. #ifdef TMATE
  193. char *msg;
  194. #endif
  195. if (backing == &wp->base)
  196. return;
  197. memcpy(&gc, &grid_default_cell, sizeof gc);
  198. old_hsize = screen_hsize(data->backing);
  199. screen_write_start(&back_ctx, NULL, backing);
  200. if (data->backing_written) {
  201. /*
  202. * On the second or later line, do a CRLF before writing
  203. * (so it's on a new line).
  204. */
  205. screen_write_carriagereturn(&back_ctx);
  206. screen_write_linefeed(&back_ctx, 0);
  207. } else
  208. data->backing_written = 1;
  209. old_cy = backing->cy;
  210. #ifdef TMATE
  211. xvasprintf(&msg, fmt, ap);
  212. screen_write_nputs(&back_ctx, 0, &gc, "%s", msg);
  213. tmate_write_copy_mode(wp, msg);
  214. free(msg);
  215. #else
  216. screen_write_vnputs(&back_ctx, 0, &gc, fmt, ap);
  217. #endif
  218. screen_write_stop(&back_ctx);
  219. data->oy += screen_hsize(data->backing) - old_hsize;
  220. screen_write_start(&ctx, wp, &data->screen);
  221. /*
  222. * If the history has changed, draw the top line.
  223. * (If there's any history at all, it has changed.)
  224. */
  225. if (screen_hsize(data->backing))
  226. window_copy_redraw_lines(wp, 0, 1);
  227. /* Write the new lines. */
  228. window_copy_redraw_lines(wp, old_cy, backing->cy - old_cy + 1);
  229. screen_write_stop(&ctx);
  230. }
  231. void
  232. window_copy_pageup(struct window_pane *wp)
  233. {
  234. struct window_copy_mode_data *data = wp->modedata;
  235. struct screen *s = &data->screen;
  236. u_int n, ox, oy;
  237. oy = screen_hsize(data->backing) + data->cy - data->oy;
  238. ox = window_copy_find_length(wp, oy);
  239. if (s->sel.lineflag == LINE_SEL_LEFT_RIGHT && oy == data->sely)
  240. window_copy_other_end(wp);
  241. if (data->cx != ox) {
  242. data->lastcx = data->cx;
  243. data->lastsx = ox;
  244. }
  245. data->cx = data->lastcx;
  246. n = 1;
  247. if (screen_size_y(s) > 2)
  248. n = screen_size_y(s) - 2;
  249. if (data->oy + n > screen_hsize(data->backing))
  250. data->oy = screen_hsize(data->backing);
  251. else
  252. data->oy += n;
  253. if (!data->screen.sel.flag || !data->rectflag) {
  254. u_int py = screen_hsize(data->backing) + data->cy - data->oy;
  255. u_int px = window_copy_find_length(wp, py);
  256. if ((data->cx >= data->lastsx && data->cx != px) || data->cx > px)
  257. window_copy_cursor_end_of_line(wp);
  258. }
  259. window_copy_update_selection(wp, 1);
  260. window_copy_redraw_screen(wp);
  261. }
  262. void
  263. window_copy_pagedown(struct window_pane *wp)
  264. {
  265. struct window_copy_mode_data *data = wp->modedata;
  266. struct screen *s = &data->screen;
  267. u_int n, ox, oy;
  268. oy = screen_hsize(data->backing) + data->cy - data->oy;
  269. ox = window_copy_find_length(wp, oy);
  270. if (s->sel.lineflag == LINE_SEL_RIGHT_LEFT && oy == data->sely)
  271. window_copy_other_end(wp);
  272. if (data->cx != ox) {
  273. data->lastcx = data->cx;
  274. data->lastsx = ox;
  275. }
  276. data->cx = data->lastcx;
  277. n = 1;
  278. if (screen_size_y(s) > 2)
  279. n = screen_size_y(s) - 2;
  280. if (data->oy < n)
  281. data->oy = 0;
  282. else
  283. data->oy -= n;
  284. if (!data->screen.sel.flag || !data->rectflag) {
  285. u_int py = screen_hsize(data->backing) + data->cy - data->oy;
  286. u_int px = window_copy_find_length(wp, py);
  287. if ((data->cx >= data->lastsx && data->cx != px) || data->cx > px)
  288. window_copy_cursor_end_of_line(wp);
  289. }
  290. if (data->scroll_exit && data->oy == 0) {
  291. window_pane_reset_mode(wp);
  292. return;
  293. }
  294. window_copy_update_selection(wp, 1);
  295. window_copy_redraw_screen(wp);
  296. }
  297. void
  298. window_copy_resize(struct window_pane *wp, u_int sx, u_int sy)
  299. {
  300. struct window_copy_mode_data *data = wp->modedata;
  301. struct screen *s = &data->screen;
  302. struct screen_write_ctx ctx;
  303. screen_resize(s, sx, sy, 1);
  304. if (data->backing != &wp->base)
  305. screen_resize(data->backing, sx, sy, 1);
  306. if (data->cy > sy - 1)
  307. data->cy = sy - 1;
  308. if (data->cx > sx)
  309. data->cx = sx;
  310. if (data->oy > screen_hsize(data->backing))
  311. data->oy = screen_hsize(data->backing);
  312. window_copy_clear_selection(wp);
  313. screen_write_start(&ctx, NULL, s);
  314. window_copy_write_lines(wp, &ctx, 0, screen_size_y(s) - 1);
  315. screen_write_stop(&ctx);
  316. window_copy_redraw_screen(wp);
  317. }
  318. static void
  319. __window_copy_key(struct window_pane *wp, struct client *c, struct session *sess,
  320. key_code key, struct mouse_event *m)
  321. {
  322. const char *word_separators;
  323. struct window_copy_mode_data *data = wp->modedata;
  324. struct screen *s = &data->screen;
  325. u_int n, np;
  326. int keys;
  327. enum mode_key_cmd cmd;
  328. const char *arg, *ss;
  329. np = 1;
  330. if (data->numprefix > 0)
  331. np = data->numprefix;
  332. if (data->inputtype == WINDOW_COPY_JUMPFORWARD ||
  333. data->inputtype == WINDOW_COPY_JUMPBACK ||
  334. data->inputtype == WINDOW_COPY_JUMPTOFORWARD ||
  335. data->inputtype == WINDOW_COPY_JUMPTOBACK) {
  336. /* Ignore keys with modifiers. */
  337. if ((key & KEYC_MASK_MOD) == 0) {
  338. data->jumpchar = key;
  339. if (data->inputtype == WINDOW_COPY_JUMPFORWARD) {
  340. for (; np != 0; np--)
  341. window_copy_cursor_jump(wp);
  342. }
  343. if (data->inputtype == WINDOW_COPY_JUMPBACK) {
  344. for (; np != 0; np--)
  345. window_copy_cursor_jump_back(wp);
  346. }
  347. if (data->inputtype == WINDOW_COPY_JUMPTOFORWARD) {
  348. for (; np != 0; np--)
  349. window_copy_cursor_jump_to(wp, 0);
  350. }
  351. if (data->inputtype == WINDOW_COPY_JUMPTOBACK) {
  352. for (; np != 0; np--)
  353. window_copy_cursor_jump_to_back(wp, 0);
  354. }
  355. }
  356. data->jumptype = data->inputtype;
  357. data->inputtype = WINDOW_COPY_OFF;
  358. window_copy_redraw_lines(wp, screen_size_y(s) - 1, 1);
  359. return;
  360. } else if (data->inputtype == WINDOW_COPY_NUMERICPREFIX) {
  361. if (window_copy_key_numeric_prefix(wp, key) == 0)
  362. return;
  363. data->inputtype = WINDOW_COPY_OFF;
  364. window_copy_redraw_lines(wp, screen_size_y(s) - 1, 1);
  365. } else if (data->inputtype != WINDOW_COPY_OFF) {
  366. if (window_copy_key_input(wp, key) != 0)
  367. goto input_off;
  368. return;
  369. }
  370. cmd = mode_key_lookup(&data->mdata, key, &arg);
  371. if (cmd != MODEKEYCOPY_PREVIOUSPAGE &&
  372. cmd != MODEKEYCOPY_NEXTPAGE &&
  373. cmd != MODEKEYCOPY_SCROLLUP &&
  374. cmd != MODEKEYCOPY_SCROLLDOWN &&
  375. cmd != MODEKEYCOPY_HALFPAGEUP &&
  376. cmd != MODEKEYCOPY_HALFPAGEDOWN)
  377. data->scroll_exit = 0;
  378. switch (cmd) {
  379. case MODEKEYCOPY_APPENDSELECTION:
  380. if (sess != NULL) {
  381. window_copy_append_selection(wp, NULL);
  382. if (arg == NULL) {
  383. window_pane_reset_mode(wp);
  384. return;
  385. }
  386. window_copy_clear_selection(wp);
  387. window_copy_redraw_screen(wp);
  388. }
  389. break;
  390. case MODEKEYCOPY_CANCEL:
  391. window_pane_reset_mode(wp);
  392. return;
  393. case MODEKEYCOPY_OTHEREND:
  394. if (np % 2)
  395. window_copy_other_end(wp);
  396. break;
  397. case MODEKEYCOPY_LEFT:
  398. for (; np != 0; np--)
  399. window_copy_cursor_left(wp);
  400. break;
  401. case MODEKEYCOPY_RIGHT:
  402. for (; np != 0; np--)
  403. window_copy_cursor_right(wp);
  404. break;
  405. case MODEKEYCOPY_UP:
  406. for (; np != 0; np--)
  407. window_copy_cursor_up(wp, 0);
  408. break;
  409. case MODEKEYCOPY_DOWN:
  410. for (; np != 0; np--)
  411. window_copy_cursor_down(wp, 0);
  412. break;
  413. case MODEKEYCOPY_SCROLLUP:
  414. for (; np != 0; np--)
  415. window_copy_cursor_up(wp, 1);
  416. break;
  417. case MODEKEYCOPY_SCROLLDOWN:
  418. for (; np != 0; np--)
  419. window_copy_cursor_down(wp, 1);
  420. if (data->scroll_exit && data->oy == 0) {
  421. window_pane_reset_mode(wp);
  422. return;
  423. }
  424. break;
  425. case MODEKEYCOPY_PREVIOUSPAGE:
  426. for (; np != 0; np--)
  427. window_copy_pageup(wp);
  428. break;
  429. case MODEKEYCOPY_NEXTPAGE:
  430. for (; np != 0; np--)
  431. window_copy_pagedown(wp);
  432. break;
  433. case MODEKEYCOPY_HALFPAGEUP:
  434. n = screen_size_y(s) / 2;
  435. for (; np != 0; np--) {
  436. if (data->oy + n > screen_hsize(data->backing))
  437. data->oy = screen_hsize(data->backing);
  438. else
  439. data->oy += n;
  440. }
  441. window_copy_update_selection(wp, 1);
  442. window_copy_redraw_screen(wp);
  443. break;
  444. case MODEKEYCOPY_HALFPAGEDOWN:
  445. n = screen_size_y(s) / 2;
  446. for (; np != 0; np--) {
  447. if (data->oy < n)
  448. data->oy = 0;
  449. else
  450. data->oy -= n;
  451. }
  452. if (data->scroll_exit && data->oy == 0) {
  453. window_pane_reset_mode(wp);
  454. return;
  455. }
  456. window_copy_update_selection(wp, 1);
  457. window_copy_redraw_screen(wp);
  458. break;
  459. case MODEKEYCOPY_TOPLINE:
  460. data->cx = 0;
  461. data->cy = 0;
  462. window_copy_update_selection(wp, 1);
  463. window_copy_redraw_screen(wp);
  464. break;
  465. case MODEKEYCOPY_MIDDLELINE:
  466. data->cx = 0;
  467. data->cy = (screen_size_y(s) - 1) / 2;
  468. window_copy_update_selection(wp, 1);
  469. window_copy_redraw_screen(wp);
  470. break;
  471. case MODEKEYCOPY_BOTTOMLINE:
  472. data->cx = 0;
  473. data->cy = screen_size_y(s) - 1;
  474. window_copy_update_selection(wp, 1);
  475. window_copy_redraw_screen(wp);
  476. break;
  477. case MODEKEYCOPY_HISTORYTOP:
  478. data->cx = 0;
  479. data->cy = 0;
  480. data->oy = screen_hsize(data->backing);
  481. window_copy_update_selection(wp, 1);
  482. window_copy_redraw_screen(wp);
  483. break;
  484. case MODEKEYCOPY_HISTORYBOTTOM:
  485. data->cx = 0;
  486. data->cy = screen_size_y(s) - 1;
  487. data->oy = 0;
  488. window_copy_update_selection(wp, 1);
  489. window_copy_redraw_screen(wp);
  490. break;
  491. case MODEKEYCOPY_STARTSELECTION:
  492. if (KEYC_IS_MOUSE(key)) {
  493. if (c != NULL)
  494. window_copy_start_drag(c, m);
  495. } else {
  496. s->sel.lineflag = LINE_SEL_NONE;
  497. window_copy_start_selection(wp);
  498. window_copy_redraw_screen(wp);
  499. }
  500. break;
  501. case MODEKEYCOPY_SELECTLINE:
  502. s->sel.lineflag = LINE_SEL_LEFT_RIGHT;
  503. data->rectflag = 0;
  504. /* FALLTHROUGH */
  505. case MODEKEYCOPY_COPYLINE:
  506. window_copy_cursor_start_of_line(wp);
  507. /* FALLTHROUGH */
  508. case MODEKEYCOPY_COPYENDOFLINE:
  509. window_copy_start_selection(wp);
  510. for (; np > 1; np--)
  511. window_copy_cursor_down(wp, 0);
  512. window_copy_cursor_end_of_line(wp);
  513. window_copy_redraw_screen(wp);
  514. /* If a copy command then copy the selection and exit. */
  515. if (sess != NULL &&
  516. (cmd == MODEKEYCOPY_COPYLINE ||
  517. cmd == MODEKEYCOPY_COPYENDOFLINE)) {
  518. window_copy_copy_selection(wp, NULL);
  519. window_pane_reset_mode(wp);
  520. return;
  521. }
  522. break;
  523. case MODEKEYCOPY_CLEARSELECTION:
  524. window_copy_clear_selection(wp);
  525. window_copy_redraw_screen(wp);
  526. break;
  527. case MODEKEYCOPY_COPYPIPE:
  528. if (sess != NULL) {
  529. window_copy_copy_pipe(wp, sess, NULL, arg);
  530. window_pane_reset_mode(wp);
  531. return;
  532. }
  533. break;
  534. case MODEKEYCOPY_COPYSELECTION:
  535. if (sess != NULL) {
  536. window_copy_copy_selection(wp, NULL);
  537. if (arg == NULL) {
  538. window_pane_reset_mode(wp);
  539. return;
  540. }
  541. window_copy_clear_selection(wp);
  542. window_copy_redraw_screen(wp);
  543. }
  544. break;
  545. case MODEKEYCOPY_STARTOFLINE:
  546. window_copy_cursor_start_of_line(wp);
  547. break;
  548. case MODEKEYCOPY_BACKTOINDENTATION:
  549. window_copy_cursor_back_to_indentation(wp);
  550. break;
  551. case MODEKEYCOPY_ENDOFLINE:
  552. window_copy_cursor_end_of_line(wp);
  553. break;
  554. case MODEKEYCOPY_NEXTSPACE:
  555. for (; np != 0; np--)
  556. window_copy_cursor_next_word(wp, " ");
  557. break;
  558. case MODEKEYCOPY_NEXTSPACEEND:
  559. for (; np != 0; np--)
  560. window_copy_cursor_next_word_end(wp, " ");
  561. break;
  562. case MODEKEYCOPY_NEXTWORD:
  563. word_separators =
  564. options_get_string(sess->options, "word-separators");
  565. for (; np != 0; np--)
  566. window_copy_cursor_next_word(wp, word_separators);
  567. break;
  568. case MODEKEYCOPY_NEXTWORDEND:
  569. word_separators =
  570. options_get_string(sess->options, "word-separators");
  571. for (; np != 0; np--)
  572. window_copy_cursor_next_word_end(wp, word_separators);
  573. break;
  574. case MODEKEYCOPY_PREVIOUSSPACE:
  575. for (; np != 0; np--)
  576. window_copy_cursor_previous_word(wp, " ");
  577. break;
  578. case MODEKEYCOPY_PREVIOUSWORD:
  579. word_separators =
  580. options_get_string(sess->options, "word-separators");
  581. for (; np != 0; np--)
  582. window_copy_cursor_previous_word(wp, word_separators);
  583. break;
  584. case MODEKEYCOPY_JUMP:
  585. data->inputtype = WINDOW_COPY_JUMPFORWARD;
  586. data->inputprompt = "Jump Forward";
  587. *data->inputstr = '\0';
  588. window_copy_redraw_lines(wp, screen_size_y(s) - 1, 1);
  589. return; /* skip numprefix reset */
  590. case MODEKEYCOPY_JUMPAGAIN:
  591. if (data->jumptype == WINDOW_COPY_JUMPFORWARD) {
  592. for (; np != 0; np--)
  593. window_copy_cursor_jump(wp);
  594. } else if (data->jumptype == WINDOW_COPY_JUMPBACK) {
  595. for (; np != 0; np--)
  596. window_copy_cursor_jump_back(wp);
  597. } else if (data->jumptype == WINDOW_COPY_JUMPTOFORWARD) {
  598. for (; np != 0; np--)
  599. window_copy_cursor_jump_to(wp, 1);
  600. } else if (data->jumptype == WINDOW_COPY_JUMPTOBACK) {
  601. for (; np != 0; np--)
  602. window_copy_cursor_jump_to_back(wp, 1);
  603. }
  604. break;
  605. case MODEKEYCOPY_JUMPREVERSE:
  606. if (data->jumptype == WINDOW_COPY_JUMPFORWARD) {
  607. for (; np != 0; np--)
  608. window_copy_cursor_jump_back(wp);
  609. } else if (data->jumptype == WINDOW_COPY_JUMPBACK) {
  610. for (; np != 0; np--)
  611. window_copy_cursor_jump(wp);
  612. } else if (data->jumptype == WINDOW_COPY_JUMPTOFORWARD) {
  613. for (; np != 0; np--)
  614. window_copy_cursor_jump_to_back(wp, 1);
  615. } else if (data->jumptype == WINDOW_COPY_JUMPTOBACK) {
  616. for (; np != 0; np--)
  617. window_copy_cursor_jump_to(wp, 1);
  618. }
  619. break;
  620. case MODEKEYCOPY_JUMPBACK:
  621. data->inputtype = WINDOW_COPY_JUMPBACK;
  622. data->inputprompt = "Jump Back";
  623. *data->inputstr = '\0';
  624. window_copy_redraw_lines(wp, screen_size_y(s) - 1, 1);
  625. return; /* skip numprefix reset */
  626. case MODEKEYCOPY_JUMPTO:
  627. data->inputtype = WINDOW_COPY_JUMPTOFORWARD;
  628. data->inputprompt = "Jump To";
  629. *data->inputstr = '\0';
  630. window_copy_redraw_lines(wp, screen_size_y(s) - 1, 1);
  631. return; /* skip numprefix reset */
  632. case MODEKEYCOPY_JUMPTOBACK:
  633. data->inputtype = WINDOW_COPY_JUMPTOBACK;
  634. data->inputprompt = "Jump To Back";
  635. *data->inputstr = '\0';
  636. window_copy_redraw_lines(wp, screen_size_y(s) - 1, 1);
  637. return; /* skip numprefix reset */
  638. case MODEKEYCOPY_SEARCHUP:
  639. data->inputtype = WINDOW_COPY_SEARCHUP;
  640. data->inputprompt = "Search Up";
  641. goto input_on;
  642. case MODEKEYCOPY_SEARCHDOWN:
  643. data->inputtype = WINDOW_COPY_SEARCHDOWN;
  644. data->inputprompt = "Search Down";
  645. goto input_on;
  646. case MODEKEYCOPY_SEARCHAGAIN:
  647. case MODEKEYCOPY_SEARCHREVERSE:
  648. switch (data->searchtype) {
  649. #ifdef TMATE
  650. case WINDOW_COPY_PASSWORD:
  651. break;
  652. #endif
  653. case WINDOW_COPY_OFF:
  654. case WINDOW_COPY_GOTOLINE:
  655. case WINDOW_COPY_JUMPFORWARD:
  656. case WINDOW_COPY_JUMPBACK:
  657. case WINDOW_COPY_JUMPTOFORWARD:
  658. case WINDOW_COPY_JUMPTOBACK:
  659. case WINDOW_COPY_NAMEDBUFFER:
  660. case WINDOW_COPY_NUMERICPREFIX:
  661. case WINDOW_COPY_SEARCHUP:
  662. ss = data->searchstr;
  663. if (cmd == MODEKEYCOPY_SEARCHAGAIN) {
  664. for (; np != 0; np--)
  665. window_copy_search_up(wp, ss);
  666. } else {
  667. for (; np != 0; np--)
  668. window_copy_search_down(wp, ss);
  669. }
  670. break;
  671. case WINDOW_COPY_SEARCHDOWN:
  672. ss = data->searchstr;
  673. if (cmd == MODEKEYCOPY_SEARCHAGAIN) {
  674. for (; np != 0; np--)
  675. window_copy_search_down(wp, ss);
  676. } else {
  677. for (; np != 0; np--)
  678. window_copy_search_up(wp, ss);
  679. }
  680. break;
  681. }
  682. break;
  683. case MODEKEYCOPY_GOTOLINE:
  684. data->inputtype = WINDOW_COPY_GOTOLINE;
  685. data->inputprompt = "Goto Line";
  686. *data->inputstr = '\0';
  687. goto input_on;
  688. case MODEKEYCOPY_STARTNAMEDBUFFER:
  689. data->inputtype = WINDOW_COPY_NAMEDBUFFER;
  690. data->inputexit = (arg == NULL);
  691. data->inputprompt = "Buffer";
  692. *data->inputstr = '\0';
  693. goto input_on;
  694. case MODEKEYCOPY_STARTNUMBERPREFIX:
  695. key &= KEYC_MASK_KEY;
  696. if (key >= '0' && key <= '9') {
  697. data->inputtype = WINDOW_COPY_NUMERICPREFIX;
  698. data->numprefix = 0;
  699. window_copy_key_numeric_prefix(wp, key);
  700. return;
  701. }
  702. break;
  703. case MODEKEYCOPY_RECTANGLETOGGLE:
  704. s->sel.lineflag = LINE_SEL_NONE;
  705. window_copy_rectangle_toggle(wp);
  706. break;
  707. default:
  708. break;
  709. }
  710. data->numprefix = -1;
  711. return;
  712. input_on:
  713. keys = options_get_number(wp->window->options, "mode-keys");
  714. if (keys == MODEKEY_EMACS)
  715. mode_key_init(&data->mdata, &mode_key_tree_emacs_edit);
  716. else
  717. mode_key_init(&data->mdata, &mode_key_tree_vi_edit);
  718. window_copy_redraw_lines(wp, screen_size_y(s) - 1, 1);
  719. return;
  720. input_off:
  721. keys = options_get_number(wp->window->options, "mode-keys");
  722. if (keys == MODEKEY_EMACS)
  723. mode_key_init(&data->mdata, &mode_key_tree_emacs_copy);
  724. else
  725. mode_key_init(&data->mdata, &mode_key_tree_vi_copy);
  726. data->inputtype = WINDOW_COPY_OFF;
  727. data->inputprompt = NULL;
  728. window_copy_redraw_lines(wp, screen_size_y(s) - 1, 1);
  729. }
  730. void
  731. window_copy_key(struct window_pane *wp, struct client *c, struct session *sess,
  732. key_code key, struct mouse_event *m)
  733. {
  734. __window_copy_key(wp, c, sess, key, m);
  735. #ifdef TMATE
  736. tmate_sync_copy_mode(wp);
  737. #endif
  738. }
  739. int
  740. window_copy_key_input(struct window_pane *wp, key_code key)
  741. {
  742. struct window_copy_mode_data *data = wp->modedata;
  743. struct screen *s = &data->screen;
  744. const char *bufdata;
  745. size_t inputlen, n, bufsize;
  746. int np;
  747. struct paste_buffer *pb;
  748. u_char ch;
  749. switch (mode_key_lookup(&data->mdata, key, NULL)) {
  750. case MODEKEYEDIT_CANCEL:
  751. data->numprefix = -1;
  752. return (-1);
  753. case MODEKEYEDIT_BACKSPACE:
  754. inputlen = strlen(data->inputstr);
  755. if (inputlen > 0)
  756. data->inputstr[inputlen - 1] = '\0';
  757. break;
  758. case MODEKEYEDIT_DELETELINE:
  759. *data->inputstr = '\0';
  760. break;
  761. case MODEKEYEDIT_PASTE:
  762. if ((pb = paste_get_top(NULL)) == NULL)
  763. break;
  764. bufdata = paste_buffer_data(pb, &bufsize);
  765. for (n = 0; n < bufsize; n++) {
  766. ch = (u_char)bufdata[n];
  767. if (ch < 32 || ch == 127)
  768. break;
  769. }
  770. inputlen = strlen(data->inputstr);
  771. data->inputstr = xrealloc(data->inputstr, inputlen + n + 1);
  772. memcpy(data->inputstr + inputlen, bufdata, n);
  773. data->inputstr[inputlen + n] = '\0';
  774. break;
  775. case MODEKEYEDIT_ENTER:
  776. np = data->numprefix;
  777. if (np <= 0)
  778. np = 1;
  779. switch (data->inputtype) {
  780. case WINDOW_COPY_OFF:
  781. case WINDOW_COPY_JUMPFORWARD:
  782. case WINDOW_COPY_JUMPBACK:
  783. case WINDOW_COPY_JUMPTOFORWARD:
  784. case WINDOW_COPY_JUMPTOBACK:
  785. case WINDOW_COPY_NUMERICPREFIX:
  786. break;
  787. case WINDOW_COPY_SEARCHUP:
  788. for (; np != 0; np--)
  789. window_copy_search_up(wp, data->inputstr);
  790. data->searchtype = data->inputtype;
  791. data->searchstr = xstrdup(data->inputstr);
  792. break;
  793. case WINDOW_COPY_SEARCHDOWN:
  794. for (; np != 0; np--)
  795. window_copy_search_down(wp, data->inputstr);
  796. data->searchtype = data->inputtype;
  797. data->searchstr = xstrdup(data->inputstr);
  798. break;
  799. case WINDOW_COPY_NAMEDBUFFER:
  800. window_copy_copy_selection(wp, data->inputstr);
  801. *data->inputstr = '\0';
  802. if (data->inputexit) {
  803. window_pane_reset_mode(wp);
  804. return (0);
  805. }
  806. window_copy_clear_selection(wp);
  807. window_copy_redraw_screen(wp);
  808. break;
  809. case WINDOW_COPY_GOTOLINE:
  810. window_copy_goto_line(wp, data->inputstr);
  811. *data->inputstr = '\0';
  812. break;
  813. #ifdef TMATE
  814. case WINDOW_COPY_PASSWORD:
  815. if (data->password_cb) {
  816. data->password_cb(data->inputstr,
  817. data->password_cb_private);
  818. }
  819. window_pane_reset_mode(wp);
  820. return 0;
  821. #endif
  822. }
  823. data->numprefix = -1;
  824. return (1);
  825. case MODEKEY_OTHER:
  826. if (key < 32 || key > 126)
  827. break;
  828. inputlen = strlen(data->inputstr) + 2;
  829. data->inputstr = xrealloc(data->inputstr, inputlen);
  830. data->inputstr[inputlen - 2] = key;
  831. data->inputstr[inputlen - 1] = '\0';
  832. break;
  833. default:
  834. break;
  835. }
  836. window_copy_redraw_lines(wp, screen_size_y(s) - 1, 1);
  837. return (0);
  838. }
  839. int
  840. window_copy_key_numeric_prefix(struct window_pane *wp, key_code key)
  841. {
  842. struct window_copy_mode_data *data = wp->modedata;
  843. struct screen *s = &data->screen;
  844. key &= KEYC_MASK_KEY;
  845. if (key < '0' || key > '9')
  846. return (1);
  847. if (data->numprefix >= 100) /* no more than three digits */
  848. return (0);
  849. data->numprefix = data->numprefix * 10 + key - '0';
  850. window_copy_redraw_lines(wp, screen_size_y(s) - 1, 1);
  851. return (0);
  852. }
  853. void
  854. window_copy_scroll_to(struct window_pane *wp, u_int px, u_int py)
  855. {
  856. struct window_copy_mode_data *data = wp->modedata;
  857. struct grid *gd = data->backing->grid;
  858. u_int offset, gap;
  859. data->cx = px;
  860. gap = gd->sy / 4;
  861. if (py < gd->sy) {
  862. offset = 0;
  863. data->cy = py;
  864. } else if (py > gd->hsize + gd->sy - gap) {
  865. offset = gd->hsize;
  866. data->cy = py - gd->hsize;
  867. } else {
  868. offset = py + gap - gd->sy;
  869. data->cy = py - offset;
  870. }
  871. data->oy = gd->hsize - offset;
  872. window_copy_update_selection(wp, 1);
  873. window_copy_redraw_screen(wp);
  874. }
  875. int
  876. window_copy_search_compare(struct grid *gd, u_int px, u_int py,
  877. struct grid *sgd, u_int spx, int cis)
  878. {
  879. struct grid_cell gc, sgc;
  880. const struct utf8_data *ud, *sud;
  881. grid_get_cell(gd, px, py, &gc);
  882. ud = &gc.data;
  883. grid_get_cell(sgd, spx, 0, &sgc);
  884. sud = &sgc.data;
  885. if (ud->size != sud->size || ud->width != sud->width)
  886. return (0);
  887. if (cis && ud->size == 1)
  888. return (tolower(ud->data[0]) == sud->data[0]);
  889. return (memcmp(ud->data, sud->data, ud->size) == 0);
  890. }
  891. int
  892. window_copy_search_lr(struct grid *gd,
  893. struct grid *sgd, u_int *ppx, u_int py, u_int first, u_int last, int cis)
  894. {
  895. u_int ax, bx, px;
  896. int matched;
  897. for (ax = first; ax < last; ax++) {
  898. if (ax + sgd->sx >= gd->sx)
  899. break;
  900. for (bx = 0; bx < sgd->sx; bx++) {
  901. px = ax + bx;
  902. matched = window_copy_search_compare(gd, px, py, sgd,
  903. bx, cis);
  904. if (!matched)
  905. break;
  906. }
  907. if (bx == sgd->sx) {
  908. *ppx = ax;
  909. return (1);
  910. }
  911. }
  912. return (0);
  913. }
  914. int
  915. window_copy_search_rl(struct grid *gd,
  916. struct grid *sgd, u_int *ppx, u_int py, u_int first, u_int last, int cis)
  917. {
  918. u_int ax, bx, px;
  919. int matched;
  920. for (ax = last + 1; ax > first; ax--) {
  921. if (gd->sx - (ax - 1) < sgd->sx)
  922. continue;
  923. for (bx = 0; bx < sgd->sx; bx++) {
  924. px = ax - 1 + bx;
  925. matched = window_copy_search_compare(gd, px, py, sgd,
  926. bx, cis);
  927. if (!matched)
  928. break;
  929. }
  930. if (bx == sgd->sx) {
  931. *ppx = ax - 1;
  932. return (1);
  933. }
  934. }
  935. return (0);
  936. }
  937. void
  938. window_copy_search_up(struct window_pane *wp, const char *searchstr)
  939. {
  940. struct window_copy_mode_data *data = wp->modedata;
  941. struct screen *s = data->backing, ss;
  942. struct screen_write_ctx ctx;
  943. struct grid *gd = s->grid, *sgd;
  944. struct grid_cell gc;
  945. size_t searchlen;
  946. u_int i, last, fx, fy, px;
  947. int n, wrapped, wrapflag, cis;
  948. const char *ptr;
  949. if (*searchstr == '\0')
  950. return;
  951. wrapflag = options_get_number(wp->window->options, "wrap-search");
  952. searchlen = screen_write_strlen("%s", searchstr);
  953. screen_init(&ss, searchlen, 1, 0);
  954. screen_write_start(&ctx, NULL, &ss);
  955. memcpy(&gc, &grid_default_cell, sizeof gc);
  956. screen_write_nputs(&ctx, -1, &gc, "%s", searchstr);
  957. screen_write_stop(&ctx);
  958. fx = data->cx;
  959. fy = gd->hsize - data->oy + data->cy;
  960. if (fx == 0) {
  961. if (fy == 0)
  962. return;
  963. fx = gd->sx - 1;
  964. fy--;
  965. } else
  966. fx--;
  967. n = wrapped = 0;
  968. cis = 1;
  969. for (ptr = searchstr; *ptr != '\0'; ptr++) {
  970. if (*ptr != tolower((u_char)*ptr)) {
  971. cis = 0;
  972. break;
  973. }
  974. }
  975. retry:
  976. sgd = ss.grid;
  977. for (i = fy + 1; i > 0; i--) {
  978. last = screen_size_x(s);
  979. if (i == fy + 1)
  980. last = fx;
  981. n = window_copy_search_rl(gd, sgd, &px, i - 1, 0, last, cis);
  982. if (n) {
  983. window_copy_scroll_to(wp, px, i - 1);
  984. break;
  985. }
  986. }
  987. if (wrapflag && !n && !wrapped) {
  988. fx = gd->sx - 1;
  989. fy = gd->hsize + gd->sy - 1;
  990. wrapped = 1;
  991. goto retry;
  992. }
  993. screen_free(&ss);
  994. }
  995. void
  996. window_copy_search_down(struct window_pane *wp, const char *searchstr)
  997. {
  998. struct window_copy_mode_data *data = wp->modedata;
  999. struct screen *s = data->backing, ss;
  1000. struct screen_write_ctx ctx;
  1001. struct grid *gd = s->grid, *sgd;
  1002. struct grid_cell gc;
  1003. size_t searchlen;
  1004. u_int i, first, fx, fy, px;
  1005. int n, wrapped, wrapflag, cis;
  1006. const char *ptr;
  1007. if (*searchstr == '\0')
  1008. return;
  1009. wrapflag = options_get_number(wp->window->options, "wrap-search");
  1010. searchlen = screen_write_strlen("%s", searchstr);
  1011. screen_init(&ss, searchlen, 1, 0);
  1012. screen_write_start(&ctx, NULL, &ss);
  1013. memcpy(&gc, &grid_default_cell, sizeof gc);
  1014. screen_write_nputs(&ctx, -1, &gc, "%s", searchstr);
  1015. screen_write_stop(&ctx);
  1016. fx = data->cx;
  1017. fy = gd->hsize - data->oy + data->cy;
  1018. if (fx == gd->sx - 1) {
  1019. if (fy == gd->hsize + gd->sy)
  1020. return;
  1021. fx = 0;
  1022. fy++;
  1023. } else
  1024. fx++;
  1025. n = wrapped = 0;
  1026. cis = 1;
  1027. for (ptr = searchstr; *ptr != '\0'; ptr++) {
  1028. if (*ptr != tolower((u_char)*ptr)) {
  1029. cis = 0;
  1030. break;
  1031. }
  1032. }
  1033. retry:
  1034. sgd = ss.grid;
  1035. for (i = fy + 1; i < gd->hsize + gd->sy + 1; i++) {
  1036. first = 0;
  1037. if (i == fy + 1)
  1038. first = fx;
  1039. n = window_copy_search_lr(gd, sgd, &px, i - 1, first, gd->sx,
  1040. cis);
  1041. if (n) {
  1042. window_copy_scroll_to(wp, px, i - 1);
  1043. break;
  1044. }
  1045. }
  1046. if (wrapflag && !n && !wrapped) {
  1047. fx = 0;
  1048. fy = 0;
  1049. wrapped = 1;
  1050. goto retry;
  1051. }
  1052. screen_free(&ss);
  1053. }
  1054. void
  1055. window_copy_goto_line(struct window_pane *wp, const char *linestr)
  1056. {
  1057. struct window_copy_mode_data *data = wp->modedata;
  1058. const char *errstr;
  1059. u_int lineno;
  1060. lineno = strtonum(linestr, 0, screen_hsize(data->backing), &errstr);
  1061. if (errstr != NULL)
  1062. return;
  1063. data->oy = lineno;
  1064. window_copy_update_selection(wp, 1);
  1065. window_copy_redraw_screen(wp);
  1066. }
  1067. void
  1068. window_copy_write_line(struct window_pane *wp, struct screen_write_ctx *ctx,
  1069. u_int py)
  1070. {
  1071. struct window_copy_mode_data *data = wp->modedata;
  1072. struct screen *s = &data->screen;
  1073. struct options *oo = wp->window->options;
  1074. struct grid_cell gc;
  1075. char hdr[512];
  1076. size_t last, xoff = 0, size = 0, limit;
  1077. style_apply(&gc, oo, "mode-style");
  1078. last = screen_size_y(s) - 1;
  1079. if (py == 0) {
  1080. #ifdef TMATE
  1081. if (data->inputtype != WINDOW_COPY_PASSWORD) {
  1082. #endif
  1083. size = xsnprintf(hdr, sizeof hdr,
  1084. "[%u/%u]", data->oy, screen_hsize(data->backing));
  1085. if (size > screen_size_x(s))
  1086. size = screen_size_x(s);
  1087. screen_write_cursormove(ctx, screen_size_x(s) - size, 0);
  1088. screen_write_puts(ctx, &gc, "%s", hdr);
  1089. #ifdef TMATE
  1090. }
  1091. #endif
  1092. } else if (py == last && data->inputtype != WINDOW_COPY_OFF) {
  1093. limit = sizeof hdr;
  1094. if (limit > screen_size_x(s) + 1)
  1095. limit = screen_size_x(s) + 1;
  1096. if (data->inputtype == WINDOW_COPY_NUMERICPREFIX) {
  1097. xoff = size = xsnprintf(hdr, limit,
  1098. "Repeat: %d", data->numprefix);
  1099. } else {
  1100. #ifdef TMATE
  1101. if (data->inputtype == WINDOW_COPY_PASSWORD) {
  1102. int password_len = strlen(data->inputstr);
  1103. xoff = size = xsnprintf(hdr, sizeof hdr, "%s: ", data->inputprompt);
  1104. memset(hdr+xoff, '*', password_len);
  1105. xoff += password_len;
  1106. size += password_len;
  1107. hdr[xoff] = '\0';
  1108. }
  1109. else
  1110. #endif
  1111. xoff = size = xsnprintf(hdr, limit,
  1112. "%s: %s", data->inputprompt, data->inputstr);
  1113. }
  1114. screen_write_cursormove(ctx, 0, last);
  1115. screen_write_puts(ctx, &gc, "%s", hdr);
  1116. } else
  1117. size = 0;
  1118. if (size < screen_size_x(s)) {
  1119. screen_write_cursormove(ctx, xoff, py);
  1120. screen_write_copy(ctx, data->backing, xoff,
  1121. (screen_hsize(data->backing) - data->oy) + py,
  1122. screen_size_x(s) - size, 1);
  1123. }
  1124. if (py == data->cy && data->cx == screen_size_x(s)) {
  1125. memcpy(&gc, &grid_default_cell, sizeof gc);
  1126. screen_write_cursormove(ctx, screen_size_x(s) - 1, py);
  1127. screen_write_putc(ctx, &gc, '$');
  1128. }
  1129. }
  1130. void
  1131. window_copy_write_lines(struct window_pane *wp, struct screen_write_ctx *ctx,
  1132. u_int py, u_int ny)
  1133. {
  1134. u_int yy;
  1135. for (yy = py; yy < py + ny; yy++)
  1136. window_copy_write_line(wp, ctx, py);
  1137. }
  1138. void
  1139. window_copy_redraw_selection(struct window_pane *wp, u_int old_y)
  1140. {
  1141. struct window_copy_mode_data *data = wp->modedata;
  1142. u_int new_y, start, end;
  1143. new_y = data->cy;
  1144. if (old_y <= new_y) {
  1145. start = old_y;
  1146. end = new_y;
  1147. } else {
  1148. start = new_y;
  1149. end = old_y;
  1150. }
  1151. window_copy_redraw_lines(wp, start, end - start + 1);
  1152. }
  1153. void
  1154. window_copy_redraw_lines(struct window_pane *wp, u_int py, u_int ny)
  1155. {
  1156. struct window_copy_mode_data *data = wp->modedata;
  1157. struct screen_write_ctx ctx;
  1158. u_int i;
  1159. screen_write_start(&ctx, wp, NULL);
  1160. for (i = py; i < py + ny; i++)
  1161. window_copy_write_line(wp, &ctx, i);
  1162. screen_write_cursormove(&ctx, data->cx, data->cy);
  1163. screen_write_stop(&ctx);
  1164. }
  1165. void
  1166. window_copy_redraw_screen(struct window_pane *wp)
  1167. {
  1168. struct window_copy_mode_data *data = wp->modedata;
  1169. window_copy_redraw_lines(wp, 0, screen_size_y(&data->screen));
  1170. }
  1171. void
  1172. window_copy_update_cursor(struct window_pane *wp, u_int cx, u_int cy)
  1173. {
  1174. struct window_copy_mode_data *data = wp->modedata;
  1175. struct screen *s = &data->screen;
  1176. struct screen_write_ctx ctx;
  1177. u_int old_cx, old_cy;
  1178. old_cx = data->cx; old_cy = data->cy;
  1179. data->cx = cx; data->cy = cy;
  1180. if (old_cx == screen_size_x(s))
  1181. window_copy_redraw_lines(wp, old_cy, 1);
  1182. if (data->cx == screen_size_x(s))
  1183. window_copy_redraw_lines(wp, data->cy, 1);
  1184. else {
  1185. screen_write_start(&ctx, wp, NULL);
  1186. screen_write_cursormove(&ctx, data->cx, data->cy);
  1187. screen_write_stop(&ctx);
  1188. }
  1189. }
  1190. void
  1191. window_copy_start_selection(struct window_pane *wp)
  1192. {
  1193. struct window_copy_mode_data *data = wp->modedata;
  1194. struct screen *s = &data->screen;
  1195. data->selx = data->cx;
  1196. data->sely = screen_hsize(data->backing) + data->cy - data->oy;
  1197. s->sel.flag = 1;
  1198. window_copy_update_selection(wp, 1);
  1199. }
  1200. int
  1201. window_copy_update_selection(struct window_pane *wp, int may_redraw)
  1202. {
  1203. struct window_copy_mode_data *data = wp->modedata;
  1204. struct screen *s = &data->screen;
  1205. struct options *oo = wp->window->options;
  1206. struct grid_cell gc;
  1207. u_int sx, sy, ty, cy;
  1208. if (!s->sel.flag && s->sel.lineflag == LINE_SEL_NONE)
  1209. return (0);
  1210. /* Set colours. */
  1211. style_apply(&gc, oo, "mode-style");
  1212. /* Find top of screen. */
  1213. ty = screen_hsize(data->backing) - data->oy;
  1214. /* Adjust the selection. */
  1215. sx = data->selx;
  1216. sy = data->sely;
  1217. if (sy < ty) { /* above screen */
  1218. if (!data->rectflag)
  1219. sx = 0;
  1220. sy = 0;
  1221. } else if (sy > ty + screen_size_y(s) - 1) { /* below screen */
  1222. if (!data->rectflag)
  1223. sx = screen_size_x(s) - 1;
  1224. sy = screen_size_y(s) - 1;
  1225. } else
  1226. sy -= ty;
  1227. sy = screen_hsize(s) + sy;
  1228. screen_set_selection(s,
  1229. sx, sy, data->cx, screen_hsize(s) + data->cy, data->rectflag, &gc);
  1230. if (data->rectflag && may_redraw) {
  1231. /*
  1232. * Can't rely on the caller to redraw the right lines for
  1233. * rectangle selection - find the highest line and the number
  1234. * of lines, and redraw just past that in both directions
  1235. */
  1236. cy = data->cy;
  1237. if (sy < cy)
  1238. window_copy_redraw_lines(wp, sy, cy - sy + 1);
  1239. else
  1240. window_copy_redraw_lines(wp, cy, sy - cy + 1);
  1241. }
  1242. return (1);
  1243. }
  1244. void *
  1245. window_copy_get_selection(struct window_pane *wp, size_t *len)
  1246. {
  1247. struct window_copy_mode_data *data = wp->modedata;
  1248. struct screen *s = &data->screen;
  1249. char *buf;
  1250. size_t off;
  1251. u_int i, xx, yy, sx, sy, ex, ey, ey_last;
  1252. u_int firstsx, lastex, restex, restsx;
  1253. int keys;
  1254. if (!s->sel.flag && s->sel.lineflag == LINE_SEL_NONE)
  1255. return (NULL);
  1256. buf = xmalloc(1);
  1257. off = 0;
  1258. *buf = '\0';
  1259. /*
  1260. * The selection extends from selx,sely to (adjusted) cx,cy on
  1261. * the base screen.
  1262. */
  1263. /* Find start and end. */
  1264. xx = data->cx;
  1265. yy = screen_hsize(data->backing) + data->cy - data->oy;
  1266. if (yy < data->sely || (yy == data->sely && xx < data->selx)) {
  1267. sx = xx; sy = yy;
  1268. ex = data->selx; ey = data->sely;
  1269. } else {
  1270. sx = data->selx; sy = data->sely;
  1271. ex = xx; ey = yy;
  1272. }
  1273. /* Trim ex to end of line. */
  1274. ey_last = window_copy_find_length(wp, ey);
  1275. if (ex > ey_last)
  1276. ex = ey_last;
  1277. /*
  1278. * Deal with rectangle-copy if necessary; four situations: start of
  1279. * first line (firstsx), end of last line (lastex), start (restsx) and
  1280. * end (restex) of all other lines.
  1281. */
  1282. xx = screen_size_x(s);
  1283. /*
  1284. * Behave according to mode-keys. If it is emacs, copy like emacs,
  1285. * keeping the top-left-most character, and dropping the
  1286. * bottom-right-most, regardless of copy direction. If it is vi, also
  1287. * keep bottom-right-most character.
  1288. */
  1289. keys = options_get_number(wp->window->options, "mode-keys");
  1290. if (data->rectflag) {
  1291. /*
  1292. * Need to ignore the column with the cursor in it, which for
  1293. * rectangular copy means knowing which side the cursor is on.
  1294. */
  1295. if (data->selx < data->cx) {
  1296. /* Selection start is on the left. */
  1297. if (keys == MODEKEY_EMACS) {
  1298. lastex = data->cx;
  1299. restex = data->cx;
  1300. }
  1301. else {
  1302. lastex = data->cx + 1;
  1303. restex = data->cx + 1;
  1304. }
  1305. firstsx = data->selx;
  1306. restsx = data->selx;
  1307. } else {
  1308. /* Cursor is on the left. */
  1309. lastex = data->selx + 1;
  1310. restex = data->selx + 1;
  1311. firstsx = data->cx;
  1312. restsx = data->cx;
  1313. }
  1314. } else {
  1315. if (keys == MODEKEY_EMACS)
  1316. lastex = ex;
  1317. else
  1318. lastex = ex + 1;
  1319. restex = xx;
  1320. firstsx = sx;
  1321. restsx = 0;
  1322. }
  1323. /* Copy the lines. */
  1324. for (i = sy; i <= ey; i++) {
  1325. window_copy_copy_line(wp, &buf, &off, i,
  1326. (i == sy ? firstsx : restsx),
  1327. (i == ey ? lastex : restex));
  1328. }
  1329. /* Don't bother if no data. */
  1330. if (off == 0) {
  1331. free(buf);
  1332. return (NULL);
  1333. }
  1334. if (keys == MODEKEY_EMACS || lastex <= ey_last)
  1335. off -= 1; /* remove final \n (unless at end in vi mode) */
  1336. *len = off;
  1337. return (buf);
  1338. }
  1339. void
  1340. window_copy_copy_buffer(struct window_pane *wp, const char *bufname, void *buf,
  1341. size_t len)
  1342. {
  1343. struct screen_write_ctx ctx;
  1344. if (options_get_number(global_options, "set-clipboard")) {
  1345. screen_write_start(&ctx, wp, NULL);
  1346. screen_write_setselection(&ctx, buf, len);
  1347. screen_write_stop(&ctx);
  1348. }
  1349. if (paste_set(buf, len, bufname, NULL) != 0)
  1350. free(buf);
  1351. }
  1352. void
  1353. window_copy_copy_pipe(struct window_pane *wp, struct session *sess,
  1354. const char *bufname, const char *arg)
  1355. {
  1356. void *buf;
  1357. size_t len;
  1358. struct job *job;
  1359. struct format_tree *ft;
  1360. char *expanded;
  1361. buf = window_copy_get_selection(wp, &len);
  1362. if (buf == NULL)
  1363. return;
  1364. ft = format_create(NULL, 0);
  1365. format_defaults(ft, NULL, sess, NULL, wp);
  1366. expanded = format_expand(ft, arg);
  1367. job = job_run(expanded, sess, NULL, NULL, NULL, NULL);
  1368. bufferevent_write(job->event, buf, len);
  1369. free(expanded);
  1370. format_free(ft);
  1371. window_copy_copy_buffer(wp, bufname, buf, len);
  1372. }
  1373. void
  1374. window_copy_copy_selection(struct window_pane *wp, const char *bufname)
  1375. {
  1376. void *buf;
  1377. size_t len;
  1378. buf = window_copy_get_selection(wp, &len);
  1379. if (buf == NULL)
  1380. return;
  1381. window_copy_copy_buffer(wp, bufname, buf, len);
  1382. }
  1383. void
  1384. window_copy_append_selection(struct window_pane *wp, const char *bufname)
  1385. {
  1386. char *buf;
  1387. struct paste_buffer *pb;
  1388. const char *bufdata;
  1389. size_t len, bufsize;
  1390. struct screen_write_ctx ctx;
  1391. buf = window_copy_get_selection(wp, &len);
  1392. if (buf == NULL)
  1393. return;
  1394. if (options_get_number(global_options, "set-clipboard")) {
  1395. screen_write_start(&ctx, wp, NULL);
  1396. screen_write_setselection(&ctx, buf, len);
  1397. screen_write_stop(&ctx);
  1398. }
  1399. if (bufname == NULL || *bufname == '\0')
  1400. pb = paste_get_top(&bufname);
  1401. else
  1402. pb = paste_get_name(bufname);
  1403. if (pb != NULL) {
  1404. bufdata = paste_buffer_data(pb, &bufsize);
  1405. buf = xrealloc(buf, len + bufsize);
  1406. memmove(buf + bufsize, buf, len);
  1407. memcpy(buf, bufdata, bufsize);
  1408. len += bufsize;
  1409. }
  1410. if (paste_set(buf, len, bufname, NULL) != 0)
  1411. free(buf);
  1412. }
  1413. void
  1414. window_copy_copy_line(struct window_pane *wp, char **buf, size_t *off, u_int sy,
  1415. u_int sx, u_int ex)
  1416. {
  1417. struct window_copy_mode_data *data = wp->modedata;
  1418. struct grid *gd = data->backing->grid;
  1419. struct grid_cell gc;
  1420. struct grid_line *gl;
  1421. struct utf8_data ud;
  1422. u_int i, xx, wrapped = 0;
  1423. const char *s;
  1424. if (sx > ex)
  1425. return;
  1426. /*
  1427. * Work out if the line was wrapped at the screen edge and all of it is
  1428. * on screen.
  1429. */
  1430. gl = &gd->linedata[sy];
  1431. if (gl->flags & GRID_LINE_WRAPPED && gl->cellsize <= gd->sx)
  1432. wrapped = 1;
  1433. /* If the line was wrapped, don't strip spaces (use the full length). */
  1434. if (wrapped)
  1435. xx = gl->cellsize;
  1436. else
  1437. xx = window_copy_find_length(wp, sy);
  1438. if (ex > xx)
  1439. ex = xx;
  1440. if (sx > xx)
  1441. sx = xx;
  1442. if (sx < ex) {
  1443. for (i = sx; i < ex; i++) {
  1444. grid_get_cell(gd, i, sy, &gc);
  1445. if (gc.flags & GRID_FLAG_PADDING)
  1446. continue;
  1447. utf8_copy(&ud, &gc.data);
  1448. if (ud.size == 1 && (gc.attr & GRID_ATTR_CHARSET)) {
  1449. s = tty_acs_get(NULL, ud.data[0]);
  1450. if (s != NULL && strlen(s) <= sizeof ud.data) {
  1451. ud.size = strlen(s);
  1452. memcpy(ud.data, s, ud.size);
  1453. }
  1454. }
  1455. *buf = xrealloc(*buf, (*off) + ud.size);
  1456. memcpy(*buf + *off, ud.data, ud.size);
  1457. *off += ud.size;
  1458. }
  1459. }
  1460. /* Only add a newline if the line wasn't wrapped. */
  1461. if (!wrapped || ex != xx) {
  1462. *buf = xrealloc(*buf, (*off) + 1);
  1463. (*buf)[(*off)++] = '\n';
  1464. }
  1465. }
  1466. void
  1467. window_copy_clear_selection(struct window_pane *wp)
  1468. {
  1469. struct window_copy_mode_data *data = wp->modedata;
  1470. u_int px, py;
  1471. screen_clear_selection(&data->screen);
  1472. py = screen_hsize(data->backing) + data->cy - data->oy;
  1473. px = window_copy_find_length(wp, py);
  1474. if (data->cx > px)
  1475. window_copy_update_cursor(wp, px, data->cy);
  1476. }
  1477. int
  1478. window_copy_in_set(struct window_pane *wp, u_int px, u_int py, const char *set)
  1479. {
  1480. struct window_copy_mode_data *data = wp->modedata;
  1481. struct grid_cell gc;
  1482. const struct utf8_data *ud;
  1483. grid_get_cell(data->backing->grid, px, py, &gc);
  1484. ud = &gc.data;
  1485. if (ud->size != 1 || (gc.flags & GRID_FLAG_PADDING))
  1486. return (0);
  1487. if (*ud->data == 0x00 || *ud->data == 0x7f)
  1488. return (0);
  1489. return (strchr(set, *ud->data) != NULL);
  1490. }
  1491. u_int
  1492. window_copy_find_length(struct window_pane *wp, u_int py)
  1493. {
  1494. struct window_copy_mode_data *data = wp->modedata;
  1495. struct screen *s = data->backing;
  1496. struct grid_cell gc;
  1497. u_int px;
  1498. /*
  1499. * If the pane has been resized, its grid can contain old overlong
  1500. * lines. grid_peek_cell does not allow accessing cells beyond the
  1501. * width of the grid, and screen_write_copy treats them as spaces, so
  1502. * ignore them here too.
  1503. */
  1504. px = s->grid->linedata[py].cellsize;
  1505. if (px > screen_size_x(s))
  1506. px = screen_size_x(s);
  1507. while (px > 0) {
  1508. grid_get_cell(s->grid, px - 1, py, &gc);
  1509. if (gc.data.size != 1 || *gc.data.data != ' ')
  1510. break;
  1511. px--;
  1512. }
  1513. return (px);
  1514. }
  1515. void
  1516. window_copy_cursor_start_of_line(struct window_pane *wp)
  1517. {
  1518. struct window_copy_mode_data *data = wp->modedata;
  1519. struct screen *back_s = data->backing;
  1520. struct screen *s = &data->screen;
  1521. struct grid *gd = back_s->grid;
  1522. u_int py;
  1523. if (data->cx == 0 && s->sel.lineflag == LINE_SEL_NONE) {
  1524. py = screen_hsize(back_s) + data->cy - data->oy;
  1525. while (py > 0 &&
  1526. gd->linedata[py-1].flags & GRID_LINE_WRAPPED) {
  1527. window_copy_cursor_up(wp, 0);
  1528. py = screen_hsize(back_s) + data->cy - data->oy;
  1529. }
  1530. }
  1531. window_copy_update_cursor(wp, 0, data->cy);
  1532. if (window_copy_update_selection(wp, 1))
  1533. window_copy_redraw_lines(wp, data->cy, 1);
  1534. }
  1535. void
  1536. window_copy_cursor_back_to_indentation(struct window_pane *wp)
  1537. {
  1538. struct window_copy_mode_data *data = wp->modedata;
  1539. u_int px, py, xx;
  1540. struct grid_cell gc;
  1541. px = 0;
  1542. py = screen_hsize(data->backing) + data->cy - data->oy;
  1543. xx = window_copy_find_length(wp, py);
  1544. while (px < xx) {
  1545. grid_get_cell(data->backing->grid, px, py, &gc);
  1546. if (gc.data.size != 1 || *gc.data.data != ' ')
  1547. break;
  1548. px++;
  1549. }
  1550. window_copy_update_cursor(wp, px, data->cy);
  1551. if (window_copy_update_selection(wp, 1))
  1552. window_copy_redraw_lines(wp, data->cy, 1);
  1553. }
  1554. void
  1555. window_copy_cursor_end_of_line(struct window_pane *wp)
  1556. {
  1557. struct window_copy_mode_data *data = wp->modedata;
  1558. struct screen *back_s = data->backing;
  1559. struct screen *s = &data->screen;
  1560. struct grid *gd = back_s->grid;
  1561. u_int px, py;
  1562. py = screen_hsize(back_s) + data->cy - data->oy;
  1563. px = window_copy_find_length(wp, py);
  1564. if (data->cx == px && s->sel.lineflag == LINE_SEL_NONE) {
  1565. if (data->screen.sel.flag && data->rectflag)
  1566. px = screen_size_x(back_s);
  1567. if (gd->linedata[py].flags & GRID_LINE_WRAPPED) {
  1568. while (py < gd->sy + gd->hsize &&
  1569. gd->linedata[py].flags & GRID_LINE_WRAPPED) {
  1570. window_copy_cursor_down(wp, 0);
  1571. py = screen_hsize(back_s)
  1572. + data->cy - data->oy;
  1573. }
  1574. px = window_copy_find_length(wp, py);
  1575. }
  1576. }
  1577. window_copy_update_cursor(wp, px, data->cy);
  1578. if (window_copy_update_selection(wp, 1))
  1579. window_copy_redraw_lines(wp, data->cy, 1);
  1580. }
  1581. void
  1582. window_copy_other_end(struct window_pane *wp)
  1583. {
  1584. struct window_copy_mode_data *data = wp->modedata;
  1585. struct screen *s = &data->screen;
  1586. u_int selx, sely, cx, cy, yy, hsize;
  1587. if (!s->sel.flag && s->sel.lineflag == LINE_SEL_NONE)
  1588. return;
  1589. if (s->sel.lineflag == LINE_SEL_LEFT_RIGHT)
  1590. s->sel.lineflag = LINE_SEL_RIGHT_LEFT;
  1591. else if (s->sel.lineflag == LINE_SEL_RIGHT_LEFT)
  1592. s->sel.lineflag = LINE_SEL_LEFT_RIGHT;
  1593. selx = data->selx;
  1594. sely = data->sely;
  1595. cx = data->cx;
  1596. cy = data->cy;
  1597. yy = screen_hsize(data->backing) + data->cy - data->oy;
  1598. data->selx = cx;
  1599. data->sely = yy;
  1600. data->cx = selx;
  1601. hsize = screen_hsize(data->backing);
  1602. if (sely < hsize - data->oy) {
  1603. data->oy = hsize - sely;
  1604. data->cy = 0;
  1605. } else if (sely > hsize - data->oy + screen_size_y(s)) {
  1606. data->oy = hsize - sely + screen_size_y(s) - 1;
  1607. data->cy = screen_size_y(s) - 1;
  1608. } else
  1609. data->cy = cy + sely - yy;
  1610. window_copy_redraw_screen(wp);
  1611. }
  1612. void
  1613. window_copy_cursor_left(struct window_pane *wp)
  1614. {
  1615. struct window_copy_mode_data *data = wp->modedata;
  1616. u_int py;
  1617. py = screen_hsize(data->backing) + data->cy - data->oy;
  1618. if (data->cx == 0 && py > 0) {
  1619. window_copy_cursor_up(wp, 0);
  1620. window_copy_cursor_end_of_line(wp);
  1621. } else if (data->cx > 0) {
  1622. window_copy_update_cursor(wp, data->cx - 1, data->cy);
  1623. if (window_copy_update_selection(wp, 1))
  1624. window_copy_redraw_lines(wp, data->cy, 1);
  1625. }
  1626. }
  1627. void
  1628. window_copy_cursor_right(struct window_pane *wp)
  1629. {
  1630. struct window_copy_mode_data *data = wp->modedata;
  1631. u_int px, py, yy;
  1632. py = screen_hsize(data->backing) + data->cy - data->oy;
  1633. yy = screen_hsize(data->backing) + screen_size_y(data->backing) - 1;
  1634. if (data->screen.sel.flag && data->rectflag)
  1635. px = screen_size_x(&data->screen);
  1636. else {
  1637. px = window_copy_find_length(wp, py);
  1638. }
  1639. if (data->cx >= px && py < yy) {
  1640. window_copy_cursor_start_of_line(wp);
  1641. window_copy_cursor_down(wp, 0);
  1642. } else if (data->cx < px) {
  1643. window_copy_update_cursor(wp, data->cx + 1, data->cy);
  1644. if (window_copy_update_selection(wp, 1))
  1645. window_copy_redraw_lines(wp, data->cy, 1);
  1646. }
  1647. }
  1648. void
  1649. window_copy_cursor_up(struct window_pane *wp, int scroll_only)
  1650. {
  1651. struct window_copy_mode_data *data = wp->modedata;
  1652. struct screen *s = &data->screen;
  1653. u_int ox, oy, px, py;
  1654. oy = screen_hsize(data->backing) + data->cy - data->oy;
  1655. ox = window_copy_find_length(wp, oy);
  1656. if (data->cx != ox) {
  1657. data->lastcx = data->cx;
  1658. data->lastsx = ox;
  1659. }
  1660. if (s->sel.lineflag == LINE_SEL_LEFT_RIGHT && oy == data->sely)
  1661. window_copy_other_end(wp);
  1662. data->cx = data->lastcx;
  1663. if (scroll_only || data->cy == 0) {
  1664. window_copy_scroll_down(wp, 1);
  1665. if (scroll_only) {
  1666. if (data->cy == screen_size_y(s) - 1)
  1667. window_copy_redraw_lines(wp, data->cy, 1);
  1668. else
  1669. window_copy_redraw_lines(wp, data->cy, 2);
  1670. }
  1671. } else {
  1672. window_copy_update_cursor(wp, data->cx, data->cy - 1);
  1673. if (window_copy_update_selection(wp, 1)) {
  1674. if (data->cy == screen_size_y(s) - 1)
  1675. window_copy_redraw_lines(wp, data->cy, 1);
  1676. else
  1677. window_copy_redraw_lines(wp, data->cy, 2);
  1678. }
  1679. }
  1680. if (!data->screen.sel.flag || !data->rectflag) {
  1681. py = screen_hsize(data->backing) + data->cy - data->oy;
  1682. px = window_copy_find_length(wp, py);
  1683. if ((data->cx >= data->lastsx && data->cx != px) ||
  1684. data->cx > px)
  1685. window_copy_cursor_end_of_line(wp);
  1686. }
  1687. if (s->sel.lineflag == LINE_SEL_LEFT_RIGHT)
  1688. window_copy_cursor_end_of_line(wp);
  1689. else if (s->sel.lineflag == LINE_SEL_RIGHT_LEFT)
  1690. window_copy_cursor_start_of_line(wp);
  1691. }
  1692. void
  1693. window_copy_cursor_down(struct window_pane *wp, int scroll_only)
  1694. {
  1695. struct window_copy_mode_data *data = wp->modedata;
  1696. struct screen *s = &data->screen;
  1697. u_int ox, oy, px, py;
  1698. oy = screen_hsize(data->backing) + data->cy - data->oy;
  1699. ox = window_copy_find_length(wp, oy);
  1700. if (data->cx != ox) {
  1701. data->lastcx = data->cx;
  1702. data->lastsx = ox;
  1703. }
  1704. if (s->sel.lineflag == LINE_SEL_RIGHT_LEFT && oy == data->sely)
  1705. window_copy_other_end(wp);
  1706. data->cx = data->lastcx;
  1707. if (scroll_only || data->cy == screen_size_y(s) - 1) {
  1708. window_copy_scroll_up(wp, 1);
  1709. if (scroll_only && data->cy > 0)
  1710. window_copy_redraw_lines(wp, data->cy - 1, 2);
  1711. } else {
  1712. window_copy_update_cursor(wp, data->cx, data->cy + 1);
  1713. if (window_copy_update_selection(wp, 1))
  1714. window_copy_redraw_lines(wp, data->cy - 1, 2);
  1715. }
  1716. if (!data->screen.sel.flag || !data->rectflag) {
  1717. py = screen_hsize(data->backing) + data->cy - data->oy;
  1718. px = window_copy_find_length(wp, py);
  1719. if ((data->cx >= data->lastsx && data->cx != px) ||
  1720. data->cx > px)
  1721. window_copy_cursor_end_of_line(wp);
  1722. }
  1723. if (s->sel.lineflag == LINE_SEL_LEFT_RIGHT)
  1724. window_copy_cursor_end_of_line(wp);
  1725. else if (s->sel.lineflag == LINE_SEL_RIGHT_LEFT)
  1726. window_copy_cursor_start_of_line(wp);
  1727. }
  1728. void
  1729. window_copy_cursor_jump(struct window_pane *wp)
  1730. {
  1731. struct window_copy_mode_data *data = wp->modedata;
  1732. struct screen *back_s = data->backing;
  1733. struct grid_cell gc;
  1734. u_int px, py, xx;
  1735. px = data->cx + 1;
  1736. py = screen_hsize(back_s) + data->cy - data->oy;
  1737. xx = window_copy_find_length(wp, py);
  1738. while (px < xx) {
  1739. grid_get_cell(back_s->grid, px, py, &gc);
  1740. if (!(gc.flags & GRID_FLAG_PADDING) &&
  1741. gc.data.size == 1 && *gc.data.data == data->jumpchar) {
  1742. window_copy_update_cursor(wp, px, data->cy);
  1743. if (window_copy_update_selection(wp, 1))
  1744. window_copy_redraw_lines(wp, data->cy, 1);
  1745. return;
  1746. }
  1747. px++;
  1748. }
  1749. }
  1750. void
  1751. window_copy_cursor_jump_back(struct window_pane *wp)
  1752. {
  1753. struct window_copy_mode_data *data = wp->modedata;
  1754. struct screen *back_s = data->backing;
  1755. struct grid_cell gc;
  1756. u_int px, py;
  1757. px = data->cx;
  1758. py = screen_hsize(back_s) + data->cy - data->oy;
  1759. if (px > 0)
  1760. px--;
  1761. for (;;) {
  1762. grid_get_cell(back_s->grid, px, py, &gc);
  1763. if (!(gc.flags & GRID_FLAG_PADDING) &&
  1764. gc.data.size == 1 && *gc.data.data == data->jumpchar) {
  1765. window_copy_update_cursor(wp, px, data->cy);
  1766. if (window_copy_update_selection(wp, 1))
  1767. window_copy_redraw_lines(wp, data->cy, 1);
  1768. return;
  1769. }
  1770. if (px == 0)
  1771. break;
  1772. px--;
  1773. }
  1774. }
  1775. void
  1776. window_copy_cursor_jump_to(struct window_pane *wp, int jump_again)
  1777. {
  1778. struct window_copy_mode_data *data = wp->modedata;
  1779. struct screen *back_s = data->backing;
  1780. struct grid_cell gc;
  1781. u_int px, py, xx;
  1782. px = data->cx + 1 + jump_again;
  1783. py = screen_hsize(back_s) + data->cy - data->oy;
  1784. xx = window_copy_find_length(wp, py);
  1785. while (px < xx) {
  1786. grid_get_cell(back_s->grid, px, py, &gc);
  1787. if (!(gc.flags & GRID_FLAG_PADDING) &&
  1788. gc.data.size == 1 && *gc.data.data == data->jumpchar) {
  1789. window_copy_update_cursor(wp, px - 1, data->cy);
  1790. if (window_copy_update_selection(wp, 1))
  1791. window_copy_redraw_lines(wp, data->cy, 1);
  1792. return;
  1793. }
  1794. px++;
  1795. }
  1796. }
  1797. void
  1798. window_copy_cursor_jump_to_back(struct window_pane *wp, int jump_again)
  1799. {
  1800. struct window_copy_mode_data *data = wp->modedata;
  1801. struct screen *back_s = data->backing;
  1802. struct grid_cell gc;
  1803. u_int px, py;
  1804. px = data->cx;
  1805. py = screen_hsize(back_s) + data->cy - data->oy;
  1806. if (px > 0)
  1807. px--;
  1808. if (jump_again && px > 0)
  1809. px--;
  1810. for (;;) {
  1811. grid_get_cell(back_s->grid, px, py, &gc);
  1812. if (!(gc.flags & GRID_FLAG_PADDING) &&
  1813. gc.data.size == 1 && *gc.data.data == data->jumpchar) {
  1814. window_copy_update_cursor(wp, px + 1, data->cy);
  1815. if (window_copy_update_selection(wp, 1))
  1816. window_copy_redraw_lines(wp, data->cy, 1);
  1817. return;
  1818. }
  1819. if (px == 0)
  1820. break;
  1821. px--;
  1822. }
  1823. }
  1824. void
  1825. window_copy_cursor_next_word(struct window_pane *wp, const char *separators)
  1826. {
  1827. struct window_copy_mode_data *data = wp->modedata;
  1828. struct screen *back_s = data->backing;
  1829. u_int px, py, xx, yy;
  1830. int expected = 0;
  1831. px = data->cx;
  1832. py = screen_hsize(back_s) + data->cy - data->oy;
  1833. xx = window_copy_find_length(wp, py);
  1834. yy = screen_hsize(back_s) + screen_size_y(back_s) - 1;
  1835. /*
  1836. * First skip past any nonword characters and then any word characters.
  1837. *
  1838. * expected is initially set to 0 for the former and then 1 for the
  1839. * latter.
  1840. */
  1841. do {
  1842. while (px > xx ||
  1843. window_copy_in_set(wp, px, py, separators) == expected) {
  1844. /* Move down if we're past the end of the line. */
  1845. if (px > xx) {
  1846. if (py == yy)
  1847. return;
  1848. window_copy_cursor_down(wp, 0);
  1849. px = 0;
  1850. py = screen_hsize(back_s) + data->cy - data->oy;
  1851. xx = window_copy_find_length(wp, py);
  1852. } else
  1853. px++;
  1854. }
  1855. expected = !expected;
  1856. } while (expected == 1);
  1857. window_copy_update_cursor(wp, px, data->cy);
  1858. if (window_copy_update_selection(wp, 1))
  1859. window_copy_redraw_lines(wp, data->cy, 1);
  1860. }
  1861. void
  1862. window_copy_cursor_next_word_end(struct window_pane *wp,
  1863. const char *separators)
  1864. {
  1865. struct window_copy_mode_data *data = wp->modedata;
  1866. struct options *oo = wp->window->options;
  1867. struct screen *back_s = data->backing;
  1868. u_int px, py, xx, yy;
  1869. int keys, expected = 1;
  1870. px = data->cx;
  1871. py = screen_hsize(back_s) + data->cy - data->oy;
  1872. xx = window_copy_find_length(wp, py);
  1873. yy = screen_hsize(back_s) + screen_size_y(back_s) - 1;
  1874. keys = options_get_number(oo, "mode-keys");
  1875. if (keys == MODEKEY_VI && !window_copy_in_set(wp, px, py, separators))
  1876. px++;
  1877. /*
  1878. * First skip past any word characters, then any nonword characters.
  1879. *
  1880. * expected is initially set to 1 for the former and then 0 for the
  1881. * latter.
  1882. */
  1883. do {
  1884. while (px > xx ||
  1885. window_copy_in_set(wp, px, py, separators) == expected) {
  1886. /* Move down if we're past the end of the line. */
  1887. if (px > xx) {
  1888. if (py == yy)
  1889. return;
  1890. window_copy_cursor_down(wp, 0);
  1891. px = 0;
  1892. py = screen_hsize(back_s) + data->cy - data->oy;
  1893. xx = window_copy_find_length(wp, py);
  1894. } else
  1895. px++;
  1896. }
  1897. expected = !expected;
  1898. } while (expected == 0);
  1899. if (keys == MODEKEY_VI && px != 0)
  1900. px--;
  1901. window_copy_update_cursor(wp, px, data->cy);
  1902. if (window_copy_update_selection(wp, 1))
  1903. window_copy_redraw_lines(wp, data->cy, 1);
  1904. }
  1905. /* Move to the previous place where a word begins. */
  1906. void
  1907. window_copy_cursor_previous_word(struct window_pane *wp,
  1908. const char *separators)
  1909. {
  1910. struct window_copy_mode_data *data = wp->modedata;
  1911. u_int px, py;
  1912. px = data->cx;
  1913. py = screen_hsize(data->backing) + data->cy - data->oy;
  1914. /* Move back to the previous word character. */
  1915. for (;;) {
  1916. if (px > 0) {
  1917. px--;
  1918. if (!window_copy_in_set(wp, px, py, separators))
  1919. break;
  1920. } else {
  1921. if (data->cy == 0 &&
  1922. (screen_hsize(data->backing) == 0 ||
  1923. data->oy >= screen_hsize(data->backing) - 1))
  1924. goto out;
  1925. window_copy_cursor_up(wp, 0);
  1926. py = screen_hsize(data->backing) + data->cy - data->oy;
  1927. px = window_copy_find_length(wp, py);
  1928. }
  1929. }
  1930. /* Move back to the beginning of this word. */
  1931. while (px > 0 && !window_copy_in_set(wp, px - 1, py, separators))
  1932. px--;
  1933. out:
  1934. window_copy_update_cursor(wp, px, data->cy);
  1935. if (window_copy_update_selection(wp, 1))
  1936. window_copy_redraw_lines(wp, data->cy, 1);
  1937. }
  1938. void
  1939. window_copy_scroll_up(struct window_pane *wp, u_int ny)
  1940. {
  1941. struct window_copy_mode_data *data = wp->modedata;
  1942. struct screen *s = &data->screen;
  1943. struct screen_write_ctx ctx;
  1944. if (data->oy < ny)
  1945. ny = data->oy;
  1946. if (ny == 0)
  1947. return;
  1948. data->oy -= ny;
  1949. window_copy_update_selection(wp, 0);
  1950. screen_write_start(&ctx, wp, NULL);
  1951. screen_write_cursormove(&ctx, 0, 0);
  1952. screen_write_deleteline(&ctx, ny);
  1953. window_copy_write_lines(wp, &ctx, screen_size_y(s) - ny, ny);
  1954. window_copy_write_line(wp, &ctx, 0);
  1955. if (screen_size_y(s) > 1)
  1956. window_copy_write_line(wp, &ctx, 1);
  1957. if (screen_size_y(s) > 3)
  1958. window_copy_write_line(wp, &ctx, screen_size_y(s) - 2);
  1959. if (s->sel.flag && screen_size_y(s) > ny)
  1960. window_copy_write_line(wp, &ctx, screen_size_y(s) - ny - 1);
  1961. screen_write_cursormove(&ctx, data->cx, data->cy);
  1962. screen_write_stop(&ctx);
  1963. }
  1964. void
  1965. window_copy_scroll_down(struct window_pane *wp, u_int ny)
  1966. {
  1967. struct window_copy_mode_data *data = wp->modedata;
  1968. struct screen *s = &data->screen;
  1969. struct screen_write_ctx ctx;
  1970. if (ny > screen_hsize(data->backing))
  1971. return;
  1972. if (data->oy > screen_hsize(data->backing) - ny)
  1973. ny = screen_hsize(data->backing) - data->oy;
  1974. if (ny == 0)
  1975. return;
  1976. data->oy += ny;
  1977. window_copy_update_selection(wp, 0);
  1978. screen_write_start(&ctx, wp, NULL);
  1979. screen_write_cursormove(&ctx, 0, 0);
  1980. screen_write_insertline(&ctx, ny);
  1981. window_copy_write_lines(wp, &ctx, 0, ny);
  1982. if (s->sel.flag && screen_size_y(s) > ny)
  1983. window_copy_write_line(wp, &ctx, ny);
  1984. else if (ny == 1) /* nuke position */
  1985. window_copy_write_line(wp, &ctx, 1);
  1986. screen_write_cursormove(&ctx, data->cx, data->cy);
  1987. screen_write_stop(&ctx);
  1988. }
  1989. int
  1990. window_copy_scroll_position(struct window_pane *wp)
  1991. {
  1992. struct window_copy_mode_data *data = wp->modedata;
  1993. if (wp->mode != &window_copy_mode)
  1994. return (-1);
  1995. return (data->oy);
  1996. }
  1997. void
  1998. window_copy_rectangle_toggle(struct window_pane *wp)
  1999. {
  2000. struct window_copy_mode_data *data = wp->modedata;
  2001. u_int px, py;
  2002. data->rectflag = !data->rectflag;
  2003. py = screen_hsize(data->backing) + data->cy - data->oy;
  2004. px = window_copy_find_length(wp, py);
  2005. if (data->cx > px)
  2006. window_copy_update_cursor(wp, px, data->cy);
  2007. window_copy_update_selection(wp, 1);
  2008. window_copy_redraw_screen(wp);
  2009. }
  2010. void
  2011. window_copy_start_drag(struct client *c, struct mouse_event *m)
  2012. {
  2013. struct window_pane *wp;
  2014. u_int x, y;
  2015. wp = cmd_mouse_pane(m, NULL, NULL);
  2016. if (wp == NULL || wp->mode != &window_copy_mode)
  2017. return;
  2018. if (cmd_mouse_at(wp, m, &x, &y, 1) != 0)
  2019. return;
  2020. c->tty.mouse_drag_update = window_copy_drag_update;
  2021. c->tty.mouse_drag_release = NULL; /* will fire MouseUp key */
  2022. window_copy_update_cursor(wp, x, y);
  2023. window_copy_start_selection(wp);
  2024. window_copy_redraw_screen(wp);
  2025. }
  2026. void
  2027. window_copy_drag_update(__unused struct client *c, struct mouse_event *m)
  2028. {
  2029. struct window_pane *wp;
  2030. struct window_copy_mode_data *data;
  2031. u_int x, y, old_cy;
  2032. wp = cmd_mouse_pane(m, NULL, NULL);
  2033. if (wp == NULL || wp->mode != &window_copy_mode)
  2034. return;
  2035. data = wp->modedata;
  2036. if (cmd_mouse_at(wp, m, &x, &y, 0) != 0)
  2037. return;
  2038. old_cy = data->cy;
  2039. window_copy_update_cursor(wp, x, y);
  2040. if (window_copy_update_selection(wp, 1))
  2041. window_copy_redraw_selection(wp, old_cy);
  2042. }