Created
January 5, 2009 09:49
-
-
Save satyr/43341 to your computer and use it in GitHub Desktop.
Extra keybinds for Ubiquity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function ubiquityLoad_keys(U, W){//function cmd_ | |
const Box = U.textBox, Panel = U.msgPanel | |
bind({ | |
'C_\\': closePanel, | |
C_j: chain(justExecute, keydown('C_Down')), | |
C_t: insert('Title'), | |
'C_u C_l': insert('URL'), | |
C_o: openPreview, | |
C_O: inspectPreview, | |
'C_[': selectFirstWord, | |
'C_]': selectLastWord, | |
'C_{': selectButLastWord, | |
'C_}': selectButFirstWord, | |
C_w: word(cut), | |
C_q: word(quote), | |
'C_+ C_;': word(increment(+1)), | |
'C_- C_=': word(increment(-1)), | |
C_k: line(cut), | |
C_s: bringSelection, | |
C_0: collapseSelection, | |
'C_/': complete, | |
C_p: indication('Up'), | |
C_n: indication('Down'), | |
C_b: key('Left'), | |
C_f: key('Right'), | |
C_d: key('Delete'), | |
'C_h C_D': key('Back_Space'), | |
F5: reload, | |
}) | |
function complete(){ | |
var {completionText} = U.cmdManager.hilitedSuggestion || 0 | |
if (completionText) Box.value = completionText | |
} | |
function reload(){ | |
U.__lastValue = null | |
U.__processInput(true) | |
} | |
function selectFirstWord(){ with(Box){ | |
selectionStart = 0 | |
selectionEnd = firstWordEnd(value) | |
}} | |
function selectLastWord(){ with(Box){ | |
selectionStart = lastWordStart(value) | |
selectionEnd = value.length | |
}} | |
function selectButFirstWord(){ with(Box){ | |
selectionStart = firstWordEnd(value) + 1 | |
selectionEnd = value.length | |
}} | |
function selectButLastWord(){ with(Box){ | |
selectionStart = 0 | |
selectionEnd = lastWordStart(value) - 1 | |
}} | |
function justExecute(){ U.__cmdManager.execute(U.__makeContext()) } | |
function indication(dir){ | |
var m = 'moveIndication'+ dir | |
return function _indication(){ U.__cmdManager[m](U.__makeContext()) } | |
} | |
function closePanel() U.closeWindow() | |
function inspectPreview(){ | |
W.inspectDOMNode(previewBrowser().contentDocument.body) | |
} | |
function openPreview(){ | |
var html = previewBrowser().contentDocument.documentElement | |
if(html) Utils.openUrlInBrowser( | |
'data:text/html;charset=utf-8,'+ encodeURI(html.innerHTML)) | |
else say('Failed to grab the preview.') | |
} | |
function bringSelection(){ | |
var w = U.__focusedWindow || CmdUtils.window, l = w.document.activeElement | |
try { var {selectionStart: b, selectionEnd: e} = l } catch(_){} | |
if(b !== e){ | |
insertText(' '+ l.value.slice(b, e)) | |
l.setSelectionRange(e, e) | |
return | |
} | |
var s = w.getSelection(), r = s.rangeCount && s.getRangeAt(0) | |
if(!r) return | |
insertText(' '+ r) | |
s.removeRange(r) | |
} | |
function collapseSelection(){ | |
with(CmdUtils.getWindow().getSelection()) | |
isCollapsed || (removeAllRanges(), U.preview()) | |
} | |
function insert(which) function _insert(){ | |
var t, u, {top, document: {title, URL}} = CmdUtils.getWindow() | |
;/^https?:\/\/www\.google\.[a-z.]+\/reader\/view/.test(URL) | |
? ({title: t, url: u}) = top.wrappedJSObject.getPermalink() : | |
/^https?:\/\/(?:reader\.livedoo|fastladde)r\.com\/reader/.test(URL) | |
? ({title: t, link: u}) = top.wrappedJSObject.get_active_item(1) : 0 | |
insertText(which === 'Title' ? t || title : u || URL) | |
} | |
function scrollPreview(X, Y) function _scrollPreview(){ | |
var win = previewBrowser().contentWindow | |
if(win) with(win) scrollBy(innerWidth * X, innerHeight * Y) | |
} | |
function key(pair, type){ | |
var a = pair.toUpperCase().match(/[CASM]_|.+/gy), b = a.pop() | |
, {C_, A_, S_, M_} = a.reduce(function(d, c) d[c] = d, {}), | |
kcode = KeyEvent['DOM_VK_'+ b] || +b || b.charCodeAt() | |
type = type || 'keypress' | |
return function _key(e){ | |
var lm = e.target, ke = lm.ownerDocument.createEvent('KeyboardEvent') | |
ke.initKeyEvent(type, 1, 1, e.view, C_, A_, S_, M_, kcode, 0) | |
lm.dispatchEvent(ke) | |
} | |
} | |
function keydown(pair) key(pair, 'keydown') | |
function keyup (pair) key(pair, 'keyup') | |
function word(cb) function _word(){ | |
var {selectionStart: s, selectionEnd: e, value: v} = Box | |
, vl = v.slice(0, s), vr = v.slice(e), vv = v.slice(s, e) | |
if(s === e){ | |
var [vl, wl] = /^[^]*?(?=(\S*)\s*$)/.exec(vl) | |
var [wr, vr] = /^\S*(?=([^]*))/.exec(vr) | |
vv = wl + vv + wr | |
} | |
Box.selectionStart = Box.selectionEnd = | |
(Box.value = vl + cb(vv) + vr).length - vr.length | |
} | |
function line(fn) function _line(){ Box.value = fn(Box.value) } | |
function firstWordEnd (s) /^\s*\S{0,}/.exec(s)[0].length | |
function lastWordStart(s) s.length - /\S{0,}\s*$/.exec(s)[0].length | |
function say(){ | |
displayMessage({ | |
title: 'keys#'+ arguments.callee.caller.name, | |
text: Array.join(arguments, ' ')}) | |
} | |
function cut(txt)(CmdUtils.copyToClipboard(txt), '') | |
function quote(txt) /^"([^]+)"$/.test(txt) ? RegExp.$1 : '"'+ txt +'"' | |
function increment(N){ | |
const timelike = /\b\d{1,2}:\d{1,2}(?:\d{1,2})?\b/ | |
, normnumb = /\d+(?=\D*$)/g | |
function inctime(t){ | |
var xs = t.split(':'), n = N | |
for(let i = xs.length, x; n && i--; xs[i] = ('0'+ x).slice(-2)){ | |
x = +xs[i] + n | |
n = (x < 0 ? (x += 60, -1) : | |
x > 59 ? (x -= 60, +1) : 0) | |
} | |
return xs.join(':') | |
} | |
function incnorm(d){ | |
var [z, v] = /^0*(?=(.*))/.exec(d), r = +d + N | |
if(r < 0) r += Math.pow(10, d.length), z = '' | |
return z.slice((r +'').length - v.length) + r | |
} | |
return function increplace(s) | |
timelike.test(s) | |
? s.replace(timelike, inctime) | |
: s.replace(normnumb, incnorm) | |
} | |
function chain(){ | |
var fs = Array.slice(arguments) | |
return function _chain(){ for each(var f in fs) f.apply(this, arguments) } | |
} | |
function previewBrowser() Panel.querySelector('browser') | |
function insertText(t){ | |
Box.QueryInterface(Ci.nsIDOMNSEditableElement) | |
.editor.QueryInterface(Ci.nsIPlaintextEditor).insertText(t) | |
} | |
function bind(K){ | |
const C = 1<<9, A = 1<<10, M = 1<<11, D = {C_: C, A_: A, M_: M}, V = 12 | |
function acc(n, k) n | ( | |
k.length == 1 && k.charCodeAt() || | |
D[k = k.toUpperCase()] || KeyEvent['DOM_VK_'+ k] << V) | |
for(var ps in K) for each(var p in ps.split(/\s+/)) | |
K[p.match(/[CAM]_+|.+/igy).reduce(acc, 0)] = K[ps] | |
Box.parentNode.addEventListener('keypress', function(v){ | |
with(v){ | |
var m = C*ctrlKey | A*altKey | M*metaKey | |
if(m == A && which){ | |
U.cmdManager.previewer.activateAccessKey(which) | |
return stopPropagation(preventDefault()) | |
} | |
var f = K[m | (which || keyCode << V)] | |
if(f) f(v) || stopPropagation(preventDefault()) | |
} | |
}, true) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment