Coverage Report

Created: 2023-06-07 06:04

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