⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tex.cpp

📁 一个完整的编辑器的代码(很值得参考
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    _T ("AtBeginDocument"),
    _T ("AtBeginDvi"),
    _T ("AtEndDocument"),
    _T ("AtEndOfPackage"),
    _T ("Big"),
    _T ("Bigg"),
    _T ("Box"),
    _T ("CheckCommand"),
    _T ("ClassError"),
    _T ("ClassInfo"),
    _T ("ClassWarning"),
    _T ("ClassWarningNoLine"),
    _T ("DeclareErrorFont"),
    _T ("DeclareFixedFont"),
    _T ("DeclareFontEncoding"),
    _T ("DeclareFontEncodingDefaults"),
    _T ("DeclareFontFamily"),
    _T ("DeclareFontShape"),
    _T ("DeclareFontSubstitution"),
    _T ("DeclareMathAccent"),
    _T ("DeclareMathAlphabet"),
    _T ("DeclareMathDelimiter"),
    _T ("DeclareMathRadical"),
    _T ("DeclareMathSizes"),
    _T ("DeclareMathSymbol"),
    _T ("DeclareMathVersion"),
    _T ("DeclareOldFontCommand"),
    _T ("DeclareOption"),
    _T ("DeclarePreloadSizes"),
    _T ("DeclareRobustCommand"),
    _T ("DeclareSizeFunction"),
    _T ("DeclareSymbolFont"),
    _T ("DeclareSymbolFontAlphabet"),
    _T ("DeclareTextAccent"),
    _T ("DeclareTextAccentDefault"),
    _T ("DeclareTextCommand"),
    _T ("DeclareTextCommandDefault"),
    _T ("DeclareTextComposite"),
    _T ("DeclareTextCompositeCommand"),
    _T ("DeclareTextFontCommand"),
    _T ("DeclareTextSymbol"),
    _T ("DeclareTextSymbolDefault"),
    _T ("ExecuteOptions"),
    _T ("IfFileExists"),
    _T ("InputIfFileExists"),
    _T ("Join"),
    _T ("LoadClass"),
    _T ("LoadClassWithOptions"),
    _T ("MessageBreak"),
    _T ("NeedsTeXFormat"),
    _T ("OptionNotUsed"),
    _T ("PackageError"),
    _T ("PackageInfo"),
    _T ("PackageWarning"),
    _T ("PackageWarningNoLine"),
    _T ("PassOptionsToClass"),
    _T ("PassOptionsToPackage"),
    _T ("ProcessOptions"),
    _T ("ProvideTextCommand"),
    _T ("ProvideTextCommandDefault"),
    _T ("ProvidesFile"),
    _T ("ProvidesPackage"),
    _T ("RequirePackage"),
    _T ("RequirePackageWithOptions"),
    _T ("Roman"),
    _T ("SetMathAlphabet"),
    _T ("SetSymbolFont"),
    _T ("TeX"),
    _T ("TextSymbolUnavailable"),
    _T ("UseTextAccent"),
    _T ("UseTextSymbol"),
    _T ("addcontentsline"),
    _T ("addpenalty"),
    _T ("addtocontents"),
    _T ("addtoversion"),
    _T ("addvspace"),
    _T ("afterassignment"),
    _T ("allocationnumber"),
    _T ("allowbreak"),
    _T ("alph"),
    _T ("arrayrulewidth"),
    _T ("arraystretch"),
    _T ("baselinestretch"),
    _T ("bezier"),
    _T ("bfdefault"),
    _T ("bfseries"),
    _T ("bibcite"),
    _T ("bibliography"),
    _T ("bibliographystyle"),
    _T ("big"),
    _T ("bigbreak"),
    _T ("bigg"),
    _T ("bigskip"),
    _T ("bigskipamount"),
    _T ("bigskipamount"),
    _T ("bmod"),
    _T ("boldmath"),
    _T ("buildrel"),
    _T ("cases"),
    _T ("catcode"),
    _T ("center"),
    _T ("centering"),
    _T ("chardef"),
    _T ("cleardoublepage"),
    _T ("contentsline"),
    _T ("countdef"),
    _T ("cr"),
    _T ("csname"),
    _T ("depth"),
    _T ("discretionary"),
    _T ("displaymath"),
    _T ("dospecials"),
    _T ("dotfill"),
    _T ("encodingdefault"),
    _T ("endcsname"),
    _T ("everycr"),
    _T ("expandafter"),
    _T ("expandafter"),
    _T ("extracolsep"),
    _T ("familydefault"),
    _T ("familydefault"),
    _T ("fbox"),
    _T ("filbreak"),
    _T ("flushleft"),
    _T ("flushright"),
    _T ("fmtname"),
    _T ("fmtversion"),
    _T ("fontencoding"),
    _T ("fontfamily"),
    _T ("fontseries"),
    _T ("fontshape"),
    _T ("footnotemark"),
    _T ("footnoterule"),
    _T ("footnotetext"),
    _T ("frac"),
    _T ("frenchspacing"),
    _T ("fussy"),
    _T ("futurelet"),
    _T ("global"),
    _T ("glossary"),
    _T ("halign"),
    _T ("height"),
    _T ("hphantom"),
    _T ("ifinner"),
    _T ("immediate"),
    _T ("include"),
    _T ("includeonly"),
    _T ("itdefault"),
    _T ("iterate"),
    _T ("kern"),
    _T ("lastskip"),
    _T ("leadsto"),
    _T ("leavevmode"),
    _T ("lefteqn"),
    _T ("leftline"),
    _T ("leftmark"),
    _T ("let"),
    _T ("lhd"),
    _T ("liminf"),
    _T ("lineskip"),
    _T ("lower"),
    _T ("lowercase"),
    _T ("makeatletter"),
    _T ("makeatother"),
    _T ("makeglossary"),
    _T ("makeindex"),
    _T ("marginpar"),
    _T ("markboth"),
    _T ("markright"),
    _T ("mathclose"),
    _T ("mathop"),
    _T ("mathopen"),
    _T ("mathrel"),
    _T ("mathstrut"),
    _T ("mddefault"),
    _T ("medbreak"),
    _T ("medmuskip"),
    _T ("medskip"),
    _T ("mho"),
    _T ("minipage"),
    _T ("mskip"),
    _T ("multispan"),
    _T ("narrower"),
    _T ("negthinspace"),
    _T ("newfont"),
    _T ("newhelp"),
    _T ("newif"),
    _T ("newinsert"),
    _T ("newlabel"),
    _T ("newlanguage"),
    _T ("newline"),
    _T ("newmathalphabet"),
    _T ("newmuskip"),
    _T ("newskip"),
    _T ("newtoks"),
    _T ("nocite"),
    _T ("nocorrlist"),
    _T ("noexpand"),
    _T ("nointerlineskip"),
    _T ("nolimits"),
    _T ("nonfrenchspacing"),
    _T ("nonumber"),
    _T ("normalbaselines"),
    _T ("normallineskip"),
    _T ("normalmarginpar"),
    _T ("null"),
    _T ("numberline"),
    _T ("obeylines"),
    _T ("obeyspaces"),
    _T ("offinterlineskip"),
    _T ("onecolumn"),
    _T ("openup"),
    _T ("pagenumberin"),
    _T ("parbox"),
    _T ("penalty"),
    _T ("phantom"),
    _T ("pmod"),
    _T ("protect"),
    _T ("providecommand"),
    _T ("raggedbottom"),
    _T ("raggedleft"),
    _T ("raggedright"),
    _T ("refstepcounter"),
    _T ("relax"),
    _T ("removelastskip"),
    _T ("renewenvironment"),
    _T ("reversemarginpar"),
    _T ("rhd"),
    _T ("rightmark"),
    _T ("rlap"),
    _T ("rmdefault"),
    _T ("roman"),
    _T ("root"),
    _T ("rq"),
    _T ("rule"),
    _T ("scdefault"),
    _T ("secdef"),
    _T ("selectfont"),
    _T ("seriesdefault"),
    _T ("settodepth"),
    _T ("settoheight"),
    _T ("sfdefault"),
    _T ("shapedefault"),
    _T ("showhyphens"),
    _T ("showoutput"),
    _T ("showoverfull"),
    _T ("sldefault"),
    _T ("sloppy"),
    _T ("sloppypar"),
    _T ("smallbreak"),
    _T ("space"),
    _T ("spacefactor"),
    _T ("sqsubset"),
    _T ("stackrel"),
    _T ("stretch"),
    _T ("strut"),
    _T ("suppressfloats"),
    _T ("symbol"),
    _T ("tabskip"),
    _T ("thanks"),
    _T ("thefootnote"),
    _T ("thickmuskip"),
    _T ("thinmuskip"),
    _T ("thinspace"),
    _T ("today"),
    _T ("tracingfonts"),
    _T ("tracingoutput"),
    _T ("trivlist"),
    _T ("ttdefault"),
    _T ("twocolumn"),
    _T ("typein"),
    _T ("typeout"),
    _T ("unboldmath"),
    _T ("undefinedpagestyle"),
    _T ("underbar"),
    _T ("underline"),
    _T ("unlhd"),
    _T ("unrhd"),
    _T ("updefault"),
    _T ("usecounter"),
    _T ("usefont"),
    _T ("usepackage"),
    _T ("vcenter"),
    _T ("verb"),
    _T ("verbatim"),
    _T ("vline"),
    _T ("vphantom"),
    _T ("width"),
    _T ("wlog"),
    _T ("xdef"),
    NULL
  };

static BOOL
IsXKeyword (LPTSTR apszKeywords[], LPCTSTR pszChars, int nLength)
{
  for (int L = 0; apszKeywords[L] != NULL; L++)
    {
      if (_tcsnicmp (apszKeywords[L], pszChars, nLength) == 0
            && apszKeywords[L][nLength] == 0)
        return TRUE;
    }
  return FALSE;
}

static BOOL
IsTexKeyword (LPCTSTR pszChars, int nLength)
{
  return IsXKeyword (s_apszTexKeywordList, pszChars, nLength);
}

static BOOL
IsUser1Keyword (LPCTSTR pszChars, int nLength)
{
  return IsXKeyword (s_apszUser1KeywordList, pszChars, nLength);
}

static BOOL
IsTexNumber (LPCTSTR pszChars, int nLength)
{
  if (nLength > 2 && pszChars[0] == '0' && pszChars[1] == 'x')
    {
      for (int I = 2; I < nLength; I++)
        {
          if (_istdigit (pszChars[I]) || (pszChars[I] >= 'A' && pszChars[I] <= 'F') ||
                (pszChars[I] >= 'a' && pszChars[I] <= 'f'))
            continue;
          return FALSE;
        }
      return TRUE;
    }
  if (!_istdigit (pszChars[0]))
    return FALSE;
  for (int I = 1; I < nLength; I++)
    {
      if (!_istdigit (pszChars[I]) && pszChars[I] != '+' &&
            pszChars[I] != '-' && pszChars[I] != '.' && pszChars[I] != 'e' &&
            pszChars[I] != 'E')
        return FALSE;
    }
  return TRUE;
}

#define DEFINE_BLOCK(pos, colorindex)   \
ASSERT((pos) >= 0 && (pos) <= nLength);\
if (pBuf != NULL)\
  {\
    if (nActualItems == 0 || pBuf[nActualItems - 1].m_nCharPos <= (pos)){\
        pBuf[nActualItems].m_nCharPos = (pos);\
        pBuf[nActualItems].m_nColorIndex = (colorindex);\
        nActualItems ++;}\
  }

#define COOKIE_COMMENT          0x0001
#define COOKIE_PREPROCESSOR     0x0002
#define COOKIE_EXT_COMMENT      0x0004
#define COOKIE_STRING           0x0008
#define COOKIE_CHAR             0x0010

DWORD CCrystalTextView::
ParseLineTex (DWORD dwCookie, int nLineIndex, TEXTBLOCK * pBuf, int &nActualItems)
{
  int nLength = GetLineLength (nLineIndex);
  if (nLength <= 1)
    return dwCookie & COOKIE_EXT_COMMENT;

  LPCTSTR pszChars = GetLineChars (nLineIndex);
  BOOL bFirstChar = (dwCookie & ~COOKIE_EXT_COMMENT) == 0;
  BOOL bRedefineBlock = TRUE;
  BOOL bDecIndex = FALSE;
  int nIdentBegin = -1;

  for (int I = 0;; I++)
    {
      if (bRedefineBlock)
        {
          int nPos = I;
          if (bDecIndex)
            nPos--;
          if (dwCookie & (COOKIE_COMMENT | COOKIE_EXT_COMMENT))
            {
              DEFINE_BLOCK (nPos, COLORINDEX_COMMENT);
            }
          else if (dwCookie & (COOKIE_CHAR | COOKIE_STRING))
            {
              DEFINE_BLOCK (nPos, COLORINDEX_STRING);
            }
          else
            {
              if (xisalnum (pszChars[nPos]) || pszChars[nPos] == '.')
                {
                  DEFINE_BLOCK (nPos, COLORINDEX_NORMALTEXT);
                }
              else
                {
                  DEFINE_BLOCK (nPos, COLORINDEX_OPERATOR);
                  bRedefineBlock = TRUE;
                  bDecIndex = TRUE;
                  goto out;
                }
            }
          bRedefineBlock = FALSE;
          bDecIndex = FALSE;
        }
out:

      if (I == nLength)
        break;

      if (dwCookie & COOKIE_COMMENT)
        {
          DEFINE_BLOCK (I, COLORINDEX_COMMENT);
          dwCookie |= COOKIE_COMMENT;
          break;
        }

      //  String constant "...."
      if (dwCookie & COOKIE_STRING)
        {
          if (pszChars[I] == '"' && (I == 0 || I == 1 && pszChars[I - 1] != '\\' || I >= 2 && (pszChars[I - 1] != '\\' || pszChars[I - 1] == '\\' && pszChars[I - 2] == '\\')))
            {
              dwCookie &= ~COOKIE_STRING;
              bRedefineBlock = TRUE;
            }
          continue;
        }

      //  Char constant '..'
      if (dwCookie & COOKIE_CHAR)
        {
          if (pszChars[I] == '\'' && (I == 0 || I == 1 && pszChars[I - 1] != '\\' || I >= 2 && (pszChars[I - 1] != '\\' || pszChars[I - 1] == '\\' && pszChars[I - 2] == '\\')))
            {
              dwCookie &= ~COOKIE_CHAR;
              bRedefineBlock = TRUE;
            }
          continue;
        }

      if (pszChars[I] == '%')
        {
          DEFINE_BLOCK (I, COLORINDEX_COMMENT);
          dwCookie |= COOKIE_COMMENT;
          break;
        }

      //  Normal text
      if (pszChars[I] == '"')
        {
          DEFINE_BLOCK (I, COLORINDEX_STRING);
          dwCookie |= COOKIE_STRING;
          continue;
        }
      if (pszChars[I] == '\'')
        {
          // if (I + 1 < nLength && pszChars[I + 1] == '\'' || I + 2 < nLength && pszChars[I + 1] != '\\' && pszChars[I + 2] == '\'' || I + 3 < nLength && pszChars[I + 1] == '\\' && pszChars[I + 3] == '\'')
          if (!I || !xisalnum (pszChars[I - 1]))
            {
              DEFINE_BLOCK (I, COLORINDEX_STRING);
              dwCookie |= COOKIE_CHAR;
              continue;
            }
        }

      if (bFirstChar)
        {
          if (!isspace (pszChars[I]))
            bFirstChar = FALSE;
        }

      if (pBuf == NULL)
        continue;               //  We don't need to extract keywords,
      //  for faster parsing skip the rest of loop

      if (xisalnum (pszChars[I]) || pszChars[I] == '.')
        {
          if (nIdentBegin == -1)
            nIdentBegin = I;
        }
      else
        {
          if (nIdentBegin >= 0)
            {
              if (IsTexKeyword (pszChars + nIdentBegin, I - nIdentBegin))
                {
                  DEFINE_BLOCK (nIdentBegin, COLORINDEX_KEYWORD);
                }
              else if (IsUser1Keyword (pszChars + nIdentBegin, I - nIdentBegin))
                {
                  DEFINE_BLOCK (nIdentBegin, COLORINDEX_USER1);
                }
              else if (IsTexNumber (pszChars + nIdentBegin, I - nIdentBegin))
                {
                  DEFINE_BLOCK (nIdentBegin, COLORINDEX_NUMBER);
                }
              else
                {
                  bool bFunction = FALSE;

                  if (!bFunction)
                    {
                      for (int j = I; j >= 0; j--)
                        {
                          if (!isspace (pszChars[j]))
                            {
                              if (pszChars[j] == '$')
                                {
                                  bFunction = TRUE;
                                }
                              break;
                            }
                        }
                    }
                  if (bFunction)
                    {
                      DEFINE_BLOCK (nIdentBegin, COLORINDEX_FUNCNAME);
                    }
                }
              bRedefineBlock = TRUE;
              bDecIndex = TRUE;
              nIdentBegin = -1;
            }
        }
    }

  if (nIdentBegin >= 0)
    {
      if (IsTexKeyword (pszChars + nIdentBegin, I - nIdentBegin))
        {
          DEFINE_BLOCK (nIdentBegin, COLORINDEX_KEYWORD);
        }
      else if (IsUser1Keyword (pszChars + nIdentBegin, I - nIdentBegin))
        {
          DEFINE_BLOCK (nIdentBegin, COLORINDEX_USER1);
        }
      else if (IsTexNumber (pszChars + nIdentBegin, I - nIdentBegin))
        {
          DEFINE_BLOCK (nIdentBegin, COLORINDEX_NUMBER);
        }
      else
        {
          bool bFunction = FALSE;

          if (!bFunction)
            {
              for (int j = I; j >= 0; j--)
                {
                  if (!isspace (pszChars[j]))
                    {
                      if (pszChars[j] == '$')
                        {
                          bFunction = TRUE;
                        }
                      break;
                    }
                }
            }
          if (bFunction)
            {
              DEFINE_BLOCK (nIdentBegin, COLORINDEX_FUNCNAME);
            }
        }
    }

  dwCookie &= COOKIE_EXT_COMMENT;
  return dwCookie;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -