Line | Count | Source (jump to first uncovered line) |
1 | | /* $OpenBSD$ */ |
2 | | |
3 | | /* |
4 | | * Copyright (c) 2017 Nicholas Marriott <nicholas.marriott@gmail.com> |
5 | | * |
6 | | * Permission to use, copy, modify, and distribute this software for any |
7 | | * purpose with or without fee is hereby granted, provided that the above |
8 | | * copyright notice and this permission notice appear in all copies. |
9 | | * |
10 | | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
11 | | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
12 | | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
13 | | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
14 | | * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER |
15 | | * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING |
16 | | * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
17 | | */ |
18 | | |
19 | | #include <sys/types.h> |
20 | | |
21 | | #include <ctype.h> |
22 | | #include <stdio.h> |
23 | | #include <stdlib.h> |
24 | | #include <string.h> |
25 | | |
26 | | #include "tmux.h" |
27 | | |
28 | | enum mode_tree_search_dir { |
29 | | MODE_TREE_SEARCH_FORWARD, |
30 | | MODE_TREE_SEARCH_BACKWARD |
31 | | }; |
32 | | |
33 | | enum mode_tree_preview { |
34 | | MODE_TREE_PREVIEW_OFF, |
35 | | MODE_TREE_PREVIEW_NORMAL, |
36 | | MODE_TREE_PREVIEW_BIG |
37 | | }; |
38 | | |
39 | | struct mode_tree_item; |
40 | | TAILQ_HEAD(mode_tree_list, mode_tree_item); |
41 | | |
42 | | struct mode_tree_data { |
43 | | int dead; |
44 | | u_int references; |
45 | | int zoomed; |
46 | | |
47 | | struct window_pane *wp; |
48 | | void *modedata; |
49 | | const struct menu_item *menu; |
50 | | |
51 | | const char **sort_list; |
52 | | u_int sort_size; |
53 | | struct mode_tree_sort_criteria sort_crit; |
54 | | |
55 | | mode_tree_build_cb buildcb; |
56 | | mode_tree_draw_cb drawcb; |
57 | | mode_tree_search_cb searchcb; |
58 | | mode_tree_menu_cb menucb; |
59 | | mode_tree_height_cb heightcb; |
60 | | mode_tree_key_cb keycb; |
61 | | mode_tree_swap_cb swapcb; |
62 | | |
63 | | struct mode_tree_list children; |
64 | | struct mode_tree_list saved; |
65 | | |
66 | | struct mode_tree_line *line_list; |
67 | | u_int line_size; |
68 | | |
69 | | u_int depth; |
70 | | u_int maxdepth; |
71 | | |
72 | | u_int width; |
73 | | u_int height; |
74 | | |
75 | | u_int offset; |
76 | | u_int current; |
77 | | |
78 | | struct screen screen; |
79 | | |
80 | | int preview; |
81 | | char *search; |
82 | | char *filter; |
83 | | int no_matches; |
84 | | enum mode_tree_search_dir search_dir; |
85 | | }; |
86 | | |
87 | | struct mode_tree_item { |
88 | | struct mode_tree_item *parent; |
89 | | void *itemdata; |
90 | | u_int line; |
91 | | |
92 | | key_code key; |
93 | | const char *keystr; |
94 | | size_t keylen; |
95 | | |
96 | | uint64_t tag; |
97 | | const char *name; |
98 | | const char *text; |
99 | | |
100 | | int expanded; |
101 | | int tagged; |
102 | | |
103 | | int draw_as_parent; |
104 | | int no_tag; |
105 | | int align; |
106 | | |
107 | | struct mode_tree_list children; |
108 | | TAILQ_ENTRY(mode_tree_item) entry; |
109 | | }; |
110 | | |
111 | | struct mode_tree_line { |
112 | | struct mode_tree_item *item; |
113 | | u_int depth; |
114 | | int last; |
115 | | int flat; |
116 | | }; |
117 | | |
118 | | struct mode_tree_menu { |
119 | | struct mode_tree_data *data; |
120 | | struct client *c; |
121 | | u_int line; |
122 | | }; |
123 | | |
124 | | static void mode_tree_free_items(struct mode_tree_list *); |
125 | | |
126 | | static const struct menu_item mode_tree_menu_items[] = { |
127 | | { "Scroll Left", '<', NULL }, |
128 | | { "Scroll Right", '>', NULL }, |
129 | | { "", KEYC_NONE, NULL }, |
130 | | { "Cancel", 'q', NULL }, |
131 | | |
132 | | { NULL, KEYC_NONE, NULL } |
133 | | }; |
134 | | |
135 | | static struct mode_tree_item * |
136 | | mode_tree_find_item(struct mode_tree_list *mtl, uint64_t tag) |
137 | 0 | { |
138 | 0 | struct mode_tree_item *mti, *child; |
139 | |
|
140 | 0 | TAILQ_FOREACH(mti, mtl, entry) { |
141 | 0 | if (mti->tag == tag) |
142 | 0 | return (mti); |
143 | 0 | child = mode_tree_find_item(&mti->children, tag); |
144 | 0 | if (child != NULL) |
145 | 0 | return (child); |
146 | 0 | } |
147 | 0 | return (NULL); |
148 | 0 | } |
149 | | |
150 | | static void |
151 | | mode_tree_free_item(struct mode_tree_item *mti) |
152 | 0 | { |
153 | 0 | mode_tree_free_items(&mti->children); |
154 | |
|
155 | 0 | free((void *)mti->name); |
156 | 0 | free((void *)mti->text); |
157 | 0 | free((void *)mti->keystr); |
158 | |
|
159 | 0 | free(mti); |
160 | 0 | } |
161 | | |
162 | | static void |
163 | | mode_tree_free_items(struct mode_tree_list *mtl) |
164 | 0 | { |
165 | 0 | struct mode_tree_item *mti, *mti1; |
166 | |
|
167 | 0 | TAILQ_FOREACH_SAFE(mti, mtl, entry, mti1) { |
168 | 0 | TAILQ_REMOVE(mtl, mti, entry); |
169 | 0 | mode_tree_free_item(mti); |
170 | 0 | } |
171 | 0 | } |
172 | | |
173 | | static void |
174 | | mode_tree_check_selected(struct mode_tree_data *mtd) |
175 | 0 | { |
176 | | /* |
177 | | * If the current line would now be off screen reset the offset to the |
178 | | * last visible line. |
179 | | */ |
180 | 0 | if (mtd->current > mtd->height - 1) |
181 | 0 | mtd->offset = mtd->current - mtd->height + 1; |
182 | 0 | } |
183 | | |
184 | | static void |
185 | | mode_tree_clear_lines(struct mode_tree_data *mtd) |
186 | 0 | { |
187 | 0 | free(mtd->line_list); |
188 | 0 | mtd->line_list = NULL; |
189 | 0 | mtd->line_size = 0; |
190 | 0 | } |
191 | | |
192 | | static void |
193 | | mode_tree_build_lines(struct mode_tree_data *mtd, |
194 | | struct mode_tree_list *mtl, u_int depth) |
195 | 0 | { |
196 | 0 | struct mode_tree_item *mti; |
197 | 0 | struct mode_tree_line *line; |
198 | 0 | u_int i; |
199 | 0 | int flat = 1; |
200 | |
|
201 | 0 | mtd->depth = depth; |
202 | 0 | if (depth > mtd->maxdepth) |
203 | 0 | mtd->maxdepth = depth; |
204 | 0 | TAILQ_FOREACH(mti, mtl, entry) { |
205 | 0 | mtd->line_list = xreallocarray(mtd->line_list, |
206 | 0 | mtd->line_size + 1, sizeof *mtd->line_list); |
207 | |
|
208 | 0 | line = &mtd->line_list[mtd->line_size++]; |
209 | 0 | line->item = mti; |
210 | 0 | line->depth = depth; |
211 | 0 | line->last = (mti == TAILQ_LAST(mtl, mode_tree_list)); |
212 | |
|
213 | 0 | mti->line = (mtd->line_size - 1); |
214 | 0 | if (!TAILQ_EMPTY(&mti->children)) |
215 | 0 | flat = 0; |
216 | 0 | if (mti->expanded) |
217 | 0 | mode_tree_build_lines(mtd, &mti->children, depth + 1); |
218 | |
|
219 | 0 | if (mtd->keycb != NULL) { |
220 | 0 | mti->key = mtd->keycb(mtd->modedata, mti->itemdata, |
221 | 0 | mti->line); |
222 | 0 | if (mti->key == KEYC_UNKNOWN) |
223 | 0 | mti->key = KEYC_NONE; |
224 | 0 | } else if (mti->line < 10) |
225 | 0 | mti->key = '0' + mti->line; |
226 | 0 | else if (mti->line < 36) |
227 | 0 | mti->key = KEYC_META|('a' + mti->line - 10); |
228 | 0 | else |
229 | 0 | mti->key = KEYC_NONE; |
230 | 0 | if (mti->key != KEYC_NONE) { |
231 | 0 | mti->keystr = xstrdup(key_string_lookup_key(mti->key, |
232 | 0 | 0)); |
233 | 0 | mti->keylen = strlen(mti->keystr); |
234 | 0 | } else { |
235 | 0 | mti->keystr = NULL; |
236 | 0 | mti->keylen = 0; |
237 | 0 | } |
238 | 0 | } |
239 | 0 | TAILQ_FOREACH(mti, mtl, entry) { |
240 | 0 | for (i = 0; i < mtd->line_size; i++) { |
241 | 0 | line = &mtd->line_list[i]; |
242 | 0 | if (line->item == mti) |
243 | 0 | line->flat = flat; |
244 | 0 | } |
245 | 0 | } |
246 | 0 | } |
247 | | |
248 | | static void |
249 | | mode_tree_clear_tagged(struct mode_tree_list *mtl) |
250 | 0 | { |
251 | 0 | struct mode_tree_item *mti; |
252 | |
|
253 | 0 | TAILQ_FOREACH(mti, mtl, entry) { |
254 | 0 | mti->tagged = 0; |
255 | 0 | mode_tree_clear_tagged(&mti->children); |
256 | 0 | } |
257 | 0 | } |
258 | | |
259 | | void |
260 | | mode_tree_up(struct mode_tree_data *mtd, int wrap) |
261 | 0 | { |
262 | 0 | if (mtd->current == 0) { |
263 | 0 | if (wrap) { |
264 | 0 | mtd->current = mtd->line_size - 1; |
265 | 0 | if (mtd->line_size >= mtd->height) |
266 | 0 | mtd->offset = mtd->line_size - mtd->height; |
267 | 0 | } |
268 | 0 | } else { |
269 | 0 | mtd->current--; |
270 | 0 | if (mtd->current < mtd->offset) |
271 | 0 | mtd->offset--; |
272 | 0 | } |
273 | 0 | } |
274 | | |
275 | | int |
276 | | mode_tree_down(struct mode_tree_data *mtd, int wrap) |
277 | 0 | { |
278 | 0 | if (mtd->current == mtd->line_size - 1) { |
279 | 0 | if (wrap) { |
280 | 0 | mtd->current = 0; |
281 | 0 | mtd->offset = 0; |
282 | 0 | } else |
283 | 0 | return (0); |
284 | 0 | } else { |
285 | 0 | mtd->current++; |
286 | 0 | if (mtd->current > mtd->offset + mtd->height - 1) |
287 | 0 | mtd->offset++; |
288 | 0 | } |
289 | 0 | return (1); |
290 | 0 | } |
291 | | |
292 | | static void |
293 | | mode_tree_swap(struct mode_tree_data *mtd, int direction) |
294 | 0 | { |
295 | 0 | u_int current_depth = mtd->line_list[mtd->current].depth; |
296 | 0 | u_int swap_with, swap_with_depth; |
297 | |
|
298 | 0 | if (mtd->swapcb == NULL) |
299 | 0 | return; |
300 | | |
301 | | /* Find the next line at the same depth with the same parent . */ |
302 | 0 | swap_with = mtd->current; |
303 | 0 | do { |
304 | 0 | if (direction < 0 && swap_with < (u_int)-direction) |
305 | 0 | return; |
306 | 0 | if (direction > 0 && swap_with + direction >= mtd->line_size) |
307 | 0 | return; |
308 | 0 | swap_with += direction; |
309 | 0 | swap_with_depth = mtd->line_list[swap_with].depth; |
310 | 0 | } while (swap_with_depth > current_depth); |
311 | 0 | if (swap_with_depth != current_depth) |
312 | 0 | return; |
313 | | |
314 | 0 | if (mtd->swapcb(mtd->line_list[mtd->current].item->itemdata, |
315 | 0 | mtd->line_list[swap_with].item->itemdata)) { |
316 | 0 | mtd->current = swap_with; |
317 | 0 | mode_tree_build(mtd); |
318 | 0 | } |
319 | 0 | } |
320 | | |
321 | | void * |
322 | | mode_tree_get_current(struct mode_tree_data *mtd) |
323 | 0 | { |
324 | 0 | return (mtd->line_list[mtd->current].item->itemdata); |
325 | 0 | } |
326 | | |
327 | | const char * |
328 | | mode_tree_get_current_name(struct mode_tree_data *mtd) |
329 | 0 | { |
330 | 0 | return (mtd->line_list[mtd->current].item->name); |
331 | 0 | } |
332 | | |
333 | | void |
334 | | mode_tree_expand_current(struct mode_tree_data *mtd) |
335 | 0 | { |
336 | 0 | if (!mtd->line_list[mtd->current].item->expanded) { |
337 | 0 | mtd->line_list[mtd->current].item->expanded = 1; |
338 | 0 | mode_tree_build(mtd); |
339 | 0 | } |
340 | 0 | } |
341 | | |
342 | | void |
343 | | mode_tree_collapse_current(struct mode_tree_data *mtd) |
344 | 0 | { |
345 | 0 | if (mtd->line_list[mtd->current].item->expanded) { |
346 | 0 | mtd->line_list[mtd->current].item->expanded = 0; |
347 | 0 | mode_tree_build(mtd); |
348 | 0 | } |
349 | 0 | } |
350 | | |
351 | | static int |
352 | | mode_tree_get_tag(struct mode_tree_data *mtd, uint64_t tag, u_int *found) |
353 | 0 | { |
354 | 0 | u_int i; |
355 | |
|
356 | 0 | for (i = 0; i < mtd->line_size; i++) { |
357 | 0 | if (mtd->line_list[i].item->tag == tag) |
358 | 0 | break; |
359 | 0 | } |
360 | 0 | if (i != mtd->line_size) { |
361 | 0 | *found = i; |
362 | 0 | return (1); |
363 | 0 | } |
364 | 0 | return (0); |
365 | 0 | } |
366 | | |
367 | | void |
368 | | mode_tree_expand(struct mode_tree_data *mtd, uint64_t tag) |
369 | 0 | { |
370 | 0 | u_int found; |
371 | |
|
372 | 0 | if (!mode_tree_get_tag(mtd, tag, &found)) |
373 | 0 | return; |
374 | 0 | if (!mtd->line_list[found].item->expanded) { |
375 | 0 | mtd->line_list[found].item->expanded = 1; |
376 | 0 | mode_tree_build(mtd); |
377 | 0 | } |
378 | 0 | } |
379 | | |
380 | | int |
381 | | mode_tree_set_current(struct mode_tree_data *mtd, uint64_t tag) |
382 | 0 | { |
383 | 0 | u_int found; |
384 | |
|
385 | 0 | if (mode_tree_get_tag(mtd, tag, &found)) { |
386 | 0 | mtd->current = found; |
387 | 0 | if (mtd->current > mtd->height - 1) |
388 | 0 | mtd->offset = mtd->current - mtd->height + 1; |
389 | 0 | else |
390 | 0 | mtd->offset = 0; |
391 | 0 | return (1); |
392 | 0 | } |
393 | 0 | if (mtd->current >= mtd->line_size) { |
394 | 0 | mtd->current = mtd->line_size - 1; |
395 | 0 | if (mtd->current > mtd->height - 1) |
396 | 0 | mtd->offset = mtd->current - mtd->height + 1; |
397 | 0 | else |
398 | 0 | mtd->offset = 0; |
399 | 0 | } |
400 | 0 | return (0); |
401 | 0 | } |
402 | | |
403 | | u_int |
404 | | mode_tree_count_tagged(struct mode_tree_data *mtd) |
405 | 0 | { |
406 | 0 | struct mode_tree_item *mti; |
407 | 0 | u_int i, tagged; |
408 | |
|
409 | 0 | tagged = 0; |
410 | 0 | for (i = 0; i < mtd->line_size; i++) { |
411 | 0 | mti = mtd->line_list[i].item; |
412 | 0 | if (mti->tagged) |
413 | 0 | tagged++; |
414 | 0 | } |
415 | 0 | return (tagged); |
416 | 0 | } |
417 | | |
418 | | void |
419 | | mode_tree_each_tagged(struct mode_tree_data *mtd, mode_tree_each_cb cb, |
420 | | struct client *c, key_code key, int current) |
421 | 0 | { |
422 | 0 | struct mode_tree_item *mti; |
423 | 0 | u_int i; |
424 | 0 | int fired; |
425 | |
|
426 | 0 | fired = 0; |
427 | 0 | for (i = 0; i < mtd->line_size; i++) { |
428 | 0 | mti = mtd->line_list[i].item; |
429 | 0 | if (mti->tagged) { |
430 | 0 | fired = 1; |
431 | 0 | cb(mtd->modedata, mti->itemdata, c, key); |
432 | 0 | } |
433 | 0 | } |
434 | 0 | if (!fired && current) { |
435 | 0 | mti = mtd->line_list[mtd->current].item; |
436 | 0 | cb(mtd->modedata, mti->itemdata, c, key); |
437 | 0 | } |
438 | 0 | } |
439 | | |
440 | | struct mode_tree_data * |
441 | | mode_tree_start(struct window_pane *wp, struct args *args, |
442 | | mode_tree_build_cb buildcb, mode_tree_draw_cb drawcb, |
443 | | mode_tree_search_cb searchcb, mode_tree_menu_cb menucb, |
444 | | mode_tree_height_cb heightcb, mode_tree_key_cb keycb, |
445 | | mode_tree_swap_cb swapcb, void *modedata, const struct menu_item *menu, |
446 | | const char **sort_list, u_int sort_size, struct screen **s) |
447 | 0 | { |
448 | 0 | struct mode_tree_data *mtd; |
449 | 0 | const char *sort; |
450 | 0 | u_int i; |
451 | |
|
452 | 0 | mtd = xcalloc(1, sizeof *mtd); |
453 | 0 | mtd->references = 1; |
454 | |
|
455 | 0 | mtd->wp = wp; |
456 | 0 | mtd->modedata = modedata; |
457 | 0 | mtd->menu = menu; |
458 | |
|
459 | 0 | mtd->sort_list = sort_list; |
460 | 0 | mtd->sort_size = sort_size; |
461 | |
|
462 | 0 | if (args_has(args, 'N') > 1) |
463 | 0 | mtd->preview = MODE_TREE_PREVIEW_BIG; |
464 | 0 | else if (args_has(args, 'N')) |
465 | 0 | mtd->preview = MODE_TREE_PREVIEW_OFF; |
466 | 0 | else |
467 | 0 | mtd->preview = MODE_TREE_PREVIEW_NORMAL; |
468 | |
|
469 | 0 | sort = args_get(args, 'O'); |
470 | 0 | if (sort != NULL) { |
471 | 0 | for (i = 0; i < sort_size; i++) { |
472 | 0 | if (strcasecmp(sort, sort_list[i]) == 0) |
473 | 0 | mtd->sort_crit.field = i; |
474 | 0 | } |
475 | 0 | } |
476 | 0 | mtd->sort_crit.reversed = args_has(args, 'r'); |
477 | |
|
478 | 0 | if (args_has(args, 'f')) |
479 | 0 | mtd->filter = xstrdup(args_get(args, 'f')); |
480 | 0 | else |
481 | 0 | mtd->filter = NULL; |
482 | |
|
483 | 0 | mtd->buildcb = buildcb; |
484 | 0 | mtd->drawcb = drawcb; |
485 | 0 | mtd->searchcb = searchcb; |
486 | 0 | mtd->menucb = menucb; |
487 | 0 | mtd->heightcb = heightcb; |
488 | 0 | mtd->keycb = keycb; |
489 | 0 | mtd->swapcb = swapcb; |
490 | |
|
491 | 0 | TAILQ_INIT(&mtd->children); |
492 | |
|
493 | 0 | *s = &mtd->screen; |
494 | 0 | screen_init(*s, screen_size_x(&wp->base), screen_size_y(&wp->base), 0); |
495 | 0 | (*s)->mode &= ~MODE_CURSOR; |
496 | |
|
497 | 0 | return (mtd); |
498 | 0 | } |
499 | | |
500 | | void |
501 | | mode_tree_zoom(struct mode_tree_data *mtd, struct args *args) |
502 | 0 | { |
503 | 0 | struct window_pane *wp = mtd->wp; |
504 | |
|
505 | 0 | if (args_has(args, 'Z')) { |
506 | 0 | mtd->zoomed = (wp->window->flags & WINDOW_ZOOMED); |
507 | 0 | if (!mtd->zoomed && window_zoom(wp) == 0) |
508 | 0 | server_redraw_window(wp->window); |
509 | 0 | } else |
510 | 0 | mtd->zoomed = -1; |
511 | 0 | } |
512 | | |
513 | | static void |
514 | | mode_tree_set_height(struct mode_tree_data *mtd) |
515 | 0 | { |
516 | 0 | struct screen *s = &mtd->screen; |
517 | 0 | u_int height; |
518 | |
|
519 | 0 | if (mtd->heightcb != NULL) { |
520 | 0 | height = mtd->heightcb(mtd, screen_size_y(s)); |
521 | 0 | if (height < screen_size_y(s)) |
522 | 0 | mtd->height = screen_size_y(s) - height; |
523 | 0 | } else { |
524 | 0 | if (mtd->preview == MODE_TREE_PREVIEW_NORMAL) { |
525 | 0 | mtd->height = (screen_size_y(s) / 3) * 2; |
526 | 0 | if (mtd->height > mtd->line_size) |
527 | 0 | mtd->height = screen_size_y(s) / 2; |
528 | 0 | if (mtd->height < 10) |
529 | 0 | mtd->height = screen_size_y(s); |
530 | 0 | } else if (mtd->preview == MODE_TREE_PREVIEW_BIG) { |
531 | 0 | mtd->height = screen_size_y(s) / 4; |
532 | 0 | if (mtd->height > mtd->line_size) |
533 | 0 | mtd->height = mtd->line_size; |
534 | 0 | if (mtd->height < 2) |
535 | 0 | mtd->height = 2; |
536 | 0 | } else |
537 | 0 | mtd->height = screen_size_y(s); |
538 | 0 | } |
539 | 0 | if (screen_size_y(s) - mtd->height < 2) |
540 | 0 | mtd->height = screen_size_y(s); |
541 | 0 | } |
542 | | |
543 | | void |
544 | | mode_tree_build(struct mode_tree_data *mtd) |
545 | 0 | { |
546 | 0 | struct screen *s = &mtd->screen; |
547 | 0 | uint64_t tag; |
548 | |
|
549 | 0 | if (mtd->line_list != NULL) |
550 | 0 | tag = mtd->line_list[mtd->current].item->tag; |
551 | 0 | else |
552 | 0 | tag = UINT64_MAX; |
553 | |
|
554 | 0 | TAILQ_CONCAT(&mtd->saved, &mtd->children, entry); |
555 | 0 | TAILQ_INIT(&mtd->children); |
556 | |
|
557 | 0 | mtd->buildcb(mtd->modedata, &mtd->sort_crit, &tag, mtd->filter); |
558 | 0 | mtd->no_matches = TAILQ_EMPTY(&mtd->children); |
559 | 0 | if (mtd->no_matches) |
560 | 0 | mtd->buildcb(mtd->modedata, &mtd->sort_crit, &tag, NULL); |
561 | |
|
562 | 0 | mode_tree_free_items(&mtd->saved); |
563 | 0 | TAILQ_INIT(&mtd->saved); |
564 | |
|
565 | 0 | mode_tree_clear_lines(mtd); |
566 | 0 | mtd->maxdepth = 0; |
567 | 0 | mode_tree_build_lines(mtd, &mtd->children, 0); |
568 | |
|
569 | 0 | if (mtd->line_list != NULL && tag == UINT64_MAX) |
570 | 0 | tag = mtd->line_list[mtd->current].item->tag; |
571 | 0 | mode_tree_set_current(mtd, tag); |
572 | |
|
573 | 0 | mtd->width = screen_size_x(s); |
574 | 0 | if (mtd->preview != MODE_TREE_PREVIEW_OFF) |
575 | 0 | mode_tree_set_height(mtd); |
576 | 0 | else |
577 | 0 | mtd->height = screen_size_y(s); |
578 | 0 | mode_tree_check_selected(mtd); |
579 | 0 | } |
580 | | |
581 | | static void |
582 | | mode_tree_remove_ref(struct mode_tree_data *mtd) |
583 | 0 | { |
584 | 0 | if (--mtd->references == 0) |
585 | 0 | free(mtd); |
586 | 0 | } |
587 | | |
588 | | void |
589 | | mode_tree_free(struct mode_tree_data *mtd) |
590 | 0 | { |
591 | 0 | struct window_pane *wp = mtd->wp; |
592 | |
|
593 | 0 | if (mtd->zoomed == 0) |
594 | 0 | server_unzoom_window(wp->window); |
595 | |
|
596 | 0 | mode_tree_free_items(&mtd->children); |
597 | 0 | mode_tree_clear_lines(mtd); |
598 | 0 | screen_free(&mtd->screen); |
599 | |
|
600 | 0 | free(mtd->search); |
601 | 0 | free(mtd->filter); |
602 | |
|
603 | 0 | mtd->dead = 1; |
604 | 0 | mode_tree_remove_ref(mtd); |
605 | 0 | } |
606 | | |
607 | | void |
608 | | mode_tree_resize(struct mode_tree_data *mtd, u_int sx, u_int sy) |
609 | 0 | { |
610 | 0 | struct screen *s = &mtd->screen; |
611 | |
|
612 | 0 | screen_resize(s, sx, sy, 0); |
613 | |
|
614 | 0 | mode_tree_build(mtd); |
615 | 0 | mode_tree_draw(mtd); |
616 | |
|
617 | 0 | mtd->wp->flags |= PANE_REDRAW; |
618 | 0 | } |
619 | | |
620 | | struct mode_tree_item * |
621 | | mode_tree_add(struct mode_tree_data *mtd, struct mode_tree_item *parent, |
622 | | void *itemdata, uint64_t tag, const char *name, const char *text, |
623 | | int expanded) |
624 | 0 | { |
625 | 0 | struct mode_tree_item *mti, *saved; |
626 | |
|
627 | 0 | log_debug("%s: %llu, %s %s", __func__, (unsigned long long)tag, |
628 | 0 | name, (text == NULL ? "" : text)); |
629 | |
|
630 | 0 | mti = xcalloc(1, sizeof *mti); |
631 | 0 | mti->parent = parent; |
632 | 0 | mti->itemdata = itemdata; |
633 | |
|
634 | 0 | mti->tag = tag; |
635 | 0 | mti->name = xstrdup(name); |
636 | 0 | if (text != NULL) |
637 | 0 | mti->text = xstrdup(text); |
638 | |
|
639 | 0 | saved = mode_tree_find_item(&mtd->saved, tag); |
640 | 0 | if (saved != NULL) { |
641 | 0 | if (parent == NULL || parent->expanded) |
642 | 0 | mti->tagged = saved->tagged; |
643 | 0 | mti->expanded = saved->expanded; |
644 | 0 | } else if (expanded == -1) |
645 | 0 | mti->expanded = 1; |
646 | 0 | else |
647 | 0 | mti->expanded = expanded; |
648 | |
|
649 | 0 | TAILQ_INIT(&mti->children); |
650 | |
|
651 | 0 | if (parent != NULL) |
652 | 0 | TAILQ_INSERT_TAIL(&parent->children, mti, entry); |
653 | 0 | else |
654 | 0 | TAILQ_INSERT_TAIL(&mtd->children, mti, entry); |
655 | |
|
656 | 0 | return (mti); |
657 | 0 | } |
658 | | |
659 | | void |
660 | | mode_tree_draw_as_parent(struct mode_tree_item *mti) |
661 | 0 | { |
662 | 0 | mti->draw_as_parent = 1; |
663 | 0 | } |
664 | | |
665 | | void |
666 | | mode_tree_no_tag(struct mode_tree_item *mti) |
667 | 0 | { |
668 | 0 | mti->no_tag = 1; |
669 | 0 | } |
670 | | |
671 | | /* |
672 | | * Set the alignment of mti->name: -1 to align left, 0 (default) to not align, |
673 | | * or 1 to align right. |
674 | | */ |
675 | | void |
676 | | mode_tree_align(struct mode_tree_item *mti, int align) |
677 | 0 | { |
678 | 0 | mti->align = align; |
679 | 0 | } |
680 | | |
681 | | void |
682 | | mode_tree_remove(struct mode_tree_data *mtd, struct mode_tree_item *mti) |
683 | 0 | { |
684 | 0 | struct mode_tree_item *parent = mti->parent; |
685 | |
|
686 | 0 | if (parent != NULL) |
687 | 0 | TAILQ_REMOVE(&parent->children, mti, entry); |
688 | 0 | else |
689 | 0 | TAILQ_REMOVE(&mtd->children, mti, entry); |
690 | 0 | mode_tree_free_item(mti); |
691 | 0 | } |
692 | | |
693 | | void |
694 | | mode_tree_draw(struct mode_tree_data *mtd) |
695 | 0 | { |
696 | 0 | struct window_pane *wp = mtd->wp; |
697 | 0 | struct screen *s = &mtd->screen; |
698 | 0 | struct mode_tree_line *line; |
699 | 0 | struct mode_tree_item *mti; |
700 | 0 | struct options *oo = wp->window->options; |
701 | 0 | struct screen_write_ctx ctx; |
702 | 0 | struct grid_cell gc0, gc; |
703 | 0 | u_int w, h, i, j, sy, box_x, box_y, width; |
704 | 0 | char *text, *start, *key; |
705 | 0 | const char *tag, *symbol; |
706 | 0 | size_t size, n; |
707 | 0 | int keylen, pad, alignlen[mtd->maxdepth + 1]; |
708 | |
|
709 | 0 | if (mtd->line_size == 0) |
710 | 0 | return; |
711 | | |
712 | 0 | memcpy(&gc0, &grid_default_cell, sizeof gc0); |
713 | 0 | memcpy(&gc, &grid_default_cell, sizeof gc); |
714 | 0 | style_apply(&gc, oo, "mode-style", NULL); |
715 | |
|
716 | 0 | w = mtd->width; |
717 | 0 | h = mtd->height; |
718 | |
|
719 | 0 | screen_write_start(&ctx, s); |
720 | 0 | screen_write_clearscreen(&ctx, 8); |
721 | |
|
722 | 0 | keylen = 0; |
723 | 0 | for (i = 0; i < mtd->line_size; i++) { |
724 | 0 | mti = mtd->line_list[i].item; |
725 | 0 | if (mti->key == KEYC_NONE) |
726 | 0 | continue; |
727 | 0 | if ((int)mti->keylen + 3 > keylen) |
728 | 0 | keylen = mti->keylen + 3; |
729 | 0 | } |
730 | |
|
731 | 0 | for (i = 0; i < mtd->maxdepth + 1; i++) |
732 | 0 | alignlen[i] = 0; |
733 | 0 | for (i = 0; i < mtd->line_size; i++) { |
734 | 0 | line = &mtd->line_list[i]; |
735 | 0 | mti = line->item; |
736 | 0 | if (mti->align && |
737 | 0 | (int)strlen(mti->name) > alignlen[line->depth]) |
738 | 0 | alignlen[line->depth] = strlen(mti->name); |
739 | 0 | } |
740 | |
|
741 | 0 | for (i = 0; i < mtd->line_size; i++) { |
742 | 0 | if (i < mtd->offset) |
743 | 0 | continue; |
744 | 0 | if (i > mtd->offset + h - 1) |
745 | 0 | break; |
746 | 0 | line = &mtd->line_list[i]; |
747 | 0 | mti = line->item; |
748 | |
|
749 | 0 | screen_write_cursormove(&ctx, 0, i - mtd->offset, 0); |
750 | |
|
751 | 0 | pad = keylen - 2 - mti->keylen; |
752 | 0 | if (mti->key != KEYC_NONE) |
753 | 0 | xasprintf(&key, "(%s)%*s", mti->keystr, pad, ""); |
754 | 0 | else |
755 | 0 | key = xstrdup(""); |
756 | |
|
757 | 0 | if (line->flat) |
758 | 0 | symbol = ""; |
759 | 0 | else if (TAILQ_EMPTY(&mti->children)) |
760 | 0 | symbol = " "; |
761 | 0 | else if (mti->expanded) |
762 | 0 | symbol = "- "; |
763 | 0 | else |
764 | 0 | symbol = "+ "; |
765 | |
|
766 | 0 | if (line->depth == 0) |
767 | 0 | start = xstrdup(symbol); |
768 | 0 | else { |
769 | 0 | size = (4 * line->depth) + 32; |
770 | |
|
771 | 0 | start = xcalloc(1, size); |
772 | 0 | for (j = 1; j < line->depth; j++) { |
773 | 0 | if (mti->parent != NULL && |
774 | 0 | mtd->line_list[mti->parent->line].last) |
775 | 0 | strlcat(start, " ", size); |
776 | 0 | else |
777 | 0 | strlcat(start, "\001x\001 ", size); |
778 | 0 | } |
779 | 0 | if (line->last) |
780 | 0 | strlcat(start, "\001mq\001> ", size); |
781 | 0 | else |
782 | 0 | strlcat(start, "\001tq\001> ", size); |
783 | 0 | strlcat(start, symbol, size); |
784 | 0 | } |
785 | |
|
786 | 0 | if (mti->tagged) |
787 | 0 | tag = "*"; |
788 | 0 | else |
789 | 0 | tag = ""; |
790 | 0 | xasprintf(&text, "%-*s%s%*s%s%s", keylen, key, start, |
791 | 0 | mti->align * alignlen[line->depth], mti->name, tag, |
792 | 0 | (mti->text != NULL) ? ": " : "" ); |
793 | 0 | width = utf8_cstrwidth(text); |
794 | 0 | if (width > w) |
795 | 0 | width = w; |
796 | 0 | free(start); |
797 | |
|
798 | 0 | if (mti->tagged) { |
799 | 0 | gc.attr ^= GRID_ATTR_BRIGHT; |
800 | 0 | gc0.attr ^= GRID_ATTR_BRIGHT; |
801 | 0 | } |
802 | |
|
803 | 0 | if (i != mtd->current) { |
804 | 0 | screen_write_clearendofline(&ctx, 8); |
805 | 0 | screen_write_nputs(&ctx, w, &gc0, "%s", text); |
806 | 0 | if (mti->text != NULL) { |
807 | 0 | format_draw(&ctx, &gc0, w - width, mti->text, |
808 | 0 | NULL, 0); |
809 | 0 | } |
810 | 0 | } else { |
811 | 0 | screen_write_clearendofline(&ctx, gc.bg); |
812 | 0 | screen_write_nputs(&ctx, w, &gc, "%s", text); |
813 | 0 | if (mti->text != NULL) { |
814 | 0 | format_draw(&ctx, &gc, w - width, mti->text, |
815 | 0 | NULL, 0); |
816 | 0 | } |
817 | 0 | } |
818 | 0 | free(text); |
819 | 0 | free(key); |
820 | |
|
821 | 0 | if (mti->tagged) { |
822 | 0 | gc.attr ^= GRID_ATTR_BRIGHT; |
823 | 0 | gc0.attr ^= GRID_ATTR_BRIGHT; |
824 | 0 | } |
825 | 0 | } |
826 | |
|
827 | 0 | if (mtd->preview == MODE_TREE_PREVIEW_OFF) |
828 | 0 | goto done; |
829 | | |
830 | 0 | sy = screen_size_y(s); |
831 | 0 | if (sy <= 4 || h < 2 || sy - h <= 4 || w <= 4) |
832 | 0 | goto done; |
833 | | |
834 | 0 | line = &mtd->line_list[mtd->current]; |
835 | 0 | mti = line->item; |
836 | 0 | if (mti->draw_as_parent) |
837 | 0 | mti = mti->parent; |
838 | |
|
839 | 0 | screen_write_cursormove(&ctx, 0, h, 0); |
840 | 0 | screen_write_box(&ctx, w, sy - h, BOX_LINES_DEFAULT, NULL, NULL); |
841 | |
|
842 | 0 | if (mtd->sort_list != NULL) { |
843 | 0 | xasprintf(&text, " %s (sort: %s%s)", mti->name, |
844 | 0 | mtd->sort_list[mtd->sort_crit.field], |
845 | 0 | mtd->sort_crit.reversed ? ", reversed" : ""); |
846 | 0 | } else |
847 | 0 | xasprintf(&text, " %s", mti->name); |
848 | 0 | if (w - 2 >= strlen(text)) { |
849 | 0 | screen_write_cursormove(&ctx, 1, h, 0); |
850 | 0 | screen_write_puts(&ctx, &gc0, "%s", text); |
851 | |
|
852 | 0 | if (mtd->no_matches) |
853 | 0 | n = (sizeof "no matches") - 1; |
854 | 0 | else |
855 | 0 | n = (sizeof "active") - 1; |
856 | 0 | if (mtd->filter != NULL && w - 2 >= strlen(text) + 10 + n + 2) { |
857 | 0 | screen_write_puts(&ctx, &gc0, " (filter: "); |
858 | 0 | if (mtd->no_matches) |
859 | 0 | screen_write_puts(&ctx, &gc, "no matches"); |
860 | 0 | else |
861 | 0 | screen_write_puts(&ctx, &gc0, "active"); |
862 | 0 | screen_write_puts(&ctx, &gc0, ") "); |
863 | 0 | } else |
864 | 0 | screen_write_puts(&ctx, &gc0, " "); |
865 | 0 | } |
866 | 0 | free(text); |
867 | |
|
868 | 0 | box_x = w - 4; |
869 | 0 | box_y = sy - h - 2; |
870 | |
|
871 | 0 | if (box_x != 0 && box_y != 0) { |
872 | 0 | screen_write_cursormove(&ctx, 2, h + 1, 0); |
873 | 0 | mtd->drawcb(mtd->modedata, mti->itemdata, &ctx, box_x, box_y); |
874 | 0 | } |
875 | |
|
876 | 0 | done: |
877 | 0 | screen_write_cursormove(&ctx, 0, mtd->current - mtd->offset, 0); |
878 | 0 | screen_write_stop(&ctx); |
879 | 0 | } |
880 | | |
881 | | static struct mode_tree_item * |
882 | | mode_tree_search_backward(struct mode_tree_data *mtd) |
883 | 0 | { |
884 | 0 | struct mode_tree_item *mti, *last, *prev; |
885 | |
|
886 | 0 | if (mtd->search == NULL) |
887 | 0 | return (NULL); |
888 | | |
889 | 0 | mti = last = mtd->line_list[mtd->current].item; |
890 | 0 | for (;;) { |
891 | 0 | if ((prev = TAILQ_PREV(mti, mode_tree_list, entry)) != NULL) { |
892 | | /* Point to the last child in the previous subtree. */ |
893 | 0 | while (!TAILQ_EMPTY(&prev->children)) |
894 | 0 | prev = TAILQ_LAST(&prev->children, mode_tree_list); |
895 | 0 | mti = prev; |
896 | 0 | } else { |
897 | | /* If prev is NULL, jump to the parent. */ |
898 | 0 | mti = mti->parent; |
899 | 0 | } |
900 | |
|
901 | 0 | if (mti == NULL) { |
902 | | /* Point to the last child in the last root subtree. */ |
903 | 0 | prev = TAILQ_LAST(&mtd->children, mode_tree_list); |
904 | 0 | while (!TAILQ_EMPTY(&prev->children)) |
905 | 0 | prev = TAILQ_LAST(&prev->children, mode_tree_list); |
906 | 0 | mti = prev; |
907 | 0 | } |
908 | 0 | if (mti == last) |
909 | 0 | break; |
910 | | |
911 | 0 | if (mtd->searchcb == NULL) { |
912 | 0 | if (strstr(mti->name, mtd->search) != NULL) |
913 | 0 | return (mti); |
914 | 0 | continue; |
915 | 0 | } |
916 | 0 | if (mtd->searchcb(mtd->modedata, mti->itemdata, mtd->search)) |
917 | 0 | return (mti); |
918 | 0 | } |
919 | 0 | return (NULL); |
920 | 0 | } |
921 | | |
922 | | |
923 | | static struct mode_tree_item * |
924 | | mode_tree_search_forward(struct mode_tree_data *mtd) |
925 | 0 | { |
926 | 0 | struct mode_tree_item *mti, *last, *next; |
927 | |
|
928 | 0 | if (mtd->search == NULL) |
929 | 0 | return (NULL); |
930 | | |
931 | 0 | mti = last = mtd->line_list[mtd->current].item; |
932 | 0 | for (;;) { |
933 | 0 | if (!TAILQ_EMPTY(&mti->children)) |
934 | 0 | mti = TAILQ_FIRST(&mti->children); |
935 | 0 | else if ((next = TAILQ_NEXT(mti, entry)) != NULL) |
936 | 0 | mti = next; |
937 | 0 | else { |
938 | 0 | for (;;) { |
939 | 0 | mti = mti->parent; |
940 | 0 | if (mti == NULL) |
941 | 0 | break; |
942 | 0 | if ((next = TAILQ_NEXT(mti, entry)) != NULL) { |
943 | 0 | mti = next; |
944 | 0 | break; |
945 | 0 | } |
946 | 0 | } |
947 | 0 | } |
948 | 0 | if (mti == NULL) |
949 | 0 | mti = TAILQ_FIRST(&mtd->children); |
950 | 0 | if (mti == last) |
951 | 0 | break; |
952 | | |
953 | 0 | if (mtd->searchcb == NULL) { |
954 | 0 | if (strstr(mti->name, mtd->search) != NULL) |
955 | 0 | return (mti); |
956 | 0 | continue; |
957 | 0 | } |
958 | 0 | if (mtd->searchcb(mtd->modedata, mti->itemdata, mtd->search)) |
959 | 0 | return (mti); |
960 | 0 | } |
961 | 0 | return (NULL); |
962 | 0 | } |
963 | | |
964 | | static void |
965 | | mode_tree_search_set(struct mode_tree_data *mtd) |
966 | 0 | { |
967 | 0 | struct mode_tree_item *mti, *loop; |
968 | 0 | uint64_t tag; |
969 | |
|
970 | 0 | if (mtd->search_dir == MODE_TREE_SEARCH_FORWARD) |
971 | 0 | mti = mode_tree_search_forward(mtd); |
972 | 0 | else |
973 | 0 | mti = mode_tree_search_backward(mtd); |
974 | 0 | if (mti == NULL) |
975 | 0 | return; |
976 | 0 | tag = mti->tag; |
977 | |
|
978 | 0 | loop = mti->parent; |
979 | 0 | while (loop != NULL) { |
980 | 0 | loop->expanded = 1; |
981 | 0 | loop = loop->parent; |
982 | 0 | } |
983 | |
|
984 | 0 | mode_tree_build(mtd); |
985 | 0 | mode_tree_set_current(mtd, tag); |
986 | 0 | mode_tree_draw(mtd); |
987 | 0 | mtd->wp->flags |= PANE_REDRAW; |
988 | 0 | } |
989 | | |
990 | | static int |
991 | | mode_tree_search_callback(__unused struct client *c, void *data, const char *s, |
992 | | __unused int done) |
993 | 0 | { |
994 | 0 | struct mode_tree_data *mtd = data; |
995 | |
|
996 | 0 | if (mtd->dead) |
997 | 0 | return (0); |
998 | | |
999 | 0 | free(mtd->search); |
1000 | 0 | if (s == NULL || *s == '\0') { |
1001 | 0 | mtd->search = NULL; |
1002 | 0 | return (0); |
1003 | 0 | } |
1004 | 0 | mtd->search = xstrdup(s); |
1005 | 0 | mode_tree_search_set(mtd); |
1006 | |
|
1007 | 0 | return (0); |
1008 | 0 | } |
1009 | | |
1010 | | static void |
1011 | | mode_tree_search_free(void *data) |
1012 | 0 | { |
1013 | 0 | mode_tree_remove_ref(data); |
1014 | 0 | } |
1015 | | |
1016 | | static int |
1017 | | mode_tree_filter_callback(__unused struct client *c, void *data, const char *s, |
1018 | | __unused int done) |
1019 | 0 | { |
1020 | 0 | struct mode_tree_data *mtd = data; |
1021 | |
|
1022 | 0 | if (mtd->dead) |
1023 | 0 | return (0); |
1024 | | |
1025 | 0 | if (mtd->filter != NULL) |
1026 | 0 | free(mtd->filter); |
1027 | 0 | if (s == NULL || *s == '\0') |
1028 | 0 | mtd->filter = NULL; |
1029 | 0 | else |
1030 | 0 | mtd->filter = xstrdup(s); |
1031 | |
|
1032 | 0 | mode_tree_build(mtd); |
1033 | 0 | mode_tree_draw(mtd); |
1034 | 0 | mtd->wp->flags |= PANE_REDRAW; |
1035 | |
|
1036 | 0 | return (0); |
1037 | 0 | } |
1038 | | |
1039 | | static void |
1040 | | mode_tree_filter_free(void *data) |
1041 | 0 | { |
1042 | 0 | mode_tree_remove_ref(data); |
1043 | 0 | } |
1044 | | |
1045 | | static void |
1046 | | mode_tree_menu_callback(__unused struct menu *menu, __unused u_int idx, |
1047 | | key_code key, void *data) |
1048 | 0 | { |
1049 | 0 | struct mode_tree_menu *mtm = data; |
1050 | 0 | struct mode_tree_data *mtd = mtm->data; |
1051 | |
|
1052 | 0 | if (mtd->dead || key == KEYC_NONE) |
1053 | 0 | goto out; |
1054 | | |
1055 | 0 | if (mtm->line >= mtd->line_size) |
1056 | 0 | goto out; |
1057 | 0 | mtd->current = mtm->line; |
1058 | 0 | mtd->menucb(mtd->modedata, mtm->c, key); |
1059 | |
|
1060 | 0 | out: |
1061 | 0 | mode_tree_remove_ref(mtd); |
1062 | 0 | free(mtm); |
1063 | 0 | } |
1064 | | |
1065 | | static void |
1066 | | mode_tree_display_menu(struct mode_tree_data *mtd, struct client *c, u_int x, |
1067 | | u_int y, int outside) |
1068 | 0 | { |
1069 | 0 | struct mode_tree_item *mti; |
1070 | 0 | struct menu *menu; |
1071 | 0 | const struct menu_item *items; |
1072 | 0 | struct mode_tree_menu *mtm; |
1073 | 0 | char *title; |
1074 | 0 | u_int line; |
1075 | |
|
1076 | 0 | if (mtd->offset + y > mtd->line_size - 1) |
1077 | 0 | line = mtd->current; |
1078 | 0 | else |
1079 | 0 | line = mtd->offset + y; |
1080 | 0 | mti = mtd->line_list[line].item; |
1081 | |
|
1082 | 0 | if (!outside) { |
1083 | 0 | items = mtd->menu; |
1084 | 0 | xasprintf(&title, "#[align=centre]%s", mti->name); |
1085 | 0 | } else { |
1086 | 0 | items = mode_tree_menu_items; |
1087 | 0 | title = xstrdup(""); |
1088 | 0 | } |
1089 | 0 | menu = menu_create(title); |
1090 | 0 | menu_add_items(menu, items, NULL, c, NULL); |
1091 | 0 | free(title); |
1092 | |
|
1093 | 0 | mtm = xmalloc(sizeof *mtm); |
1094 | 0 | mtm->data = mtd; |
1095 | 0 | mtm->c = c; |
1096 | 0 | mtm->line = line; |
1097 | 0 | mtd->references++; |
1098 | |
|
1099 | 0 | if (x >= (menu->width + 4) / 2) |
1100 | 0 | x -= (menu->width + 4) / 2; |
1101 | 0 | else |
1102 | 0 | x = 0; |
1103 | 0 | if (menu_display(menu, 0, 0, NULL, x, y, c, BOX_LINES_DEFAULT, NULL, |
1104 | 0 | NULL, NULL, NULL, mode_tree_menu_callback, mtm) != 0) { |
1105 | 0 | mode_tree_remove_ref(mtd); |
1106 | 0 | free(mtm); |
1107 | 0 | menu_free(menu); |
1108 | 0 | } |
1109 | 0 | } |
1110 | | |
1111 | | int |
1112 | | mode_tree_key(struct mode_tree_data *mtd, struct client *c, key_code *key, |
1113 | | struct mouse_event *m, u_int *xp, u_int *yp) |
1114 | 0 | { |
1115 | 0 | struct mode_tree_line *line; |
1116 | 0 | struct mode_tree_item *current, *parent, *mti; |
1117 | 0 | u_int i, x, y; |
1118 | 0 | int choice; |
1119 | |
|
1120 | 0 | if (KEYC_IS_MOUSE(*key) && m != NULL) { |
1121 | 0 | if (cmd_mouse_at(mtd->wp, m, &x, &y, 0) != 0) { |
1122 | 0 | *key = KEYC_NONE; |
1123 | 0 | return (0); |
1124 | 0 | } |
1125 | 0 | if (xp != NULL) |
1126 | 0 | *xp = x; |
1127 | 0 | if (yp != NULL) |
1128 | 0 | *yp = y; |
1129 | 0 | if (x > mtd->width || y > mtd->height) { |
1130 | 0 | if (*key == KEYC_MOUSEDOWN3_PANE) |
1131 | 0 | mode_tree_display_menu(mtd, c, x, y, 1); |
1132 | 0 | if (mtd->preview == MODE_TREE_PREVIEW_OFF) |
1133 | 0 | *key = KEYC_NONE; |
1134 | 0 | return (0); |
1135 | 0 | } |
1136 | 0 | if (mtd->offset + y < mtd->line_size) { |
1137 | 0 | if (*key == KEYC_MOUSEDOWN1_PANE || |
1138 | 0 | *key == KEYC_MOUSEDOWN3_PANE || |
1139 | 0 | *key == KEYC_DOUBLECLICK1_PANE) |
1140 | 0 | mtd->current = mtd->offset + y; |
1141 | 0 | if (*key == KEYC_DOUBLECLICK1_PANE) |
1142 | 0 | *key = '\r'; |
1143 | 0 | else { |
1144 | 0 | if (*key == KEYC_MOUSEDOWN3_PANE) |
1145 | 0 | mode_tree_display_menu(mtd, c, x, y, 0); |
1146 | 0 | *key = KEYC_NONE; |
1147 | 0 | } |
1148 | 0 | } else { |
1149 | 0 | if (*key == KEYC_MOUSEDOWN3_PANE) |
1150 | 0 | mode_tree_display_menu(mtd, c, x, y, 0); |
1151 | 0 | *key = KEYC_NONE; |
1152 | 0 | } |
1153 | 0 | return (0); |
1154 | 0 | } |
1155 | | |
1156 | 0 | line = &mtd->line_list[mtd->current]; |
1157 | 0 | current = line->item; |
1158 | |
|
1159 | 0 | choice = -1; |
1160 | 0 | for (i = 0; i < mtd->line_size; i++) { |
1161 | 0 | if (*key == mtd->line_list[i].item->key) { |
1162 | 0 | choice = i; |
1163 | 0 | break; |
1164 | 0 | } |
1165 | 0 | } |
1166 | 0 | if (choice != -1) { |
1167 | 0 | if ((u_int)choice > mtd->line_size - 1) { |
1168 | 0 | *key = KEYC_NONE; |
1169 | 0 | return (0); |
1170 | 0 | } |
1171 | 0 | mtd->current = choice; |
1172 | 0 | *key = '\r'; |
1173 | 0 | return (0); |
1174 | 0 | } |
1175 | | |
1176 | 0 | switch (*key) { |
1177 | 0 | case 'q': |
1178 | 0 | case '\033': /* Escape */ |
1179 | 0 | case 'g'|KEYC_CTRL: |
1180 | 0 | return (1); |
1181 | 0 | case KEYC_UP: |
1182 | 0 | case 'k': |
1183 | 0 | case KEYC_WHEELUP_PANE: |
1184 | 0 | case 'p'|KEYC_CTRL: |
1185 | 0 | mode_tree_up(mtd, 1); |
1186 | 0 | break; |
1187 | 0 | case KEYC_DOWN: |
1188 | 0 | case 'j': |
1189 | 0 | case KEYC_WHEELDOWN_PANE: |
1190 | 0 | case 'n'|KEYC_CTRL: |
1191 | 0 | mode_tree_down(mtd, 1); |
1192 | 0 | break; |
1193 | 0 | case KEYC_UP|KEYC_SHIFT: |
1194 | 0 | case 'K': |
1195 | 0 | mode_tree_swap(mtd, -1); |
1196 | 0 | break; |
1197 | 0 | case KEYC_DOWN|KEYC_SHIFT: |
1198 | 0 | case 'J': |
1199 | 0 | mode_tree_swap(mtd, 1); |
1200 | 0 | break; |
1201 | 0 | case KEYC_PPAGE: |
1202 | 0 | case 'b'|KEYC_CTRL: |
1203 | 0 | for (i = 0; i < mtd->height; i++) { |
1204 | 0 | if (mtd->current == 0) |
1205 | 0 | break; |
1206 | 0 | mode_tree_up(mtd, 1); |
1207 | 0 | } |
1208 | 0 | break; |
1209 | 0 | case KEYC_NPAGE: |
1210 | 0 | case 'f'|KEYC_CTRL: |
1211 | 0 | for (i = 0; i < mtd->height; i++) { |
1212 | 0 | if (mtd->current == mtd->line_size - 1) |
1213 | 0 | break; |
1214 | 0 | mode_tree_down(mtd, 1); |
1215 | 0 | } |
1216 | 0 | break; |
1217 | 0 | case 'g': |
1218 | 0 | case KEYC_HOME: |
1219 | 0 | mtd->current = 0; |
1220 | 0 | mtd->offset = 0; |
1221 | 0 | break; |
1222 | 0 | case 'G': |
1223 | 0 | case KEYC_END: |
1224 | 0 | mtd->current = mtd->line_size - 1; |
1225 | 0 | if (mtd->current > mtd->height - 1) |
1226 | 0 | mtd->offset = mtd->current - mtd->height + 1; |
1227 | 0 | else |
1228 | 0 | mtd->offset = 0; |
1229 | 0 | break; |
1230 | 0 | case 't': |
1231 | | /* |
1232 | | * Do not allow parents and children to both be tagged: untag |
1233 | | * all parents and children of current. |
1234 | | */ |
1235 | 0 | if (current->no_tag) |
1236 | 0 | break; |
1237 | 0 | if (!current->tagged) { |
1238 | 0 | parent = current->parent; |
1239 | 0 | while (parent != NULL) { |
1240 | 0 | parent->tagged = 0; |
1241 | 0 | parent = parent->parent; |
1242 | 0 | } |
1243 | 0 | mode_tree_clear_tagged(¤t->children); |
1244 | 0 | current->tagged = 1; |
1245 | 0 | } else |
1246 | 0 | current->tagged = 0; |
1247 | 0 | if (m != NULL) |
1248 | 0 | mode_tree_down(mtd, 0); |
1249 | 0 | break; |
1250 | 0 | case 'T': |
1251 | 0 | for (i = 0; i < mtd->line_size; i++) |
1252 | 0 | mtd->line_list[i].item->tagged = 0; |
1253 | 0 | break; |
1254 | 0 | case 't'|KEYC_CTRL: |
1255 | 0 | for (i = 0; i < mtd->line_size; i++) { |
1256 | 0 | if ((mtd->line_list[i].item->parent == NULL && |
1257 | 0 | !mtd->line_list[i].item->no_tag) || |
1258 | 0 | (mtd->line_list[i].item->parent != NULL && |
1259 | 0 | mtd->line_list[i].item->parent->no_tag)) |
1260 | 0 | mtd->line_list[i].item->tagged = 1; |
1261 | 0 | else |
1262 | 0 | mtd->line_list[i].item->tagged = 0; |
1263 | 0 | } |
1264 | 0 | break; |
1265 | 0 | case 'O': |
1266 | 0 | mtd->sort_crit.field++; |
1267 | 0 | if (mtd->sort_crit.field >= mtd->sort_size) |
1268 | 0 | mtd->sort_crit.field = 0; |
1269 | 0 | mode_tree_build(mtd); |
1270 | 0 | break; |
1271 | 0 | case 'r': |
1272 | 0 | mtd->sort_crit.reversed = !mtd->sort_crit.reversed; |
1273 | 0 | mode_tree_build(mtd); |
1274 | 0 | break; |
1275 | 0 | case KEYC_LEFT: |
1276 | 0 | case 'h': |
1277 | 0 | case '-': |
1278 | 0 | if (line->flat || !current->expanded) |
1279 | 0 | current = current->parent; |
1280 | 0 | if (current == NULL) |
1281 | 0 | mode_tree_up(mtd, 0); |
1282 | 0 | else { |
1283 | 0 | current->expanded = 0; |
1284 | 0 | mtd->current = current->line; |
1285 | 0 | mode_tree_build(mtd); |
1286 | 0 | } |
1287 | 0 | break; |
1288 | 0 | case KEYC_RIGHT: |
1289 | 0 | case 'l': |
1290 | 0 | case '+': |
1291 | 0 | if (line->flat || current->expanded) |
1292 | 0 | mode_tree_down(mtd, 0); |
1293 | 0 | else if (!line->flat) { |
1294 | 0 | current->expanded = 1; |
1295 | 0 | mode_tree_build(mtd); |
1296 | 0 | } |
1297 | 0 | break; |
1298 | 0 | case '-'|KEYC_META: |
1299 | 0 | TAILQ_FOREACH(mti, &mtd->children, entry) |
1300 | 0 | mti->expanded = 0; |
1301 | 0 | mode_tree_build(mtd); |
1302 | 0 | break; |
1303 | 0 | case '+'|KEYC_META: |
1304 | 0 | TAILQ_FOREACH(mti, &mtd->children, entry) |
1305 | 0 | mti->expanded = 1; |
1306 | 0 | mode_tree_build(mtd); |
1307 | 0 | break; |
1308 | 0 | case '?': |
1309 | 0 | case '/': |
1310 | 0 | case 's'|KEYC_CTRL: |
1311 | 0 | mtd->references++; |
1312 | 0 | status_prompt_set(c, NULL, "(search) ", "", |
1313 | 0 | mode_tree_search_callback, mode_tree_search_free, mtd, |
1314 | 0 | PROMPT_NOFORMAT, PROMPT_TYPE_SEARCH); |
1315 | 0 | break; |
1316 | 0 | case 'n': |
1317 | 0 | mtd->search_dir = MODE_TREE_SEARCH_FORWARD; |
1318 | 0 | mode_tree_search_set(mtd); |
1319 | 0 | break; |
1320 | 0 | case 'N': |
1321 | 0 | mtd->search_dir = MODE_TREE_SEARCH_BACKWARD; |
1322 | 0 | mode_tree_search_set(mtd); |
1323 | 0 | break; |
1324 | 0 | case 'f': |
1325 | 0 | mtd->references++; |
1326 | 0 | status_prompt_set(c, NULL, "(filter) ", mtd->filter, |
1327 | 0 | mode_tree_filter_callback, mode_tree_filter_free, mtd, |
1328 | 0 | PROMPT_NOFORMAT, PROMPT_TYPE_SEARCH); |
1329 | 0 | break; |
1330 | 0 | case 'v': |
1331 | 0 | switch (mtd->preview) { |
1332 | 0 | case MODE_TREE_PREVIEW_OFF: |
1333 | 0 | mtd->preview = MODE_TREE_PREVIEW_BIG; |
1334 | 0 | break; |
1335 | 0 | case MODE_TREE_PREVIEW_NORMAL: |
1336 | 0 | mtd->preview = MODE_TREE_PREVIEW_OFF; |
1337 | 0 | break; |
1338 | 0 | case MODE_TREE_PREVIEW_BIG: |
1339 | 0 | mtd->preview = MODE_TREE_PREVIEW_NORMAL; |
1340 | 0 | break; |
1341 | 0 | } |
1342 | 0 | mode_tree_build(mtd); |
1343 | 0 | if (mtd->preview != MODE_TREE_PREVIEW_OFF) |
1344 | 0 | mode_tree_check_selected(mtd); |
1345 | 0 | break; |
1346 | 0 | } |
1347 | 0 | return (0); |
1348 | 0 | } |
1349 | | |
1350 | | void |
1351 | | mode_tree_run_command(struct client *c, struct cmd_find_state *fs, |
1352 | | const char *template, const char *name) |
1353 | 0 | { |
1354 | 0 | struct cmdq_state *state; |
1355 | 0 | char *command, *error; |
1356 | 0 | enum cmd_parse_status status; |
1357 | |
|
1358 | 0 | command = cmd_template_replace(template, name, 1); |
1359 | 0 | if (command != NULL && *command != '\0') { |
1360 | 0 | state = cmdq_new_state(fs, NULL, 0); |
1361 | 0 | status = cmd_parse_and_append(command, NULL, c, state, &error); |
1362 | 0 | if (status == CMD_PARSE_ERROR) { |
1363 | 0 | if (c != NULL) { |
1364 | 0 | *error = toupper((u_char)*error); |
1365 | 0 | status_message_set(c, -1, 1, 0, 0, "%s", error); |
1366 | 0 | } |
1367 | 0 | free(error); |
1368 | 0 | } |
1369 | 0 | cmdq_free_state(state); |
1370 | 0 | } |
1371 | 0 | free(command); |
1372 | 0 | } |