aboutsummaryrefslogtreecommitdiffstats
path: root/copy.js
diff options
context:
space:
mode:
authordrry2008-03-21 02:11:34 +0000
committerdrry2008-03-21 02:11:34 +0000
commitcc76a40ad3a001def2f2a9348bad82453d8fd5d6 (patch)
treed46fb40b49e9f5e3a2a3303fb5943ecfaef2c941 /copy.js
parent0d19a1e18f331d8a73a34839947209d1c58949b9 (diff)
downloadvimperator-plugins-cc76a40ad3a001def2f2a9348bad82453d8fd5d6.tar.bz2
lang/javascript/vimperator-plugins/trunk/xpathBlink.js
lang/javascript/vimperator-plugins/trunk/autoIgnoreKey.js lang/javascript/vimperator-plugins/trunk/copy.js lang/javascript/vimperator-plugins/trunk/lookupDictionary.js lang/javascript/vimperator-plugins/trunk/splitBrowser.js lang/javascript/vimperator-plugins/trunk/gmperator.js: * 消えたファイルを trunk にコピーしました。 * ほか。 git-svn-id: http://svn.coderepos.org/share/lang/javascript/vimperator-plugins/trunk@8235 d0d07461-0603-4401-acd4-de1884942a52
Diffstat (limited to 'copy.js')
-rw-r--r--copy.js168
1 files changed, 168 insertions, 0 deletions
diff --git a/copy.js b/copy.js
new file mode 100644
index 0000000..7a74faa
--- /dev/null
+++ b/copy.js
@@ -0,0 +1,168 @@
+/**
+ * vimperator plugin
+ * Add `copy' command
+ * For vimperator 0.5.3
+ * @author teramako teramako@gmail.com
+ * @version 0.1
+ *
+ * Usage:
+ * :copy {copyString} -> copy the argument replaced some certain string
+ * :copy! {expr} -> evaluate the argument and copy the result
+ *
+ * ex)
+ * :copy %TITLE% -> copied the title of the current page
+ * :copy title -> same as `:copy %TITLE%' by default
+ * :copy! vimperator.version -> copy the value of vimperator.version
+ *
+ * If non-argument, used `default'
+ *
+ * Change the value by `set' command. (only the current session)
+ * :set copy_{label}=....
+ * or
+ * :set {label}=...
+ */
+
+(function(){
+/*
+ * label: template name which is command argument
+ * copy: copy string
+ * the certian string is replace to ...
+ * %TITTLE% -> to the title of the current page
+ * %URL% -> to the URL of the current page
+ * %SEL% -> to the string of selection
+ * %HTMLSEL% -> to the html string of selection
+ */
+const templates = [
+ { label: 'titleAndURL', value: '%TITLE%\n%URL%' },
+ { label: 'title', value: '%TITLE%' },
+ { label: 'anchor', value: '<a href="%URL%">%TITLE%</a>' },
+ { label: 'selanchor', value: '<a href="%URL%" title="%TITLE%">%SEL%</a>' },
+ { label: 'htmlblockquote', value: '<blockquote cite="%URL%" title="%TITLE%">%HTMLSEL%</blockquote>' }
+];
+// used when argument is none
+const defaultValue = templates[0].label;
+vimperator.commands.add(new vimperator.Command( ['copy'],
+ function(arg, special){
+ var copyString = '';
+ var isError = false;
+ if (special && arg){
+ try {
+ copyString = window.eval('with(vimperator){' + arg + '}');
+ switch (typeof copyString){
+ case 'object':
+ copyString = copyString === null ? 'null' : copyString.toSource();
+ break;
+ case 'function':
+ copyString = copyString.toString();
+ break;
+ case 'number':
+ case 'boolean':
+ copyString = '' + copyString;
+ break;
+ case 'undefined':
+ copyString = 'undefined';
+ break;
+ }
+ } catch(e){
+ isError = true;
+ copyString = e.toString();
+ }
+ } else {
+ if (!arg){ arg = defaultValue; }
+ var str = getCopyTemplate(arg) || arg;
+ copyString = replaceVariable(str);
+ }
+ vimperator.copyToClipboard(copyString);
+ if (isError){
+ vimperator.echoerr('CopiedErrorString: `' + copyString + "'");
+ } else {
+ vimperator.echo('CopiedString: `' + vimperator.util.escapeHTML(copyString) + "'");
+ }
+ },{
+ usage: ['copy {copyString}','copy! {expr}'],
+ shortHelp: 'Copy to clipboard',
+ completer: function(filter){
+ if ( vimperator.commands.parseCommand(vimperator.commandline.getCommand())[2] ){
+ return vimperator.completion.javascript(filter);
+ }
+ var templates = [];
+ for (var option in vimperator.options){
+ if ( option.name.indexOf('copy_') == 0 ){
+ templates.push([option.names[1], option.value]);
+ }
+ }
+ if (!filter){ return templates; }
+ var candidates = [];
+ templates.forEach(function(template){
+ if (template[0].indexOf(filter) == 0 || ('copy_'+template[0]).indexOf(filter) == 0){
+ candidates.push(template);
+ }
+ });
+ return candidates;
+ }
+ }
+));
+function getCopyTemplate(label){
+ for (var option in vimperator.options){
+ if ( option.hasName('copy_'+label) || option.hasName(label) ){
+ return option.value;
+ }
+ }
+ return null;
+}
+function replaceVariable(str){
+ if (!str) return;
+ var win = new XPCNativeWrapper(window.content.window);
+ var sel = '',htmlsel = '';
+ if (str.indexOf('%SEL%') >= 0 || str.indexOf('%HTMLSEL%') >= 0){
+ sel = win.getSelection().getRangeAt(0);
+ }
+ if (str.indexOf('%HTMLSEL%') >= 0){
+ var serializer = new XMLSerializer();
+ htmlsel = serializer.serializeToString(sel.cloneContents());
+ }
+ return str.replace(/%TITLE%/g,vimperator.buffer.title)
+ .replace(/%URL%/g,vimperator.buffer.URL)
+ .replace(/%SEL%/g,sel.toString())
+ .replace(/%HTMLSEL%/g,htmlsel);
+}
+
+templates.forEach(function(template){
+ vimperator.options.add(new vimperator.Option( ['copy_'+template.label, template.label], 'string',{
+ defaultValue: template.value,
+ shortHelp: 'Copy template: `' + vimperator.util.escapeHTML(template.value) + "'"
+ }));
+});
+vimperator.completion.exTabCompletion = function (str) {
+ var [count, cmd, special, args] = vimperator.commands.parseCommand(str);
+ var completions = [];
+ var start = 0;
+ var matches = str.match(/^:*\d*(?=\w*$)/);
+ if (matches) {
+ completions = this.command(cmd);
+ start = matches[0].length;
+ } else {
+ var command = vimperator.commands.get(cmd);
+ if (command && command.completer) {
+ matches = str.match(/^:*\d*\w+!?\s+/);
+ start = matches ? matches[0].length : 0;
+ if (command.hasName("open") || command.hasName("tabopen") || command.hasName("winopen")) {
+ var skip = args.match(/^(.*,\s+)(.*)/);
+ if (skip) {
+ start += skip[1].length;
+ args = skip[2];
+ }
+ } else if (command.hasName("echo") || command.hasName("echoerr") ||
+ command.hasName("javascript") || command.hasName("copy")) {
+ var skip = args.match(/^.*?(?=\w*$)/);
+ if (skip)
+ start += skip[0].length;
+ }
+ completions = command.completer.call(this, args);
+ }
+ }
+ return [start, completions];
+};
+})();
+
+// vim: set fdm=marker sw=4 ts=4 et: