Annotation of src/usr.bin/tmux/mode-tree.c, Revision 1.43
1.43 ! nicm 1: /* $OpenBSD: mode-tree.c,v 1.42 2020/05/16 15:01:31 nicm Exp $ */
1.1 nicm 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: struct mode_tree_item;
29: TAILQ_HEAD(mode_tree_list, mode_tree_item);
30:
31: struct mode_tree_data {
1.3 nicm 32: int dead;
33: u_int references;
1.23 nicm 34: int zoomed;
1.3 nicm 35:
1.1 nicm 36: struct window_pane *wp;
37: void *modedata;
1.33 nicm 38: const struct menu_item *menu;
1.1 nicm 39:
40: const char **sort_list;
41: u_int sort_size;
1.36 nicm 42: struct mode_tree_sort_criteria sort_crit;
1.1 nicm 43:
1.17 nicm 44: mode_tree_build_cb buildcb;
45: mode_tree_draw_cb drawcb;
46: mode_tree_search_cb searchcb;
1.28 nicm 47: mode_tree_menu_cb menucb;
1.1 nicm 48:
49: struct mode_tree_list children;
50: struct mode_tree_list saved;
51:
52: struct mode_tree_line *line_list;
53: u_int line_size;
54:
55: u_int depth;
56:
57: u_int width;
58: u_int height;
59:
60: u_int offset;
61: u_int current;
62:
63: struct screen screen;
1.3 nicm 64:
1.9 nicm 65: int preview;
1.6 nicm 66: char *search;
67: char *filter;
1.21 nicm 68: int no_matches;
1.1 nicm 69: };
70:
71: struct mode_tree_item {
72: struct mode_tree_item *parent;
73: void *itemdata;
74: u_int line;
75:
76: uint64_t tag;
77: const char *name;
78: const char *text;
79:
80: int expanded;
81: int tagged;
82:
83: struct mode_tree_list children;
84: TAILQ_ENTRY(mode_tree_item) entry;
85: };
86:
87: struct mode_tree_line {
88: struct mode_tree_item *item;
89: u_int depth;
90: int last;
91: int flat;
92: };
93:
1.28 nicm 94: struct mode_tree_menu {
95: struct mode_tree_data *data;
96: struct client *c;
97: u_int line;
98: void *itemdata;
99: };
100:
1.1 nicm 101: static void mode_tree_free_items(struct mode_tree_list *);
102:
1.33 nicm 103: static const struct menu_item mode_tree_menu_items[] = {
104: { "Scroll Left", '<', NULL },
105: { "Scroll Right", '>', NULL },
106: { "", KEYC_NONE, NULL },
107: { "Cancel", 'q', NULL },
108:
109: { NULL, KEYC_NONE, NULL }
110: };
1.28 nicm 111:
1.1 nicm 112: static struct mode_tree_item *
113: mode_tree_find_item(struct mode_tree_list *mtl, uint64_t tag)
114: {
115: struct mode_tree_item *mti, *child;
116:
117: TAILQ_FOREACH(mti, mtl, entry) {
118: if (mti->tag == tag)
119: return (mti);
120: child = mode_tree_find_item(&mti->children, tag);
121: if (child != NULL)
122: return (child);
123: }
124: return (NULL);
125: }
126:
127: static void
128: mode_tree_free_item(struct mode_tree_item *mti)
129: {
130: mode_tree_free_items(&mti->children);
131:
132: free((void *)mti->name);
133: free((void *)mti->text);
134:
135: free(mti);
136: }
137:
138: static void
139: mode_tree_free_items(struct mode_tree_list *mtl)
140: {
141: struct mode_tree_item *mti, *mti1;
142:
143: TAILQ_FOREACH_SAFE(mti, mtl, entry, mti1) {
144: TAILQ_REMOVE(mtl, mti, entry);
145: mode_tree_free_item(mti);
146: }
147: }
148:
149: static void
1.11 nicm 150: mode_tree_check_selected(struct mode_tree_data *mtd)
151: {
152: /*
153: * If the current line would now be off screen reset the offset to the
154: * last visible line.
155: */
156: if (mtd->current > mtd->height - 1)
157: mtd->offset = mtd->current - mtd->height + 1;
158: }
159:
160: static void
1.1 nicm 161: mode_tree_clear_lines(struct mode_tree_data *mtd)
162: {
163: free(mtd->line_list);
164: mtd->line_list = NULL;
165: mtd->line_size = 0;
166: }
167:
168: static void
169: mode_tree_build_lines(struct mode_tree_data *mtd,
170: struct mode_tree_list *mtl, u_int depth)
171: {
172: struct mode_tree_item *mti;
173: struct mode_tree_line *line;
174: u_int i;
175: int flat = 1;
176:
177: mtd->depth = depth;
178: TAILQ_FOREACH(mti, mtl, entry) {
179: mtd->line_list = xreallocarray(mtd->line_list,
180: mtd->line_size + 1, sizeof *mtd->line_list);
181:
182: line = &mtd->line_list[mtd->line_size++];
183: line->item = mti;
184: line->depth = depth;
185: line->last = (mti == TAILQ_LAST(mtl, mode_tree_list));
186:
187: mti->line = (mtd->line_size - 1);
188: if (!TAILQ_EMPTY(&mti->children))
189: flat = 0;
190: if (mti->expanded)
191: mode_tree_build_lines(mtd, &mti->children, depth + 1);
192: }
193: TAILQ_FOREACH(mti, mtl, entry) {
194: for (i = 0; i < mtd->line_size; i++) {
195: line = &mtd->line_list[i];
196: if (line->item == mti)
197: line->flat = flat;
198: }
199: }
200: }
201:
202: static void
203: mode_tree_clear_tagged(struct mode_tree_list *mtl)
204: {
205: struct mode_tree_item *mti;
206:
207: TAILQ_FOREACH(mti, mtl, entry) {
208: mti->tagged = 0;
209: mode_tree_clear_tagged(&mti->children);
210: }
211: }
212:
1.24 nicm 213: static void
1.1 nicm 214: mode_tree_up(struct mode_tree_data *mtd, int wrap)
215: {
216: if (mtd->current == 0) {
217: if (wrap) {
218: mtd->current = mtd->line_size - 1;
219: if (mtd->line_size >= mtd->height)
220: mtd->offset = mtd->line_size - mtd->height;
221: }
222: } else {
223: mtd->current--;
224: if (mtd->current < mtd->offset)
225: mtd->offset--;
226: }
227: }
228:
229: void
230: mode_tree_down(struct mode_tree_data *mtd, int wrap)
231: {
232: if (mtd->current == mtd->line_size - 1) {
233: if (wrap) {
234: mtd->current = 0;
235: mtd->offset = 0;
236: }
237: } else {
238: mtd->current++;
239: if (mtd->current > mtd->offset + mtd->height - 1)
240: mtd->offset++;
241: }
242: }
243:
244: void *
245: mode_tree_get_current(struct mode_tree_data *mtd)
246: {
247: return (mtd->line_list[mtd->current].item->itemdata);
248: }
249:
1.19 nicm 250: void
251: mode_tree_expand_current(struct mode_tree_data *mtd)
252: {
253: if (!mtd->line_list[mtd->current].item->expanded) {
254: mtd->line_list[mtd->current].item->expanded = 1;
255: mode_tree_build(mtd);
256: }
257: }
258:
1.41 nicm 259: static int
260: mode_tree_get_tag(struct mode_tree_data *mtd, uint64_t tag, u_int *found)
1.19 nicm 261: {
262: u_int i;
263:
264: for (i = 0; i < mtd->line_size; i++) {
265: if (mtd->line_list[i].item->tag == tag)
266: break;
267: }
268: if (i != mtd->line_size) {
1.41 nicm 269: *found = i;
270: return (1);
271: }
272: return (0);
273: }
274:
275: void
276: mode_tree_expand(struct mode_tree_data *mtd, uint64_t tag)
277: {
278: u_int found;
279:
280: if (!mode_tree_get_tag(mtd, tag, &found))
281: return;
282: if (!mtd->line_list[found].item->expanded) {
283: mtd->line_list[found].item->expanded = 1;
284: mode_tree_build(mtd);
285: }
286: }
287:
288: int
289: mode_tree_set_current(struct mode_tree_data *mtd, uint64_t tag)
290: {
291: u_int found;
292:
293: if (mode_tree_get_tag(mtd, tag, &found)) {
294: mtd->current = found;
1.19 nicm 295: if (mtd->current > mtd->height - 1)
296: mtd->offset = mtd->current - mtd->height + 1;
297: else
298: mtd->offset = 0;
1.41 nicm 299: return (1);
1.19 nicm 300: }
1.41 nicm 301: mtd->current = 0;
302: mtd->offset = 0;
303: return (0);
1.19 nicm 304: }
305:
1.1 nicm 306: u_int
307: mode_tree_count_tagged(struct mode_tree_data *mtd)
308: {
309: struct mode_tree_item *mti;
310: u_int i, tagged;
311:
312: tagged = 0;
313: for (i = 0; i < mtd->line_size; i++) {
314: mti = mtd->line_list[i].item;
315: if (mti->tagged)
316: tagged++;
317: }
318: return (tagged);
319: }
320:
321: void
1.17 nicm 322: mode_tree_each_tagged(struct mode_tree_data *mtd, mode_tree_each_cb cb,
323: struct client *c, key_code key, int current)
1.1 nicm 324: {
325: struct mode_tree_item *mti;
326: u_int i;
327: int fired;
328:
329: fired = 0;
330: for (i = 0; i < mtd->line_size; i++) {
331: mti = mtd->line_list[i].item;
332: if (mti->tagged) {
333: fired = 1;
1.14 nicm 334: cb(mtd->modedata, mti->itemdata, c, key);
1.1 nicm 335: }
336: }
337: if (!fired && current) {
338: mti = mtd->line_list[mtd->current].item;
1.14 nicm 339: cb(mtd->modedata, mti->itemdata, c, key);
1.1 nicm 340: }
341: }
342:
343: struct mode_tree_data *
1.5 nicm 344: mode_tree_start(struct window_pane *wp, struct args *args,
1.17 nicm 345: mode_tree_build_cb buildcb, mode_tree_draw_cb drawcb,
1.28 nicm 346: mode_tree_search_cb searchcb, mode_tree_menu_cb menucb, void *modedata,
1.33 nicm 347: const struct menu_item *menu, const char **sort_list, u_int sort_size,
1.28 nicm 348: struct screen **s)
1.1 nicm 349: {
350: struct mode_tree_data *mtd;
1.5 nicm 351: const char *sort;
352: u_int i;
1.1 nicm 353:
354: mtd = xcalloc(1, sizeof *mtd);
1.3 nicm 355: mtd->references = 1;
356:
1.1 nicm 357: mtd->wp = wp;
358: mtd->modedata = modedata;
1.28 nicm 359: mtd->menu = menu;
1.1 nicm 360:
361: mtd->sort_list = sort_list;
362: mtd->sort_size = sort_size;
1.5 nicm 363:
1.9 nicm 364: mtd->preview = !args_has(args, 'N');
365:
1.5 nicm 366: sort = args_get(args, 'O');
367: if (sort != NULL) {
368: for (i = 0; i < sort_size; i++) {
369: if (strcasecmp(sort, sort_list[i]) == 0)
1.36 nicm 370: mtd->sort_crit.field = i;
1.5 nicm 371: }
372: }
1.36 nicm 373: mtd->sort_crit.reversed = args_has(args, 'r');
1.1 nicm 374:
1.6 nicm 375: if (args_has(args, 'f'))
376: mtd->filter = xstrdup(args_get(args, 'f'));
377: else
378: mtd->filter = NULL;
379:
1.1 nicm 380: mtd->buildcb = buildcb;
381: mtd->drawcb = drawcb;
1.3 nicm 382: mtd->searchcb = searchcb;
1.28 nicm 383: mtd->menucb = menucb;
1.1 nicm 384:
385: TAILQ_INIT(&mtd->children);
386:
387: *s = &mtd->screen;
388: screen_init(*s, screen_size_x(&wp->base), screen_size_y(&wp->base), 0);
389: (*s)->mode &= ~MODE_CURSOR;
390:
391: return (mtd);
392: }
393:
394: void
1.23 nicm 395: mode_tree_zoom(struct mode_tree_data *mtd, struct args *args)
396: {
397: struct window_pane *wp = mtd->wp;
398:
399: if (args_has(args, 'Z')) {
400: mtd->zoomed = (wp->window->flags & WINDOW_ZOOMED);
401: if (!mtd->zoomed && window_zoom(wp) == 0)
402: server_redraw_window(wp->window);
403: } else
404: mtd->zoomed = -1;
405: }
406:
407: void
1.1 nicm 408: mode_tree_build(struct mode_tree_data *mtd)
409: {
410: struct screen *s = &mtd->screen;
411: uint64_t tag;
412:
413: if (mtd->line_list != NULL)
414: tag = mtd->line_list[mtd->current].item->tag;
415: else
1.32 nicm 416: tag = UINT64_MAX;
1.1 nicm 417:
418: TAILQ_CONCAT(&mtd->saved, &mtd->children, entry);
419: TAILQ_INIT(&mtd->children);
420:
1.36 nicm 421: mtd->buildcb(mtd->modedata, &mtd->sort_crit, &tag, mtd->filter);
1.21 nicm 422: mtd->no_matches = TAILQ_EMPTY(&mtd->children);
423: if (mtd->no_matches)
1.36 nicm 424: mtd->buildcb(mtd->modedata, &mtd->sort_crit, &tag, NULL);
1.1 nicm 425:
426: mode_tree_free_items(&mtd->saved);
427: TAILQ_INIT(&mtd->saved);
428:
429: mode_tree_clear_lines(mtd);
430: mode_tree_build_lines(mtd, &mtd->children, 0);
431:
1.32 nicm 432: if (tag == UINT64_MAX)
433: tag = mtd->line_list[mtd->current].item->tag;
1.3 nicm 434: mode_tree_set_current(mtd, tag);
1.1 nicm 435:
436: mtd->width = screen_size_x(s);
1.9 nicm 437: if (mtd->preview) {
438: mtd->height = (screen_size_y(s) / 3) * 2;
439: if (mtd->height > mtd->line_size)
440: mtd->height = screen_size_y(s) / 2;
441: if (mtd->height < 10)
442: mtd->height = screen_size_y(s);
443: if (screen_size_y(s) - mtd->height < 2)
444: mtd->height = screen_size_y(s);
445: } else
1.1 nicm 446: mtd->height = screen_size_y(s);
1.11 nicm 447: mode_tree_check_selected(mtd);
1.1 nicm 448: }
449:
1.3 nicm 450: static void
451: mode_tree_remove_ref(struct mode_tree_data *mtd)
452: {
453: if (--mtd->references == 0)
454: free(mtd);
455: }
456:
1.1 nicm 457: void
458: mode_tree_free(struct mode_tree_data *mtd)
459: {
1.23 nicm 460: struct window_pane *wp = mtd->wp;
461:
462: if (mtd->zoomed == 0)
463: server_unzoom_window(wp->window);
464:
1.1 nicm 465: mode_tree_free_items(&mtd->children);
466: mode_tree_clear_lines(mtd);
467: screen_free(&mtd->screen);
1.3 nicm 468:
1.6 nicm 469: free(mtd->search);
470: free(mtd->filter);
471:
1.3 nicm 472: mtd->dead = 1;
473: mode_tree_remove_ref(mtd);
1.1 nicm 474: }
475:
476: void
477: mode_tree_resize(struct mode_tree_data *mtd, u_int sx, u_int sy)
478: {
479: struct screen *s = &mtd->screen;
480:
481: screen_resize(s, sx, sy, 0);
482:
483: mode_tree_build(mtd);
484: mode_tree_draw(mtd);
485:
486: mtd->wp->flags |= PANE_REDRAW;
487: }
488:
489: struct mode_tree_item *
490: mode_tree_add(struct mode_tree_data *mtd, struct mode_tree_item *parent,
491: void *itemdata, uint64_t tag, const char *name, const char *text,
492: int expanded)
493: {
494: struct mode_tree_item *mti, *saved;
495:
496: log_debug("%s: %llu, %s %s", __func__, (unsigned long long)tag,
497: name, text);
498:
499: mti = xcalloc(1, sizeof *mti);
500: mti->parent = parent;
501: mti->itemdata = itemdata;
502:
503: mti->tag = tag;
504: mti->name = xstrdup(name);
505: mti->text = xstrdup(text);
506:
507: saved = mode_tree_find_item(&mtd->saved, tag);
508: if (saved != NULL) {
1.34 nicm 509: if (parent == NULL || parent->expanded)
1.1 nicm 510: mti->tagged = saved->tagged;
511: mti->expanded = saved->expanded;
512: } else if (expanded == -1)
513: mti->expanded = 1;
514: else
515: mti->expanded = expanded;
516:
517: TAILQ_INIT(&mti->children);
518:
519: if (parent != NULL)
520: TAILQ_INSERT_TAIL(&parent->children, mti, entry);
521: else
522: TAILQ_INSERT_TAIL(&mtd->children, mti, entry);
523:
524: return (mti);
525: }
526:
527: void
528: mode_tree_remove(struct mode_tree_data *mtd, struct mode_tree_item *mti)
529: {
530: struct mode_tree_item *parent = mti->parent;
531:
532: if (parent != NULL)
533: TAILQ_REMOVE(&parent->children, mti, entry);
534: else
535: TAILQ_REMOVE(&mtd->children, mti, entry);
536: mode_tree_free_item(mti);
537: }
538:
539: void
540: mode_tree_draw(struct mode_tree_data *mtd)
541: {
542: struct window_pane *wp = mtd->wp;
1.17 nicm 543: struct screen *s = &mtd->screen;
1.1 nicm 544: struct mode_tree_line *line;
545: struct mode_tree_item *mti;
546: struct options *oo = wp->window->options;
547: struct screen_write_ctx ctx;
548: struct grid_cell gc0, gc;
1.27 nicm 549: u_int w, h, i, j, sy, box_x, box_y, width;
1.1 nicm 550: char *text, *start, key[7];
551: const char *tag, *symbol;
1.21 nicm 552: size_t size, n;
1.1 nicm 553: int keylen;
554:
555: if (mtd->line_size == 0)
556: return;
557:
558: memcpy(&gc0, &grid_default_cell, sizeof gc0);
559: memcpy(&gc, &grid_default_cell, sizeof gc);
1.42 nicm 560: style_apply(&gc, oo, "mode-style", NULL);
1.1 nicm 561:
562: w = mtd->width;
563: h = mtd->height;
564:
1.43 ! nicm 565: screen_write_start(&ctx, s);
1.1 nicm 566: screen_write_clearscreen(&ctx, 8);
567:
568: if (mtd->line_size > 10)
569: keylen = 6;
570: else
571: keylen = 4;
572:
573: for (i = 0; i < mtd->line_size; i++) {
574: if (i < mtd->offset)
575: continue;
576: if (i > mtd->offset + h - 1)
577: break;
578:
579: line = &mtd->line_list[i];
580: mti = line->item;
581:
1.26 nicm 582: screen_write_cursormove(&ctx, 0, i - mtd->offset, 0);
1.1 nicm 583:
584: if (i < 10)
1.8 nicm 585: snprintf(key, sizeof key, "(%c) ", '0' + i);
1.1 nicm 586: else if (i < 36)
587: snprintf(key, sizeof key, "(M-%c)", 'a' + (i - 10));
588: else
589: *key = '\0';
590:
591: if (line->flat)
592: symbol = "";
593: else if (TAILQ_EMPTY(&mti->children))
594: symbol = " ";
595: else if (mti->expanded)
596: symbol = "- ";
597: else
598: symbol = "+ ";
599:
600: if (line->depth == 0)
601: start = xstrdup(symbol);
602: else {
603: size = (4 * line->depth) + 32;
604:
605: start = xcalloc(1, size);
606: for (j = 1; j < line->depth; j++) {
607: if (mti->parent != NULL &&
608: mtd->line_list[mti->parent->line].last)
609: strlcat(start, " ", size);
610: else
611: strlcat(start, "\001x\001 ", size);
612: }
613: if (line->last)
614: strlcat(start, "\001mq\001> ", size);
615: else
616: strlcat(start, "\001tq\001> ", size);
617: strlcat(start, symbol, size);
618: }
619:
620: if (mti->tagged)
621: tag = "*";
622: else
623: tag = "";
1.27 nicm 624: xasprintf(&text, "%-*s%s%s%s: ", keylen, key, start, mti->name,
625: tag);
626: width = utf8_cstrwidth(text);
1.37 nicm 627: if (width > w)
628: width = w;
1.1 nicm 629: free(start);
630:
631: if (mti->tagged) {
632: gc.attr ^= GRID_ATTR_BRIGHT;
633: gc0.attr ^= GRID_ATTR_BRIGHT;
634: }
635:
636: if (i != mtd->current) {
637: screen_write_clearendofline(&ctx, 8);
1.37 nicm 638: screen_write_nputs(&ctx, w, &gc0, "%s", text);
1.27 nicm 639: format_draw(&ctx, &gc0, w - width, mti->text, NULL);
1.13 nicm 640: } else {
641: screen_write_clearendofline(&ctx, gc.bg);
1.37 nicm 642: screen_write_nputs(&ctx, w, &gc, "%s", text);
1.27 nicm 643: format_draw(&ctx, &gc, w - width, mti->text, NULL);
1.13 nicm 644: }
1.1 nicm 645: free(text);
646:
647: if (mti->tagged) {
648: gc.attr ^= GRID_ATTR_BRIGHT;
649: gc0.attr ^= GRID_ATTR_BRIGHT;
650: }
651: }
652:
653: sy = screen_size_y(s);
1.9 nicm 654: if (!mtd->preview || sy <= 4 || h <= 4 || sy - h <= 4 || w <= 4) {
1.1 nicm 655: screen_write_stop(&ctx);
656: return;
657: }
658:
659: line = &mtd->line_list[mtd->current];
660: mti = line->item;
661:
1.26 nicm 662: screen_write_cursormove(&ctx, 0, h, 0);
1.1 nicm 663: screen_write_box(&ctx, w, sy - h);
664:
1.36 nicm 665: xasprintf(&text, " %s (sort: %s%s)", mti->name,
666: mtd->sort_list[mtd->sort_crit.field],
667: mtd->sort_crit.reversed ? ", reversed" : "");
1.1 nicm 668: if (w - 2 >= strlen(text)) {
1.26 nicm 669: screen_write_cursormove(&ctx, 1, h, 0);
1.1 nicm 670: screen_write_puts(&ctx, &gc0, "%s", text);
1.21 nicm 671:
672: if (mtd->no_matches)
673: n = (sizeof "no matches") - 1;
674: else
675: n = (sizeof "active") - 1;
676: if (mtd->filter != NULL && w - 2 >= strlen(text) + 10 + n + 2) {
677: screen_write_puts(&ctx, &gc0, " (filter: ");
678: if (mtd->no_matches)
679: screen_write_puts(&ctx, &gc, "no matches");
680: else
681: screen_write_puts(&ctx, &gc0, "active");
682: screen_write_puts(&ctx, &gc0, ") ");
683: }
1.1 nicm 684: }
685: free(text);
686:
687: box_x = w - 4;
688: box_y = sy - h - 2;
689:
1.17 nicm 690: if (box_x != 0 && box_y != 0) {
1.26 nicm 691: screen_write_cursormove(&ctx, 2, h + 1, 0);
1.17 nicm 692: mtd->drawcb(mtd->modedata, mti->itemdata, &ctx, box_x, box_y);
1.1 nicm 693: }
694:
695: screen_write_stop(&ctx);
696: }
697:
1.3 nicm 698: static struct mode_tree_item *
699: mode_tree_search_for(struct mode_tree_data *mtd)
700: {
701: struct mode_tree_item *mti, *last, *next;
702:
1.6 nicm 703: if (mtd->search == NULL)
1.3 nicm 704: return (NULL);
705:
706: mti = last = mtd->line_list[mtd->current].item;
707: for (;;) {
708: if (!TAILQ_EMPTY(&mti->children))
709: mti = TAILQ_FIRST(&mti->children);
710: else if ((next = TAILQ_NEXT(mti, entry)) != NULL)
711: mti = next;
712: else {
713: for (;;) {
714: mti = mti->parent;
715: if (mti == NULL)
716: break;
717: if ((next = TAILQ_NEXT(mti, entry)) != NULL) {
718: mti = next;
719: break;
720: }
721: }
722: }
723: if (mti == NULL)
724: mti = TAILQ_FIRST(&mtd->children);
725: if (mti == last)
726: break;
727:
728: if (mtd->searchcb == NULL) {
1.6 nicm 729: if (strstr(mti->name, mtd->search) != NULL)
1.3 nicm 730: return (mti);
731: continue;
732: }
1.6 nicm 733: if (mtd->searchcb(mtd->modedata, mti->itemdata, mtd->search))
1.4 nicm 734: return (mti);
1.3 nicm 735: }
736: return (NULL);
737: }
738:
739: static void
740: mode_tree_search_set(struct mode_tree_data *mtd)
741: {
742: struct mode_tree_item *mti, *loop;
743: uint64_t tag;
744:
745: mti = mode_tree_search_for(mtd);
746: if (mti == NULL)
747: return;
748: tag = mti->tag;
749:
750: loop = mti->parent;
751: while (loop != NULL) {
752: loop->expanded = 1;
753: loop = loop->parent;
754: }
1.6 nicm 755:
1.3 nicm 756: mode_tree_build(mtd);
757: mode_tree_set_current(mtd, tag);
758: mode_tree_draw(mtd);
1.6 nicm 759: mtd->wp->flags |= PANE_REDRAW;
1.3 nicm 760: }
761:
762: static int
763: mode_tree_search_callback(__unused struct client *c, void *data, const char *s,
764: __unused int done)
765: {
766: struct mode_tree_data *mtd = data;
767:
768: if (mtd->dead)
769: return (0);
770:
1.6 nicm 771: free(mtd->search);
772: if (s == NULL || *s == '\0') {
773: mtd->search = NULL;
1.3 nicm 774: return (0);
775: }
1.6 nicm 776: mtd->search = xstrdup(s);
1.3 nicm 777: mode_tree_search_set(mtd);
778:
779: return (0);
780: }
781:
782: static void
783: mode_tree_search_free(void *data)
784: {
785: mode_tree_remove_ref(data);
786: }
787:
1.6 nicm 788: static int
789: mode_tree_filter_callback(__unused struct client *c, void *data, const char *s,
790: __unused int done)
791: {
792: struct mode_tree_data *mtd = data;
793:
794: if (mtd->dead)
795: return (0);
796:
797: if (mtd->filter != NULL)
798: free(mtd->filter);
799: if (s == NULL || *s == '\0')
800: mtd->filter = NULL;
801: else
802: mtd->filter = xstrdup(s);
803:
804: mode_tree_build(mtd);
805: mode_tree_draw(mtd);
806: mtd->wp->flags |= PANE_REDRAW;
807:
808: return (0);
809: }
810:
811: static void
812: mode_tree_filter_free(void *data)
813: {
814: mode_tree_remove_ref(data);
815: }
816:
1.28 nicm 817: static void
818: mode_tree_menu_callback(__unused struct menu *menu, __unused u_int idx,
819: key_code key, void *data)
820: {
821: struct mode_tree_menu *mtm = data;
822: struct mode_tree_data *mtd = mtm->data;
823: struct mode_tree_item *mti;
824:
825: if (mtd->dead || key == KEYC_NONE)
826: goto out;
827:
828: if (mtm->line >= mtd->line_size)
829: goto out;
830: mti = mtd->line_list[mtm->line].item;
831: if (mti->itemdata != mtm->itemdata)
832: goto out;
833: mtd->current = mtm->line;
834: mtd->menucb (mtd->modedata, mtm->c, key);
835:
836: out:
837: mode_tree_remove_ref(mtd);
838: free(mtm);
839: }
840:
841: static void
842: mode_tree_display_menu(struct mode_tree_data *mtd, struct client *c, u_int x,
843: u_int y, int outside)
844: {
845: struct mode_tree_item *mti;
846: struct menu *menu;
1.33 nicm 847: const struct menu_item *items;
1.28 nicm 848: struct mode_tree_menu *mtm;
849: char *title;
850: u_int line;
851:
852: if (mtd->offset + y > mtd->line_size - 1)
853: line = mtd->current;
854: else
855: line = mtd->offset + y;
1.29 nicm 856: mti = mtd->line_list[line].item;
1.28 nicm 857:
858: if (!outside) {
1.33 nicm 859: items = mtd->menu;
1.28 nicm 860: xasprintf(&title, "#[align=centre]%s", mti->name);
861: } else {
1.33 nicm 862: items = mode_tree_menu_items;
1.28 nicm 863: title = xstrdup("");
864: }
1.33 nicm 865: menu = menu_create(title);
866: menu_add_items(menu, items, NULL, NULL, NULL);
1.28 nicm 867: free(title);
868:
869: mtm = xmalloc(sizeof *mtm);
870: mtm->data = mtd;
871: mtm->c = c;
872: mtm->line = line;
873: mtm->itemdata = mti->itemdata;
874: mtd->references++;
875:
1.38 nicm 876: if (x >= (menu->width + 4) / 2)
877: x -= (menu->width + 4) / 2;
878: else
879: x = 0;
1.28 nicm 880: if (menu_display(menu, 0, NULL, x, y, c, NULL, mode_tree_menu_callback,
881: mtm) != 0)
882: menu_free(menu);
883: }
884:
1.1 nicm 885: int
1.3 nicm 886: mode_tree_key(struct mode_tree_data *mtd, struct client *c, key_code *key,
1.19 nicm 887: struct mouse_event *m, u_int *xp, u_int *yp)
1.1 nicm 888: {
889: struct mode_tree_line *line;
890: struct mode_tree_item *current, *parent;
891: u_int i, x, y;
892: int choice;
893: key_code tmp;
894:
1.28 nicm 895: if (KEYC_IS_MOUSE(*key) && m != NULL) {
1.1 nicm 896: if (cmd_mouse_at(mtd->wp, m, &x, &y, 0) != 0) {
897: *key = KEYC_NONE;
898: return (0);
899: }
1.19 nicm 900: if (xp != NULL)
901: *xp = x;
902: if (yp != NULL)
903: *yp = y;
1.1 nicm 904: if (x > mtd->width || y > mtd->height) {
1.28 nicm 905: if (*key == KEYC_MOUSEDOWN3_PANE)
906: mode_tree_display_menu(mtd, c, x, y, 1);
1.20 nicm 907: if (!mtd->preview)
1.19 nicm 908: *key = KEYC_NONE;
1.1 nicm 909: return (0);
910: }
911: if (mtd->offset + y < mtd->line_size) {
1.18 nicm 912: if (*key == KEYC_MOUSEDOWN1_PANE ||
1.28 nicm 913: *key == KEYC_MOUSEDOWN3_PANE ||
1.18 nicm 914: *key == KEYC_DOUBLECLICK1_PANE)
915: mtd->current = mtd->offset + y;
916: if (*key == KEYC_DOUBLECLICK1_PANE)
917: *key = '\r';
1.28 nicm 918: else {
919: if (*key == KEYC_MOUSEDOWN3_PANE)
920: mode_tree_display_menu(mtd, c, x, y, 0);
1.20 nicm 921: *key = KEYC_NONE;
1.28 nicm 922: }
923: } else {
924: if (*key == KEYC_MOUSEDOWN3_PANE)
925: mode_tree_display_menu(mtd, c, x, y, 0);
1.20 nicm 926: *key = KEYC_NONE;
1.28 nicm 927: }
1.20 nicm 928: return (0);
1.1 nicm 929: }
930:
931: line = &mtd->line_list[mtd->current];
932: current = line->item;
933:
934: choice = -1;
935: if (*key >= '0' && *key <= '9')
936: choice = (*key) - '0';
937: else if (((*key) & KEYC_MASK_MOD) == KEYC_ESCAPE) {
938: tmp = (*key) & KEYC_MASK_KEY;
939: if (tmp >= 'a' && tmp <= 'z')
940: choice = 10 + (tmp - 'a');
941: }
942: if (choice != -1) {
943: if ((u_int)choice > mtd->line_size - 1) {
944: *key = KEYC_NONE;
945: return (0);
946: }
947: mtd->current = choice;
948: *key = '\r';
949: return (0);
950: }
951:
952: switch (*key) {
953: case 'q':
954: case '\033': /* Escape */
1.22 nicm 955: case '\007': /* C-g */
1.1 nicm 956: return (1);
957: case KEYC_UP:
958: case 'k':
959: case KEYC_WHEELUP_PANE:
1.12 nicm 960: case '\020': /* C-p */
1.1 nicm 961: mode_tree_up(mtd, 1);
962: break;
963: case KEYC_DOWN:
964: case 'j':
965: case KEYC_WHEELDOWN_PANE:
1.12 nicm 966: case '\016': /* C-n */
1.1 nicm 967: mode_tree_down(mtd, 1);
968: break;
1.35 nicm 969: case 'g':
1.1 nicm 970: case KEYC_PPAGE:
971: case '\002': /* C-b */
972: for (i = 0; i < mtd->height; i++) {
973: if (mtd->current == 0)
974: break;
975: mode_tree_up(mtd, 1);
976: }
977: break;
1.35 nicm 978: case 'G':
1.1 nicm 979: case KEYC_NPAGE:
980: case '\006': /* C-f */
981: for (i = 0; i < mtd->height; i++) {
982: if (mtd->current == mtd->line_size - 1)
983: break;
984: mode_tree_down(mtd, 1);
985: }
986: break;
987: case KEYC_HOME:
988: mtd->current = 0;
989: mtd->offset = 0;
990: break;
991: case KEYC_END:
992: mtd->current = mtd->line_size - 1;
993: if (mtd->current > mtd->height - 1)
1.11 nicm 994: mtd->offset = mtd->current - mtd->height + 1;
1.1 nicm 995: else
996: mtd->offset = 0;
997: break;
998: case 't':
999: /*
1000: * Do not allow parents and children to both be tagged: untag
1001: * all parents and children of current.
1002: */
1003: if (!current->tagged) {
1004: parent = current->parent;
1005: while (parent != NULL) {
1006: parent->tagged = 0;
1007: parent = parent->parent;
1008: }
1009: mode_tree_clear_tagged(¤t->children);
1010: current->tagged = 1;
1011: } else
1012: current->tagged = 0;
1.28 nicm 1013: if (m != NULL)
1014: mode_tree_down(mtd, 0);
1.1 nicm 1015: break;
1016: case 'T':
1017: for (i = 0; i < mtd->line_size; i++)
1018: mtd->line_list[i].item->tagged = 0;
1019: break;
1020: case '\024': /* C-t */
1021: for (i = 0; i < mtd->line_size; i++) {
1022: if (mtd->line_list[i].item->parent == NULL)
1023: mtd->line_list[i].item->tagged = 1;
1024: else
1025: mtd->line_list[i].item->tagged = 0;
1026: }
1027: break;
1028: case 'O':
1.36 nicm 1029: mtd->sort_crit.field++;
1030: if (mtd->sort_crit.field == mtd->sort_size)
1031: mtd->sort_crit.field = 0;
1032: mode_tree_build(mtd);
1033: break;
1034: case 'r':
1035: mtd->sort_crit.reversed = !mtd->sort_crit.reversed;
1.1 nicm 1036: mode_tree_build(mtd);
1037: break;
1038: case KEYC_LEFT:
1.15 nicm 1039: case 'h':
1.1 nicm 1040: case '-':
1041: if (line->flat || !current->expanded)
1042: current = current->parent;
1043: if (current == NULL)
1044: mode_tree_up(mtd, 0);
1045: else {
1046: current->expanded = 0;
1047: mtd->current = current->line;
1048: mode_tree_build(mtd);
1049: }
1050: break;
1051: case KEYC_RIGHT:
1.15 nicm 1052: case 'l':
1.1 nicm 1053: case '+':
1054: if (line->flat || current->expanded)
1055: mode_tree_down(mtd, 0);
1056: else if (!line->flat) {
1057: current->expanded = 1;
1058: mode_tree_build(mtd);
1059: }
1.3 nicm 1060: break;
1.35 nicm 1061: case '?':
1062: case '/':
1.3 nicm 1063: case '\023': /* C-s */
1064: mtd->references++;
1065: status_prompt_set(c, "(search) ", "",
1066: mode_tree_search_callback, mode_tree_search_free, mtd,
1067: PROMPT_NOFORMAT);
1068: break;
1069: case 'n':
1070: mode_tree_search_set(mtd);
1.6 nicm 1071: break;
1072: case 'f':
1073: mtd->references++;
1074: status_prompt_set(c, "(filter) ", mtd->filter,
1075: mode_tree_filter_callback, mode_tree_filter_free, mtd,
1076: PROMPT_NOFORMAT);
1.9 nicm 1077: break;
1078: case 'v':
1079: mtd->preview = !mtd->preview;
1080: mode_tree_build(mtd);
1.11 nicm 1081: if (mtd->preview)
1082: mode_tree_check_selected(mtd);
1.1 nicm 1083: break;
1084: }
1085: return (0);
1086: }
1087:
1088: void
1089: mode_tree_run_command(struct client *c, struct cmd_find_state *fs,
1090: const char *template, const char *name)
1091: {
1.40 nicm 1092: struct cmdq_state *state;
1093: char *command, *error;
1094: enum cmd_parse_status status;
1.1 nicm 1095:
1096: command = cmd_template_replace(template, name, 1);
1.40 nicm 1097: if (command != NULL && *command != '\0') {
1098: state = cmdq_new_state(fs, NULL, 0);
1099: status = cmd_parse_and_append(command, NULL, c, state, &error);
1100: if (status == CMD_PARSE_ERROR) {
1101: if (c != NULL) {
1102: *error = toupper((u_char)*error);
1103: status_message_set(c, "%s", error);
1104: }
1105: free(error);
1.1 nicm 1106: }
1.40 nicm 1107: cmdq_free_state(state);
1.1 nicm 1108: }
1109: free(command);
1110: }