Insert Tab into code field

Discuss issues and requests related with the forum software
User avatar
Almost_there
Posts: 404
Joined: 30 Sep 2014, 10:32

Insert Tab into code field

22 Jan 2017, 17:06

Hi.

When editing code directly in forum editor, there is no easy way to insert a tab. Workaround is to copy Tab to clipboard and then paste (or can do that by simple script).

But that means more work for less, so I suggest a button to insert Tab character into test field.

As most web browsers, Firefox that I use does not support insert Tab directly into text fields, but rather select next controller/element.
User avatar
joedf
Posts: 8940
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: Insert Tab into code field

22 Jan 2017, 21:05

I just created a greasemonkey script for you...
try it out:

Code: Select all

// ==UserScript==
// @name        inserttab button
// @namespace   ahkscriptreplyeditor
// @include     https://autohotkey.com/boards/posting.php?mode=reply*
// @version     1
// @grant       none
// @author      [email protected] (MIT license)
// @description  Adds a "tab" button to insert tabs when using the full reply page on ahk forums
// ==/UserScript==

/*
// Author: http://alexking.org/blog/2003/06/02/inserting-at-the-cursor-using-javascript
// Modified so it's safe across browser windows
function insertAtCursor(myField, myValue) {
  var doc = myField.ownerDocument;
  //IE support
  if (doc.selection) {
    myField.focus();
    sel = doc.selection.createRange();
    sel.text = myValue;
  }
  //FF, hopefully others
  else if (myField.selectionStart || myField.selectionStart == '0') {
    var startPos = myField.selectionStart;
    var endPos = myField.selectionEnd;
    myField.value = myField.value.substring(0, startPos) + 
                    myValue + myField.value.substring(endPos, myField.value.length);
  } 
  // fallback to appending it to the field
  else {
    myField.value += myValue;
  }
}

// http://stackoverflow.com/a/512542/883015
function setCaretPosition(elemId, caretPos) {
    var elem = document.getElementById(elemId);

    if(elem != null) {
        if(elem.createTextRange) {
            var range = elem.createTextRange();
            range.move('character', caretPos);
            range.select();
        }
        else {
            if(elem.selectionStart) {
                elem.focus();
                elem.setSelectionRange(caretPos, caretPos);
            }
            else
                elem.focus();
        }
    }
}

// http://stackoverflow.com/a/2897229/883015
function doGetCaretPosition(oField) {

  // Initialize
  var iCaretPos = 0;

  // IE Support
  if (document.selection) {

    // Set focus on the element
    oField.focus();

    // To get cursor position, get empty selection range
    var oSel = document.selection.createRange();

    // Move selection start to 0 position
    oSel.moveStart('character', -oField.value.length);

    // The caret position is selection length
    iCaretPos = oSel.text.length;
  }

  // Firefox support
  else if (oField.selectionStart || oField.selectionStart == '0')
    iCaretPos = oField.selectionStart;

  // Return results
  return iCaretPos;
}

function inserttab() {
  tbox = document.getElementById("message");
  pos = doGetCaretPosition(tbox) + 1;
  insertAtCursor(tbox,"\t");
  tbox.focus();
  setCaretPosition(tbox,pos);
  tbox.focus();
}
*/

//code (but compressed) above is injected

js_inject = "function insertAtCursor(a,b){var c=a.ownerDocument;if(c.selection)a.focus(),sel=c.selection.createRange(),sel.text=b;else if(a.selectionStart||\"0\"==a.selectionStart){var d=a.selectionStart,e=a.selectionEnd;a.value=a.value.substring(0,d)+b+a.value.substring(e,a.value.length)}else a.value+=b}function setCaretPosition(a,b){var c=document.getElementById(a);if(null!=c)if(c.createTextRange){var d=c.createTextRange();d.move(\"character\",b),d.select()}else c.selectionStart?(c.focus(),c.setSelectionRange(b,b)):c.focus()}function doGetCaretPosition(a){var b=0;if(document.selection){a.focus();var c=document.selection.createRange();c.moveStart(\"character\",-a.value.length),b=c.text.length}else(a.selectionStart||\"0\"==a.selectionStart)&&(b=a.selectionStart);return b}function inserttab(){tbox=document.getElementById(\"message\"),pos=doGetCaretPosition(tbox)+1,insertAtCursor(tbox,\"\\t\"),tbox.focus(),setCaretPosition(tbox,pos),tbox.focus()}"

var inserttabjs = document.createElement("script");
inserttabjs.id = "inserttabjs"
inserttabjs.type = "text/javascript"
inserttabjs.innerHTML = js_inject
document.body.appendChild(inserttabjs);


x = document.getElementById("format-buttons").innerHTML
document.getElementById("format-buttons").innerHTML = x + '<input class="button2" value="Tab" onclick="javascript:inserttab()" type="button">'
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
guest3456
Posts: 3454
Joined: 09 Oct 2013, 10:31

Re: Insert Tab into code field

23 Jan 2017, 08:45

when within a

Code: Select all

[/c] block, pressing TAB already inserts a tab. when not in a [c][code][/c] block, it tabs to the next element

i'm on Chrome

User avatar
joedf
Posts: 8940
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: Insert Tab into code field

23 Jan 2017, 10:23

Interesting. :think: ill have to try that when I get home.
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
User avatar
tidbit
Posts: 1272
Joined: 29 Sep 2013, 17:15
Location: USA

Re: Insert Tab into code field

23 Jan 2017, 11:09

ha
rawr. fear me.
*poke*
Is it December 21, 2012 yet?
User avatar
Almost_there
Posts: 404
Joined: 30 Sep 2014, 10:32

Re: Insert Tab into code field

23 Jan 2017, 13:27

Huh - must test :geek:

Code: Select all

;test	
	; Ok - it work, but only in "advanced mode"
Thanks - I didn't know that :mrgreen:
User avatar
tank
Posts: 3122
Joined: 28 Sep 2013, 22:15
Location: CarrolltonTX
Contact:

Re: Insert Tab into code field

09 Feb 2017, 11:21

yea this is more a limitation of how browsers handle tab characters by default. which is to switch to the next tab index. its a relic of how ancient word processors worked
We are troubled on every side‚ yet not distressed; we are perplexed‚
but not in despair; Persecuted‚ but not forsaken; cast down‚ but not destroyed;
Telegram is the best way to reach me
https://t.me/ttnnkkrr
If you have forum suggestions please submit a
Check Out WebWriter
letconex

Re: Insert Tab into code field

12 Oct 2018, 01:12

Take a look, it works for me, I wanted to send a tab in Word in a table, without going to the next cell, so it just pastes a TAB:

Clipboard = " %A_Tab% " ;MsgBox %Clipboard%
word_array := StrSplit(ClipBoard, A_Space) ; Omits periods.
Clipboard := word_array[2] ; MsgBox X%Clipboard%X
SendInput ^v ; paste tab
Return

Return to “Forum Issues”

Who is online

Users browsing this forum: No registered users and 33 guests