buffer: [], // Stores previously run commands\r
buffer_pos: 0, // The current position in the buffer\r
\r
+ // Hold tab autocomplete data\r
+ tabcomplete: {active: false, data: [], prefix: ''},\r
+\r
events: {\r
'keydown input': 'process'\r
},\r
},\r
\r
process: function (ev) {\r
- var inp = $(ev.currentTarget),\r
+ var that = this,\r
+ inp = $(ev.currentTarget),\r
inp_val = inp.val(),\r
meta;\r
\r
} else {\r
meta = ev.altKey;\r
}\r
+\r
+ // If not a tab key, reset the tabcomplete data\r
+ if (this.tabcomplete.active && ev.keyCode !== 9) {\r
+ this.tabcomplete.active = false;\r
+ this.tabcomplete.data = [];\r
+ this.tabcomplete.prefix = '';\r
+ }\r
\r
switch (true) {\r
case (ev.keyCode === 13): // return\r
case (ev.keyCode === 37 && meta): // left\r
kiwi.app.panels.view.prev();\r
return false;\r
- break;\r
\r
case (ev.keyCode === 39 && meta): // right\r
kiwi.app.panels.view.next();\r
return false;\r
- break;\r
+\r
+ case (ev.keyCode === 9): // tab\r
+ this.tabcomplete.active = true;\r
+ if (_.isEqual(this.tabcomplete.data, [])) {\r
+ // Get possible autocompletions\r
+ var ac_data = [];\r
+ $.each(kiwi.app.panels.active.get('members').models, function (i, member) {\r
+ if (!member) return;\r
+ ac_data.push(member.get('nick'));\r
+ });\r
+ ac_data = _.sortBy(ac_data, function (nick) {\r
+ return nick;\r
+ });\r
+ this.tabcomplete.data = ac_data;\r
+ }\r
+\r
+ if (inp_val[inp[0].selectionStart - 1] === ' ') {\r
+ return false;\r
+ }\r
+ \r
+ (function () {\r
+ var tokens = inp_val.substring(0, inp[0].selectionStart).split(' '),\r
+ val,\r
+ p1,\r
+ newnick,\r
+ range,\r
+ nick = tokens[tokens.length - 1];\r
+ if (this.tabcomplete.prefix === '') {\r
+ this.tabcomplete.prefix = nick;\r
+ }\r
+\r
+ this.tabcomplete.data = _.select(this.tabcomplete.data, function (n) {\r
+ return (n.toLowerCase().indexOf(that.tabcomplete.prefix.toLowerCase()) === 0);\r
+ });\r
+\r
+ if (this.tabcomplete.data.length > 0) {\r
+ p1 = inp[0].selectionStart - (nick.length);\r
+ val = inp_val.substr(0, p1);\r
+ newnick = this.tabcomplete.data.shift();\r
+ this.tabcomplete.data.push(newnick);\r
+ val += newnick;\r
+ val += inp_val.substr(inp[0].selectionStart);\r
+ inp.val(val);\r
+\r
+ if (inp[0].setSelectionRange) {\r
+ inp[0].setSelectionRange(p1 + newnick.length, p1 + newnick.length);\r
+ } else if (inp[0].createTextRange) { // not sure if this bit is actually needed....\r
+ range = inp[0].createTextRange();\r
+ range.collapse(true);\r
+ range.moveEnd('character', p1 + newnick.length);\r
+ range.moveStart('character', p1 + newnick.length);\r
+ range.select();\r
+ }\r
+ }\r
+ }).apply(this);\r
+ return false;\r
}\r
},\r
\r