auto-completion text entering
Hi,
I'm building a patch to tag my daily files more easily. As my
(meta-)tags are predefined, I'd like them to be automatically added in
[textedit] as I start typing in it. The problem is I'm running a bit
out of idea as for how to implement such a feature. Amongst
unsuccessful methods, I tried the regexp route in order to get which
tags from my predefined list matches what's being typed in textedit.
However, my knowledge of regular expressions being still close to
none, I could not manage to do it out of trials and errors.
Does someone have another idea to patch such a feature ? For example :
assuming I have a list of tags such as '&author= &source= &title=
etc', the point is to get '&author=' added in [textedit] when I type
'a' ; '&source=' if I type 's' etc.
Thanks in advance for any hint
Best,
Julien
Hi Julien,
I don't have access to Max right now to validate this approach, but I think you could do something like this with textedit and standard Max objects.
One limitation is textedit can only set the entire text or append. A fairly straightforward approach would be to make the user type '&a' to autocomplete '&author=' by appending 'uthor='. If you really want to type 'a' then it's probably easiest to do something with javascript.
If you're ok with typing '&a', then here's what I would do. I'd rely on the user hitting tab to attempt an autocomplete, rather than doing it fully automatically. I think it will be easier to implement *and* to use if you do it this way. So I'd configure textedit to output when enter is pressed, but not output when tab is pressed (I believe it does the opposite by default).
Say you're typing into textedit and want to autocomplete '&a' -> '&author=', so you type '&a' and then hit tab. The patch will watch the character outlet (the 2nd) for a tab key press. Just figure out what ASCII value is tab and use a [select] to match that. At that point, I'd send an 'enter' message to textedit via a [grab], so we can grab the current contents without triggering normal output. Please note there is some funkiness in Max 5 right now and you may need to use a deferlow or delay until 5.0.5 is release (see this thread https://cycling74.com/forums/index.php?t=msg&goto=148955)
At this point you have the current text in textedit and you just need to get the last two characters to see if an autocomplete should happen. I'm sure there are various ways to do this in Max, maybe regexp will do it if you capture the last 2 characters with parentheses, something like this .*(..)
Then run the last 2 characters through a [coll] that maps characters to autocompletion text, if there is match, append it to the textedit. '&a' should match to 'uthor=', you append that and you should be done. One potential problem is you may have an extra tab character in there. If that is an issue, I have some ideas about how to work around that, or maybe it really would be better to do this all in javascript...
And unfortunately this approach will not work if you put the cursor in the middle of some text and try to autocomplete. It will only work when appending at the end of the text. Again, javascript could solve this.
Anyway, maybe that will give you some ideas. If you are stuck, I might have some time to try this later.
Sorry, I forgot. We are not supposed to use the 'enter' message for textedit. So when I said send 'enter' -> [grab] -> [textedit], that should have been a 'bang'. That's discussed in the thread I referred to.
bonsoir julien
have a look at this patch, it's pretty much Adam way but I read his post
after finishing m example so... here is it ;)
Ciao
ps: you have to type a "key" to trigger autocompletion, here "&a" will
be replaced by "&author", etc...
max v2;
... slightly simpler :