Coverage Report

Created: 2026-05-30 06:39

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