Looking for free software including mechanism equivalent to that of Simple Writer

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

mok-kong shen
Posts: 15
Joined: Mon Oct 31, 2016 10:19 am UTC

Looking for free software including mechanism equivalent to that of Simple Writer

Postby mok-kong shen » Sun Dec 11, 2016 11:12 am UTC

I am looking for a free software with which I could implement things equivalent to the mechanism realized by Simple Writer, i.e. checking the words in an input window against a given list of words and turning the colour of those words not in the list from black to red. I tried tkinter of Python, but it appears to be no means there to do that (or perhaps only in a very tedious round-about and hence practically not acceptable way).

User avatar
chridd
Has a vermicelli title
Posts: 730
Joined: Tue Aug 19, 2008 10:07 am UTC
Location: ...Earth, I guess?
Contact:

Re: Looking for free software including mechanism equivalent to that of Simple Writer

Postby chridd » Mon Dec 12, 2016 1:18 am UTC

One way is to use a text editor that has customizable syntax highlighting (like Vim; I assume Emacs would work as well), and set up the syntax highlighting to highlight the words in the list.
~ chri d. d. /tʃɹɪ.di.di/ (Phonotactics, schmphonotactics) · they (for now, at least) · Forum game scores
mittfh wrote:I wish this post was very quotable...
flicky1991 wrote:In both cases the quote is "I'm being quoted too much!"

User avatar
Soupspoon
You have done something you shouldn't. Or are about to.
Posts: 2038
Joined: Thu Jan 28, 2016 7:00 pm UTC
Location: 53-1

Re: Looking for free software including mechanism equivalent to that of Simple Writer

Postby Soupspoon » Mon Dec 12, 2016 2:48 am UTC

OP is asking for non-list words to be highlighted. Simple keyword highlighting would do the opposite crom that asked. (Could be done by setting default formatting as 'highlit' and then highlight with 'no highlighting' format, plus arrange for all non-alphabetic cruft to also be 'dehighlighted', but that last bit might be hard, unless regexpy capabilities exist, in which case there are probably simpler ways to 'highlight if not in list' anyway.)


For the Python Tkinter method, I've a feeling that using a hidden but active (and autofocussed/permanently tied as keyboard-magnet) text input (Entry) field that is tied/whatever, perhaps with a trace lamba method, to a function that then writes1 to a text output (Text) widget, after intermediate processing to identify points of necessary highlighting and SELectively adjust the foreground.

Assuming that Text itself doesn't accept its own 'entry' (not too familiar with Python in Tkinter mode, I'm largely extrapolating my experience with other languages from Delphi to Perl/Tk). But if you do go down this road, you might find it useful to also do something (put a "|" in alternating foreground/background colour on a timer-tick?) to mark the cursor position upon the faux-editor widget.


1 Perhaps anew, from scratch, upon each and every change, perhaps only needing to alter local text.

User avatar
Flumble
Yes Man
Posts: 1835
Joined: Sun Aug 05, 2012 9:35 pm UTC

Re: Looking for free software including mechanism equivalent to that of Simple Writer

Postby Flumble » Mon Dec 12, 2016 6:21 pm UTC

mok-kong shen wrote:checking the words in an input window against a given list of words and turning the colour of those words not in the list from black to red

...or turning the colour of the words that are in the list from red to black. (which is easier to achieve with most highlighting tools)

chridd wrote:One way is to use a text editor that has customizable syntax highlighting (like Vim; I assume Emacs would work as well), and set up the syntax highlighting to highlight the words in the list.

Or if you're not working in a terminal window all day: Notepad++ has an easy-to-use dialog for making custom syntax highlighting, whereas Atom, Sublime Text, VS Code, Brackets and whatnot allow custom highlighting via a configuration file (like vim and emacs).

Spoiler:
I thought of being smug and leveraging the contenteditable attribute of HTML, but it's horrible because every change places the caret at the start (and I'm not escaping HTML special characters). Eh, at least you can paste a word list and some normal text in there to see if anything's wrong.

Code: Select all

<!DOCTYPE html>
<html>
  <head>
    <title>Word highlighter</title>
    <meta charset="UTF-8">
    <style>
      #text {
        width: 100%;
        min-height: 60vh;
        white-space: pre-wrap;
      }
      strong {
        color: red;
      }
    </style>
    <script>
      document.addEventListener("DOMContentLoaded", ev => {
        var textField = document.querySelector("#text");
        var wordField = document.querySelector("#words");
        var wordSet   = new Set(wordField.value.split(/\W+/));

        textField.addEventListener("input", ev => {
          textField.innerHTML = textField.innerText.replace(/\w+/g, word => wordSet.has(word.toLowerCase()) ? word : "<strong>"+word+"</strong>");
        });
       
        wordField.addEventListener("change", ev => {
          wordSet = new Set(wordField.value.split(/\W+/));
        });
      });
    </script>
  </head>
  <body>
    <div id="text" contenteditable></div>
    <textarea id="words" cols="60" rows="20">word, test, the, of, to, and, a, in, is, it, you, that, he, was, for, on, are, with, as, I, his, they, be, at</textarea>
  </body>
</html>


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 7 guests