Last active
May 11, 2016 23:58
-
-
Save satyr/402862 to your computer and use it in GitHub Desktop.
[forked] replace.ubiquity.js
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
var noun_typed_text = { | |
name: 'typed text', | |
suggest: function suggestTyped(txt, htm, cb, si){ | |
return !si && {text: txt, summary: htm, score: .4} | |
}, | |
} | |
var replace = CmdUtils.CreateCommand({ | |
name: 'replace', | |
icon: 'chrome://ubiquity/skin/icons/page_refresh.png', | |
description: 'Replaces selection.', | |
arguments: { | |
object_regexp: noun_typed_text, | |
goal: noun_typed_text, | |
format: { | |
default: {summary: 'mg'}, | |
suggest: function suggestFlag(txt, htm, cb, si){ | |
if(!si && (txt = txt.toLowerCase().match(/[igmy]/g))) | |
return {summary: Utils.uniq(txt).join('')} | |
}, | |
}, | |
}, | |
authors: [{name: 'powchin', homepage: 'http://friendfeed.com/powchin'}, | |
'satyr'], | |
license: 'MIT', | |
execute: function executeReplace(args){ | |
var so = this._do(args, args.goal.text) | |
if(so) CmdUtils.setSelection(so.html, so) | |
}, | |
preview: function previewReplace(pb, args){ | |
var so = this._do(args, '\0$&\0'+ args.goal.text +'\0') | |
if(!so) return void this.previewDefault(pb) | |
for(let p in so) if(so[p]) | |
so[p] = Utils.escapeHtml(so[p]).replace( | |
/\0([^\0]*)\0([^\0]*)\0/g, '<del>$1</del><ins>$2</ins>') | |
pb.innerHTML = '<pre id="replace">'+ (so.html || so.text) | |
}, | |
_do: function _doReplace({object, format}, to){ | |
var so = {html: CmdUtils.htmlSelection} | |
if(!so.html && !(so.text = CmdUtils.selection)) return null | |
if(object.text) gsub(so, regex(object.text, format.summary), to) | |
return so | |
}, | |
}) | |
CmdUtils.CreateCommand({ | |
name: 'indent', | |
icon: 'chrome://ubiquity/skin/icons/page_refresh.png', | |
description: 'Indents selection with X characters (default: whitespace).', | |
help: 'Specify a negative number to unindent.', | |
arguments: { | |
object: CmdUtils.NounType(/^-?\d+$/, '4'), | |
instrument: noun_typed_text, | |
}, | |
execute: function executeIndent({object: {text: n}, instrument: {text}}){ | |
if(!(n |= 0)) return | |
text = text || ' ' | |
var so = CmdUtils.selectionObject | |
, [re, to] = | |
n > 0 | |
? [/^(?=.)/mg, Array(n+1).join(text)] | |
: [RegExp('^(?:'+ Utils.regexp.quote(text) +'){0,'+ -n +'}', 'mg'), ''] | |
gsub(so, re, to) | |
CmdUtils.setSelection(so.html, so) | |
}, | |
}) | |
CmdUtils.CreateCommand({ | |
name: 'quote', | |
icon: 'chrome://ubiquity/skin/icons/page_code.png', | |
description: "Quotes selection by adding `>' to each line.", | |
execute: function executeQuote(){ | |
replace.execute({ | |
object : {text: '^'}, | |
goal : {text: '>\\x20'}, | |
format : {summary: 'mg'}, | |
}) | |
}, | |
}) | |
CmdUtils.CreateCommand({ | |
name: 'unquote', | |
icon: 'chrome://ubiquity/skin/icons/page_code.png', | |
description: "Unquotes selection by removing `>' from each line.", | |
execute: function executeUnquote(){ | |
replace.execute({ | |
object : {text: '^>\x20'}, | |
goal : {text: ''}, | |
format : {summary: 'mg'}, | |
}) | |
}, | |
}) | |
function gsub(so, re, to){ | |
to = to.replace( | |
/\\(?:u[0-9a-fA-F]{4}|x[0-9a-fA-F]{2}|[0-3]?[0-7]{1,2}|.)/g, | |
s => JSON.parse('"'+ s +'"')) | |
for(let p in so) if(so[p]) so[p] = so[p].replace(re, to) | |
} | |
function regex(pattern, flags){ | |
try { return RegExp( pattern , flags) } | |
catch({}){ return RegExp(Utils.regexp.quote(pattern), flags) } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment