bookloupe/bookloupe.c
changeset 123 ddb5ddba6ef3
parent 103 adc06e9e8470
child 128 f3c293593d44
child 132 237b058061f2
     1.1 --- a/bookloupe/bookloupe.c	Mon Sep 23 21:18:27 2013 +0100
     1.2 +++ b/bookloupe/bookloupe.c	Fri Sep 27 07:13:06 2013 +0100
     1.3 @@ -160,8 +160,7 @@
     1.4      { NULL }
     1.5  };
     1.6  
     1.7 -long cnt_dquot;		/* for overview mode, count of doublequote queries */
     1.8 -long cnt_squot;		/* for overview mode, count of singlequote queries */
     1.9 +long cnt_quote;		/* for overview mode, count of quote queries */
    1.10  long cnt_brack;		/* for overview mode, count of brackets queries */
    1.11  long cnt_bin;		/* for overview mode, count of non-ASCII queries */
    1.12  long cnt_odd;		/* for overview mode, count of odd character queries */
    1.13 @@ -407,10 +406,8 @@
    1.14  	    g_print("    Line-end problems:	     %14ld\n",cnt_lineend);
    1.15  	if (cnt_word)
    1.16  	    g_print("    Common typos:		  %14ld\n",cnt_word);
    1.17 -	if (cnt_dquot)
    1.18 -	    g_print("    Unmatched quotes:	      %14ld\n",cnt_dquot);
    1.19 -	if (cnt_squot)
    1.20 -	    g_print("    Unmatched SingleQuotes:	%14ld\n",cnt_squot);
    1.21 +	if (cnt_quote)
    1.22 +	    g_print("    Unmatched quotes:	      %14ld\n",cnt_quote);
    1.23  	if (cnt_brack)
    1.24  	    g_print("    Unmatched brackets:	    %14ld\n",cnt_brack);
    1.25  	if (cnt_bin)
    1.26 @@ -425,8 +422,8 @@
    1.27  	    g_print("    Possible HTML tags:	    %14ld\n",cnt_html);
    1.28  	g_print("\n");
    1.29  	g_print("    TOTAL QUERIES		  %14ld\n",
    1.30 -	  cnt_dquot+cnt_squot+cnt_brack+cnt_bin+cnt_odd+cnt_long+
    1.31 -	  cnt_short+cnt_punct+cnt_dash+cnt_word+cnt_html+cnt_lineend);
    1.32 +	  cnt_quote+cnt_brack+cnt_bin+cnt_odd+cnt_long+cnt_short+cnt_punct+
    1.33 +	  cnt_dash+cnt_word+cnt_html+cnt_lineend);
    1.34      }
    1.35      g_free(running_from);
    1.36      if (usertypo)
    1.37 @@ -788,7 +785,7 @@
    1.38   *
    1.39   * Returns: TRUE if the line is empty.
    1.40   */
    1.41 -gboolean analyse_quotes(const char *aline,struct counters *counters)
    1.42 +gboolean analyse_quotes(const char *aline,int linecnt,struct counters *counters)
    1.43  {
    1.44      int guessquote=0;
    1.45      /* assume the line is empty until proven otherwise */
    1.46 @@ -796,23 +793,24 @@
    1.47      const char *s=aline,*sprev,*snext;
    1.48      gunichar c;
    1.49      sprev=NULL;
    1.50 +    GError *tmp_err=NULL;
    1.51      while (*s)
    1.52      {
    1.53  	snext=g_utf8_next_char(s);
    1.54  	c=g_utf8_get_char(s);
    1.55 -	if (c==CHAR_DQUOTE)
    1.56 -	    counters->quot++;
    1.57 -	if (CHAR_IS_SQUOTE(c))
    1.58 +	if (CHAR_IS_DQUOTE(c))
    1.59 +	    (void)count_quote(counters,c,QUOTE_CLASS(c),&tmp_err);
    1.60 +	else if (CHAR_IS_SQUOTE(c) && pswit[SQUOTE_SWITCH])
    1.61  	{
    1.62  	    if (s==aline)
    1.63  	    {
    1.64  		/*
    1.65 -		 * At start of line, it can only be an openquote.
    1.66 +		 * At start of line, it can only be a quotation mark.
    1.67  		 * Hardcode a very common exception!
    1.68  		 */
    1.69  		if (!g_str_has_prefix(snext,"tis") &&
    1.70  		  !g_str_has_prefix(snext,"Tis"))
    1.71 -		    increment_matching(counters,c,TRUE);
    1.72 +		    (void)count_quote(counters,c,NEUTRAL_QUOTE,&tmp_err);
    1.73  	    }
    1.74  	    else if (g_unichar_isalpha(g_utf8_get_char(sprev)) &&
    1.75  	      g_unichar_isalpha(g_utf8_get_char(snext)))
    1.76 @@ -822,15 +820,20 @@
    1.77  	    else if (c==CHAR_OPEN_SQUOTE || c==CHAR_LS_QUOTE ||
    1.78  	      g_unichar_isalpha(g_utf8_get_char(snext)))
    1.79  	    {
    1.80 -		/* it damwell better BE an openquote */
    1.81 +		/* certainly looks like a quotation mark */
    1.82  		if (!g_str_has_prefix(snext,"tis") &&
    1.83  		  !g_str_has_prefix(snext,"Tis"))
    1.84  		    /* hardcode a very common exception! */
    1.85 -		    increment_matching(counters,c,TRUE);
    1.86 +		{
    1.87 +		    if (strchr(".?!,;:",g_utf8_get_char(sprev)))
    1.88 +			(void)count_quote(counters,c,NEUTRAL_QUOTE,&tmp_err);
    1.89 +		    else
    1.90 +			(void)count_quote(counters,c,OPENING_QUOTE,&tmp_err);
    1.91 +		}
    1.92  	    }
    1.93  	    else
    1.94  	    {
    1.95 -		/* now - is it a closequote? */
    1.96 +		/* now - is it a quotation mark? */
    1.97  		guessquote=0;   /* accumulate clues */
    1.98  		if (g_unichar_isalpha(g_utf8_get_char(sprev)))
    1.99  		{
   1.100 @@ -844,25 +847,31 @@
   1.101  			    /* bonus marks! */
   1.102  			    guessquote-=2;
   1.103  		    }
   1.104 +		    if (innermost_quote_matches(counters,c))
   1.105 +			/*
   1.106 +			 * Give it the benefit of some doubt,
   1.107 +			 * if a squote is already open.
   1.108 +			 */
   1.109 +			guessquote++;
   1.110 +		    else
   1.111 +			guessquote--;
   1.112 +		    if (guessquote>=0)
   1.113 +			(void)count_quote(counters,c,CLOSING_QUOTE,&tmp_err);
   1.114  		}
   1.115 -		/* it doesn't have a letter either side */
   1.116 -		else if (strchr(".?!,;:",g_utf8_get_char(sprev)) &&
   1.117 -		  strchr(".?!,;: ",g_utf8_get_char(snext)))
   1.118 -		    guessquote+=8; /* looks like a closequote */
   1.119  		else
   1.120 -		    guessquote++;
   1.121 -		if (matching_difference(counters,CHAR_SQUOTE)>0)
   1.122 -		    /*
   1.123 -		     * Give it the benefit of some doubt,
   1.124 -		     * if a squote is already open.
   1.125 -		     */
   1.126 -		    guessquote++;
   1.127 -		else
   1.128 -		    guessquote--;
   1.129 -		if (guessquote>=0)
   1.130 -		    increment_matching(counters,c,FALSE);
   1.131 +		    /* no adjacent letter - it must be a quote of some kind */
   1.132 +		    (void)count_quote(counters,c,NEUTRAL_QUOTE,&tmp_err);
   1.133  	    }
   1.134  	}
   1.135 +	if (tmp_err)
   1.136 +	{
   1.137 +	    if (pswit[ECHO_SWITCH])
   1.138 +		g_print("\n%s\n",aline);
   1.139 +	    if (!pswit[OVERVIEW_SWITCH])
   1.140 +		g_print("    Line %ld column %ld - %s\n",
   1.141 +		  linecnt,g_utf8_pointer_to_offset(aline,s)+1,tmp_err->message);
   1.142 +	    g_clear_error(&tmp_err);
   1.143 +	}
   1.144  	if (c!=CHAR_SPACE && c!='-' && c!='.' && c!=CHAR_ASTERISK &&
   1.145  	  c!='\r' && c!='\n')
   1.146  	    isemptyline=FALSE;  /* ignore lines like  *  *  *  as spacers */
   1.147 @@ -2396,6 +2405,7 @@
   1.148      gboolean letter_on_line=FALSE;
   1.149      const char *s;
   1.150      gunichar c;
   1.151 +    gboolean closing_quote;
   1.152      for (s=prevline;*s;s=g_utf8_next_char(s))
   1.153  	if (g_unichar_isalpha(g_utf8_get_char(s)))
   1.154  	{
   1.155 @@ -2417,7 +2427,11 @@
   1.156  	{
   1.157  	    s=g_utf8_prev_char(s);
   1.158  	    c=g_utf8_get_char(s);
   1.159 -	} while (CHAR_IS_CLOSING_QUOTE(c) && c>CHAR_SPACE && s>prevline);
   1.160 +	    if (QUOTE_CLASS(c)==CLOSING_QUOTE || QUOTE_CLASS(c)==NEUTRAL_QUOTE)
   1.161 +		closing_quote=TRUE;
   1.162 +	    else
   1.163 +		closing_quote=FALSE;
   1.164 +	} while (closing_quote && s>prevline);
   1.165  	for (;s>prevline;s=g_utf8_prev_char(s))
   1.166  	{
   1.167  	    if (g_unichar_isalpha(g_utf8_get_char(s)))
   1.168 @@ -2548,7 +2562,7 @@
   1.169  	}
   1.170  	checked_linecnt++;
   1.171  	print_pending(aline,parastart,&pending);
   1.172 -	isemptyline=analyse_quotes(aline,&counters);
   1.173 +	isemptyline=analyse_quotes(aline,linecnt,&counters);
   1.174  	if (isnewpara && !isemptyline)
   1.175  	{
   1.176  	    /* This line is the start of a new paragraph. */