Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Highlight numbers like keywords in a Notepad++ custom language (for access logs)

I want to write a custom language for access logs in Notepad++.

The Problem is that numbers (here: HTTP status codes) won't be highlighted like real keywords (i.e. GET). Notepad++ only provides a highlight color for numbers in general.

How do I handle numbers like text?

Sample log file

192.23.0.9 - - [10/Sep/2012:13:46:42 +0200] "GET /js/jquery-ui.custom.min.js HTTP/1.1" 200 206731
192.23.0.9 - - [10/Sep/2012:13:46:43 +0200] "GET /js/onmediaquery.min.js HTTP/1.1" 200 1229
192.23.0.9 - - [10/Sep/2012:13:46:43 +0200] "GET /en/contact HTTP/1.1" 200 12836
192.23.0.9 - - [10/Sep/2012:13:46:44 +0200] "GET /en/imprint HTTP/1.1" 200 17380
192.23.0.9 - - [10/Sep/2012:13:46:46 +0200] "GET /en/nothere HTTP/1.1" 404 2785

Sample custom languages
http://sourceforge.net/apps/mediawiki/notepad-plus/index.php?title=User_Defined_Language_Files

I also tried editing and importing a predefined language like this:
http://notepad-plus.sourceforge.net/commun/userDefinedLang/Log4Net.xml

I thought the custom language should look like this:

<KeywordLists>
[...]
    <Keywords name="Words1">404 501</Keywords>
    <Keywords name="Words2">301 303</Keywords>
    <Keywords name="Words3">200</Keywords>
</KeywordLists>

<Styles>
    <WordsStyle name="DEFAULT" styleID="11" fgColor="000000" bgColor="FFFFFF" colorStyle="0" fontName="Courier New" fontStyle="0"/>
    [...]
    <WordsStyle name="KEYWORD1" styleID="5" fgColor="FF0000" bgColor="FFFFFF" colorStyle="1" fontName="" fontStyle="0"/>
    <WordsStyle name="KEYWORD2" styleID="6" fgColor="0000FF" bgColor="FFFFFF" colorStyle="1" fontName="" fontStyle="1"/>
    <WordsStyle name="KEYWORD3" styleID="7" fgColor="00FF00" bgColor="FFFFFF" colorStyle="1" fontName="" fontStyle="0"/>
    [...]

    // This line causes number highlighting. Deletion doesn't work either.
    <WordsStyle name="NUMBER" styleID="4" fgColor="0F7F00" bgColor="FFFFFF" fontName="" fontStyle="0"/>
</Styles>

Unfortunately numbers will be colored in the same color.

I'd like to color them like this:
Sample highlighting of numbers in the access log

etc.

Any suggestions? How to handle the numbers like keywords?

like image 967
Smamatti Avatar asked Sep 13 '12 09:09

Smamatti


People also ask

How do you highlight all words in notepad?

If you want to highlight a whole line of text, move your cursor to the start of the line, hold the Shift key, and then press the Down arrow . You may also use the shortcut key combination Shift + End . If you want to highlight all text (the entire page), press the shortcut key Ctrl + A .

Does Notepad have syntax highlighting?

Like other text editors, Notepad++ (which is recommended by the basic tutorial) uses a system called Syntax Highlighting which helps in coding by visually identifying different parts of code with color.

How do I add syntax highlights to Notepad?

To configure syntax highlighting, click on “Language” in the top bar, then click the letter the language starts with, and then the language. If you want to define your own language, click on “Language” again, then click on “User Defined Language”, third from the bottom, and then click “Define your language”.


1 Answers

It isn't possible to highlight numbers as keywords as the built-in lexers (parsers/language definitions) use a numeric as a token meaning that the only way to differentiate between a numeric and your keyword would be to parse the whole numeric block and then compare to the keyword list, in which case it becomes required to also parse the delimiters around the numeric block to ensure that .200. doesn't highlight as 200. This is why your numbers all highlighted as the same color; namely the 'number' color.

While this could be done using a custom lexer using either fixed position tokens or regex matching you'll find the user defined languages (the last I heard) do not have this capability.

As your request is actually a fairly simple, from what I understand, being as general as possible ( as requested in your comment )...

Highlight space delimited numeric values contained in a given set.

    [[:space:]](200|301|404)[[:space:]]

We can use the 'Mark' feature of the 'Find' dialog with that regex but then everything is marked the same color like with your failed experiment.

Perhaps what would be simple and suit your needs would be to use a npp pythonscript and the Mark Style settings in the Style Configurator to get the desired result?

something like this crude macro style:

from Npp import *

def found(line, m):
    global first
    pos = editor.positionFromLine(line)
    if first:
        editor.setSelection(pos + m.end(), pos + m.start())
        first = False
    else:
        editor.addSelection(pos + m.end(), pos + m.start())


editor.setMultipleSelection(True)
lines = editor.getUserLineSelection()

# Use space padded search since MARKALLEXT2 will act just
# like the internal lexer if only the numeric is selected
# when it is called.

first = True
editor.pysearch( " 200 ", found, 0, lines[0], lines[1])
notepad.menuCommand(MENUCOMMAND.SEARCH_MARKALLEXT1)

first = True
editor.pysearch( " 301 ", found, 0, lines[0], lines[1])
notepad.menuCommand(MENUCOMMAND.SEARCH_MARKALLEXT2)

first = True
editor.pysearch( " 404 ", found, 0, lines[0], lines[1])
notepad.menuCommand(MENUCOMMAND.SEARCH_MARKALLEXT3)

Which, to use, just use the plugin manager to install Python Script, go to the plugin menu and select New Script then paste, save, select the tab for the doc you want to parse, and execute the script (once again from the plugin menu).

Obviously you could use all 5 Mark styles for different terms, you could assign to a shortcut, and you could get more into the 'scripting' -vs- 'macro' style of nppPython and make a full blown script to parse whatever you want... shoot having a script trigger whenever you select a particular lexer style is doable too.

like image 119
Thell Avatar answered Oct 11 '22 14:10

Thell