列表:仅在分隔符内启用语言设置

列表:仅在分隔符内启用语言设置

我希望仅在分隔符内启用语言设置(关键字、字符串等)。

基于 Jubobs 对突出显示单行分隔符的问题的回答的示例。

\documentclass[a4paper, 12pt]{article}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[svgnames]{xcolor}
\usepackage{listings}
\usepackage{newtxtt} 

\newcommand\styleAfterSqlitePrompt{%
    \ttfamily\bfseries\color{red}%
}

% switch to keep track of context (in a command or not)
\newif\ifcom\comfalse

\makeatletter

% This is what happens if a delimiter is encountered
\newcommand\processSqlitePrompt[1]{%
    \ifcom%                       % In this case, we're already in a command.
        % TODO enable SQL as language
    \else%                         % Otherwise, we just started a command;
        \global\comtrue%          % set the switch to true and
        {\lst@basicstyle #1}%     % typeset the delimiter in the basic style.
    \fi
    \styleAfterSqlitePrompt%      % In any case, apply the comment style.
}

\newcommand\processDotCommand{%
    \ifcom%    
       \itshape\color{magenta}% hightlight dotCommand after Sqlite Prompt
    \else%  
        \lst@basicstyle%
    \fi%
}

% Reset the switch at each End-Of-Line character.
\lst@AddToHook{EOL}{\global\comfalse}


\makeatother


\lstnewenvironment{lstSqlite}{%
    \lstset{
        %language     = SQL,
        basicstyle   = \ttfamily\color{black},
        stringstyle  = \sffamily\color{green},
        keywordstyle = \bfseries\sffamily\color{blue},
        moredelim    = **[il][\processSqlitePrompt{sqlite>}]{sqlite>},
        moredelim    = **[il][\processSqlitePrompt{\ \ \ ...>}]{\ \ \ ...>},
        moredelim    = [s][\processDotCommand]{\ .}{\ }, 
    }%
}{}

\begin{document}

\begin{lstSqlite}
$ c:/sqlite/sqlite3.exe
SQLite version 3.9.2 2015-11-02 18:31:45
Enter ".help" for usage hints.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database.
sqlite> .open ex3.db
sqlite> create table words (word varchar(20), desc text);
sqlite> insert into words
   ...> values (".open", "dot-command"),
   ...> ("select", "SQL keyword"),
   ...> ("foo", "common word");
sqlite> .mode insert new_table
sqlite> select * from words;
INSERT INTO new_table VALUES('.open','dot-command');
INSERT INTO new_table VALUES('select','SQL keyword');
INSERT INTO new_table VALUES('foo','common word');
sqlite> .mode list
sqlite> .separator ", "
sqlite> select `desc`, `word` from words
   ...> order by lower(`desc`) desc;
SQL keyword, select
dot-command, .open
common word, foo
\end{lstSqlite}

\end{document}

从而产生。

列表示例

提示后的 Delim 是红色的,点命令是洋红色的,但没有 SQL 突出显示。如果我取消注释

    %language     = SQL,

SQL 语法会在各处突出显示,这很不好,因为在普通文本中可以找到像“INSERT”和“select”这样的词。

相关内容