Skip to content

Commit

Permalink
Improve synctex support for beamer
Browse files Browse the repository at this point in the history
It now tries to stick to the current page, even if it is not the first
of the slide being edited, and rendering takes a long time.
  • Loading branch information
let-def committed Sep 2, 2023
1 parent d3cb110 commit 3a5e8e5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 21 deletions.
13 changes: 5 additions & 8 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,7 @@ static bool need_advance(fz_context *ctx, ui_state *ui)
synctex_t *stx = send(synctex, ui->eng, &buf);
need =
(ui->need_synctex && synctex_page_count(stx) <= ui->page) ||
(synctex_has_target(stx) &&
!synctex_find_target(ctx, stx, buf, ui->page, NULL, NULL, NULL));
synctex_has_target(stx);
}

return (need && send(get_status, ui->eng) == DOC_RUNNING);
Expand Down Expand Up @@ -424,7 +423,7 @@ static void wakeup_poll_thread(int poll_stdin_pipe[2], char c)

static void previous_page(ui_state *ui)
{
synctex_set_target(send(synctex, ui->eng, NULL), NULL, 0);
synctex_set_target(send(synctex, ui->eng, NULL), 0, NULL, 0);
if (ui->page > 0)
{
ui->page -= 1;
Expand All @@ -438,7 +437,7 @@ static void previous_page(ui_state *ui)

static void next_page(ui_state *ui)
{
synctex_set_target(send(synctex, ui->eng, NULL), NULL, 0);
synctex_set_target(send(synctex, ui->eng, NULL), 0, NULL, 0);
ui->page += 1;
schedule_event(RELOAD_EVENT);
}
Expand Down Expand Up @@ -825,7 +824,7 @@ static void interpret_command(struct persistent_state *ps,
}
else
{
synctex_set_target(stx, path, cmd.synctex_forward.line);
synctex_set_target(stx, ui->page, path, cmd.synctex_forward.line);
schedule_event(STDIN_EVENT);
}
}
Expand Down Expand Up @@ -986,8 +985,7 @@ bool texpresso_main(struct persistent_state *ps)
fz_buffer *buf;
synctex_t *stx = send(synctex, ui->eng, &buf);
int page = -1, x = -1, y = -1;
if (synctex_has_target(stx) &&
synctex_find_target(ps->ctx, stx, buf, ui->page, &page, &x, &y))
if (synctex_find_target(ps->ctx, stx, buf, &page, &x, &y))
{
fprintf(stderr, "[synctex forward] sync: hit page %d, coordinates (%d, %d)\n",
page, x, y);
Expand Down Expand Up @@ -1020,7 +1018,6 @@ bool texpresso_main(struct persistent_state *ps)
fprintf(stderr, "[synctex forward] pan.y = %.02f + %.02f = %.02f\n",
config->pan.y, delta, config->pan.y + delta);
config->pan.y += delta;
synctex_set_target(stx, NULL, 0);
if (delta != 0.0)
schedule_event(RENDER_EVENT);
}
Expand Down
25 changes: 15 additions & 10 deletions src/synctex.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,9 @@ struct synctex_s

char target_path[1024];
int target_tag, target_line;
int target_inp_len, target_page_cand;
int target_page, target_x, target_y;
int target_inp_len;
int target_page, target_current_page, target_page_cand;
int target_x, target_y;
};

synctex_t *synctex_new(fz_context *ctx)
Expand Down Expand Up @@ -626,7 +627,7 @@ int synctex_has_target(synctex_t *stx)
return stx && (stx->target_path[0] != 0);
}

void synctex_set_target(synctex_t *stx, const char *path, int line)
void synctex_set_target(synctex_t *stx, int current_page, const char *path, int line)
{
if (!stx)
return;
Expand All @@ -646,6 +647,7 @@ void synctex_set_target(synctex_t *stx, const char *path, int line)
stx->target_line = line;
stx->target_inp_len = 0;
stx->target_page_cand = 0;
stx->target_current_page = current_page;
}

static bool is_oneliner(enum kind k)
Expand Down Expand Up @@ -734,8 +736,7 @@ check_beamer_page(synctex_t *stx, fz_buffer *buf, struct record *r0, const uint8
return 0;
}

int synctex_find_target(fz_context *ctx, synctex_t *stx, fz_buffer *buf,
int current, int *page, int *x, int *y)
int synctex_find_target(fz_context *ctx, synctex_t *stx, fz_buffer *buf, int *page, int *x, int *y)
{
if (!stx->target_path[0])
return 0;
Expand All @@ -750,7 +751,7 @@ int synctex_find_target(fz_context *ctx, synctex_t *stx, fz_buffer *buf,
if (plen == len && strncmp(stx->target_path, fname, len) == 0)
{
stx->target_tag = stx->target_inp_len;
fprintf(stderr, "[synctex forward] target tag: %d\n", stx->target_tag);
// fprintf(stderr, "[synctex forward] target tag: %d\n", stx->target_tag);
int page = 0, offset = stx->inputs.ptr[stx->target_tag];
while (page < synctex_page_count(stx) &&
stx->pages.ptr[page * 2 + 1] < offset)
Expand All @@ -766,6 +767,10 @@ int synctex_find_target(fz_context *ctx, synctex_t *stx, fz_buffer *buf,
if (stx->target_tag == -1)
return 0;

int current = stx->target_current_page;
if (current >= synctex_page_count(stx))
current = synctex_page_count(stx) - 1;

struct record r;
while (stx->target_page_cand < synctex_page_count(stx))
{
Expand All @@ -780,9 +785,6 @@ int synctex_find_target(fz_context *ctx, synctex_t *stx, fz_buffer *buf,
if (x) *x = stx->target_x;
if (y) *y = stx->target_y;

if (current >= synctex_page_count(stx))
current = synctex_page_count(stx) - 1;

// Beamer hack: check if multiple consecutive pages are at the same location.
// If its the case, target the page closest to the current page one.
if (stx->target_page == stx->target_page_cand)
Expand All @@ -798,12 +800,15 @@ int synctex_find_target(fz_context *ctx, synctex_t *stx, fz_buffer *buf,
if (page) *page = stx->target_page;
}
else
{
synctex_set_target(stx, 0, NULL, 0);
break;
}
}
}
return 1;
}
fprintf(stderr, "[synctex forward] scanned page %d\n", stx->target_page_cand);
// fprintf(stderr, "[synctex forward] scanned page %d\n", stx->target_page_cand);
stx->target_page_cand += 1;
}

Expand Down
5 changes: 2 additions & 3 deletions src/synctex.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ int synctex_input_offset(fz_context *ctx, synctex_t *stx, unsigned index);
void synctex_scan(fz_context *ctx, synctex_t *stx, fz_buffer *buf, const char *doc_dir, unsigned page, int x, int y);

int synctex_has_target(synctex_t *stx);
void synctex_set_target(synctex_t *stx, const char *path, int line);
int synctex_find_target(fz_context *ctx, synctex_t *stx, fz_buffer *buf,
int current, int *page, int *x, int *y);
void synctex_set_target(synctex_t *stx, int current_page, const char *path, int line);
int synctex_find_target(fz_context *ctx, synctex_t *stx, fz_buffer *buf, int *page, int *x, int *y);

#endif // SYNCTEX_H_

0 comments on commit 3a5e8e5

Please sign in to comment.