summaryrefslogtreecommitdiff
path: root/Build/source/texk/xdvik/filehist.c
blob: 43664025e1edb7200e2560b1b0ba30f988062279 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*
 * Copyright (c) 2004 Stefan Ulrich
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL PAUL VOJTA OR ANY OTHER AUTHOR OF THIS SOFTWARE BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

/*
  List of recently visited files, initialized from X resource
  fileHistory and displayed in `File -> Open Recent' menu.
*/

#include <ctype.h>

#include "xdvi-config.h"
#include "xdvi.h"
#include "util.h"
#include "my-snprintf.h"
#include "string-utils.h"
#include "dvi-init.h"
#include "events.h"
#include "message-window.h"
#include "xm_menu.h"
#include "xaw_menu.h"
#include "filehist.h"

/****************************************************************************
 *
 * File-scope globals
 *
 ****************************************************************************/

/* list of files */
static struct dl_list *m_file_history = NULL;

/* current size of the list */
static int m_file_history_length = 0;

/* /\* current position in the list *\/ */
/* static int m_file_history_currpos = 0; */

/* item in above list */
struct file_history {
    char *filename;
    int pageno;
};

#define DEBUG 1

/****************************************************************************
 *
 * Private functions
 *
 ****************************************************************************/

static void
file_history_show(struct dl_list *list)
{
    if (globals.debug & DBG_FILES) {
	int n;
	fprintf(stderr, "======= File history:\n");
	for (n = 0; list != NULL; list = list->next, n++) {
	    struct file_history *item = (struct file_history *)(list->item);
	    if (item == NULL) {
		fprintf(stderr, "item %d is NULL!\n", n);
		continue;
	    }
	    fprintf(stderr, "item %d: %d:%s\n", n, item->pageno, item->filename);
	}
    }
}


/****************************************************************************
 *
 * Exported functions
 *
 ****************************************************************************/

void
file_history_init(void)
{
    char **fileinfo;
    size_t i;
    struct dl_list *head = NULL;
    
    m_file_history_length = 0;

    if (resource.file_history == NULL)
	return;
	
    fileinfo = get_separated_list(resource.file_history, "\n", False);
    
    for (i = 0; fileinfo[i] != NULL && i < (size_t)resource.file_history_size; i++) {
	struct file_history *item = xmalloc(sizeof *item);
	char *ptr;
	int pageno = strtol(fileinfo[i], &ptr, 10);
	TRACE_FILES((stderr, "FILEINFO: %s", fileinfo[i]));
	if (ptr == fileinfo[i]) {
	    XDVI_WARNING((stderr, "Missing page number in resource line `%s'!\n",
			  fileinfo[i]));
	    pageno = 0;
	}
	else {
	    while (isspace((int)*ptr))
		ptr++;
	}

	item->pageno = pageno;
	item->filename = xstrdup(ptr);

	TRACE_FILES((stderr, "FILE: %d:%s", item->pageno, item->filename));
	
	m_file_history = dl_list_insert(m_file_history, item);
	if (head == NULL) /* remember head position */
	    head = m_file_history;
	
	TRACE_FILES((stderr, "NEW ELEM: %p", (void *)m_file_history));

	m_file_history_length++;

	free(fileinfo[i]);
    }
    free(fileinfo);

    m_file_history = head;    
    file_history_show(m_file_history);
}

static Boolean
equals_filename(const void *it, const void *fname)
{
    const struct file_history *item = (const struct file_history *)it;
    const char *filename = (const char *)fname;

    return strcmp(item->filename, filename) == 0;
}


/* put new elem for filename `filename' and page number `page' at the front
   of the list. Return True if the addition caused the history to grow, else
   False (in case the item was only moved to the front).
*/
Boolean
file_history_push(const char *filename)
{
    int i;
    struct file_history *item = NULL;
    void *removed_data = NULL;
    struct file_history *removed_item = NULL;
    int len_bak = m_file_history_length;
    int count = 0;

    TRACE_FILES((stderr, "Pushing: |%s|", filename));
    
    /* if list already contains an item with same filename, remove it */
    m_file_history = dl_list_remove(m_file_history, filename, &count, &removed_data, equals_filename);
    if (count == 0)
	m_file_history_length++;

    removed_item = (struct file_history *)removed_data;
    
    file_history_show(m_file_history); 

    TRACE_FILES((stderr, "current length: %d, max: %d", m_file_history_length, resource.file_history_size));
    
    /* truncate list if it has reached its max length */
    if (m_file_history_length > resource.file_history_size) {
	struct dl_list *listpos = m_file_history;
	struct dl_list *last_pos = listpos;
	for (i = 0; listpos != NULL && i < m_file_history_length; i++) {
	    last_pos = listpos;
	    listpos = listpos->next;
	}

	item = last_pos->item; /* reuse item */
	TRACE_FILES((stderr, "Re-using item: |%s| -> |%s|", item->filename, filename));
	item->filename = xrealloc(item->filename, strlen(filename) + 1);
	strcpy(item->filename, filename);
	
	(void)dl_list_remove_item(&last_pos);
	m_file_history_length--;
    }
    else if (removed_item != NULL) {
	TRACE_FILES((stderr, "Re-using item: |%s|\n", removed_item->filename));
	item = removed_item; /* reuse item */
    }
    else {
	item = xmalloc(sizeof *item);
	TRACE_FILES((stderr, "NEW item: |%s|\n", filename));
	item->filename = xstrdup(filename);
	item->pageno = 0;
    }

    /* add new element at front of list */
    m_file_history = dl_list_push_front(m_file_history, item);
    
    file_history_show(m_file_history);

    TRACE_FILES((stderr, "returning: %d < %d", len_bak, m_file_history_length));
    return len_bak < m_file_history_length;
}

size_t file_history_size(void)
{
    return m_file_history_length;
}

void file_history_set_page(int pageno)
{
    struct dl_list *head;

    TRACE_FILES((stderr, "SETTING HEAD to %d", pageno));
    file_history_show(m_file_history);
    head = dl_list_head(m_file_history);
    if (head != NULL) {
	struct file_history *item = (struct file_history *)head->item;
	TRACE_FILES((stderr, "Setting page of |%s| to %d", item->filename, pageno));
	item->pageno = pageno;
    }
}

void file_history_set_page_at(int idx, int pageno)
{
    int i;
    struct dl_list *listpos = dl_list_head(m_file_history);
    struct file_history *item;
    
    for (i = 0; listpos != NULL && i < idx; i++) {
	listpos = listpos->next;
    }
    if (listpos == NULL) {
	TRACE_FILES((stderr, "Asked for file at position %d, but only %d elements in list",
		     idx, i - 1));
	return;
    }
    item = (struct file_history *)listpos->item;
    TRACE_FILES((stderr, "set_page_at index %d: Setting page of |%s| to %d", idx, item->filename, pageno));
    item->pageno = pageno;
}

int file_history_get_page(void)
{
    struct dl_list *head = dl_list_head(m_file_history);
    if (head != NULL) {
	struct file_history *item = (struct file_history *)head->item;
	TRACE_FILES((stderr, "Getting page of |%s|: %d", item->filename, item->pageno));
	return item->pageno;
    }
    return 0;
}

/*
 * Invoke `callback' for each elem of file history, passing
 * the current index, filename, page number, and data passed to
 * this function.
 */
void
file_history_enumerate(filehistCallbackT callback, void *data)
{
    int i;
    struct dl_list *listpos = dl_list_head(m_file_history);

    for (i = 0; listpos != NULL; i++) {
	struct file_history *item = (struct file_history *)listpos->item;
	callback(i, item->filename, item->pageno, data);
	listpos = listpos->next;
    }
}

char *
file_history_get_elem(int idx, int *ret_page)
{
    int i;
    struct dl_list *listpos = dl_list_head(m_file_history);
    struct file_history *item;
    
    for (i = 0; listpos != NULL && i < idx; i++) {
	listpos = listpos->next;
    }
    if (listpos == NULL) {
	XDVI_WARNING((stderr, "Asked for file at position %d, but only %d elements in list",
		      idx, i - 1));
	return NULL;
    }
    item = (struct file_history *)listpos->item;
    *ret_page = item->pageno;
    file_history_show(m_file_history);
    return item->filename;
}

char *
file_history_get_list(void)
{
    char buf[LENGTH_OF_INT];
    char *ret = xstrdup("");
    struct dl_list *listpos;
    
    for (listpos = dl_list_head(m_file_history);
	 listpos != NULL;
	 listpos = listpos->next) {
	
	struct file_history *item = (struct file_history *)listpos->item;
	SNPRINTF(buf, LENGTH_OF_INT, "%d ", item->pageno);
	ret = xstrcat(ret, buf);
	ret = xstrcat(ret, item->filename);
	ret = xstrcat(ret, "\n");	
    }

    ret[strlen(ret) - 1] = '\0'; /* chop off excess \n at end */
    return ret;
}

void
file_history_open(const char *fname)
{
    Boolean tried_dvi_ext = True;
    char *new_dvi_name = NULL;

    int dummy_cnt = 0;
    void *dummy_data = NULL;
    
    file_history_set_page(current_page);
    if ((new_dvi_name = open_dvi_file_wrapper(fname,
					      True, /* pretend file is from commandline, otherwise xdvi will
						       try to fork for non-existing files, leading to confusing
						       popup error messages */
					      False, &tried_dvi_ext,
					      True /* don't exit on error */)) == NULL) {
	/* remove this item from the file history */
	m_file_history = dl_list_remove(m_file_history, fname, &dummy_cnt, &dummy_data, equals_filename);
	m_file_history_length--;
	file_history_show(m_file_history); 

	filehist_menu_refresh();
	return;
    }
    else {
	dviErrFlagT errflag;
	if (load_dvi_file(True, &errflag)) {
	    /* page_history_insert(pageno); */
	    set_dvi_name(new_dvi_name);
	    
	    globals.ev.flags |= EV_NEWDOC;
	    globals.ev.flags |= EV_FILEHIST_GOTO_PAGE;
	    globals.ev.flags |= EV_PAGEHIST_INSERT;
	}
	else { /* re-open old file */
	    popup_message(globals.widgets.top_level,
			  MSG_ERR,
			  NULL,
			  "Could not open `%s': %s.\n"
			  /* "Removing this file from the history." */,
			  globals.dvi_name, get_dvi_error(errflag));
	    /* remove this item from the file history */
	    m_file_history = dl_list_remove(m_file_history, globals.dvi_name,
					    &dummy_cnt, &dummy_data,
					    equals_filename);
	    m_file_history_length--;
	    filehist_menu_refresh();
	    
	    if (!internal_open_dvi(globals.dvi_name, &errflag, True)) {
/*  		|| !internal_init_dvi(&errflag, True)) { */
		popup_message(globals.widgets.top_level,
			      MSG_ERR,
			      NULL,
			      "Couldn't reopen `%s': %s.\n"
			      /* "Removing this file from the history." */,
			      globals.dvi_name, get_dvi_error(errflag));
		/* remove this item from the file history */
		m_file_history = dl_list_remove(m_file_history, globals.dvi_name, &dummy_cnt, &dummy_data, equals_filename);
		m_file_history_length--;
		filehist_menu_refresh();
	    }
	    else {
		globals.ev.flags |= EV_NEWPAGE;
	    }
	}
    }
}