Tab Override

Tab Override allows you to enter tabs into the content field (the main text area) when writing posts or pages.

Version: 1.1

Compatibility: WordPress 2.7+

Download: Tab Override

Installation

Download and extract the plugin from the above link. Upload the tab-override folder to your plugins directory. You can then activate the plugin for your Wordpress dashboard.

Usage

No configuration or editing required. Just activate the plugin

Some Notes

As always the Visual editor will destroy your formatting, including tabs. In addition to this, tabs will function as they normally would in the Visual Editor. If you stick to the HTML editor, your tabs will remain in your code, though they will only be visible if they are nested in pre tags.

If you primarily use the visual editor this plugin is most likely not for you. If you use the HTML editor or frequently post source code this plugin is for you.

Compatible Browsers

  • Opera 9.6-10.10
  • Firefox 3

Not Compatible Yet

  • IE 6,7,8
  • Safari
  • Chrome

Changelog

1.1

  • Added Firefox support

1.0

  • Initial Release
  • Opera Only

The Code

This is the javascript used to implement this plugin. If you have any improvements please contact me (I’m more of a PHP programmer than a javascript one). If anyone knows how to make this code compatible with more browsers please contact me.

//firefox
function firefoxTab(evt) {
    var t = evt.target;
    var ss = t.selectionStart;
    var se = t.selectionEnd;
	var tab = String.fromCharCode(9);

    // Tab key - insert tab expansion
    if (evt.keyCode == 9) {
        evt.preventDefault();

        // Special case of multi line selection
        if (ss != se && t.value.slice(ss,se).indexOf("\n") != -1) {
            // In case selection was not of entire lines (e.g. selection begins in the middle of a line)
            // we ought to tab at the beginning as well as at the start of every following line.
            var pre = t.value.slice(0,ss);
            var sel = t.value.slice(ss,se).replace(/\n/g,"\n"+tab);
            var post = t.value.slice(se,t.value.length);
            t.value = pre.concat(tab).concat(sel).concat(post);

            t.selectionStart = ss + tab.length;
            t.selectionEnd = se + tab.length;
        }
        // "Normal" case (no selection or selection on one line only)
        else {
            t.value = t.value.slice(0,ss).concat(tab).concat(t.value.slice(ss,t.value.length));
            if (ss == se) {
                t.selectionStart = t.selectionEnd = ss + tab.length;
            }
            else {
                t.selectionStart = ss + tab.length;
                t.selectionEnd = se + tab.length;
            }
        }
    }
}

//opera
function operaTab(e)
{
	var evtobj=window.event? event : e;
	var unicode=evtobj.charCode? evtobj.charCode : evtobj.keyCode;
	if (unicode==9)
	{
		var obj = document.getElementById('content');
		if(document.selection)
		{
			obj.selection = document.selection.createRange();
			obj.selection.text = String.fromCharCode(9);
		}
		else
		{
			var before, after;
			before = obj.value.substring(0, obj.selectionStart);
			after = obj.value.substring(obj.selectionEnd, obj.value.length);

			obj.value= String.concat(before, String.fromCharCode(9), after);
		}

		if(evtobj.stopPropagation) {
			evtobj.stopPropagation(); }
		else {
			evtobj.cancelBubble = true; }

		if(evtobj.preventDefault) {
			evtobj.preventDefault(); }
		else {
			evtobj.returnValue = false; }
	}
	return false;
}

function chooseFunction(e)
{
	if(navigator.userAgent.indexOf("Firefox") != -1)
	{
	   return firefoxTab(e);
	}
	else if(navigator.userAgent.indexOf("Opera") != -1)
	{
		return operaTab(e);
	}
	else
	{
		return;
	}
}

function tablistener(){document.getElementById('content').addEventListener('keypress', chooseFunction, true);}
window.onload=tablistener;
  • Twitter
  • Digg
  • StumbleUpon
  • Delicious
  • Reddit
  • Technorati Favorites
  • Slashdot
  • Share/Bookmark

6 Comments

    • Tinsley says:

      You’re thinking of the wrong kind of tab. The plugin you refer to allows you to add tabbed content to a post (ie like tabbed browsing). The plugin I wrote allows you to press the tab key and have a tab character appear in a text field.

      Porting that plugin to wordpress wouldn’t be too difficult of a task, but right now I have a little to much to do to address that. Maybe in the future, but right now I have 2 plugins that I just released, another that I’m trying to update, school, and a second blog that I just started.

    • gestroud says:

      @ Tinsley: This sound like exactly what I’ve been looking for.

      @ Supermag: There are two plugins that accomplish what you are looking for. I added their links to your post on WordPress.

Leave a Reply