+ delete iss_;
+}
+
+
+void Parser::deparse()
+{
+ string s;
+ for(size_type i = pos_ ; i < tokens_.size() ; ++i) {
+ s += tokens_[i].asInput();
+ }
+ is_.putback(from_utf8(s));
+ tokens_.erase(tokens_.begin() + pos_, tokens_.end());
+ // make sure that next token is read
+ tokenize_one();
+}
+
+
+bool Parser::setEncoding(std::string const & e, int const & p)
+{
+ // We may (and need to) use unsafe encodings here: Since the text is
+ // converted to unicode while reading from is_, we never see text in
+ // the original encoding of the parser, but operate on utf8 strings
+ // instead. Therefore, we cannot misparse high bytes as {, } or \\.
+ Encoding const * const enc = encodings.fromLaTeXName(e, p, true);
+ if (!enc) {
+ cerr << "Unknown encoding " << e << ". Ignoring." << std::endl;
+ return false;
+ }
+ return setEncoding(enc->iconvName());
+}
+
+
+void Parser::catInit()
+{
+ if (curr_cat_ == theCatcodesType_)
+ return;
+ curr_cat_ = theCatcodesType_;
+
+ fill(theCatcode_, theCatcode_ + 256, catOther);
+ fill(theCatcode_ + 'a', theCatcode_ + 'z' + 1, catLetter);
+ fill(theCatcode_ + 'A', theCatcode_ + 'Z' + 1, catLetter);
+ // This is wrong!
+ theCatcode_[int('@')] = catLetter;
+
+ if (theCatcodesType_ == NORMAL_CATCODES) {
+ theCatcode_[int('\\')] = catEscape;
+ theCatcode_[int('{')] = catBegin;
+ theCatcode_[int('}')] = catEnd;
+ theCatcode_[int('$')] = catMath;
+ theCatcode_[int('&')] = catAlign;
+ theCatcode_[int('\n')] = catNewline;
+ theCatcode_[int('#')] = catParameter;
+ theCatcode_[int('^')] = catSuper;
+ theCatcode_[int('_')] = catSub;
+ theCatcode_[0x7f] = catIgnore;
+ theCatcode_[int(' ')] = catSpace;
+ theCatcode_[int('\t')] = catSpace;
+ theCatcode_[int('\r')] = catNewline;
+ theCatcode_[int('~')] = catActive;
+ theCatcode_[int('%')] = catComment;
+ }
+}
+
+CatCode Parser::catcode(char_type c) const
+{
+ if (c < 256)
+ return theCatcode_[(unsigned char)c];
+ return catOther;
+}
+
+
+void Parser::setCatcode(char c, CatCode cat)
+{
+ theCatcode_[(unsigned char)c] = cat;
+ deparse();
+}
+
+
+void Parser::setCatcodes(cat_type t)
+{
+ theCatcodesType_ = t;
+ deparse();
+}
+
+
+bool Parser::setEncoding(std::string const & e)
+{
+ //cerr << "setting encoding to " << e << std::endl;
+ encoding_iconv_ = e;
+ // If the encoding is fixed, we must not change the stream encoding
+ // (because the whole input uses that encoding, e.g. if it comes from
+ // the clipboard). We still need to track the original encoding in
+ // encoding_iconv_, so that the generated output is correct.
+ if (!fixed_enc_)
+ is_.setEncoding(e);
+ return true;