Take the 2-minute tour ×
Stack Apps is a question and answer site for apps, scripts, and development with the Stack Exchange API. It's 100% free, no registration required.

This is a mini-script to convert Ctrl+Space to a non-break-space (0x160, not  ).

You can copy-paste the code below (if you want to change the includes, for example), or check out the gist here:

UPDATE, now converts " to « and » (alternately).

Click HERE to install the user script.

Click HERE to preview the code.

// ==UserScript==
// @name NonBreakSpaces+
// @namespace benjol
// @description Make Ctrl-space add a non-break-space in textareas
// @include http://french.stackexchange.com/questions/*
// @include http://meta.french.stackexchange.com/questions/*
// @include http://chat.stackexchange.com/rooms/1098/*
// ==/UserScript==
function inject(f) {
  var script = document.createElement("script");
  script.type = "text/javascript";
  script.textContent = "(" + f.toString() + ")()";
  document.body.appendChild(script);
};

//Note that if you copy in Firefox, you'll get the impression that the nbsp isn't there, but it is!
// (https://bugzilla.mozilla.org/show_bug.cgi?id=359303)
inject(function () {
  var nbsp = String.fromCharCode(160),
      left = "«" + nbsp, //171
      right = nbsp + "»", //187
      next = left;

  function insert(field, val) {
    if(field.selectionStart || field.selectionStart == '0') {
      var cursorAfter = field.selectionStart + val.length;
      var first = field.value.slice(0, field.selectionStart);
      var second = field.value.slice(field.selectionEnd);
      field.value = first + val + second;
      field.setSelectionRange(cursorAfter, cursorAfter);
    }
    else //won't work in IE, sorry!
      field.value += val;
  }

  $(document).keypress(function(event) {
    var field = event.target;

    if(event.ctrlKey && event.charCode == 32) {
        insert(field, nbsp);
        event.preventDefault();
    } else if (event.charCode == 34) {
        insert(field, next);
next = (next == left) ? right : left;
        event.preventDefault();
    };
  });
});
share|improve this question
    
You should include meta.french.* –  M'vy Sep 6 '11 at 9:14
    
@M'vy, good point, fixed. –  Benjol Sep 6 '11 at 11:57
    
You should use the narrow no-break space ‘U+202F’ after « and before ». I'm a total js-ignorant, would it be possible to perform automatic substitutions just before submitting content? e.g. "\u020?" → "\u202F?". or "\u020:" → "\u00A0:"? –  Stéphane Gimenez Sep 9 '11 at 13:53
    
@Stéphane, I'll look into it... –  Benjol Sep 10 '11 at 21:10
    
@StéphaneGimenez, Word doesn't seem to like your nnbsp... not sure if that's a deal-breaker or not.. –  Benjol Sep 12 '11 at 13:06
    
Word? Anyway, if you know how to hook some substitutions just before the contents submission, I'd be really interested in using it. –  Stéphane Gimenez Sep 12 '11 at 13:35
    
@Stéphane, I'll have a look, it's slightly more complicated because I have to check which textarea is being submitted (currently I just replace anything as you type in any textarea). –  Benjol Sep 12 '11 at 18:45
    
@Stéphane, for Word, see the screenshot I pasted in the answer below. It doesn't recognise narrow no break spaces. Not exactly a show-stopper, but it's what makes me hesitate to implement your request. –  Benjol Sep 15 '11 at 6:57
add comment

1 Answer 1

(Screenshot for Stéphane)

Word no likee

Nor Windows

share|improve this answer
    
It's very ugly, it will be better to use non-breaking space instead of narrow non-breaking space !! –  chepseskaf Sep 28 '11 at 6:13
add comment

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .