Commit | Line | Data |
---|---|---|
53924375 | 1 | /* Copyright (c) 2006-2007 Jonas Fonseca <fonseca@diku.dk> |
192d9a60 | 2 | * |
5cfbde75 JF |
3 | * This program is free software; you can redistribute it and/or |
4 | * modify it under the terms of the GNU General Public License as | |
5 | * published by the Free Software Foundation; either version 2 of | |
6 | * the License, or (at your option) any later version. | |
192d9a60 JF |
7 | * |
8 | * This program is distributed in the hope that it will be useful, | |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 | * GNU General Public License for more details. | |
12 | */ | |
b801d8b2 | 13 | |
776bf2ac JF |
14 | #ifdef HAVE_CONFIG_H |
15 | #include "config.h" | |
16 | #endif | |
17 | ||
ec31d0d0 SG |
18 | #ifndef TIG_VERSION |
19 | #define TIG_VERSION "unknown-version" | |
b76c2afc JF |
20 | #endif |
21 | ||
8855ada4 JF |
22 | #ifndef DEBUG |
23 | #define NDEBUG | |
24 | #endif | |
25 | ||
22f66b0a | 26 | #include <assert.h> |
4c6fabc2 | 27 | #include <errno.h> |
22f66b0a JF |
28 | #include <ctype.h> |
29 | #include <signal.h> | |
b801d8b2 | 30 | #include <stdarg.h> |
b801d8b2 | 31 | #include <stdio.h> |
22f66b0a | 32 | #include <stdlib.h> |
b801d8b2 | 33 | #include <string.h> |
810f0078 JF |
34 | #include <sys/types.h> |
35 | #include <sys/stat.h> | |
6908bdbd | 36 | #include <unistd.h> |
b76c2afc | 37 | #include <time.h> |
b801d8b2 | 38 | |
4af34daa JF |
39 | #include <regex.h> |
40 | ||
6b68fd24 JF |
41 | #include <locale.h> |
42 | #include <langinfo.h> | |
43 | #include <iconv.h> | |
44 | ||
b801d8b2 | 45 | #include <curses.h> |
b801d8b2 | 46 | |
e2da526d JF |
47 | #if __GNUC__ >= 3 |
48 | #define __NORETURN __attribute__((__noreturn__)) | |
49 | #else | |
50 | #define __NORETURN | |
51 | #endif | |
52 | ||
53 | static void __NORETURN die(const char *err, ...); | |
b801d8b2 | 54 | static void report(const char *msg, ...); |
5699e0cf | 55 | static int read_properties(FILE *pipe, const char *separators, int (*read)(char *, size_t, char *, size_t)); |
1ba2ae4b | 56 | static void set_nonblocking_input(bool loading); |
10e290ee | 57 | static size_t utf8_length(const char *string, size_t max_width, int *coloffset, int *trimmed); |
6b161b31 JF |
58 | |
59 | #define ABS(x) ((x) >= 0 ? (x) : -(x)) | |
60 | #define MIN(x, y) ((x) < (y) ? (x) : (y)) | |
61 | ||
62 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0])) | |
63 | #define STRING_SIZE(x) (sizeof(x) - 1) | |
b76c2afc | 64 | |
17482b11 | 65 | #define SIZEOF_STR 1024 /* Default string size. */ |
2e8488b4 | 66 | #define SIZEOF_REF 256 /* Size of symbolic or SHA1 ID. */ |
10446330 | 67 | #define SIZEOF_REV 41 /* Holds a SHA-1 and an ending NUL */ |
c8d60a25 JF |
68 | |
69 | /* Revision graph */ | |
70 | ||
71 | #define REVGRAPH_INIT 'I' | |
72 | #define REVGRAPH_MERGE 'M' | |
73 | #define REVGRAPH_BRANCH '+' | |
74 | #define REVGRAPH_COMMIT '*' | |
75 | #define REVGRAPH_LINE '|' | |
76 | ||
54efb62b | 77 | #define SIZEOF_REVGRAPH 19 /* Size of revision ancestry graphics. */ |
b801d8b2 | 78 | |
82e78006 JF |
79 | /* This color name can be used to refer to the default term colors. */ |
80 | #define COLOR_DEFAULT (-1) | |
78c70acd | 81 | |
6b68fd24 | 82 | #define ICONV_NONE ((iconv_t) -1) |
da633326 JF |
83 | #ifndef ICONV_INBUF_TYPE |
84 | #define ICONV_INBUF_TYPE char * | |
85 | #endif | |
6b68fd24 | 86 | |
82e78006 | 87 | /* The format and size of the date column in the main view. */ |
4c6fabc2 | 88 | #define DATE_FORMAT "%Y-%m-%d %H:%M" |
6b161b31 | 89 | #define DATE_COLS STRING_SIZE("2006-04-29 14:21 ") |
4c6fabc2 | 90 | |
10e290ee JF |
91 | #define AUTHOR_COLS 20 |
92 | ||
a28bcc22 | 93 | /* The default interval between line numbers. */ |
4a2909a7 | 94 | #define NUMBER_INTERVAL 1 |
82e78006 | 95 | |
6706b2ba JF |
96 | #define TABSIZE 8 |
97 | ||
a28bcc22 JF |
98 | #define SCALE_SPLIT_VIEW(height) ((height) * 2 / 3) |
99 | ||
96e58f5b | 100 | #ifndef GIT_CONFIG |
da633326 | 101 | #define GIT_CONFIG "git config" |
96e58f5b JF |
102 | #endif |
103 | ||
8eb62770 | 104 | #define TIG_LS_REMOTE \ |
337d7377 | 105 | "git ls-remote $(git rev-parse --git-dir) 2>/dev/null" |
8eb62770 JF |
106 | |
107 | #define TIG_DIFF_CMD \ | |
73fb51d5 | 108 | "git show --root --patch-with-stat --find-copies-harder -B -C %s 2>/dev/null" |
8eb62770 JF |
109 | |
110 | #define TIG_LOG_CMD \ | |
8fee6614 | 111 | "git log --cc --stat -n100 %s 2>/dev/null" |
8eb62770 JF |
112 | |
113 | #define TIG_MAIN_CMD \ | |
8fee6614 | 114 | "git log --topo-order --pretty=raw %s 2>/dev/null" |
8eb62770 | 115 | |
e733ee54 JF |
116 | #define TIG_TREE_CMD \ |
117 | "git ls-tree %s %s" | |
118 | ||
119 | #define TIG_BLOB_CMD \ | |
120 | "git cat-file blob %s" | |
121 | ||
8eb62770 JF |
122 | /* XXX: Needs to be defined to the empty string. */ |
123 | #define TIG_HELP_CMD "" | |
124 | #define TIG_PAGER_CMD "" | |
173d76ea | 125 | #define TIG_STATUS_CMD "" |
3e634113 | 126 | #define TIG_STAGE_CMD "" |
8eb62770 | 127 | |
8855ada4 | 128 | /* Some ascii-shorthands fitted into the ncurses namespace. */ |
a28bcc22 JF |
129 | #define KEY_TAB '\t' |
130 | #define KEY_RETURN '\r' | |
4a2909a7 JF |
131 | #define KEY_ESC 27 |
132 | ||
6706b2ba | 133 | |
c34d9c9f | 134 | struct ref { |
468876c9 | 135 | char *name; /* Ref name; tag or head names are shortened. */ |
10446330 | 136 | char id[SIZEOF_REV]; /* Commit SHA1 ID */ |
468876c9 | 137 | unsigned int tag:1; /* Is it a tag? */ |
e15ec88e | 138 | unsigned int remote:1; /* Is it a remote ref? */ |
468876c9 | 139 | unsigned int next:1; /* For ref lists: are there more refs? */ |
c34d9c9f JF |
140 | }; |
141 | ||
ff26aa29 | 142 | static struct ref **get_refs(char *id); |
4c6fabc2 | 143 | |
660e09ad JF |
144 | struct int_map { |
145 | const char *name; | |
146 | int namelen; | |
147 | int value; | |
148 | }; | |
149 | ||
150 | static int | |
151 | set_from_int_map(struct int_map *map, size_t map_size, | |
152 | int *value, const char *name, int namelen) | |
153 | { | |
154 | ||
155 | int i; | |
156 | ||
157 | for (i = 0; i < map_size; i++) | |
158 | if (namelen == map[i].namelen && | |
159 | !strncasecmp(name, map[i].name, namelen)) { | |
160 | *value = map[i].value; | |
161 | return OK; | |
162 | } | |
163 | ||
164 | return ERR; | |
165 | } | |
166 | ||
6706b2ba | 167 | |
03a93dbb JF |
168 | /* |
169 | * String helpers | |
170 | */ | |
78c70acd | 171 | |
82e78006 | 172 | static inline void |
9a48919b | 173 | string_ncopy_do(char *dst, size_t dstlen, const char *src, size_t srclen) |
82e78006 | 174 | { |
9a48919b JF |
175 | if (srclen > dstlen - 1) |
176 | srclen = dstlen - 1; | |
03a93dbb | 177 | |
9a48919b JF |
178 | strncpy(dst, src, srclen); |
179 | dst[srclen] = 0; | |
82e78006 JF |
180 | } |
181 | ||
9a48919b JF |
182 | /* Shorthands for safely copying into a fixed buffer. */ |
183 | ||
82e78006 | 184 | #define string_copy(dst, src) \ |
751e27c9 | 185 | string_ncopy_do(dst, sizeof(dst), src, sizeof(src)) |
9a48919b JF |
186 | |
187 | #define string_ncopy(dst, src, srclen) \ | |
188 | string_ncopy_do(dst, sizeof(dst), src, srclen) | |
82e78006 | 189 | |
2463b4ea JF |
190 | #define string_copy_rev(dst, src) \ |
191 | string_ncopy_do(dst, SIZEOF_REV, src, SIZEOF_REV - 1) | |
192 | ||
91c5d983 JF |
193 | #define string_add(dst, from, src) \ |
194 | string_ncopy_do(dst + (from), sizeof(dst) - (from), src, sizeof(src)) | |
195 | ||
4a63c884 JF |
196 | static char * |
197 | chomp_string(char *name) | |
198 | { | |
199 | int namelen; | |
200 | ||
201 | while (isspace(*name)) | |
202 | name++; | |
203 | ||
204 | namelen = strlen(name) - 1; | |
205 | while (namelen > 0 && isspace(name[namelen])) | |
206 | name[namelen--] = 0; | |
207 | ||
208 | return name; | |
209 | } | |
210 | ||
cc2d1364 | 211 | static bool |
d65ced0d | 212 | string_nformat(char *buf, size_t bufsize, size_t *bufpos, const char *fmt, ...) |
cc2d1364 JF |
213 | { |
214 | va_list args; | |
d65ced0d | 215 | size_t pos = bufpos ? *bufpos : 0; |
cc2d1364 JF |
216 | |
217 | va_start(args, fmt); | |
218 | pos += vsnprintf(buf + pos, bufsize - pos, fmt, args); | |
219 | va_end(args); | |
220 | ||
221 | if (bufpos) | |
222 | *bufpos = pos; | |
223 | ||
224 | return pos >= bufsize ? FALSE : TRUE; | |
225 | } | |
226 | ||
227 | #define string_format(buf, fmt, args...) \ | |
228 | string_nformat(buf, sizeof(buf), NULL, fmt, args) | |
229 | ||
230 | #define string_format_from(buf, from, fmt, args...) \ | |
231 | string_nformat(buf, sizeof(buf), from, fmt, args) | |
6706b2ba | 232 | |
201f5a18 JF |
233 | static int |
234 | string_enum_compare(const char *str1, const char *str2, int len) | |
235 | { | |
236 | size_t i; | |
237 | ||
238 | #define string_enum_sep(x) ((x) == '-' || (x) == '_' || (x) == '.') | |
239 | ||
240 | /* Diff-Header == DIFF_HEADER */ | |
241 | for (i = 0; i < len; i++) { | |
242 | if (toupper(str1[i]) == toupper(str2[i])) | |
243 | continue; | |
244 | ||
245 | if (string_enum_sep(str1[i]) && | |
246 | string_enum_sep(str2[i])) | |
247 | continue; | |
248 | ||
249 | return str1[i] - str2[i]; | |
250 | } | |
251 | ||
252 | return 0; | |
253 | } | |
254 | ||
03a93dbb JF |
255 | /* Shell quoting |
256 | * | |
257 | * NOTE: The following is a slightly modified copy of the git project's shell | |
258 | * quoting routines found in the quote.c file. | |
259 | * | |
260 | * Help to copy the thing properly quoted for the shell safety. any single | |
261 | * quote is replaced with '\'', any exclamation point is replaced with '\!', | |
262 | * and the whole thing is enclosed in a | |
263 | * | |
264 | * E.g. | |
265 | * original sq_quote result | |
266 | * name ==> name ==> 'name' | |
267 | * a b ==> a b ==> 'a b' | |
268 | * a'b ==> a'\''b ==> 'a'\''b' | |
269 | * a!b ==> a'\!'b ==> 'a'\!'b' | |
270 | */ | |
271 | ||
272 | static size_t | |
17482b11 | 273 | sq_quote(char buf[SIZEOF_STR], size_t bufsize, const char *src) |
03a93dbb JF |
274 | { |
275 | char c; | |
276 | ||
17482b11 | 277 | #define BUFPUT(x) do { if (bufsize < SIZEOF_STR) buf[bufsize++] = (x); } while (0) |
03a93dbb JF |
278 | |
279 | BUFPUT('\''); | |
280 | while ((c = *src++)) { | |
281 | if (c == '\'' || c == '!') { | |
282 | BUFPUT('\''); | |
283 | BUFPUT('\\'); | |
284 | BUFPUT(c); | |
285 | BUFPUT('\''); | |
286 | } else { | |
287 | BUFPUT(c); | |
288 | } | |
289 | } | |
290 | BUFPUT('\''); | |
291 | ||
f0f114ac JF |
292 | if (bufsize < SIZEOF_STR) |
293 | buf[bufsize] = 0; | |
294 | ||
03a93dbb JF |
295 | return bufsize; |
296 | } | |
297 | ||
82e78006 | 298 | |
24b5b3e0 JF |
299 | /* |
300 | * User requests | |
301 | */ | |
302 | ||
303 | #define REQ_INFO \ | |
304 | /* XXX: Keep the view request first and in sync with views[]. */ \ | |
305 | REQ_GROUP("View switching") \ | |
306 | REQ_(VIEW_MAIN, "Show main view"), \ | |
307 | REQ_(VIEW_DIFF, "Show diff view"), \ | |
308 | REQ_(VIEW_LOG, "Show log view"), \ | |
e733ee54 JF |
309 | REQ_(VIEW_TREE, "Show tree view"), \ |
310 | REQ_(VIEW_BLOB, "Show blob view"), \ | |
24b5b3e0 JF |
311 | REQ_(VIEW_HELP, "Show help page"), \ |
312 | REQ_(VIEW_PAGER, "Show pager view"), \ | |
173d76ea | 313 | REQ_(VIEW_STATUS, "Show status view"), \ |
3e634113 | 314 | REQ_(VIEW_STAGE, "Show stage view"), \ |
24b5b3e0 JF |
315 | \ |
316 | REQ_GROUP("View manipulation") \ | |
317 | REQ_(ENTER, "Enter current line and scroll"), \ | |
318 | REQ_(NEXT, "Move to next"), \ | |
319 | REQ_(PREVIOUS, "Move to previous"), \ | |
320 | REQ_(VIEW_NEXT, "Move focus to next view"), \ | |
321 | REQ_(VIEW_CLOSE, "Close the current view"), \ | |
322 | REQ_(QUIT, "Close all views and quit"), \ | |
323 | \ | |
324 | REQ_GROUP("Cursor navigation") \ | |
325 | REQ_(MOVE_UP, "Move cursor one line up"), \ | |
326 | REQ_(MOVE_DOWN, "Move cursor one line down"), \ | |
327 | REQ_(MOVE_PAGE_DOWN, "Move cursor one page down"), \ | |
328 | REQ_(MOVE_PAGE_UP, "Move cursor one page up"), \ | |
329 | REQ_(MOVE_FIRST_LINE, "Move cursor to first line"), \ | |
330 | REQ_(MOVE_LAST_LINE, "Move cursor to last line"), \ | |
331 | \ | |
332 | REQ_GROUP("Scrolling") \ | |
333 | REQ_(SCROLL_LINE_UP, "Scroll one line up"), \ | |
334 | REQ_(SCROLL_LINE_DOWN, "Scroll one line down"), \ | |
335 | REQ_(SCROLL_PAGE_UP, "Scroll one page up"), \ | |
336 | REQ_(SCROLL_PAGE_DOWN, "Scroll one page down"), \ | |
337 | \ | |
4af34daa JF |
338 | REQ_GROUP("Searching") \ |
339 | REQ_(SEARCH, "Search the view"), \ | |
340 | REQ_(SEARCH_BACK, "Search backwards in the view"), \ | |
341 | REQ_(FIND_NEXT, "Find next search match"), \ | |
342 | REQ_(FIND_PREV, "Find previous search match"), \ | |
343 | \ | |
24b5b3e0 | 344 | REQ_GROUP("Misc") \ |
1d754561 | 345 | REQ_(NONE, "Do nothing"), \ |
24b5b3e0 | 346 | REQ_(PROMPT, "Bring up the prompt"), \ |
24b5b3e0 JF |
347 | REQ_(SCREEN_REDRAW, "Redraw the screen"), \ |
348 | REQ_(SCREEN_RESIZE, "Resize the screen"), \ | |
349 | REQ_(SHOW_VERSION, "Show version information"), \ | |
350 | REQ_(STOP_LOADING, "Stop all loading views"), \ | |
54efb62b | 351 | REQ_(TOGGLE_LINENO, "Toggle line numbers"), \ |
ca1d71ea | 352 | REQ_(TOGGLE_REV_GRAPH, "Toggle revision graph visualization"), \ |
0cea0d43 JF |
353 | REQ_(STATUS_UPDATE, "Update file status"), \ |
354 | REQ_(EDIT, "Open in editor") | |
24b5b3e0 JF |
355 | |
356 | ||
357 | /* User action requests. */ | |
358 | enum request { | |
359 | #define REQ_GROUP(help) | |
360 | #define REQ_(req, help) REQ_##req | |
361 | ||
362 | /* Offset all requests to avoid conflicts with ncurses getch values. */ | |
363 | REQ_OFFSET = KEY_MAX + 1, | |
04e2b7b2 JF |
364 | REQ_INFO, |
365 | REQ_UNKNOWN, | |
24b5b3e0 JF |
366 | |
367 | #undef REQ_GROUP | |
368 | #undef REQ_ | |
369 | }; | |
370 | ||
371 | struct request_info { | |
372 | enum request request; | |
04e2b7b2 JF |
373 | char *name; |
374 | int namelen; | |
24b5b3e0 JF |
375 | char *help; |
376 | }; | |
377 | ||
378 | static struct request_info req_info[] = { | |
04e2b7b2 JF |
379 | #define REQ_GROUP(help) { 0, NULL, 0, (help) }, |
380 | #define REQ_(req, help) { REQ_##req, (#req), STRING_SIZE(#req), (help) } | |
24b5b3e0 JF |
381 | REQ_INFO |
382 | #undef REQ_GROUP | |
383 | #undef REQ_ | |
384 | }; | |
385 | ||
04e2b7b2 JF |
386 | static enum request |
387 | get_request(const char *name) | |
388 | { | |
389 | int namelen = strlen(name); | |
390 | int i; | |
391 | ||
392 | for (i = 0; i < ARRAY_SIZE(req_info); i++) | |
393 | if (req_info[i].namelen == namelen && | |
394 | !string_enum_compare(req_info[i].name, name, namelen)) | |
395 | return req_info[i].request; | |
396 | ||
397 | return REQ_UNKNOWN; | |
398 | } | |
399 | ||
400 | ||
8eb62770 JF |
401 | /* |
402 | * Options | |
403 | */ | |
b76c2afc | 404 | |
4b8c01a3 | 405 | static const char usage[] = |
ec31d0d0 | 406 | "tig " TIG_VERSION " (" __DATE__ ")\n" |
4b8c01a3 JF |
407 | "\n" |
408 | "Usage: tig [options]\n" | |
409 | " or: tig [options] [--] [git log options]\n" | |
410 | " or: tig [options] log [git log options]\n" | |
411 | " or: tig [options] diff [git diff options]\n" | |
412 | " or: tig [options] show [git show options]\n" | |
413 | " or: tig [options] < [git command output]\n" | |
414 | "\n" | |
415 | "Options:\n" | |
416 | " -l Start up in log view\n" | |
417 | " -d Start up in diff view\n" | |
173d76ea | 418 | " -S Start up in status view\n" |
4b8c01a3 | 419 | " -n[I], --line-number[=I] Show line numbers with given interval\n" |
b3c965c9 | 420 | " -b[N], --tab-size[=N] Set number of spaces for tab expansion\n" |
4b8c01a3 JF |
421 | " -- Mark end of tig options\n" |
422 | " -v, --version Show version and exit\n" | |
423 | " -h, --help Show help message and exit\n"; | |
424 | ||
6706b2ba | 425 | /* Option and state variables. */ |
92d30f5c | 426 | static bool opt_line_number = FALSE; |
11ce319e | 427 | static bool opt_rev_graph = FALSE; |
92d30f5c JF |
428 | static int opt_num_interval = NUMBER_INTERVAL; |
429 | static int opt_tab_size = TABSIZE; | |
430 | static enum request opt_request = REQ_VIEW_MAIN; | |
431 | static char opt_cmd[SIZEOF_STR] = ""; | |
e733ee54 | 432 | static char opt_path[SIZEOF_STR] = ""; |
92d30f5c JF |
433 | static FILE *opt_pipe = NULL; |
434 | static char opt_encoding[20] = "UTF-8"; | |
435 | static bool opt_utf8 = TRUE; | |
436 | static char opt_codeset[20] = "UTF-8"; | |
437 | static iconv_t opt_iconv = ICONV_NONE; | |
438 | static char opt_search[SIZEOF_STR] = ""; | |
91c5d983 | 439 | static char opt_cdup[SIZEOF_STR] = ""; |
810f0078 | 440 | static char opt_git_dir[SIZEOF_STR] = ""; |
0cea0d43 | 441 | static char opt_editor[SIZEOF_STR] = ""; |
b76c2afc | 442 | |
6dbf6c19 JF |
443 | enum option_type { |
444 | OPT_NONE, | |
445 | OPT_INT, | |
446 | }; | |
447 | ||
448 | static bool | |
449 | check_option(char *opt, char short_name, char *name, enum option_type type, ...) | |
450 | { | |
451 | va_list args; | |
452 | char *value = ""; | |
453 | int *number; | |
454 | ||
455 | if (opt[0] != '-') | |
456 | return FALSE; | |
457 | ||
458 | if (opt[1] == '-') { | |
459 | int namelen = strlen(name); | |
460 | ||
461 | opt += 2; | |
462 | ||
463 | if (strncmp(opt, name, namelen)) | |
464 | return FALSE; | |
465 | ||
466 | if (opt[namelen] == '=') | |
467 | value = opt + namelen + 1; | |
468 | ||
469 | } else { | |
470 | if (!short_name || opt[1] != short_name) | |
471 | return FALSE; | |
472 | value = opt + 2; | |
473 | } | |
474 | ||
475 | va_start(args, type); | |
476 | if (type == OPT_INT) { | |
477 | number = va_arg(args, int *); | |
478 | if (isdigit(*value)) | |
479 | *number = atoi(value); | |
480 | } | |
481 | va_end(args); | |
482 | ||
483 | return TRUE; | |
484 | } | |
485 | ||
b76c2afc | 486 | /* Returns the index of log or diff command or -1 to exit. */ |
8855ada4 | 487 | static bool |
b76c2afc JF |
488 | parse_options(int argc, char *argv[]) |
489 | { | |
490 | int i; | |
491 | ||
492 | for (i = 1; i < argc; i++) { | |
493 | char *opt = argv[i]; | |
494 | ||
3621d94e JF |
495 | if (!strcmp(opt, "log") || |
496 | !strcmp(opt, "diff") || | |
497 | !strcmp(opt, "show")) { | |
498 | opt_request = opt[0] == 'l' | |
499 | ? REQ_VIEW_LOG : REQ_VIEW_DIFF; | |
500 | break; | |
501 | } | |
502 | ||
8d27b36e JF |
503 | if (opt[0] && opt[0] != '-') |
504 | break; | |
505 | ||
6b161b31 | 506 | if (!strcmp(opt, "-l")) { |
4a2909a7 | 507 | opt_request = REQ_VIEW_LOG; |
6b161b31 JF |
508 | continue; |
509 | } | |
b76c2afc | 510 | |
6b161b31 | 511 | if (!strcmp(opt, "-d")) { |
4a2909a7 | 512 | opt_request = REQ_VIEW_DIFF; |
6b161b31 JF |
513 | continue; |
514 | } | |
b76c2afc | 515 | |
173d76ea JF |
516 | if (!strcmp(opt, "-S")) { |
517 | opt_request = REQ_VIEW_STATUS; | |
bdcc0d6b | 518 | continue; |
173d76ea JF |
519 | } |
520 | ||
6dbf6c19 | 521 | if (check_option(opt, 'n', "line-number", OPT_INT, &opt_num_interval)) { |
6b161b31 JF |
522 | opt_line_number = TRUE; |
523 | continue; | |
524 | } | |
b76c2afc | 525 | |
6dbf6c19 JF |
526 | if (check_option(opt, 'b', "tab-size", OPT_INT, &opt_tab_size)) { |
527 | opt_tab_size = MIN(opt_tab_size, TABSIZE); | |
6706b2ba JF |
528 | continue; |
529 | } | |
530 | ||
6dbf6c19 | 531 | if (check_option(opt, 'v', "version", OPT_NONE)) { |
ec31d0d0 | 532 | printf("tig version %s\n", TIG_VERSION); |
8855ada4 | 533 | return FALSE; |
6b161b31 | 534 | } |
b76c2afc | 535 | |
6dbf6c19 | 536 | if (check_option(opt, 'h', "help", OPT_NONE)) { |
4b8c01a3 JF |
537 | printf(usage); |
538 | return FALSE; | |
539 | } | |
540 | ||
6908bdbd JF |
541 | if (!strcmp(opt, "--")) { |
542 | i++; | |
543 | break; | |
544 | } | |
03a93dbb | 545 | |
bf174187 | 546 | die("unknown option '%s'\n\n%s", opt, usage); |
b76c2afc JF |
547 | } |
548 | ||
6908bdbd | 549 | if (!isatty(STDIN_FILENO)) { |
6908bdbd JF |
550 | opt_request = REQ_VIEW_PAGER; |
551 | opt_pipe = stdin; | |
552 | ||
553 | } else if (i < argc) { | |
554 | size_t buf_size; | |
555 | ||
6908bdbd | 556 | if (opt_request == REQ_VIEW_MAIN) |
8855ada4 JF |
557 | /* XXX: This is vulnerable to the user overriding |
558 | * options required for the main view parser. */ | |
832a47c4 | 559 | string_copy(opt_cmd, "git log --pretty=raw"); |
6908bdbd JF |
560 | else |
561 | string_copy(opt_cmd, "git"); | |
562 | buf_size = strlen(opt_cmd); | |
563 | ||
564 | while (buf_size < sizeof(opt_cmd) && i < argc) { | |
565 | opt_cmd[buf_size++] = ' '; | |
566 | buf_size = sq_quote(opt_cmd, buf_size, argv[i++]); | |
567 | } | |
568 | ||
569 | if (buf_size >= sizeof(opt_cmd)) | |
570 | die("command too long"); | |
571 | ||
572 | opt_cmd[buf_size] = 0; | |
6908bdbd JF |
573 | } |
574 | ||
afdc35b3 JF |
575 | if (*opt_encoding && strcasecmp(opt_encoding, "UTF-8")) |
576 | opt_utf8 = FALSE; | |
577 | ||
8855ada4 | 578 | return TRUE; |
b76c2afc JF |
579 | } |
580 | ||
581 | ||
54efb62b JF |
582 | /* |
583 | * Line-oriented content detection. | |
584 | */ | |
585 | ||
2e8488b4 | 586 | #define LINE_INFO \ |
660e09ad | 587 | LINE(DIFF_HEADER, "diff --git ", COLOR_YELLOW, COLOR_DEFAULT, 0), \ |
a28bcc22 JF |
588 | LINE(DIFF_CHUNK, "@@", COLOR_MAGENTA, COLOR_DEFAULT, 0), \ |
589 | LINE(DIFF_ADD, "+", COLOR_GREEN, COLOR_DEFAULT, 0), \ | |
590 | LINE(DIFF_DEL, "-", COLOR_RED, COLOR_DEFAULT, 0), \ | |
660e09ad JF |
591 | LINE(DIFF_INDEX, "index ", COLOR_BLUE, COLOR_DEFAULT, 0), \ |
592 | LINE(DIFF_OLDMODE, "old file mode ", COLOR_YELLOW, COLOR_DEFAULT, 0), \ | |
593 | LINE(DIFF_NEWMODE, "new file mode ", COLOR_YELLOW, COLOR_DEFAULT, 0), \ | |
594 | LINE(DIFF_COPY_FROM, "copy from", COLOR_YELLOW, COLOR_DEFAULT, 0), \ | |
595 | LINE(DIFF_COPY_TO, "copy to", COLOR_YELLOW, COLOR_DEFAULT, 0), \ | |
596 | LINE(DIFF_RENAME_FROM, "rename from", COLOR_YELLOW, COLOR_DEFAULT, 0), \ | |
597 | LINE(DIFF_RENAME_TO, "rename to", COLOR_YELLOW, COLOR_DEFAULT, 0), \ | |
598 | LINE(DIFF_SIMILARITY, "similarity ", COLOR_YELLOW, COLOR_DEFAULT, 0), \ | |
599 | LINE(DIFF_DISSIMILARITY,"dissimilarity ", COLOR_YELLOW, COLOR_DEFAULT, 0), \ | |
600 | LINE(DIFF_TREE, "diff-tree ", COLOR_BLUE, COLOR_DEFAULT, 0), \ | |
6908bdbd | 601 | LINE(PP_AUTHOR, "Author: ", COLOR_CYAN, COLOR_DEFAULT, 0), \ |
8855ada4 | 602 | LINE(PP_COMMIT, "Commit: ", COLOR_MAGENTA, COLOR_DEFAULT, 0), \ |
6908bdbd JF |
603 | LINE(PP_MERGE, "Merge: ", COLOR_BLUE, COLOR_DEFAULT, 0), \ |
604 | LINE(PP_DATE, "Date: ", COLOR_YELLOW, COLOR_DEFAULT, 0), \ | |
8855ada4 JF |
605 | LINE(PP_ADATE, "AuthorDate: ", COLOR_YELLOW, COLOR_DEFAULT, 0), \ |
606 | LINE(PP_CDATE, "CommitDate: ", COLOR_YELLOW, COLOR_DEFAULT, 0), \ | |
7b99a34c | 607 | LINE(PP_REFS, "Refs: ", COLOR_RED, COLOR_DEFAULT, 0), \ |
a28bcc22 JF |
608 | LINE(COMMIT, "commit ", COLOR_GREEN, COLOR_DEFAULT, 0), \ |
609 | LINE(PARENT, "parent ", COLOR_BLUE, COLOR_DEFAULT, 0), \ | |
610 | LINE(TREE, "tree ", COLOR_BLUE, COLOR_DEFAULT, 0), \ | |
8855ada4 | 611 | LINE(AUTHOR, "author ", COLOR_CYAN, COLOR_DEFAULT, 0), \ |
a28bcc22 | 612 | LINE(COMMITTER, "committer ", COLOR_MAGENTA, COLOR_DEFAULT, 0), \ |
a28bcc22 | 613 | LINE(SIGNOFF, " Signed-off-by", COLOR_YELLOW, COLOR_DEFAULT, 0), \ |
d4d8de8f | 614 | LINE(ACKED, " Acked-by", COLOR_YELLOW, COLOR_DEFAULT, 0), \ |
a28bcc22 JF |
615 | LINE(DEFAULT, "", COLOR_DEFAULT, COLOR_DEFAULT, A_NORMAL), \ |
616 | LINE(CURSOR, "", COLOR_WHITE, COLOR_GREEN, A_BOLD), \ | |
617 | LINE(STATUS, "", COLOR_GREEN, COLOR_DEFAULT, 0), \ | |
6b161b31 JF |
618 | LINE(TITLE_BLUR, "", COLOR_WHITE, COLOR_BLUE, 0), \ |
619 | LINE(TITLE_FOCUS, "", COLOR_WHITE, COLOR_BLUE, A_BOLD), \ | |
a28bcc22 JF |
620 | LINE(MAIN_DATE, "", COLOR_BLUE, COLOR_DEFAULT, 0), \ |
621 | LINE(MAIN_AUTHOR, "", COLOR_GREEN, COLOR_DEFAULT, 0), \ | |
622 | LINE(MAIN_COMMIT, "", COLOR_DEFAULT, COLOR_DEFAULT, 0), \ | |
c34d9c9f JF |
623 | LINE(MAIN_DELIM, "", COLOR_MAGENTA, COLOR_DEFAULT, 0), \ |
624 | LINE(MAIN_TAG, "", COLOR_MAGENTA, COLOR_DEFAULT, A_BOLD), \ | |
e15ec88e | 625 | LINE(MAIN_REMOTE, "", COLOR_YELLOW, COLOR_DEFAULT, A_BOLD), \ |
660e09ad | 626 | LINE(MAIN_REF, "", COLOR_CYAN, COLOR_DEFAULT, A_BOLD), \ |
e733ee54 | 627 | LINE(TREE_DIR, "", COLOR_DEFAULT, COLOR_DEFAULT, A_NORMAL), \ |
173d76ea | 628 | LINE(TREE_FILE, "", COLOR_DEFAULT, COLOR_DEFAULT, A_NORMAL), \ |
53924375 | 629 | LINE(STAT_SECTION, "", COLOR_CYAN, COLOR_DEFAULT, 0), \ |
173d76ea | 630 | LINE(STAT_NONE, "", COLOR_DEFAULT, COLOR_DEFAULT, 0), \ |
53924375 JF |
631 | LINE(STAT_STAGED, "", COLOR_MAGENTA, COLOR_DEFAULT, 0), \ |
632 | LINE(STAT_UNSTAGED,"", COLOR_MAGENTA, COLOR_DEFAULT, 0), \ | |
173d76ea | 633 | LINE(STAT_UNTRACKED,"", COLOR_MAGENTA, COLOR_DEFAULT, 0) |
660e09ad | 634 | |
78c70acd | 635 | enum line_type { |
2e8488b4 JF |
636 | #define LINE(type, line, fg, bg, attr) \ |
637 | LINE_##type | |
638 | LINE_INFO | |
639 | #undef LINE | |
78c70acd JF |
640 | }; |
641 | ||
642 | struct line_info { | |
660e09ad JF |
643 | const char *name; /* Option name. */ |
644 | int namelen; /* Size of option name. */ | |
4685845e | 645 | const char *line; /* The start of line to match. */ |
2e8488b4 JF |
646 | int linelen; /* Size of string to match. */ |
647 | int fg, bg, attr; /* Color and text attributes for the lines. */ | |
78c70acd JF |
648 | }; |
649 | ||
2e8488b4 | 650 | static struct line_info line_info[] = { |
78c70acd | 651 | #define LINE(type, line, fg, bg, attr) \ |
660e09ad | 652 | { #type, STRING_SIZE(#type), (line), STRING_SIZE(line), (fg), (bg), (attr) } |
2e8488b4 JF |
653 | LINE_INFO |
654 | #undef LINE | |
78c70acd JF |
655 | }; |
656 | ||
2e8488b4 JF |
657 | static enum line_type |
658 | get_line_type(char *line) | |
78c70acd JF |
659 | { |
660 | int linelen = strlen(line); | |
a28bcc22 | 661 | enum line_type type; |
78c70acd | 662 | |
a28bcc22 | 663 | for (type = 0; type < ARRAY_SIZE(line_info); type++) |
2e8488b4 | 664 | /* Case insensitive search matches Signed-off-by lines better. */ |
a28bcc22 JF |
665 | if (linelen >= line_info[type].linelen && |
666 | !strncasecmp(line_info[type].line, line, line_info[type].linelen)) | |
667 | return type; | |
78c70acd | 668 | |
2e8488b4 | 669 | return LINE_DEFAULT; |
78c70acd JF |
670 | } |
671 | ||
2e8488b4 | 672 | static inline int |
78c70acd JF |
673 | get_line_attr(enum line_type type) |
674 | { | |
2e8488b4 JF |
675 | assert(type < ARRAY_SIZE(line_info)); |
676 | return COLOR_PAIR(type) | line_info[type].attr; | |
78c70acd JF |
677 | } |
678 | ||
660e09ad JF |
679 | static struct line_info * |
680 | get_line_info(char *name, int namelen) | |
681 | { | |
682 | enum line_type type; | |
660e09ad JF |
683 | |
684 | for (type = 0; type < ARRAY_SIZE(line_info); type++) | |
685 | if (namelen == line_info[type].namelen && | |
201f5a18 | 686 | !string_enum_compare(line_info[type].name, name, namelen)) |
660e09ad JF |
687 | return &line_info[type]; |
688 | ||
689 | return NULL; | |
690 | } | |
691 | ||
78c70acd JF |
692 | static void |
693 | init_colors(void) | |
694 | { | |
82e78006 JF |
695 | int default_bg = COLOR_BLACK; |
696 | int default_fg = COLOR_WHITE; | |
a28bcc22 | 697 | enum line_type type; |
78c70acd JF |
698 | |
699 | start_color(); | |
700 | ||
701 | if (use_default_colors() != ERR) { | |
82e78006 JF |
702 | default_bg = -1; |
703 | default_fg = -1; | |
78c70acd JF |
704 | } |
705 | ||
a28bcc22 JF |
706 | for (type = 0; type < ARRAY_SIZE(line_info); type++) { |
707 | struct line_info *info = &line_info[type]; | |
82e78006 JF |
708 | int bg = info->bg == COLOR_DEFAULT ? default_bg : info->bg; |
709 | int fg = info->fg == COLOR_DEFAULT ? default_fg : info->fg; | |
78c70acd | 710 | |
a28bcc22 | 711 | init_pair(type, fg, bg); |
78c70acd JF |
712 | } |
713 | } | |
714 | ||
fe7233c3 JF |
715 | struct line { |
716 | enum line_type type; | |
3c571d67 JF |
717 | |
718 | /* State flags */ | |
719 | unsigned int selected:1; | |
720 | ||
fe7233c3 JF |
721 | void *data; /* User data */ |
722 | }; | |
723 | ||
78c70acd | 724 | |
1899507c | 725 | /* |
37157fa0 JF |
726 | * Keys |
727 | */ | |
728 | ||
93a97d86 | 729 | struct keybinding { |
37157fa0 | 730 | int alias; |
93a97d86 | 731 | enum request request; |
04e2b7b2 | 732 | struct keybinding *next; |
37157fa0 JF |
733 | }; |
734 | ||
93a97d86 | 735 | static struct keybinding default_keybindings[] = { |
37157fa0 JF |
736 | /* View switching */ |
737 | { 'm', REQ_VIEW_MAIN }, | |
738 | { 'd', REQ_VIEW_DIFF }, | |
739 | { 'l', REQ_VIEW_LOG }, | |
e733ee54 | 740 | { 't', REQ_VIEW_TREE }, |
0001fc34 | 741 | { 'f', REQ_VIEW_BLOB }, |
37157fa0 JF |
742 | { 'p', REQ_VIEW_PAGER }, |
743 | { 'h', REQ_VIEW_HELP }, | |
173d76ea | 744 | { 'S', REQ_VIEW_STATUS }, |
3e634113 | 745 | { 'c', REQ_VIEW_STAGE }, |
37157fa0 JF |
746 | |
747 | /* View manipulation */ | |
748 | { 'q', REQ_VIEW_CLOSE }, | |
749 | { KEY_TAB, REQ_VIEW_NEXT }, | |
750 | { KEY_RETURN, REQ_ENTER }, | |
751 | { KEY_UP, REQ_PREVIOUS }, | |
752 | { KEY_DOWN, REQ_NEXT }, | |
753 | ||
754 | /* Cursor navigation */ | |
755 | { 'k', REQ_MOVE_UP }, | |
756 | { 'j', REQ_MOVE_DOWN }, | |
757 | { KEY_HOME, REQ_MOVE_FIRST_LINE }, | |
758 | { KEY_END, REQ_MOVE_LAST_LINE }, | |
759 | { KEY_NPAGE, REQ_MOVE_PAGE_DOWN }, | |
760 | { ' ', REQ_MOVE_PAGE_DOWN }, | |
761 | { KEY_PPAGE, REQ_MOVE_PAGE_UP }, | |
762 | { 'b', REQ_MOVE_PAGE_UP }, | |
763 | { '-', REQ_MOVE_PAGE_UP }, | |
764 | ||
765 | /* Scrolling */ | |
766 | { KEY_IC, REQ_SCROLL_LINE_UP }, | |
767 | { KEY_DC, REQ_SCROLL_LINE_DOWN }, | |
768 | { 'w', REQ_SCROLL_PAGE_UP }, | |
769 | { 's', REQ_SCROLL_PAGE_DOWN }, | |
770 | ||
4af34daa JF |
771 | /* Searching */ |
772 | { '/', REQ_SEARCH }, | |
773 | { '?', REQ_SEARCH_BACK }, | |
774 | { 'n', REQ_FIND_NEXT }, | |
775 | { 'N', REQ_FIND_PREV }, | |
776 | ||
37157fa0 JF |
777 | /* Misc */ |
778 | { 'Q', REQ_QUIT }, | |
779 | { 'z', REQ_STOP_LOADING }, | |
780 | { 'v', REQ_SHOW_VERSION }, | |
781 | { 'r', REQ_SCREEN_REDRAW }, | |
904e68d8 | 782 | { '.', REQ_TOGGLE_LINENO }, |
73fb51d5 | 783 | { 'g', REQ_TOGGLE_REV_GRAPH }, |
37157fa0 | 784 | { ':', REQ_PROMPT }, |
ca1d71ea | 785 | { 'u', REQ_STATUS_UPDATE }, |
0cea0d43 | 786 | { 'e', REQ_EDIT }, |
37157fa0 | 787 | |
1d754561 | 788 | /* Using the ncurses SIGWINCH handler. */ |
37157fa0 JF |
789 | { KEY_RESIZE, REQ_SCREEN_RESIZE }, |
790 | }; | |
791 | ||
04e2b7b2 JF |
792 | #define KEYMAP_INFO \ |
793 | KEYMAP_(GENERIC), \ | |
794 | KEYMAP_(MAIN), \ | |
795 | KEYMAP_(DIFF), \ | |
796 | KEYMAP_(LOG), \ | |
e733ee54 JF |
797 | KEYMAP_(TREE), \ |
798 | KEYMAP_(BLOB), \ | |
04e2b7b2 | 799 | KEYMAP_(PAGER), \ |
173d76ea | 800 | KEYMAP_(HELP), \ |
3e634113 JF |
801 | KEYMAP_(STATUS), \ |
802 | KEYMAP_(STAGE) | |
04e2b7b2 JF |
803 | |
804 | enum keymap { | |
805 | #define KEYMAP_(name) KEYMAP_##name | |
806 | KEYMAP_INFO | |
807 | #undef KEYMAP_ | |
808 | }; | |
809 | ||
810 | static struct int_map keymap_table[] = { | |
811 | #define KEYMAP_(name) { #name, STRING_SIZE(#name), KEYMAP_##name } | |
812 | KEYMAP_INFO | |
813 | #undef KEYMAP_ | |
814 | }; | |
815 | ||
816 | #define set_keymap(map, name) \ | |
817 | set_from_int_map(keymap_table, ARRAY_SIZE(keymap_table), map, name, strlen(name)) | |
818 | ||
819 | static struct keybinding *keybindings[ARRAY_SIZE(keymap_table)]; | |
820 | ||
821 | static void | |
822 | add_keybinding(enum keymap keymap, enum request request, int key) | |
823 | { | |
824 | struct keybinding *keybinding; | |
825 | ||
826 | keybinding = calloc(1, sizeof(*keybinding)); | |
827 | if (!keybinding) | |
828 | die("Failed to allocate keybinding"); | |
829 | ||
830 | keybinding->alias = key; | |
831 | keybinding->request = request; | |
832 | keybinding->next = keybindings[keymap]; | |
833 | keybindings[keymap] = keybinding; | |
834 | } | |
835 | ||
836 | /* Looks for a key binding first in the given map, then in the generic map, and | |
837 | * lastly in the default keybindings. */ | |
37157fa0 | 838 | static enum request |
04e2b7b2 | 839 | get_keybinding(enum keymap keymap, int key) |
37157fa0 | 840 | { |
04e2b7b2 | 841 | struct keybinding *kbd; |
37157fa0 JF |
842 | int i; |
843 | ||
04e2b7b2 JF |
844 | for (kbd = keybindings[keymap]; kbd; kbd = kbd->next) |
845 | if (kbd->alias == key) | |
846 | return kbd->request; | |
847 | ||
848 | for (kbd = keybindings[KEYMAP_GENERIC]; kbd; kbd = kbd->next) | |
849 | if (kbd->alias == key) | |
850 | return kbd->request; | |
851 | ||
93a97d86 JF |
852 | for (i = 0; i < ARRAY_SIZE(default_keybindings); i++) |
853 | if (default_keybindings[i].alias == key) | |
854 | return default_keybindings[i].request; | |
37157fa0 JF |
855 | |
856 | return (enum request) key; | |
857 | } | |
858 | ||
93a97d86 | 859 | |
37157fa0 JF |
860 | struct key { |
861 | char *name; | |
862 | int value; | |
863 | }; | |
864 | ||
865 | static struct key key_table[] = { | |
866 | { "Enter", KEY_RETURN }, | |
867 | { "Space", ' ' }, | |
868 | { "Backspace", KEY_BACKSPACE }, | |
869 | { "Tab", KEY_TAB }, | |
870 | { "Escape", KEY_ESC }, | |
871 | { "Left", KEY_LEFT }, | |
872 | { "Right", KEY_RIGHT }, | |
873 | { "Up", KEY_UP }, | |
874 | { "Down", KEY_DOWN }, | |
875 | { "Insert", KEY_IC }, | |
876 | { "Delete", KEY_DC }, | |
74f83ee6 | 877 | { "Hash", '#' }, |
37157fa0 JF |
878 | { "Home", KEY_HOME }, |
879 | { "End", KEY_END }, | |
880 | { "PageUp", KEY_PPAGE }, | |
881 | { "PageDown", KEY_NPAGE }, | |
882 | { "F1", KEY_F(1) }, | |
883 | { "F2", KEY_F(2) }, | |
884 | { "F3", KEY_F(3) }, | |
885 | { "F4", KEY_F(4) }, | |
886 | { "F5", KEY_F(5) }, | |
887 | { "F6", KEY_F(6) }, | |
888 | { "F7", KEY_F(7) }, | |
889 | { "F8", KEY_F(8) }, | |
890 | { "F9", KEY_F(9) }, | |
891 | { "F10", KEY_F(10) }, | |
892 | { "F11", KEY_F(11) }, | |
893 | { "F12", KEY_F(12) }, | |
894 | }; | |
895 | ||
04e2b7b2 JF |
896 | static int |
897 | get_key_value(const char *name) | |
898 | { | |
899 | int i; | |
900 | ||
901 | for (i = 0; i < ARRAY_SIZE(key_table); i++) | |
902 | if (!strcasecmp(key_table[i].name, name)) | |
903 | return key_table[i].value; | |
904 | ||
905 | if (strlen(name) == 1 && isprint(*name)) | |
906 | return (int) *name; | |
907 | ||
908 | return ERR; | |
909 | } | |
910 | ||
37157fa0 JF |
911 | static char * |
912 | get_key(enum request request) | |
913 | { | |
914 | static char buf[BUFSIZ]; | |
915 | static char key_char[] = "'X'"; | |
d65ced0d | 916 | size_t pos = 0; |
520094b4 | 917 | char *sep = ""; |
37157fa0 JF |
918 | int i; |
919 | ||
920 | buf[pos] = 0; | |
921 | ||
93a97d86 JF |
922 | for (i = 0; i < ARRAY_SIZE(default_keybindings); i++) { |
923 | struct keybinding *keybinding = &default_keybindings[i]; | |
37157fa0 JF |
924 | char *seq = NULL; |
925 | int key; | |
926 | ||
93a97d86 | 927 | if (keybinding->request != request) |
37157fa0 JF |
928 | continue; |
929 | ||
930 | for (key = 0; key < ARRAY_SIZE(key_table); key++) | |
93a97d86 | 931 | if (key_table[key].value == keybinding->alias) |
37157fa0 JF |
932 | seq = key_table[key].name; |
933 | ||
934 | if (seq == NULL && | |
93a97d86 JF |
935 | keybinding->alias < 127 && |
936 | isprint(keybinding->alias)) { | |
937 | key_char[1] = (char) keybinding->alias; | |
37157fa0 JF |
938 | seq = key_char; |
939 | } | |
940 | ||
941 | if (!seq) | |
942 | seq = "'?'"; | |
943 | ||
944 | if (!string_format_from(buf, &pos, "%s%s", sep, seq)) | |
945 | return "Too many keybindings!"; | |
946 | sep = ", "; | |
947 | } | |
948 | ||
949 | return buf; | |
950 | } | |
951 | ||
952 | ||
953 | /* | |
1899507c JF |
954 | * User config file handling. |
955 | */ | |
956 | ||
5dc795f2 JF |
957 | static struct int_map color_map[] = { |
958 | #define COLOR_MAP(name) { #name, STRING_SIZE(#name), COLOR_##name } | |
959 | COLOR_MAP(DEFAULT), | |
960 | COLOR_MAP(BLACK), | |
961 | COLOR_MAP(BLUE), | |
962 | COLOR_MAP(CYAN), | |
963 | COLOR_MAP(GREEN), | |
964 | COLOR_MAP(MAGENTA), | |
965 | COLOR_MAP(RED), | |
966 | COLOR_MAP(WHITE), | |
967 | COLOR_MAP(YELLOW), | |
968 | }; | |
969 | ||
9256ab05 JF |
970 | #define set_color(color, name) \ |
971 | set_from_int_map(color_map, ARRAY_SIZE(color_map), color, name, strlen(name)) | |
660e09ad | 972 | |
5dc795f2 JF |
973 | static struct int_map attr_map[] = { |
974 | #define ATTR_MAP(name) { #name, STRING_SIZE(#name), A_##name } | |
975 | ATTR_MAP(NORMAL), | |
976 | ATTR_MAP(BLINK), | |
977 | ATTR_MAP(BOLD), | |
978 | ATTR_MAP(DIM), | |
979 | ATTR_MAP(REVERSE), | |
980 | ATTR_MAP(STANDOUT), | |
981 | ATTR_MAP(UNDERLINE), | |
982 | }; | |
983 | ||
9256ab05 JF |
984 | #define set_attribute(attr, name) \ |
985 | set_from_int_map(attr_map, ARRAY_SIZE(attr_map), attr, name, strlen(name)) | |
660e09ad | 986 | |
3c3801c2 JF |
987 | static int config_lineno; |
988 | static bool config_errors; | |
989 | static char *config_msg; | |
990 | ||
5bfd96c7 | 991 | /* Wants: object fgcolor bgcolor [attr] */ |
660e09ad | 992 | static int |
5bfd96c7 | 993 | option_color_command(int argc, char *argv[]) |
660e09ad | 994 | { |
bca8fcaa JF |
995 | struct line_info *info; |
996 | ||
9256ab05 JF |
997 | if (argc != 3 && argc != 4) { |
998 | config_msg = "Wrong number of arguments given to color command"; | |
999 | return ERR; | |
1000 | } | |
1001 | ||
1002 | info = get_line_info(argv[0], strlen(argv[0])); | |
bca8fcaa JF |
1003 | if (!info) { |
1004 | config_msg = "Unknown color name"; | |
1005 | return ERR; | |
1006 | } | |
660e09ad | 1007 | |
a3653368 JF |
1008 | if (set_color(&info->fg, argv[1]) == ERR || |
1009 | set_color(&info->bg, argv[2]) == ERR) { | |
bca8fcaa JF |
1010 | config_msg = "Unknown color"; |
1011 | return ERR; | |
1012 | } | |
660e09ad | 1013 | |
9256ab05 | 1014 | if (argc == 4 && set_attribute(&info->attr, argv[3]) == ERR) { |
bca8fcaa JF |
1015 | config_msg = "Unknown attribute"; |
1016 | return ERR; | |
660e09ad JF |
1017 | } |
1018 | ||
bca8fcaa JF |
1019 | return OK; |
1020 | } | |
1021 | ||
5bfd96c7 JF |
1022 | /* Wants: name = value */ |
1023 | static int | |
1024 | option_set_command(int argc, char *argv[]) | |
1025 | { | |
1026 | if (argc != 3) { | |
1027 | config_msg = "Wrong number of arguments given to set command"; | |
1028 | return ERR; | |
1029 | } | |
1030 | ||
1031 | if (strcmp(argv[1], "=")) { | |
1032 | config_msg = "No value assigned"; | |
1033 | return ERR; | |
1034 | } | |
1035 | ||
1036 | if (!strcmp(argv[0], "show-rev-graph")) { | |
1037 | opt_rev_graph = (!strcmp(argv[2], "1") || | |
1038 | !strcmp(argv[2], "true") || | |
1039 | !strcmp(argv[2], "yes")); | |
1040 | return OK; | |
1041 | } | |
1042 | ||
1043 | if (!strcmp(argv[0], "line-number-interval")) { | |
1044 | opt_num_interval = atoi(argv[2]); | |
1045 | return OK; | |
1046 | } | |
1047 | ||
1048 | if (!strcmp(argv[0], "tab-size")) { | |
1049 | opt_tab_size = atoi(argv[2]); | |
1050 | return OK; | |
1051 | } | |
1052 | ||
cb7267ee | 1053 | if (!strcmp(argv[0], "commit-encoding")) { |
3cc9a4d4 JF |
1054 | char *arg = argv[2]; |
1055 | int delimiter = *arg; | |
1056 | int i; | |
1057 | ||
1058 | switch (delimiter) { | |
1059 | case '"': | |
1060 | case '\'': | |
1061 | for (arg++, i = 0; arg[i]; i++) | |
1062 | if (arg[i] == delimiter) { | |
1063 | arg[i] = 0; | |
1064 | break; | |
1065 | } | |
1066 | default: | |
739e81de | 1067 | string_ncopy(opt_encoding, arg, strlen(arg)); |
3cc9a4d4 JF |
1068 | return OK; |
1069 | } | |
5bfd96c7 JF |
1070 | } |
1071 | ||
a3653368 | 1072 | config_msg = "Unknown variable name"; |
5bfd96c7 JF |
1073 | return ERR; |
1074 | } | |
1075 | ||
04e2b7b2 JF |
1076 | /* Wants: mode request key */ |
1077 | static int | |
1078 | option_bind_command(int argc, char *argv[]) | |
1079 | { | |
1080 | enum request request; | |
1081 | int keymap; | |
1082 | int key; | |
1083 | ||
1084 | if (argc != 3) { | |
1085 | config_msg = "Wrong number of arguments given to bind command"; | |
1086 | return ERR; | |
1087 | } | |
1088 | ||
1089 | if (set_keymap(&keymap, argv[0]) == ERR) { | |
1090 | config_msg = "Unknown key map"; | |
1091 | return ERR; | |
1092 | } | |
1093 | ||
1094 | key = get_key_value(argv[1]); | |
1095 | if (key == ERR) { | |
1096 | config_msg = "Unknown key"; | |
1097 | return ERR; | |
1098 | } | |
1099 | ||
1100 | request = get_request(argv[2]); | |
1101 | if (request == REQ_UNKNOWN) { | |
1102 | config_msg = "Unknown request name"; | |
1103 | return ERR; | |
1104 | } | |
1105 | ||
1106 | add_keybinding(keymap, request, key); | |
1107 | ||
1108 | return OK; | |
1109 | } | |
1110 | ||
bca8fcaa | 1111 | static int |
9256ab05 | 1112 | set_option(char *opt, char *value) |
bca8fcaa | 1113 | { |
9256ab05 JF |
1114 | char *argv[16]; |
1115 | int valuelen; | |
1116 | int argc = 0; | |
1117 | ||
1118 | /* Tokenize */ | |
1119 | while (argc < ARRAY_SIZE(argv) && (valuelen = strcspn(value, " \t"))) { | |
1120 | argv[argc++] = value; | |
1121 | ||
1122 | value += valuelen; | |
1123 | if (!*value) | |
1124 | break; | |
1125 | ||
1126 | *value++ = 0; | |
1127 | while (isspace(*value)) | |
1128 | value++; | |
1129 | } | |
1130 | ||
1131 | if (!strcmp(opt, "color")) | |
5bfd96c7 JF |
1132 | return option_color_command(argc, argv); |
1133 | ||
1134 | if (!strcmp(opt, "set")) | |
1135 | return option_set_command(argc, argv); | |
bca8fcaa | 1136 | |
04e2b7b2 JF |
1137 | if (!strcmp(opt, "bind")) |
1138 | return option_bind_command(argc, argv); | |
1139 | ||
a3653368 | 1140 | config_msg = "Unknown option command"; |
660e09ad JF |
1141 | return ERR; |
1142 | } | |
1143 | ||
1144 | static int | |
5699e0cf | 1145 | read_option(char *opt, size_t optlen, char *value, size_t valuelen) |
3c3801c2 | 1146 | { |
a3653368 JF |
1147 | int status = OK; |
1148 | ||
3c3801c2 JF |
1149 | config_lineno++; |
1150 | config_msg = "Internal error"; | |
1151 | ||
a3653368 JF |
1152 | /* Check for comment markers, since read_properties() will |
1153 | * only ensure opt and value are split at first " \t". */ | |
74f83ee6 | 1154 | optlen = strcspn(opt, "#"); |
a3653368 | 1155 | if (optlen == 0) |
3c3801c2 JF |
1156 | return OK; |
1157 | ||
a3653368 JF |
1158 | if (opt[optlen] != 0) { |
1159 | config_msg = "No option value"; | |
1160 | status = ERR; | |
1161 | ||
1162 | } else { | |
1163 | /* Look for comment endings in the value. */ | |
5699e0cf | 1164 | size_t len = strcspn(value, "#"); |
a3653368 JF |
1165 | |
1166 | if (len < valuelen) { | |
1167 | valuelen = len; | |
1168 | value[valuelen] = 0; | |
1169 | } | |
1170 | ||
1171 | status = set_option(opt, value); | |
3c3801c2 JF |
1172 | } |
1173 | ||
a3653368 JF |
1174 | if (status == ERR) { |
1175 | fprintf(stderr, "Error on line %d, near '%.*s': %s\n", | |
07c3971e | 1176 | config_lineno, (int) optlen, opt, config_msg); |
3c3801c2 JF |
1177 | config_errors = TRUE; |
1178 | } | |
1179 | ||
1180 | /* Always keep going if errors are encountered. */ | |
1181 | return OK; | |
1182 | } | |
1183 | ||
1184 | static int | |
660e09ad JF |
1185 | load_options(void) |
1186 | { | |
1187 | char *home = getenv("HOME"); | |
17482b11 | 1188 | char buf[SIZEOF_STR]; |
660e09ad JF |
1189 | FILE *file; |
1190 | ||
3c3801c2 JF |
1191 | config_lineno = 0; |
1192 | config_errors = FALSE; | |
1193 | ||
cc2d1364 | 1194 | if (!home || !string_format(buf, "%s/.tigrc", home)) |
660e09ad JF |
1195 | return ERR; |
1196 | ||
1197 | /* It's ok that the file doesn't exist. */ | |
1198 | file = fopen(buf, "r"); | |
1199 | if (!file) | |
1200 | return OK; | |
1201 | ||
3c3801c2 JF |
1202 | if (read_properties(file, " \t", read_option) == ERR || |
1203 | config_errors == TRUE) | |
1204 | fprintf(stderr, "Errors while loading %s.\n", buf); | |
1205 | ||
1206 | return OK; | |
660e09ad JF |
1207 | } |
1208 | ||
1209 | ||
d839253b | 1210 | /* |
468876c9 | 1211 | * The viewer |
d839253b | 1212 | */ |
c2124ccd JF |
1213 | |
1214 | struct view; | |
fe7233c3 | 1215 | struct view_ops; |
c2124ccd JF |
1216 | |
1217 | /* The display array of active views and the index of the current view. */ | |
1218 | static struct view *display[2]; | |
1219 | static unsigned int current_view; | |
1220 | ||
ab4af23e JF |
1221 | /* Reading from the prompt? */ |
1222 | static bool input_mode = FALSE; | |
1223 | ||
33c4f9ea | 1224 | #define foreach_displayed_view(view, i) \ |
c2124ccd JF |
1225 | for (i = 0; i < ARRAY_SIZE(display) && (view = display[i]); i++) |
1226 | ||
9f41488f | 1227 | #define displayed_views() (display[1] != NULL ? 2 : 1) |
c2124ccd | 1228 | |
d839253b | 1229 | /* Current head and commit ID */ |
e733ee54 | 1230 | static char ref_blob[SIZEOF_REF] = ""; |
c2124ccd JF |
1231 | static char ref_commit[SIZEOF_REF] = "HEAD"; |
1232 | static char ref_head[SIZEOF_REF] = "HEAD"; | |
1233 | ||
b801d8b2 | 1234 | struct view { |
03a93dbb | 1235 | const char *name; /* View name */ |
4685845e TH |
1236 | const char *cmd_fmt; /* Default command line format */ |
1237 | const char *cmd_env; /* Command line set via environment */ | |
e733ee54 | 1238 | const char *id; /* Points to either of ref_{head,commit,blob} */ |
6b161b31 | 1239 | |
fe7233c3 | 1240 | struct view_ops *ops; /* View operations */ |
22f66b0a | 1241 | |
04e2b7b2 JF |
1242 | enum keymap keymap; /* What keymap does this view have */ |
1243 | ||
17482b11 | 1244 | char cmd[SIZEOF_STR]; /* Command buffer */ |
49f2b43f JF |
1245 | char ref[SIZEOF_REF]; /* Hovered commit reference */ |
1246 | char vid[SIZEOF_REF]; /* View ID. Set to id member when updating. */ | |
2e8488b4 | 1247 | |
8855ada4 JF |
1248 | int height, width; /* The width and height of the main window */ |
1249 | WINDOW *win; /* The main window */ | |
1250 | WINDOW *title; /* The title window living below the main window */ | |
b801d8b2 JF |
1251 | |
1252 | /* Navigation */ | |
1253 | unsigned long offset; /* Offset of the window top */ | |
1254 | unsigned long lineno; /* Current line number */ | |
1255 | ||
4af34daa JF |
1256 | /* Searching */ |
1257 | char grep[SIZEOF_STR]; /* Search string */ | |
b77b2cb8 | 1258 | regex_t *regex; /* Pre-compiled regex */ |
4af34daa | 1259 | |
f6da0b66 JF |
1260 | /* If non-NULL, points to the view that opened this view. If this view |
1261 | * is closed tig will switch back to the parent view. */ | |
1262 | struct view *parent; | |
1263 | ||
b801d8b2 JF |
1264 | /* Buffering */ |
1265 | unsigned long lines; /* Total number of lines */ | |
fe7233c3 | 1266 | struct line *line; /* Line index */ |
e2c01617 | 1267 | unsigned long line_size;/* Total number of allocated lines */ |
8855ada4 | 1268 | unsigned int digits; /* Number of digits in the lines member. */ |
b801d8b2 JF |
1269 | |
1270 | /* Loading */ | |
1271 | FILE *pipe; | |
2e8488b4 | 1272 | time_t start_time; |
b801d8b2 JF |
1273 | }; |
1274 | ||
fe7233c3 JF |
1275 | struct view_ops { |
1276 | /* What type of content being displayed. Used in the title bar. */ | |
1277 | const char *type; | |
f098944b JF |
1278 | /* Open and reads in all view content. */ |
1279 | bool (*open)(struct view *view); | |
fe7233c3 | 1280 | /* Read one line; updates view->line. */ |
701e4f5d | 1281 | bool (*read)(struct view *view, char *data); |
f098944b JF |
1282 | /* Draw one line; @lineno must be < view->height. */ |
1283 | bool (*draw)(struct view *view, struct line *line, unsigned int lineno, bool selected); | |
586c423d JF |
1284 | /* Depending on view handle a special requests. */ |
1285 | enum request (*request)(struct view *view, enum request request, struct line *line); | |
4af34daa JF |
1286 | /* Search for regex in a line. */ |
1287 | bool (*grep)(struct view *view, struct line *line); | |
d720de4b JF |
1288 | /* Select line */ |
1289 | void (*select)(struct view *view, struct line *line); | |
fe7233c3 JF |
1290 | }; |
1291 | ||
6b161b31 JF |
1292 | static struct view_ops pager_ops; |
1293 | static struct view_ops main_ops; | |
e733ee54 JF |
1294 | static struct view_ops tree_ops; |
1295 | static struct view_ops blob_ops; | |
f098944b | 1296 | static struct view_ops help_ops; |
173d76ea | 1297 | static struct view_ops status_ops; |
3e634113 | 1298 | static struct view_ops stage_ops; |
a28bcc22 | 1299 | |
04e2b7b2 JF |
1300 | #define VIEW_STR(name, cmd, env, ref, ops, map) \ |
1301 | { name, cmd, #env, ref, ops, map} | |
1ba2ae4b | 1302 | |
95d7ddcd | 1303 | #define VIEW_(id, name, ops, ref) \ |
04e2b7b2 | 1304 | VIEW_STR(name, TIG_##id##_CMD, TIG_##id##_CMD, ref, ops, KEYMAP_##id) |
1ba2ae4b | 1305 | |
c2124ccd | 1306 | |
b801d8b2 | 1307 | static struct view views[] = { |
173d76ea JF |
1308 | VIEW_(MAIN, "main", &main_ops, ref_head), |
1309 | VIEW_(DIFF, "diff", &pager_ops, ref_commit), | |
1310 | VIEW_(LOG, "log", &pager_ops, ref_head), | |
1311 | VIEW_(TREE, "tree", &tree_ops, ref_commit), | |
1312 | VIEW_(BLOB, "blob", &blob_ops, ref_blob), | |
b64c5b75 JF |
1313 | VIEW_(HELP, "help", &help_ops, ""), |
1314 | VIEW_(PAGER, "pager", &pager_ops, "stdin"), | |
173d76ea | 1315 | VIEW_(STATUS, "status", &status_ops, ""), |
3e634113 | 1316 | VIEW_(STAGE, "stage", &stage_ops, ""), |
b801d8b2 JF |
1317 | }; |
1318 | ||
a28bcc22 JF |
1319 | #define VIEW(req) (&views[(req) - REQ_OFFSET - 1]) |
1320 | ||
699ae55b JF |
1321 | #define foreach_view(view, i) \ |
1322 | for (i = 0; i < ARRAY_SIZE(views) && (view = &views[i]); i++) | |
1323 | ||
1324 | #define view_is_displayed(view) \ | |
1325 | (view == display[0] || view == display[1]) | |
4c6fabc2 | 1326 | |
fe7233c3 JF |
1327 | static bool |
1328 | draw_view_line(struct view *view, unsigned int lineno) | |
1329 | { | |
d720de4b | 1330 | struct line *line; |
5dcf8064 | 1331 | bool selected = (view->offset + lineno == view->lineno); |
4887d44e | 1332 | bool draw_ok; |
d720de4b | 1333 | |
699ae55b JF |
1334 | assert(view_is_displayed(view)); |
1335 | ||
fe7233c3 JF |
1336 | if (view->offset + lineno >= view->lines) |
1337 | return FALSE; | |
1338 | ||
d720de4b JF |
1339 | line = &view->line[view->offset + lineno]; |
1340 | ||
3c571d67 JF |
1341 | if (selected) { |
1342 | line->selected = TRUE; | |
d720de4b | 1343 | view->ops->select(view, line); |
3c571d67 JF |
1344 | } else if (line->selected) { |
1345 | line->selected = FALSE; | |
1346 | wmove(view->win, lineno, 0); | |
1347 | wclrtoeol(view->win); | |
1348 | } | |
d720de4b | 1349 | |
4887d44e JF |
1350 | scrollok(view->win, FALSE); |
1351 | draw_ok = view->ops->draw(view, line, lineno, selected); | |
1352 | scrollok(view->win, TRUE); | |
1353 | ||
1354 | return draw_ok; | |
fe7233c3 JF |
1355 | } |
1356 | ||
b801d8b2 | 1357 | static void |
82e78006 | 1358 | redraw_view_from(struct view *view, int lineno) |
b801d8b2 | 1359 | { |
82e78006 | 1360 | assert(0 <= lineno && lineno < view->height); |
b801d8b2 | 1361 | |
82e78006 | 1362 | for (; lineno < view->height; lineno++) { |
fe7233c3 | 1363 | if (!draw_view_line(view, lineno)) |
fd85fef1 | 1364 | break; |
b801d8b2 JF |
1365 | } |
1366 | ||
1367 | redrawwin(view->win); | |
ab4af23e JF |
1368 | if (input_mode) |
1369 | wnoutrefresh(view->win); | |
1370 | else | |
1371 | wrefresh(view->win); | |
b801d8b2 JF |
1372 | } |
1373 | ||
b76c2afc | 1374 | static void |
82e78006 JF |
1375 | redraw_view(struct view *view) |
1376 | { | |
1377 | wclear(view->win); | |
1378 | redraw_view_from(view, 0); | |
1379 | } | |
1380 | ||
c2124ccd | 1381 | |
6b161b31 | 1382 | static void |
81030ec8 JF |
1383 | update_view_title(struct view *view) |
1384 | { | |
3c112a88 | 1385 | char buf[SIZEOF_STR]; |
71d1c7db JF |
1386 | char state[SIZEOF_STR]; |
1387 | size_t bufpos = 0, statelen = 0; | |
81030ec8 | 1388 | |
3c112a88 | 1389 | assert(view_is_displayed(view)); |
81030ec8 | 1390 | |
249016a6 | 1391 | if (view != VIEW(REQ_VIEW_STATUS) && (view->lines || view->pipe)) { |
6d9c07af | 1392 | unsigned int view_lines = view->offset + view->height; |
c19f8017 | 1393 | unsigned int lines = view->lines |
6d9c07af | 1394 | ? MIN(view_lines, view->lines) * 100 / view->lines |
c19f8017 JF |
1395 | : 0; |
1396 | ||
71d1c7db | 1397 | string_format_from(state, &statelen, "- %s %d of %d (%d%%)", |
3c112a88 JF |
1398 | view->ops->type, |
1399 | view->lineno + 1, | |
1400 | view->lines, | |
1401 | lines); | |
81030ec8 | 1402 | |
5becf244 JF |
1403 | if (view->pipe) { |
1404 | time_t secs = time(NULL) - view->start_time; | |
f97f4012 | 1405 | |
5becf244 JF |
1406 | /* Three git seconds are a long time ... */ |
1407 | if (secs > 2) | |
71d1c7db | 1408 | string_format_from(state, &statelen, " %lds", secs); |
5becf244 | 1409 | } |
81030ec8 JF |
1410 | } |
1411 | ||
71d1c7db JF |
1412 | string_format_from(buf, &bufpos, "[%s]", view->name); |
1413 | if (*view->ref && bufpos < view->width) { | |
1414 | size_t refsize = strlen(view->ref); | |
1415 | size_t minsize = bufpos + 1 + /* abbrev= */ 7 + 1 + statelen; | |
1416 | ||
1417 | if (minsize < view->width) | |
1418 | refsize = view->width - minsize + 7; | |
d1858deb | 1419 | string_format_from(buf, &bufpos, " %.*s", (int) refsize, view->ref); |
71d1c7db | 1420 | } |
f97f4012 | 1421 | |
71d1c7db JF |
1422 | if (statelen && bufpos < view->width) { |
1423 | string_format_from(buf, &bufpos, " %s", state); | |
f97f4012 JF |
1424 | } |
1425 | ||
3c112a88 JF |
1426 | if (view == display[current_view]) |
1427 | wbkgdset(view->title, get_line_attr(LINE_TITLE_FOCUS)); | |
1428 | else | |
1429 | wbkgdset(view->title, get_line_attr(LINE_TITLE_BLUR)); | |
1430 | ||
3c112a88 | 1431 | mvwaddnstr(view->title, 0, 0, buf, bufpos); |
390a8262 | 1432 | wclrtoeol(view->title); |
976447f8 | 1433 | wmove(view->title, 0, view->width - 1); |
ab4af23e JF |
1434 | |
1435 | if (input_mode) | |
1436 | wnoutrefresh(view->title); | |
1437 | else | |
1438 | wrefresh(view->title); | |
81030ec8 JF |
1439 | } |
1440 | ||
1441 | static void | |
6b161b31 | 1442 | resize_display(void) |
b76c2afc | 1443 | { |
03a93dbb | 1444 | int offset, i; |
6b161b31 JF |
1445 | struct view *base = display[0]; |
1446 | struct view *view = display[1] ? display[1] : display[0]; | |
b76c2afc | 1447 | |
6b161b31 | 1448 | /* Setup window dimensions */ |
b76c2afc | 1449 | |
03a93dbb | 1450 | getmaxyx(stdscr, base->height, base->width); |
b76c2afc | 1451 | |
6b161b31 | 1452 | /* Make room for the status window. */ |
03a93dbb | 1453 | base->height -= 1; |
6b161b31 JF |
1454 | |
1455 | if (view != base) { | |
03a93dbb JF |
1456 | /* Horizontal split. */ |
1457 | view->width = base->width; | |
6b161b31 JF |
1458 | view->height = SCALE_SPLIT_VIEW(base->height); |
1459 | base->height -= view->height; | |
1460 | ||
1461 | /* Make room for the title bar. */ | |
1462 | view->height -= 1; | |
1463 | } | |
1464 | ||
1465 | /* Make room for the title bar. */ | |
1466 | base->height -= 1; | |
1467 | ||
1468 | offset = 0; | |
1469 | ||
33c4f9ea | 1470 | foreach_displayed_view (view, i) { |
b76c2afc | 1471 | if (!view->win) { |
c19f8017 | 1472 | view->win = newwin(view->height, 0, offset, 0); |
6b161b31 JF |
1473 | if (!view->win) |
1474 | die("Failed to create %s view", view->name); | |
1475 | ||
1476 | scrollok(view->win, TRUE); | |
1477 | ||
1478 | view->title = newwin(1, 0, offset + view->height, 0); | |
1479 | if (!view->title) | |
1480 | die("Failed to create title window"); | |
1481 | ||
1482 | } else { | |
c19f8017 | 1483 | wresize(view->win, view->height, view->width); |
6b161b31 JF |
1484 | mvwin(view->win, offset, 0); |
1485 | mvwin(view->title, offset + view->height, 0); | |
a28bcc22 | 1486 | } |
a28bcc22 | 1487 | |
6b161b31 | 1488 | offset += view->height + 1; |
b76c2afc | 1489 | } |
6b161b31 | 1490 | } |
b76c2afc | 1491 | |
6b161b31 | 1492 | static void |
20bb5e18 JF |
1493 | redraw_display(void) |
1494 | { | |
1495 | struct view *view; | |
1496 | int i; | |
1497 | ||
33c4f9ea | 1498 | foreach_displayed_view (view, i) { |
20bb5e18 JF |
1499 | redraw_view(view); |
1500 | update_view_title(view); | |
1501 | } | |
1502 | } | |
1503 | ||
85af6284 | 1504 | static void |
2bee3bde | 1505 | update_display_cursor(struct view *view) |
85af6284 | 1506 | { |
85af6284 JF |
1507 | /* Move the cursor to the right-most column of the cursor line. |
1508 | * | |
1509 | * XXX: This could turn out to be a bit expensive, but it ensures that | |
1510 | * the cursor does not jump around. */ | |
1511 | if (view->lines) { | |
1512 | wmove(view->win, view->lineno - view->offset, view->width - 1); | |
1513 | wrefresh(view->win); | |
1514 | } | |
1515 | } | |
20bb5e18 | 1516 | |
2e8488b4 JF |
1517 | /* |
1518 | * Navigation | |
1519 | */ | |
1520 | ||
4a2909a7 | 1521 | /* Scrolling backend */ |
b801d8b2 | 1522 | static void |
8c317212 | 1523 | do_scroll_view(struct view *view, int lines) |
b801d8b2 | 1524 | { |
a0087dd5 JF |
1525 | bool redraw_current_line = FALSE; |
1526 | ||
fd85fef1 JF |
1527 | /* The rendering expects the new offset. */ |
1528 | view->offset += lines; | |
1529 | ||
1530 | assert(0 <= view->offset && view->offset < view->lines); | |
1531 | assert(lines); | |
b801d8b2 | 1532 | |
a0087dd5 JF |
1533 | /* Move current line into the view. */ |
1534 | if (view->lineno < view->offset) { | |
1535 | view->lineno = view->offset; | |
1536 | redraw_current_line = TRUE; | |
1537 | } else if (view->lineno >= view->offset + view->height) { | |
1538 | view->lineno = view->offset + view->height - 1; | |
1539 | redraw_current_line = TRUE; | |
1540 | } | |
1541 | ||
1542 | assert(view->offset <= view->lineno && view->lineno < view->lines); | |
1543 | ||
82e78006 | 1544 | /* Redraw the whole screen if scrolling is pointless. */ |
4c6fabc2 | 1545 | if (view->height < ABS(lines)) { |
b76c2afc JF |
1546 | redraw_view(view); |
1547 | ||
1548 | } else { | |
22f66b0a | 1549 | int line = lines > 0 ? view->height - lines : 0; |
82e78006 | 1550 | int end = line + ABS(lines); |
fd85fef1 JF |
1551 | |
1552 | wscrl(view->win, lines); | |
1553 | ||
22f66b0a | 1554 | for (; line < end; line++) { |
fe7233c3 | 1555 | if (!draw_view_line(view, line)) |
fd85fef1 JF |
1556 | break; |
1557 | } | |
fd85fef1 | 1558 | |
a0087dd5 JF |
1559 | if (redraw_current_line) |
1560 | draw_view_line(view, view->lineno - view->offset); | |
fd85fef1 JF |
1561 | } |
1562 | ||
fd85fef1 JF |
1563 | redrawwin(view->win); |
1564 | wrefresh(view->win); | |
9d3f5834 | 1565 | report(""); |
fd85fef1 | 1566 | } |
78c70acd | 1567 | |
4a2909a7 | 1568 | /* Scroll frontend */ |
fd85fef1 | 1569 | static void |
6b161b31 | 1570 | scroll_view(struct view *view, enum request request) |
fd85fef1 JF |
1571 | { |
1572 | int lines = 1; | |
b801d8b2 | 1573 | |
8c317212 JF |
1574 | assert(view_is_displayed(view)); |
1575 | ||
b801d8b2 | 1576 | switch (request) { |
4a2909a7 | 1577 | case REQ_SCROLL_PAGE_DOWN: |
fd85fef1 | 1578 | lines = view->height; |
4a2909a7 | 1579 | case REQ_SCROLL_LINE_DOWN: |
b801d8b2 | 1580 | if (view->offset + lines > view->lines) |
bde3653a | 1581 | lines = view->lines - view->offset; |
b801d8b2 | 1582 | |
fd85fef1 | 1583 | if (lines == 0 || view->offset + view->height >= view->lines) { |
eb98559e | 1584 | report("Cannot scroll beyond the last line"); |
b801d8b2 JF |
1585 | return; |
1586 | } | |
1587 | break; | |
1588 | ||
4a2909a7 | 1589 | case REQ_SCROLL_PAGE_UP: |
fd85fef1 | 1590 | lines = view->height; |
4a2909a7 | 1591 | case REQ_SCROLL_LINE_UP: |
b801d8b2 JF |
1592 | if (lines > view->offset) |
1593 | lines = view->offset; | |
1594 | ||
1595 | if (lines == 0) { | |
eb98559e | 1596 | report("Cannot scroll beyond the first line"); |
b801d8b2 JF |
1597 | return; |
1598 | } | |
1599 | ||
fd85fef1 | 1600 | lines = -lines; |
b801d8b2 | 1601 | break; |
03a93dbb | 1602 | |
6b161b31 JF |
1603 | default: |
1604 | die("request %d not handled in switch", request); | |
b801d8b2 JF |
1605 | } |
1606 | ||
8c317212 | 1607 | do_scroll_view(view, lines); |
fd85fef1 | 1608 | } |
b801d8b2 | 1609 | |
4a2909a7 | 1610 | /* Cursor moving */ |
fd85fef1 | 1611 | static void |
8522ecc7 | 1612 | move_view(struct view *view, enum request request) |
fd85fef1 | 1613 | { |
dfaa6c81 | 1614 | int scroll_steps = 0; |
fd85fef1 | 1615 | int steps; |
b801d8b2 | 1616 | |
fd85fef1 | 1617 | switch (request) { |
4a2909a7 | 1618 | case REQ_MOVE_FIRST_LINE: |
78c70acd JF |
1619 | steps = -view->lineno; |
1620 | break; | |
1621 | ||
4a2909a7 | 1622 | case REQ_MOVE_LAST_LINE: |
78c70acd JF |
1623 | steps = view->lines - view->lineno - 1; |
1624 | break; | |
1625 | ||
4a2909a7 | 1626 | case REQ_MOVE_PAGE_UP: |
78c70acd JF |
1627 | steps = view->height > view->lineno |
1628 | ? -view->lineno : -view->height; | |
1629 | break; | |
1630 | ||
4a2909a7 | 1631 | case REQ_MOVE_PAGE_DOWN: |
78c70acd JF |
1632 | steps = view->lineno + view->height >= view->lines |
1633 | ? view->lines - view->lineno - 1 : view->height; | |
1634 | break; | |
1635 | ||
4a2909a7 | 1636 | case REQ_MOVE_UP: |
fd85fef1 JF |
1637 | steps = -1; |
1638 | break; | |
b801d8b2 | 1639 | |
4a2909a7 | 1640 | case REQ_MOVE_DOWN: |
fd85fef1 JF |
1641 | steps = 1; |
1642 | break; | |
6b161b31 JF |
1643 | |
1644 | default: | |
1645 | die("request %d not handled in switch", request); | |
78c70acd | 1646 | } |
b801d8b2 | 1647 | |
4c6fabc2 | 1648 | if (steps <= 0 && view->lineno == 0) { |
eb98559e | 1649 | report("Cannot move beyond the first line"); |
78c70acd | 1650 | return; |
b801d8b2 | 1651 | |
6908bdbd | 1652 | } else if (steps >= 0 && view->lineno + 1 >= view->lines) { |
eb98559e | 1653 | report("Cannot move beyond the last line"); |
78c70acd | 1654 | return; |
fd85fef1 JF |
1655 | } |
1656 | ||
4c6fabc2 | 1657 | /* Move the current line */ |
fd85fef1 | 1658 | view->lineno += steps; |
4c6fabc2 JF |
1659 | assert(0 <= view->lineno && view->lineno < view->lines); |
1660 | ||
4c6fabc2 | 1661 | /* Check whether the view needs to be scrolled */ |
fd85fef1 JF |
1662 | if (view->lineno < view->offset || |
1663 | view->lineno >= view->offset + view->height) { | |
dfaa6c81 | 1664 | scroll_steps = steps; |
fd85fef1 | 1665 | if (steps < 0 && -steps > view->offset) { |
dfaa6c81 | 1666 | scroll_steps = -view->offset; |
b76c2afc JF |
1667 | |
1668 | } else if (steps > 0) { | |
1669 | if (view->lineno == view->lines - 1 && | |
1670 | view->lines > view->height) { | |
dfaa6c81 JF |
1671 | scroll_steps = view->lines - view->offset - 1; |
1672 | if (scroll_steps >= view->height) | |
1673 | scroll_steps -= view->height - 1; | |
b76c2afc | 1674 | } |
b801d8b2 | 1675 | } |
8522ecc7 JF |
1676 | } |
1677 | ||
1678 | if (!view_is_displayed(view)) { | |
a3965365 JF |
1679 | view->offset += scroll_steps; |
1680 | assert(0 <= view->offset && view->offset < view->lines); | |
8522ecc7 JF |
1681 | view->ops->select(view, &view->line[view->lineno]); |
1682 | return; | |
1683 | } | |
1684 | ||
1685 | /* Repaint the old "current" line if we be scrolling */ | |
1686 | if (ABS(steps) < view->height) | |
1687 | draw_view_line(view, view->lineno - steps - view->offset); | |
1688 | ||
dfaa6c81 JF |
1689 | if (scroll_steps) { |
1690 | do_scroll_view(view, scroll_steps); | |
fd85fef1 | 1691 | return; |
b801d8b2 JF |
1692 | } |
1693 | ||
4c6fabc2 | 1694 | /* Draw the current line */ |
fe7233c3 | 1695 | draw_view_line(view, view->lineno - view->offset); |
fd85fef1 | 1696 | |
b801d8b2 JF |
1697 | redrawwin(view->win); |
1698 | wrefresh(view->win); | |
9d3f5834 | 1699 | report(""); |
b801d8b2 JF |
1700 | } |
1701 | ||
b801d8b2 | 1702 | |
2e8488b4 | 1703 | /* |
4af34daa JF |
1704 | * Searching |
1705 | */ | |
1706 | ||
c02d8fce | 1707 | static void search_view(struct view *view, enum request request); |
4af34daa JF |
1708 | |
1709 | static bool | |
1710 | find_next_line(struct view *view, unsigned long lineno, struct line *line) | |
1711 | { | |
699ae55b JF |
1712 | assert(view_is_displayed(view)); |
1713 | ||
4af34daa JF |
1714 | if (!view->ops->grep(view, line)) |
1715 | return FALSE; | |
1716 | ||
1717 | if (lineno - view->offset >= view->height) { | |
1718 | view->offset = lineno; | |
1719 | view->lineno = lineno; | |
1720 | redraw_view(view); | |
1721 | ||
1722 | } else { | |
1723 | unsigned long old_lineno = view->lineno - view->offset; | |
1724 | ||
1725 | view->lineno = lineno; | |
4af34daa JF |
1726 | draw_view_line(view, old_lineno); |
1727 | ||
1728 | draw_view_line(view, view->lineno - view->offset); | |
1729 | redrawwin(view->win); | |
1730 | wrefresh(view->win); | |
1731 | } | |
1732 | ||
1733 | report("Line %ld matches '%s'", lineno + 1, view->grep); | |
1734 | return TRUE; | |
1735 | } | |
1736 | ||
1737 | static void | |
1738 | find_next(struct view *view, enum request request) | |
1739 | { | |
1740 | unsigned long lineno = view->lineno; | |
1741 | int direction; | |
1742 | ||
1743 | if (!*view->grep) { | |
1744 | if (!*opt_search) | |
1745 | report("No previous search"); | |
1746 | else | |
c02d8fce | 1747 | search_view(view, request); |
4af34daa JF |
1748 | return; |
1749 | } | |
1750 | ||
1751 | switch (request) { | |
1752 | case REQ_SEARCH: | |
1753 | case REQ_FIND_NEXT: | |
1754 | direction = 1; | |
1755 | break; | |
1756 | ||
1757 | case REQ_SEARCH_BACK: | |
1758 | case REQ_FIND_PREV: | |
1759 | direction = -1; | |
1760 | break; | |
1761 | ||
1762 | default: | |
1763 | return; | |
1764 | } | |
1765 | ||
1766 | if (request == REQ_FIND_NEXT || request == REQ_FIND_PREV) | |
1767 | lineno += direction; | |
1768 | ||
1769 | /* Note, lineno is unsigned long so will wrap around in which case it | |
1770 | * will become bigger than view->lines. */ | |
1771 | for (; lineno < view->lines; lineno += direction) { | |
1772 | struct line *line = &view->line[lineno]; | |
1773 | ||
1774 | if (find_next_line(view, lineno, line)) | |
1775 | return; | |
1776 | } | |
1777 | ||
1778 | report("No match found for '%s'", view->grep); | |
1779 | } | |
1780 | ||
1781 | static void | |
c02d8fce | 1782 | search_view(struct view *view, enum request request) |
4af34daa JF |
1783 | { |
1784 | int regex_err; | |
1785 | ||
b77b2cb8 JF |
1786 | if (view->regex) { |
1787 | regfree(view->regex); | |
4af34daa | 1788 | *view->grep = 0; |
b77b2cb8 JF |
1789 | } else { |
1790 | view->regex = calloc(1, sizeof(*view->regex)); | |
1791 | if (!view->regex) | |
1792 | return; | |
4af34daa JF |
1793 | } |
1794 | ||
c02d8fce | 1795 | regex_err = regcomp(view->regex, opt_search, REG_EXTENDED); |
4af34daa JF |
1796 | if (regex_err != 0) { |
1797 | char buf[SIZEOF_STR] = "unknown error"; | |
1798 | ||
b77b2cb8 | 1799 | regerror(regex_err, view->regex, buf, sizeof(buf)); |
e9cacd58 | 1800 | report("Search failed: %s", buf); |
4af34daa JF |
1801 | return; |
1802 | } | |
1803 | ||
c02d8fce | 1804 | string_copy(view->grep, opt_search); |
4af34daa JF |
1805 | |
1806 | find_next(view, request); | |
1807 | } | |
1808 | ||
1809 | /* | |
2e8488b4 JF |
1810 | * Incremental updating |
1811 | */ | |
b801d8b2 | 1812 | |
199d1288 JF |
1813 | static void |
1814 | end_update(struct view *view) | |
1815 | { | |
1816 | if (!view->pipe) | |
1817 | return; | |
1818 | set_nonblocking_input(FALSE); | |
1819 | if (view->pipe == stdin) | |
1820 | fclose(view->pipe); | |
1821 | else | |
1822 | pclose(view->pipe); | |
1823 | view->pipe = NULL; | |
1824 | } | |
1825 | ||
03a93dbb | 1826 | static bool |
b801d8b2 JF |
1827 | begin_update(struct view *view) |
1828 | { | |
199d1288 JF |
1829 | if (view->pipe) |
1830 | end_update(view); | |
1831 | ||
03a93dbb JF |
1832 | if (opt_cmd[0]) { |
1833 | string_copy(view->cmd, opt_cmd); | |
1834 | opt_cmd[0] = 0; | |
035ba11f JF |
1835 | /* When running random commands, initially show the |
1836 | * command in the title. However, it maybe later be | |
1837 | * overwritten if a commit line is selected. */ | |
809a9f48 JF |
1838 | if (view == VIEW(REQ_VIEW_PAGER)) |
1839 | string_copy(view->ref, view->cmd); | |
1840 | else | |
1841 | view->ref[0] = 0; | |
e733ee54 JF |
1842 | |
1843 | } else if (view == VIEW(REQ_VIEW_TREE)) { | |
1844 | const char *format = view->cmd_env ? view->cmd_env : view->cmd_fmt; | |
f0f114ac | 1845 | char path[SIZEOF_STR]; |
e733ee54 JF |
1846 | |
1847 | if (strcmp(view->vid, view->id)) | |
f0f114ac JF |
1848 | opt_path[0] = path[0] = 0; |
1849 | else if (sq_quote(path, 0, opt_path) >= sizeof(path)) | |
1850 | return FALSE; | |
e733ee54 | 1851 | |
739e81de | 1852 | if (!string_format(view->cmd, format, view->id, path)) |
e733ee54 JF |
1853 | return FALSE; |
1854 | ||
03a93dbb | 1855 | } else { |
4685845e | 1856 | const char *format = view->cmd_env ? view->cmd_env : view->cmd_fmt; |
739e81de | 1857 | const char *id = view->id; |
1ba2ae4b | 1858 | |
cc2d1364 | 1859 | if (!string_format(view->cmd, format, id, id, id, id, id)) |
03a93dbb | 1860 | return FALSE; |
035ba11f JF |
1861 | |
1862 | /* Put the current ref_* value to the view title ref | |
1863 | * member. This is needed by the blob view. Most other | |
1864 | * views sets it automatically after loading because the | |
1865 | * first line is a commit line. */ | |
739e81de | 1866 | string_copy_rev(view->ref, view->id); |
03a93dbb | 1867 | } |
b801d8b2 | 1868 | |
6908bdbd JF |
1869 | /* Special case for the pager view. */ |
1870 | if (opt_pipe) { | |
1871 | view->pipe = opt_pipe; | |
1872 | opt_pipe = NULL; | |
1873 | } else { | |
1874 | view->pipe = popen(view->cmd, "r"); | |
1875 | } | |
1876 | ||
2e8488b4 JF |
1877 | if (!view->pipe) |
1878 | return FALSE; | |
b801d8b2 | 1879 | |
6b161b31 | 1880 | set_nonblocking_input(TRUE); |
b801d8b2 JF |
1881 | |
1882 | view->offset = 0; | |
1883 | view->lines = 0; | |
1884 | view->lineno = 0; | |
739e81de | 1885 | string_copy_rev(view->vid, view->id); |
b801d8b2 | 1886 | |
2e8488b4 JF |
1887 | if (view->line) { |
1888 | int i; | |
1889 | ||
1890 | for (i = 0; i < view->lines; i++) | |
fe7233c3 JF |
1891 | if (view->line[i].data) |
1892 | free(view->line[i].data); | |
2e8488b4 JF |
1893 | |
1894 | free(view->line); | |
1895 | view->line = NULL; | |
1896 | } | |
1897 | ||
1898 | view->start_time = time(NULL); | |
1899 | ||
b801d8b2 JF |
1900 | return TRUE; |
1901 | } | |
1902 | ||
e2c01617 JF |
1903 | static struct line * |
1904 | realloc_lines(struct view *view, size_t line_size) | |
1905 | { | |
1906 | struct line *tmp = realloc(view->line, sizeof(*view->line) * line_size); | |
1907 | ||
1908 | if (!tmp) | |
1909 | return NULL; | |
1910 | ||
1911 | view->line = tmp; | |
1912 | view->line_size = line_size; | |
1913 | return view->line; | |
1914 | } | |
1915 | ||
03a93dbb | 1916 | static bool |
b801d8b2 JF |
1917 | update_view(struct view *view) |
1918 | { | |
6b68fd24 JF |
1919 | char in_buffer[BUFSIZ]; |
1920 | char out_buffer[BUFSIZ * 2]; | |
b801d8b2 | 1921 | char *line; |
82e78006 JF |
1922 | /* The number of lines to read. If too low it will cause too much |
1923 | * redrawing (and possible flickering), if too high responsiveness | |
1924 | * will suffer. */ | |
8855ada4 | 1925 | unsigned long lines = view->height; |
82e78006 | 1926 | int redraw_from = -1; |
b801d8b2 JF |
1927 | |
1928 | if (!view->pipe) | |
1929 | return TRUE; | |
1930 | ||
82e78006 JF |
1931 | /* Only redraw if lines are visible. */ |
1932 | if (view->offset + view->height >= view->lines) | |
1933 | redraw_from = view->lines - view->offset; | |
b801d8b2 | 1934 | |
699ae55b | 1935 | /* FIXME: This is probably not perfect for backgrounded views. */ |
e2c01617 | 1936 | if (!realloc_lines(view, view->lines + lines)) |
b801d8b2 JF |
1937 | goto alloc_error; |
1938 | ||
6b68fd24 JF |
1939 | while ((line = fgets(in_buffer, sizeof(in_buffer), view->pipe))) { |
1940 | size_t linelen = strlen(line); | |
b801d8b2 | 1941 | |
b801d8b2 JF |
1942 | if (linelen) |
1943 | line[linelen - 1] = 0; | |
1944 | ||
6b68fd24 | 1945 | if (opt_iconv != ICONV_NONE) { |
ec31d0d0 | 1946 | ICONV_INBUF_TYPE inbuf = line; |
6b68fd24 JF |
1947 | size_t inlen = linelen; |
1948 | ||
1949 | char *outbuf = out_buffer; | |
1950 | size_t outlen = sizeof(out_buffer); | |
1951 | ||
1952 | size_t ret; | |
1953 | ||
7361622d | 1954 | ret = iconv(opt_iconv, &inbuf, &inlen, &outbuf, &outlen); |
6b68fd24 JF |
1955 | if (ret != (size_t) -1) { |
1956 | line = out_buffer; | |
1957 | linelen = strlen(out_buffer); | |
1958 | } | |
1959 | } | |
1960 | ||
701e4f5d | 1961 | if (!view->ops->read(view, line)) |
b801d8b2 | 1962 | goto alloc_error; |
fd85fef1 JF |
1963 | |
1964 | if (lines-- == 1) | |
1965 | break; | |
b801d8b2 JF |
1966 | } |
1967 | ||
8855ada4 JF |
1968 | { |
1969 | int digits; | |
1970 | ||
1971 | lines = view->lines; | |
1972 | for (digits = 0; lines; digits++) | |
1973 | lines /= 10; | |
1974 | ||
1975 | /* Keep the displayed view in sync with line number scaling. */ | |
1976 | if (digits != view->digits) { | |
1977 | view->digits = digits; | |
1978 | redraw_from = 0; | |
1979 | } | |
1980 | } | |
1981 | ||
699ae55b JF |
1982 | if (!view_is_displayed(view)) |
1983 | goto check_pipe; | |
1984 | ||
e733ee54 JF |
1985 | if (view == VIEW(REQ_VIEW_TREE)) { |
1986 | /* Clear the view and redraw everything since the tree sorting | |
1987 | * might have rearranged things. */ | |
1988 | redraw_view(view); | |
1989 | ||
1990 | } else if (redraw_from >= 0) { | |
82e78006 | 1991 | /* If this is an incremental update, redraw the previous line |
a28bcc22 JF |
1992 | * since for commits some members could have changed when |
1993 | * loading the main view. */ | |
82e78006 JF |
1994 | if (redraw_from > 0) |
1995 | redraw_from--; | |
1996 | ||
9eded379 JF |
1997 | /* Since revision graph visualization requires knowledge |
1998 | * about the parent commit, it causes a further one-off | |
1999 | * needed to be redrawn for incremental updates. */ | |
2000 | if (redraw_from > 0 && opt_rev_graph) | |
2001 | redraw_from--; | |
2002 | ||
82e78006 JF |
2003 | /* Incrementally draw avoids flickering. */ |
2004 | redraw_view_from(view, redraw_from); | |
4c6fabc2 | 2005 | } |
b801d8b2 | 2006 | |
eb98559e JF |
2007 | /* Update the title _after_ the redraw so that if the redraw picks up a |
2008 | * commit reference in view->ref it'll be available here. */ | |
2009 | update_view_title(view); | |
2010 | ||
699ae55b | 2011 | check_pipe: |
b801d8b2 | 2012 | if (ferror(view->pipe)) { |
03a93dbb | 2013 | report("Failed to read: %s", strerror(errno)); |
b801d8b2 JF |
2014 | goto end; |
2015 | ||
2016 | } else if (feof(view->pipe)) { | |
f97f4012 | 2017 | report(""); |
b801d8b2 JF |
2018 | goto end; |
2019 | } | |
2020 | ||
2021 | return TRUE; | |
2022 | ||
2023 | alloc_error: | |
2e8488b4 | 2024 | report("Allocation failure"); |
b801d8b2 JF |
2025 | |
2026 | end: | |
be04d936 | 2027 | view->ops->read(view, NULL); |
b801d8b2 JF |
2028 | end_update(view); |
2029 | return FALSE; | |
2030 | } | |
2031 | ||
0a0d8910 | 2032 | static struct line * |
e314c36d | 2033 | add_line_data(struct view *view, void *data, enum line_type type) |
0a0d8910 | 2034 | { |
e314c36d | 2035 | struct line *line = &view->line[view->lines++]; |
0a0d8910 | 2036 | |
e314c36d | 2037 | memset(line, 0, sizeof(*line)); |
0a0d8910 | 2038 | line->type = type; |
e314c36d | 2039 | line->data = data; |
0a0d8910 JF |
2040 | |
2041 | return line; | |
2042 | } | |
2043 | ||
e314c36d JF |
2044 | static struct line * |
2045 | add_line_text(struct view *view, char *data, enum line_type type) | |
2046 | { | |
2047 | if (data) | |
2048 | data = strdup(data); | |
2049 | ||
2050 | return data ? add_line_data(view, data, type) : NULL; | |
2051 | } | |
2052 | ||
79d445ca | 2053 | |
e10154d5 JF |
2054 | /* |
2055 | * View opening | |
2056 | */ | |
2057 | ||
49f2b43f JF |
2058 | enum open_flags { |
2059 | OPEN_DEFAULT = 0, /* Use default view switching. */ | |
2060 | OPEN_SPLIT = 1, /* Split current view. */ | |
2061 | OPEN_BACKGROUNDED = 2, /* Backgrounded. */ | |
2062 | OPEN_RELOAD = 4, /* Reload view even if it is the current. */ | |
2063 | }; | |
2064 | ||
6b161b31 | 2065 | static void |
49f2b43f | 2066 | open_view(struct view *prev, enum request request, enum open_flags flags) |
b801d8b2 | 2067 | { |
49f2b43f JF |
2068 | bool backgrounded = !!(flags & OPEN_BACKGROUNDED); |
2069 | bool split = !!(flags & OPEN_SPLIT); | |
2070 | bool reload = !!(flags & OPEN_RELOAD); | |
a28bcc22 | 2071 | struct view *view = VIEW(request); |
9f41488f | 2072 | int nviews = displayed_views(); |
6e950a52 | 2073 | struct view *base_view = display[0]; |
b801d8b2 | 2074 | |
49f2b43f | 2075 | if (view == prev && nviews == 1 && !reload) { |
6b161b31 JF |
2076 | report("Already in %s view", view->name); |
2077 | return; | |
2078 | } | |
b801d8b2 | 2079 | |
f098944b JF |
2080 | if (view->ops->open) { |
2081 | if (!view->ops->open(view)) { | |
2082 | report("Failed to load %s view", view->name); | |
2083 | return; | |
2084 | } | |
2509b112 JF |
2085 | |
2086 | } else if ((reload || strcmp(view->vid, view->id)) && | |
2087 | !begin_update(view)) { | |
6b161b31 JF |
2088 | report("Failed to load %s view", view->name); |
2089 | return; | |
2090 | } | |
a28bcc22 | 2091 | |
6b161b31 | 2092 | if (split) { |
8d741c06 | 2093 | display[1] = view; |
6b161b31 | 2094 | if (!backgrounded) |
8d741c06 | 2095 | current_view = 1; |
6b161b31 JF |
2096 | } else { |
2097 | /* Maximize the current view. */ | |
2098 | memset(display, 0, sizeof(display)); | |
2099 | current_view = 0; | |
2100 | display[current_view] = view; | |
a28bcc22 | 2101 | } |
b801d8b2 | 2102 | |
6e950a52 JF |
2103 | /* Resize the view when switching between split- and full-screen, |
2104 | * or when switching between two different full-screen views. */ | |
2105 | if (nviews != displayed_views() || | |
2106 | (nviews == 1 && base_view != display[0])) | |
a006db63 | 2107 | resize_display(); |
b801d8b2 | 2108 | |
a8891802 | 2109 | if (split && prev->lineno - prev->offset >= prev->height) { |
03a93dbb | 2110 | /* Take the title line into account. */ |
eb98559e | 2111 | int lines = prev->lineno - prev->offset - prev->height + 1; |
03a93dbb JF |
2112 | |
2113 | /* Scroll the view that was split if the current line is | |
2114 | * outside the new limited view. */ | |
8c317212 | 2115 | do_scroll_view(prev, lines); |
03a93dbb JF |
2116 | } |
2117 | ||
6b161b31 | 2118 | if (prev && view != prev) { |
9b995f0c | 2119 | if (split && !backgrounded) { |
f0b3ab80 JF |
2120 | /* "Blur" the previous view. */ |
2121 | update_view_title(prev); | |
9f396969 | 2122 | } |
f0b3ab80 | 2123 | |
f6da0b66 | 2124 | view->parent = prev; |
b801d8b2 JF |
2125 | } |
2126 | ||
9f396969 | 2127 | if (view->pipe && view->lines == 0) { |
03a93dbb JF |
2128 | /* Clear the old view and let the incremental updating refill |
2129 | * the screen. */ | |
2130 | wclear(view->win); | |
f97f4012 | 2131 | report(""); |
03a93dbb JF |
2132 | } else { |
2133 | redraw_view(view); | |
24b5b3e0 | 2134 | report(""); |
03a93dbb | 2135 | } |
6706b2ba JF |
2136 | |
2137 | /* If the view is backgrounded the above calls to report() | |
2138 | * won't redraw the view title. */ | |
2139 | if (backgrounded) | |
2140 | update_view_title(view); | |
b801d8b2 JF |
2141 | } |
2142 | ||
0cea0d43 JF |
2143 | static void |
2144 | open_editor(struct view *view, char *file) | |
2145 | { | |
2146 | char cmd[SIZEOF_STR]; | |
2147 | char file_sq[SIZEOF_STR]; | |
2148 | char *editor; | |
2149 | ||
2150 | editor = getenv("GIT_EDITOR"); | |
2151 | if (!editor && *opt_editor) | |
2152 | editor = opt_editor; | |
2153 | if (!editor) | |
2154 | editor = getenv("VISUAL"); | |
2155 | if (!editor) | |
2156 | editor = getenv("EDITOR"); | |
2157 | if (!editor) | |
2158 | editor = "vi"; | |
2159 | ||
2160 | if (sq_quote(file_sq, 0, file) < sizeof(file_sq) && | |
2161 | string_format(cmd, "%s %s", editor, file_sq)) { | |
2162 | def_prog_mode(); /* save current tty modes */ | |
2163 | endwin(); /* restore original tty modes */ | |
2164 | system(cmd); | |
2165 | reset_prog_mode(); | |
2166 | redraw_display(); | |
2167 | } | |
2168 | } | |
b801d8b2 | 2169 | |
6b161b31 JF |
2170 | /* |
2171 | * User request switch noodle | |
2172 | */ | |
2173 | ||
b801d8b2 | 2174 | static int |
6b161b31 | 2175 | view_driver(struct view *view, enum request request) |
b801d8b2 | 2176 | { |
b801d8b2 JF |
2177 | int i; |
2178 | ||
586c423d JF |
2179 | if (view && view->lines) { |
2180 | request = view->ops->request(view, request, &view->line[view->lineno]); | |
2181 | if (request == REQ_NONE) | |
2182 | return TRUE; | |
2183 | } | |
2184 | ||
b801d8b2 | 2185 | switch (request) { |
4a2909a7 JF |
2186 | case REQ_MOVE_UP: |
2187 | case REQ_MOVE_DOWN: | |
2188 | case REQ_MOVE_PAGE_UP: | |
2189 | case REQ_MOVE_PAGE_DOWN: | |
2190 | case REQ_MOVE_FIRST_LINE: | |
2191 | case REQ_MOVE_LAST_LINE: | |
8522ecc7 | 2192 | move_view(view, request); |
fd85fef1 JF |
2193 | break; |
2194 | ||
4a2909a7 JF |
2195 | case REQ_SCROLL_LINE_DOWN: |
2196 | case REQ_SCROLL_LINE_UP: | |
2197 | case REQ_SCROLL_PAGE_DOWN: | |
2198 | case REQ_SCROLL_PAGE_UP: | |
a28bcc22 | 2199 | scroll_view(view, request); |
b801d8b2 JF |
2200 | break; |
2201 | ||
e733ee54 JF |
2202 | case REQ_VIEW_BLOB: |
2203 | if (!ref_blob[0]) { | |
550cd4b5 JF |
2204 | report("No file chosen, press %s to open tree view", |
2205 | get_key(REQ_VIEW_TREE)); | |
e733ee54 JF |
2206 | break; |
2207 | } | |
5c4358d1 JF |
2208 | open_view(view, request, OPEN_DEFAULT); |
2209 | break; | |
2210 | ||
2211 | case REQ_VIEW_PAGER: | |
b64c5b75 | 2212 | if (!opt_pipe && !VIEW(REQ_VIEW_PAGER)->lines) { |
5c4358d1 JF |
2213 | report("No pager content, press %s to run command from prompt", |
2214 | get_key(REQ_PROMPT)); | |
2215 | break; | |
2216 | } | |
2217 | open_view(view, request, OPEN_DEFAULT); | |
2218 | break; | |
2219 | ||
3e634113 JF |
2220 | case REQ_VIEW_STAGE: |
2221 | if (!VIEW(REQ_VIEW_STAGE)->lines) { | |
2222 | report("No stage content, press %s to open the status view and choose file", | |
2223 | get_key(REQ_VIEW_STATUS)); | |
2224 | break; | |
2225 | } | |
2226 | open_view(view, request, OPEN_DEFAULT); | |
2227 | break; | |
2228 | ||
4a2909a7 | 2229 | case REQ_VIEW_MAIN: |
4a2909a7 | 2230 | case REQ_VIEW_DIFF: |
2e8488b4 | 2231 | case REQ_VIEW_LOG: |
e733ee54 | 2232 | case REQ_VIEW_TREE: |
2e8488b4 | 2233 | case REQ_VIEW_HELP: |
173d76ea | 2234 | case REQ_VIEW_STATUS: |
49f2b43f | 2235 | open_view(view, request, OPEN_DEFAULT); |
b801d8b2 JF |
2236 | break; |
2237 | ||
b3a54cba JF |
2238 | case REQ_NEXT: |
2239 | case REQ_PREVIOUS: | |
2240 | request = request == REQ_NEXT ? REQ_MOVE_DOWN : REQ_MOVE_UP; | |
2241 | ||
e733ee54 JF |
2242 | if ((view == VIEW(REQ_VIEW_DIFF) && |
2243 | view->parent == VIEW(REQ_VIEW_MAIN)) || | |
3e634113 | 2244 | (view == VIEW(REQ_VIEW_STAGE) && |
b9b5b4cd | 2245 | view->parent == VIEW(REQ_VIEW_STATUS)) || |
e733ee54 JF |
2246 | (view == VIEW(REQ_VIEW_BLOB) && |
2247 | view->parent == VIEW(REQ_VIEW_TREE))) { | |
03400136 WF |
2248 | int line; |
2249 | ||
b3a54cba | 2250 | view = view->parent; |
03400136 | 2251 | line = view->lineno; |
8522ecc7 JF |
2252 | move_view(view, request); |
2253 | if (view_is_displayed(view)) | |
f0b3ab80 | 2254 | update_view_title(view); |
328d27f7 JF |
2255 | if (line != view->lineno) |
2256 | view->ops->request(view, REQ_ENTER, | |
2257 | &view->line[view->lineno]); | |
2258 | ||
b3a54cba | 2259 | } else { |
8522ecc7 | 2260 | move_view(view, request); |
b3a54cba | 2261 | } |
328d27f7 | 2262 | break; |
6b161b31 | 2263 | |
03a93dbb JF |
2264 | case REQ_VIEW_NEXT: |
2265 | { | |
9f41488f | 2266 | int nviews = displayed_views(); |
03a93dbb JF |
2267 | int next_view = (current_view + 1) % nviews; |
2268 | ||
2269 | if (next_view == current_view) { | |
2270 | report("Only one view is displayed"); | |
2271 | break; | |
2272 | } | |
2273 | ||
2274 | current_view = next_view; | |
2275 | /* Blur out the title of the previous view. */ | |
2276 | update_view_title(view); | |
6734f6b9 | 2277 | report(""); |
03a93dbb JF |
2278 | break; |
2279 | } | |
24b5b3e0 | 2280 | case REQ_TOGGLE_LINENO: |
b76c2afc | 2281 | opt_line_number = !opt_line_number; |
20bb5e18 | 2282 | redraw_display(); |
b801d8b2 JF |
2283 | break; |
2284 | ||
54efb62b JF |
2285 | case REQ_TOGGLE_REV_GRAPH: |
2286 | opt_rev_graph = !opt_rev_graph; | |
2287 | redraw_display(); | |
2288 | break; | |
2289 | ||
03a93dbb | 2290 | case REQ_PROMPT: |
8855ada4 | 2291 | /* Always reload^Wrerun commands from the prompt. */ |
49f2b43f | 2292 | open_view(view, opt_request, OPEN_RELOAD); |
03a93dbb JF |
2293 | break; |
2294 | ||
4af34daa JF |
2295 | case REQ_SEARCH: |
2296 | case REQ_SEARCH_BACK: | |
c02d8fce | 2297 | search_view(view, request); |
4af34daa JF |
2298 | break; |
2299 | ||
2300 | case REQ_FIND_NEXT: | |
2301 | case REQ_FIND_PREV: | |
2302 | find_next(view, request); | |
2303 | break; | |
2304 | ||
4a2909a7 | 2305 | case REQ_STOP_LOADING: |
59a45d3a JF |
2306 | for (i = 0; i < ARRAY_SIZE(views); i++) { |
2307 | view = &views[i]; | |
2e8488b4 | 2308 | if (view->pipe) |
6a7bb912 | 2309 | report("Stopped loading the %s view", view->name), |
03a93dbb JF |
2310 | end_update(view); |
2311 | } | |
b801d8b2 JF |
2312 | break; |
2313 | ||
4a2909a7 | 2314 | case REQ_SHOW_VERSION: |
ec31d0d0 | 2315 | report("tig-%s (built %s)", TIG_VERSION, __DATE__); |
b801d8b2 JF |
2316 | return TRUE; |
2317 | ||
fac7db6c JF |
2318 | case REQ_SCREEN_RESIZE: |
2319 | resize_display(); | |
2320 | /* Fall-through */ | |
4a2909a7 | 2321 | case REQ_SCREEN_REDRAW: |
20bb5e18 | 2322 | redraw_display(); |
4a2909a7 JF |
2323 | break; |
2324 | ||
0cea0d43 JF |
2325 | case REQ_EDIT: |
2326 | report("Nothing to edit"); | |
531c6c69 JF |
2327 | break; |
2328 | ||
2329 | case REQ_ENTER: | |
2330 | report("Nothing to enter"); | |
2331 | break; | |
ca1d71ea | 2332 | |
1d754561 | 2333 | case REQ_NONE: |
b801d8b2 JF |
2334 | doupdate(); |
2335 | return TRUE; | |
2336 | ||
4f9b667a | 2337 | case REQ_VIEW_CLOSE: |
2fcf5401 JF |
2338 | /* XXX: Mark closed views by letting view->parent point to the |
2339 | * view itself. Parents to closed view should never be | |
2340 | * followed. */ | |
2341 | if (view->parent && | |
2342 | view->parent->parent != view->parent) { | |
4f9b667a JF |
2343 | memset(display, 0, sizeof(display)); |
2344 | current_view = 0; | |
f6da0b66 | 2345 | display[current_view] = view->parent; |
2fcf5401 | 2346 | view->parent = view; |
4f9b667a JF |
2347 | resize_display(); |
2348 | redraw_display(); | |
2349 | break; | |
2350 | } | |
2351 | /* Fall-through */ | |
b801d8b2 JF |
2352 | case REQ_QUIT: |
2353 | return FALSE; | |
2354 | ||
2355 | default: | |
2e8488b4 | 2356 | /* An unknown key will show most commonly used commands. */ |
468876c9 | 2357 | report("Unknown key, press 'h' for help"); |
b801d8b2 JF |
2358 | return TRUE; |
2359 | } | |
2360 | ||
2361 | return TRUE; | |
2362 | } | |
2363 | ||
2364 | ||
2365 | /* | |
ff26aa29 | 2366 | * Pager backend |
b801d8b2 JF |
2367 | */ |
2368 | ||
6b161b31 | 2369 | static bool |
5dcf8064 | 2370 | pager_draw(struct view *view, struct line *line, unsigned int lineno, bool selected) |
b801d8b2 | 2371 | { |
fe7233c3 JF |
2372 | char *text = line->data; |
2373 | enum line_type type = line->type; | |
2374 | int textlen = strlen(text); | |
78c70acd | 2375 | int attr; |
b801d8b2 | 2376 | |
6706b2ba JF |
2377 | wmove(view->win, lineno, 0); |
2378 | ||
5dcf8064 | 2379 | if (selected) { |
78c70acd | 2380 | type = LINE_CURSOR; |
6706b2ba | 2381 | wchgat(view->win, -1, 0, type, NULL); |
fd85fef1 JF |
2382 | } |
2383 | ||
78c70acd | 2384 | attr = get_line_attr(type); |
b801d8b2 | 2385 | wattrset(view->win, attr); |
b76c2afc | 2386 | |
6706b2ba JF |
2387 | if (opt_line_number || opt_tab_size < TABSIZE) { |
2388 | static char spaces[] = " "; | |
2389 | int col_offset = 0, col = 0; | |
2390 | ||
2391 | if (opt_line_number) { | |
2392 | unsigned long real_lineno = view->offset + lineno + 1; | |
82e78006 | 2393 | |
6706b2ba JF |
2394 | if (real_lineno == 1 || |
2395 | (real_lineno % opt_num_interval) == 0) { | |
2396 | wprintw(view->win, "%.*d", view->digits, real_lineno); | |
8855ada4 | 2397 | |
6706b2ba JF |
2398 | } else { |
2399 | waddnstr(view->win, spaces, | |
2400 | MIN(view->digits, STRING_SIZE(spaces))); | |
2401 | } | |
2402 | waddstr(view->win, ": "); | |
2403 | col_offset = view->digits + 2; | |
2404 | } | |
8855ada4 | 2405 | |
fe7233c3 | 2406 | while (text && col_offset + col < view->width) { |
6706b2ba | 2407 | int cols_max = view->width - col_offset - col; |
fe7233c3 | 2408 | char *pos = text; |
6706b2ba | 2409 | int cols; |
4c6fabc2 | 2410 | |
fe7233c3 JF |
2411 | if (*text == '\t') { |
2412 | text++; | |
6706b2ba | 2413 | assert(sizeof(spaces) > TABSIZE); |
fe7233c3 | 2414 | pos = spaces; |
6706b2ba | 2415 | cols = opt_tab_size - (col % opt_tab_size); |
82e78006 | 2416 | |
b76c2afc | 2417 | } else { |
fe7233c3 JF |
2418 | text = strchr(text, '\t'); |
2419 | cols = line ? text - pos : strlen(pos); | |
b76c2afc | 2420 | } |
6706b2ba | 2421 | |
fe7233c3 | 2422 | waddnstr(view->win, pos, MIN(cols, cols_max)); |
6706b2ba | 2423 | col += cols; |
b76c2afc | 2424 | } |
b76c2afc JF |
2425 | |
2426 | } else { | |
6706b2ba | 2427 | int col = 0, pos = 0; |
b801d8b2 | 2428 | |
fe7233c3 JF |
2429 | for (; pos < textlen && col < view->width; pos++, col++) |
2430 | if (text[pos] == '\t') | |
6706b2ba JF |
2431 | col += TABSIZE - (col % TABSIZE) - 1; |
2432 | ||
fe7233c3 | 2433 | waddnstr(view->win, text, pos); |
6706b2ba | 2434 | } |
2e8488b4 | 2435 | |
b801d8b2 JF |
2436 | return TRUE; |
2437 | } | |
2438 | ||
dc23c0e3 | 2439 | static bool |
d65ced0d | 2440 | add_describe_ref(char *buf, size_t *bufpos, char *commit_id, const char *sep) |
dc23c0e3 | 2441 | { |
17482b11 | 2442 | char refbuf[SIZEOF_STR]; |
dc23c0e3 JF |
2443 | char *ref = NULL; |
2444 | FILE *pipe; | |
2445 | ||
d3c345f7 | 2446 | if (!string_format(refbuf, "git describe %s 2>/dev/null", commit_id)) |
dc23c0e3 JF |
2447 | return TRUE; |
2448 | ||
2449 | pipe = popen(refbuf, "r"); | |
2450 | if (!pipe) | |
2451 | return TRUE; | |
2452 | ||
2453 | if ((ref = fgets(refbuf, sizeof(refbuf), pipe))) | |
2454 | ref = chomp_string(ref); | |
2455 | pclose(pipe); | |
2456 | ||
2457 | if (!ref || !*ref) | |
2458 | return TRUE; | |
2459 | ||
2460 | /* This is the only fatal call, since it can "corrupt" the buffer. */ | |
17482b11 | 2461 | if (!string_nformat(buf, SIZEOF_STR, bufpos, "%s%s", sep, ref)) |
dc23c0e3 JF |
2462 | return FALSE; |
2463 | ||
2464 | return TRUE; | |
2465 | } | |
2466 | ||
7b99a34c JF |
2467 | static void |
2468 | add_pager_refs(struct view *view, struct line *line) | |
2469 | { | |
17482b11 | 2470 | char buf[SIZEOF_STR]; |
c9ca1ec3 | 2471 | char *commit_id = line->data + STRING_SIZE("commit "); |
7b99a34c | 2472 | struct ref **refs; |
d65ced0d | 2473 | size_t bufpos = 0, refpos = 0; |
7b99a34c | 2474 | const char *sep = "Refs: "; |
dc23c0e3 | 2475 | bool is_tag = FALSE; |
7b99a34c JF |
2476 | |
2477 | assert(line->type == LINE_COMMIT); | |
2478 | ||
c9ca1ec3 | 2479 | refs = get_refs(commit_id); |
dc23c0e3 JF |
2480 | if (!refs) { |
2481 | if (view == VIEW(REQ_VIEW_DIFF)) | |
2482 | goto try_add_describe_ref; | |
7b99a34c | 2483 | return; |
dc23c0e3 | 2484 | } |
7b99a34c JF |
2485 | |
2486 | do { | |
cc2d1364 | 2487 | struct ref *ref = refs[refpos]; |
e15ec88e JF |
2488 | char *fmt = ref->tag ? "%s[%s]" : |
2489 | ref->remote ? "%s<%s>" : "%s%s"; | |
7b99a34c | 2490 | |
cc2d1364 JF |
2491 | if (!string_format_from(buf, &bufpos, fmt, sep, ref->name)) |
2492 | return; | |
7b99a34c | 2493 | sep = ", "; |
dc23c0e3 JF |
2494 | if (ref->tag) |
2495 | is_tag = TRUE; | |
7b99a34c JF |
2496 | } while (refs[refpos++]->next); |
2497 | ||
dc23c0e3 JF |
2498 | if (!is_tag && view == VIEW(REQ_VIEW_DIFF)) { |
2499 | try_add_describe_ref: | |
d42c8a35 | 2500 | /* Add <tag>-g<commit_id> "fake" reference. */ |
dc23c0e3 JF |
2501 | if (!add_describe_ref(buf, &bufpos, commit_id, sep)) |
2502 | return; | |
2503 | } | |
2504 | ||
d42c8a35 JF |
2505 | if (bufpos == 0) |
2506 | return; | |
2507 | ||
cc2d1364 | 2508 | if (!realloc_lines(view, view->line_size + 1)) |
7b99a34c JF |
2509 | return; |
2510 | ||
0a0d8910 | 2511 | add_line_text(view, buf, LINE_PP_REFS); |
7b99a34c JF |
2512 | } |
2513 | ||
6b161b31 | 2514 | static bool |
701e4f5d | 2515 | pager_read(struct view *view, char *data) |
22f66b0a | 2516 | { |
0a0d8910 | 2517 | struct line *line; |
22f66b0a | 2518 | |
be04d936 JF |
2519 | if (!data) |
2520 | return TRUE; | |
2521 | ||
0a0d8910 JF |
2522 | line = add_line_text(view, data, get_line_type(data)); |
2523 | if (!line) | |
7b99a34c | 2524 | return FALSE; |
fe7233c3 | 2525 | |
7b99a34c JF |
2526 | if (line->type == LINE_COMMIT && |
2527 | (view == VIEW(REQ_VIEW_DIFF) || | |
2528 | view == VIEW(REQ_VIEW_LOG))) | |
2529 | add_pager_refs(view, line); | |
2530 | ||
22f66b0a JF |
2531 | return TRUE; |
2532 | } | |
2533 | ||
586c423d JF |
2534 | static enum request |
2535 | pager_request(struct view *view, enum request request, struct line *line) | |
6b161b31 | 2536 | { |
91e8e277 | 2537 | int split = 0; |
6b161b31 | 2538 | |
586c423d JF |
2539 | if (request != REQ_ENTER) |
2540 | return request; | |
2541 | ||
9fbbd28f JF |
2542 | if (line->type == LINE_COMMIT && |
2543 | (view == VIEW(REQ_VIEW_LOG) || | |
2544 | view == VIEW(REQ_VIEW_PAGER))) { | |
91e8e277 JF |
2545 | open_view(view, REQ_VIEW_DIFF, OPEN_SPLIT); |
2546 | split = 1; | |
67e48ac5 JF |
2547 | } |
2548 | ||
91e8e277 JF |
2549 | /* Always scroll the view even if it was split. That way |
2550 | * you can use Enter to scroll through the log view and | |
2551 | * split open each commit diff. */ | |
2552 | scroll_view(view, REQ_SCROLL_LINE_DOWN); | |
2553 | ||
2554 | /* FIXME: A minor workaround. Scrolling the view will call report("") | |
9d82d824 JF |
2555 | * but if we are scrolling a non-current view this won't properly |
2556 | * update the view title. */ | |
91e8e277 JF |
2557 | if (split) |
2558 | update_view_title(view); | |
6b161b31 | 2559 | |
586c423d | 2560 | return REQ_NONE; |
6b161b31 JF |
2561 | } |
2562 | ||
4af34daa JF |
2563 | static bool |
2564 | pager_grep(struct view *view, struct line *line) | |
2565 | { | |
2566 | regmatch_t pmatch; | |
2567 | char *text = line->data; | |
2568 | ||
2569 | if (!*text) | |
2570 | return FALSE; | |
2571 | ||
b77b2cb8 | 2572 | if (regexec(view->regex, text, 1, &pmatch, 0) == REG_NOMATCH) |
4af34daa JF |
2573 | return FALSE; |
2574 | ||
2575 | return TRUE; | |
2576 | } | |
2577 | ||
d720de4b JF |
2578 | static void |
2579 | pager_select(struct view *view, struct line *line) | |
2580 | { | |
2581 | if (line->type == LINE_COMMIT) { | |
035ba11f | 2582 | char *text = line->data + STRING_SIZE("commit "); |
d720de4b | 2583 | |
035ba11f | 2584 | if (view != VIEW(REQ_VIEW_PAGER)) |
2463b4ea JF |
2585 | string_copy_rev(view->ref, text); |
2586 | string_copy_rev(ref_commit, text); | |
d720de4b JF |
2587 | } |
2588 | } | |
2589 | ||
6b161b31 | 2590 | static struct view_ops pager_ops = { |
6734f6b9 | 2591 | "line", |
f098944b | 2592 | NULL, |
6b161b31 | 2593 | pager_read, |
f098944b | 2594 | pager_draw, |
586c423d | 2595 | pager_request, |
f098944b JF |
2596 | pager_grep, |
2597 | pager_select, | |
2598 | }; | |
2599 | ||
2600 | ||
2601 | /* | |
2602 | * Help backend | |
2603 | */ | |
2604 | ||
2605 | static bool | |
2606 | help_open(struct view *view) | |
2607 | { | |
2608 | char buf[BUFSIZ]; | |
2609 | int lines = ARRAY_SIZE(req_info) + 2; | |
2610 | int i; | |
2611 | ||
2612 | if (view->lines > 0) | |
2613 | return TRUE; | |
2614 | ||
2615 | for (i = 0; i < ARRAY_SIZE(req_info); i++) | |
2616 | if (!req_info[i].request) | |
2617 | lines++; | |
2618 | ||
2619 | view->line = calloc(lines, sizeof(*view->line)); | |
2620 | if (!view->line) | |
2621 | return FALSE; | |
2622 | ||
2623 | add_line_text(view, "Quick reference for tig keybindings:", LINE_DEFAULT); | |
2624 | ||
2625 | for (i = 0; i < ARRAY_SIZE(req_info); i++) { | |
2626 | char *key; | |
2627 | ||
2628 | if (!req_info[i].request) { | |
2629 | add_line_text(view, "", LINE_DEFAULT); | |
2630 | add_line_text(view, req_info[i].help, LINE_DEFAULT); | |
2631 | continue; | |
2632 | } | |
2633 | ||
2634 | key = get_key(req_info[i].request); | |
2635 | if (!string_format(buf, " %-25s %s", key, req_info[i].help)) | |
2636 | continue; | |
2637 | ||
2638 | add_line_text(view, buf, LINE_DEFAULT); | |
2639 | } | |
2640 | ||
2641 | return TRUE; | |
2642 | } | |
2643 | ||
2644 | static struct view_ops help_ops = { | |
2645 | "line", | |
2646 | help_open, | |
2647 | NULL, | |
2648 | pager_draw, | |
586c423d | 2649 | pager_request, |
4af34daa | 2650 | pager_grep, |
d720de4b | 2651 | pager_select, |
6b161b31 JF |
2652 | }; |
2653 | ||
80ce96ea | 2654 | |
ff26aa29 | 2655 | /* |
e733ee54 JF |
2656 | * Tree backend |
2657 | */ | |
2658 | ||
69efc854 JF |
2659 | struct tree_stack_entry { |
2660 | struct tree_stack_entry *prev; /* Entry below this in the stack */ | |
2661 | unsigned long lineno; /* Line number to restore */ | |
2662 | char *name; /* Position of name in opt_path */ | |
2663 | }; | |
2664 | ||
2665 | /* The top of the path stack. */ | |
2666 | static struct tree_stack_entry *tree_stack = NULL; | |
2667 | unsigned long tree_lineno = 0; | |
2668 | ||
2669 | static void | |
2670 | pop_tree_stack_entry(void) | |
2671 | { | |
2672 | struct tree_stack_entry *entry = tree_stack; | |
2673 | ||
2674 | tree_lineno = entry->lineno; | |
2675 | entry->name[0] = 0; | |
2676 | tree_stack = entry->prev; | |
2677 | free(entry); | |
2678 | } | |
2679 | ||
2680 | static void | |
2681 | push_tree_stack_entry(char *name, unsigned long lineno) | |
2682 | { | |
2683 | struct tree_stack_entry *entry = calloc(1, sizeof(*entry)); | |
2684 | size_t pathlen = strlen(opt_path); | |
2685 | ||
2686 | if (!entry) | |
2687 | return; | |
2688 | ||
2689 | entry->prev = tree_stack; | |
2690 | entry->name = opt_path + pathlen; | |
2691 | tree_stack = entry; | |
2692 | ||
2693 | if (!string_format_from(opt_path, &pathlen, "%s/", name)) { | |
2694 | pop_tree_stack_entry(); | |
2695 | return; | |
2696 | } | |
2697 | ||
2698 | /* Move the current line to the first tree entry. */ | |
2699 | tree_lineno = 1; | |
2700 | entry->lineno = lineno; | |
2701 | } | |
2702 | ||
4795d620 | 2703 | /* Parse output from git-ls-tree(1): |
e733ee54 JF |
2704 | * |
2705 | * 100644 blob fb0e31ea6cc679b7379631188190e975f5789c26 Makefile | |
2706 | * 100644 blob 5304ca4260aaddaee6498f9630e7d471b8591ea6 README | |
2707 | * 100644 blob f931e1d229c3e185caad4449bf5b66ed72462657 tig.c | |
2708 | * 100644 blob ed09fe897f3c7c9af90bcf80cae92558ea88ae38 web.conf | |
2709 | */ | |
2710 | ||
2711 | #define SIZEOF_TREE_ATTR \ | |
2712 | STRING_SIZE("100644 blob ed09fe897f3c7c9af90bcf80cae92558ea88ae38\t") | |
2713 | ||
2714 | #define TREE_UP_FORMAT "040000 tree %s\t.." | |
2715 | ||
2716 | static int | |
2717 | tree_compare_entry(enum line_type type1, char *name1, | |
2718 | enum line_type type2, char *name2) | |
2719 | { | |
2720 | if (type1 != type2) { | |
2721 | if (type1 == LINE_TREE_DIR) | |
2722 | return -1; | |
2723 | return 1; | |
2724 | } | |
2725 | ||
2726 | return strcmp(name1, name2); | |
2727 | } | |
2728 | ||
2729 | static bool | |
2730 | tree_read(struct view *view, char *text) | |
2731 | { | |
be04d936 | 2732 | size_t textlen = text ? strlen(text) : 0; |
e733ee54 JF |
2733 | char buf[SIZEOF_STR]; |
2734 | unsigned long pos; | |
2735 | enum line_type type; | |
f88a5319 | 2736 | bool first_read = view->lines == 0; |
e733ee54 JF |
2737 | |
2738 | if (textlen <= SIZEOF_TREE_ATTR) | |
2739 | return FALSE; | |
2740 | ||
2741 | type = text[STRING_SIZE("100644 ")] == 't' | |
2742 | ? LINE_TREE_DIR : LINE_TREE_FILE; | |
2743 | ||
f88a5319 | 2744 | if (first_read) { |
e733ee54 | 2745 | /* Add path info line */ |
0a0d8910 JF |
2746 | if (!string_format(buf, "Directory path /%s", opt_path) || |
2747 | !realloc_lines(view, view->line_size + 1) || | |
2748 | !add_line_text(view, buf, LINE_DEFAULT)) | |
e733ee54 JF |
2749 | return FALSE; |
2750 | ||
2751 | /* Insert "link" to parent directory. */ | |
0a0d8910 JF |
2752 | if (*opt_path) { |
2753 | if (!string_format(buf, TREE_UP_FORMAT, view->ref) || | |
2754 | !realloc_lines(view, view->line_size + 1) || | |
2755 | !add_line_text(view, buf, LINE_TREE_DIR)) | |
2756 | return FALSE; | |
2757 | } | |
e733ee54 JF |
2758 | } |
2759 | ||
2760 | /* Strip the path part ... */ | |
2761 | if (*opt_path) { | |
2762 | size_t pathlen = textlen - SIZEOF_TREE_ATTR; | |
2763 | size_t striplen = strlen(opt_path); | |
2764 | char *path = text + SIZEOF_TREE_ATTR; | |
2765 | ||
2766 | if (pathlen > striplen) | |
2767 | memmove(path, path + striplen, | |
2768 | pathlen - striplen + 1); | |
2769 | } | |
2770 | ||
2771 | /* Skip "Directory ..." and ".." line. */ | |
2772 | for (pos = 1 + !!*opt_path; pos < view->lines; pos++) { | |
2773 | struct line *line = &view->line[pos]; | |
2774 | char *path1 = ((char *) line->data) + SIZEOF_TREE_ATTR; | |
2775 | char *path2 = text + SIZEOF_TREE_ATTR; | |
2776 | int cmp = tree_compare_entry(line->type, path1, type, path2); | |
2777 | ||
2778 | if (cmp <= 0) | |
2779 | continue; | |
2780 | ||
2781 | text = strdup(text); | |
2782 | if (!text) | |
2783 | return FALSE; | |
2784 | ||
2785 | if (view->lines > pos) | |
2786 | memmove(&view->line[pos + 1], &view->line[pos], | |
2787 | (view->lines - pos) * sizeof(*line)); | |
2788 | ||
2789 | line = &view->line[pos]; | |
2790 | line->data = text; | |
2791 | line->type = type; | |
2792 | view->lines++; | |
2793 | return TRUE; | |
2794 | } | |
2795 | ||
0a0d8910 | 2796 | if (!add_line_text(view, text, type)) |
e733ee54 JF |
2797 | return FALSE; |
2798 | ||
69efc854 JF |
2799 | if (tree_lineno > view->lineno) { |
2800 | view->lineno = tree_lineno; | |
2801 | tree_lineno = 0; | |
2802 | } | |
f88a5319 | 2803 | |
e733ee54 JF |
2804 | return TRUE; |
2805 | } | |
2806 | ||
586c423d JF |
2807 | static enum request |
2808 | tree_request(struct view *view, enum request request, struct line *line) | |
e733ee54 | 2809 | { |
aac64c17 | 2810 | enum open_flags flags; |
586c423d JF |
2811 | |
2812 | if (request != REQ_ENTER) | |
2813 | return request; | |
e733ee54 | 2814 | |
69efc854 JF |
2815 | /* Cleanup the stack if the tree view is at a different tree. */ |
2816 | while (!*opt_path && tree_stack) | |
2817 | pop_tree_stack_entry(); | |
2818 | ||
e733ee54 JF |
2819 | switch (line->type) { |
2820 | case LINE_TREE_DIR: | |
2821 | /* Depending on whether it is a subdir or parent (updir?) link | |
2822 | * mangle the path buffer. */ | |
2823 | if (line == &view->line[1] && *opt_path) { | |
69efc854 | 2824 | pop_tree_stack_entry(); |
e733ee54 JF |
2825 | |
2826 | } else { | |
4795d620 | 2827 | char *data = line->data; |
e733ee54 JF |
2828 | char *basename = data + SIZEOF_TREE_ATTR; |
2829 | ||
69efc854 | 2830 | push_tree_stack_entry(basename, view->lineno); |
e733ee54 JF |
2831 | } |
2832 | ||
2833 | /* Trees and subtrees share the same ID, so they are not not | |
2834 | * unique like blobs. */ | |
aac64c17 | 2835 | flags = OPEN_RELOAD; |
e733ee54 JF |
2836 | request = REQ_VIEW_TREE; |
2837 | break; | |
2838 | ||
2839 | case LINE_TREE_FILE: | |
aac64c17 | 2840 | flags = display[0] == view ? OPEN_SPLIT : OPEN_DEFAULT; |
e733ee54 JF |
2841 | request = REQ_VIEW_BLOB; |
2842 | break; | |
2843 | ||
2844 | default: | |
2845 | return TRUE; | |
2846 | } | |
2847 | ||
2848 | open_view(view, request, flags); | |
69efc854 JF |
2849 | if (request == REQ_VIEW_TREE) { |
2850 | view->lineno = tree_lineno; | |
2851 | } | |
e733ee54 | 2852 | |
586c423d | 2853 | return REQ_NONE; |
e733ee54 JF |
2854 | } |
2855 | ||
d720de4b JF |
2856 | static void |
2857 | tree_select(struct view *view, struct line *line) | |
2858 | { | |
035ba11f | 2859 | char *text = line->data + STRING_SIZE("100644 blob "); |
73c76ef5 JF |
2860 | |
2861 | if (line->type == LINE_TREE_FILE) { | |
2463b4ea | 2862 | string_copy_rev(ref_blob, text); |
d720de4b | 2863 | |
ebbaf4fe JF |
2864 | } else if (line->type != LINE_TREE_DIR) { |
2865 | return; | |
d720de4b | 2866 | } |
ebbaf4fe | 2867 | |
2463b4ea | 2868 | string_copy_rev(view->ref, text); |
d720de4b JF |
2869 | } |
2870 | ||
e733ee54 JF |
2871 | static struct view_ops tree_ops = { |
2872 | "file", | |
f098944b | 2873 | NULL, |
e733ee54 | 2874 | tree_read, |
f098944b | 2875 | pager_draw, |
586c423d | 2876 | tree_request, |
e733ee54 | 2877 | pager_grep, |
d720de4b | 2878 | tree_select, |
e733ee54 JF |
2879 | }; |
2880 | ||
2881 | static bool | |
2882 | blob_read(struct view *view, char *line) | |
2883 | { | |
c115e7ac | 2884 | return add_line_text(view, line, LINE_DEFAULT) != NULL; |
e733ee54 JF |
2885 | } |
2886 | ||
2887 | static struct view_ops blob_ops = { | |
2888 | "line", | |
f098944b | 2889 | NULL, |
e733ee54 | 2890 | blob_read, |
f098944b | 2891 | pager_draw, |
586c423d | 2892 | pager_request, |
e733ee54 | 2893 | pager_grep, |
d720de4b | 2894 | pager_select, |
e733ee54 JF |
2895 | }; |
2896 | ||
2897 | ||
2898 | /* | |
173d76ea JF |
2899 | * Status backend |
2900 | */ | |
2901 | ||
2902 | struct status { | |
2903 | char status; | |
2904 | struct { | |
2905 | mode_t mode; | |
2906 | char rev[SIZEOF_REV]; | |
2907 | } old; | |
2908 | struct { | |
2909 | mode_t mode; | |
2910 | char rev[SIZEOF_REV]; | |
2911 | } new; | |
2912 | char name[SIZEOF_STR]; | |
2913 | }; | |
2914 | ||
b33611d8 JF |
2915 | static struct status stage_status; |
2916 | static enum line_type stage_line_type; | |
2917 | ||
173d76ea JF |
2918 | /* Get fields from the diff line: |
2919 | * :100644 100644 06a5d6ae9eca55be2e0e585a152e6b1336f2b20e 0000000000000000000000000000000000000000 M | |
2920 | */ | |
2921 | static inline bool | |
2922 | status_get_diff(struct status *file, char *buf, size_t bufsize) | |
2923 | { | |
2924 | char *old_mode = buf + 1; | |
2925 | char *new_mode = buf + 8; | |
2926 | char *old_rev = buf + 15; | |
2927 | char *new_rev = buf + 56; | |
2928 | char *status = buf + 97; | |
2929 | ||
2930 | if (bufsize != 99 || | |
2931 | old_mode[-1] != ':' || | |
2932 | new_mode[-1] != ' ' || | |
2933 | old_rev[-1] != ' ' || | |
2934 | new_rev[-1] != ' ' || | |
2935 | status[-1] != ' ') | |
2936 | return FALSE; | |
2937 | ||
2938 | file->status = *status; | |
2939 | ||
2940 | string_copy_rev(file->old.rev, old_rev); | |
2941 | string_copy_rev(file->new.rev, new_rev); | |
2942 | ||
2943 | file->old.mode = strtoul(old_mode, NULL, 8); | |
2944 | file->new.mode = strtoul(new_mode, NULL, 8); | |
2945 | ||
2946 | file->name[0] = 0; | |
2947 | ||
2948 | return TRUE; | |
2949 | } | |
2950 | ||
2951 | static bool | |
2952 | status_run(struct view *view, const char cmd[], bool diff, enum line_type type) | |
2953 | { | |
2954 | struct status *file = NULL; | |
2955 | char buf[SIZEOF_STR * 4]; | |
2956 | size_t bufsize = 0; | |
2957 | FILE *pipe; | |
2958 | ||
2959 | pipe = popen(cmd, "r"); | |
2960 | if (!pipe) | |
2961 | return FALSE; | |
2962 | ||
2963 | add_line_data(view, NULL, type); | |
2964 | ||
2965 | while (!feof(pipe) && !ferror(pipe)) { | |
2966 | char *sep; | |
2967 | size_t readsize; | |
2968 | ||
2969 | readsize = fread(buf + bufsize, 1, sizeof(buf) - bufsize, pipe); | |
2970 | if (!readsize) | |
2971 | break; | |
2972 | bufsize += readsize; | |
2973 | ||
2974 | /* Process while we have NUL chars. */ | |
2975 | while ((sep = memchr(buf, 0, bufsize))) { | |
2976 | size_t sepsize = sep - buf + 1; | |
2977 | ||
2978 | if (!file) { | |
2979 | if (!realloc_lines(view, view->line_size + 1)) | |
2980 | goto error_out; | |
2981 | ||
2982 | file = calloc(1, sizeof(*file)); | |
2983 | if (!file) | |
2984 | goto error_out; | |
2985 | ||
2986 | add_line_data(view, file, type); | |
2987 | } | |
2988 | ||
2989 | /* Parse diff info part. */ | |
2990 | if (!diff) { | |
2991 | file->status = '?'; | |
2992 | ||
2993 | } else if (!file->status) { | |
2994 | if (!status_get_diff(file, buf, sepsize)) | |
2995 | goto error_out; | |
2996 | ||
2997 | bufsize -= sepsize; | |
2998 | memmove(buf, sep + 1, bufsize); | |
2999 | ||
3000 | sep = memchr(buf, 0, bufsize); | |
3001 | if (!sep) | |
3002 | break; | |
3003 | sepsize = sep - buf + 1; | |
3004 | } | |
3005 | ||
3006 | /* git-ls-files just delivers a NUL separated | |
3007 | * list of file names similar to the second half | |
3008 | * of the git-diff-* output. */ | |
3009 | string_ncopy(file->name, buf, sepsize); | |
3010 | bufsize -= sepsize; | |
3011 | memmove(buf, sep + 1, bufsize); | |
3012 | file = NULL; | |
3013 | } | |
3014 | } | |
3015 | ||
3016 | if (ferror(pipe)) { | |
3017 | error_out: | |
3018 | pclose(pipe); | |
3019 | return FALSE; | |
3020 | } | |
3021 | ||
3022 | if (!view->line[view->lines - 1].data) | |
3023 | add_line_data(view, NULL, LINE_STAT_NONE); | |
3024 | ||
3025 | pclose(pipe); | |
3026 | return TRUE; | |
3027 | } | |
3028 | ||
3029 | #define STATUS_DIFF_INDEX_CMD "git diff-index -z --cached HEAD" | |
3030 | #define STATUS_DIFF_FILES_CMD "git diff-files -z" | |
3031 | #define STATUS_LIST_OTHER_CMD \ | |
810f0078 | 3032 | "git ls-files -z --others --exclude-per-directory=.gitignore" |
173d76ea | 3033 | |
89d917a2 JF |
3034 | #define STATUS_DIFF_SHOW_CMD \ |
3035 | "git diff --root --patch-with-stat --find-copies-harder -B -C %s -- %s 2>/dev/null" | |
3036 | ||
173d76ea JF |
3037 | /* First parse staged info using git-diff-index(1), then parse unstaged |
3038 | * info using git-diff-files(1), and finally untracked files using | |
3039 | * git-ls-files(1). */ | |
3040 | static bool | |
3041 | status_open(struct view *view) | |
3042 | { | |
810f0078 JF |
3043 | struct stat statbuf; |
3044 | char exclude[SIZEOF_STR]; | |
3045 | char cmd[SIZEOF_STR]; | |
173d76ea JF |
3046 | size_t i; |
3047 | ||
3048 | for (i = 0; i < view->lines; i++) | |
3049 | free(view->line[i].data); | |
3050 | free(view->line); | |
3051 | view->lines = view->line_size = 0; | |
3052 | view->line = NULL; | |
3053 | ||
3054 | if (!realloc_lines(view, view->line_size + 6)) | |
3055 | return FALSE; | |
3056 | ||
810f0078 JF |
3057 | if (!string_format(exclude, "%s/info/exclude", opt_git_dir)) |
3058 | return FALSE; | |
3059 | ||
3060 | string_copy(cmd, STATUS_LIST_OTHER_CMD); | |
3061 | ||
3062 | if (stat(exclude, &statbuf) >= 0) { | |
3063 | size_t cmdsize = strlen(cmd); | |
3064 | ||
3065 | if (!string_format_from(cmd, &cmdsize, " %s", "--exclude-from=") || | |
3066 | sq_quote(cmd, cmdsize, exclude) >= sizeof(cmd)) | |
3067 | return FALSE; | |
3068 | } | |
3069 | ||
173d76ea JF |
3070 | if (!status_run(view, STATUS_DIFF_INDEX_CMD, TRUE, LINE_STAT_STAGED) || |
3071 | !status_run(view, STATUS_DIFF_FILES_CMD, TRUE, LINE_STAT_UNSTAGED) || | |
810f0078 | 3072 | !status_run(view, cmd, FALSE, LINE_STAT_UNTRACKED)) |
173d76ea JF |
3073 | return FALSE; |
3074 | ||
3075 | return TRUE; | |
3076 | } | |
3077 | ||
3078 | static bool | |
3079 | status_draw(struct view *view, struct line *line, unsigned int lineno, bool selected) | |
3080 | { | |
3081 | struct status *status = line->data; | |
3082 | ||
3083 | wmove(view->win, lineno, 0); | |
3084 | ||
3085 | if (selected) { | |
3086 | wattrset(view->win, get_line_attr(LINE_CURSOR)); | |
3087 | wchgat(view->win, -1, 0, LINE_CURSOR, NULL); | |
3088 | ||
3089 | } else if (!status && line->type != LINE_STAT_NONE) { | |
3090 | wattrset(view->win, get_line_attr(LINE_STAT_SECTION)); | |
3091 | wchgat(view->win, -1, 0, LINE_STAT_SECTION, NULL); | |
3092 | ||
3093 | } else { | |
3094 | wattrset(view->win, get_line_attr(line->type)); | |
3095 | } | |
3096 | ||
3097 | if (!status) { | |
3098 | char *text; | |
3099 | ||
3100 | switch (line->type) { | |
3101 | case LINE_STAT_STAGED: | |
3102 | text = "Changes to be committed:"; | |
3103 | break; | |
3104 | ||
3105 | case LINE_STAT_UNSTAGED: | |
3106 | text = "Changed but not updated:"; | |
3107 | break; | |
3108 | ||
3109 | case LINE_STAT_UNTRACKED: | |
3110 | text = "Untracked files:"; | |
3111 | break; | |
3112 | ||
3113 | case LINE_STAT_NONE: | |
3114 | text = " (no files)"; | |
3115 | break; | |
3116 | ||
3117 | default: | |
3118 | return FALSE; | |
3119 | } | |
3120 | ||
3121 | waddstr(view->win, text); | |
3122 | return TRUE; | |
3123 | } | |
3124 | ||
3125 | waddch(view->win, status->status); | |
3126 | if (!selected) | |
3127 | wattrset(view->win, A_NORMAL); | |
3128 | wmove(view->win, lineno, 4); | |
3129 | waddstr(view->win, status->name); | |
3130 | ||
3131 | return TRUE; | |
3132 | } | |
3133 | ||
586c423d | 3134 | static enum request |
88f66e2d | 3135 | status_enter(struct view *view, struct line *line) |
173d76ea | 3136 | { |
89d917a2 | 3137 | struct status *status = line->data; |
4e8159cf | 3138 | char path[SIZEOF_STR] = ""; |
89d917a2 JF |
3139 | char *info; |
3140 | size_t cmdsize = 0; | |
3141 | ||
4e8159cf JF |
3142 | if (line->type == LINE_STAT_NONE || |
3143 | (!status && line[1].type == LINE_STAT_NONE)) { | |
3144 | report("No file to diff"); | |
586c423d | 3145 | return REQ_NONE; |
89d917a2 JF |
3146 | } |
3147 | ||
4e8159cf | 3148 | if (status && sq_quote(path, 0, status->name) >= sizeof(path)) |
586c423d | 3149 | return REQ_QUIT; |
89d917a2 JF |
3150 | |
3151 | if (opt_cdup[0] && | |
3152 | line->type != LINE_STAT_UNTRACKED && | |
3153 | !string_format_from(opt_cmd, &cmdsize, "cd %s;", opt_cdup)) | |
586c423d | 3154 | return REQ_QUIT; |
89d917a2 JF |
3155 | |
3156 | switch (line->type) { | |
3157 | case LINE_STAT_STAGED: | |
3158 | if (!string_format_from(opt_cmd, &cmdsize, STATUS_DIFF_SHOW_CMD, | |
3159 | "--cached", path)) | |
586c423d | 3160 | return REQ_QUIT; |
4e8159cf JF |
3161 | if (status) |
3162 | info = "Staged changes to %s"; | |
3163 | else | |
3164 | info = "Staged changes"; | |
89d917a2 JF |
3165 | break; |
3166 | ||
3167 | case LINE_STAT_UNSTAGED: | |
3168 | if (!string_format_from(opt_cmd, &cmdsize, STATUS_DIFF_SHOW_CMD, | |
3169 | "", path)) | |
586c423d | 3170 | return REQ_QUIT; |
4e8159cf JF |
3171 | if (status) |
3172 | info = "Unstaged changes to %s"; | |
3173 | else | |
3174 | info = "Unstaged changes"; | |
89d917a2 JF |
3175 | break; |
3176 | ||
3177 | case LINE_STAT_UNTRACKED: | |
3178 | if (opt_pipe) | |
586c423d JF |
3179 | return REQ_QUIT; |
3180 | ||
4e8159cf JF |
3181 | |
3182 | if (!status) { | |
3183 | report("No file to show"); | |
586c423d | 3184 | return REQ_NONE; |
4e8159cf JF |
3185 | } |
3186 | ||
89d917a2 JF |
3187 | opt_pipe = fopen(status->name, "r"); |
3188 | info = "Untracked file %s"; | |
3189 | break; | |
3190 | ||
3191 | default: | |
3192 | die("w00t"); | |
3193 | } | |
3194 | ||
3e634113 JF |
3195 | open_view(view, REQ_VIEW_STAGE, OPEN_RELOAD | OPEN_SPLIT); |
3196 | if (view_is_displayed(VIEW(REQ_VIEW_STAGE))) { | |
b33611d8 JF |
3197 | if (status) { |
3198 | stage_status = *status; | |
3199 | } else { | |
3200 | memset(&stage_status, 0, sizeof(stage_status)); | |
3201 | } | |
3202 | ||
3203 | stage_line_type = line->type; | |
3204 | string_format(VIEW(REQ_VIEW_STAGE)->ref, info, stage_status.name); | |
89d917a2 JF |
3205 | } |
3206 | ||
586c423d | 3207 | return REQ_NONE; |
ca1d71ea JF |
3208 | } |
3209 | ||
88f66e2d | 3210 | |
ca1d71ea JF |
3211 | static bool |
3212 | status_update_file(struct view *view, struct status *status, enum line_type type) | |
3213 | { | |
91c5d983 | 3214 | char cmd[SIZEOF_STR]; |
173d76ea | 3215 | char buf[SIZEOF_STR]; |
91c5d983 | 3216 | size_t cmdsize = 0; |
173d76ea JF |
3217 | size_t bufsize = 0; |
3218 | size_t written = 0; | |
3219 | FILE *pipe; | |
3220 | ||
91c5d983 | 3221 | if (opt_cdup[0] && |
ca1d71ea | 3222 | type != LINE_STAT_UNTRACKED && |
91c5d983 JF |
3223 | !string_format_from(cmd, &cmdsize, "cd %s;", opt_cdup)) |
3224 | return FALSE; | |
3225 | ||
ca1d71ea | 3226 | switch (type) { |
173d76ea JF |
3227 | case LINE_STAT_STAGED: |
3228 | if (!string_format_from(buf, &bufsize, "%06o %s\t%s%c", | |
3229 | status->old.mode, | |
3230 | status->old.rev, | |
3231 | status->name, 0)) | |
3232 | return FALSE; | |
91c5d983 JF |
3233 | |
3234 | string_add(cmd, cmdsize, "git update-index -z --index-info"); | |
173d76ea JF |
3235 | break; |
3236 | ||
3237 | case LINE_STAT_UNSTAGED: | |
3238 | case LINE_STAT_UNTRACKED: | |
3239 | if (!string_format_from(buf, &bufsize, "%s%c", status->name, 0)) | |
3240 | return FALSE; | |
91c5d983 JF |
3241 | |
3242 | string_add(cmd, cmdsize, "git update-index -z --add --remove --stdin"); | |
173d76ea JF |
3243 | break; |
3244 | ||
3245 | default: | |
3246 | die("w00t"); | |
3247 | } | |
3248 | ||
3249 | pipe = popen(cmd, "w"); | |
3250 | if (!pipe) | |
3251 | return FALSE; | |
3252 | ||
3253 | while (!ferror(pipe) && written < bufsize) { | |
3254 | written += fwrite(buf + written, 1, bufsize - written, pipe); | |
3255 | } | |
3256 | ||
3257 | pclose(pipe); | |
3258 | ||
3259 | if (written != bufsize) | |
3260 | return FALSE; | |
3261 | ||
173d76ea JF |
3262 | return TRUE; |
3263 | } | |
3264 | ||
3265 | static void | |
ca1d71ea JF |
3266 | status_update(struct view *view) |
3267 | { | |
dec7b437 | 3268 | struct line *line = &view->line[view->lineno]; |
351917f8 | 3269 | |
dec7b437 | 3270 | assert(view->lines); |
11359638 | 3271 | |
dec7b437 JF |
3272 | if (!line->data) { |
3273 | while (++line < view->line + view->lines && line->data) { | |
3274 | if (!status_update_file(view, line->data, line->type)) | |
3275 | report("Failed to update file status"); | |
3276 | } | |
11359638 | 3277 | |
dec7b437 JF |
3278 | if (!line[-1].data) { |
3279 | report("Nothing to update"); | |
3280 | return; | |
93e4c4f6 JF |
3281 | } |
3282 | ||
dec7b437 JF |
3283 | } else if (!status_update_file(view, line->data, line->type)) { |
3284 | report("Failed to update file status"); | |
ca1d71ea | 3285 | } |
dec7b437 JF |
3286 | |
3287 | open_view(view, REQ_VIEW_STATUS, OPEN_RELOAD); | |
ca1d71ea JF |
3288 | } |
3289 | ||
88f66e2d JF |
3290 | static enum request |
3291 | status_request(struct view *view, enum request request, struct line *line) | |
3292 | { | |
0cea0d43 JF |
3293 | struct status *status = line->data; |
3294 | ||
88f66e2d JF |
3295 | switch (request) { |
3296 | case REQ_STATUS_UPDATE: | |
3297 | status_update(view); | |
3298 | break; | |
3299 | ||
0cea0d43 JF |
3300 | case REQ_EDIT: |
3301 | if (!status) | |
3302 | return request; | |
3303 | ||
3304 | open_editor(view, status->name); | |
3305 | break; | |
3306 | ||
88f66e2d JF |
3307 | case REQ_ENTER: |
3308 | status_enter(view, line); | |
3309 | break; | |
3310 | ||
3311 | default: | |
3312 | return request; | |
3313 | } | |
3314 | ||
3315 | return REQ_NONE; | |
3316 | } | |
3317 | ||
ca1d71ea | 3318 | static void |
173d76ea JF |
3319 | status_select(struct view *view, struct line *line) |
3320 | { | |
11359638 JF |
3321 | struct status *status = line->data; |
3322 | char file[SIZEOF_STR] = "all files"; | |
173d76ea JF |
3323 | char *text; |
3324 | ||
11359638 JF |
3325 | if (status && !string_format(file, "'%s'", status->name)) |
3326 | return; | |
3327 | ||
3328 | if (!status && line[1].type == LINE_STAT_NONE) | |
3329 | line++; | |
3330 | ||
173d76ea JF |
3331 | switch (line->type) { |
3332 | case LINE_STAT_STAGED: | |
11359638 | 3333 | text = "Press %s to unstage %s for commit"; |
173d76ea JF |
3334 | break; |
3335 | ||
3336 | case LINE_STAT_UNSTAGED: | |
11359638 | 3337 | text = "Press %s to stage %s for commit"; |
173d76ea JF |
3338 | break; |
3339 | ||
3340 | case LINE_STAT_UNTRACKED: | |
11359638 | 3341 | text = "Press %s to stage %s for addition"; |
173d76ea JF |
3342 | break; |
3343 | ||
3344 | case LINE_STAT_NONE: | |
11359638 JF |
3345 | text = "Nothing to update"; |
3346 | break; | |
173d76ea JF |
3347 | |
3348 | default: | |
3349 | die("w00t"); | |
3350 | } | |
3351 | ||
11359638 | 3352 | string_format(view->ref, text, get_key(REQ_STATUS_UPDATE), file); |
173d76ea JF |
3353 | } |
3354 | ||
3355 | static bool | |
3356 | status_grep(struct view *view, struct line *line) | |
3357 | { | |
3358 | struct status *status = line->data; | |
3359 | enum { S_STATUS, S_NAME, S_END } state; | |
3360 | char buf[2] = "?"; | |
3361 | regmatch_t pmatch; | |
3362 | ||
3363 | if (!status) | |
3364 | return FALSE; | |
3365 | ||
3366 | for (state = S_STATUS; state < S_END; state++) { | |
3367 | char *text; | |
3368 | ||
3369 | switch (state) { | |
3370 | case S_NAME: text = status->name; break; | |
3371 | case S_STATUS: | |
3372 | buf[0] = status->status; | |
3373 | text = buf; | |
3374 | break; | |
3375 | ||
3376 | default: | |
3377 | return FALSE; | |
3378 | } | |
3379 | ||
3380 | if (regexec(view->regex, text, 1, &pmatch, 0) != REG_NOMATCH) | |
3381 | return TRUE; | |
3382 | } | |
3383 | ||
3384 | return FALSE; | |
3385 | } | |
3386 | ||
3387 | static struct view_ops status_ops = { | |
3388 | "file", | |
3389 | status_open, | |
3390 | NULL, | |
3391 | status_draw, | |
586c423d | 3392 | status_request, |
173d76ea JF |
3393 | status_grep, |
3394 | status_select, | |
3395 | }; | |
3396 | ||
b33611d8 JF |
3397 | |
3398 | static bool | |
3399 | stage_diff_line(FILE *pipe, struct line *line) | |
3400 | { | |
3401 | char *buf = line->data; | |
3402 | size_t bufsize = strlen(buf); | |
3403 | size_t written = 0; | |
3404 | ||
3405 | while (!ferror(pipe) && written < bufsize) { | |
3406 | written += fwrite(buf + written, 1, bufsize - written, pipe); | |
3407 | } | |
3408 | ||
3409 | fputc('\n', pipe); | |
3410 | ||
3411 | return written == bufsize; | |
3412 | } | |
3413 | ||
3414 | static struct line * | |
3415 | stage_diff_hdr(struct view *view, struct line *line) | |
3416 | { | |
3417 | int diff_hdr_dir = line->type == LINE_DIFF_CHUNK ? -1 : 1; | |
3418 | struct line *diff_hdr; | |
3419 | ||
3420 | if (line->type == LINE_DIFF_CHUNK) | |
3421 | diff_hdr = line - 1; | |
3422 | else | |
3423 | diff_hdr = view->line + 1; | |
3424 | ||
3425 | while (diff_hdr > view->line && diff_hdr < view->line + view->lines) { | |
3426 | if (diff_hdr->type == LINE_DIFF_HEADER) | |
3427 | return diff_hdr; | |
3428 | ||
3429 | diff_hdr += diff_hdr_dir; | |
3430 | } | |
3431 | ||
3432 | return NULL; | |
3433 | } | |
3434 | ||
3435 | static bool | |
3436 | stage_update_chunk(struct view *view, struct line *line) | |
3437 | { | |
3438 | char cmd[SIZEOF_STR]; | |
3439 | size_t cmdsize = 0; | |
3440 | struct line *diff_hdr, *diff_chunk, *diff_end; | |
3441 | FILE *pipe; | |
3442 | ||
3443 | diff_hdr = stage_diff_hdr(view, line); | |
3444 | if (!diff_hdr) | |
3445 | return FALSE; | |
3446 | ||
3447 | if (opt_cdup[0] && | |
3448 | !string_format_from(cmd, &cmdsize, "cd %s;", opt_cdup)) | |
3449 | return FALSE; | |
3450 | ||
3451 | if (!string_format_from(cmd, &cmdsize, | |
3452 | "git apply --cached %s - && " | |
3453 | "git update-index -q --unmerged --refresh 2>/dev/null", | |
3454 | stage_line_type == LINE_STAT_STAGED ? "-R" : "")) | |
3455 | return FALSE; | |
3456 | ||
3457 | pipe = popen(cmd, "w"); | |
3458 | if (!pipe) | |
3459 | return FALSE; | |
3460 | ||
3461 | diff_end = view->line + view->lines; | |
3462 | if (line->type != LINE_DIFF_CHUNK) { | |
3463 | diff_chunk = diff_hdr; | |
3464 | ||
3465 | } else { | |
3466 | for (diff_chunk = line + 1; diff_chunk < diff_end; diff_chunk++) | |
3467 | if (diff_chunk->type == LINE_DIFF_CHUNK || | |
3468 | diff_chunk->type == LINE_DIFF_HEADER) | |
3469 | diff_end = diff_chunk; | |
3470 | ||
3471 | diff_chunk = line; | |
3472 | ||
3473 | while (diff_hdr->type != LINE_DIFF_CHUNK) { | |
3474 | switch (diff_hdr->type) { | |
3475 | case LINE_DIFF_HEADER: | |
3476 | case LINE_DIFF_INDEX: | |
3477 | case LINE_DIFF_ADD: | |
3478 | case LINE_DIFF_DEL: | |
3479 | break; | |
3480 | ||
3481 | default: | |
3482 | diff_hdr++; | |
3483 | continue; | |
3484 | } | |
3485 | ||
3486 | if (!stage_diff_line(pipe, diff_hdr++)) { | |
3487 | pclose(pipe); | |
3488 | return FALSE; | |
3489 | } | |
3490 | } | |
3491 | } | |
3492 | ||
3493 | while (diff_chunk < diff_end && stage_diff_line(pipe, diff_chunk)) | |
3494 | diff_chunk++; | |
3495 | ||
3496 | pclose(pipe); | |
3497 | ||
3498 | if (diff_chunk != diff_end) | |
3499 | return FALSE; | |
3500 | ||
3501 | return TRUE; | |
3502 | } | |
3503 | ||
3504 | static void | |
3505 | stage_update(struct view *view, struct line *line) | |
3506 | { | |
3507 | if (stage_line_type != LINE_STAT_UNTRACKED && | |
3508 | (line->type == LINE_DIFF_CHUNK || !stage_status.status)) { | |
3509 | if (!stage_update_chunk(view, line)) { | |
3510 | report("Failed to apply chunk"); | |
3511 | return; | |
3512 | } | |
3513 | ||
3514 | } else if (!status_update_file(view, &stage_status, stage_line_type)) { | |
3515 | report("Failed to update file"); | |
3516 | return; | |
3517 | } | |
3518 | ||
3519 | open_view(view, REQ_VIEW_STATUS, OPEN_RELOAD); | |
3520 | ||
3521 | view = VIEW(REQ_VIEW_STATUS); | |
3522 | if (view_is_displayed(view)) | |
3523 | status_enter(view, &view->line[view->lineno]); | |
3524 | } | |
3525 | ||
3526 | static enum request | |
3527 | stage_request(struct view *view, enum request request, struct line *line) | |
3528 | { | |
3529 | switch (request) { | |
3530 | case REQ_STATUS_UPDATE: | |
3531 | stage_update(view, line); | |
3532 | break; | |
3533 | ||
3534 | case REQ_EDIT: | |
3535 | if (!stage_status.name[0]) | |
3536 | return request; | |
3537 | ||
3538 | open_editor(view, stage_status.name); | |
3539 | break; | |
3540 | ||
3541 | case REQ_ENTER: | |
3542 | pager_request(view, request, line); | |
3543 | break; | |
3544 | ||
3545 | default: | |
3546 | return request; | |
3547 | } | |
3548 | ||
3549 | return REQ_NONE; | |
3550 | } | |
3551 | ||
3e634113 JF |
3552 | static struct view_ops stage_ops = { |
3553 | "line", | |
3554 | NULL, | |
3555 | pager_read, | |
3556 | pager_draw, | |
b33611d8 | 3557 | stage_request, |
3e634113 JF |
3558 | pager_grep, |
3559 | pager_select, | |
3560 | }; | |
173d76ea | 3561 | |
b33611d8 | 3562 | |
173d76ea | 3563 | /* |
ccc33449 | 3564 | * Revision graph |
ff26aa29 JF |
3565 | */ |
3566 | ||
3567 | struct commit { | |
10446330 | 3568 | char id[SIZEOF_REV]; /* SHA1 ID. */ |
aea510c8 | 3569 | char title[128]; /* First line of the commit message. */ |
54efb62b JF |
3570 | char author[75]; /* Author of the commit. */ |
3571 | struct tm time; /* Date from the author ident. */ | |
3572 | struct ref **refs; /* Repository references. */ | |
3573 | chtype graph[SIZEOF_REVGRAPH]; /* Ancestry chain graphics. */ | |
3574 | size_t graph_size; /* The width of the graph array. */ | |
ff26aa29 | 3575 | }; |
c34d9c9f | 3576 | |
ccc33449 JF |
3577 | /* Size of rev graph with no "padding" columns */ |
3578 | #define SIZEOF_REVITEMS (SIZEOF_REVGRAPH - (SIZEOF_REVGRAPH / 2)) | |
2b757533 | 3579 | |
2ce5c87c JF |
3580 | struct rev_graph { |
3581 | struct rev_graph *prev, *next, *parents; | |
2b757533 JF |
3582 | char rev[SIZEOF_REVITEMS][SIZEOF_REV]; |
3583 | size_t size; | |
88757ebd JF |
3584 | struct commit *commit; |
3585 | size_t pos; | |
2b757533 JF |
3586 | }; |
3587 | ||
2b757533 | 3588 | /* Parents of the commit being visualized. */ |
446a5c36 | 3589 | static struct rev_graph graph_parents[4]; |
c8d60a25 | 3590 | |
c65a501a | 3591 | /* The current stack of revisions on the graph. */ |
446a5c36 JF |
3592 | static struct rev_graph graph_stacks[4] = { |
3593 | { &graph_stacks[3], &graph_stacks[1], &graph_parents[0] }, | |
c65a501a | 3594 | { &graph_stacks[0], &graph_stacks[2], &graph_parents[1] }, |
446a5c36 JF |
3595 | { &graph_stacks[1], &graph_stacks[3], &graph_parents[2] }, |
3596 | { &graph_stacks[2], &graph_stacks[0], &graph_parents[3] }, | |
c65a501a JF |
3597 | }; |
3598 | ||
9e43b9cd | 3599 | static inline bool |
2ce5c87c | 3600 | graph_parent_is_merge(struct rev_graph *graph) |
9e43b9cd JF |
3601 | { |
3602 | return graph->parents->size > 1; | |
3603 | } | |
3604 | ||
88757ebd | 3605 | static inline void |
2ce5c87c | 3606 | append_to_rev_graph(struct rev_graph *graph, chtype symbol) |
88757ebd | 3607 | { |
2c27faac JF |
3608 | struct commit *commit = graph->commit; |
3609 | ||
3610 | if (commit->graph_size < ARRAY_SIZE(commit->graph) - 1) | |
3611 | commit->graph[commit->graph_size++] = symbol; | |
88757ebd JF |
3612 | } |
3613 | ||
2b757533 | 3614 | static void |
2ce5c87c | 3615 | done_rev_graph(struct rev_graph *graph) |
987890af JF |
3616 | { |
3617 | if (graph_parent_is_merge(graph) && | |
3618 | graph->pos < graph->size - 1 && | |
3619 | graph->next->size == graph->size + graph->parents->size - 1) { | |
3620 | size_t i = graph->pos + graph->parents->size - 1; | |
3621 | ||
3622 | graph->commit->graph_size = i * 2; | |
3623 | while (i < graph->next->size - 1) { | |
3624 | append_to_rev_graph(graph, ' '); | |
3625 | append_to_rev_graph(graph, '\\'); | |
3626 | i++; | |
3627 | } | |
3628 | } | |
3629 | ||
3630 | graph->size = graph->pos = 0; | |
3631 | graph->commit = NULL; | |
3632 | memset(graph->parents, 0, sizeof(*graph->parents)); | |
3633 | } | |
3634 | ||
3635 | static void | |
2ce5c87c | 3636 | push_rev_graph(struct rev_graph *graph, char *parent) |
2b757533 | 3637 | { |
2fe894e6 JF |
3638 | int i; |
3639 | ||
3640 | /* "Collapse" duplicate parents lines. | |
3641 | * | |
3642 | * FIXME: This needs to also update update the drawn graph but | |
3643 | * for now it just serves as a method for pruning graph lines. */ | |
3644 | for (i = 0; i < graph->size; i++) | |
3645 | if (!strncmp(graph->rev[i], parent, SIZEOF_REV)) | |
3646 | return; | |
2b757533 | 3647 | |
2ce5c87c | 3648 | if (graph->size < SIZEOF_REVITEMS) { |
739e81de | 3649 | string_copy_rev(graph->rev[graph->size++], parent); |
2b757533 JF |
3650 | } |
3651 | } | |
3652 | ||
92507a24 JF |
3653 | static chtype |
3654 | get_rev_graph_symbol(struct rev_graph *graph) | |
2b757533 | 3655 | { |
92507a24 | 3656 | chtype symbol; |
2b757533 | 3657 | |
c65a501a | 3658 | if (graph->parents->size == 0) |
c8d60a25 | 3659 | symbol = REVGRAPH_INIT; |
18ffaa23 | 3660 | else if (graph_parent_is_merge(graph)) |
c8d60a25 | 3661 | symbol = REVGRAPH_MERGE; |
c65a501a | 3662 | else if (graph->pos >= graph->size) |
c8d60a25 | 3663 | symbol = REVGRAPH_BRANCH; |
2b757533 | 3664 | else |
c8d60a25 | 3665 | symbol = REVGRAPH_COMMIT; |
1dcb3bec | 3666 | |
92507a24 JF |
3667 | return symbol; |
3668 | } | |
3669 | ||
3670 | static void | |
3671 | draw_rev_graph(struct rev_graph *graph) | |
3672 | { | |
e937c2c8 JF |
3673 | struct rev_filler { |
3674 | chtype separator, line; | |
3675 | }; | |
3676 | enum { DEFAULT, RSHARP, RDIAG, LDIAG }; | |
3677 | static struct rev_filler fillers[] = { | |
3678 | { ' ', REVGRAPH_LINE }, | |
3679 | { '`', '.' }, | |
3680 | { '\'', ' ' }, | |
3681 | { '/', ' ' }, | |
e937c2c8 | 3682 | }; |
92507a24 | 3683 | chtype symbol = get_rev_graph_symbol(graph); |
e937c2c8 | 3684 | struct rev_filler *filler; |
92507a24 JF |
3685 | size_t i; |
3686 | ||
e937c2c8 | 3687 | filler = &fillers[DEFAULT]; |
110e948e | 3688 | |
c65a501a | 3689 | for (i = 0; i < graph->pos; i++) { |
e937c2c8 | 3690 | append_to_rev_graph(graph, filler->line); |
9e43b9cd | 3691 | if (graph_parent_is_merge(graph->prev) && |
e937c2c8 JF |
3692 | graph->prev->pos == i) |
3693 | filler = &fillers[RSHARP]; | |
3694 | ||
3695 | append_to_rev_graph(graph, filler->separator); | |
110e948e JF |
3696 | } |
3697 | ||
92507a24 | 3698 | /* Place the symbol for this revision. */ |
c65a501a | 3699 | append_to_rev_graph(graph, symbol); |
2b757533 | 3700 | |
e937c2c8 JF |
3701 | if (graph->prev->size > graph->size) |
3702 | filler = &fillers[RDIAG]; | |
3703 | else | |
3704 | filler = &fillers[DEFAULT]; | |
3705 | ||
c8d60a25 | 3706 | i++; |
2b757533 | 3707 | |
c65a501a | 3708 | for (; i < graph->size; i++) { |
e937c2c8 JF |
3709 | append_to_rev_graph(graph, filler->separator); |
3710 | append_to_rev_graph(graph, filler->line); | |
3711 | if (graph_parent_is_merge(graph->prev) && | |
3712 | i < graph->prev->pos + graph->parents->size) | |
3713 | filler = &fillers[RSHARP]; | |
3714 | if (graph->prev->size > graph->size) | |
3715 | filler = &fillers[LDIAG]; | |
c65a501a JF |
3716 | } |
3717 | ||
3718 | if (graph->prev->size > graph->size) { | |
e937c2c8 JF |
3719 | append_to_rev_graph(graph, filler->separator); |
3720 | if (filler->line != ' ') | |
3721 | append_to_rev_graph(graph, filler->line); | |
2b757533 | 3722 | } |
b5d8f208 JF |
3723 | } |
3724 | ||
61eed810 JF |
3725 | /* Prepare the next rev graph */ |
3726 | static void | |
3727 | prepare_rev_graph(struct rev_graph *graph) | |
b5d8f208 | 3728 | { |
b5d8f208 JF |
3729 | size_t i; |
3730 | ||
320df4ea | 3731 | /* First, traverse all lines of revisions up to the active one. */ |
c65a501a JF |
3732 | for (graph->pos = 0; graph->pos < graph->size; graph->pos++) { |
3733 | if (!strcmp(graph->rev[graph->pos], graph->commit->id)) | |
b5d8f208 | 3734 | break; |
b5d8f208 | 3735 | |
2ce5c87c | 3736 | push_rev_graph(graph->next, graph->rev[graph->pos]); |
b5d8f208 JF |
3737 | } |
3738 | ||
320df4ea | 3739 | /* Interleave the new revision parent(s). */ |
c65a501a | 3740 | for (i = 0; i < graph->parents->size; i++) |
2ce5c87c | 3741 | push_rev_graph(graph->next, graph->parents->rev[i]); |
b5d8f208 | 3742 | |
320df4ea | 3743 | /* Lastly, put any remaining revisions. */ |
c65a501a | 3744 | for (i = graph->pos + 1; i < graph->size; i++) |
2ce5c87c | 3745 | push_rev_graph(graph->next, graph->rev[i]); |
61eed810 JF |
3746 | } |
3747 | ||
3748 | static void | |
3749 | update_rev_graph(struct rev_graph *graph) | |
3750 | { | |
446a5c36 JF |
3751 | /* If this is the finalizing update ... */ |
3752 | if (graph->commit) | |
3753 | prepare_rev_graph(graph); | |
3754 | ||
3755 | /* Graph visualization needs a one rev look-ahead, | |
3756 | * so the first update doesn't visualize anything. */ | |
3757 | if (!graph->prev->commit) | |
3758 | return; | |
c65a501a | 3759 | |
61eed810 JF |
3760 | draw_rev_graph(graph->prev); |
3761 | done_rev_graph(graph->prev->prev); | |
2b757533 JF |
3762 | } |
3763 | ||
ccc33449 JF |
3764 | |
3765 | /* | |
3766 | * Main view backend | |
3767 | */ | |
3768 | ||
3769 | static bool | |
3770 | main_draw(struct view *view, struct line *line, unsigned int lineno, bool selected) | |
3771 | { | |
3772 | char buf[DATE_COLS + 1]; | |
3773 | struct commit *commit = line->data; | |
3774 | enum line_type type; | |
3775 | int col = 0; | |
3776 | size_t timelen; | |
3777 | size_t authorlen; | |
3778 | int trimmed = 1; | |
3779 | ||
3780 | if (!*commit->author) | |
3781 | return FALSE; | |
3782 | ||
3783 | wmove(view->win, lineno, col); | |
3784 | ||
3785 | if (selected) { | |
3786 | type = LINE_CURSOR; | |
3787 | wattrset(view->win, get_line_attr(type)); | |
3788 | wchgat(view->win, -1, 0, type, NULL); | |
3789 | ||
3790 | } else { | |
3791 | type = LINE_MAIN_COMMIT; | |
3792 | wattrset(view->win, get_line_attr(LINE_MAIN_DATE)); | |
3793 | } | |
3794 | ||
3795 | timelen = strftime(buf, sizeof(buf), DATE_FORMAT, &commit->time); | |
3796 | waddnstr(view->win, buf, timelen); | |
3797 | waddstr(view->win, " "); | |
3798 | ||
3799 | col += DATE_COLS; | |
3800 | wmove(view->win, lineno, col); | |
3801 | if (type != LINE_CURSOR) | |
3802 | wattrset(view->win, get_line_attr(LINE_MAIN_AUTHOR)); | |
3803 | ||
3804 | if (opt_utf8) { | |
3805 | authorlen = utf8_length(commit->author, AUTHOR_COLS - 2, &col, &trimmed); | |
3806 | } else { | |
3807 | authorlen = strlen(commit->author); | |
3808 | if (authorlen > AUTHOR_COLS - 2) { | |
3809 | authorlen = AUTHOR_COLS - 2; | |
3810 | trimmed = 1; | |
3811 | } | |
3812 | } | |
3813 | ||
3814 | if (trimmed) { | |
3815 | waddnstr(view->win, commit->author, authorlen); | |
3816 | if (type != LINE_CURSOR) | |
3817 | wattrset(view->win, get_line_attr(LINE_MAIN_DELIM)); | |
3818 | waddch(view->win, '~'); | |
3819 | } else { | |
3820 | waddstr(view->win, commit->author); | |
3821 | } | |
3822 | ||
3823 | col += AUTHOR_COLS; | |
3824 | if (type != LINE_CURSOR) | |
3825 | wattrset(view->win, A_NORMAL); | |
3826 | ||
3827 | if (opt_rev_graph && commit->graph_size) { | |
3828 | size_t i; | |
3829 | ||
3830 | wmove(view->win, lineno, col); | |
3831 | /* Using waddch() instead of waddnstr() ensures that | |
3832 | * they'll be rendered correctly for the cursor line. */ | |
3833 | for (i = 0; i < commit->graph_size; i++) | |
3834 | waddch(view->win, commit->graph[i]); | |
3835 | ||
8716b9ed | 3836 | waddch(view->win, ' '); |
ccc33449 JF |
3837 | col += commit->graph_size + 1; |
3838 | } | |
3839 | ||
3840 | wmove(view->win, lineno, col); | |
3841 | ||
3842 | if (commit->refs) { | |
3843 | size_t i = 0; | |
3844 | ||
3845 | do { | |
3846 | if (type == LINE_CURSOR) | |
3847 | ; | |
3848 | else if (commit->refs[i]->tag) | |
3849 | wattrset(view->win, get_line_attr(LINE_MAIN_TAG)); | |
e15ec88e JF |
3850 | else if (commit->refs[i]->remote) |
3851 | wattrset(view->win, get_line_attr(LINE_MAIN_REMOTE)); | |
ccc33449 JF |
3852 | else |
3853 | wattrset(view->win, get_line_attr(LINE_MAIN_REF)); | |
3854 | waddstr(view->win, "["); | |
3855 | waddstr(view->win, commit->refs[i]->name); | |
3856 | waddstr(view->win, "]"); | |
3857 | if (type != LINE_CURSOR) | |
3858 | wattrset(view->win, A_NORMAL); | |
3859 | waddstr(view->win, " "); | |
3860 | col += strlen(commit->refs[i]->name) + STRING_SIZE("[] "); | |
3861 | } while (commit->refs[i++]->next); | |
3862 | } | |
3863 | ||
3864 | if (type != LINE_CURSOR) | |
3865 | wattrset(view->win, get_line_attr(type)); | |
3866 | ||
3867 | { | |
3868 | int titlelen = strlen(commit->title); | |
3869 | ||
3870 | if (col + titlelen > view->width) | |
3871 | titlelen = view->width - col; | |
3872 | ||
3873 | waddnstr(view->win, commit->title, titlelen); | |
3874 | } | |
3875 | ||
3876 | return TRUE; | |
3877 | } | |
3878 | ||
4c6fabc2 | 3879 | /* Reads git log --pretty=raw output and parses it into the commit struct. */ |
6b161b31 | 3880 | static bool |
701e4f5d | 3881 | main_read(struct view *view, char *line) |
22f66b0a | 3882 | { |
2ce5c87c | 3883 | static struct rev_graph *graph = graph_stacks; |
be04d936 | 3884 | enum line_type type; |
0ff3b97c | 3885 | struct commit *commit; |
22f66b0a | 3886 | |
be04d936 | 3887 | if (!line) { |
446a5c36 | 3888 | update_rev_graph(graph); |
be04d936 JF |
3889 | return TRUE; |
3890 | } | |
3891 | ||
3892 | type = get_line_type(line); | |
0ff3b97c | 3893 | if (type == LINE_COMMIT) { |
22f66b0a JF |
3894 | commit = calloc(1, sizeof(struct commit)); |
3895 | if (!commit) | |
3896 | return FALSE; | |
3897 | ||
0ff3b97c | 3898 | string_copy_rev(commit->id, line + STRING_SIZE("commit ")); |
c34d9c9f | 3899 | commit->refs = get_refs(commit->id); |
c65a501a | 3900 | graph->commit = commit; |
e314c36d | 3901 | add_line_data(view, commit, LINE_MAIN_COMMIT); |
0ff3b97c JF |
3902 | return TRUE; |
3903 | } | |
2b757533 | 3904 | |
0ff3b97c JF |
3905 | if (!view->lines) |
3906 | return TRUE; | |
3907 | commit = view->line[view->lines - 1].data; | |
3908 | ||
3909 | switch (type) { | |
2b757533 | 3910 | case LINE_PARENT: |
0ff3b97c | 3911 | push_rev_graph(graph->parents, line + STRING_SIZE("parent ")); |
78c70acd | 3912 | break; |
22f66b0a | 3913 | |
8855ada4 | 3914 | case LINE_AUTHOR: |
b76c2afc | 3915 | { |
19c3ac60 JF |
3916 | /* Parse author lines where the name may be empty: |
3917 | * author <email@address.tld> 1138474660 +0100 | |
3918 | */ | |
4c6fabc2 | 3919 | char *ident = line + STRING_SIZE("author "); |
19c3ac60 JF |
3920 | char *nameend = strchr(ident, '<'); |
3921 | char *emailend = strchr(ident, '>'); | |
b76c2afc | 3922 | |
0ff3b97c | 3923 | if (!nameend || !emailend) |
fe7233c3 JF |
3924 | break; |
3925 | ||
c65a501a JF |
3926 | update_rev_graph(graph); |
3927 | graph = graph->next; | |
2b757533 | 3928 | |
19c3ac60 JF |
3929 | *nameend = *emailend = 0; |
3930 | ident = chomp_string(ident); | |
3931 | if (!*ident) { | |
3932 | ident = chomp_string(nameend + 1); | |
3933 | if (!*ident) | |
3934 | ident = "Unknown"; | |
b76c2afc JF |
3935 | } |
3936 | ||
739e81de | 3937 | string_ncopy(commit->author, ident, strlen(ident)); |
b76c2afc | 3938 | |
4c6fabc2 | 3939 | /* Parse epoch and timezone */ |
19c3ac60 JF |
3940 | if (emailend[1] == ' ') { |
3941 | char *secs = emailend + 2; | |
3942 | char *zone = strchr(secs, ' '); | |
3943 | time_t time = (time_t) atol(secs); | |
b76c2afc | 3944 | |
4c6fabc2 | 3945 | if (zone && strlen(zone) == STRING_SIZE(" +0700")) { |
b76c2afc JF |
3946 | long tz; |
3947 | ||
3948 | zone++; | |
3949 | tz = ('0' - zone[1]) * 60 * 60 * 10; | |
3950 | tz += ('0' - zone[2]) * 60 * 60; | |
3951 | tz += ('0' - zone[3]) * 60; | |
3952 | tz += ('0' - zone[4]) * 60; | |
3953 | ||
3954 | if (zone[0] == '-') | |
3955 | tz = -tz; | |
3956 | ||
3957 | time -= tz; | |
3958 | } | |
19c3ac60 | 3959 | |
b76c2afc JF |
3960 | gmtime_r(&time, &commit->time); |
3961 | } | |
3962 | break; | |
3963 | } | |
78c70acd | 3964 | default: |
2e8488b4 | 3965 | /* Fill in the commit title if it has not already been set. */ |
2e8488b4 JF |
3966 | if (commit->title[0]) |
3967 | break; | |
3968 | ||
3969 | /* Require titles to start with a non-space character at the | |
3970 | * offset used by git log. */ | |
9073c64a JF |
3971 | if (strncmp(line, " ", 4)) |
3972 | break; | |
3973 | line += 4; | |
3974 | /* Well, if the title starts with a whitespace character, | |
3975 | * try to be forgiving. Otherwise we end up with no title. */ | |
3976 | while (isspace(*line)) | |
3977 | line++; | |
3978 | if (*line == '\0') | |
82e78006 | 3979 | break; |
9073c64a JF |
3980 | /* FIXME: More graceful handling of titles; append "..." to |
3981 | * shortened titles, etc. */ | |
82e78006 | 3982 | |
739e81de | 3983 | string_ncopy(commit->title, line, strlen(line)); |
22f66b0a JF |
3984 | } |
3985 | ||
3986 | return TRUE; | |
3987 | } | |
3988 | ||
586c423d JF |
3989 | static enum request |
3990 | main_request(struct view *view, enum request request, struct line *line) | |
b801d8b2 | 3991 | { |
b3a54cba JF |
3992 | enum open_flags flags = display[0] == view ? OPEN_SPLIT : OPEN_DEFAULT; |
3993 | ||
586c423d JF |
3994 | if (request == REQ_ENTER) |
3995 | open_view(view, REQ_VIEW_DIFF, flags); | |
3996 | else | |
3997 | return request; | |
3998 | ||
3999 | return REQ_NONE; | |
b801d8b2 JF |
4000 | } |
4001 | ||
4af34daa JF |
4002 | static bool |
4003 | main_grep(struct view *view, struct line *line) | |
4004 | { | |
4005 | struct commit *commit = line->data; | |
4006 | enum { S_TITLE, S_AUTHOR, S_DATE, S_END } state; | |
4007 | char buf[DATE_COLS + 1]; | |
4008 | regmatch_t pmatch; | |
4009 | ||
4010 | for (state = S_TITLE; state < S_END; state++) { | |
4011 | char *text; | |
4012 | ||
4013 | switch (state) { | |
4014 | case S_TITLE: text = commit->title; break; | |
4015 | case S_AUTHOR: text = commit->author; break; | |
4016 | case S_DATE: | |
4017 | if (!strftime(buf, sizeof(buf), DATE_FORMAT, &commit->time)) | |
4018 | continue; | |
4019 | text = buf; | |
4020 | break; | |
4021 | ||
4022 | default: | |
4023 | return FALSE; | |
4024 | } | |
4025 | ||
b77b2cb8 | 4026 | if (regexec(view->regex, text, 1, &pmatch, 0) != REG_NOMATCH) |
4af34daa JF |
4027 | return TRUE; |
4028 | } | |
4029 | ||
4030 | return FALSE; | |
4031 | } | |
4032 | ||
d720de4b JF |
4033 | static void |
4034 | main_select(struct view *view, struct line *line) | |
4035 | { | |
4036 | struct commit *commit = line->data; | |
4037 | ||
2463b4ea JF |
4038 | string_copy_rev(view->ref, commit->id); |
4039 | string_copy_rev(ref_commit, view->ref); | |
d720de4b JF |
4040 | } |
4041 | ||
6b161b31 | 4042 | static struct view_ops main_ops = { |
6734f6b9 | 4043 | "commit", |
f098944b | 4044 | NULL, |
6b161b31 | 4045 | main_read, |
f098944b | 4046 | main_draw, |
586c423d | 4047 | main_request, |
4af34daa | 4048 | main_grep, |
d720de4b | 4049 | main_select, |
6b161b31 | 4050 | }; |
2e8488b4 | 4051 | |
c34d9c9f | 4052 | |
6b161b31 | 4053 | /* |
10e290ee JF |
4054 | * Unicode / UTF-8 handling |
4055 | * | |
4056 | * NOTE: Much of the following code for dealing with unicode is derived from | |
4057 | * ELinks' UTF-8 code developed by Scrool <scroolik@gmail.com>. Origin file is | |
4058 | * src/intl/charset.c from the utf8 branch commit elinks-0.11.0-g31f2c28. | |
4059 | */ | |
4060 | ||
4061 | /* I've (over)annotated a lot of code snippets because I am not entirely | |
4062 | * confident that the approach taken by this small UTF-8 interface is correct. | |
4063 | * --jonas */ | |
4064 | ||
4065 | static inline int | |
4066 | unicode_width(unsigned long c) | |
4067 | { | |
4068 | if (c >= 0x1100 && | |
4069 | (c <= 0x115f /* Hangul Jamo */ | |
4070 | || c == 0x2329 | |
4071 | || c == 0x232a | |
4072 | || (c >= 0x2e80 && c <= 0xa4cf && c != 0x303f) | |
f97f4012 | 4073 | /* CJK ... Yi */ |
10e290ee JF |
4074 | || (c >= 0xac00 && c <= 0xd7a3) /* Hangul Syllables */ |
4075 | || (c >= 0xf900 && c <= 0xfaff) /* CJK Compatibility Ideographs */ | |
4076 | || (c >= 0xfe30 && c <= 0xfe6f) /* CJK Compatibility Forms */ | |
4077 | || (c >= 0xff00 && c <= 0xff60) /* Fullwidth Forms */ | |
4078 | || (c >= 0xffe0 && c <= 0xffe6) | |
4079 | || (c >= 0x20000 && c <= 0x2fffd) | |
4080 | || (c >= 0x30000 && c <= 0x3fffd))) | |
4081 | return 2; | |
4082 | ||
4083 | return 1; | |
4084 | } | |
4085 | ||
4086 | /* Number of bytes used for encoding a UTF-8 character indexed by first byte. | |
4087 | * Illegal bytes are set one. */ | |
4088 | static const unsigned char utf8_bytes[256] = { | |
4089 | 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, | |
4090 | 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, | |
4091 | 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, | |
4092 | 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, | |
4093 | 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, | |
4094 | 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, | |
4095 | 2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2, | |
4096 | 3,3,3,3,3,3,3,3, 3,3,3,3,3,3,3,3, 4,4,4,4,4,4,4,4, 5,5,5,5,6,6,1,1, | |
4097 | }; | |
4098 | ||
4099 | /* Decode UTF-8 multi-byte representation into a unicode character. */ | |
4100 | static inline unsigned long | |
4101 | utf8_to_unicode(const char *string, size_t length) | |
4102 | { | |
4103 | unsigned long unicode; | |
4104 | ||
4105 | switch (length) { | |
4106 | case 1: | |
4107 | unicode = string[0]; | |
4108 | break; | |
4109 | case 2: | |
4110 | unicode = (string[0] & 0x1f) << 6; | |
4111 | unicode += (string[1] & 0x3f); | |
4112 | break; | |
4113 | case 3: | |
4114 | unicode = (string[0] & 0x0f) << 12; | |
4115 | unicode += ((string[1] & 0x3f) << 6); | |
4116 | unicode += (string[2] & 0x3f); | |
4117 | break; | |
4118 | case 4: | |
4119 | unicode = (string[0] & 0x0f) << 18; | |
4120 | unicode += ((string[1] & 0x3f) << 12); | |
4121 | unicode += ((string[2] & 0x3f) << 6); | |
4122 | unicode += (string[3] & 0x3f); | |
4123 | break; | |
4124 | case 5: | |
4125 | unicode = (string[0] & 0x0f) << 24; | |
4126 | unicode += ((string[1] & 0x3f) << 18); | |
4127 | unicode += ((string[2] & 0x3f) << 12); | |
4128 | unicode += ((string[3] & 0x3f) << 6); | |
4129 | unicode += (string[4] & 0x3f); | |
4130 | break; | |
68b6e0eb | 4131 | case 6: |
10e290ee JF |
4132 | unicode = (string[0] & 0x01) << 30; |
4133 | unicode += ((string[1] & 0x3f) << 24); | |
4134 | unicode += ((string[2] & 0x3f) << 18); | |
4135 | unicode += ((string[3] & 0x3f) << 12); | |
4136 | unicode += ((string[4] & 0x3f) << 6); | |
4137 | unicode += (string[5] & 0x3f); | |
4138 | break; | |
4139 | default: | |
4140 | die("Invalid unicode length"); | |
4141 | } | |
4142 | ||
4143 | /* Invalid characters could return the special 0xfffd value but NUL | |
4144 | * should be just as good. */ | |
4145 | return unicode > 0xffff ? 0 : unicode; | |
4146 | } | |
4147 | ||
4148 | /* Calculates how much of string can be shown within the given maximum width | |
4149 | * and sets trimmed parameter to non-zero value if all of string could not be | |
4150 | * shown. | |
4151 | * | |
4152 | * Additionally, adds to coloffset how many many columns to move to align with | |
4153 | * the expected position. Takes into account how multi-byte and double-width | |
4154 | * characters will effect the cursor position. | |
4155 | * | |
4156 | * Returns the number of bytes to output from string to satisfy max_width. */ | |
4157 | static size_t | |
4158 | utf8_length(const char *string, size_t max_width, int *coloffset, int *trimmed) | |
4159 | { | |
4160 | const char *start = string; | |
4161 | const char *end = strchr(string, '\0'); | |
4162 | size_t mbwidth = 0; | |
4163 | size_t width = 0; | |
4164 | ||
4165 | *trimmed = 0; | |
4166 | ||
4167 | while (string < end) { | |
4168 | int c = *(unsigned char *) string; | |
4169 | unsigned char bytes = utf8_bytes[c]; | |
4170 | size_t ucwidth; | |
4171 | unsigned long unicode; | |
4172 | ||
4173 | if (string + bytes > end) | |
4174 | break; | |
4175 | ||
4176 | /* Change representation to figure out whether | |
4177 | * it is a single- or double-width character. */ | |
4178 | ||
4179 | unicode = utf8_to_unicode(string, bytes); | |
4180 | /* FIXME: Graceful handling of invalid unicode character. */ | |
4181 | if (!unicode) | |
4182 | break; | |
4183 | ||
4184 | ucwidth = unicode_width(unicode); | |
4185 | width += ucwidth; | |
4186 | if (width > max_width) { | |
4187 | *trimmed = 1; | |
4188 | break; | |
4189 | } | |
4190 | ||
4191 | /* The column offset collects the differences between the | |
4192 | * number of bytes encoding a character and the number of | |
4193 | * columns will be used for rendering said character. | |
4194 | * | |
4195 | * So if some character A is encoded in 2 bytes, but will be | |
4196 | * represented on the screen using only 1 byte this will and up | |
4197 | * adding 1 to the multi-byte column offset. | |
4198 | * | |
4199 | * Assumes that no double-width character can be encoding in | |
4200 | * less than two bytes. */ | |
4201 | if (bytes > ucwidth) | |
4202 | mbwidth += bytes - ucwidth; | |
4203 | ||
4204 | string += bytes; | |
4205 | } | |
4206 | ||
4207 | *coloffset += mbwidth; | |
4208 | ||
4209 | return string - start; | |
4210 | } | |
4211 | ||
4212 | ||
4213 | /* | |
6b161b31 JF |
4214 | * Status management |
4215 | */ | |
2e8488b4 | 4216 | |
8855ada4 | 4217 | /* Whether or not the curses interface has been initialized. */ |
68b6e0eb | 4218 | static bool cursed = FALSE; |
8855ada4 | 4219 | |
6b161b31 JF |
4220 | /* The status window is used for polling keystrokes. */ |
4221 | static WINDOW *status_win; | |
4a2909a7 | 4222 | |
21be28fb JF |
4223 | static bool status_empty = TRUE; |
4224 | ||
2e8488b4 | 4225 | /* Update status and title window. */ |
4a2909a7 JF |
4226 | static void |
4227 | report(const char *msg, ...) | |
4228 | { | |
6706b2ba | 4229 | struct view *view = display[current_view]; |
b76c2afc | 4230 | |
ab4af23e JF |
4231 | if (input_mode) |
4232 | return; | |
4233 | ||
21be28fb | 4234 | if (!status_empty || *msg) { |
6706b2ba | 4235 | va_list args; |
4a2909a7 | 4236 | |
6706b2ba | 4237 | va_start(args, msg); |
4b76734f | 4238 | |
6706b2ba JF |
4239 | wmove(status_win, 0, 0); |
4240 | if (*msg) { | |
4241 | vwprintw(status_win, msg, args); | |
21be28fb | 4242 | status_empty = FALSE; |
6706b2ba | 4243 | } else { |
21be28fb | 4244 | status_empty = TRUE; |
6706b2ba | 4245 | } |
390a8262 | 4246 | wclrtoeol(status_win); |
6706b2ba | 4247 | wrefresh(status_win); |
b801d8b2 | 4248 | |
6706b2ba JF |
4249 | va_end(args); |
4250 | } | |
4251 | ||
4252 | update_view_title(view); | |
2bee3bde | 4253 | update_display_cursor(view); |
b801d8b2 JF |
4254 | } |
4255 | ||
6b161b31 JF |
4256 | /* Controls when nodelay should be in effect when polling user input. */ |
4257 | static void | |
1ba2ae4b | 4258 | set_nonblocking_input(bool loading) |
b801d8b2 | 4259 | { |
6706b2ba | 4260 | static unsigned int loading_views; |
b801d8b2 | 4261 | |
6706b2ba JF |
4262 | if ((loading == FALSE && loading_views-- == 1) || |
4263 | (loading == TRUE && loading_views++ == 0)) | |
1ba2ae4b | 4264 | nodelay(status_win, loading); |
6b161b31 JF |
4265 | } |
4266 | ||
4267 | static void | |
4268 | init_display(void) | |
4269 | { | |
4270 | int x, y; | |
b76c2afc | 4271 | |
6908bdbd JF |
4272 | /* Initialize the curses library */ |
4273 | if (isatty(STDIN_FILENO)) { | |
8855ada4 | 4274 | cursed = !!initscr(); |
6908bdbd JF |
4275 | } else { |
4276 | /* Leave stdin and stdout alone when acting as a pager. */ | |
4277 | FILE *io = fopen("/dev/tty", "r+"); | |
4278 | ||
e6f60674 JF |
4279 | if (!io) |
4280 | die("Failed to open /dev/tty"); | |
8855ada4 | 4281 | cursed = !!newterm(NULL, io, io); |
6908bdbd JF |
4282 | } |
4283 | ||
8855ada4 JF |
4284 | if (!cursed) |
4285 | die("Failed to initialize curses"); | |
4286 | ||
2e8488b4 JF |
4287 | nonl(); /* Tell curses not to do NL->CR/NL on output */ |
4288 | cbreak(); /* Take input chars one at a time, no wait for \n */ | |
4289 | noecho(); /* Don't echo input */ | |
b801d8b2 | 4290 | leaveok(stdscr, TRUE); |
b801d8b2 JF |
4291 | |
4292 | if (has_colors()) | |
4293 | init_colors(); | |
4294 | ||
4295 | getmaxyx(stdscr, y, x); | |
4296 | status_win = newwin(1, 0, y - 1, 0); | |
4297 | if (!status_win) | |
4298 | die("Failed to create status window"); | |
4299 | ||
4300 | /* Enable keyboard mapping */ | |
4301 | keypad(status_win, TRUE); | |
78c70acd | 4302 | wbkgdset(status_win, get_line_attr(LINE_STATUS)); |
6b161b31 JF |
4303 | } |
4304 | ||
4af34daa | 4305 | static char * |
cb9e48c1 | 4306 | read_prompt(const char *prompt) |
ef5404a4 JF |
4307 | { |
4308 | enum { READING, STOP, CANCEL } status = READING; | |
9e21ce5c | 4309 | static char buf[sizeof(opt_cmd) - STRING_SIZE("git \0")]; |
ef5404a4 JF |
4310 | int pos = 0; |
4311 | ||
4312 | while (status == READING) { | |
4313 | struct view *view; | |
4314 | int i, key; | |
4315 | ||
ab4af23e JF |
4316 | input_mode = TRUE; |
4317 | ||
699ae55b | 4318 | foreach_view (view, i) |
ef5404a4 JF |
4319 | update_view(view); |
4320 | ||
ab4af23e JF |
4321 | input_mode = FALSE; |
4322 | ||
4323 | mvwprintw(status_win, 0, 0, "%s%.*s", prompt, pos, buf); | |
4324 | wclrtoeol(status_win); | |
4325 | ||
ef5404a4 JF |
4326 | /* Refresh, accept single keystroke of input */ |
4327 | key = wgetch(status_win); | |
4328 | switch (key) { | |
4329 | case KEY_RETURN: | |
4330 | case KEY_ENTER: | |
4331 | case '\n': | |
4332 | status = pos ? STOP : CANCEL; | |
4333 | break; | |
4334 | ||
4335 | case KEY_BACKSPACE: | |
4336 | if (pos > 0) | |
4337 | pos--; | |
4338 | else | |
4339 | status = CANCEL; | |
4340 | break; | |
4341 | ||
4342 | case KEY_ESC: | |
4343 | status = CANCEL; | |
4344 | break; | |
4345 | ||
4346 | case ERR: | |
4347 | break; | |
4348 | ||
4349 | default: | |
4350 | if (pos >= sizeof(buf)) { | |
4351 | report("Input string too long"); | |
9e21ce5c | 4352 | return NULL; |
ef5404a4 JF |
4353 | } |
4354 | ||
4355 | if (isprint(key)) | |
4356 | buf[pos++] = (char) key; | |
4357 | } | |
4358 | } | |
4359 | ||
7a06ebdf JF |
4360 | /* Clear the status window */ |
4361 | status_empty = FALSE; | |
4362 | report(""); | |
4363 | ||
4364 | if (status == CANCEL) | |
9e21ce5c | 4365 | return NULL; |
ef5404a4 JF |
4366 | |
4367 | buf[pos++] = 0; | |
ef5404a4 | 4368 | |
9e21ce5c | 4369 | return buf; |
ef5404a4 | 4370 | } |
c34d9c9f JF |
4371 | |
4372 | /* | |
4373 | * Repository references | |
4374 | */ | |
4375 | ||
4376 | static struct ref *refs; | |
3a91b75e | 4377 | static size_t refs_size; |
c34d9c9f | 4378 | |
1307df1a JF |
4379 | /* Id <-> ref store */ |
4380 | static struct ref ***id_refs; | |
4381 | static size_t id_refs_size; | |
4382 | ||
c34d9c9f JF |
4383 | static struct ref ** |
4384 | get_refs(char *id) | |
4385 | { | |
1307df1a JF |
4386 | struct ref ***tmp_id_refs; |
4387 | struct ref **ref_list = NULL; | |
4388 | size_t ref_list_size = 0; | |
c34d9c9f JF |
4389 | size_t i; |
4390 | ||
1307df1a JF |
4391 | for (i = 0; i < id_refs_size; i++) |
4392 | if (!strcmp(id, id_refs[i][0]->id)) | |
4393 | return id_refs[i]; | |
4394 | ||
4395 | tmp_id_refs = realloc(id_refs, (id_refs_size + 1) * sizeof(*id_refs)); | |
4396 | if (!tmp_id_refs) | |
4397 | return NULL; | |
4398 | ||
4399 | id_refs = tmp_id_refs; | |
4400 | ||
c34d9c9f JF |
4401 | for (i = 0; i < refs_size; i++) { |
4402 | struct ref **tmp; | |
4403 | ||
4404 | if (strcmp(id, refs[i].id)) | |
4405 | continue; | |
4406 | ||
1307df1a | 4407 | tmp = realloc(ref_list, (ref_list_size + 1) * sizeof(*ref_list)); |
c34d9c9f | 4408 | if (!tmp) { |
1307df1a JF |
4409 | if (ref_list) |
4410 | free(ref_list); | |
c34d9c9f JF |
4411 | return NULL; |
4412 | } | |
4413 | ||
1307df1a JF |
4414 | ref_list = tmp; |
4415 | if (ref_list_size > 0) | |
4416 | ref_list[ref_list_size - 1]->next = 1; | |
4417 | ref_list[ref_list_size] = &refs[i]; | |
3af8774e JF |
4418 | |
4419 | /* XXX: The properties of the commit chains ensures that we can | |
4420 | * safely modify the shared ref. The repo references will | |
4421 | * always be similar for the same id. */ | |
1307df1a JF |
4422 | ref_list[ref_list_size]->next = 0; |
4423 | ref_list_size++; | |
c34d9c9f JF |
4424 | } |
4425 | ||
1307df1a JF |
4426 | if (ref_list) |
4427 | id_refs[id_refs_size++] = ref_list; | |
4428 | ||
4429 | return ref_list; | |
c34d9c9f JF |
4430 | } |
4431 | ||
4432 | static int | |
5699e0cf | 4433 | read_ref(char *id, size_t idlen, char *name, size_t namelen) |
c34d9c9f | 4434 | { |
d0cea5f9 JF |
4435 | struct ref *ref; |
4436 | bool tag = FALSE; | |
e15ec88e | 4437 | bool remote = FALSE; |
d0cea5f9 | 4438 | |
8b0297ae JF |
4439 | if (!strncmp(name, "refs/tags/", STRING_SIZE("refs/tags/"))) { |
4440 | /* Commits referenced by tags has "^{}" appended. */ | |
4441 | if (name[namelen - 1] != '}') | |
4442 | return OK; | |
4443 | ||
d0cea5f9 JF |
4444 | while (namelen > 0 && name[namelen] != '^') |
4445 | namelen--; | |
c34d9c9f | 4446 | |
d0cea5f9 | 4447 | tag = TRUE; |
8b0297ae JF |
4448 | namelen -= STRING_SIZE("refs/tags/"); |
4449 | name += STRING_SIZE("refs/tags/"); | |
c34d9c9f | 4450 | |
e15ec88e JF |
4451 | } else if (!strncmp(name, "refs/remotes/", STRING_SIZE("refs/remotes/"))) { |
4452 | remote = TRUE; | |
4453 | namelen -= STRING_SIZE("refs/remotes/"); | |
4454 | name += STRING_SIZE("refs/remotes/"); | |
4455 | ||
d0cea5f9 | 4456 | } else if (!strncmp(name, "refs/heads/", STRING_SIZE("refs/heads/"))) { |
8b0297ae JF |
4457 | namelen -= STRING_SIZE("refs/heads/"); |
4458 | name += STRING_SIZE("refs/heads/"); | |
c34d9c9f | 4459 | |
d0cea5f9 JF |
4460 | } else if (!strcmp(name, "HEAD")) { |
4461 | return OK; | |
4462 | } | |
6706b2ba | 4463 | |
d0cea5f9 JF |
4464 | refs = realloc(refs, sizeof(*refs) * (refs_size + 1)); |
4465 | if (!refs) | |
4466 | return ERR; | |
c34d9c9f | 4467 | |
d0cea5f9 | 4468 | ref = &refs[refs_size++]; |
8b0297ae | 4469 | ref->name = malloc(namelen + 1); |
d0cea5f9 JF |
4470 | if (!ref->name) |
4471 | return ERR; | |
3af8774e | 4472 | |
8b0297ae JF |
4473 | strncpy(ref->name, name, namelen); |
4474 | ref->name[namelen] = 0; | |
d0cea5f9 | 4475 | ref->tag = tag; |
e15ec88e | 4476 | ref->remote = remote; |
2463b4ea | 4477 | string_copy_rev(ref->id, id); |
3af8774e | 4478 | |
d0cea5f9 JF |
4479 | return OK; |
4480 | } | |
c34d9c9f | 4481 | |
d0cea5f9 JF |
4482 | static int |
4483 | load_refs(void) | |
4484 | { | |
4485 | const char *cmd_env = getenv("TIG_LS_REMOTE"); | |
4486 | const char *cmd = cmd_env && *cmd_env ? cmd_env : TIG_LS_REMOTE; | |
c34d9c9f | 4487 | |
4a63c884 | 4488 | return read_properties(popen(cmd, "r"), "\t", read_ref); |
d0cea5f9 | 4489 | } |
c34d9c9f | 4490 | |
d0cea5f9 | 4491 | static int |
5699e0cf | 4492 | read_repo_config_option(char *name, size_t namelen, char *value, size_t valuelen) |
d0cea5f9 | 4493 | { |
22913179 | 4494 | if (!strcmp(name, "i18n.commitencoding")) |
739e81de | 4495 | string_ncopy(opt_encoding, value, valuelen); |
c34d9c9f | 4496 | |
0cea0d43 JF |
4497 | if (!strcmp(name, "core.editor")) |
4498 | string_ncopy(opt_editor, value, valuelen); | |
4499 | ||
c34d9c9f JF |
4500 | return OK; |
4501 | } | |
4502 | ||
4670cf89 | 4503 | static int |
14c778a6 | 4504 | load_repo_config(void) |
4670cf89 | 4505 | { |
96e58f5b | 4506 | return read_properties(popen(GIT_CONFIG " --list", "r"), |
14c778a6 | 4507 | "=", read_repo_config_option); |
d0cea5f9 JF |
4508 | } |
4509 | ||
4510 | static int | |
5699e0cf | 4511 | read_repo_info(char *name, size_t namelen, char *value, size_t valuelen) |
91c5d983 | 4512 | { |
810f0078 JF |
4513 | if (!opt_git_dir[0]) |
4514 | string_ncopy(opt_git_dir, name, namelen); | |
4515 | else | |
739e81de | 4516 | string_ncopy(opt_cdup, name, namelen); |
91c5d983 JF |
4517 | return OK; |
4518 | } | |
4519 | ||
810f0078 JF |
4520 | /* XXX: The line outputted by "--show-cdup" can be empty so the option |
4521 | * must be the last one! */ | |
91c5d983 JF |
4522 | static int |
4523 | load_repo_info(void) | |
4524 | { | |
e0f50df0 | 4525 | return read_properties(popen("git rev-parse --git-dir --show-cdup 2>/dev/null", "r"), |
91c5d983 JF |
4526 | "=", read_repo_info); |
4527 | } | |
4528 | ||
4529 | static int | |
4a63c884 | 4530 | read_properties(FILE *pipe, const char *separators, |
5699e0cf | 4531 | int (*read_property)(char *, size_t, char *, size_t)) |
d0cea5f9 | 4532 | { |
4670cf89 JF |
4533 | char buffer[BUFSIZ]; |
4534 | char *name; | |
d0cea5f9 | 4535 | int state = OK; |
4670cf89 JF |
4536 | |
4537 | if (!pipe) | |
4538 | return ERR; | |
4539 | ||
d0cea5f9 | 4540 | while (state == OK && (name = fgets(buffer, sizeof(buffer), pipe))) { |
4a63c884 JF |
4541 | char *value; |
4542 | size_t namelen; | |
4543 | size_t valuelen; | |
4670cf89 | 4544 | |
4a63c884 JF |
4545 | name = chomp_string(name); |
4546 | namelen = strcspn(name, separators); | |
4547 | ||
4548 | if (name[namelen]) { | |
4549 | name[namelen] = 0; | |
4550 | value = chomp_string(name + namelen + 1); | |
d0cea5f9 | 4551 | valuelen = strlen(value); |
4670cf89 | 4552 | |
d0cea5f9 | 4553 | } else { |
d0cea5f9 JF |
4554 | value = ""; |
4555 | valuelen = 0; | |
4670cf89 | 4556 | } |
d0cea5f9 | 4557 | |
3c3801c2 | 4558 | state = read_property(name, namelen, value, valuelen); |
4670cf89 JF |
4559 | } |
4560 | ||
d0cea5f9 JF |
4561 | if (state != ERR && ferror(pipe)) |
4562 | state = ERR; | |
4670cf89 JF |
4563 | |
4564 | pclose(pipe); | |
4565 | ||
d0cea5f9 | 4566 | return state; |
4670cf89 JF |
4567 | } |
4568 | ||
d0cea5f9 | 4569 | |
6b161b31 JF |
4570 | /* |
4571 | * Main | |
4572 | */ | |
4573 | ||
b5c9e67f | 4574 | static void __NORETURN |
6b161b31 JF |
4575 | quit(int sig) |
4576 | { | |
8855ada4 JF |
4577 | /* XXX: Restore tty modes and let the OS cleanup the rest! */ |
4578 | if (cursed) | |
4579 | endwin(); | |
6b161b31 JF |
4580 | exit(0); |
4581 | } | |
4582 | ||
c6704a4e JF |
4583 | static void __NORETURN |
4584 | die(const char *err, ...) | |
6b161b31 JF |
4585 | { |
4586 | va_list args; | |
4587 | ||
4588 | endwin(); | |
4589 | ||
4590 | va_start(args, err); | |
4591 | fputs("tig: ", stderr); | |
4592 | vfprintf(stderr, err, args); | |
4593 | fputs("\n", stderr); | |
4594 | va_end(args); | |
4595 | ||
4596 | exit(1); | |
4597 | } | |
4598 | ||
4599 | int | |
4600 | main(int argc, char *argv[]) | |
4601 | { | |
1ba2ae4b | 4602 | struct view *view; |
6b161b31 | 4603 | enum request request; |
1ba2ae4b | 4604 | size_t i; |
6b161b31 JF |
4605 | |
4606 | signal(SIGINT, quit); | |
4607 | ||
6b68fd24 | 4608 | if (setlocale(LC_ALL, "")) { |
739e81de JF |
4609 | char *codeset = nl_langinfo(CODESET); |
4610 | ||
4611 | string_ncopy(opt_codeset, codeset, strlen(codeset)); | |
6b68fd24 JF |
4612 | } |
4613 | ||
e0f50df0 JF |
4614 | if (load_repo_info() == ERR) |
4615 | die("Failed to load repo info."); | |
4616 | ||
4617 | /* Require a git repository unless when running in pager mode. */ | |
4618 | if (!opt_git_dir[0]) | |
4619 | die("Not a git repository"); | |
4620 | ||
660e09ad JF |
4621 | if (load_options() == ERR) |
4622 | die("Failed to load user config."); | |
4623 | ||
4624 | /* Load the repo config file so options can be overwritten from | |
739e81de | 4625 | * the command line. */ |
14c778a6 | 4626 | if (load_repo_config() == ERR) |
afdc35b3 | 4627 | die("Failed to load repo config."); |
91c5d983 | 4628 | |
8855ada4 | 4629 | if (!parse_options(argc, argv)) |
6b161b31 JF |
4630 | return 0; |
4631 | ||
6b68fd24 JF |
4632 | if (*opt_codeset && strcmp(opt_codeset, opt_encoding)) { |
4633 | opt_iconv = iconv_open(opt_codeset, opt_encoding); | |
20f4b4a3 | 4634 | if (opt_iconv == ICONV_NONE) |
6b68fd24 JF |
4635 | die("Failed to initialize character set conversion"); |
4636 | } | |
4637 | ||
c34d9c9f JF |
4638 | if (load_refs() == ERR) |
4639 | die("Failed to load refs."); | |
4640 | ||
1ba2ae4b JF |
4641 | for (i = 0; i < ARRAY_SIZE(views) && (view = &views[i]); i++) |
4642 | view->cmd_env = getenv(view->cmd_env); | |
4643 | ||
6b161b31 JF |
4644 | request = opt_request; |
4645 | ||
4646 | init_display(); | |
b801d8b2 JF |
4647 | |
4648 | while (view_driver(display[current_view], request)) { | |
6b161b31 | 4649 | int key; |
b801d8b2 JF |
4650 | int i; |
4651 | ||
699ae55b | 4652 | foreach_view (view, i) |
6b161b31 | 4653 | update_view(view); |
b801d8b2 JF |
4654 | |
4655 | /* Refresh, accept single keystroke of input */ | |
6b161b31 | 4656 | key = wgetch(status_win); |
04e2b7b2 | 4657 | |
cf4d82e6 JF |
4658 | /* wgetch() with nodelay() enabled returns ERR when there's no |
4659 | * input. */ | |
4660 | if (key == ERR) { | |
4661 | request = REQ_NONE; | |
8b534a13 | 4662 | continue; |
cf4d82e6 | 4663 | } |
04e2b7b2 JF |
4664 | |
4665 | request = get_keybinding(display[current_view]->keymap, key); | |
03a93dbb | 4666 | |
6706b2ba | 4667 | /* Some low-level request handling. This keeps access to |
fac7db6c JF |
4668 | * status_win restricted. */ |
4669 | switch (request) { | |
4670 | case REQ_PROMPT: | |
9e21ce5c JF |
4671 | { |
4672 | char *cmd = read_prompt(":"); | |
4673 | ||
4674 | if (cmd && string_format(opt_cmd, "git %s", cmd)) { | |
4675 | if (strncmp(cmd, "show", 4) && isspace(cmd[4])) { | |
4676 | opt_request = REQ_VIEW_DIFF; | |
4677 | } else { | |
4678 | opt_request = REQ_VIEW_PAGER; | |
4679 | } | |
4680 | break; | |
4681 | } | |
fac7db6c | 4682 | |
1d754561 | 4683 | request = REQ_NONE; |
9e21ce5c JF |
4684 | break; |
4685 | } | |
4af34daa JF |
4686 | case REQ_SEARCH: |
4687 | case REQ_SEARCH_BACK: | |
4688 | { | |
4689 | const char *prompt = request == REQ_SEARCH | |
4690 | ? "/" : "?"; | |
4691 | char *search = read_prompt(prompt); | |
4692 | ||
4693 | if (search) | |
739e81de | 4694 | string_ncopy(opt_search, search, strlen(search)); |
4af34daa JF |
4695 | else |
4696 | request = REQ_NONE; | |
4697 | break; | |
4698 | } | |
fac7db6c JF |
4699 | case REQ_SCREEN_RESIZE: |
4700 | { | |
4701 | int height, width; | |
4702 | ||
4703 | getmaxyx(stdscr, height, width); | |
4704 | ||
4705 | /* Resize the status view and let the view driver take | |
4706 | * care of resizing the displayed views. */ | |
4707 | wresize(status_win, 1, width); | |
4708 | mvwin(status_win, height - 1, 0); | |
4709 | wrefresh(status_win); | |
4710 | break; | |
4711 | } | |
4712 | default: | |
4713 | break; | |
03a93dbb | 4714 | } |
b801d8b2 JF |
4715 | } |
4716 | ||
4717 | quit(0); | |
4718 | ||
4719 | return 0; | |
4720 | } |