| 1 |
/* |
| 2 |
* Copyright (c) 2004 Stefan Ulrich |
| 3 |
* |
| 4 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 |
* of this software and associated documentation files (the "Software"), to |
| 6 |
* deal in the Software without restriction, including without limitation the |
| 7 |
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 8 |
* sell copies of the Software, and to permit persons to whom the Software is |
| 9 |
* furnished to do so, subject to the following conditions: |
| 10 |
* |
| 11 |
* The above copyright notice and this permission notice shall be included in |
| 12 |
* all copies or substantial portions of the Software. |
| 13 |
* |
| 14 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 15 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 16 |
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 17 |
* IN NO EVENT SHALL PAUL VOJTA OR ANY OTHER AUTHOR OF THIS SOFTWARE BE |
| 18 |
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 19 |
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 20 |
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 21 |
*/ |
| 22 |
|
| 23 |
/* |
| 24 |
List of recently visited files, initialized from X resource |
| 25 |
fileHistory and displayed in `File -> Open Recent' menu. |
| 26 |
*/ |
| 27 |
|
| 28 |
#include "xdvi-config.h" |
| 29 |
|
| 30 |
#include <ctype.h> |
| 31 |
|
| 32 |
#include "xdvi.h" |
| 33 |
#include "util.h" |
| 34 |
#include "dl_list.h" |
| 35 |
#include "my-snprintf.h" |
| 36 |
#include "string-utils.h" |
| 37 |
#include "dvi-init.h" |
| 38 |
#include "events.h" |
| 39 |
#include "message-window.h" |
| 40 |
#include "xm_menu.h" |
| 41 |
#include "xaw_menu.h" |
| 42 |
#include "filehist.h" |
| 43 |
|
| 44 |
/**************************************************************************** |
| 45 |
* |
| 46 |
* File-scope globals |
| 47 |
* |
| 48 |
****************************************************************************/ |
| 49 |
|
| 50 |
/* list of files */ |
| 51 |
static struct dl_list *m_file_history = NULL; |
| 52 |
|
| 53 |
/* current size of the list */ |
| 54 |
static int m_file_history_length = 0; |
| 55 |
|
| 56 |
/* /\* current position in the list *\/ */ |
| 57 |
/* static int m_file_history_currpos = 0; */ |
| 58 |
|
| 59 |
/* item in above list */ |
| 60 |
struct file_history { |
| 61 |
char *filename; |
| 62 |
int pageno; |
| 63 |
}; |
| 64 |
|
| 65 |
#define DEBUG 1 |
| 66 |
|
| 67 |
/**************************************************************************** |
| 68 |
* |
| 69 |
* Private functions |
| 70 |
* |
| 71 |
****************************************************************************/ |
| 72 |
|
| 73 |
static void |
| 74 |
file_history_show(struct dl_list *list) |
| 75 |
{ |
| 76 |
if (globals.debug & DBG_FILES) { |
| 77 |
int n; |
| 78 |
fprintf(stderr, "======= File history:\n"); |
| 79 |
for (n = 0; list != NULL; list = list->next, n++) { |
| 80 |
struct file_history *item = (struct file_history *)(list->item); |
| 81 |
if (item == NULL) { |
| 82 |
fprintf(stderr, "item %d is NULL!\n", n); |
| 83 |
continue; |
| 84 |
} |
| 85 |
fprintf(stderr, "item %d: %d:%s\n", n, item->pageno, item->filename); |
| 86 |
} |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
/**************************************************************************** |
| 92 |
* |
| 93 |
* Exported functions |
| 94 |
* |
| 95 |
****************************************************************************/ |
| 96 |
|
| 97 |
void |
| 98 |
file_history_init(void) |
| 99 |
{ |
| 100 |
char **fileinfo; |
| 101 |
size_t i; |
| 102 |
struct dl_list *head = NULL; |
| 103 |
|
| 104 |
m_file_history_length = 0; |
| 105 |
|
| 106 |
if (resource.file_history == NULL) |
| 107 |
return; |
| 108 |
|
| 109 |
fileinfo = get_separated_list(resource.file_history, "\n", False); |
| 110 |
|
| 111 |
for (i = 0; fileinfo[i] != NULL && i < (size_t)resource.file_history_size; i++) { |
| 112 |
struct file_history *item = xmalloc(sizeof *item); |
| 113 |
char *ptr; |
| 114 |
int pageno = strtol(fileinfo[i], &ptr, 10); |
| 115 |
TRACE_FILES((stderr, "FILEINFO: %s", fileinfo[i])); |
| 116 |
if (ptr == fileinfo[i]) { |
| 117 |
XDVI_WARNING((stderr, "Missing page number in resource line `%s'!\n", |
| 118 |
fileinfo[i])); |
| 119 |
pageno = 0; |
| 120 |
} |
| 121 |
else { |
| 122 |
while (isspace((int)*ptr)) |
| 123 |
ptr++; |
| 124 |
} |
| 125 |
|
| 126 |
item->pageno = pageno; |
| 127 |
item->filename = xstrdup(ptr); |
| 128 |
|
| 129 |
TRACE_FILES((stderr, "FILE: %d:%s", item->pageno, item->filename)); |
| 130 |
|
| 131 |
m_file_history = dl_list_insert(m_file_history, item); |
| 132 |
if (head == NULL) /* remember head position */ |
| 133 |
head = m_file_history; |
| 134 |
|
| 135 |
TRACE_FILES((stderr, "NEW ELEM: %p", (void *)m_file_history)); |
| 136 |
|
| 137 |
m_file_history_length++; |
| 138 |
|
| 139 |
free(fileinfo[i]); |
| 140 |
} |
| 141 |
free(fileinfo); |
| 142 |
|
| 143 |
m_file_history = head; |
| 144 |
file_history_show(m_file_history); |
| 145 |
} |
| 146 |
|
| 147 |
static Boolean |
| 148 |
equals_filename(const void *it, const void *fname) |
| 149 |
{ |
| 150 |
const struct file_history *item = (const struct file_history *)it; |
| 151 |
const char *filename = (const char *)fname; |
| 152 |
|
| 153 |
return strcmp(item->filename, filename) == 0; |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
/* put new elem for filename `filename' and page number `page' at the front |
| 158 |
of the list. Return True if the addition caused the history to grow, else |
| 159 |
False (in case the item was only moved to the front). |
| 160 |
*/ |
| 161 |
Boolean |
| 162 |
file_history_push(const char *filename) |
| 163 |
{ |
| 164 |
int i; |
| 165 |
struct file_history *item = NULL; |
| 166 |
void *removed_data = NULL; |
| 167 |
struct file_history *removed_item = NULL; |
| 168 |
int len_bak = m_file_history_length; |
| 169 |
int count = 0; |
| 170 |
|
| 171 |
TRACE_FILES((stderr, "Pushing: |%s|", filename)); |
| 172 |
|
| 173 |
/* if list already contains an item with same filename, remove it */ |
| 174 |
m_file_history = dl_list_remove(m_file_history, filename, &count, &removed_data, equals_filename); |
| 175 |
if (count == 0) |
| 176 |
m_file_history_length++; |
| 177 |
|
| 178 |
removed_item = (struct file_history *)removed_data; |
| 179 |
|
| 180 |
file_history_show(m_file_history); |
| 181 |
|
| 182 |
TRACE_FILES((stderr, "current length: %d, max: %d", m_file_history_length, resource.file_history_size)); |
| 183 |
|
| 184 |
/* truncate list if it has reached its max length */ |
| 185 |
if (m_file_history_length > resource.file_history_size) { |
| 186 |
struct dl_list *listpos = m_file_history; |
| 187 |
struct dl_list *last_pos = listpos; |
| 188 |
for (i = 0; listpos != NULL && i < m_file_history_length; i++) { |
| 189 |
last_pos = listpos; |
| 190 |
listpos = listpos->next; |
| 191 |
} |
| 192 |
|
| 193 |
item = last_pos->item; /* reuse item */ |
| 194 |
TRACE_FILES((stderr, "Re-using item: |%s| -> |%s|", item->filename, filename)); |
| 195 |
item->filename = xrealloc(item->filename, strlen(filename) + 1); |
| 196 |
strcpy(item->filename, filename); |
| 197 |
|
| 198 |
(void)dl_list_remove_item(&last_pos); |
| 199 |
m_file_history_length--; |
| 200 |
} |
| 201 |
else if (removed_item != NULL) { |
| 202 |
TRACE_FILES((stderr, "Re-using item: |%s|\n", removed_item->filename)); |
| 203 |
item = removed_item; /* reuse item */ |
| 204 |
} |
| 205 |
else { |
| 206 |
item = xmalloc(sizeof *item); |
| 207 |
TRACE_FILES((stderr, "NEW item: |%s|\n", filename)); |
| 208 |
item->filename = xstrdup(filename); |
| 209 |
item->pageno = 0; |
| 210 |
} |
| 211 |
|
| 212 |
/* add new element at front of list */ |
| 213 |
m_file_history = dl_list_push_front(m_file_history, item); |
| 214 |
|
| 215 |
file_history_show(m_file_history); |
| 216 |
|
| 217 |
TRACE_FILES((stderr, "returning: %d < %d", len_bak, m_file_history_length)); |
| 218 |
return len_bak < m_file_history_length; |
| 219 |
} |
| 220 |
|
| 221 |
size_t file_history_size(void) |
| 222 |
{ |
| 223 |
return m_file_history_length; |
| 224 |
} |
| 225 |
|
| 226 |
void file_history_set_page(int pageno) |
| 227 |
{ |
| 228 |
struct dl_list *head; |
| 229 |
|
| 230 |
TRACE_FILES((stderr, "SETTING HEAD to %d", pageno)); |
| 231 |
file_history_show(m_file_history); |
| 232 |
head = dl_list_head(m_file_history); |
| 233 |
if (head != NULL) { |
| 234 |
struct file_history *item = (struct file_history *)head->item; |
| 235 |
TRACE_FILES((stderr, "Setting page of |%s| to %d", item->filename, pageno)); |
| 236 |
item->pageno = pageno; |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
void file_history_set_page_at(int idx, int pageno) |
| 241 |
{ |
| 242 |
int i; |
| 243 |
struct dl_list *listpos = dl_list_head(m_file_history); |
| 244 |
struct file_history *item; |
| 245 |
|
| 246 |
for (i = 0; listpos != NULL && i < idx; i++) { |
| 247 |
listpos = listpos->next; |
| 248 |
} |
| 249 |
if (listpos == NULL) { |
| 250 |
TRACE_FILES((stderr, "Asked for file at position %d, but only %d elements in list", |
| 251 |
idx, i - 1)); |
| 252 |
return; |
| 253 |
} |
| 254 |
item = (struct file_history *)listpos->item; |
| 255 |
TRACE_FILES((stderr, "set_page_at index %d: Setting page of |%s| to %d", idx, item->filename, pageno)); |
| 256 |
item->pageno = pageno; |
| 257 |
} |
| 258 |
|
| 259 |
int file_history_get_page(void) |
| 260 |
{ |
| 261 |
struct dl_list *head = dl_list_head(m_file_history); |
| 262 |
if (head != NULL) { |
| 263 |
struct file_history *item = (struct file_history *)head->item; |
| 264 |
TRACE_FILES((stderr, "Getting page of |%s|: %d", item->filename, item->pageno)); |
| 265 |
return item->pageno; |
| 266 |
} |
| 267 |
return 0; |
| 268 |
} |
| 269 |
|
| 270 |
/* |
| 271 |
* Invoke `callback' for each elem of file history, passing |
| 272 |
* the current index, filename, page number, and data passed to |
| 273 |
* this function. |
| 274 |
*/ |
| 275 |
void |
| 276 |
file_history_enumerate(filehistCallbackT callback, void *data) |
| 277 |
{ |
| 278 |
int i; |
| 279 |
struct dl_list *listpos = dl_list_head(m_file_history); |
| 280 |
|
| 281 |
for (i = 0; listpos != NULL; i++) { |
| 282 |
struct file_history *item = (struct file_history *)listpos->item; |
| 283 |
callback(i, item->filename, item->pageno, data); |
| 284 |
listpos = listpos->next; |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
char * |
| 289 |
file_history_get_elem(int idx, int *ret_page) |
| 290 |
{ |
| 291 |
int i; |
| 292 |
struct dl_list *listpos = dl_list_head(m_file_history); |
| 293 |
struct file_history *item; |
| 294 |
|
| 295 |
for (i = 0; listpos != NULL && i < idx; i++) { |
| 296 |
listpos = listpos->next; |
| 297 |
} |
| 298 |
if (listpos == NULL) { |
| 299 |
XDVI_WARNING((stderr, "Asked for file at position %d, but only %d elements in list", |
| 300 |
idx, i - 1)); |
| 301 |
return NULL; |
| 302 |
} |
| 303 |
item = (struct file_history *)listpos->item; |
| 304 |
*ret_page = item->pageno; |
| 305 |
file_history_show(m_file_history); |
| 306 |
return item->filename; |
| 307 |
} |
| 308 |
|
| 309 |
char * |
| 310 |
file_history_get_list(void) |
| 311 |
{ |
| 312 |
char buf[LENGTH_OF_INT]; |
| 313 |
char *ret = xstrdup(""); |
| 314 |
struct dl_list *listpos; |
| 315 |
|
| 316 |
for (listpos = dl_list_head(m_file_history); |
| 317 |
listpos != NULL; |
| 318 |
listpos = listpos->next) { |
| 319 |
|
| 320 |
struct file_history *item = (struct file_history *)listpos->item; |
| 321 |
SNPRINTF(buf, LENGTH_OF_INT, "%d ", item->pageno); |
| 322 |
ret = xstrcat(ret, buf); |
| 323 |
ret = xstrcat(ret, item->filename); |
| 324 |
ret = xstrcat(ret, "\n"); |
| 325 |
} |
| 326 |
|
| 327 |
ret[strlen(ret) - 1] = '\0'; /* chop off excess \n at end */ |
| 328 |
return ret; |
| 329 |
} |
| 330 |
|
| 331 |
void |
| 332 |
file_history_open(const char *fname) |
| 333 |
{ |
| 334 |
Boolean tried_dvi_ext = True; |
| 335 |
char *new_dvi_name = NULL; |
| 336 |
|
| 337 |
int dummy_cnt = 0; |
| 338 |
void *dummy_data = NULL; |
| 339 |
|
| 340 |
file_history_set_page(current_page); |
| 341 |
if ((new_dvi_name = open_dvi_file_wrapper(fname, |
| 342 |
True, /* pretend file is from commandline, otherwise xdvi will |
| 343 |
try to fork for non-existing files, leading to confusing |
| 344 |
popup error messages */ |
| 345 |
False, &tried_dvi_ext, |
| 346 |
True /* don't exit on error */)) == NULL) { |
| 347 |
/* remove this item from the file history */ |
| 348 |
m_file_history = dl_list_remove(m_file_history, fname, &dummy_cnt, &dummy_data, equals_filename); |
| 349 |
m_file_history_length--; |
| 350 |
file_history_show(m_file_history); |
| 351 |
|
| 352 |
filehist_menu_refresh(); |
| 353 |
return; |
| 354 |
} |
| 355 |
else { |
| 356 |
dviErrFlagT errflag; |
| 357 |
if (load_dvi_file( |
| 358 |
#if !DELAYED_MKTEXPK |
| 359 |
True, |
| 360 |
#endif |
| 361 |
&errflag)) { |
| 362 |
/* page_history_insert(pageno); */ |
| 363 |
set_dvi_name(new_dvi_name); |
| 364 |
|
| 365 |
globals.ev.flags |= EV_NEWDOC; |
| 366 |
globals.ev.flags |= EV_FILEHIST_GOTO_PAGE; |
| 367 |
} |
| 368 |
else { /* re-open old file */ |
| 369 |
popup_message(globals.widgets.top_level, |
| 370 |
MSG_ERR, |
| 371 |
NULL, |
| 372 |
"Could not open `%s': %s.\n" |
| 373 |
/* "Removing this file from the history." */, |
| 374 |
globals.dvi_name, get_dvi_error(errflag)); |
| 375 |
/* remove this item from the file history */ |
| 376 |
m_file_history = dl_list_remove(m_file_history, globals.dvi_name, |
| 377 |
&dummy_cnt, &dummy_data, |
| 378 |
equals_filename); |
| 379 |
m_file_history_length--; |
| 380 |
filehist_menu_refresh(); |
| 381 |
|
| 382 |
if (!internal_open_dvi(globals.dvi_name, &errflag, True |
| 383 |
#if DELAYED_MKTEXPK |
| 384 |
, True |
| 385 |
#endif |
| 386 |
)) { |
| 387 |
popup_message(globals.widgets.top_level, |
| 388 |
MSG_ERR, |
| 389 |
NULL, |
| 390 |
"Couldn't reopen `%s': %s.\n" |
| 391 |
/* "Removing this file from the history." */, |
| 392 |
globals.dvi_name, get_dvi_error(errflag)); |
| 393 |
/* remove this item from the file history */ |
| 394 |
m_file_history = dl_list_remove(m_file_history, globals.dvi_name, &dummy_cnt, &dummy_data, equals_filename); |
| 395 |
m_file_history_length--; |
| 396 |
filehist_menu_refresh(); |
| 397 |
} |
| 398 |
else { |
| 399 |
globals.ev.flags |= EV_NEWPAGE; |
| 400 |
} |
| 401 |
} |
| 402 |
} |
| 403 |
} |