2013-10-27 00:44:48 +02:00
|
|
|
/**
|
|
|
|
* Portable utilities for IRC.
|
|
|
|
*/
|
|
|
|
|
|
|
|
var IrcUtils = {
|
2013-10-27 08:49:59 +01:00
|
|
|
/**
|
|
|
|
* Get a new version of a nick list, sorted alphabetically by lowercase nick.
|
|
|
|
*
|
|
|
|
* @param nickList Original nick list
|
|
|
|
* @return Nick list sorted alphabetically by lowercase nick
|
|
|
|
*/
|
|
|
|
_ciSearchNickList: function(nickList) {
|
|
|
|
var newList = [];
|
|
|
|
|
|
|
|
nickList.forEach(function(nick) {
|
|
|
|
newList.push(nick);
|
|
|
|
});
|
|
|
|
newList.sort(function(a, b) {
|
|
|
|
return a.toLowerCase() < b.toLowerCase() ? -1 : 1;
|
|
|
|
});
|
|
|
|
|
|
|
|
return newList;
|
|
|
|
},
|
|
|
|
|
2013-10-27 00:44:48 +02:00
|
|
|
/**
|
|
|
|
* Completes a single nick.
|
|
|
|
*
|
|
|
|
* @param candidate What to search for
|
2013-10-27 08:49:59 +01:00
|
|
|
* @param nickList Array of current nicks sorted for case insensitive searching
|
2013-10-27 00:44:48 +02:00
|
|
|
* @return Completed nick (null if not found)
|
|
|
|
*/
|
|
|
|
_completeSingleNick: function(candidate, nickList) {
|
|
|
|
var foundNick = null;
|
|
|
|
|
|
|
|
nickList.some(function(nick) {
|
2013-10-27 08:49:59 +01:00
|
|
|
if (nick.toLowerCase().search(candidate.toLowerCase()) == 0) {
|
2013-10-27 00:44:48 +02:00
|
|
|
// found!
|
|
|
|
foundNick = nick;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
return foundNick;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the next nick when iterating nicks.
|
|
|
|
*
|
|
|
|
* @param iterCandidate First characters to look at
|
|
|
|
* @param currentNick Current selected nick
|
2013-10-27 08:49:59 +01:00
|
|
|
* @param nickList Array of current nicks sorted for case insensitive searching
|
2013-10-27 00:44:48 +02:00
|
|
|
* @return Next nick (may be the same)
|
|
|
|
*/
|
|
|
|
_nextNick: function(iterCandidate, currentNick, nickList) {
|
|
|
|
var firstInGroup = null;
|
|
|
|
var matchingNicks = [];
|
|
|
|
var at = null;
|
2013-10-27 08:49:59 +01:00
|
|
|
var lcIterCandidate = iterCandidate.toLowerCase();
|
|
|
|
var lcCurrentNick = currentNick.toLowerCase();
|
2013-10-27 00:44:48 +02:00
|
|
|
|
|
|
|
// collect matching nicks
|
|
|
|
for (var i = 0; i < nickList.length; ++i) {
|
2013-10-27 08:49:59 +01:00
|
|
|
var lcNick = nickList[i].toLowerCase();
|
|
|
|
if (lcNick.search(lcIterCandidate) == 0) {
|
2013-10-27 00:44:48 +02:00
|
|
|
matchingNicks.push(nickList[i]);
|
2013-10-27 08:49:59 +01:00
|
|
|
if (lcCurrentNick == lcNick) {
|
2013-10-27 00:44:48 +02:00
|
|
|
at = matchingNicks.length - 1;
|
|
|
|
}
|
|
|
|
} else if (matchingNicks.length > 0) {
|
|
|
|
// end of group, no need to check after this
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (at == null || matchingNicks.length == 0) {
|
|
|
|
return currentNick;
|
|
|
|
} else {
|
|
|
|
++at;
|
|
|
|
if (at == matchingNicks.length) {
|
|
|
|
// cycle
|
|
|
|
at = 0;
|
|
|
|
}
|
|
|
|
return matchingNicks[at];
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Nicks tab completion.
|
|
|
|
*
|
|
|
|
* @param text Plain text (no colors)
|
|
|
|
* @param caretPos Current caret position (0 means before the first character)
|
|
|
|
* @param iterCandidate Current iteration candidate (null if not iterating)
|
2013-10-27 08:49:59 +01:00
|
|
|
* @param nickList Array of current nicks
|
2013-10-27 08:01:19 +01:00
|
|
|
* @param suf Custom suffix (at least one character, escaped for regex)
|
2013-10-27 00:44:48 +02:00
|
|
|
* @return Object with following properties:
|
|
|
|
* text: new complete replacement text
|
|
|
|
* caretPos: new caret position within new text
|
|
|
|
* foundNick: completed nick (or null if not possible)
|
|
|
|
* iterCandidate: current iterating candidate
|
|
|
|
*/
|
2013-10-27 08:01:19 +01:00
|
|
|
completeNick: function(text, caretPos, iterCandidate, nickList, suf) {
|
2013-10-27 07:48:30 +01:00
|
|
|
var doIterate = (iterCandidate !== null);
|
2013-10-27 08:01:19 +01:00
|
|
|
if (suf === null) {
|
|
|
|
suf = ':';
|
|
|
|
}
|
2013-10-27 07:48:30 +01:00
|
|
|
|
2013-10-27 08:49:59 +01:00
|
|
|
// new nick list to search in
|
|
|
|
var searchNickList = IrcUtils._ciSearchNickList(nickList);
|
|
|
|
|
2013-10-27 00:44:48 +02:00
|
|
|
// text before and after caret
|
|
|
|
var beforeCaret = text.substring(0, caretPos);
|
|
|
|
var afterCaret = text.substring(caretPos);
|
|
|
|
|
|
|
|
// default: don't change anything
|
|
|
|
var ret = {
|
|
|
|
text: text,
|
|
|
|
caretPos: caretPos,
|
|
|
|
foundNick: null,
|
|
|
|
iterCandidate: null
|
|
|
|
};
|
|
|
|
|
|
|
|
// iterating nicks at the beginning?
|
2013-10-27 08:01:19 +01:00
|
|
|
var m = beforeCaret.match(new RegExp('^([a-zA-Z0-9_\\\\\\[\\]{}^`|-]+)' + suf + ' $'));
|
2013-10-27 00:44:48 +02:00
|
|
|
if (m) {
|
|
|
|
if (doIterate) {
|
|
|
|
// try iterating
|
2013-10-27 08:49:59 +01:00
|
|
|
var newNick = IrcUtils._nextNick(iterCandidate, m[1], searchNickList);
|
2013-10-27 08:01:19 +01:00
|
|
|
beforeCaret = newNick + suf + ' ';
|
2013-10-27 00:44:48 +02:00
|
|
|
return {
|
|
|
|
text: beforeCaret + afterCaret,
|
|
|
|
caretPos: beforeCaret.length,
|
|
|
|
foundNick: newNick,
|
|
|
|
iterCandidate: iterCandidate
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
// if not iterating, don't do anything
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// nick completion in the beginning?
|
|
|
|
m = beforeCaret.match(/^([a-zA-Z0-9_\\\[\]{}^`|-]+)$/);
|
|
|
|
if (m) {
|
|
|
|
// try completing
|
2013-10-27 08:49:59 +01:00
|
|
|
var newNick = IrcUtils._completeSingleNick(m[1], searchNickList);
|
2013-10-27 00:44:48 +02:00
|
|
|
if (newNick === null) {
|
|
|
|
// no match
|
|
|
|
return ret;
|
|
|
|
}
|
2013-10-27 08:01:19 +01:00
|
|
|
beforeCaret = newNick + suf + ' ';
|
2013-10-27 00:44:48 +02:00
|
|
|
if (afterCaret[0] == ' ') {
|
|
|
|
// swallow first space after caret if any
|
|
|
|
afterCaret = afterCaret.substring(1);
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
text: beforeCaret + afterCaret,
|
|
|
|
caretPos: beforeCaret.length,
|
|
|
|
foundNick: newNick,
|
|
|
|
iterCandidate: m[1]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// iterating nicks in the middle?
|
|
|
|
m = beforeCaret.match(/^(.* )([a-zA-Z0-9_\\\[\]{}^`|-]+) $/);
|
|
|
|
if (m) {
|
|
|
|
if (doIterate) {
|
|
|
|
// try iterating
|
2013-10-27 08:49:59 +01:00
|
|
|
var newNick = IrcUtils._nextNick(iterCandidate, m[2], searchNickList);
|
2013-10-27 00:44:48 +02:00
|
|
|
beforeCaret = m[1] + newNick + ' ';
|
|
|
|
return {
|
|
|
|
text: beforeCaret + afterCaret,
|
|
|
|
caretPos: beforeCaret.length,
|
|
|
|
foundNick: newNick,
|
|
|
|
iterCandidate: iterCandidate
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
// if not iterating, don't do anything
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// nick completion elsewhere in the middle?
|
|
|
|
m = beforeCaret.match(/^(.* )([a-zA-Z0-9_\\\[\]{}^`|-]+)$/);
|
|
|
|
if (m) {
|
|
|
|
// try completing
|
2013-10-27 08:49:59 +01:00
|
|
|
var newNick = IrcUtils._completeSingleNick(m[2], searchNickList);
|
2013-10-27 00:44:48 +02:00
|
|
|
if (newNick === null) {
|
|
|
|
// no match
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
beforeCaret = m[1] + newNick + ' ';
|
2013-10-27 01:07:04 +02:00
|
|
|
if (afterCaret[0] == ' ') {
|
|
|
|
// swallow first space after caret if any
|
|
|
|
afterCaret = afterCaret.substring(1);
|
|
|
|
}
|
2013-10-27 00:44:48 +02:00
|
|
|
return {
|
|
|
|
text: beforeCaret + afterCaret,
|
|
|
|
caretPos: beforeCaret.length,
|
|
|
|
foundNick: newNick,
|
|
|
|
iterCandidate: m[2]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// completion not possible
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
};
|