comparison src/editors.c @ 442:4b2d7f9af171

Big whitespaces cleanup: - drop whitespaces at end of lines - convert eight spaces to tab at start of lines - drop spurious spaces mixed with tabs - remove empty lines at end of files
author zas_
date Sun, 20 Apr 2008 13:04:57 +0000
parents 3d16af3b133a
children e7361d06d83a
comparison
equal deleted inserted replaced
441:08eb7137cd94 442:4b2d7f9af171
192 gtk_widget_show(vd->progress); 192 gtk_widget_show(vd->progress);
193 193
194 vd->spinner = spinner_new(NULL, SPINNER_SPEED); 194 vd->spinner = spinner_new(NULL, SPINNER_SPEED);
195 gtk_box_pack_start(GTK_BOX(hbox), vd->spinner, FALSE, FALSE, 0); 195 gtk_box_pack_start(GTK_BOX(hbox), vd->spinner, FALSE, FALSE, 0);
196 gtk_widget_show(vd->spinner); 196 gtk_widget_show(vd->spinner);
197 197
198 gtk_widget_show(vd->gd->dialog); 198 gtk_widget_show(vd->gd->dialog);
199 199
200 ed->vd = vd; 200 ed->vd = vd;
201 return vd; 201 return vd;
202 } 202 }
274 GString *string; 274 GString *string;
275 gchar *pathl; 275 gchar *pathl;
276 const gchar *p = NULL; 276 const gchar *p = NULL;
277 277
278 string = g_string_new(""); 278 string = g_string_new("");
279 279
280 if (type == PATH_FILE) 280 if (type == PATH_FILE)
281 { 281 {
282 GList *ext_list = filter_to_list(extensions); 282 GList *ext_list = filter_to_list(extensions);
283 GList *work = ext_list; 283 GList *work = ext_list;
284 284
285 if (!work) 285 if (!work)
286 p = fd->path; 286 p = fd->path;
287 else 287 else
288 { 288 {
289 while(work) 289 while(work)
290 { 290 {
291 gchar *ext = work->data; 291 gchar *ext = work->data;
292 work = work->next; 292 work = work->next;
293 293
294 if (strcmp(ext, "*") == 0 || 294 if (strcmp(ext, "*") == 0 ||
295 strcasecmp(ext, fd->extension) == 0) 295 strcasecmp(ext, fd->extension) == 0)
296 { 296 {
297 p = fd->path; 297 p = fd->path;
298 break; 298 break;
299 } 299 }
300 300
301 GList *work2 = fd->sidecar_files; 301 GList *work2 = fd->sidecar_files;
302 302
303 while(work2) 303 while(work2)
304 { 304 {
305 FileData *sfd = work2->data; 305 FileData *sfd = work2->data;
306 work2 = work2->next; 306 work2 = work2->next;
307 307
308 if (strcasecmp(ext, sfd->extension) == 0) 308 if (strcasecmp(ext, sfd->extension) == 0)
309 { 309 {
310 p = sfd->path; 310 p = sfd->path;
311 break; 311 break;
312 } 312 }
377 { 377 {
378 gint flags = 0; 378 gint flags = 0;
379 const gchar *p = template; 379 const gchar *p = template;
380 GString *result = NULL; 380 GString *result = NULL;
381 gchar *extensions = NULL; 381 gchar *extensions = NULL;
382 382
383 if (output) 383 if (output)
384 result = g_string_new(""); 384 result = g_string_new("");
385 385
386 if (!template || template[0] == '\0') 386 if (!template || template[0] == '\0')
387 { 387 {
388 flags |= EDITOR_ERROR_EMPTY; 388 flags |= EDITOR_ERROR_EMPTY;
389 goto err; 389 goto err;
390 } 390 }
391 391
392 392
393 /* global flags */ 393 /* global flags */
394 while (*p == '%') 394 while (*p == '%')
395 { 395 {
396 switch (*++p) 396 switch (*++p)
397 { 397 {
407 flags |= EDITOR_VERBOSE_MULTI; 407 flags |= EDITOR_VERBOSE_MULTI;
408 p++; 408 p++;
409 break; 409 break;
410 } 410 }
411 } 411 }
412 412
413 /* command */ 413 /* command */
414 414
415 while (*p) 415 while (*p)
416 { 416 {
417 if (*p != '%') 417 if (*p != '%')
418 { 418 {
419 if (output) result = g_string_append_c(result, *p); 419 if (output) result = g_string_append_c(result, *p);
422 { 422 {
423 extensions = NULL; 423 extensions = NULL;
424 gchar *pathl = NULL; 424 gchar *pathl = NULL;
425 425
426 p++; 426 p++;
427 427
428 /* for example "%f" or "%{.crw;.raw;.cr2}f" */ 428 /* for example "%f" or "%{.crw;.raw;.cr2}f" */
429 if (*p == '{') 429 if (*p == '{')
430 { 430 {
431 p++; 431 p++;
432 gchar *end = strchr(p, '}'); 432 gchar *end = strchr(p, '}');
433 if (!end) 433 if (!end)
434 { 434 {
435 flags |= EDITOR_ERROR_SYNTAX; 435 flags |= EDITOR_ERROR_SYNTAX;
436 goto err; 436 goto err;
437 } 437 }
438 438
439 extensions = g_strndup(p, end - p); 439 extensions = g_strndup(p, end - p);
440 p = end + 1; 440 p = end + 1;
441 } 441 }
442 442
443 switch (*p) 443 switch (*p)
444 { 444 {
445 case 'd': 445 case 'd':
446 flags |= EDITOR_DEST; 446 flags |= EDITOR_DEST;
447 case 'p': 447 case 'p':
448 flags |= EDITOR_FOR_EACH; 448 flags |= EDITOR_FOR_EACH;
452 goto err; 452 goto err;
453 } 453 }
454 if (output) 454 if (output)
455 { 455 {
456 /* use the first file from the list */ 456 /* use the first file from the list */
457 if (!list || !list->data) 457 if (!list || !list->data)
458 { 458 {
459 flags |= EDITOR_ERROR_NO_FILE; 459 flags |= EDITOR_ERROR_NO_FILE;
460 goto err; 460 goto err;
461 } 461 }
462 pathl = editor_command_path_parse((FileData *)list->data, (*p == 'd') ? PATH_DEST : PATH_FILE, extensions); 462 pathl = editor_command_path_parse((FileData *)list->data, (*p == 'd') ? PATH_DEST : PATH_FILE, extensions);
463 if (!pathl) 463 if (!pathl)
464 { 464 {
465 flags |= EDITOR_ERROR_NO_FILE; 465 flags |= EDITOR_ERROR_NO_FILE;
466 goto err; 466 goto err;
467 } 467 }
468 result = g_string_append_c(result, '"'); 468 result = g_string_append_c(result, '"');
469 result = g_string_append(result, pathl); 469 result = g_string_append(result, pathl);
470 g_free(pathl); 470 g_free(pathl);
471 result = g_string_append_c(result, '"'); 471 result = g_string_append_c(result, '"');
472 } 472 }
473 break; 473 break;
474 474
475 case 'f': 475 case 'f':
476 flags |= EDITOR_SINGLE_COMMAND; 476 flags |= EDITOR_SINGLE_COMMAND;
477 if (flags & (EDITOR_FOR_EACH | EDITOR_DEST)) 477 if (flags & (EDITOR_FOR_EACH | EDITOR_DEST))
478 { 478 {
499 g_free(pathl); 499 g_free(pathl);
500 result = g_string_append_c(result, '"'); 500 result = g_string_append_c(result, '"');
501 } 501 }
502 work = work->next; 502 work = work->next;
503 } 503 }
504 if (!ok) 504 if (!ok)
505 { 505 {
506 flags |= EDITOR_ERROR_NO_FILE; 506 flags |= EDITOR_ERROR_NO_FILE;
507 goto err; 507 goto err;
508 } 508 }
509 } 509 }
510 break; 510 break;
511 default: 511 default:
512 flags |= EDITOR_ERROR_SYNTAX; 512 flags |= EDITOR_ERROR_SYNTAX;
513 goto err; 513 goto err;
514 } 514 }
515 if (extensions) g_free(extensions); 515 if (extensions) g_free(extensions);
519 } 519 }
520 520
521 if (output) *output = g_string_free(result, FALSE); 521 if (output) *output = g_string_free(result, FALSE);
522 return flags; 522 return flags;
523 523
524 524
525 err: 525 err:
526 if (output) 526 if (output)
527 { 527 {
528 g_string_free(result, TRUE); 528 g_string_free(result, TRUE);
529 *output = NULL; 529 *output = NULL;
530 } 530 }
531 if (extensions) g_free(extensions); 531 if (extensions) g_free(extensions);
535 static void editor_child_exit_cb (GPid pid, gint status, gpointer data) 535 static void editor_child_exit_cb (GPid pid, gint status, gpointer data)
536 { 536 {
537 EditorData *ed = data; 537 EditorData *ed = data;
538 g_spawn_close_pid(pid); 538 g_spawn_close_pid(pid);
539 ed->pid = -1; 539 ed->pid = -1;
540 540
541 editor_command_next_finish(ed, status); 541 editor_command_next_finish(ed, status);
542 } 542 }
543 543
544 544
545 static gint editor_command_one(const gchar *template, GList *list, EditorData *ed) 545 static gint editor_command_one(const gchar *template, GList *list, EditorData *ed)
547 gchar *command; 547 gchar *command;
548 gchar *working_directory; 548 gchar *working_directory;
549 FileData *fd = list->data; 549 FileData *fd = list->data;
550 gchar *args[4]; 550 gchar *args[4];
551 GPid pid; 551 GPid pid;
552 gint standard_output; 552 gint standard_output;
553 gint standard_error; 553 gint standard_error;
554 gboolean ok; 554 gboolean ok;
555 555
556 556
557 ed->pid = -1; 557 ed->pid = -1;
558 558
559 working_directory = remove_level_from_path(fd->path); 559 working_directory = remove_level_from_path(fd->path);
560 560
561 ed->flags = editor_command_parse(template, list, &command); 561 ed->flags = editor_command_parse(template, list, &command);
562 562
563 ok = !(ed->flags & EDITOR_ERROR_MASK); 563 ok = !(ed->flags & EDITOR_ERROR_MASK);
564 564
565 565
566 args[0] = COMMAND_SHELL; 566 args[0] = COMMAND_SHELL;
567 args[1] = COMMAND_OPT; 567 args[1] = COMMAND_OPT;
568 args[2] = command; 568 args[2] = command;
569 args[3] = NULL; 569 args[3] = NULL;
570 570
571 if (ok) 571 if (ok)
572 { 572 {
573 ok = g_spawn_async_with_pipes(working_directory, args, NULL, 573 ok = g_spawn_async_with_pipes(working_directory, args, NULL,
574 G_SPAWN_DO_NOT_REAP_CHILD, /* GSpawnFlags */ 574 G_SPAWN_DO_NOT_REAP_CHILD, /* GSpawnFlags */
575 NULL, NULL, 575 NULL, NULL,
576 &pid, 576 &pid,
577 NULL, 577 NULL,
578 ed->vd ? &standard_output : NULL, 578 ed->vd ? &standard_output : NULL,
579 ed->vd ? &standard_error : NULL, 579 ed->vd ? &standard_error : NULL,
580 NULL); 580 NULL);
581 581
582 if (!ok) ed->flags |= EDITOR_ERROR_CANT_EXEC; 582 if (!ok) ed->flags |= EDITOR_ERROR_CANT_EXEC;
583 } 583 }
584 584
585 if (ok) 585 if (ok)
586 { 586 {
587 g_child_watch_add(pid, editor_child_exit_cb, ed); 587 g_child_watch_add(pid, editor_child_exit_cb, ed);
588 ed->pid = pid; 588 ed->pid = pid;
589 } 589 }
590 590
591 591
592 if (ed->vd) 592 if (ed->vd)
593 { 593 {
594 594
595 if (!ok) 595 if (!ok)
596 { 596 {
599 buf = g_strdup_printf(_("Failed to run command:\n%s\n"), template); 599 buf = g_strdup_printf(_("Failed to run command:\n%s\n"), template);
600 editor_verbose_window_fill(ed->vd, buf, strlen(buf)); 600 editor_verbose_window_fill(ed->vd, buf, strlen(buf));
601 g_free(buf); 601 g_free(buf);
602 602
603 } 603 }
604 else 604 else
605 { 605 {
606 606
607 GIOChannel *channel_output; 607 GIOChannel *channel_output;
608 GIOChannel *channel_error; 608 GIOChannel *channel_error;
609 channel_output = g_io_channel_unix_new(standard_output); 609 channel_output = g_io_channel_unix_new(standard_output);
610 g_io_channel_set_flags(channel_output, G_IO_FLAG_NONBLOCK, NULL); 610 g_io_channel_set_flags(channel_output, G_IO_FLAG_NONBLOCK, NULL);
611 611
619 g_io_add_watch_full(channel_error, G_PRIORITY_HIGH, G_IO_IN | G_IO_ERR | G_IO_HUP, 619 g_io_add_watch_full(channel_error, G_PRIORITY_HIGH, G_IO_IN | G_IO_ERR | G_IO_HUP,
620 editor_verbose_io_cb, ed, NULL); 620 editor_verbose_io_cb, ed, NULL);
621 g_io_channel_unref(channel_error); 621 g_io_channel_unref(channel_error);
622 } 622 }
623 } 623 }
624 624
625 625
626 626
627 g_free(command); 627 g_free(command);
628 g_free(working_directory); 628 g_free(working_directory);
629 629
630 return ed->flags & EDITOR_ERROR_MASK; 630 return ed->flags & EDITOR_ERROR_MASK;
631 } 631 }
657 editor_verbose_window_fill(ed->vd, fd->path, strlen(fd->path)); 657 editor_verbose_window_fill(ed->vd, fd->path, strlen(fd->path));
658 editor_verbose_window_fill(ed->vd, "\n", 1); 658 editor_verbose_window_fill(ed->vd, "\n", 1);
659 } 659 }
660 } 660 }
661 661
662 if (!error) 662 if (!error)
663 return 0; 663 return 0;
664 else 664 else
665 /* command was not started, call the finish immediately */ 665 /* command was not started, call the finish immediately */
666 return editor_command_next_finish(ed, 0); 666 return editor_command_next_finish(ed, 0);
667 } 667 }
668 668
669 /* everything is done */ 669 /* everything is done */
670 return editor_command_done(ed); 670 return editor_command_done(ed);
671 } 671 }
672 672
673 static gint editor_command_next_finish(EditorData *ed, gint status) 673 static gint editor_command_next_finish(EditorData *ed, gint status)
745 { 745 {
746 editor_command_next_start(ed); 746 editor_command_next_start(ed);
747 } 747 }
748 void editor_skip(gpointer ed) 748 void editor_skip(gpointer ed)
749 { 749 {
750 editor_command_done(ed); 750 editor_command_done(ed);
751 } 751 }
752 752
753 static gint editor_command_start(const gchar *template, const gchar *text, GList *list, EditorCallback cb, gpointer data) 753 static gint editor_command_start(const gchar *template, const gchar *text, GList *list, EditorCallback cb, gpointer data)
754 { 754 {
755 EditorData *ed; 755 EditorData *ed;
756 gint flags = editor_command_parse(template, NULL, NULL); 756 gint flags = editor_command_parse(template, NULL, NULL);
757 757
758 if (flags & EDITOR_ERROR_MASK) return flags & EDITOR_ERROR_MASK; 758 if (flags & EDITOR_ERROR_MASK) return flags & EDITOR_ERROR_MASK;
759 759
760 ed = g_new0(EditorData, 1); 760 ed = g_new0(EditorData, 1);
761 ed->list = filelist_copy(list); 761 ed->list = filelist_copy(list);
762 ed->flags = flags; 762 ed->flags = flags;
764 ed->total = (flags & EDITOR_SINGLE_COMMAND) ? 1 : g_list_length(list); 764 ed->total = (flags & EDITOR_SINGLE_COMMAND) ? 1 : g_list_length(list);
765 ed->count = 0; 765 ed->count = 0;
766 ed->stopping = FALSE; 766 ed->stopping = FALSE;
767 ed->callback = cb; 767 ed->callback = cb;
768 ed->data = data; 768 ed->data = data;
769 769
770 if ((flags & EDITOR_VERBOSE_MULTI) && list && list->next) 770 if ((flags & EDITOR_VERBOSE_MULTI) && list && list->next)
771 flags |= EDITOR_VERBOSE; 771 flags |= EDITOR_VERBOSE;
772 772
773 773
774 if (flags & EDITOR_VERBOSE) 774 if (flags & EDITOR_VERBOSE)
775 editor_verbose_window(ed, text); 775 editor_verbose_window(ed, text);
776 776
777 editor_command_next_start(ed); 777 editor_command_next_start(ed);
778 /* errors from editor_command_next_start will be handled via callback */ 778 /* errors from editor_command_next_start will be handled via callback */
779 return flags & EDITOR_ERROR_MASK; 779 return flags & EDITOR_ERROR_MASK;
780 } 780 }
781 781
782 gint start_editor_from_filelist_full(gint n, GList *list, EditorCallback cb, gpointer data) 782 gint start_editor_from_filelist_full(gint n, GList *list, EditorCallback cb, gpointer data)