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