Smart Quotes Written by David Dunham A Sharp 1517 NW 64th St Seattle, WA 98107-2343 206Ð783Ð7404 This note describes the algorithm used by Acta and miniWRITER to generate typographically correct single and double quotes (ÔÕ and ÒÓ) as the user types using the quotes on a standard keyboard (' and "). As the user types, the characters typed are automatically replaced, according to the context of the insertion point, before being inserted into the text. A quote is turned into its left equivalent if it is at the beginning of the text, or if it follows a space, tab, return, or left punctuation (Ô(,Õ Ô[,Õ Ô{,Õ or Ô<Õ). A quote is also considered an opening quote if it follows an opening quote of the opposite type (as in: ÒÔSorryÕ is all you have to say?Ó she asked). In the code fragments below, gTE is a global TextEdit record, c is the char typed by the user, and bit 0 of optionWord indicates whether the user chose to use smart quotes. if (c == '"' && optionWord & 0x0001) { if (left_quote(c)) c = 'Ò'; else c = 'Ó'; } if (c == '\'' && optionWord & 0x0001) { if (left_quote(c)) c = 'Ô'; else c = 'Õ'; } /* Do some Undo processing */ : TEKey(c, gTE); /****************************************************************/ /* */ /* LEFT_QUOTE - Tell if insertion point indicates a left quote */ /* */ /****************************************************************/ left_quote(q) register char q; { register TERec *TE_ptr; register unsigned char c; TE_ptr = *gTE; /* Optimize (note we donÕt need to lock) */ /* See if weÕre at the beginning of the buffer */ if (TE_ptr->selStart == 0) return(TRUE); /* First quote is always a left quote */ /* Get the previous character */ c = (*TE_ptr->hText)[TE_ptr->selStart - 1]; /* Check for space, left thingy, tab, return, or non-breaking space */ if (c == ' ' || c == '(' || c == '[' || c == '{' || c == '<' || c == '\t' || c == '\r' || c == '\xCA') return(TRUE); /* Also check for opening quote of different sort */ if (c == 'Ô' && q == '"') return(TRUE); if (c == 'Ò' && q == '\'') return(TRUE); return(FALSE); } This code may be used freely under the following conditions: the author (David Dunham) receives credit in the manual, a beta copy of the program with smart quotes implemented, and a final copy of the program with smart quotes implemented. The smart quotes feature should be presented to the user in a ÒpreferencesÓ or ÒoptionsÓ dialog. A suggested method for overriding the smart quotes option is x' and x" (the latter is useful for entering a value in inches). Note that the override to the dumb quotes option is to use the [Option] key with the appropriate keys. The term Òsmart quotesÓ should refer to the process of curving the quotes as the user types, not the resulting typographerÕs quotes.