Coverage Report

Created: 2026-10-05 06:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/tmux/mode-tree.c
Line
Count
Source
1
/* $OpenBSD: mode-tree.c,v 1.103 2026/09/24 18:59:00 nicm Exp $ */
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
#define MODE_TREE_PREFIX_STYLE \
40
0
  "#[fg=themelightgrey]#[bg=default]#[noacs]"
41
42
#define MODE_TREE_PREFIX_FORMAT \
43
0
  MODE_TREE_PREFIX_STYLE \
44
0
  "#{p/#{mode_tree_key_width}:" \
45
0
  "#{?#{!=:#{mode_tree_key},},(#{mode_tree_key}),}}" \
46
0
  "#{R:#{?mode_tree_parent_last,    ," \
47
0
  "#[acs]x" MODE_TREE_PREFIX_STYLE "   }," \
48
0
  "#{mode_tree_repeat}}" \
49
0
  "#{?mode_tree_branch," \
50
0
  "#[acs]#{?mode_tree_last,mq,tq}+" MODE_TREE_PREFIX_STYLE " ,}" \
51
0
  "#{?mode_tree_has_children," \
52
0
  "#{?mode_tree_expanded,#[fg=themered]-" MODE_TREE_PREFIX_STYLE " ," \
53
0
  "#[fg=themegreen]+" MODE_TREE_PREFIX_STYLE " }," \
54
0
  "#{?mode_tree_flat,,  }}"
55
56
struct mode_tree_item;
57
struct mode_tree_prompt;
58
TAILQ_HEAD(mode_tree_list, mode_tree_item);
59
60
struct mode_tree_data {
61
  int       dead;
62
  u_int       references;
63
  int       zoomed;
64
65
  struct window_pane   *wp;
66
  void       *modedata;
67
  const struct menu_item   *menu;
68
69
  struct sort_criteria    sort_crit;
70
  const char     *view_name;
71
72
  mode_tree_build_cb    buildcb;
73
  mode_tree_draw_cb   drawcb;
74
  mode_tree_search_cb   searchcb;
75
  mode_tree_menu_cb   menucb;
76
  mode_tree_height_cb   heightcb;
77
  mode_tree_key_cb    keycb;
78
  mode_tree_swap_cb   swapcb;
79
  mode_tree_sort_cb   sortcb;
80
  mode_tree_help_cb   helpcb;
81
82
  struct mode_tree_list   children;
83
  struct mode_tree_list   saved;
84
85
  struct mode_tree_line  *line_list;
86
  u_int       line_size;
87
88
  u_int       depth;
89
  u_int       maxdepth;
90
91
  u_int       width;
92
  u_int       height;
93
94
  u_int       offset;
95
  u_int       current;
96
97
  struct screen     screen;
98
  struct prompt    *prompt;
99
  struct mode_tree_prompt  *prompt_data;
100
  u_int       prompt_cx;
101
  int       prompt_top;
102
103
  int       preview;
104
  char       *search;
105
  char       *filter;
106
  int       no_matches;
107
  enum mode_tree_search_dir search_dir;
108
  int       search_icase;
109
  int       help;
110
};
111
112
struct mode_tree_item {
113
  struct mode_tree_item   *parent;
114
  void        *itemdata;
115
  u_int        line;
116
117
  key_code       key;
118
  const char      *keystr;
119
  size_t         keylen;
120
121
  uint64_t       tag;
122
  const char      *name;
123
  const char      *text;
124
125
  int        expanded;
126
  int        tagged;
127
128
  int        draw_as_parent;
129
  int        no_tag;
130
  int        align;
131
132
  struct mode_tree_list    children;
133
  TAILQ_ENTRY(mode_tree_item)  entry;
134
};
135
136
struct mode_tree_line {
137
  struct mode_tree_item   *item;
138
  u_int        depth;
139
  int        last;
140
  int        flat;
141
};
142
143
struct mode_tree_menu {
144
  struct mode_tree_data   *data;
145
  struct client     *c;
146
  u_int        line;
147
};
148
149
/*
150
 * Wrapper around a prompt owned by a mode tree. The mode tree holds a reference
151
 * while the prompt is alive; the wrapper callbacks forward to the caller's
152
 * callbacks and drop that reference when the prompt is freed.
153
 */
154
struct mode_tree_prompt {
155
  struct mode_tree_data   *mtd;
156
  struct client     *c;
157
  mode_tree_prompt_input_cb  inputcb;
158
  prompt_free_cb       freecb;
159
  void        *data;
160
};
161
162
static void mode_tree_free_items(struct mode_tree_list *);
163
static void mode_tree_draw_help(struct mode_tree_data *,
164
        struct screen_write_ctx *);
165
static void mode_tree_draw_prompt(struct mode_tree_data *,
166
        struct screen_write_ctx *);
167
static enum cmd_retval mode_tree_prompt_accept(struct cmdq_item *, void *);
168
169
static const struct menu_item mode_tree_menu_items[] = {
170
  { "Scroll Left", '<', NULL },
171
  { "Scroll Right", '>', NULL },
172
  { "", KEYC_NONE, NULL },
173
  { "Cancel", 'q', NULL },
174
175
  { NULL, KEYC_NONE, NULL }
176
};
177
178
static const char* mode_tree_help_start[] = {
179
  "#[fg=themelightgrey]"
180
  "      Up, k #[#{E:tree-mode-border-style},acs]x#[default] Move cursor up",
181
  "#[fg=themelightgrey]"
182
  "    Down, j #[#{E:tree-mode-border-style},acs]x#[default] Move cursor down",
183
  "#[fg=themelightgrey]"
184
  "          g #[#{E:tree-mode-border-style},acs]x#[default] Go to top",
185
  "#[fg=themelightgrey]"
186
  "          G #[#{E:tree-mode-border-style},acs]x#[default] Go to bottom",
187
  "#[fg=themelightgrey]"
188
  " PPage, C-b #[#{E:tree-mode-border-style},acs]x#[default] Page up",
189
  "#[fg=themelightgrey]"
190
  " NPage, C-f #[#{E:tree-mode-border-style},acs]x#[default] Page down",
191
  "#[fg=themelightgrey]"
192
  "    Left, h #[#{E:tree-mode-border-style},acs]x#[default] Collapse %1",
193
  "#[fg=themelightgrey]"
194
  "   Right, l #[#{E:tree-mode-border-style},acs]x#[default] Expand %1",
195
  "#[fg=themelightgrey]"
196
  "        M-- #[#{E:tree-mode-border-style},acs]x#[default] Collapse all %1s",
197
  "#[fg=themelightgrey]"
198
  "        M-+ #[#{E:tree-mode-border-style},acs]x#[default] Expand all %1s",
199
  "#[fg=themelightgrey]"
200
  "          t #[#{E:tree-mode-border-style},acs]x#[default] Toggle %1 tag",
201
  "#[fg=themelightgrey]"
202
  "          T #[#{E:tree-mode-border-style},acs]x#[default] Untag all %1s",
203
  "#[fg=themelightgrey]"
204
  "        C-t #[#{E:tree-mode-border-style},acs]x#[default] Tag all %1s",
205
  "#[fg=themelightgrey]"
206
  "        C-s #[#{E:tree-mode-border-style},acs]x#[default] Search forward",
207
  "#[fg=themelightgrey]"
208
  "          n #[#{E:tree-mode-border-style},acs]x#[default] Repeat search forward",
209
  "#[fg=themelightgrey]"
210
  "          N #[#{E:tree-mode-border-style},acs]x#[default] Repeat search backward",
211
  "#[fg=themelightgrey]"
212
  "          f #[#{E:tree-mode-border-style},acs]x#[default] Filter %1s",
213
  "#[fg=themelightgrey]"
214
  "          O #[#{E:tree-mode-border-style},acs]x#[default] Change sort order",
215
  "#[fg=themelightgrey]"
216
  "          r #[#{E:tree-mode-border-style},acs]x#[default] Reverse sort order",
217
  "#[fg=themelightgrey]"
218
  "          v #[#{E:tree-mode-border-style},acs]x#[default] Toggle preview",
219
  NULL
220
};
221
static const char* mode_tree_help_end[] = {
222
  "#[fg=themelightgrey]"
223
  "  q, Escape #[#{E:tree-mode-border-style},acs]x#[default] Exit mode",
224
  NULL
225
};
226
0
#define MODE_TREE_HELP_DEFAULT_WIDTH 39
227
228
static int
229
mode_tree_is_lowercase(const char *ptr)
230
0
{
231
0
  while (*ptr != '\0') {
232
0
    if (*ptr != tolower((u_char)*ptr))
233
0
      return (0);
234
0
    ++ptr;
235
0
  }
236
0
  return (1);
237
0
}
238
239
static struct mode_tree_item *
240
mode_tree_find_item(struct mode_tree_list *mtl, uint64_t tag)
241
0
{
242
0
  struct mode_tree_item *mti, *child;
243
244
0
  TAILQ_FOREACH(mti, mtl, entry) {
245
0
    if (mti->tag == tag)
246
0
      return (mti);
247
0
    child = mode_tree_find_item(&mti->children, tag);
248
0
    if (child != NULL)
249
0
      return (child);
250
0
  }
251
0
  return (NULL);
252
0
}
253
254
static void
255
mode_tree_free_item(struct mode_tree_item *mti)
256
0
{
257
0
  mode_tree_free_items(&mti->children);
258
259
0
  free((void *)mti->name);
260
0
  free((void *)mti->text);
261
0
  free((void *)mti->keystr);
262
263
0
  free(mti);
264
0
}
265
266
static void
267
mode_tree_free_items(struct mode_tree_list *mtl)
268
0
{
269
0
  struct mode_tree_item *mti, *mti1;
270
271
0
  TAILQ_FOREACH_SAFE(mti, mtl, entry, mti1) {
272
0
    TAILQ_REMOVE(mtl, mti, entry);
273
0
    mode_tree_free_item(mti);
274
0
  }
275
0
}
276
277
static void
278
mode_tree_check_selected(struct mode_tree_data *mtd)
279
0
{
280
0
  if (mtd->height == 0)
281
0
    return;
282
283
  /* If the list has shrunk, do not leave empty lines at the bottom. */
284
0
  if (mtd->line_size <= mtd->height)
285
0
    mtd->offset = 0;
286
0
  else if (mtd->offset > mtd->line_size - mtd->height)
287
0
    mtd->offset = mtd->line_size - mtd->height;
288
289
  /*
290
   * If the current line would now be off screen move the offset so it is
291
   * visible.
292
   */
293
0
  if (mtd->current < mtd->offset)
294
0
    mtd->offset = mtd->current;
295
0
  else if (mtd->current > mtd->offset + mtd->height - 1)
296
0
    mtd->offset = mtd->current - mtd->height + 1;
297
0
}
298
299
static void
300
mode_tree_clear_lines(struct mode_tree_data *mtd)
301
0
{
302
0
  free(mtd->line_list);
303
0
  mtd->line_list = NULL;
304
0
  mtd->line_size = 0;
305
0
}
306
307
static void
308
mode_tree_build_lines(struct mode_tree_data *mtd,
309
    struct mode_tree_list *mtl, u_int depth)
310
0
{
311
0
  struct mode_tree_item *mti;
312
0
  struct mode_tree_line *line;
313
0
  u_int      i;
314
0
  int      flat = 1;
315
316
0
  mtd->depth = depth;
317
0
  if (depth > mtd->maxdepth)
318
0
    mtd->maxdepth = depth;
319
0
  TAILQ_FOREACH(mti, mtl, entry) {
320
0
    mtd->line_list = xreallocarray(mtd->line_list,
321
0
        mtd->line_size + 1, sizeof *mtd->line_list);
322
323
0
    line = &mtd->line_list[mtd->line_size++];
324
0
    line->item = mti;
325
0
    line->depth = depth;
326
0
    line->last = (mti == TAILQ_LAST(mtl, mode_tree_list));
327
328
0
    mti->line = (mtd->line_size - 1);
329
0
    if (!TAILQ_EMPTY(&mti->children))
330
0
      flat = 0;
331
0
    if (mti->expanded)
332
0
      mode_tree_build_lines(mtd, &mti->children, depth + 1);
333
334
0
    if (mtd->keycb != NULL) {
335
0
      mti->key = mtd->keycb(mtd->modedata, mti->itemdata,
336
0
          mti->line);
337
0
      if (mti->key == KEYC_UNKNOWN)
338
0
        mti->key = KEYC_NONE;
339
0
    } else if (mti->line < 10)
340
0
      mti->key = '0' + mti->line;
341
0
    else if (mti->line < 36)
342
0
      mti->key = KEYC_META|('a' + mti->line - 10);
343
0
    else
344
0
      mti->key = KEYC_NONE;
345
0
    if (mti->key != KEYC_NONE) {
346
0
      mti->keystr = xstrdup(key_string_lookup_key(mti->key,
347
0
          0));
348
0
      mti->keylen = strlen(mti->keystr);
349
0
    } else {
350
0
      mti->keystr = NULL;
351
0
      mti->keylen = 0;
352
0
    }
353
0
  }
354
0
  TAILQ_FOREACH(mti, mtl, entry) {
355
0
    for (i = 0; i < mtd->line_size; i++) {
356
0
      line = &mtd->line_list[i];
357
0
      if (line->item == mti)
358
0
        line->flat = flat;
359
0
    }
360
0
  }
361
0
}
362
363
static void
364
mode_tree_clear_tagged(struct mode_tree_list *mtl)
365
0
{
366
0
  struct mode_tree_item *mti;
367
368
0
  TAILQ_FOREACH(mti, mtl, entry) {
369
0
    mti->tagged = 0;
370
0
    mode_tree_clear_tagged(&mti->children);
371
0
  }
372
0
}
373
374
void
375
mode_tree_up(struct mode_tree_data *mtd, int wrap)
376
0
{
377
0
  if (mtd->line_size == 0)
378
0
    return;
379
0
  if (mtd->current == 0) {
380
0
    if (wrap) {
381
0
      mtd->current = mtd->line_size - 1;
382
0
      if (mtd->line_size >= mtd->height)
383
0
        mtd->offset = mtd->line_size - mtd->height;
384
0
    }
385
0
  } else {
386
0
    mtd->current--;
387
0
    if (mtd->current < mtd->offset)
388
0
      mtd->offset--;
389
0
  }
390
0
}
391
392
int
393
mode_tree_down(struct mode_tree_data *mtd, int wrap)
394
0
{
395
0
  if (mtd->line_size == 0)
396
0
    return (0);
397
0
  if (mtd->current == mtd->line_size - 1) {
398
0
    if (wrap) {
399
0
      mtd->current = 0;
400
0
      mtd->offset = 0;
401
0
    } else
402
0
      return (0);
403
0
  } else {
404
0
    mtd->current++;
405
0
    if (mtd->current > mtd->offset + mtd->height - 1)
406
0
      mtd->offset++;
407
0
  }
408
0
  return (1);
409
0
}
410
411
static void
412
mode_tree_swap(struct mode_tree_data *mtd, int direction)
413
0
{
414
0
  u_int current_depth = mtd->line_list[mtd->current].depth;
415
0
  u_int swap_with, swap_with_depth;
416
417
0
  if (mtd->swapcb == NULL)
418
0
    return;
419
420
  /* Find the next line at the same depth with the same parent . */
421
0
  swap_with = mtd->current;
422
0
  do {
423
0
    if (direction < 0 && swap_with < (u_int)-direction)
424
0
      return;
425
0
    if (direction > 0 && swap_with + direction >= mtd->line_size)
426
0
      return;
427
0
    swap_with += direction;
428
0
    swap_with_depth = mtd->line_list[swap_with].depth;
429
0
  } while (swap_with_depth > current_depth);
430
0
  if (swap_with_depth != current_depth)
431
0
    return;
432
433
0
  if (mtd->swapcb(mtd->line_list[mtd->current].item->itemdata,
434
0
      mtd->line_list[swap_with].item->itemdata, &mtd->sort_crit)) {
435
0
    mtd->current = swap_with;
436
0
    mode_tree_build(mtd);
437
0
  }
438
0
}
439
440
void *
441
mode_tree_get_current(struct mode_tree_data *mtd)
442
0
{
443
0
  if (mtd->line_size == 0)
444
0
    return (NULL);
445
0
  return (mtd->line_list[mtd->current].item->itemdata);
446
0
}
447
448
const char *
449
mode_tree_get_current_name(struct mode_tree_data *mtd)
450
0
{
451
0
  return (mtd->line_list[mtd->current].item->name);
452
0
}
453
454
void
455
mode_tree_select_top(struct mode_tree_data *mtd)
456
0
{
457
0
  mtd->current = 0;
458
0
  mtd->offset = 0;
459
0
}
460
461
void
462
mode_tree_expand_current(struct mode_tree_data *mtd)
463
0
{
464
0
  if (!mtd->line_list[mtd->current].item->expanded) {
465
0
    mtd->line_list[mtd->current].item->expanded = 1;
466
0
    mode_tree_build(mtd);
467
0
  }
468
0
}
469
470
void
471
mode_tree_collapse_current(struct mode_tree_data *mtd)
472
0
{
473
0
  if (mtd->line_list[mtd->current].item->expanded) {
474
0
    mtd->line_list[mtd->current].item->expanded = 0;
475
0
    mode_tree_build(mtd);
476
0
  }
477
0
}
478
479
static int
480
mode_tree_get_tag(struct mode_tree_data *mtd, uint64_t tag, u_int *found)
481
0
{
482
0
  u_int i;
483
484
0
  for (i = 0; i < mtd->line_size; i++) {
485
0
    if (mtd->line_list[i].item->tag == tag)
486
0
      break;
487
0
  }
488
0
  if (i != mtd->line_size) {
489
0
    *found = i;
490
0
    return (1);
491
0
  }
492
0
  return (0);
493
0
}
494
495
void
496
mode_tree_expand(struct mode_tree_data *mtd, uint64_t tag)
497
0
{
498
0
  u_int found;
499
500
0
  if (!mode_tree_get_tag(mtd, tag, &found))
501
0
    return;
502
0
  if (!mtd->line_list[found].item->expanded) {
503
0
    mtd->line_list[found].item->expanded = 1;
504
0
    mode_tree_build(mtd);
505
0
  }
506
0
}
507
508
int
509
mode_tree_set_current(struct mode_tree_data *mtd, uint64_t tag)
510
0
{
511
0
  u_int found;
512
513
0
  if (mode_tree_get_tag(mtd, tag, &found)) {
514
0
    mtd->current = found;
515
0
    mode_tree_check_selected(mtd);
516
0
    return (1);
517
0
  }
518
0
  if (mtd->current >= mtd->line_size) {
519
0
    if (mtd->line_size == 0)
520
0
      return (0);
521
0
    mtd->current = mtd->line_size - 1;
522
0
    mode_tree_check_selected(mtd);
523
0
  }
524
0
  return (0);
525
0
}
526
527
u_int
528
mode_tree_count_tagged(struct mode_tree_data *mtd)
529
0
{
530
0
  struct mode_tree_item *mti;
531
0
  u_int      i, tagged;
532
533
0
  tagged = 0;
534
0
  for (i = 0; i < mtd->line_size; i++) {
535
0
    mti = mtd->line_list[i].item;
536
0
    if (mti->tagged)
537
0
      tagged++;
538
0
  }
539
0
  return (tagged);
540
0
}
541
542
void
543
mode_tree_each_tagged(struct mode_tree_data *mtd, mode_tree_each_cb cb,
544
    struct client *c, key_code key, int current)
545
0
{
546
0
  struct mode_tree_item *mti;
547
0
  u_int      i;
548
0
  int      fired;
549
550
0
  fired = 0;
551
0
  for (i = 0; i < mtd->line_size; i++) {
552
0
    mti = mtd->line_list[i].item;
553
0
    if (mti->tagged) {
554
0
      fired = 1;
555
0
      cb(mtd->modedata, mti->itemdata, c, key);
556
0
    }
557
0
  }
558
0
  if (!fired && current) {
559
0
    mti = mtd->line_list[mtd->current].item;
560
0
    cb(mtd->modedata, mti->itemdata, c, key);
561
0
  }
562
0
}
563
564
struct mode_tree_data *
565
mode_tree_start(struct window_pane *wp, struct args *args,
566
    mode_tree_build_cb buildcb, mode_tree_draw_cb drawcb,
567
    mode_tree_search_cb searchcb, mode_tree_menu_cb menucb,
568
    mode_tree_height_cb heightcb, mode_tree_key_cb keycb,
569
    mode_tree_swap_cb swapcb, mode_tree_sort_cb sortcb,
570
    mode_tree_help_cb helpcb, void *modedata, const struct menu_item *menu,
571
    struct screen **s)
572
0
{
573
0
  struct mode_tree_data *mtd;
574
575
0
  mtd = xcalloc(1, sizeof *mtd);
576
0
  mtd->references = 1;
577
578
0
  mtd->wp = wp;
579
0
  mtd->modedata = modedata;
580
0
  mtd->menu = menu;
581
582
0
  if (drawcb == NULL)
583
0
    mtd->preview = MODE_TREE_PREVIEW_OFF;
584
0
  else if (args_has(args, 'N') > 1)
585
0
    mtd->preview = MODE_TREE_PREVIEW_BIG;
586
0
  else if (args_has(args, 'N'))
587
0
    mtd->preview = MODE_TREE_PREVIEW_OFF;
588
0
  else
589
0
    mtd->preview = MODE_TREE_PREVIEW_NORMAL;
590
591
0
  mtd->sort_crit.order = sort_order_from_string(args_get(args, 'O'));
592
0
  mtd->sort_crit.reversed = args_has(args, 'r');
593
594
0
  if (args_has(args, 'f'))
595
0
    mtd->filter = xstrdup(args_get(args, 'f'));
596
0
  else
597
0
    mtd->filter = NULL;
598
599
0
  mtd->buildcb = buildcb;
600
0
  mtd->drawcb = drawcb;
601
0
  mtd->searchcb = searchcb;
602
0
  mtd->menucb = menucb;
603
0
  mtd->heightcb = heightcb;
604
0
  mtd->keycb = keycb;
605
0
  mtd->swapcb = swapcb;
606
0
  mtd->sortcb = sortcb;
607
0
  mtd->helpcb = helpcb;
608
609
0
  TAILQ_INIT(&mtd->children);
610
611
0
  *s = &mtd->screen;
612
0
  screen_init(*s, screen_size_x(&wp->base), screen_size_y(&wp->base), 0);
613
0
  (*s)->mode &= ~MODE_CURSOR;
614
615
0
  return (mtd);
616
0
}
617
618
void
619
mode_tree_zoom(struct mode_tree_data *mtd, struct args *args)
620
0
{
621
0
  struct window_pane  *wp = mtd->wp;
622
623
0
  if (args_has(args, 'Z')) {
624
0
    mtd->zoomed = (wp->window->flags & WINDOW_ZOOMED);
625
0
    if (!mtd->zoomed && window_zoom(wp) == 0)
626
0
      server_redraw_window(wp->window);
627
0
  } else
628
0
    mtd->zoomed = -1;
629
0
}
630
631
static void
632
mode_tree_set_height(struct mode_tree_data *mtd)
633
0
{
634
0
  struct screen *s = &mtd->screen;
635
0
  u_int    height;
636
637
0
  if (mtd->heightcb != NULL) {
638
0
    height = mtd->heightcb(mtd, screen_size_y(s));
639
0
    if (height < screen_size_y(s))
640
0
        mtd->height = screen_size_y(s) - height;
641
0
  } else {
642
0
    if (mtd->preview == MODE_TREE_PREVIEW_NORMAL) {
643
0
      mtd->height = (screen_size_y(s) / 3) * 2;
644
0
      if (mtd->height > mtd->line_size)
645
0
        mtd->height = screen_size_y(s) / 2;
646
0
      if (mtd->height < 10)
647
0
        mtd->height = screen_size_y(s);
648
0
    } else if (mtd->preview == MODE_TREE_PREVIEW_BIG) {
649
0
      mtd->height = screen_size_y(s) / 4;
650
0
      if (mtd->height > mtd->line_size)
651
0
        mtd->height = mtd->line_size;
652
0
      if (mtd->height < 2)
653
0
        mtd->height = 2;
654
0
    } else
655
0
      mtd->height = screen_size_y(s);
656
0
  }
657
0
  if (screen_size_y(s) - mtd->height < 2)
658
0
    mtd->height = screen_size_y(s);
659
0
}
660
661
void
662
mode_tree_build(struct mode_tree_data *mtd)
663
0
{
664
0
  struct screen *s = &mtd->screen;
665
0
  uint64_t   tag;
666
667
0
  if (mtd->line_list != NULL)
668
0
    tag = mtd->line_list[mtd->current].item->tag;
669
0
  else
670
0
    tag = UINT64_MAX;
671
672
0
  TAILQ_CONCAT(&mtd->saved, &mtd->children, entry);
673
0
  TAILQ_INIT(&mtd->children);
674
675
0
  if (mtd->sortcb != NULL)
676
0
    mtd->sortcb(&mtd->sort_crit);
677
0
  mtd->buildcb(mtd->modedata, &mtd->sort_crit, &tag, mtd->filter);
678
0
  mtd->no_matches = TAILQ_EMPTY(&mtd->children);
679
0
  if (mtd->no_matches)
680
0
    mtd->buildcb(mtd->modedata, &mtd->sort_crit, &tag, NULL);
681
682
0
  mode_tree_free_items(&mtd->saved);
683
0
  TAILQ_INIT(&mtd->saved);
684
685
0
  mode_tree_clear_lines(mtd);
686
0
  mtd->maxdepth = 0;
687
0
  mode_tree_build_lines(mtd, &mtd->children, 0);
688
689
0
  if (mtd->line_list != NULL && tag == UINT64_MAX)
690
0
    tag = mtd->line_list[mtd->current].item->tag;
691
692
0
  mtd->width = screen_size_x(s);
693
0
  if (mtd->preview != MODE_TREE_PREVIEW_OFF)
694
0
    mode_tree_set_height(mtd);
695
0
  else
696
0
    mtd->height = screen_size_y(s);
697
0
  mode_tree_set_current(mtd, tag);
698
0
  mode_tree_check_selected(mtd);
699
0
}
700
701
static void
702
mode_tree_remove_ref(struct mode_tree_data *mtd)
703
0
{
704
0
  if (--mtd->references == 0)
705
0
    free(mtd);
706
0
}
707
708
void
709
mode_tree_free(struct mode_tree_data *mtd)
710
0
{
711
0
  struct window_pane  *wp = mtd->wp;
712
713
0
  if (mtd->zoomed == 0)
714
0
    server_unzoom_window(wp->window);
715
716
0
  mode_tree_clear_prompt(mtd);
717
0
  mode_tree_free_items(&mtd->children);
718
0
  mode_tree_clear_lines(mtd);
719
0
  screen_free(&mtd->screen);
720
721
0
  free(mtd->search);
722
0
  free(mtd->filter);
723
724
0
  mtd->dead = 1;
725
0
  mode_tree_remove_ref(mtd);
726
0
}
727
728
void
729
mode_tree_resize(struct mode_tree_data *mtd, u_int sx, u_int sy)
730
0
{
731
0
  struct screen *s = &mtd->screen;
732
733
0
  screen_resize(s, sx, sy, 0);
734
735
0
  mode_tree_build(mtd);
736
0
  mode_tree_draw(mtd);
737
738
0
  mtd->wp->flags |= PANE_REDRAW;
739
0
}
740
741
struct mode_tree_item *
742
mode_tree_add(struct mode_tree_data *mtd, struct mode_tree_item *parent,
743
    void *itemdata, uint64_t tag, const char *name, const char *text,
744
    int expanded)
745
0
{
746
0
  struct mode_tree_item *mti, *saved;
747
748
0
  log_debug("%s: %llu, %s %s", __func__, (unsigned long long)tag,
749
0
      name, (text == NULL ? "" : text));
750
751
0
  mti = xcalloc(1, sizeof *mti);
752
0
  mti->parent = parent;
753
0
  mti->itemdata = itemdata;
754
755
0
  mti->tag = tag;
756
0
  mti->name = xstrdup(name);
757
0
  if (text != NULL)
758
0
    mti->text = xstrdup(text);
759
760
0
  saved = mode_tree_find_item(&mtd->saved, tag);
761
0
  if (saved != NULL) {
762
0
    if (parent == NULL || parent->expanded)
763
0
      mti->tagged = saved->tagged;
764
0
    mti->expanded = saved->expanded;
765
0
  } else if (expanded == -1)
766
0
    mti->expanded = 1;
767
0
  else
768
0
    mti->expanded = expanded;
769
770
0
  TAILQ_INIT(&mti->children);
771
772
0
  if (parent != NULL)
773
0
    TAILQ_INSERT_TAIL(&parent->children, mti, entry);
774
0
  else
775
0
    TAILQ_INSERT_TAIL(&mtd->children, mti, entry);
776
777
0
  return (mti);
778
0
}
779
780
void
781
mode_tree_view_name(struct mode_tree_data *mtd, const char *name)
782
0
{
783
0
  mtd->view_name = name;
784
0
}
785
786
void
787
mode_tree_draw_as_parent(struct mode_tree_item *mti)
788
0
{
789
0
  mti->draw_as_parent = 1;
790
0
}
791
792
void
793
mode_tree_no_tag(struct mode_tree_item *mti)
794
0
{
795
0
  mti->no_tag = 1;
796
0
}
797
798
/*
799
 * Set the alignment of the item name: -1 to align left, 0 (default) to not
800
 * align, or 1 to align right.
801
 */
802
void
803
mode_tree_align(struct mode_tree_item *mti, int align)
804
0
{
805
0
  mti->align = align;
806
0
}
807
808
void
809
mode_tree_remove(struct mode_tree_data *mtd, struct mode_tree_item *mti)
810
0
{
811
0
  struct mode_tree_item *parent = mti->parent;
812
813
0
  if (parent != NULL)
814
0
    TAILQ_REMOVE(&parent->children, mti, entry);
815
0
  else
816
0
    TAILQ_REMOVE(&mtd->children, mti, entry);
817
0
  mode_tree_free_item(mti);
818
0
}
819
820
void
821
mode_tree_draw(struct mode_tree_data *mtd)
822
0
{
823
0
  struct window_pane  *wp = mtd->wp;
824
0
  struct screen   *s = &mtd->screen;
825
0
  struct mode_tree_line *line;
826
0
  struct mode_tree_item *mti;
827
0
  struct options    *oo = wp->window->options;
828
0
  struct screen_write_ctx  ctx;
829
0
  struct format_tree  *ft;
830
0
  struct grid_cell   gc0, gc, box_gc;
831
0
  u_int      w, h, i, sy, box_x, box_y;
832
0
  u_int      width, text_width, prefix_width, left;
833
0
  char      *text, *prefix;
834
0
  const char    *tag, *separator;
835
0
  size_t       n;
836
0
  int      keylen, alignlen[mtd->maxdepth + 1];
837
0
  int      dfg, dfg0;
838
839
0
  if (mtd->line_size == 0)
840
0
    return;
841
842
0
  w = mtd->width;
843
0
  h = mtd->height;
844
0
  if (w == 0 || h == 0)
845
0
    return;
846
847
0
  memcpy(&gc0, &grid_default_cell, sizeof gc0);
848
0
  memcpy(&gc, &grid_default_cell, sizeof gc);
849
0
  style_apply(&gc, oo, "tree-mode-selection-style", NULL);
850
0
  memcpy(&box_gc, &grid_default_cell, sizeof box_gc);
851
0
  style_apply(&box_gc, oo, "tree-mode-border-style", NULL);
852
853
0
  dfg = gc.fg;
854
0
  dfg0 = gc0.fg;
855
856
0
  screen_write_start(&ctx, s);
857
0
  screen_write_clearscreen(&ctx, 8);
858
0
  ft = format_create_defaults(NULL, NULL, NULL, NULL, wp);
859
860
0
  keylen = 0;
861
0
  for (i = 0; i < mtd->line_size; i++) {
862
0
    mti = mtd->line_list[i].item;
863
0
    if (mti->key == KEYC_NONE)
864
0
      continue;
865
0
    if ((int)mti->keylen + 3 > keylen)
866
0
      keylen = mti->keylen + 3;
867
0
  }
868
869
0
  for (i = 0; i < mtd->maxdepth + 1; i++)
870
0
    alignlen[i] = 0;
871
0
  for (i = 0; i < mtd->line_size; i++) {
872
0
    line = &mtd->line_list[i];
873
0
    mti = line->item;
874
0
    if (mti->align &&
875
0
        (int)strlen(mti->name) > alignlen[line->depth])
876
0
      alignlen[line->depth] = strlen(mti->name);
877
0
  }
878
879
0
  for (i = 0; i < mtd->line_size; i++) {
880
0
    if (i < mtd->offset)
881
0
      continue;
882
0
    if (i > mtd->offset + h - 1)
883
0
      break;
884
0
    line = &mtd->line_list[i];
885
0
    mti = line->item;
886
887
0
    screen_write_cursormove(&ctx, 0, i - mtd->offset, 0);
888
889
0
    if (mti->key != KEYC_NONE)
890
0
      format_add(ft, "mode_tree_key", "%s", mti->keystr);
891
0
    else
892
0
      format_add(ft, "mode_tree_key", "%s", "");
893
0
    format_add(ft, "mode_tree_key_width", "%d", keylen);
894
0
    format_add(ft, "mode_tree_selected", "%d", i == mtd->current);
895
0
    if (line->depth == 0) {
896
0
      format_add(ft, "mode_tree_repeat", "%u", 0);
897
0
      format_add(ft, "mode_tree_branch", "0");
898
0
      format_add(ft, "mode_tree_parent_last", "0");
899
0
    } else {
900
0
      format_add(ft, "mode_tree_repeat", "%u",
901
0
          line->depth - 1);
902
0
      format_add(ft, "mode_tree_branch", "1");
903
0
      if (mti->parent != NULL &&
904
0
          mtd->line_list[mti->parent->line].last)
905
0
        format_add(ft, "mode_tree_parent_last", "1");
906
0
      else
907
0
        format_add(ft, "mode_tree_parent_last", "0");
908
0
    }
909
0
    if (TAILQ_EMPTY(&mti->children))
910
0
      format_add(ft, "mode_tree_has_children", "0");
911
0
    else
912
0
      format_add(ft, "mode_tree_has_children", "1");
913
0
    format_add(ft, "mode_tree_last", "%d", line->last);
914
0
    format_add(ft, "mode_tree_expanded", "%d", mti->expanded);
915
0
    format_add(ft, "mode_tree_flat", "%d", line->flat);
916
0
    prefix = format_expand(ft, MODE_TREE_PREFIX_FORMAT);
917
0
    prefix_width = format_width(prefix);
918
0
    if (prefix_width > w)
919
0
      prefix_width = w;
920
921
0
    if (mti->tagged)
922
0
      tag = "*";
923
0
    else
924
0
      tag = "";
925
0
    if (mti->text != NULL)
926
0
      separator = "#[fg=themelightgrey]: #[default]";
927
0
    else
928
0
      separator = "";
929
0
    xasprintf(&text, "%*s%s%s",
930
0
        mti->align * alignlen[line->depth], mti->name, tag, separator);
931
0
    text_width = format_width(text);
932
0
    left = (prefix_width < w) ? (w - prefix_width) : 0;
933
0
    if (text_width > left)
934
0
      text_width = left;
935
0
    width = prefix_width + text_width;
936
937
0
    if (mti->tagged) {
938
0
      gc.fg = COLOUR_THEME_CYAN|COLOUR_FLAG_THEME;
939
0
      gc0.fg = COLOUR_THEME_CYAN|COLOUR_FLAG_THEME;
940
0
    }
941
942
0
    if (i != mtd->current) {
943
0
      screen_write_clearendofline(&ctx, 8);
944
0
      format_draw(&ctx, &grid_default_cell, prefix_width,
945
0
          prefix, NULL, 0);
946
0
      if (left != 0) {
947
0
        screen_write_cursormove(&ctx, prefix_width,
948
0
            i - mtd->offset, 0);
949
0
        format_draw(&ctx, &gc0, left, text, NULL, 0);
950
0
        if (mti->text != NULL && width < w) {
951
0
          screen_write_cursormove(&ctx, width,
952
0
              i - mtd->offset, 0);
953
0
          format_draw(&ctx, &gc0, w - width,
954
0
              mti->text, NULL, 0);
955
0
        }
956
0
      }
957
0
    } else {
958
0
      screen_write_clearendofline(&ctx, gc.bg);
959
0
      format_draw(&ctx, &gc, prefix_width, prefix, NULL, 1);
960
0
      if (left != 0) {
961
0
        screen_write_cursormove(&ctx, prefix_width,
962
0
            i - mtd->offset, 0);
963
0
        format_draw(&ctx, &gc, left, text, NULL, 1);
964
0
        if (mti->text != NULL && width < w) {
965
0
          screen_write_cursormove(&ctx, width,
966
0
              i - mtd->offset, 0);
967
0
          format_draw(&ctx, &gc, w - width,
968
0
              mti->text, NULL, 1);
969
0
        }
970
0
      }
971
0
    }
972
0
    free(text);
973
0
    free(prefix);
974
975
0
    if (mti->tagged) {
976
0
      gc.fg = dfg;
977
0
      gc0.fg = dfg0;
978
0
    }
979
0
  }
980
0
  format_free(ft);
981
982
0
  if (mtd->preview == MODE_TREE_PREVIEW_OFF)
983
0
    goto done;
984
985
0
  sy = screen_size_y(s);
986
0
  if (sy <= 4 || h < 2 || sy - h <= 4 || w <= 4)
987
0
    goto done;
988
989
0
  line = &mtd->line_list[mtd->current];
990
0
  mti = line->item;
991
0
  if (mti->draw_as_parent)
992
0
    mti = mti->parent;
993
994
0
  screen_write_cursormove(&ctx, 0, h, 0);
995
0
  screen_write_box(&ctx, w, sy - h, BOX_LINES_DEFAULT, &box_gc, NULL);
996
997
0
  if (mtd->sort_crit.order_seq != NULL) {
998
0
    xasprintf(&text, " %s (sort: %s%s)%s%s%s", mti->name,
999
0
        sort_order_to_string(mtd->sort_crit.order),
1000
0
        mtd->sort_crit.reversed ? ", reversed" : "",
1001
0
        mtd->view_name == NULL ? "" : " (view: ",
1002
0
        mtd->view_name == NULL ? "" : mtd->view_name,
1003
0
        mtd->view_name == NULL ? "" : ")");
1004
0
  } else
1005
0
    xasprintf(&text, " %s", mti->name);
1006
0
  if (w - 2 >= strlen(text)) {
1007
0
    screen_write_cursormove(&ctx, 1, h, 0);
1008
0
    screen_write_puts(&ctx, &box_gc, "%s", text);
1009
1010
0
    if (mtd->no_matches)
1011
0
      n = (sizeof "no matches") - 1;
1012
0
    else
1013
0
      n = (sizeof "active") - 1;
1014
0
    if (mtd->filter != NULL && w - 2 >= strlen(text) + 10 + n + 2) {
1015
0
      screen_write_puts(&ctx, &box_gc, " (filter: ");
1016
0
      if (mtd->no_matches)
1017
0
        screen_write_puts(&ctx, &box_gc, "no matches");
1018
0
      else
1019
0
        screen_write_puts(&ctx, &box_gc, "active");
1020
0
      screen_write_puts(&ctx, &box_gc, ") ");
1021
0
    } else
1022
0
      screen_write_puts(&ctx, &box_gc, " ");
1023
0
  }
1024
0
  free(text);
1025
1026
0
  box_x = w - 4;
1027
0
  box_y = sy - h - 2;
1028
1029
0
  if (box_x != 0 && box_y != 0) {
1030
0
    screen_write_cursormove(&ctx, 2, h + 1, 0);
1031
0
    mtd->drawcb(mtd->modedata, mti->itemdata, &ctx, box_x, box_y);
1032
0
  }
1033
1034
0
done:
1035
0
  if (mtd->help)
1036
0
    mode_tree_draw_help(mtd, &ctx);
1037
0
  if (mtd->prompt != NULL)
1038
0
    mode_tree_draw_prompt(mtd, &ctx);
1039
0
  else {
1040
0
    s->mode &= ~MODE_CURSOR;
1041
0
    screen_write_cursormove(&ctx, 0, mtd->current - mtd->offset, 0);
1042
0
  }
1043
0
  screen_write_stop(&ctx);
1044
0
}
1045
1046
static void
1047
mode_tree_draw_prompt(struct mode_tree_data *mtd, struct screen_write_ctx *ctx)
1048
0
{
1049
0
  struct screen   *s = &mtd->screen;
1050
0
  struct prompt_draw_data  pdd;
1051
0
  u_int      sx = screen_size_x(s), sy = screen_size_y(s);
1052
0
  u_int      py;
1053
1054
0
  if (sx == 0 || sy == 0)
1055
0
    return;
1056
1057
0
  if (mtd->prompt_top)
1058
0
    py = 0;
1059
0
  else
1060
0
    py = sy - 1;
1061
1062
0
  pdd.ctx = ctx;
1063
0
  pdd.cursor_x = &mtd->prompt_cx;
1064
0
  pdd.area_x = 0;
1065
0
  pdd.area_width = sx;
1066
0
  pdd.prompt_line = py;
1067
1068
0
  s->mode |= MODE_CURSOR;
1069
0
  prompt_draw(mtd->prompt, &pdd);
1070
0
  screen_write_cursormove(ctx, mtd->prompt_cx, py, 0);
1071
0
}
1072
1073
void
1074
mode_tree_clear_prompt(struct mode_tree_data *mtd)
1075
0
{
1076
0
  struct prompt *prompt = mtd->prompt;
1077
1078
0
  if (mtd->prompt != NULL) {
1079
0
    mtd->prompt = NULL;
1080
0
    prompt_free(prompt);
1081
0
    mtd->screen.mode &= ~MODE_CURSOR;
1082
0
  }
1083
0
}
1084
1085
int
1086
mode_tree_has_prompt(struct mode_tree_data *mtd)
1087
0
{
1088
0
  return (mtd->prompt != NULL);
1089
0
}
1090
1091
static enum cmd_retval
1092
mode_tree_prompt_accept(struct cmdq_item *item, void *data)
1093
0
{
1094
0
  struct mode_tree_data *mtd = data;
1095
0
  struct client   *c = cmdq_get_client(item);
1096
0
  key_code     key = 'y';
1097
1098
0
  if (mtd->prompt != NULL && c != NULL)
1099
0
    mode_tree_key(mtd, c, &key, NULL, NULL, NULL);
1100
1101
0
  mode_tree_remove_ref(mtd);
1102
0
  return (CMD_RETURN_NORMAL);
1103
0
}
1104
1105
static enum prompt_result
1106
mode_tree_prompt_input_callback(void *data, const char *s,
1107
    enum prompt_key_result key)
1108
0
{
1109
0
  struct mode_tree_prompt *mtp = data;
1110
1111
0
  if (mtp->inputcb != NULL)
1112
0
    return (mtp->inputcb(mtp->c, mtp->data, s, key));
1113
0
  return (PROMPT_CLOSE);
1114
0
}
1115
1116
static void
1117
mode_tree_prompt_free_callback(void *data)
1118
0
{
1119
0
  struct mode_tree_prompt *mtp = data;
1120
1121
0
  if (mtp->mtd->prompt_data == mtp)
1122
0
    mtp->mtd->prompt_data = NULL;
1123
0
  if (mtp->freecb != NULL)
1124
0
    mtp->freecb(mtp->data);
1125
0
  mode_tree_remove_ref(mtp->mtd);
1126
0
  free(mtp);
1127
0
}
1128
1129
void
1130
mode_tree_set_prompt(struct mode_tree_data *mtd, struct client *c,
1131
    const char *prompt, const char *input, enum prompt_type type, int flags,
1132
    mode_tree_prompt_input_cb inputcb, prompt_free_cb freecb, void *data)
1133
0
{
1134
0
  struct session      *s;
1135
0
  struct options      *oo;
1136
0
  struct prompt_create_data  pd;
1137
0
  struct mode_tree_prompt   *mtp;
1138
1139
0
  if (c != NULL && c->session != NULL) {
1140
0
    s = c->session;
1141
0
    oo = s->options;
1142
0
  } else {
1143
0
    s = NULL;
1144
0
    oo = global_s_options;
1145
0
  }
1146
1147
0
  mode_tree_clear_prompt(mtd);
1148
1149
0
  mtp = xcalloc(1, sizeof *mtp);
1150
0
  mtp->mtd = mtd;
1151
0
  mtp->c = c;
1152
0
  mtp->inputcb = inputcb;
1153
0
  mtp->freecb = freecb;
1154
0
  mtp->data = data;
1155
1156
0
  mtd->references++;
1157
0
  mtd->prompt_top = options_get_number(oo, "status-position") == 0;
1158
1159
0
  memset(&pd, 0, sizeof pd);
1160
0
  prompt_set_options(&pd, s);
1161
0
  pd.prompt = prompt;
1162
0
  pd.input = input;
1163
0
  pd.type = type;
1164
0
  pd.flags = flags|PROMPT_ISMODE;
1165
0
  pd.inputcb = mode_tree_prompt_input_callback;
1166
0
  pd.freecb = mode_tree_prompt_free_callback;
1167
0
  pd.data = mtp;
1168
0
  mtd->prompt = prompt_create(&pd);
1169
0
  mtd->prompt_data = mtp;
1170
1171
0
  mode_tree_draw(mtd);
1172
0
  mtd->wp->flags |= PANE_REDRAW;
1173
1174
0
  if ((flags & PROMPT_SINGLE) && (flags & PROMPT_ACCEPT) && c != NULL) {
1175
0
    mtd->references++;
1176
0
    cmdq_append(c, cmdq_get_callback(mode_tree_prompt_accept, mtd));
1177
0
  }
1178
0
}
1179
1180
static struct mode_tree_item *
1181
mode_tree_search_backward(struct mode_tree_data *mtd)
1182
0
{
1183
0
  struct mode_tree_item *mti, *last, *prev;
1184
0
  int      icase = mtd->search_icase;
1185
1186
0
  if (mtd->search == NULL)
1187
0
    return (NULL);
1188
1189
0
  mti = last = mtd->line_list[mtd->current].item;
1190
0
  for (;;) {
1191
0
    if ((prev = TAILQ_PREV(mti, mode_tree_list, entry)) != NULL) {
1192
      /* Point to the last child in the previous subtree. */
1193
0
      while (!TAILQ_EMPTY(&prev->children)) {
1194
0
        prev = TAILQ_LAST(&prev->children,
1195
0
            mode_tree_list);
1196
0
      }
1197
0
      mti = prev;
1198
0
    } else {
1199
      /* If prev is NULL, jump to the parent. */
1200
0
      mti = mti->parent;
1201
0
    }
1202
1203
0
    if (mti == NULL) {
1204
      /* Point to the last child in the last root subtree. */
1205
0
      prev = TAILQ_LAST(&mtd->children, mode_tree_list);
1206
0
      while (!TAILQ_EMPTY(&prev->children)) {
1207
0
        prev = TAILQ_LAST(&prev->children,
1208
0
            mode_tree_list);
1209
0
      }
1210
0
      mti = prev;
1211
0
    }
1212
0
    if (mti == last)
1213
0
      break;
1214
1215
0
    if (mtd->searchcb == NULL) {
1216
0
      if (!icase && strstr(mti->name, mtd->search) != NULL)
1217
0
        return (mti);
1218
0
      if (icase && strcasestr(mti->name, mtd->search) != NULL)
1219
0
        return (mti);
1220
0
      continue;
1221
0
    }
1222
0
    if (mtd->searchcb(mtd->modedata, mti->itemdata, mtd->search,
1223
0
        icase))
1224
0
      return (mti);
1225
0
  }
1226
0
  return (NULL);
1227
0
}
1228
1229
static struct mode_tree_item *
1230
mode_tree_search_forward(struct mode_tree_data *mtd)
1231
0
{
1232
0
  struct mode_tree_item *mti, *last, *next;
1233
0
  int      icase = mtd->search_icase;
1234
1235
0
  if (mtd->search == NULL)
1236
0
    return (NULL);
1237
1238
0
  mti = last = mtd->line_list[mtd->current].item;
1239
0
  for (;;) {
1240
0
    if (!TAILQ_EMPTY(&mti->children))
1241
0
      mti = TAILQ_FIRST(&mti->children);
1242
0
    else if ((next = TAILQ_NEXT(mti, entry)) != NULL)
1243
0
      mti = next;
1244
0
    else {
1245
0
      for (;;) {
1246
0
        mti = mti->parent;
1247
0
        if (mti == NULL)
1248
0
          break;
1249
0
        if ((next = TAILQ_NEXT(mti, entry)) != NULL) {
1250
0
          mti = next;
1251
0
          break;
1252
0
        }
1253
0
      }
1254
0
    }
1255
0
    if (mti == NULL)
1256
0
      mti = TAILQ_FIRST(&mtd->children);
1257
0
    if (mti == last)
1258
0
      break;
1259
1260
0
    if (mtd->searchcb == NULL) {
1261
0
      if (!icase && strstr(mti->name, mtd->search) != NULL)
1262
0
        return (mti);
1263
0
      if (icase && strcasestr(mti->name, mtd->search) != NULL)
1264
0
        return (mti);
1265
0
      continue;
1266
0
    }
1267
0
    if (mtd->searchcb(mtd->modedata, mti->itemdata, mtd->search,
1268
0
        icase))
1269
0
      return (mti);
1270
0
  }
1271
0
  return (NULL);
1272
0
}
1273
1274
static void
1275
mode_tree_search_set(struct mode_tree_data *mtd)
1276
0
{
1277
0
  struct mode_tree_item *mti, *loop;
1278
0
  uint64_t     tag;
1279
1280
0
  if (mtd->search_dir == MODE_TREE_SEARCH_FORWARD)
1281
0
    mti = mode_tree_search_forward(mtd);
1282
0
  else
1283
0
    mti = mode_tree_search_backward(mtd);
1284
0
  if (mti == NULL)
1285
0
    return;
1286
0
  tag = mti->tag;
1287
1288
0
  loop = mti->parent;
1289
0
  while (loop != NULL) {
1290
0
    loop->expanded = 1;
1291
0
    loop = loop->parent;
1292
0
  }
1293
1294
0
  mode_tree_build(mtd);
1295
0
  mode_tree_set_current(mtd, tag);
1296
0
  mode_tree_draw(mtd);
1297
0
  mtd->wp->flags |= PANE_REDRAW;
1298
0
}
1299
1300
static enum prompt_result
1301
mode_tree_search_callback(__unused struct client *c, void *data, const char *s,
1302
    enum prompt_key_result key)
1303
0
{
1304
0
  struct mode_tree_data *mtd = data;
1305
1306
0
  if (mtd->dead)
1307
0
    return (PROMPT_CLOSE);
1308
1309
0
  free(mtd->search);
1310
0
  if (s == NULL || *s == '\0')
1311
0
    mtd->search = NULL;
1312
0
  else {
1313
0
    mtd->search = xstrdup(s);
1314
0
    mtd->search_icase = mode_tree_is_lowercase(s);
1315
0
    mode_tree_search_set(mtd);
1316
0
  }
1317
1318
0
  if (key == PROMPT_KEY_HANDLED)
1319
0
    return (PROMPT_CONTINUE);
1320
0
  return (PROMPT_CLOSE);
1321
0
}
1322
1323
static enum prompt_result
1324
mode_tree_filter_callback(__unused struct client *c, void *data, const char *s,
1325
    enum prompt_key_result key)
1326
0
{
1327
0
  struct mode_tree_data *mtd = data;
1328
1329
0
  if (mtd->dead)
1330
0
    return (PROMPT_CLOSE);
1331
1332
0
  if (mtd->filter != NULL)
1333
0
    free(mtd->filter);
1334
0
  if (s == NULL || *s == '\0')
1335
0
    mtd->filter = NULL;
1336
0
  else
1337
0
    mtd->filter = xstrdup(s);
1338
1339
0
  mode_tree_build(mtd);
1340
0
  mode_tree_draw(mtd);
1341
0
  mtd->wp->flags |= PANE_REDRAW;
1342
1343
0
  if (key == PROMPT_KEY_HANDLED)
1344
0
    return (PROMPT_CONTINUE);
1345
0
  return (PROMPT_CLOSE);
1346
0
}
1347
1348
static void
1349
mode_tree_clear_filter(struct mode_tree_data *mtd)
1350
0
{
1351
0
  free(mtd->filter);
1352
0
  mtd->filter = NULL;
1353
1354
0
  mode_tree_build(mtd);
1355
0
  mode_tree_draw(mtd);
1356
0
  mtd->wp->flags |= PANE_REDRAW;
1357
0
}
1358
1359
static void
1360
mode_tree_menu_callback(__unused struct menu *menu, __unused u_int idx,
1361
    key_code key, void *data)
1362
0
{
1363
0
  struct mode_tree_menu *mtm = data;
1364
0
  struct mode_tree_data *mtd = mtm->data;
1365
1366
0
  if (mtd->dead || key == KEYC_NONE)
1367
0
    goto out;
1368
1369
0
  if (mtm->line >= mtd->line_size)
1370
0
    goto out;
1371
0
  mtd->current = mtm->line;
1372
0
  mtd->menucb(mtd->modedata, mtm->c, key);
1373
1374
0
out:
1375
0
  mode_tree_remove_ref(mtd);
1376
0
  free(mtm);
1377
0
}
1378
1379
static void
1380
mode_tree_display_menu(struct mode_tree_data *mtd, struct client *c, u_int x,
1381
    u_int y, int outside)
1382
0
{
1383
0
  struct window   *w = mtd->wp->window;
1384
0
  struct mode_tree_item *mti;
1385
0
  struct menu   *menu;
1386
0
  const struct menu_item  *items;
1387
0
  struct mode_tree_menu *mtm;
1388
0
  char      *title;
1389
0
  enum box_lines     lines;
1390
0
  u_int      line, sx, sy;
1391
1392
0
  if (mtd->offset + y > mtd->line_size - 1)
1393
0
    line = mtd->current;
1394
0
  else
1395
0
    line = mtd->offset + y;
1396
0
  mti = mtd->line_list[line].item;
1397
1398
0
  if (!outside) {
1399
0
    items = mtd->menu;
1400
0
    xasprintf(&title, "#[align=centre]%s", mti->name);
1401
0
  } else {
1402
0
    items = mode_tree_menu_items;
1403
0
    title = xstrdup("");
1404
0
  }
1405
0
  menu = menu_create(title);
1406
0
  menu_add_items(menu, items, NULL, c, NULL);
1407
0
  free(title);
1408
1409
0
  mtm = xmalloc(sizeof *mtm);
1410
0
  mtm->data = mtd;
1411
0
  mtm->c = c;
1412
0
  mtm->line = line;
1413
0
  mtd->references++;
1414
1415
0
  lines = options_get_number(w->options, "menu-border-lines");
1416
0
  menu_get_size(menu, lines, &sx, &sy);
1417
0
  if (x >= sx / 2)
1418
0
    x -= sx / 2;
1419
0
  else
1420
0
    x = 0;
1421
0
  x += mtd->wp->xoff;
1422
0
  y += mtd->wp->yoff;
1423
1424
0
  if (menu_display(menu, 0, 0, NULL, x, y, c, lines, NULL, NULL, NULL,
1425
0
      NULL, mode_tree_menu_callback, mtm) != 0) {
1426
0
    mode_tree_remove_ref(mtd);
1427
0
    free(mtm);
1428
0
    menu_free(menu);
1429
0
  }
1430
0
}
1431
1432
static void
1433
mode_tree_draw_help_line(struct screen_write_ctx *ctx,
1434
    const struct grid_cell *gc, struct format_tree *ft, const char *line,
1435
    const char *item, u_int x, u_int y, u_int w)
1436
0
{
1437
0
  char  *expanded, *replaced;
1438
1439
0
  replaced = cmd_template_replace(line, item, 1);
1440
0
  expanded = format_expand(ft, replaced);
1441
0
  free(replaced);
1442
0
  screen_write_cursormove(ctx, x, y, 0);
1443
0
  screen_write_clearcharacter(ctx, w, gc->bg);
1444
0
  screen_write_cursormove(ctx, x, y, 0);
1445
0
  format_draw(ctx, gc, w, expanded, NULL, 0);
1446
0
  free(expanded);
1447
0
}
1448
1449
static void
1450
mode_tree_draw_help(struct mode_tree_data *mtd, struct screen_write_ctx *ctx)
1451
0
{
1452
0
  struct screen    *s = &mtd->screen;
1453
0
  struct options     *oo = mtd->wp->window->options;
1454
0
  struct grid_cell    box_gc, gc;
1455
0
  struct format_tree   *ft;
1456
0
  const char    **line, **lines = NULL, *item = "item";
1457
0
  u_int       sx = screen_size_x(s), sy = screen_size_y(s);
1458
0
  u_int       x, y, w, h = 0, box_w, box_h;
1459
1460
0
  if (mtd->helpcb == NULL)
1461
0
    w = MODE_TREE_HELP_DEFAULT_WIDTH;
1462
0
  else {
1463
0
    lines = mtd->helpcb(&w, &item);
1464
0
    if (w < MODE_TREE_HELP_DEFAULT_WIDTH)
1465
0
      w = MODE_TREE_HELP_DEFAULT_WIDTH;
1466
0
  }
1467
0
  for (line = mode_tree_help_start; *line != NULL; line++)
1468
0
    h++;
1469
0
  for (line = lines; line != NULL && *line != NULL; line++)
1470
0
    h++;
1471
0
  for (line = mode_tree_help_end; *line != NULL; line++)
1472
0
    h++;
1473
1474
0
  box_w = w + 2;
1475
0
  box_h = h + 2;
1476
0
  if (sx < box_w || sy < box_h)
1477
0
    return;
1478
0
  x = (sx - box_w) / 2;
1479
0
  y = (sy - box_h) / 2;
1480
1481
0
  memcpy(&box_gc, &grid_default_cell, sizeof box_gc);
1482
0
  style_apply(&box_gc, oo, "tree-mode-border-style", NULL);
1483
0
  memcpy(&gc, &grid_default_cell, sizeof gc);
1484
0
  ft = format_create_defaults(NULL, NULL, NULL, NULL, mtd->wp);
1485
0
  screen_write_cursormove(ctx, x, y, 0);
1486
0
  screen_write_box(ctx, box_w, box_h, BOX_LINES_DEFAULT, &box_gc, NULL);
1487
1488
0
  y++;
1489
0
  x++;
1490
0
  for (line = mode_tree_help_start; *line != NULL; line++, y++)
1491
0
    mode_tree_draw_help_line(ctx, &gc, ft, *line, item, x, y, w);
1492
0
  for (line = lines; line != NULL && *line != NULL; line++, y++)
1493
0
    mode_tree_draw_help_line(ctx, &gc, ft, *line, item, x, y, w);
1494
0
  for (line = mode_tree_help_end; *line != NULL; line++, y++)
1495
0
    mode_tree_draw_help_line(ctx, &gc, ft, *line, item, x, y, w);
1496
0
  format_free(ft);
1497
0
}
1498
1499
static void
1500
mode_tree_display_help(struct mode_tree_data *mtd)
1501
0
{
1502
0
  mtd->help = 1;
1503
0
  mode_tree_draw(mtd);
1504
0
}
1505
1506
int
1507
mode_tree_key(struct mode_tree_data *mtd, struct client *c, key_code *key,
1508
    struct mouse_event *m, u_int *xp, u_int *yp)
1509
0
{
1510
0
  struct mode_tree_line *line;
1511
0
  struct mode_tree_item *current, *parent, *mti;
1512
0
  u_int      i, x, y, py, sx;
1513
0
  int      choice, preview;
1514
0
  enum prompt_key_result   result;
1515
0
  int      redraw;
1516
0
  struct prompt   *prompt;
1517
0
  struct mode_tree_prompt *mtp;
1518
1519
0
  if (mtd->line_size == 0) {
1520
0
    *key = KEYC_NONE;
1521
0
    return (1);
1522
0
  }
1523
1524
0
  if (mtd->prompt != NULL) {
1525
0
    redraw = 0;
1526
0
    prompt = mtd->prompt;
1527
1528
0
    mtp = mtd->prompt_data;
1529
0
    if (mtp != NULL)
1530
0
      mtp->c = c;
1531
0
    if (KEYC_IS_MOUSE(*key)) {
1532
0
      if (m == NULL ||
1533
0
          MOUSE_BUTTONS(m->b) != MOUSE_BUTTON_1 ||
1534
0
          MOUSE_DRAG(m->b) || MOUSE_RELEASE(m->b) ||
1535
0
          cmd_mouse_at(mtd->wp, m, &x, &y, 0) != 0)
1536
0
        result = PROMPT_KEY_NOT_HANDLED;
1537
0
      else {
1538
0
        sx = screen_size_x(&mtd->screen);
1539
0
        if (mtd->prompt_top)
1540
0
          py = 0;
1541
0
        else
1542
0
          py = screen_size_y(&mtd->screen) - 1;
1543
0
        if (y == py) {
1544
0
          result = prompt_mouse(prompt, x, 0, sx,
1545
0
              &redraw);
1546
0
        } else
1547
0
          result = PROMPT_KEY_NOT_HANDLED;
1548
0
      }
1549
0
    } else
1550
0
      result = prompt_key(prompt, *key, &redraw);
1551
0
    if (mtd->prompt_data == mtp && mtp != NULL)
1552
0
      mtp->c = NULL;
1553
1554
    /*
1555
     * Only an explicit close or the prompt marking itself closed
1556
     * ends it; cursor movement and editing keep it open.
1557
     */
1558
0
    if (mtd->prompt == prompt &&
1559
0
        (result == PROMPT_KEY_CLOSE || prompt_closed(prompt)))
1560
0
      mode_tree_clear_prompt(mtd);
1561
1562
0
    if (redraw || mtd->prompt != prompt) {
1563
0
      mode_tree_draw(mtd);
1564
0
      mtd->wp->flags |= PANE_REDRAW;
1565
0
    }
1566
0
    if (result != PROMPT_KEY_NOT_HANDLED) {
1567
0
      *key = KEYC_NONE;
1568
0
      return (0);
1569
0
    }
1570
0
  }
1571
1572
0
  if (mtd->help) {
1573
0
    if (KEYC_IS_MOUSE(*key)) {
1574
0
      *key = KEYC_NONE;
1575
0
      return (0);
1576
0
    }
1577
0
    if (*key == KEYC_FOCUS_IN || *key == KEYC_FOCUS_OUT) {
1578
0
      *key = KEYC_NONE;
1579
0
      return (0);
1580
0
    }
1581
0
    mtd->help = 0;
1582
0
    mode_tree_draw(mtd);
1583
0
    *key = KEYC_NONE;
1584
0
    return (0);
1585
0
  }
1586
1587
0
  if (KEYC_IS_MOUSE(*key) && m != NULL) {
1588
0
    if (cmd_mouse_at(mtd->wp, m, &x, &y, 0) != 0) {
1589
0
      *key = KEYC_NONE;
1590
0
      return (0);
1591
0
    }
1592
0
    if (xp != NULL)
1593
0
      *xp = x;
1594
0
    if (yp != NULL)
1595
0
      *yp = y;
1596
0
    if (x > mtd->width || y > mtd->height) {
1597
0
      preview = mtd->preview;
1598
0
      if (*key == KEYC_MOUSEDOWN3_PANE)
1599
0
        mode_tree_display_menu(mtd, c, x, y, 1);
1600
0
      if (preview == MODE_TREE_PREVIEW_OFF)
1601
0
        *key = KEYC_NONE;
1602
0
      return (0);
1603
0
    }
1604
0
    if (mtd->offset + y < mtd->line_size) {
1605
0
      if (*key == KEYC_MOUSEDOWN1_PANE ||
1606
0
          *key == KEYC_MOUSEDOWN3_PANE ||
1607
0
          *key == KEYC_DOUBLECLICK1_PANE)
1608
0
        mtd->current = mtd->offset + y;
1609
0
      if (*key == KEYC_DOUBLECLICK1_PANE)
1610
0
        *key = '\r';
1611
0
      else {
1612
0
        if (*key == KEYC_MOUSEDOWN3_PANE)
1613
0
          mode_tree_display_menu(mtd, c, x, y, 0);
1614
0
        *key = KEYC_NONE;
1615
0
      }
1616
0
    } else {
1617
0
      if (*key == KEYC_MOUSEDOWN3_PANE)
1618
0
        mode_tree_display_menu(mtd, c, x, y, 0);
1619
0
      *key = KEYC_NONE;
1620
0
    }
1621
0
    return (0);
1622
0
  }
1623
1624
0
  line = &mtd->line_list[mtd->current];
1625
0
  current = line->item;
1626
1627
0
  choice = -1;
1628
0
  for (i = 0; i < mtd->line_size; i++) {
1629
0
    if (*key == mtd->line_list[i].item->key) {
1630
0
      choice = i;
1631
0
      break;
1632
0
    }
1633
0
  }
1634
0
  if (choice != -1) {
1635
0
    if ((u_int)choice > mtd->line_size - 1) {
1636
0
      *key = KEYC_NONE;
1637
0
      return (0);
1638
0
    }
1639
0
    mtd->current = choice;
1640
0
    *key = '\r';
1641
0
    return (0);
1642
0
  }
1643
1644
0
  switch (*key) {
1645
0
  case 'q':
1646
0
  case '\033': /* Escape */
1647
0
  case '['|KEYC_CTRL:
1648
0
  case 'g'|KEYC_CTRL:
1649
0
    return (1);
1650
0
  case KEYC_F1:
1651
0
  case 'h'|KEYC_CTRL:
1652
0
    mode_tree_display_help(mtd);
1653
0
    break;
1654
0
  case KEYC_UP:
1655
0
  case 'k':
1656
0
  case KEYC_WHEELUP_PANE:
1657
0
  case 'p'|KEYC_CTRL:
1658
0
    mode_tree_up(mtd, 1);
1659
0
    break;
1660
0
  case KEYC_DOWN:
1661
0
  case 'j':
1662
0
  case KEYC_WHEELDOWN_PANE:
1663
0
  case 'n'|KEYC_CTRL:
1664
0
    mode_tree_down(mtd, 1);
1665
0
    break;
1666
0
  case KEYC_UP|KEYC_SHIFT:
1667
0
  case 'K':
1668
0
    mode_tree_swap(mtd, -1);
1669
0
    break;
1670
0
  case KEYC_DOWN|KEYC_SHIFT:
1671
0
  case 'J':
1672
0
    mode_tree_swap(mtd, 1);
1673
0
    break;
1674
0
  case KEYC_PPAGE:
1675
0
  case 'b'|KEYC_CTRL:
1676
0
    for (i = 0; i < mtd->height; i++) {
1677
0
      if (mtd->current == 0)
1678
0
        break;
1679
0
      mode_tree_up(mtd, 1);
1680
0
    }
1681
0
    break;
1682
0
  case KEYC_NPAGE:
1683
0
  case 'f'|KEYC_CTRL:
1684
0
    for (i = 0; i < mtd->height; i++) {
1685
0
      if (mtd->current == mtd->line_size - 1)
1686
0
        break;
1687
0
      mode_tree_down(mtd, 1);
1688
0
    }
1689
0
    break;
1690
0
  case 'g':
1691
0
  case KEYC_HOME:
1692
0
    mtd->current = 0;
1693
0
    mtd->offset = 0;
1694
0
    break;
1695
0
  case 'G':
1696
0
  case KEYC_END:
1697
0
    mtd->current = mtd->line_size - 1;
1698
0
    if (mtd->current > mtd->height - 1)
1699
0
      mtd->offset = mtd->current - mtd->height + 1;
1700
0
    else
1701
0
      mtd->offset = 0;
1702
0
    break;
1703
0
  case 't':
1704
    /*
1705
     * Do not allow parents and children to both be tagged: untag
1706
     * all parents and children of current.
1707
     */
1708
0
    if (current->no_tag)
1709
0
      break;
1710
0
    if (!current->tagged) {
1711
0
      parent = current->parent;
1712
0
      while (parent != NULL) {
1713
0
        parent->tagged = 0;
1714
0
        parent = parent->parent;
1715
0
      }
1716
0
      mode_tree_clear_tagged(&current->children);
1717
0
      current->tagged = 1;
1718
0
    } else
1719
0
      current->tagged = 0;
1720
0
    if (m != NULL)
1721
0
      mode_tree_down(mtd, 0);
1722
0
    break;
1723
0
  case 'T':
1724
0
    for (i = 0; i < mtd->line_size; i++)
1725
0
      mtd->line_list[i].item->tagged = 0;
1726
0
    break;
1727
0
  case 't'|KEYC_CTRL:
1728
0
    for (i = 0; i < mtd->line_size; i++) {
1729
0
      if ((mtd->line_list[i].item->parent == NULL &&
1730
0
          !mtd->line_list[i].item->no_tag) ||
1731
0
          (mtd->line_list[i].item->parent != NULL &&
1732
0
          mtd->line_list[i].item->parent->no_tag))
1733
0
        mtd->line_list[i].item->tagged = 1;
1734
0
      else
1735
0
        mtd->line_list[i].item->tagged = 0;
1736
0
    }
1737
0
    break;
1738
0
  case 'O':
1739
0
    sort_next_order(&mtd->sort_crit);
1740
0
    mode_tree_build(mtd);
1741
0
    break;
1742
0
  case 'r':
1743
0
    mtd->sort_crit.reversed = !mtd->sort_crit.reversed;
1744
0
    mode_tree_build(mtd);
1745
0
    break;
1746
0
  case KEYC_LEFT:
1747
0
  case 'h':
1748
0
  case '-':
1749
0
    if (line->flat || !current->expanded)
1750
0
      current = current->parent;
1751
0
    if (current == NULL)
1752
0
      mode_tree_up(mtd, 0);
1753
0
    else {
1754
0
      current->expanded = 0;
1755
0
      mtd->current = current->line;
1756
0
      mode_tree_build(mtd);
1757
0
    }
1758
0
    break;
1759
0
  case KEYC_RIGHT:
1760
0
  case 'l':
1761
0
  case '+':
1762
0
    if (line->flat || current->expanded)
1763
0
      mode_tree_down(mtd, 0);
1764
0
    else if (!line->flat) {
1765
0
      current->expanded = 1;
1766
0
      mode_tree_build(mtd);
1767
0
    }
1768
0
    break;
1769
0
  case '-'|KEYC_META:
1770
0
    TAILQ_FOREACH(mti, &mtd->children, entry)
1771
0
      mti->expanded = 0;
1772
0
    mode_tree_build(mtd);
1773
0
    break;
1774
0
  case '+'|KEYC_META:
1775
0
    TAILQ_FOREACH(mti, &mtd->children, entry)
1776
0
      mti->expanded = 1;
1777
0
    mode_tree_build(mtd);
1778
0
    break;
1779
0
  case '?':
1780
0
  case '/':
1781
0
  case 's'|KEYC_CTRL:
1782
0
    mtd->search_dir = MODE_TREE_SEARCH_FORWARD;
1783
0
    mode_tree_set_prompt(mtd, c, "(search) ", "",
1784
0
        PROMPT_TYPE_SEARCH, PROMPT_NOFORMAT,
1785
0
        mode_tree_search_callback, NULL, mtd);
1786
0
    break;
1787
0
  case 'n':
1788
0
    mtd->search_dir = MODE_TREE_SEARCH_FORWARD;
1789
0
    mode_tree_search_set(mtd);
1790
0
    break;
1791
0
  case 'N':
1792
0
    mtd->search_dir = MODE_TREE_SEARCH_BACKWARD;
1793
0
    mode_tree_search_set(mtd);
1794
0
    break;
1795
0
  case 'f':
1796
0
    mode_tree_set_prompt(mtd, c, "(filter) ", mtd->filter,
1797
0
        PROMPT_TYPE_SEARCH, PROMPT_NOFORMAT,
1798
0
        mode_tree_filter_callback, NULL, mtd);
1799
0
    break;
1800
0
  case 'c':
1801
0
    mode_tree_clear_prompt(mtd);
1802
0
    mode_tree_clear_filter(mtd);
1803
0
    break;
1804
0
  case 'v':
1805
0
    switch (mtd->preview) {
1806
0
    case MODE_TREE_PREVIEW_OFF:
1807
0
      mtd->preview = MODE_TREE_PREVIEW_BIG;
1808
0
      break;
1809
0
    case MODE_TREE_PREVIEW_NORMAL:
1810
0
      mtd->preview = MODE_TREE_PREVIEW_OFF;
1811
0
      break;
1812
0
    case MODE_TREE_PREVIEW_BIG:
1813
0
      mtd->preview = MODE_TREE_PREVIEW_NORMAL;
1814
0
      break;
1815
0
    }
1816
0
    mode_tree_build(mtd);
1817
0
    if (mtd->preview != MODE_TREE_PREVIEW_OFF)
1818
0
      mode_tree_check_selected(mtd);
1819
0
    break;
1820
0
  }
1821
0
  return (0);
1822
0
}
1823
1824
void
1825
mode_tree_run_command(struct client *c, struct cmd_find_state *fs,
1826
    const char *template, const char *name)
1827
0
{
1828
0
  struct cmdq_state *state;
1829
0
  char      *command, *error;
1830
0
  enum cmd_parse_status  status;
1831
1832
0
  command = cmd_template_replace(template, name, 1);
1833
0
  if (command != NULL && *command != '\0') {
1834
0
    state = cmdq_new_state(fs, NULL, 0);
1835
0
    status = cmd_parse_and_append(command, NULL, c, state, &error);
1836
0
    if (status == CMD_PARSE_ERROR) {
1837
0
      if (c != NULL) {
1838
        *error = toupper((u_char)*error);
1839
0
        status_message_set(c, -1, 1, 0, 0, "%s", error);
1840
0
      }
1841
0
      free(error);
1842
0
    }
1843
0
    cmdq_free_state(state);
1844
0
  }
1845
0
  free(command);
1846
0
}