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:19, 26 January 2006 (factoring from User:Quarl/afd_helper.js). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
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 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;
}

/* This version replaces only the first word and doesn't require uppercase */
function subst_shortcut1(msg,shortcuts) {
    if (!msg) return msg;
    var m = msg.match(/^([a-zA-Z]+)(.*)$/);
    if (m) {
        return subst_word(m[1],shortcuts) + m[2];
    }
    return msg;
}

function 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>