Skip to content

Instantly share code, notes, and snippets.

@satyr
Created February 19, 2009 22:40
Show Gist options
  • Save satyr/67165 to your computer and use it in GitHub Desktop.
Save satyr/67165 to your computer and use it in GitHub Desktop.
Saves/accesses your command history.
const Name = 'command-history',
PHistory = 'extensions.ubiquity.history.',
PCache = PHistory +'cache',
PCount = PHistory +'count',
Sep = '\t', InitCount = 40, {prefs} = Application;
CmdUtils.CreateCommand({
name: Name,
icon: 'chrome://ubiquity/skin/icons/favicon.ico',
takes: {regex: {_name: '/re/i', suggest: function(txt, htm, cb, sx){
if(sx) return [];
try { var re = RegExp(txt, 'i') } catch(_){ re = /^/ }
return [{text: txt, data: re, summary: re}];
}}},
description: 'Accesses your command history.',
help: ''+<ul style="list-style-image:none">
<li>Use accesskey or click to reclaim a command.</li>
<li>Type <abbr title="Regular Expression">regex</abbr>
to filter commands.</li>
<li>Execute to delete all matched commands from history.</li>
<li>Create or edit <a href="about:config"><code>{PCount}</code></a> to
set maximum number of commands stored.
(defaults to {InitCount})</li></ul>,
execute: function({data}){
if(!data) return this._show('Type ".*" to delete all.');
prefs.setValue(PCache, this._find(data, true).join(Sep));
this._show('Matched commands deleted.');
},
preview: function(pbl, {data}){
var ol = this._find(data).reduce(function(ol, c, i){
var k = i < 30 ? String.fromCharCode(i + 65) : i < 40 ? i - 30 : '-';
ol.* += (<li><label for={i}><button id={i} accesskey={k} value={c}
>{k}</button><code>{c}</code></label></li>);
return ol;
}, <ol class={Name}></ol>);
if(!('li' in ol)){ pbl.innerHTML = this.description + this.help; return }
pbl.innerHTML = this._style + ol;
jQuery('button', pbl).each(function(){
this.addEventListener('focus', function(e){
var {gUbiquity} = context.chromeWindow;
gUbiquity.__textBox.value = this.value;
gUbiquity.__processInput();
e.preventDefault(), e.stopPropagation();
}, true) });
},
previewDelay: 222,
_show: function(text){
displayMessage({icon: this.icon, title: this.name, text: text});
},
_find: function(re, ex){
var ls = prefs.getValue(PCache, '').split(Sep);
return re ? ls.filter(function(cmd) ex ^ (cmd && re.test(cmd))) : ls;
},
_style: <style>{<![CDATA[
@{margin:0; padding:2px}
@li {list-style-type:none}
@button {padding:0; border-width:1px;
font:bold 108% "Consolas", monospace}
]]>.toString().replace(/@/g, '.'+ Name +' ')}</style>,
author: 'satyr'.link('http://d.hatena.ne.jp/murky-satyr'), license: 'MIT',
});
function startup_commandHistory(){{}
function setup(U){
U.__msgPanel.addEventListener('popuphidden', function saveCommand(){
var cmd = jQuery.trim(U.__textBox.value);
if(!cmd) return;
var hst = prefs.getValue(PCache, '').split(Sep),
idx = hst.indexOf(cmd), max = prefs.getValue(PCount, InitCount);
if(~idx) hst.unshift(hst.splice(idx, 1));
else if(hst.unshift(cmd) > max) hst.length = max;
prefs.setValue(PCache, hst.join(Sep));
}, false);
}
function wait4U(){
var {gUbiquity} = Utils.currentChromeWindow;
gUbiquity ? setup(gUbiquity) : Utils.setTimeout(wait4U, 42);
}
wait4U();
Cc["@mozilla.org/observer-service;1"].getService(Ci.nsIObserverService)
.addObserver({
observe: function(subject, topic, data){
subject.addEventListener('load', this, false);
},
handleEvent: function({originalTarget: doc}){
doc.removeEventListener('load', this, false);
doc.URL === 'chrome://browser/content/browser.xul' && wait4U();
}}, 'toplevel-window-ready', false);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment