diff -r 000000000000 -r 5e27fa988c5c bookloupe/pending.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/bookloupe/pending.c Mon Sep 30 08:18:42 2013 +0100 @@ -0,0 +1,239 @@ +#include +#include +#include +#include "bookloupe.h" +#include "pending.h" + +/* + * print_pending: + * + * If we are in a state of unbalanced quotes, and this line + * doesn't begin with a quote, output the stored error message. + * If the -p switch was used, print the warning even if the + * new para starts with quotes. + */ +void print_pending(const char *aline,const char *parastart, + struct pending *pending) +{ + const char *s; + gunichar c; + if (aline) + { + s=aline; + while (*s==' ') + s++; + c=g_utf8_get_char(s); + } + else + { + s=NULL; + c='\0'; + } + if (pending->illustration.warning_text) + { + if (aline) + { + if (pswit[ECHO_SWITCH] && !pending->illustration.queried_line) + pending->illustration.queried_line=g_strdup(parastart); + } + else + { + if (!pswit[OVERVIEW_SWITCH]) + { + if (pending->illustration.queried_line) + g_print("\n%s\n",pending->illustration.queried_line); + g_print("%s\n",pending->illustration.warning_text); + } + else + cnt_brack++; + g_free(pending->illustration.warning_text); + pending->illustration.warning_text=NULL; + g_free(pending->illustration.queried_line); + pending->illustration.queried_line=NULL; + } + } + if (pending->dquote) + { + if (c!=CHAR_DQUOTE || pswit[QPARA_SWITCH]) + { + if (!pswit[OVERVIEW_SWITCH]) + { + if (pswit[ECHO_SWITCH]) + g_print("\n%s\n",parastart); + g_print("%s\n",pending->dquote); + } + else + cnt_dquot++; + } + g_free(pending->dquote); + pending->dquote=NULL; + } + if (pending->squote) + { + if (!CHAR_IS_SQUOTE(c) || pswit[QPARA_SWITCH] || pending->squot) + { + if (!pswit[OVERVIEW_SWITCH]) + { + if (pswit[ECHO_SWITCH]) + g_print("\n%s\n",parastart); + g_print("%s\n",pending->squote); + } + else + cnt_squot++; + } + g_free(pending->squote); + pending->squote=NULL; + } + if (pending->rbrack) + { + if (!pswit[OVERVIEW_SWITCH]) + { + if (pswit[ECHO_SWITCH]) + g_print("\n%s\n",parastart); + g_print("%s\n",pending->rbrack); + } + else + cnt_brack++; + g_free(pending->rbrack); + pending->rbrack=NULL; + } + if (pending->sbrack) + { + if (!pswit[OVERVIEW_SWITCH]) + { + if (pswit[ECHO_SWITCH]) + g_print("\n%s\n",parastart); + g_print("%s\n",pending->sbrack); + } + else + cnt_brack++; + g_free(pending->sbrack); + pending->sbrack=NULL; + } + if (pending->cbrack) + { + if (!pswit[OVERVIEW_SWITCH]) + { + if (pswit[ECHO_SWITCH]) + g_print("\n%s\n",parastart); + g_print("%s\n",pending->cbrack); + } + else + cnt_brack++; + g_free(pending->cbrack); + pending->cbrack=NULL; + } + if (pending->unders) + { + if (!pswit[OVERVIEW_SWITCH]) + { + if (pswit[ECHO_SWITCH]) + g_print("\n%s\n",parastart); + g_print("%s\n",pending->unders); + } + else + cnt_brack++; + g_free(pending->unders); + pending->unders=NULL; + } +} + +void reset_pending(struct pending *pending) +{ + memset(pending,0,sizeof(*pending)); +} + +/* + * check_for_mismatched_quotes: + * + * At end of paragraph, check for mismatched quotes. + * + * We don't want to report an error immediately, since it is a + * common convention to omit the quotes at end of paragraph if + * the next paragraph is a continuation of the same speaker. + * Where this is the case, the next para should begin with a + * quote, so we store the warning message and only display it + * at the top of the next iteration if the new para doesn't + * start with a quote. + * The -p switch overrides this default, and warns of unclosed + * quotes on _every_ paragraph, whether the next begins with a + * quote or not. + */ +void check_for_mismatched_quotes(const struct counters *counters, + struct pending *pending) +{ + int squote_straight,squote_curved,difference; + if (counters->quot%2) + pending->dquote= + g_strdup_printf(" Line %ld - Mismatched quotes",linecnt); + if (pswit[SQUOTE_SWITCH]) + { + if (matching_count(counters,CHAR_SQUOTE,TRUE)) + squote_straight=matching_difference(counters,CHAR_SQUOTE); + else + squote_straight=0; + if (matching_count(counters,CHAR_LS_QUOTE,TRUE)) + squote_curved=matching_difference(counters,CHAR_LS_QUOTE); + else + squote_curved=0; + if (squote_straight || squote_curved) + pending->squote= + g_strdup_printf(" Line %ld - Mismatched singlequotes?", + linecnt); + if (squote_straight && squote_straight!=1 || + squote_curved && squote_curved!=1) + /* + * Flag it to be noted regardless of the + * first char of the next para. + */ + pending->squot=1; + } + difference=matching_difference(counters,COUNTER_ILLUSTRATION); + if (difference) + { + if (difference<0 && pending->illustration.warning_text) + { + difference++; + g_free(pending->illustration.queried_line); + pending->illustration.queried_line=NULL; + g_free(pending->illustration.warning_text); + pending->illustration.warning_text=NULL; + } + if (difference<0) + { + increment_matching(counters,CHAR_OPEN_SBRACK,FALSE); + difference++; + } + if (difference) + { + if (pending->illustration.warning_text) + { + if (!pswit[OVERVIEW_SWITCH]) + { + if (pending->illustration.queried_line) + g_print("\n%s\n",pending->illustration.queried_line); + g_print("%s\n",pending->illustration.warning_text); + } + else + cnt_brack++; + g_free(pending->illustration.warning_text); + } + pending->illustration.warning_text=g_strdup_printf( + " Line %ld - Mismatched illustration tag?",linecnt); + g_free(pending->illustration.queried_line); + pending->illustration.queried_line=NULL; + } + } + if (matching_difference(counters,CHAR_OPEN_RBRACK)) + pending->rbrack= + g_strdup_printf(" Line %ld - Mismatched round brackets?",linecnt); + if (matching_difference(counters,CHAR_OPEN_SBRACK)) + pending->sbrack= + g_strdup_printf(" Line %ld - Mismatched square brackets?",linecnt); + if (matching_difference(counters,CHAR_OPEN_CBRACK)) + pending->cbrack= + g_strdup_printf(" Line %ld - Mismatched curly brackets?",linecnt); + if (counters->c_unders%2) + pending->unders= + g_strdup_printf(" Line %ld - Mismatched underscores?",linecnt); +}