6

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();
    };
  });
});
7
  • You should include meta.french.* Commented Sep 6, 2011 at 9:14
  • @M'vy, good point, fixed. Commented Sep 6, 2011 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:"? Commented Sep 9, 2011 at 13:53
  • @StéphaneGimenez, Word doesn't seem to like your nnbsp... not sure if that's a deal-breaker or not.. Commented Sep 12, 2011 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. Commented Sep 12, 2011 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). Commented Sep 12, 2011 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. Commented Sep 15, 2011 at 6:57

1 Answer 1

2

(Screenshot for Stéphane)

Word no likee

Nor Windows

1
  • It's very ugly, it will be better to use non-breaking space instead of narrow non-breaking space !! Commented Sep 28, 2011 at 6:13

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.