Is there any easy way to automatically highlight dates in a certain color in any text file (.txt file) opened in Komodo Edit? For example, I would like strings like 06/06/2017 to be highlighted in such a way that dates can be easily spotted in text files.
I could write a regular expression to match dates, but I am not sure about the best way to change the color visualized in the editor. I was thinking about defining a trigger with a JavaScript code to execute every time a text file is loaded or saved and that manually checks the whole contents of the file, but this might be a slow solution.
Maybe the color scheme editor could help, but I’m not sure how I could use it in this case, as the highlighting should apply to regular plain text files.
@s1617, Would it be an option to perform a “Find in file” action? To make it a little faster you could dump the code that executes a find into a Userscript, then add a key binding to it so you don’t have to open the find dialog every time.
Here’s some code that should do that:
function findThis()
{
var findSvc = Cc["@activestate.com/koFindService;1"]
.getService(Ci.koIFindService);
var patternType = findSvc.options.patternType;
// Set that you want to use a regex as the pattern
findSvc.options.patternType = Ci.koIFindOptions.FOT_REGEX_PYTHON;
var context = Cc["@activestate.com/koFindContext;1"]
.createInstance(Ci.koIFindContext);
// And you want to find the pattern in the current file
context.type = Ci.koIFindContext.FCT_CURRENT_DOC;
var pattern = "\\d{2}/\\d{2}/\\d{4}"; // Since we pass in a string you have to escape the escapes
// And away we go
ko.find.findAll(require("ko/windows").getMain(), context, pattern);
// Reset findSvc option so your find dialog doesn't inherit these settings
findSvc.options.patternType = patternType;
}
findThis();
Put the above code in a Userscript in your toolbox and try running it where you have dates formatted as 01/01/1990.
I am trying to debug it. It seems that there is some problem with the regular expression, as I find no match. I also changed the pattern to “\d{1,2}/\d{1,2}/\d{4}” (to allow 1 or 2 digits for the day and month, in case the first one is 0 and omitted).
A minor problem with this solution is that it opens a tab with the search results, every time the script is executed.
Therefore, I was thinking about other alternatives also. Maybe if instead of highlighting a date I want to highlight other fix tokens it would be possible to find an easy and unobtrusive solution. For example, syntax highlighting in a JavaScript file colors lines starting with // (comments). Can I do something similar with plain text files? In this way, I could just add a special fixed symbol before a date, like //, and automatically benefit from that highlighting.
Use the regex tool in komodo to test your regex and input, Tools > Rx Toolkit. The regex I shared worked for the format I stated but I wasn’t intending for it to work for all dates. It was just an example.
To accomplish your suggestion of syntax highlighting dates you would have to implement a new UDL (user defined language) and load it into Komodo. It’s not an easier solution unfortunately though it is possible. I’m not sure how to do that though. @mitchell, are there good resources for explaining that if @s1617 wants to go that route.