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