Merge pull request #267 from torhve/recentspeaker

Use recent speaker instead of alphabetical nick tab complete
This commit is contained in:
David Cormier 2014-04-25 18:35:34 -04:00
commit 82cab2c6af
3 changed files with 68 additions and 42 deletions

View file

@ -666,6 +666,18 @@ weechat.controller('WeechatCtrl', ['$rootScope', '$scope', '$store', '$timeout',
$scope.bufferlines = ab.lines; $scope.bufferlines = ab.lines;
$scope.nicklist = ab.nicklist; $scope.nicklist = ab.nicklist;
// Send a request for the nicklist if it hasn't been loaded yet
if (!ab.nicklistRequested()) {
connection.requestNicklist(ab.fullName, function() {
$scope.showNicklist = $scope.updateShowNicklist();
// Scroll after nicklist has been loaded, as it may break long lines
$rootScope.scrollWithBuffer(true);
});
} else {
// Check if we should show nicklist or not
$scope.showNicklist = $scope.updateShowNicklist();
}
if (ab.requestedLines < $scope.lines) { if (ab.requestedLines < $scope.lines) {
// buffer has not been loaded, but some lines may already be present if they arrived after we connected // buffer has not been loaded, but some lines may already be present if they arrived after we connected
// try to determine how many lines to fetch // try to determine how many lines to fetch
@ -679,18 +691,6 @@ weechat.controller('WeechatCtrl', ['$rootScope', '$scope', '$store', '$timeout',
} }
$rootScope.updateTitle(ab); $rootScope.updateTitle(ab);
// Send a request for the nicklist if it hasn't been loaded yet
if (!ab.nicklistRequested()) {
connection.requestNicklist(ab.fullName, function() {
$scope.showNicklist = $scope.updateShowNicklist();
// Scroll after nicklist has been loaded, as it may break long lines
$rootScope.scrollWithBuffer(true);
});
} else {
// Check if we should show nicklist or not
$scope.showNicklist = $scope.updateShowNicklist();
}
$rootScope.scrollWithBuffer(true); $rootScope.scrollWithBuffer(true);
// If user wants to sync hotlist with weechat // If user wants to sync hotlist with weechat
@ -868,7 +868,7 @@ weechat.controller('WeechatCtrl', ['$rootScope', '$scope', '$store', '$timeout',
// get last word // get last word
var lastSpace = trimmedValue.lastIndexOf(' ') + 1; var lastSpace = trimmedValue.lastIndexOf(' ') + 1;
var lastWord = trimmedValue.slice(lastSpace, trimmedValue.length - 1); var lastWord = trimmedValue.slice(lastSpace, trimmedValue.length - 1);
var nicklist = models.getActiveBuffer().flatNicklist(); var nicklist = models.getActiveBuffer().getNicklistByTime();
// check against nicklist to see if it's a list of highlights // check against nicklist to see if it's a list of highlights
if (nicklist.indexOf(lastWord) !== -1) { if (nicklist.indexOf(lastWord) !== -1) {
// It's another highlight! // It's another highlight!
@ -1230,12 +1230,12 @@ weechat.directive('inputBar', function() {
// get current caret position // get current caret position
var caretPos = inputNode.selectionStart; var caretPos = inputNode.selectionStart;
// create flat array of nicks // get current active buffer
var activeBuffer = models.getActiveBuffer(); var activeBuffer = models.getActiveBuffer();
// complete nick // complete nick
var nickComp = IrcUtils.completeNick(inputText, caretPos, var nickComp = IrcUtils.completeNick(inputText, caretPos,
$scope.iterCandidate, activeBuffer.flatNicklist(), ':'); $scope.iterCandidate, activeBuffer.getNicklistByTime(), ':');
// remember iteration candidate // remember iteration candidate
$scope.iterCandidate = nickComp.iterCandidate; $scope.iterCandidate = nickComp.iterCandidate;

View file

@ -4,20 +4,17 @@
var IrcUtils = { var IrcUtils = {
/** /**
* Get a new version of a nick list, sorted alphabetically by lowercase nick. * Get a new version of a nick list, sorted by last speaker
* *
* @param nickList Original nick list * @param nickList Original nick list
* @return Nick list sorted alphabetically by lowercase nick * @return Sorted nick list
*/ */
_ciSearchNickList: function(nickList) { _ciNickList: function(nickList) {
var newList = [];
nickList.forEach(function(nick) { var newList = _(nickList).sortBy(function(nickObj) {
newList.push(nick); return -nickObj.spokeAt;
});
newList.sort(function(a, b) {
return a.toLowerCase() < b.toLowerCase() ? -1 : 1;
}); });
newList = _(newList).pluck('name');
return newList; return newList;
}, },
@ -66,10 +63,13 @@ var IrcUtils = {
if (lcCurrentNick === lcNick) { if (lcCurrentNick === lcNick) {
at = matchingNicks.length - 1; at = matchingNicks.length - 1;
} }
} else if (matchingNicks.length > 0) { }
/* Since we aren't sorted any more torhve disabled this:
else if (matchingNicks.length > 0) {
// end of group, no need to check after this // end of group, no need to check after this
break; //break;
} }
*/
} }
if (at === null || matchingNicks.length === 0) { if (at === null || matchingNicks.length === 0) {
@ -105,7 +105,7 @@ var IrcUtils = {
} }
// new nick list to search in // new nick list to search in
var searchNickList = IrcUtils._ciSearchNickList(nickList); var searchNickList = IrcUtils._ciNickList(nickList);
// text before and after caret // text before and after caret
var beforeCaret = text.substring(0, caretPos); var beforeCaret = text.substring(0, caretPos);

View file

@ -20,7 +20,6 @@ models.service('models', ['$rootScope', '$filter', function($rootScope, $filter)
var lines = []; var lines = [];
var requestedLines = 0; var requestedLines = 0;
var nicklist = {}; var nicklist = {};
var flatnicklist = [];
var history = []; var history = [];
var historyPos = 0; var historyPos = 0;
var active = false; var active = false;
@ -43,6 +42,7 @@ models.service('models', ['$rootScope', '$filter', function($rootScope, $filter)
*/ */
var addLine = function(line) { var addLine = function(line) {
lines.push(line); lines.push(line);
updateNickSpeak(line);
}; };
/* /*
@ -50,8 +50,8 @@ models.service('models', ['$rootScope', '$filter', function($rootScope, $filter)
*/ */
var addNick = function(group, nick) { var addNick = function(group, nick) {
if (nicklistRequested()) { if (nicklistRequested()) {
nick.spokeAt = Date.now();
nicklist[group].nicks.push(nick); nicklist[group].nicks.push(nick);
flatnicklist = getFlatNicklist();
} }
}; };
/* /*
@ -63,7 +63,6 @@ models.service('models', ['$rootScope', '$filter', function($rootScope, $filter)
return; return;
} }
group.nicks = _.filter(group.nicks, function(n) { return n.name !== nick.name;}); group.nicks = _.filter(group.nicks, function(n) { return n.name !== nick.name;});
flatnicklist = getFlatNicklist();
/* /*
for (i in group.nicks) { for (i in group.nicks) {
if (group.nicks[i].name == nick.name) { if (group.nicks[i].name == nick.name) {
@ -84,28 +83,55 @@ models.service('models', ['$rootScope', '$filter', function($rootScope, $filter)
break; break;
} }
} }
flatnicklist = getFlatNicklist();
}; };
/* /*
* Maintain a cached version of a flat sorted nicklist * Update a nick with a fresh timestamp so tab completion
* can use time to complete recent speakers
*/
var updateNickSpeak = function(line) {
// Try to find nick from prefix
var prefix = line.prefix;
var nick = prefix[prefix.length - 1].text;
// Action / me, find the nick as the first word of the message
if (nick === " *") {
var match = line.text.match(/^(.+)\s/);
if (match) {
nick = match[1];
}
}
else if (nick === "" || nick === "=!=") {
return;
}
_.each(nicklist, function(nickGroup) {
_.each(nickGroup.nicks, function(nickObj) {
if (nickObj.name === nick) {
// Use the order the line arrive in for simplicity
// instead of using weechat's own timestamp
nickObj.spokeAt = Date.now();
}
});
});
};
/*
* Get a flat nicklist sorted by speaker time. This function is
* called for every tab key press by the user.
* *
*/ */
var getFlatNicklist = function() { var getNicklistByTime = function() {
var newlist = []; var newlist = [];
_.each(nicklist, function(nickGroup) { _.each(nicklist, function(nickGroup) {
_.each(nickGroup.nicks, function(nickObj) { _.each(nickGroup.nicks, function(nickObj) {
newlist.push(nickObj.name); newlist.push(nickObj);
}); });
}); });
newlist.sort(function(a, b) {
return a.toLowerCase() < b.toLowerCase() ? -1 : 1;
});
return newlist;
};
var flatNicklist = function() { newlist.sort(function(a, b) {
return flatnicklist; return a.spokeAt < b.spokeAt;
});
return newlist;
}; };
var addToHistory = function(line) { var addToHistory = function(line) {
@ -189,7 +215,7 @@ models.service('models', ['$rootScope', '$filter', function($rootScope, $filter)
addNick: addNick, addNick: addNick,
delNick: delNick, delNick: delNick,
updateNick: updateNick, updateNick: updateNick,
flatNicklist: flatNicklist, getNicklistByTime: getNicklistByTime,
serverSortKey: serverSortKey, serverSortKey: serverSortKey,
indent: indent, indent: indent,
history: history, history: history,