1 /* Copyright (c) 2006-2014 Jonas Fonseca <jonas.fonseca@gmail.com>
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.
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.
17 #include "tig/watch.h"
18 #include "tig/options.h"
21 #include "tig/display.h"
28 goto_view_line(struct view
*view
, unsigned long offset
, unsigned long lineno
)
30 if (lineno
>= view
->lines
)
31 lineno
= view
->lines
> 0 ? view
->lines
- 1 : 0;
33 if (offset
> lineno
|| offset
+ view
->height
<= lineno
) {
34 unsigned long half
= view
->height
/ 2;
37 offset
= lineno
- half
;
42 if (offset
!= view
->pos
.offset
|| lineno
!= view
->pos
.lineno
) {
43 view
->pos
.offset
= offset
;
44 view
->pos
.lineno
= lineno
;
51 /* Scrolling backend */
53 do_scroll_view(struct view
*view
, int lines
)
55 bool redraw_current_line
= FALSE
;
57 /* The rendering expects the new offset. */
58 view
->pos
.offset
+= lines
;
60 assert(0 <= view
->pos
.offset
&& view
->pos
.offset
< view
->lines
);
63 /* Move current line into the view. */
64 if (view
->pos
.lineno
< view
->pos
.offset
) {
65 view
->pos
.lineno
= view
->pos
.offset
;
66 redraw_current_line
= TRUE
;
67 } else if (view
->pos
.lineno
>= view
->pos
.offset
+ view
->height
) {
68 view
->pos
.lineno
= view
->pos
.offset
+ view
->height
- 1;
69 redraw_current_line
= TRUE
;
72 assert(view
->pos
.offset
<= view
->pos
.lineno
&& view
->pos
.lineno
< view
->lines
);
74 /* Redraw the whole screen if scrolling is pointless. */
75 if (view
->height
< ABS(lines
)) {
79 int line
= lines
> 0 ? view
->height
- lines
: 0;
80 int end
= line
+ ABS(lines
);
82 scrollok(view
->win
, TRUE
);
83 wscrl(view
->win
, lines
);
84 scrollok(view
->win
, FALSE
);
86 while (line
< end
&& draw_view_line(view
, line
))
89 if (redraw_current_line
)
90 draw_view_line(view
, view
->pos
.lineno
- view
->pos
.offset
);
91 wnoutrefresh(view
->win
);
94 view
->has_scrolled
= TRUE
;
100 scroll_view(struct view
*view
, enum request request
)
104 assert(view_is_displayed(view
));
106 if (request
== REQ_SCROLL_WHEEL_DOWN
|| request
== REQ_SCROLL_WHEEL_UP
)
107 lines
= opt_mouse_scroll
;
110 case REQ_SCROLL_FIRST_COL
:
112 redraw_view_from(view
, 0);
115 case REQ_SCROLL_LEFT
:
116 if (view
->pos
.col
== 0) {
117 report("Cannot scroll beyond the first column");
120 if (view
->pos
.col
<= apply_step(opt_horizontal_scroll
, view
->width
))
123 view
->pos
.col
-= apply_step(opt_horizontal_scroll
, view
->width
);
124 redraw_view_from(view
, 0);
127 case REQ_SCROLL_RIGHT
:
128 view
->pos
.col
+= apply_step(opt_horizontal_scroll
, view
->width
);
132 case REQ_SCROLL_PAGE_DOWN
:
133 lines
= view
->height
;
134 case REQ_SCROLL_WHEEL_DOWN
:
135 case REQ_SCROLL_LINE_DOWN
:
136 if (view
->pos
.offset
+ lines
> view
->lines
)
137 lines
= view
->lines
- view
->pos
.offset
;
139 if (lines
== 0 || view
->pos
.offset
+ view
->height
>= view
->lines
) {
140 report("Cannot scroll beyond the last line");
145 case REQ_SCROLL_PAGE_UP
:
146 lines
= view
->height
;
147 case REQ_SCROLL_LINE_UP
:
148 case REQ_SCROLL_WHEEL_UP
:
149 if (lines
> view
->pos
.offset
)
150 lines
= view
->pos
.offset
;
153 report("Cannot scroll beyond the first line");
161 die("request %d not handled in switch", request
);
164 do_scroll_view(view
, lines
);
169 move_view(struct view
*view
, enum request request
)
171 int scroll_steps
= 0;
175 case REQ_MOVE_FIRST_LINE
:
176 steps
= -view
->pos
.lineno
;
179 case REQ_MOVE_LAST_LINE
:
180 steps
= view
->lines
- view
->pos
.lineno
- 1;
183 case REQ_MOVE_PAGE_UP
:
184 steps
= view
->height
> view
->pos
.lineno
185 ? -view
->pos
.lineno
: -view
->height
;
188 case REQ_MOVE_PAGE_DOWN
:
189 steps
= view
->pos
.lineno
+ view
->height
>= view
->lines
190 ? view
->lines
- view
->pos
.lineno
- 1 : view
->height
;
204 die("request %d not handled in switch", request
);
207 if (steps
<= 0 && view
->pos
.lineno
== 0) {
208 report("Cannot move beyond the first line");
211 } else if (steps
>= 0 && view
->pos
.lineno
+ 1 >= view
->lines
) {
212 report("Cannot move beyond the last line");
216 /* Move the current line */
217 view
->pos
.lineno
+= steps
;
218 assert(0 <= view
->pos
.lineno
&& view
->pos
.lineno
< view
->lines
);
220 /* Check whether the view needs to be scrolled */
221 if (view
->pos
.lineno
< view
->pos
.offset
||
222 view
->pos
.lineno
>= view
->pos
.offset
+ view
->height
) {
223 scroll_steps
= steps
;
224 if (steps
< 0 && -steps
> view
->pos
.offset
) {
225 scroll_steps
= -view
->pos
.offset
;
227 } else if (steps
> 0) {
228 if (view
->pos
.lineno
== view
->lines
- 1 &&
229 view
->lines
> view
->height
) {
230 scroll_steps
= view
->lines
- view
->pos
.offset
- 1;
231 if (scroll_steps
>= view
->height
)
232 scroll_steps
-= view
->height
- 1;
237 if (!view_is_displayed(view
)) {
238 view
->pos
.offset
+= scroll_steps
;
239 assert(0 <= view
->pos
.offset
&& view
->pos
.offset
< view
->lines
);
240 view
->ops
->select(view
, &view
->line
[view
->pos
.lineno
]);
244 /* Repaint the old "current" line if we be scrolling */
245 if (ABS(steps
) < view
->height
)
246 draw_view_line(view
, view
->pos
.lineno
- steps
- view
->pos
.offset
);
249 do_scroll_view(view
, scroll_steps
);
253 /* Draw the current line */
254 draw_view_line(view
, view
->pos
.lineno
- view
->pos
.offset
);
256 wnoutrefresh(view
->win
);
264 DEFINE_ALLOCATOR(realloc_unsigned_ints
, unsigned int, 32)
267 grep_text(struct view
*view
, const char *text
[])
272 for (i
= 0; text
[i
]; i
++)
273 if (*text
[i
] && !regexec(view
->regex
, text
[i
], 1, &pmatch
, 0))
279 select_view_line(struct view
*view
, unsigned long lineno
)
281 struct position old
= view
->pos
;
283 if (goto_view_line(view
, view
->pos
.offset
, lineno
)) {
284 if (view_is_displayed(view
)) {
285 if (old
.offset
!= view
->pos
.offset
) {
288 draw_view_line(view
, old
.lineno
- view
->pos
.offset
);
289 draw_view_line(view
, view
->pos
.lineno
- view
->pos
.offset
);
290 wnoutrefresh(view
->win
);
293 view
->ops
->select(view
, &view
->line
[view
->pos
.lineno
]);
299 find_matches(struct view
*view
)
303 /* Note, lineno is unsigned long so will wrap around in which case it
304 * will become bigger than view->lines. */
305 for (lineno
= 0; lineno
< view
->lines
; lineno
++) {
306 if (!view
->ops
->grep(view
, &view
->line
[lineno
]))
309 if (!realloc_unsigned_ints(&view
->matched_line
, view
->matched_lines
, 1))
312 view
->matched_line
[view
->matched_lines
++] = lineno
;
319 find_next(struct view
*view
, enum request request
)
325 if (!*view
->env
->search
)
326 report("No previous search");
328 search_view(view
, request
);
338 case REQ_SEARCH_BACK
:
347 if (!view
->matched_lines
&& !find_matches(view
)) {
348 report("Allocation failure");
352 /* Note, `i` is unsigned and will wrap around in which case it
353 * will become bigger than view->matched_lines. */
354 i
= direction
> 0 ? 0 : view
->matched_lines
- 1;
355 for (; i
< view
->matched_lines
; i
+= direction
) {
356 size_t lineno
= view
->matched_line
[i
];
358 if (direction
> 0 && lineno
<= view
->pos
.lineno
)
361 if (direction
< 0 && lineno
>= view
->pos
.lineno
)
364 select_view_line(view
, lineno
);
365 report("Line %zu matches '%s' (%zu of %zu)", lineno
+ 1, view
->grep
, i
+ 1, view
->matched_lines
);
369 report("No match found for '%s'", view
->grep
);
373 reset_matches(struct view
*view
)
375 free(view
->matched_line
);
376 view
->matched_line
= NULL
;
377 view
->matched_lines
= 0;
381 search_view(struct view
*view
, enum request request
)
384 int regex_flags
= opt_ignore_case
? REG_ICASE
: 0;
387 regfree(view
->regex
);
390 view
->regex
= calloc(1, sizeof(*view
->regex
));
395 regex_err
= regcomp(view
->regex
, view
->env
->search
, REG_EXTENDED
| regex_flags
);
396 if (regex_err
!= 0) {
397 char buf
[SIZEOF_STR
] = "unknown error";
399 regerror(regex_err
, view
->regex
, buf
, sizeof(buf
));
400 report("Search failed: %s", buf
);
404 string_copy(view
->grep
, view
->env
->search
);
408 find_next(view
, request
);
416 view_history_is_empty(struct view_history
*history
)
418 return !history
->stack
;
422 push_view_history_state(struct view_history
*history
, struct position
*position
, void *data
)
424 struct view_state
*state
= history
->stack
;
426 if (state
&& data
&& history
->state_alloc
&&
427 !memcmp(state
->data
, data
, history
->state_alloc
))
430 state
= calloc(1, sizeof(*state
) + history
->state_alloc
);
434 state
->prev
= history
->stack
;
435 history
->stack
= state
;
436 clear_position(&history
->position
);
437 state
->position
= *position
;
438 state
->data
= &state
[1];
439 if (data
&& history
->state_alloc
)
440 memcpy(state
->data
, data
, history
->state_alloc
);
445 pop_view_history_state(struct view_history
*history
, struct position
*position
, void *data
)
447 struct view_state
*state
= history
->stack
;
449 if (view_history_is_empty(history
))
452 history
->position
= state
->position
;
453 history
->stack
= state
->prev
;
455 if (data
&& history
->state_alloc
)
456 memcpy(data
, state
->data
, history
->state_alloc
);
458 *position
= state
->position
;
465 reset_view_history(struct view_history
*history
)
467 while (pop_view_history_state(history
, NULL
, NULL
))
472 * Incremental updating
476 reset_view(struct view
*view
)
480 for (i
= 0; i
< view
->lines
; i
++)
481 free(view
->line
[i
].data
);
485 view
->prev_pos
= view
->pos
;
486 clear_position(&view
->pos
);
489 view_column_reset(view
);
494 view
->custom_lines
= 0;
495 view
->update_secs
= 0;
499 restore_view_position(struct view
*view
)
501 /* A view without a previous view is the first view */
502 if (!view
->prev
&& view
->env
->lineno
&& view
->env
->lineno
<= view
->lines
) {
503 select_view_line(view
, view
->env
->lineno
);
504 view
->env
->lineno
= 0;
507 /* Ensure that the view position is in a valid state. */
508 if (!check_position(&view
->prev_pos
) ||
509 (view
->pipe
&& view
->lines
<= view
->prev_pos
.lineno
))
510 return goto_view_line(view
, view
->pos
.offset
, view
->pos
.lineno
);
512 /* Changing the view position cancels the restoring. */
513 /* FIXME: Changing back to the first line is not detected. */
514 if (check_position(&view
->pos
)) {
515 clear_position(&view
->prev_pos
);
519 if (goto_view_line(view
, view
->prev_pos
.offset
, view
->prev_pos
.lineno
) &&
520 view_is_displayed(view
))
523 view
->pos
.col
= view
->prev_pos
.col
;
524 clear_position(&view
->prev_pos
);
530 end_update(struct view
*view
, bool force
)
534 while (!view
->ops
->read(view
, NULL
))
544 setup_update(struct view
*view
, const char *vid
)
547 /* XXX: Do not use string_copy_rev(), it copies until first space. */
548 string_ncopy(view
->vid
, vid
, strlen(vid
));
549 view
->pipe
= &view
->io
;
550 view
->start_time
= time(NULL
);
554 view_no_refresh(struct view
*view
, enum open_flags flags
)
556 bool reload
= !!(flags
& OPEN_ALWAYS_LOAD
) || !view
->lines
;
558 return (!reload
&& !strcmp(view
->vid
, view
->ops
->id
)) ||
559 ((flags
& OPEN_REFRESH
) && !view_can_refresh(view
));
563 begin_update(struct view
*view
, const char *dir
, const char **argv
, enum open_flags flags
)
565 bool extra
= !!(flags
& (OPEN_EXTRA
));
566 bool refresh
= flags
& (OPEN_REFRESH
| OPEN_PREPARED
| OPEN_STDIN
);
567 int forward_stdin
= (flags
& OPEN_FORWARD_STDIN
) ? IO_RD_FORWARD_STDIN
: 0;
568 int with_stderr
= (flags
& OPEN_WITH_STDERR
) ? IO_RD_WITH_STDERR
: 0;
569 int io_flags
= forward_stdin
| with_stderr
;
571 if (view_no_refresh(view
, flags
))
578 end_update(view
, TRUE
);
581 view
->unrefreshable
= open_in_pager_mode(flags
);
583 if (!refresh
&& argv
) {
584 bool file_filter
= !view_has_flags(view
, VIEW_FILE_FILTER
) || opt_file_filter
;
587 if (!argv_format(view
->env
, &view
->argv
, argv
, !view
->prev
, file_filter
)) {
588 report("Failed to format %s arguments", view
->name
);
592 /* Put the current view ref value to the view title ref
593 * member. This is needed by the blob view. Most other
594 * views sets it automatically after loading because the
595 * first line is a commit line. */
596 string_copy_rev(view
->ref
, view
->ops
->id
);
599 if (view
->argv
&& view
->argv
[0] &&
600 !io_exec(&view
->io
, IO_RD
, view
->dir
, opt_env
, view
->argv
, io_flags
)) {
601 report("Failed to open %s view", view
->name
);
605 if (open_from_stdin(flags
)) {
606 if (!io_open(&view
->io
, "%s", ""))
607 die("Failed to open stdin");
611 setup_update(view
, view
->ops
->id
);
617 update_view(struct view
*view
)
620 /* Clear the view and redraw everything since the tree sorting
621 * might have rearranged things. */
622 bool redraw
= view
->lines
== 0;
623 bool can_read
= TRUE
;
624 struct encoding
*encoding
= view
->encoding
? view
->encoding
: default_encoding
;
629 if (!io_can_read(view
->pipe
, FALSE
)) {
630 if (view
->lines
== 0 && view_is_displayed(view
)) {
631 time_t secs
= time(NULL
) - view
->start_time
;
633 if (secs
> 1 && secs
> view
->update_secs
) {
634 if (view
->update_secs
== 0)
636 update_view_title(view
);
637 view
->update_secs
= secs
;
643 for (; (line
= io_get(view
->pipe
, '\n', can_read
)); can_read
= FALSE
) {
645 line
= encoding_convert(encoding
, line
);
648 if (!view
->ops
->read(view
, line
)) {
649 report("Allocation failure");
650 end_update(view
, TRUE
);
655 if (io_error(view
->pipe
)) {
656 report("Failed to read: %s", io_strerror(view
->pipe
));
657 end_update(view
, TRUE
);
659 } else if (io_eof(view
->pipe
)) {
660 end_update(view
, FALSE
);
663 if (restore_view_position(view
))
666 if (!view_is_displayed(view
))
669 if (redraw
|| view
->force_redraw
)
670 redraw_view_from(view
, 0);
672 redraw_view_dirty(view
);
673 view
->force_redraw
= FALSE
;
675 /* Update the title _after_ the redraw so that if the redraw picks up a
676 * commit reference in view->ref it'll be available here. */
677 update_view_title(view
);
682 update_view_title(struct view
*view
)
684 WINDOW
*window
= view
->title
;
685 struct line
*line
= &view
->line
[view
->pos
.lineno
];
686 unsigned int view_lines
, lines
;
688 assert(view_is_displayed(view
));
690 if (view
== display
[current_view
])
691 wbkgdset(window
, get_view_attr(view
, LINE_TITLE_FOCUS
));
693 wbkgdset(window
, get_view_attr(view
, LINE_TITLE_BLUR
));
696 mvwprintw(window
, 0, 0, "[%s]", view
->name
);
699 wprintw(window
, " %s", view
->ref
);
702 if (!view_has_flags(view
, VIEW_CUSTOM_STATUS
) && view_has_line(view
, line
) &&
704 wprintw(window
, " - %s %d of %zd",
707 view
->lines
- view
->custom_lines
);
711 time_t secs
= time(NULL
) - view
->start_time
;
713 /* Three git seconds are a long time ... */
715 wprintw(window
, " loading %lds", secs
);
718 view_lines
= view
->pos
.offset
+ view
->height
;
719 lines
= view
->lines
? MIN(view_lines
, view
->lines
) * 100 / view
->lines
: 0;
720 mvwprintw(window
, 0, view
->width
- count_digits(lines
) - 1, "%d%%", lines
);
722 wnoutrefresh(window
);
730 split_view(struct view
*prev
, struct view
*view
)
733 current_view
= opt_focus_child
? 1 : 0;
737 if (prev
->pos
.lineno
- prev
->pos
.offset
>= prev
->height
) {
738 /* Take the title line into account. */
739 int lines
= prev
->pos
.lineno
- prev
->pos
.offset
- prev
->height
+ 1;
741 /* Scroll the view that was split if the current line is
742 * outside the new limited view. */
743 do_scroll_view(prev
, lines
);
746 if (view
!= prev
&& view_is_displayed(prev
)) {
747 /* "Blur" the previous view. */
748 update_view_title(prev
);
753 maximize_view(struct view
*view
, bool redraw
)
755 memset(display
, 0, sizeof(display
));
757 display
[current_view
] = view
;
760 redraw_display(FALSE
);
766 load_view(struct view
*view
, struct view
*prev
, enum open_flags flags
)
768 bool refresh
= !view_no_refresh(view
, flags
);
770 /* When prev == view it means this is the first loaded view. */
771 if (prev
&& view
!= prev
) {
775 if (!refresh
&& view_can_refresh(view
) &&
776 watch_update(WATCH_EVENT_SWITCH_VIEW
)) {
777 refresh
= watch_dirty(&view
->watch
);
779 flags
|= OPEN_REFRESH
;
784 end_update(view
, TRUE
);
785 if (view
->ops
->private_size
) {
786 if (!view
->private) {
787 view
->private = calloc(1, view
->ops
->private_size
);
790 view
->ops
->done(view
);
791 memset(view
->private, 0, view
->ops
->private_size
);
795 if (!view
->ops
->open(view
, flags
))
800 bool split
= !!(flags
& OPEN_SPLIT
);
803 split_view(prev
, view
);
805 maximize_view(view
, FALSE
);
809 restore_view_position(view
);
811 if (view
->pipe
&& view
->lines
== 0) {
812 /* Clear the old view and let the incremental updating refill
815 if (!(flags
& (OPEN_RELOAD
| OPEN_REFRESH
)))
816 clear_position(&view
->prev_pos
);
818 } else if (view_is_displayed(view
)) {
824 #define refresh_view(view) load_view(view, NULL, OPEN_REFRESH)
825 #define reload_view(view) load_view(view, NULL, OPEN_RELOAD)
828 open_view(struct view
*prev
, struct view
*view
, enum open_flags flags
)
830 bool reload
= !!(flags
& (OPEN_RELOAD
| OPEN_PREPARED
));
831 int nviews
= displayed_views();
833 assert(flags
^ OPEN_REFRESH
);
835 if (view
== prev
&& nviews
== 1 && !reload
) {
836 report("Already in %s view", view
->name
);
840 if (!view_has_flags(view
, VIEW_NO_GIT_DIR
) && !repo
.git_dir
[0]) {
841 report("The %s view is disabled in pager mode", view
->name
);
846 view
->keymap
= get_keymap(view
->name
, strlen(view
->name
));
847 load_view(view
, prev
? prev
: view
, flags
);
851 open_argv(struct view
*prev
, struct view
*view
, const char *argv
[], const char *dir
, enum open_flags flags
)
854 end_update(view
, TRUE
);
857 if (!argv_copy(&view
->argv
, argv
)) {
858 report("Failed to open %s view: %s", view
->name
, io_strerror(&view
->io
));
860 open_view(prev
, view
, flags
| OPEN_PREPARED
);
868 static struct view
*sorting_view
;
870 #define sort_order_reverse(state, result) \
871 ((state)->reverse ? -(result) : (result))
873 #define sort_order(state, cmp, o1, o2) \
874 sort_order_reverse(state, (!(o1) || !(o2)) ? !!(o2) - !!(o1) : cmp(o1, o2))
876 #define number_compare(size1, size2) (*(size1) - *(size2))
878 #define mode_is_dir(mode) ((mode) && S_ISDIR(*(mode)))
881 sort_view_compare(const void *l1
, const void *l2
)
883 const struct line
*line1
= l1
;
884 const struct line
*line2
= l2
;
885 struct view_column_data column_data1
= {};
886 struct view_column_data column_data2
= {};
887 struct sort_state
*sort
= &sorting_view
->sort
;
889 if (!sorting_view
->ops
->get_column_data(sorting_view
, line1
, &column_data1
))
891 else if (!sorting_view
->ops
->get_column_data(sorting_view
, line2
, &column_data2
))
894 switch (get_sort_field(sorting_view
)) {
895 case VIEW_COLUMN_AUTHOR
:
896 return sort_order(sort
, ident_compare
, column_data1
.author
, column_data2
.author
);
898 case VIEW_COLUMN_DATE
:
899 return sort_order(sort
, timecmp
, column_data1
.date
, column_data2
.date
);
902 if (column_data1
.reflog
&& column_data2
.reflog
)
903 return sort_order(sort
, strcmp
, column_data1
.reflog
, column_data2
.reflog
);
904 return sort_order(sort
, strcmp
, column_data1
.id
, column_data2
.id
);
906 case VIEW_COLUMN_FILE_NAME
:
907 if (mode_is_dir(column_data1
.mode
) != mode_is_dir(column_data2
.mode
))
908 return sort_order_reverse(sort
, mode_is_dir(column_data1
.mode
) ? -1 : 1);
909 return sort_order(sort
, strcmp
, column_data1
.file_name
, column_data2
.file_name
);
911 case VIEW_COLUMN_FILE_SIZE
:
912 return sort_order(sort
, number_compare
, column_data1
.file_size
, column_data2
.file_size
);
914 case VIEW_COLUMN_LINE_NUMBER
:
915 return sort_order_reverse(sort
, line1
->lineno
- line2
->lineno
);
917 case VIEW_COLUMN_MODE
:
918 return sort_order(sort
, number_compare
, column_data1
.mode
, column_data2
.mode
);
920 case VIEW_COLUMN_REF
:
921 return sort_order(sort
, ref_compare
, column_data1
.ref
, column_data2
.ref
);
923 case VIEW_COLUMN_COMMIT_TITLE
:
924 return sort_order(sort
, strcmp
, column_data1
.commit_title
, column_data2
.commit_title
);
926 case VIEW_COLUMN_SECTION
:
927 return sort_order(sort
, strcmp
, column_data1
.section
->opt
.section
.text
,
928 column_data2
.section
->opt
.section
.text
);
930 case VIEW_COLUMN_STATUS
:
931 return sort_order(sort
, number_compare
, column_data1
.status
, column_data2
.status
);
933 case VIEW_COLUMN_TEXT
:
934 return sort_order(sort
, strcmp
, column_data1
.text
, column_data2
.text
);
941 sort_view(struct view
*view
, bool change_field
)
943 struct sort_state
*state
= &view
->sort
;
947 state
->current
= state
->current
->next
948 ? state
->current
->next
: view
->columns
;
949 if (get_sort_field(view
) == VIEW_COLUMN_ID
&&
950 !state
->current
->opt
.id
.display
)
955 state
->reverse
= !state
->reverse
;
959 qsort(view
->line
, view
->lines
, sizeof(*view
->line
), sort_view_compare
);
963 view_column_text(struct view
*view
, struct view_column_data
*column_data
,
964 struct view_column
*column
)
966 const char *text
= "";
968 switch (column
->type
) {
969 case VIEW_COLUMN_AUTHOR
:
970 if (column_data
->author
)
971 text
= mkauthor(column_data
->author
, column
->opt
.author
.width
, column
->opt
.author
.display
);
974 case VIEW_COLUMN_COMMIT_TITLE
:
975 text
= column_data
->commit_title
;
978 case VIEW_COLUMN_DATE
:
979 if (column_data
->date
)
980 text
= mkdate(column_data
->date
, column
->opt
.date
.display
);
983 case VIEW_COLUMN_REF
:
984 if (column_data
->ref
)
985 text
= column_data
->ref
->name
;
988 case VIEW_COLUMN_FILE_NAME
:
989 if (column_data
->file_name
)
990 text
= column_data
->file_name
;
993 case VIEW_COLUMN_FILE_SIZE
:
994 if (column_data
->file_size
)
995 text
= mkfilesize(*column_data
->file_size
, column
->opt
.file_size
.display
);
999 if (column
->opt
.id
.display
)
1000 text
= column_data
->reflog
? column_data
->reflog
: column_data
->id
;
1003 case VIEW_COLUMN_LINE_NUMBER
:
1006 case VIEW_COLUMN_MODE
:
1007 if (column_data
->mode
)
1008 text
= mkmode(*column_data
->mode
);
1011 case VIEW_COLUMN_STATUS
:
1012 if (column_data
->status
)
1013 text
= mkstatus(*column_data
->status
, column
->opt
.status
.display
);
1016 case VIEW_COLUMN_SECTION
:
1017 text
= column_data
->section
->opt
.section
.text
;
1020 case VIEW_COLUMN_TEXT
:
1021 text
= column_data
->text
;
1025 return text
? text
: "";
1029 grep_refs(struct view
*view
, struct view_column
*column
, const struct ref_list
*list
)
1037 for (i
= 0; i
< list
->size
; i
++) {
1038 if (!regexec(view
->regex
, list
->refs
[i
]->name
, 1, &pmatch
, 0))
1046 view_column_grep(struct view
*view
, struct line
*line
)
1048 struct view_column_data column_data
= {};
1049 bool ok
= view
->ops
->get_column_data(view
, line
, &column_data
);
1050 struct view_column
*column
;
1055 for (column
= view
->columns
; column
; column
= column
->next
) {
1056 const char *text
[] = {
1057 view_column_text(view
, &column_data
, column
),
1061 if (grep_text(view
, text
))
1064 if (column
->type
== VIEW_COLUMN_COMMIT_TITLE
&&
1065 column
->opt
.commit_title
.refs
&&
1066 grep_refs(view
, column
, column_data
.refs
))
1074 view_column_info_changed(struct view
*view
, bool update
)
1076 struct view_column
*column
;
1077 bool changed
= FALSE
;
1079 for (column
= view
->columns
; column
; column
= column
->next
) {
1080 if (memcmp(&column
->prev_opt
, &column
->opt
, sizeof(column
->opt
))) {
1083 column
->prev_opt
= column
->opt
;
1092 view_column_reset(struct view
*view
)
1094 struct view_column
*column
;
1096 view_column_info_changed(view
, TRUE
);
1097 for (column
= view
->columns
; column
; column
= column
->next
)
1101 static enum status_code
1102 parse_view_column_config(char **pos
, const char **name
, const char **value
, bool first
)
1104 size_t len
= strcspn(*pos
, ",");
1107 if (strlen(*pos
) > len
)
1109 optlen
= strcspn(*pos
, ":=");
1114 if (optlen
== len
) {
1115 *value
= len
? *pos
: "yes";
1120 /* Fake boolean enum value. */
1129 *value
= *pos
+ optlen
+ 1;
1136 static enum status_code
1137 parse_view_column_option(struct view_column
*column
,
1138 const char *opt_name
, const char *opt_value
)
1140 #define DEFINE_COLUMN_OPTION_INFO(name, type, flags) \
1141 { #name, STRING_SIZE(#name), #type, &opt->name },
1143 #define DEFINE_COLUMN_OPTIONS_PARSE(name, id, options) \
1144 if (column->type == VIEW_COLUMN_##id) { \
1145 struct name##_options *opt = &column->opt.name; \
1146 struct option_info info[] = { \
1147 options(DEFINE_COLUMN_OPTION_INFO) \
1149 struct option_info *option = find_option_info(info, ARRAY_SIZE(info), opt_name); \
1151 return error("Unknown option `%s' for column %s", opt_name, \
1152 view_column_name(VIEW_COLUMN_##id)); \
1153 return parse_option(option, #name, opt_value); \
1156 COLUMN_OPTIONS(DEFINE_COLUMN_OPTIONS_PARSE
);
1158 return error("Unknown view column option: %s", opt_name
);
1161 static enum status_code
1162 parse_view_column_type(struct view_column
*column
, const char **arg
)
1164 enum view_column_type type
;
1165 size_t typelen
= strcspn(*arg
, ":,");
1167 for (type
= 0; type
< view_column_type_map
->size
; type
++)
1168 if (enum_equals(view_column_type_map
->entries
[type
], *arg
, typelen
)) {
1169 *arg
+= typelen
+ !!(*arg
)[typelen
];
1170 column
->type
= type
;
1174 return error("Failed to parse view column type: %.*s", (int) typelen
, *arg
);
1177 static struct view
*
1178 find_view(const char *view_name
)
1183 foreach_view(view
, i
)
1184 if (!strncmp(view_name
, view
->name
, strlen(view
->name
)))
1191 parse_view_config(const char *view_name
, const char *argv
[])
1193 enum status_code code
= SUCCESS
;
1194 size_t size
= argv_size(argv
);
1195 struct view_column
*columns
;
1196 struct view_column
*column
;
1197 struct view
*view
= find_view(view_name
);
1201 return error("Unknown view: %s", view_name
);
1203 columns
= calloc(size
, sizeof(*columns
));
1205 return ERROR_OUT_OF_MEMORY
;
1207 for (i
= 0, column
= NULL
; code
== SUCCESS
&& i
< size
; i
++) {
1208 const char *arg
= argv
[i
];
1209 char buf
[SIZEOF_STR
] = "";
1213 column
->next
= &columns
[i
];
1214 column
= &columns
[i
];
1216 code
= parse_view_column_type(column
, &arg
);
1217 if (code
!= SUCCESS
)
1220 if (!(view
->ops
->column_bits
& (1 << column
->type
)))
1221 return error("The %s view does not support %s column", view
->name
,
1222 view_column_name(column
->type
));
1224 if ((column
->type
== VIEW_COLUMN_TEXT
||
1225 column
->type
== VIEW_COLUMN_COMMIT_TITLE
) &&
1227 return error("The %s column must always be last",
1228 view_column_name(column
->type
));
1230 string_ncopy(buf
, arg
, strlen(arg
));
1232 for (pos
= buf
, end
= pos
+ strlen(pos
); code
== SUCCESS
&& pos
<= end
; ) {
1233 const char *name
= NULL
;
1234 const char *value
= NULL
;
1236 code
= parse_view_column_config(&pos
, &name
, &value
, buf
== pos
);
1237 if (code
== SUCCESS
)
1238 code
= parse_view_column_option(column
, name
, value
);
1241 column
->prev_opt
= column
->opt
;
1244 if (code
== SUCCESS
) {
1245 free(view
->columns
);
1246 view
->columns
= columns
;
1247 view
->sort
.current
= view
->columns
;
1255 struct view_column
*
1256 get_view_column(struct view
*view
, enum view_column_type type
)
1258 struct view_column
*column
;
1260 for (column
= view
->columns
; column
; column
= column
->next
)
1261 if (column
->type
== type
)
1267 view_column_info_update(struct view
*view
, struct line
*line
)
1269 struct view_column_data column_data
= {};
1270 struct view_column
*column
;
1271 bool changed
= FALSE
;
1273 if (!view
->ops
->get_column_data(view
, line
, &column_data
))
1276 for (column
= view
->columns
; column
; column
= column
->next
) {
1277 const char *text
= view_column_text(view
, &column_data
, column
);
1280 switch (column
->type
) {
1281 case VIEW_COLUMN_AUTHOR
:
1282 width
= column
->opt
.author
.width
;
1285 case VIEW_COLUMN_COMMIT_TITLE
:
1286 width
= column
->opt
.commit_title
.width
;
1289 case VIEW_COLUMN_DATE
:
1290 width
= column
->opt
.date
.width
;
1293 case VIEW_COLUMN_FILE_NAME
:
1294 width
= column
->opt
.file_name
.width
;
1297 case VIEW_COLUMN_FILE_SIZE
:
1298 width
= column
->opt
.file_size
.width
;
1301 case VIEW_COLUMN_ID
:
1302 width
= column
->opt
.id
.width
;
1304 width
= opt_id_width
;
1305 if (!column_data
.reflog
&& !width
)
1309 case VIEW_COLUMN_LINE_NUMBER
:
1310 if (column_data
.line_number
)
1311 width
= count_digits(*column_data
.line_number
);
1313 width
= count_digits(view
->lines
);
1318 case VIEW_COLUMN_MODE
:
1319 width
= column
->opt
.mode
.width
;
1322 case VIEW_COLUMN_REF
:
1323 width
= column
->opt
.ref
.width
;
1326 case VIEW_COLUMN_SECTION
:
1329 case VIEW_COLUMN_STATUS
:
1330 width
= column
->opt
.status
.width
;
1333 case VIEW_COLUMN_TEXT
:
1334 width
= column
->opt
.text
.width
;
1338 if (*text
&& !width
)
1339 width
= utf8_width(text
);
1341 if (width
> column
->width
) {
1342 column
->width
= width
;
1348 view
->force_redraw
= TRUE
;
1353 find_line_by_type(struct view
*view
, struct line
*line
, enum line_type type
, int direction
)
1355 for (; view_has_line(view
, line
); line
+= direction
)
1356 if (line
->type
== type
)
1366 DEFINE_ALLOCATOR(realloc_lines
, struct line
, 256)
1369 add_line_at(struct view
*view
, unsigned long pos
, const void *data
, enum line_type type
, size_t data_size
, bool custom
)
1372 unsigned long lineno
;
1374 if (!realloc_lines(&view
->line
, view
->lines
, 1))
1378 void *alloc_data
= calloc(1, data_size
);
1384 memcpy(alloc_data
, data
, data_size
);
1388 if (pos
< view
->lines
) {
1390 line
= view
->line
+ pos
;
1391 lineno
= line
->lineno
;
1393 memmove(line
+ 1, line
, (view
->lines
- pos
) * sizeof(*view
->line
));
1394 while (pos
< view
->lines
) {
1395 view
->line
[pos
].lineno
++;
1396 view
->line
[pos
++].dirty
= 1;
1399 line
= &view
->line
[view
->lines
++];
1400 lineno
= view
->lines
- view
->custom_lines
;
1403 memset(line
, 0, sizeof(*line
));
1405 line
->data
= (void *) data
;
1409 view
->custom_lines
++;
1411 line
->lineno
= lineno
;
1417 add_line(struct view
*view
, const void *data
, enum line_type type
, size_t data_size
, bool custom
)
1419 return add_line_at(view
, view
->lines
, data
, type
, data_size
, custom
);
1423 add_line_alloc_(struct view
*view
, void **ptr
, enum line_type type
, size_t data_size
, bool custom
)
1425 struct line
*line
= add_line(view
, NULL
, type
, data_size
, custom
);
1433 add_line_nodata(struct view
*view
, enum line_type type
)
1435 return add_line(view
, NULL
, type
, 0, FALSE
);
1439 add_line_text(struct view
*view
, const char *text
, enum line_type type
)
1441 struct line
*line
= add_line(view
, text
, type
, strlen(text
) + 1, FALSE
);
1443 if (line
&& view
->ops
->column_bits
)
1444 view_column_info_update(view
, line
);
1448 struct line
* PRINTF_LIKE(3, 4)
1449 add_line_format(struct view
*view
, enum line_type type
, const char *fmt
, ...)
1451 char buf
[SIZEOF_STR
];
1454 FORMAT_BUFFER(buf
, sizeof(buf
), fmt
, retval
, FALSE
);
1455 return retval
>= 0 ? add_line_text(view
, buf
, type
) : NULL
;
1459 * Global view state.
1462 /* Included last to not pollute the rest of the file. */
1463 #include "tig/main.h"
1464 #include "tig/diff.h"
1465 #include "tig/log.h"
1466 #include "tig/tree.h"
1467 #include "tig/blob.h"
1468 #include "tig/blame.h"
1469 #include "tig/refs.h"
1470 #include "tig/status.h"
1471 #include "tig/stage.h"
1472 #include "tig/stash.h"
1473 #include "tig/grep.h"
1474 #include "tig/pager.h"
1475 #include "tig/help.h"
1477 static struct view
*views
[] = {
1478 #define VIEW_DATA(id, name) &name##_view
1479 VIEW_INFO(VIEW_DATA
)
1485 return 0 <= i
&& i
< ARRAY_SIZE(views
) ? views
[i
] : NULL
;
1488 /* vim: set ts=8 sw=8 noexpandtab: */