Mon 21 Jul 22:43:21 CEST 2025

This commit is contained in:
sbosse 2025-07-21 23:13:12 +02:00
parent eda5ffe919
commit 8ca99e7e82

104
js/web/killring.js Normal file
View File

@ -0,0 +1,104 @@
/* ------------------------------------------------------------------------*
* Copyright 2013-2014 Arne F. Claassen
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*-------------------------------------------------------------------------*/
var Josh = Josh || {};
(function(root) {
Josh.KillRing = function(config) {
config = config || {};
var _console = Josh.Debug && root.console ? root.console : {log: function() {
}};
var _ring = config.ring || [];
var _cursor = config.cursor || 0;
var _uncommitted = false;
var _yanking = false;
if(_ring.length == 0) {
_cursor = -1;
} else if(_cursor >= _ring.length) {
_cursor = _ring.length - 1;
}
var self = {
isinkill: function() {
return _uncommitted;
},
lastyanklength: function() {
if(!_yanking) {
return 0;
}
return _ring[_cursor].length;
},
append: function(value) {
_yanking = false;
if(!value) {
return;
}
if(_ring.length == 0 || !_uncommitted) {
_ring.push('');
}
_cursor = _ring.length - 1;
_console.log("appending: " + value);
_uncommitted = true;
_ring[_cursor] += value;
},
prepend: function(value) {
_yanking = false;
if(!value) {
return;
}
if(_ring.length == 0 || !_uncommitted) {
_ring.push('');
}
_cursor = _ring.length - 1;
_console.log("prepending: " + value);
_uncommitted = true;
_ring[_cursor] = value + _ring[_cursor];
},
commit: function() {
_console.log("committing");
_yanking = false;
_uncommitted = false;
},
yank: function() {
self.commit();
if(_ring.length == 0) {
return null;
}
_yanking = true;
return _ring[_cursor];
},
rotate: function() {
if(!_yanking || _ring.length == 0) {
return null;
}
--_cursor;
if(_cursor < 0) {
_cursor = _ring.length - 1;
}
return self.yank();
},
items: function() {
return _ring.slice(0);
},
clear: function() {
_ring = [];
_cursor = -1;
_yanking = false;
_uncommited = false;
}
};
return self;
}
})(this);