Jump to content

User:Quarl/shortcuts.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Quarl (talk | contribs) at 10:22, 26 January 2006 (expand multi). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// [[User:Quarl/shortcuts.js]] - shortcut utilities

// <pre><nowiki>

function shorcut_expand_multi(input) {
    var result = new Array();
    for (k in input) {
        var keys = k.split(',');
        result[keys[0]] = input[k];
        for (var i=1; i < keys.length; ++i) {
            result[keys[i]] = input[k] + ' ';
        }
    }
    return result;
}

function shortcut_msg(shortcuts) {
    var msg = 'Shortcuts available:\n';
    for (var key in shortcuts) {
        if (shortcuts[key].match(/ $/)) continue;
        msg += key + ': ' + shortcuts[key] + '\n';
    }
    return msg;
}

// replace the first word (doesn't require uppercase)
function shortcut_subst_1word(msg,shortcuts) {
    if (!msg) return msg;
    if (msg.match(/^([a-zA-Z]+)(.*)$/)) {
        return shortcut_subst_word(RegExp.$1,shortcuts) + RegExp.$2;
    }
    return msg;
}

function shortcut_subst_word(word,shortcuts) {
    return trimspaces(shortcuts[word.toUpperCase()]) || word;
}

/* This version replaces all uppercase words */
function subst_shortcut(msg,shortcuts) {
    if (!msg) return msg;
    var ret = '';
    var m;
    while (msg && (m = msg.match(/^(.*?)\b([A-Z]+)\b(.*)$/)) ) {
        ret += m[1] + subst_word(m[2],shortcuts);
        msg = m[3];
    }

    ret += msg;
    return ret;
}

// </nowiki></pre>