Line | Count | Source |
1 | | /* $OpenBSD: mode-tree.c,v 1.105 2026/10/16 07:59:06 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 | return; |
706 | | |
707 | 0 | mode_tree_clear_prompt(mtd); |
708 | 0 | mode_tree_free_items(&mtd->children); |
709 | 0 | mode_tree_clear_lines(mtd); |
710 | 0 | screen_free(&mtd->screen); |
711 | |
|
712 | 0 | free(mtd->search); |
713 | 0 | free(mtd->filter); |
714 | 0 | free(mtd); |
715 | 0 | } |
716 | | |
717 | | void |
718 | | mode_tree_free(struct mode_tree_data *mtd) |
719 | 0 | { |
720 | 0 | struct window_pane *wp = mtd->wp; |
721 | |
|
722 | 0 | if (mtd->zoomed == 0) |
723 | 0 | server_unzoom_window(wp->window); |
724 | |
|
725 | 0 | mtd->dead = 1; |
726 | 0 | mode_tree_remove_ref(mtd); |
727 | 0 | } |
728 | | |
729 | | void |
730 | | mode_tree_resize(struct mode_tree_data *mtd, u_int sx, u_int sy) |
731 | 0 | { |
732 | 0 | struct screen *s = &mtd->screen; |
733 | |
|
734 | 0 | screen_resize(s, sx, sy, 0); |
735 | |
|
736 | 0 | mode_tree_build(mtd); |
737 | 0 | mode_tree_draw(mtd); |
738 | |
|
739 | 0 | mtd->wp->flags |= PANE_REDRAW; |
740 | 0 | } |
741 | | |
742 | | struct mode_tree_item * |
743 | | mode_tree_add(struct mode_tree_data *mtd, struct mode_tree_item *parent, |
744 | | void *itemdata, uint64_t tag, const char *name, const char *text, |
745 | | int expanded) |
746 | 0 | { |
747 | 0 | struct mode_tree_item *mti, *saved; |
748 | |
|
749 | 0 | log_debug("%s: %llu, %s %s", __func__, (unsigned long long)tag, |
750 | 0 | name, (text == NULL ? "" : text)); |
751 | |
|
752 | 0 | mti = xcalloc(1, sizeof *mti); |
753 | 0 | mti->parent = parent; |
754 | 0 | mti->itemdata = itemdata; |
755 | |
|
756 | 0 | mti->tag = tag; |
757 | 0 | mti->name = xstrdup(name); |
758 | 0 | if (text != NULL) |
759 | 0 | mti->text = xstrdup(text); |
760 | |
|
761 | 0 | saved = mode_tree_find_item(&mtd->saved, tag); |
762 | 0 | if (saved != NULL) { |
763 | 0 | if (parent == NULL || parent->expanded) |
764 | 0 | mti->tagged = saved->tagged; |
765 | 0 | mti->expanded = saved->expanded; |
766 | 0 | } else if (expanded == -1) |
767 | 0 | mti->expanded = 1; |
768 | 0 | else |
769 | 0 | mti->expanded = expanded; |
770 | |
|
771 | 0 | TAILQ_INIT(&mti->children); |
772 | |
|
773 | 0 | if (parent != NULL) |
774 | 0 | TAILQ_INSERT_TAIL(&parent->children, mti, entry); |
775 | 0 | else |
776 | 0 | TAILQ_INSERT_TAIL(&mtd->children, mti, entry); |
777 | |
|
778 | 0 | return (mti); |
779 | 0 | } |
780 | | |
781 | | void |
782 | | mode_tree_view_name(struct mode_tree_data *mtd, const char *name) |
783 | 0 | { |
784 | 0 | mtd->view_name = name; |
785 | 0 | } |
786 | | |
787 | | void |
788 | | mode_tree_draw_as_parent(struct mode_tree_item *mti) |
789 | 0 | { |
790 | 0 | mti->draw_as_parent = 1; |
791 | 0 | } |
792 | | |
793 | | void |
794 | | mode_tree_no_tag(struct mode_tree_item *mti) |
795 | 0 | { |
796 | 0 | mti->no_tag = 1; |
797 | 0 | } |
798 | | |
799 | | /* |
800 | | * Set the alignment of the item name: -1 to align left, 0 (default) to not |
801 | | * align, or 1 to align right. |
802 | | */ |
803 | | void |
804 | | mode_tree_align(struct mode_tree_item *mti, int align) |
805 | 0 | { |
806 | 0 | mti->align = align; |
807 | 0 | } |
808 | | |
809 | | void |
810 | | mode_tree_remove(struct mode_tree_data *mtd, struct mode_tree_item *mti) |
811 | 0 | { |
812 | 0 | struct mode_tree_item *parent = mti->parent; |
813 | |
|
814 | 0 | if (parent != NULL) |
815 | 0 | TAILQ_REMOVE(&parent->children, mti, entry); |
816 | 0 | else |
817 | 0 | TAILQ_REMOVE(&mtd->children, mti, entry); |
818 | 0 | mode_tree_free_item(mti); |
819 | 0 | } |
820 | | |
821 | | void |
822 | | mode_tree_draw(struct mode_tree_data *mtd) |
823 | 0 | { |
824 | 0 | struct window_pane *wp = mtd->wp; |
825 | 0 | struct screen *s = &mtd->screen; |
826 | 0 | struct mode_tree_line *line; |
827 | 0 | struct mode_tree_item *mti; |
828 | 0 | struct options *oo = wp->window->options; |
829 | 0 | struct screen_write_ctx ctx; |
830 | 0 | struct format_tree *ft; |
831 | 0 | struct grid_cell gc0, gc, box_gc; |
832 | 0 | u_int w, h, i, sy, box_x, box_y; |
833 | 0 | u_int width, text_width, prefix_width, left; |
834 | 0 | char *text, *prefix; |
835 | 0 | const char *tag, *separator; |
836 | 0 | size_t n; |
837 | 0 | int keylen, alignlen[mtd->maxdepth + 1]; |
838 | 0 | int dfg, dfg0; |
839 | |
|
840 | 0 | if (mtd->line_size == 0) |
841 | 0 | return; |
842 | | |
843 | 0 | w = mtd->width; |
844 | 0 | h = mtd->height; |
845 | 0 | if (w == 0 || h == 0) |
846 | 0 | return; |
847 | | |
848 | 0 | memcpy(&gc0, &grid_default_cell, sizeof gc0); |
849 | 0 | memcpy(&gc, &grid_default_cell, sizeof gc); |
850 | 0 | style_apply(&gc, oo, "tree-mode-selection-style", NULL); |
851 | 0 | memcpy(&box_gc, &grid_default_cell, sizeof box_gc); |
852 | 0 | style_apply(&box_gc, oo, "tree-mode-border-style", NULL); |
853 | |
|
854 | 0 | dfg = gc.fg; |
855 | 0 | dfg0 = gc0.fg; |
856 | |
|
857 | 0 | screen_write_start(&ctx, s); |
858 | 0 | screen_write_clearscreen(&ctx, 8); |
859 | 0 | ft = format_create_defaults(NULL, NULL, NULL, NULL, wp); |
860 | |
|
861 | 0 | keylen = 0; |
862 | 0 | for (i = 0; i < mtd->line_size; i++) { |
863 | 0 | mti = mtd->line_list[i].item; |
864 | 0 | if (mti->key == KEYC_NONE) |
865 | 0 | continue; |
866 | 0 | if ((int)mti->keylen + 3 > keylen) |
867 | 0 | keylen = mti->keylen + 3; |
868 | 0 | } |
869 | |
|
870 | 0 | for (i = 0; i < mtd->maxdepth + 1; i++) |
871 | 0 | alignlen[i] = 0; |
872 | 0 | for (i = 0; i < mtd->line_size; i++) { |
873 | 0 | line = &mtd->line_list[i]; |
874 | 0 | mti = line->item; |
875 | 0 | if (mti->align && |
876 | 0 | (int)strlen(mti->name) > alignlen[line->depth]) |
877 | 0 | alignlen[line->depth] = strlen(mti->name); |
878 | 0 | } |
879 | |
|
880 | 0 | for (i = 0; i < mtd->line_size; i++) { |
881 | 0 | if (i < mtd->offset) |
882 | 0 | continue; |
883 | 0 | if (i > mtd->offset + h - 1) |
884 | 0 | break; |
885 | 0 | line = &mtd->line_list[i]; |
886 | 0 | mti = line->item; |
887 | |
|
888 | 0 | screen_write_cursormove(&ctx, 0, i - mtd->offset, 0); |
889 | |
|
890 | 0 | if (mti->key != KEYC_NONE) |
891 | 0 | format_add(ft, "mode_tree_key", "%s", mti->keystr); |
892 | 0 | else |
893 | 0 | format_add(ft, "mode_tree_key", "%s", ""); |
894 | 0 | format_add(ft, "mode_tree_key_width", "%d", keylen); |
895 | 0 | format_add(ft, "mode_tree_selected", "%d", i == mtd->current); |
896 | 0 | if (line->depth == 0) { |
897 | 0 | format_add(ft, "mode_tree_repeat", "%u", 0); |
898 | 0 | format_add(ft, "mode_tree_branch", "0"); |
899 | 0 | format_add(ft, "mode_tree_parent_last", "0"); |
900 | 0 | } else { |
901 | 0 | format_add(ft, "mode_tree_repeat", "%u", |
902 | 0 | line->depth - 1); |
903 | 0 | format_add(ft, "mode_tree_branch", "1"); |
904 | 0 | if (mti->parent != NULL && |
905 | 0 | mtd->line_list[mti->parent->line].last) |
906 | 0 | format_add(ft, "mode_tree_parent_last", "1"); |
907 | 0 | else |
908 | 0 | format_add(ft, "mode_tree_parent_last", "0"); |
909 | 0 | } |
910 | 0 | if (TAILQ_EMPTY(&mti->children)) |
911 | 0 | format_add(ft, "mode_tree_has_children", "0"); |
912 | 0 | else |
913 | 0 | format_add(ft, "mode_tree_has_children", "1"); |
914 | 0 | format_add(ft, "mode_tree_last", "%d", line->last); |
915 | 0 | format_add(ft, "mode_tree_expanded", "%d", mti->expanded); |
916 | 0 | format_add(ft, "mode_tree_flat", "%d", line->flat); |
917 | 0 | prefix = format_expand(ft, MODE_TREE_PREFIX_FORMAT); |
918 | 0 | prefix_width = format_width(prefix); |
919 | 0 | if (prefix_width > w) |
920 | 0 | prefix_width = w; |
921 | |
|
922 | 0 | if (mti->tagged) |
923 | 0 | tag = "*"; |
924 | 0 | else |
925 | 0 | tag = ""; |
926 | 0 | if (mti->text != NULL) |
927 | 0 | separator = "#[fg=themelightgrey]: #[default]"; |
928 | 0 | else |
929 | 0 | separator = ""; |
930 | 0 | xasprintf(&text, "%*s%s%s", |
931 | 0 | mti->align * alignlen[line->depth], mti->name, tag, separator); |
932 | 0 | text_width = format_width(text); |
933 | 0 | left = (prefix_width < w) ? (w - prefix_width) : 0; |
934 | 0 | if (text_width > left) |
935 | 0 | text_width = left; |
936 | 0 | width = prefix_width + text_width; |
937 | |
|
938 | 0 | if (mti->tagged) { |
939 | 0 | gc.fg = COLOUR_THEME_CYAN|COLOUR_FLAG_THEME; |
940 | 0 | gc0.fg = COLOUR_THEME_CYAN|COLOUR_FLAG_THEME; |
941 | 0 | } |
942 | |
|
943 | 0 | if (i != mtd->current) { |
944 | 0 | screen_write_clearendofline(&ctx, 8); |
945 | 0 | format_draw(&ctx, &grid_default_cell, prefix_width, |
946 | 0 | prefix, NULL, 0); |
947 | 0 | if (left != 0) { |
948 | 0 | screen_write_cursormove(&ctx, prefix_width, |
949 | 0 | i - mtd->offset, 0); |
950 | 0 | format_draw(&ctx, &gc0, left, text, NULL, 0); |
951 | 0 | if (mti->text != NULL && width < w) { |
952 | 0 | screen_write_cursormove(&ctx, width, |
953 | 0 | i - mtd->offset, 0); |
954 | 0 | format_draw(&ctx, &gc0, w - width, |
955 | 0 | mti->text, NULL, 0); |
956 | 0 | } |
957 | 0 | } |
958 | 0 | } else { |
959 | 0 | screen_write_clearendofline(&ctx, gc.bg); |
960 | 0 | format_draw(&ctx, &gc, prefix_width, prefix, NULL, |
961 | 0 | FORMAT_DRAW_DEFAULT_COLOURS); |
962 | 0 | if (left != 0) { |
963 | 0 | screen_write_cursormove(&ctx, prefix_width, |
964 | 0 | i - mtd->offset, 0); |
965 | 0 | format_draw(&ctx, &gc, left, text, NULL, |
966 | 0 | FORMAT_DRAW_DEFAULT_COLOURS); |
967 | 0 | if (mti->text != NULL && width < w) { |
968 | 0 | screen_write_cursormove(&ctx, width, |
969 | 0 | i - mtd->offset, 0); |
970 | 0 | format_draw(&ctx, &gc, w - width, |
971 | 0 | mti->text, NULL, |
972 | 0 | FORMAT_DRAW_DEFAULT_COLOURS); |
973 | 0 | } |
974 | 0 | } |
975 | 0 | } |
976 | 0 | free(text); |
977 | 0 | free(prefix); |
978 | |
|
979 | 0 | if (mti->tagged) { |
980 | 0 | gc.fg = dfg; |
981 | 0 | gc0.fg = dfg0; |
982 | 0 | } |
983 | 0 | } |
984 | 0 | format_free(ft); |
985 | |
|
986 | 0 | if (mtd->preview == MODE_TREE_PREVIEW_OFF) |
987 | 0 | goto done; |
988 | | |
989 | 0 | sy = screen_size_y(s); |
990 | 0 | if (sy <= 4 || h < 2 || sy - h <= 4 || w <= 4) |
991 | 0 | goto done; |
992 | | |
993 | 0 | line = &mtd->line_list[mtd->current]; |
994 | 0 | mti = line->item; |
995 | 0 | if (mti->draw_as_parent) |
996 | 0 | mti = mti->parent; |
997 | |
|
998 | 0 | screen_write_cursormove(&ctx, 0, h, 0); |
999 | 0 | screen_write_box(&ctx, w, sy - h, BOX_LINES_DEFAULT, &box_gc, NULL); |
1000 | |
|
1001 | 0 | if (mtd->sort_crit.order_seq != NULL) { |
1002 | 0 | xasprintf(&text, " %s (sort: %s%s)%s%s%s", mti->name, |
1003 | 0 | sort_order_to_string(mtd->sort_crit.order), |
1004 | 0 | mtd->sort_crit.reversed ? ", reversed" : "", |
1005 | 0 | mtd->view_name == NULL ? "" : " (view: ", |
1006 | 0 | mtd->view_name == NULL ? "" : mtd->view_name, |
1007 | 0 | mtd->view_name == NULL ? "" : ")"); |
1008 | 0 | } else |
1009 | 0 | xasprintf(&text, " %s", mti->name); |
1010 | 0 | if (w - 2 >= strlen(text)) { |
1011 | 0 | screen_write_cursormove(&ctx, 1, h, 0); |
1012 | 0 | screen_write_puts(&ctx, &box_gc, "%s", text); |
1013 | |
|
1014 | 0 | if (mtd->no_matches) |
1015 | 0 | n = (sizeof "no matches") - 1; |
1016 | 0 | else |
1017 | 0 | n = (sizeof "active") - 1; |
1018 | 0 | if (mtd->filter != NULL && w - 2 >= strlen(text) + 10 + n + 2) { |
1019 | 0 | screen_write_puts(&ctx, &box_gc, " (filter: "); |
1020 | 0 | if (mtd->no_matches) |
1021 | 0 | screen_write_puts(&ctx, &box_gc, "no matches"); |
1022 | 0 | else |
1023 | 0 | screen_write_puts(&ctx, &box_gc, "active"); |
1024 | 0 | screen_write_puts(&ctx, &box_gc, ") "); |
1025 | 0 | } else |
1026 | 0 | screen_write_puts(&ctx, &box_gc, " "); |
1027 | 0 | } |
1028 | 0 | free(text); |
1029 | |
|
1030 | 0 | box_x = w - 4; |
1031 | 0 | box_y = sy - h - 2; |
1032 | |
|
1033 | 0 | if (box_x != 0 && box_y != 0) { |
1034 | 0 | screen_write_cursormove(&ctx, 2, h + 1, 0); |
1035 | 0 | mtd->drawcb(mtd->modedata, mti->itemdata, &ctx, box_x, box_y); |
1036 | 0 | } |
1037 | |
|
1038 | 0 | done: |
1039 | 0 | if (mtd->help) |
1040 | 0 | mode_tree_draw_help(mtd, &ctx); |
1041 | 0 | if (mtd->prompt != NULL) |
1042 | 0 | mode_tree_draw_prompt(mtd, &ctx); |
1043 | 0 | else { |
1044 | 0 | s->mode &= ~MODE_CURSOR; |
1045 | 0 | screen_write_cursormove(&ctx, 0, mtd->current - mtd->offset, 0); |
1046 | 0 | } |
1047 | 0 | screen_write_stop(&ctx); |
1048 | 0 | } |
1049 | | |
1050 | | static void |
1051 | | mode_tree_draw_prompt(struct mode_tree_data *mtd, struct screen_write_ctx *ctx) |
1052 | 0 | { |
1053 | 0 | struct screen *s = &mtd->screen; |
1054 | 0 | struct prompt_draw_data pdd; |
1055 | 0 | u_int sx = screen_size_x(s), sy = screen_size_y(s); |
1056 | 0 | u_int py; |
1057 | |
|
1058 | 0 | if (sx == 0 || sy == 0) |
1059 | 0 | return; |
1060 | | |
1061 | 0 | if (mtd->prompt_top) |
1062 | 0 | py = 0; |
1063 | 0 | else |
1064 | 0 | py = sy - 1; |
1065 | |
|
1066 | 0 | pdd.ctx = ctx; |
1067 | 0 | pdd.cursor_x = &mtd->prompt_cx; |
1068 | 0 | pdd.area_x = 0; |
1069 | 0 | pdd.area_width = sx; |
1070 | 0 | pdd.prompt_line = py; |
1071 | |
|
1072 | 0 | s->mode |= MODE_CURSOR; |
1073 | 0 | prompt_draw(mtd->prompt, &pdd); |
1074 | 0 | screen_write_cursormove(ctx, mtd->prompt_cx, py, 0); |
1075 | 0 | } |
1076 | | |
1077 | | void |
1078 | | mode_tree_clear_prompt(struct mode_tree_data *mtd) |
1079 | 0 | { |
1080 | 0 | struct prompt *prompt = mtd->prompt; |
1081 | |
|
1082 | 0 | if (mtd->prompt != NULL) { |
1083 | 0 | mtd->prompt = NULL; |
1084 | 0 | prompt_free(prompt); |
1085 | 0 | mtd->screen.mode &= ~MODE_CURSOR; |
1086 | 0 | } |
1087 | 0 | } |
1088 | | |
1089 | | int |
1090 | | mode_tree_has_prompt(struct mode_tree_data *mtd) |
1091 | 0 | { |
1092 | 0 | return (mtd->prompt != NULL); |
1093 | 0 | } |
1094 | | |
1095 | | static enum cmd_retval |
1096 | | mode_tree_prompt_accept(struct cmdq_item *item, void *data) |
1097 | 0 | { |
1098 | 0 | struct mode_tree_data *mtd = data; |
1099 | 0 | struct client *c = cmdq_get_client(item); |
1100 | 0 | key_code key = 'y'; |
1101 | |
|
1102 | 0 | if (!mtd->dead && mtd->prompt != NULL && c != NULL) |
1103 | 0 | mode_tree_key(mtd, c, &key, NULL, NULL, NULL); |
1104 | |
|
1105 | 0 | mode_tree_remove_ref(mtd); |
1106 | 0 | return (CMD_RETURN_NORMAL); |
1107 | 0 | } |
1108 | | |
1109 | | static enum prompt_result |
1110 | | mode_tree_prompt_input_callback(void *data, const char *s, |
1111 | | enum prompt_key_result key) |
1112 | 0 | { |
1113 | 0 | struct mode_tree_prompt *mtp = data; |
1114 | |
|
1115 | 0 | if (mtp->inputcb != NULL) |
1116 | 0 | return (mtp->inputcb(mtp->c, mtp->data, s, key)); |
1117 | 0 | return (PROMPT_CLOSE); |
1118 | 0 | } |
1119 | | |
1120 | | static void |
1121 | | mode_tree_prompt_free_callback(void *data) |
1122 | 0 | { |
1123 | 0 | struct mode_tree_prompt *mtp = data; |
1124 | |
|
1125 | 0 | if (mtp->mtd->prompt_data == mtp) |
1126 | 0 | mtp->mtd->prompt_data = NULL; |
1127 | 0 | if (mtp->freecb != NULL) |
1128 | 0 | mtp->freecb(mtp->data); |
1129 | 0 | free(mtp); |
1130 | 0 | } |
1131 | | |
1132 | | void |
1133 | | mode_tree_set_prompt(struct mode_tree_data *mtd, struct client *c, |
1134 | | const char *prompt, const char *input, enum prompt_type type, int flags, |
1135 | | mode_tree_prompt_input_cb inputcb, prompt_free_cb freecb, void *data) |
1136 | 0 | { |
1137 | 0 | struct session *s; |
1138 | 0 | struct options *oo; |
1139 | 0 | struct prompt_create_data pd; |
1140 | 0 | struct mode_tree_prompt *mtp; |
1141 | |
|
1142 | 0 | if (c != NULL && c->session != NULL) { |
1143 | 0 | s = c->session; |
1144 | 0 | oo = s->options; |
1145 | 0 | } else { |
1146 | 0 | s = NULL; |
1147 | 0 | oo = global_s_options; |
1148 | 0 | } |
1149 | |
|
1150 | 0 | mode_tree_clear_prompt(mtd); |
1151 | |
|
1152 | 0 | mtp = xcalloc(1, sizeof *mtp); |
1153 | 0 | mtp->mtd = mtd; |
1154 | 0 | mtp->c = c; |
1155 | 0 | mtp->inputcb = inputcb; |
1156 | 0 | mtp->freecb = freecb; |
1157 | 0 | mtp->data = data; |
1158 | 0 | mtd->prompt_top = options_get_number(oo, "status-position") == 0; |
1159 | |
|
1160 | 0 | memset(&pd, 0, sizeof pd); |
1161 | 0 | prompt_set_options(&pd, s); |
1162 | 0 | pd.prompt = prompt; |
1163 | 0 | pd.input = input; |
1164 | 0 | pd.type = type; |
1165 | 0 | pd.flags = flags|PROMPT_ISMODE; |
1166 | 0 | pd.inputcb = mode_tree_prompt_input_callback; |
1167 | 0 | pd.freecb = mode_tree_prompt_free_callback; |
1168 | 0 | pd.data = mtp; |
1169 | 0 | mtd->prompt = prompt_create(&pd); |
1170 | 0 | mtd->prompt_data = mtp; |
1171 | |
|
1172 | 0 | mode_tree_draw(mtd); |
1173 | 0 | mtd->wp->flags |= PANE_REDRAW; |
1174 | |
|
1175 | 0 | if ((flags & PROMPT_SINGLE) && (flags & PROMPT_ACCEPT) && c != NULL) { |
1176 | 0 | mtd->references++; |
1177 | 0 | cmdq_append(c, cmdq_get_callback(mode_tree_prompt_accept, mtd)); |
1178 | 0 | } |
1179 | 0 | } |
1180 | | |
1181 | | static struct mode_tree_item * |
1182 | | mode_tree_search_backward(struct mode_tree_data *mtd) |
1183 | 0 | { |
1184 | 0 | struct mode_tree_item *mti, *last, *prev; |
1185 | 0 | int icase = mtd->search_icase; |
1186 | |
|
1187 | 0 | if (mtd->search == NULL) |
1188 | 0 | return (NULL); |
1189 | | |
1190 | 0 | mti = last = mtd->line_list[mtd->current].item; |
1191 | 0 | for (;;) { |
1192 | 0 | if ((prev = TAILQ_PREV(mti, mode_tree_list, entry)) != NULL) { |
1193 | | /* Point to the last child in the previous subtree. */ |
1194 | 0 | while (!TAILQ_EMPTY(&prev->children)) { |
1195 | 0 | prev = TAILQ_LAST(&prev->children, |
1196 | 0 | mode_tree_list); |
1197 | 0 | } |
1198 | 0 | mti = prev; |
1199 | 0 | } else { |
1200 | | /* If prev is NULL, jump to the parent. */ |
1201 | 0 | mti = mti->parent; |
1202 | 0 | } |
1203 | |
|
1204 | 0 | if (mti == NULL) { |
1205 | | /* Point to the last child in the last root subtree. */ |
1206 | 0 | prev = TAILQ_LAST(&mtd->children, mode_tree_list); |
1207 | 0 | while (!TAILQ_EMPTY(&prev->children)) { |
1208 | 0 | prev = TAILQ_LAST(&prev->children, |
1209 | 0 | mode_tree_list); |
1210 | 0 | } |
1211 | 0 | mti = prev; |
1212 | 0 | } |
1213 | 0 | if (mti == last) |
1214 | 0 | break; |
1215 | | |
1216 | 0 | if (mtd->searchcb == NULL) { |
1217 | 0 | if (!icase && strstr(mti->name, mtd->search) != NULL) |
1218 | 0 | return (mti); |
1219 | 0 | if (icase && strcasestr(mti->name, mtd->search) != NULL) |
1220 | 0 | return (mti); |
1221 | 0 | continue; |
1222 | 0 | } |
1223 | 0 | if (mtd->searchcb(mtd->modedata, mti->itemdata, mtd->search, |
1224 | 0 | icase)) |
1225 | 0 | return (mti); |
1226 | 0 | } |
1227 | 0 | return (NULL); |
1228 | 0 | } |
1229 | | |
1230 | | static struct mode_tree_item * |
1231 | | mode_tree_search_forward(struct mode_tree_data *mtd) |
1232 | 0 | { |
1233 | 0 | struct mode_tree_item *mti, *last, *next; |
1234 | 0 | int icase = mtd->search_icase; |
1235 | |
|
1236 | 0 | if (mtd->search == NULL) |
1237 | 0 | return (NULL); |
1238 | | |
1239 | 0 | mti = last = mtd->line_list[mtd->current].item; |
1240 | 0 | for (;;) { |
1241 | 0 | if (!TAILQ_EMPTY(&mti->children)) |
1242 | 0 | mti = TAILQ_FIRST(&mti->children); |
1243 | 0 | else if ((next = TAILQ_NEXT(mti, entry)) != NULL) |
1244 | 0 | mti = next; |
1245 | 0 | else { |
1246 | 0 | for (;;) { |
1247 | 0 | mti = mti->parent; |
1248 | 0 | if (mti == NULL) |
1249 | 0 | break; |
1250 | 0 | if ((next = TAILQ_NEXT(mti, entry)) != NULL) { |
1251 | 0 | mti = next; |
1252 | 0 | break; |
1253 | 0 | } |
1254 | 0 | } |
1255 | 0 | } |
1256 | 0 | if (mti == NULL) |
1257 | 0 | mti = TAILQ_FIRST(&mtd->children); |
1258 | 0 | if (mti == last) |
1259 | 0 | break; |
1260 | | |
1261 | 0 | if (mtd->searchcb == NULL) { |
1262 | 0 | if (!icase && strstr(mti->name, mtd->search) != NULL) |
1263 | 0 | return (mti); |
1264 | 0 | if (icase && strcasestr(mti->name, mtd->search) != NULL) |
1265 | 0 | return (mti); |
1266 | 0 | continue; |
1267 | 0 | } |
1268 | 0 | if (mtd->searchcb(mtd->modedata, mti->itemdata, mtd->search, |
1269 | 0 | icase)) |
1270 | 0 | return (mti); |
1271 | 0 | } |
1272 | 0 | return (NULL); |
1273 | 0 | } |
1274 | | |
1275 | | static void |
1276 | | mode_tree_search_set(struct mode_tree_data *mtd) |
1277 | 0 | { |
1278 | 0 | struct mode_tree_item *mti, *loop; |
1279 | 0 | uint64_t tag; |
1280 | |
|
1281 | 0 | if (mtd->search_dir == MODE_TREE_SEARCH_FORWARD) |
1282 | 0 | mti = mode_tree_search_forward(mtd); |
1283 | 0 | else |
1284 | 0 | mti = mode_tree_search_backward(mtd); |
1285 | 0 | if (mti == NULL) |
1286 | 0 | return; |
1287 | 0 | tag = mti->tag; |
1288 | |
|
1289 | 0 | loop = mti->parent; |
1290 | 0 | while (loop != NULL) { |
1291 | 0 | loop->expanded = 1; |
1292 | 0 | loop = loop->parent; |
1293 | 0 | } |
1294 | |
|
1295 | 0 | mode_tree_build(mtd); |
1296 | 0 | mode_tree_set_current(mtd, tag); |
1297 | 0 | mode_tree_draw(mtd); |
1298 | 0 | mtd->wp->flags |= PANE_REDRAW; |
1299 | 0 | } |
1300 | | |
1301 | | static enum prompt_result |
1302 | | mode_tree_search_callback(__unused struct client *c, void *data, const char *s, |
1303 | | enum prompt_key_result key) |
1304 | 0 | { |
1305 | 0 | struct mode_tree_data *mtd = data; |
1306 | |
|
1307 | 0 | if (mtd->dead) |
1308 | 0 | return (PROMPT_CLOSE); |
1309 | | |
1310 | 0 | free(mtd->search); |
1311 | 0 | if (s == NULL || *s == '\0') |
1312 | 0 | mtd->search = NULL; |
1313 | 0 | else { |
1314 | 0 | mtd->search = xstrdup(s); |
1315 | 0 | mtd->search_icase = mode_tree_is_lowercase(s); |
1316 | 0 | mode_tree_search_set(mtd); |
1317 | 0 | } |
1318 | |
|
1319 | 0 | if (key == PROMPT_KEY_HANDLED) |
1320 | 0 | return (PROMPT_CONTINUE); |
1321 | 0 | return (PROMPT_CLOSE); |
1322 | 0 | } |
1323 | | |
1324 | | static enum prompt_result |
1325 | | mode_tree_filter_callback(__unused struct client *c, void *data, const char *s, |
1326 | | enum prompt_key_result key) |
1327 | 0 | { |
1328 | 0 | struct mode_tree_data *mtd = data; |
1329 | |
|
1330 | 0 | if (mtd->dead) |
1331 | 0 | return (PROMPT_CLOSE); |
1332 | | |
1333 | 0 | if (mtd->filter != NULL) |
1334 | 0 | free(mtd->filter); |
1335 | 0 | if (s == NULL || *s == '\0') |
1336 | 0 | mtd->filter = NULL; |
1337 | 0 | else |
1338 | 0 | mtd->filter = xstrdup(s); |
1339 | |
|
1340 | 0 | mode_tree_build(mtd); |
1341 | 0 | mode_tree_draw(mtd); |
1342 | 0 | mtd->wp->flags |= PANE_REDRAW; |
1343 | |
|
1344 | 0 | if (key == PROMPT_KEY_HANDLED) |
1345 | 0 | return (PROMPT_CONTINUE); |
1346 | 0 | return (PROMPT_CLOSE); |
1347 | 0 | } |
1348 | | |
1349 | | static void |
1350 | | mode_tree_clear_filter(struct mode_tree_data *mtd) |
1351 | 0 | { |
1352 | 0 | free(mtd->filter); |
1353 | 0 | mtd->filter = NULL; |
1354 | |
|
1355 | 0 | mode_tree_build(mtd); |
1356 | 0 | mode_tree_draw(mtd); |
1357 | 0 | mtd->wp->flags |= PANE_REDRAW; |
1358 | 0 | } |
1359 | | |
1360 | | static void |
1361 | | mode_tree_menu_callback(__unused struct menu *menu, __unused u_int idx, |
1362 | | key_code key, void *data) |
1363 | 0 | { |
1364 | 0 | struct mode_tree_menu *mtm = data; |
1365 | 0 | struct mode_tree_data *mtd = mtm->data; |
1366 | |
|
1367 | 0 | if (mtd->dead || key == KEYC_NONE) |
1368 | 0 | goto out; |
1369 | | |
1370 | 0 | if (mtm->line >= mtd->line_size) |
1371 | 0 | goto out; |
1372 | 0 | mtd->current = mtm->line; |
1373 | 0 | mtd->menucb(mtd->modedata, mtm->c, key); |
1374 | |
|
1375 | 0 | out: |
1376 | 0 | mode_tree_remove_ref(mtd); |
1377 | 0 | free(mtm); |
1378 | 0 | } |
1379 | | |
1380 | | static void |
1381 | | mode_tree_display_menu(struct mode_tree_data *mtd, struct client *c, u_int x, |
1382 | | u_int y, int outside) |
1383 | 0 | { |
1384 | 0 | struct window *w = mtd->wp->window; |
1385 | 0 | struct mode_tree_item *mti; |
1386 | 0 | struct menu *menu; |
1387 | 0 | const struct menu_item *items; |
1388 | 0 | struct mode_tree_menu *mtm; |
1389 | 0 | char *title; |
1390 | 0 | enum box_lines lines; |
1391 | 0 | u_int line, sx, sy; |
1392 | |
|
1393 | 0 | if (mtd->offset + y > mtd->line_size - 1) |
1394 | 0 | line = mtd->current; |
1395 | 0 | else |
1396 | 0 | line = mtd->offset + y; |
1397 | 0 | mti = mtd->line_list[line].item; |
1398 | |
|
1399 | 0 | if (!outside) { |
1400 | 0 | items = mtd->menu; |
1401 | 0 | xasprintf(&title, "#[align=centre]%s", mti->name); |
1402 | 0 | } else { |
1403 | 0 | items = mode_tree_menu_items; |
1404 | 0 | title = xstrdup(""); |
1405 | 0 | } |
1406 | 0 | menu = menu_create(title); |
1407 | 0 | menu_add_items(menu, items, NULL, c, NULL); |
1408 | 0 | free(title); |
1409 | |
|
1410 | 0 | mtm = xmalloc(sizeof *mtm); |
1411 | 0 | mtm->data = mtd; |
1412 | 0 | mtm->c = c; |
1413 | 0 | mtm->line = line; |
1414 | 0 | mtd->references++; |
1415 | |
|
1416 | 0 | lines = options_get_number(w->options, "menu-border-lines"); |
1417 | 0 | menu_get_size(menu, lines, &sx, &sy); |
1418 | 0 | if (x >= sx / 2) |
1419 | 0 | x -= sx / 2; |
1420 | 0 | else |
1421 | 0 | x = 0; |
1422 | 0 | x += mtd->wp->xoff; |
1423 | 0 | y += mtd->wp->yoff; |
1424 | |
|
1425 | 0 | if (menu_display(menu, 0, 0, NULL, x, y, c, lines, NULL, NULL, NULL, |
1426 | 0 | NULL, mode_tree_menu_callback, mtm) != 0) { |
1427 | 0 | mode_tree_remove_ref(mtd); |
1428 | 0 | free(mtm); |
1429 | 0 | menu_free(menu); |
1430 | 0 | } |
1431 | 0 | } |
1432 | | |
1433 | | static void |
1434 | | mode_tree_draw_help_line(struct screen_write_ctx *ctx, |
1435 | | const struct grid_cell *gc, struct format_tree *ft, const char *line, |
1436 | | const char *item, u_int x, u_int y, u_int w) |
1437 | 0 | { |
1438 | 0 | char *expanded, *replaced; |
1439 | |
|
1440 | 0 | replaced = cmd_template_replace(line, item, 1); |
1441 | 0 | expanded = format_expand(ft, replaced); |
1442 | 0 | free(replaced); |
1443 | 0 | screen_write_cursormove(ctx, x, y, 0); |
1444 | 0 | screen_write_clearcharacter(ctx, w, gc->bg); |
1445 | 0 | screen_write_cursormove(ctx, x, y, 0); |
1446 | 0 | format_draw(ctx, gc, w, expanded, NULL, 0); |
1447 | 0 | free(expanded); |
1448 | 0 | } |
1449 | | |
1450 | | static void |
1451 | | mode_tree_draw_help(struct mode_tree_data *mtd, struct screen_write_ctx *ctx) |
1452 | 0 | { |
1453 | 0 | struct screen *s = &mtd->screen; |
1454 | 0 | struct options *oo = mtd->wp->window->options; |
1455 | 0 | struct grid_cell box_gc, gc; |
1456 | 0 | struct format_tree *ft; |
1457 | 0 | const char **line, **lines = NULL, *item = "item"; |
1458 | 0 | u_int sx = screen_size_x(s), sy = screen_size_y(s); |
1459 | 0 | u_int x, y, w, h = 0, box_w, box_h; |
1460 | |
|
1461 | 0 | if (mtd->helpcb == NULL) |
1462 | 0 | w = MODE_TREE_HELP_DEFAULT_WIDTH; |
1463 | 0 | else { |
1464 | 0 | lines = mtd->helpcb(&w, &item); |
1465 | 0 | if (w < MODE_TREE_HELP_DEFAULT_WIDTH) |
1466 | 0 | w = MODE_TREE_HELP_DEFAULT_WIDTH; |
1467 | 0 | } |
1468 | 0 | for (line = mode_tree_help_start; *line != NULL; line++) |
1469 | 0 | h++; |
1470 | 0 | for (line = lines; line != NULL && *line != NULL; line++) |
1471 | 0 | h++; |
1472 | 0 | for (line = mode_tree_help_end; *line != NULL; line++) |
1473 | 0 | h++; |
1474 | |
|
1475 | 0 | box_w = w + 2; |
1476 | 0 | box_h = h + 2; |
1477 | 0 | if (sx < box_w || sy < box_h) |
1478 | 0 | return; |
1479 | 0 | x = (sx - box_w) / 2; |
1480 | 0 | y = (sy - box_h) / 2; |
1481 | |
|
1482 | 0 | memcpy(&box_gc, &grid_default_cell, sizeof box_gc); |
1483 | 0 | style_apply(&box_gc, oo, "tree-mode-border-style", NULL); |
1484 | 0 | memcpy(&gc, &grid_default_cell, sizeof gc); |
1485 | 0 | ft = format_create_defaults(NULL, NULL, NULL, NULL, mtd->wp); |
1486 | 0 | screen_write_cursormove(ctx, x, y, 0); |
1487 | 0 | screen_write_box(ctx, box_w, box_h, BOX_LINES_DEFAULT, &box_gc, NULL); |
1488 | |
|
1489 | 0 | y++; |
1490 | 0 | x++; |
1491 | 0 | for (line = mode_tree_help_start; *line != NULL; line++, y++) |
1492 | 0 | mode_tree_draw_help_line(ctx, &gc, ft, *line, item, x, y, w); |
1493 | 0 | for (line = lines; line != NULL && *line != NULL; line++, y++) |
1494 | 0 | mode_tree_draw_help_line(ctx, &gc, ft, *line, item, x, y, w); |
1495 | 0 | for (line = mode_tree_help_end; *line != NULL; line++, y++) |
1496 | 0 | mode_tree_draw_help_line(ctx, &gc, ft, *line, item, x, y, w); |
1497 | 0 | format_free(ft); |
1498 | 0 | } |
1499 | | |
1500 | | static void |
1501 | | mode_tree_display_help(struct mode_tree_data *mtd) |
1502 | 0 | { |
1503 | 0 | mtd->help = 1; |
1504 | 0 | mode_tree_draw(mtd); |
1505 | 0 | } |
1506 | | |
1507 | | int |
1508 | | mode_tree_key(struct mode_tree_data *mtd, struct client *c, key_code *key, |
1509 | | struct mouse_event *m, u_int *xp, u_int *yp) |
1510 | 0 | { |
1511 | 0 | struct mode_tree_line *line; |
1512 | 0 | struct mode_tree_item *current, *parent, *mti; |
1513 | 0 | u_int i, x, y, py, sx; |
1514 | 0 | int choice, preview; |
1515 | 0 | enum prompt_key_result result; |
1516 | 0 | int redraw; |
1517 | 0 | struct prompt *prompt; |
1518 | 0 | struct mode_tree_prompt *mtp; |
1519 | |
|
1520 | 0 | if (mtd->line_size == 0) { |
1521 | 0 | *key = KEYC_NONE; |
1522 | 0 | return (1); |
1523 | 0 | } |
1524 | | |
1525 | 0 | if (mtd->prompt != NULL) { |
1526 | 0 | redraw = 0; |
1527 | 0 | prompt = mtd->prompt; |
1528 | 0 | mtd->references++; |
1529 | |
|
1530 | 0 | mtp = mtd->prompt_data; |
1531 | 0 | if (mtp != NULL) |
1532 | 0 | mtp->c = c; |
1533 | 0 | if (KEYC_IS_MOUSE(*key)) { |
1534 | 0 | if (m == NULL || |
1535 | 0 | MOUSE_BUTTONS(m->b) != MOUSE_BUTTON_1 || |
1536 | 0 | MOUSE_DRAG(m->b) || MOUSE_RELEASE(m->b) || |
1537 | 0 | cmd_mouse_at(mtd->wp, m, &x, &y, 0) != 0) |
1538 | 0 | result = PROMPT_KEY_NOT_HANDLED; |
1539 | 0 | else { |
1540 | 0 | sx = screen_size_x(&mtd->screen); |
1541 | 0 | if (mtd->prompt_top) |
1542 | 0 | py = 0; |
1543 | 0 | else |
1544 | 0 | py = screen_size_y(&mtd->screen) - 1; |
1545 | 0 | if (y == py) { |
1546 | 0 | result = prompt_mouse(prompt, x, 0, sx, |
1547 | 0 | &redraw); |
1548 | 0 | } else |
1549 | 0 | result = PROMPT_KEY_NOT_HANDLED; |
1550 | 0 | } |
1551 | 0 | } else |
1552 | 0 | result = prompt_key(prompt, *key, &redraw); |
1553 | 0 | if (mtd->dead) { |
1554 | 0 | mode_tree_remove_ref(mtd); |
1555 | 0 | *key = KEYC_NONE; |
1556 | 0 | return (1); |
1557 | 0 | } |
1558 | 0 | if (mtd->prompt_data == mtp && mtp != NULL) |
1559 | 0 | mtp->c = NULL; |
1560 | | |
1561 | | /* |
1562 | | * Only an explicit close or the prompt marking itself closed |
1563 | | * ends it; cursor movement and editing keep it open. |
1564 | | */ |
1565 | 0 | if (mtd->prompt == prompt && |
1566 | 0 | (result == PROMPT_KEY_CLOSE || prompt_closed(prompt))) |
1567 | 0 | mode_tree_clear_prompt(mtd); |
1568 | |
|
1569 | 0 | if (redraw || mtd->prompt != prompt) { |
1570 | 0 | mode_tree_draw(mtd); |
1571 | 0 | mtd->wp->flags |= PANE_REDRAW; |
1572 | 0 | } |
1573 | 0 | mode_tree_remove_ref(mtd); |
1574 | 0 | if (result != PROMPT_KEY_NOT_HANDLED) { |
1575 | 0 | *key = KEYC_NONE; |
1576 | 0 | return (0); |
1577 | 0 | } |
1578 | 0 | } |
1579 | | |
1580 | 0 | if (mtd->help) { |
1581 | 0 | if (KEYC_IS_MOUSE(*key)) { |
1582 | 0 | *key = KEYC_NONE; |
1583 | 0 | return (0); |
1584 | 0 | } |
1585 | 0 | if (*key == KEYC_FOCUS_IN || *key == KEYC_FOCUS_OUT) { |
1586 | 0 | *key = KEYC_NONE; |
1587 | 0 | return (0); |
1588 | 0 | } |
1589 | 0 | mtd->help = 0; |
1590 | 0 | mode_tree_draw(mtd); |
1591 | 0 | *key = KEYC_NONE; |
1592 | 0 | return (0); |
1593 | 0 | } |
1594 | | |
1595 | 0 | if (KEYC_IS_MOUSE(*key) && m != NULL) { |
1596 | 0 | if (cmd_mouse_at(mtd->wp, m, &x, &y, 0) != 0) { |
1597 | 0 | *key = KEYC_NONE; |
1598 | 0 | return (0); |
1599 | 0 | } |
1600 | 0 | if (xp != NULL) |
1601 | 0 | *xp = x; |
1602 | 0 | if (yp != NULL) |
1603 | 0 | *yp = y; |
1604 | 0 | if (x > mtd->width || y > mtd->height) { |
1605 | 0 | preview = mtd->preview; |
1606 | 0 | if (*key == KEYC_MOUSEDOWN3_PANE) |
1607 | 0 | mode_tree_display_menu(mtd, c, x, y, 1); |
1608 | 0 | if (preview == MODE_TREE_PREVIEW_OFF) |
1609 | 0 | *key = KEYC_NONE; |
1610 | 0 | return (0); |
1611 | 0 | } |
1612 | 0 | if (mtd->offset + y < mtd->line_size) { |
1613 | 0 | if (*key == KEYC_MOUSEDOWN1_PANE || |
1614 | 0 | *key == KEYC_MOUSEDOWN3_PANE || |
1615 | 0 | *key == KEYC_DOUBLECLICK1_PANE) |
1616 | 0 | mtd->current = mtd->offset + y; |
1617 | 0 | if (*key == KEYC_DOUBLECLICK1_PANE) |
1618 | 0 | *key = '\r'; |
1619 | 0 | else { |
1620 | 0 | if (*key == KEYC_MOUSEDOWN3_PANE) |
1621 | 0 | mode_tree_display_menu(mtd, c, x, y, 0); |
1622 | 0 | *key = KEYC_NONE; |
1623 | 0 | } |
1624 | 0 | } else { |
1625 | 0 | if (*key == KEYC_MOUSEDOWN3_PANE) |
1626 | 0 | mode_tree_display_menu(mtd, c, x, y, 0); |
1627 | 0 | *key = KEYC_NONE; |
1628 | 0 | } |
1629 | 0 | return (0); |
1630 | 0 | } |
1631 | | |
1632 | 0 | line = &mtd->line_list[mtd->current]; |
1633 | 0 | current = line->item; |
1634 | |
|
1635 | 0 | choice = -1; |
1636 | 0 | for (i = 0; i < mtd->line_size; i++) { |
1637 | 0 | if (*key == mtd->line_list[i].item->key) { |
1638 | 0 | choice = i; |
1639 | 0 | break; |
1640 | 0 | } |
1641 | 0 | } |
1642 | 0 | if (choice != -1) { |
1643 | 0 | if ((u_int)choice > mtd->line_size - 1) { |
1644 | 0 | *key = KEYC_NONE; |
1645 | 0 | return (0); |
1646 | 0 | } |
1647 | 0 | mtd->current = choice; |
1648 | 0 | *key = '\r'; |
1649 | 0 | return (0); |
1650 | 0 | } |
1651 | | |
1652 | 0 | switch (*key) { |
1653 | 0 | case 'q': |
1654 | 0 | case '\033': /* Escape */ |
1655 | 0 | case '['|KEYC_CTRL: |
1656 | 0 | case 'g'|KEYC_CTRL: |
1657 | 0 | return (1); |
1658 | 0 | case KEYC_F1: |
1659 | 0 | case 'h'|KEYC_CTRL: |
1660 | 0 | mode_tree_display_help(mtd); |
1661 | 0 | break; |
1662 | 0 | case KEYC_UP: |
1663 | 0 | case 'k': |
1664 | 0 | case KEYC_WHEELUP_PANE: |
1665 | 0 | case 'p'|KEYC_CTRL: |
1666 | 0 | mode_tree_up(mtd, 1); |
1667 | 0 | break; |
1668 | 0 | case KEYC_DOWN: |
1669 | 0 | case 'j': |
1670 | 0 | case KEYC_WHEELDOWN_PANE: |
1671 | 0 | case 'n'|KEYC_CTRL: |
1672 | 0 | mode_tree_down(mtd, 1); |
1673 | 0 | break; |
1674 | 0 | case KEYC_UP|KEYC_SHIFT: |
1675 | 0 | case 'K': |
1676 | 0 | mode_tree_swap(mtd, -1); |
1677 | 0 | break; |
1678 | 0 | case KEYC_DOWN|KEYC_SHIFT: |
1679 | 0 | case 'J': |
1680 | 0 | mode_tree_swap(mtd, 1); |
1681 | 0 | break; |
1682 | 0 | case KEYC_PPAGE: |
1683 | 0 | case 'b'|KEYC_CTRL: |
1684 | 0 | for (i = 0; i < mtd->height; i++) { |
1685 | 0 | if (mtd->current == 0) |
1686 | 0 | break; |
1687 | 0 | mode_tree_up(mtd, 1); |
1688 | 0 | } |
1689 | 0 | break; |
1690 | 0 | case KEYC_NPAGE: |
1691 | 0 | case 'f'|KEYC_CTRL: |
1692 | 0 | for (i = 0; i < mtd->height; i++) { |
1693 | 0 | if (mtd->current == mtd->line_size - 1) |
1694 | 0 | break; |
1695 | 0 | mode_tree_down(mtd, 1); |
1696 | 0 | } |
1697 | 0 | break; |
1698 | 0 | case 'g': |
1699 | 0 | case KEYC_HOME: |
1700 | 0 | mtd->current = 0; |
1701 | 0 | mtd->offset = 0; |
1702 | 0 | break; |
1703 | 0 | case 'G': |
1704 | 0 | case KEYC_END: |
1705 | 0 | mtd->current = mtd->line_size - 1; |
1706 | 0 | if (mtd->current > mtd->height - 1) |
1707 | 0 | mtd->offset = mtd->current - mtd->height + 1; |
1708 | 0 | else |
1709 | 0 | mtd->offset = 0; |
1710 | 0 | break; |
1711 | 0 | case 't': |
1712 | | /* |
1713 | | * Do not allow parents and children to both be tagged: untag |
1714 | | * all parents and children of current. |
1715 | | */ |
1716 | 0 | if (current->no_tag) |
1717 | 0 | break; |
1718 | 0 | if (!current->tagged) { |
1719 | 0 | parent = current->parent; |
1720 | 0 | while (parent != NULL) { |
1721 | 0 | parent->tagged = 0; |
1722 | 0 | parent = parent->parent; |
1723 | 0 | } |
1724 | 0 | mode_tree_clear_tagged(¤t->children); |
1725 | 0 | current->tagged = 1; |
1726 | 0 | } else |
1727 | 0 | current->tagged = 0; |
1728 | 0 | if (m != NULL) |
1729 | 0 | mode_tree_down(mtd, 0); |
1730 | 0 | break; |
1731 | 0 | case 'T': |
1732 | 0 | for (i = 0; i < mtd->line_size; i++) |
1733 | 0 | mtd->line_list[i].item->tagged = 0; |
1734 | 0 | break; |
1735 | 0 | case 't'|KEYC_CTRL: |
1736 | 0 | for (i = 0; i < mtd->line_size; i++) { |
1737 | 0 | if ((mtd->line_list[i].item->parent == NULL && |
1738 | 0 | !mtd->line_list[i].item->no_tag) || |
1739 | 0 | (mtd->line_list[i].item->parent != NULL && |
1740 | 0 | mtd->line_list[i].item->parent->no_tag)) |
1741 | 0 | mtd->line_list[i].item->tagged = 1; |
1742 | 0 | else |
1743 | 0 | mtd->line_list[i].item->tagged = 0; |
1744 | 0 | } |
1745 | 0 | break; |
1746 | 0 | case 'O': |
1747 | 0 | sort_next_order(&mtd->sort_crit); |
1748 | 0 | mode_tree_build(mtd); |
1749 | 0 | break; |
1750 | 0 | case 'r': |
1751 | 0 | mtd->sort_crit.reversed = !mtd->sort_crit.reversed; |
1752 | 0 | mode_tree_build(mtd); |
1753 | 0 | break; |
1754 | 0 | case KEYC_LEFT: |
1755 | 0 | case 'h': |
1756 | 0 | case '-': |
1757 | 0 | if (line->flat || !current->expanded) |
1758 | 0 | current = current->parent; |
1759 | 0 | if (current == NULL) |
1760 | 0 | mode_tree_up(mtd, 0); |
1761 | 0 | else { |
1762 | 0 | current->expanded = 0; |
1763 | 0 | mtd->current = current->line; |
1764 | 0 | mode_tree_build(mtd); |
1765 | 0 | } |
1766 | 0 | break; |
1767 | 0 | case KEYC_RIGHT: |
1768 | 0 | case 'l': |
1769 | 0 | case '+': |
1770 | 0 | if (line->flat || current->expanded) |
1771 | 0 | mode_tree_down(mtd, 0); |
1772 | 0 | else if (!line->flat) { |
1773 | 0 | current->expanded = 1; |
1774 | 0 | mode_tree_build(mtd); |
1775 | 0 | } |
1776 | 0 | break; |
1777 | 0 | case '-'|KEYC_META: |
1778 | 0 | TAILQ_FOREACH(mti, &mtd->children, entry) |
1779 | 0 | mti->expanded = 0; |
1780 | 0 | mode_tree_build(mtd); |
1781 | 0 | break; |
1782 | 0 | case '+'|KEYC_META: |
1783 | 0 | TAILQ_FOREACH(mti, &mtd->children, entry) |
1784 | 0 | mti->expanded = 1; |
1785 | 0 | mode_tree_build(mtd); |
1786 | 0 | break; |
1787 | 0 | case '?': |
1788 | 0 | case '/': |
1789 | 0 | case 's'|KEYC_CTRL: |
1790 | 0 | mtd->search_dir = MODE_TREE_SEARCH_FORWARD; |
1791 | 0 | mode_tree_set_prompt(mtd, c, "(search) ", "", |
1792 | 0 | PROMPT_TYPE_SEARCH, PROMPT_NOFORMAT, |
1793 | 0 | mode_tree_search_callback, NULL, mtd); |
1794 | 0 | break; |
1795 | 0 | case 'n': |
1796 | 0 | mtd->search_dir = MODE_TREE_SEARCH_FORWARD; |
1797 | 0 | mode_tree_search_set(mtd); |
1798 | 0 | break; |
1799 | 0 | case 'N': |
1800 | 0 | mtd->search_dir = MODE_TREE_SEARCH_BACKWARD; |
1801 | 0 | mode_tree_search_set(mtd); |
1802 | 0 | break; |
1803 | 0 | case 'f': |
1804 | 0 | mode_tree_set_prompt(mtd, c, "(filter) ", mtd->filter, |
1805 | 0 | PROMPT_TYPE_SEARCH, PROMPT_NOFORMAT, |
1806 | 0 | mode_tree_filter_callback, NULL, mtd); |
1807 | 0 | break; |
1808 | 0 | case 'c': |
1809 | 0 | mode_tree_clear_prompt(mtd); |
1810 | 0 | mode_tree_clear_filter(mtd); |
1811 | 0 | break; |
1812 | 0 | case 'v': |
1813 | 0 | switch (mtd->preview) { |
1814 | 0 | case MODE_TREE_PREVIEW_OFF: |
1815 | 0 | mtd->preview = MODE_TREE_PREVIEW_BIG; |
1816 | 0 | break; |
1817 | 0 | case MODE_TREE_PREVIEW_NORMAL: |
1818 | 0 | mtd->preview = MODE_TREE_PREVIEW_OFF; |
1819 | 0 | break; |
1820 | 0 | case MODE_TREE_PREVIEW_BIG: |
1821 | 0 | mtd->preview = MODE_TREE_PREVIEW_NORMAL; |
1822 | 0 | break; |
1823 | 0 | } |
1824 | 0 | mode_tree_build(mtd); |
1825 | 0 | if (mtd->preview != MODE_TREE_PREVIEW_OFF) |
1826 | 0 | mode_tree_check_selected(mtd); |
1827 | 0 | break; |
1828 | 0 | } |
1829 | 0 | return (0); |
1830 | 0 | } |
1831 | | |
1832 | | void |
1833 | | mode_tree_run_command(struct client *c, struct cmd_find_state *fs, |
1834 | | const char *template, const char *name) |
1835 | 0 | { |
1836 | 0 | struct cmdq_state *state; |
1837 | 0 | char *command, *error; |
1838 | 0 | enum cmd_parse_status status; |
1839 | |
|
1840 | 0 | command = cmd_template_replace(template, name, 1); |
1841 | 0 | if (command != NULL && *command != '\0') { |
1842 | 0 | state = cmdq_new_state(fs, NULL, 0); |
1843 | 0 | status = cmd_parse_and_append(command, NULL, c, state, &error); |
1844 | 0 | if (status == CMD_PARSE_ERROR) { |
1845 | 0 | if (c != NULL) { |
1846 | | *error = toupper((u_char)*error); |
1847 | 0 | status_message_set(c, -1, 1, 0, 0, "%s", error); |
1848 | 0 | } |
1849 | 0 | free(error); |
1850 | 0 | } |
1851 | 0 | cmdq_free_state(state); |
1852 | 0 | } |
1853 | 0 | free(command); |
1854 | 0 | } |