summaryrefslogtreecommitdiff
path: root/js/jquery/jquery.console.js
blob: 4a75a99be495d82ee4ee828a7bb88f3d58716839 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
// JQuery Console 1.0
// Sun Feb 21 20:28:47 GMT 2010
//
// Copyright 2010 Chris Done, Simon David Pratt. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
//    1. Redistributions of source code must retain the above
//       copyright notice, this list of conditions and the following
//       disclaimer.
//
//    2. Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials
//       provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

// TESTED ON
//   Internet Explorer 6
//   Opera 10.01
//   Chromium 4.0.237.0 (Ubuntu build 31094)
//   Firefox 3.5.8, 3.6.2 (Mac)
//   Safari 4.0.5 (6531.22.7) (Mac)
//   Google Chrome 5.0.375.55 (Mac)

(function($){
  var isWebkit = !!~navigator.userAgent.indexOf(' AppleWebKit/');

  $.fn.console = function(config){
    ////////////////////////////////////////////////////////////////////////
    // Constants
    // Some are enums, data types, others just for optimisation
    var keyCodes = {
      // left
      37: moveBackward,
      // right
      39: moveForward,
      // up
      38: previousHistory,
      // down
      40: nextHistory,
      // backspace
      8:  backDelete,
      // delete
      46: forwardDelete,
      // end
      35: moveToEnd,
      // start
      36: moveToStart,
      // return
      13: commandTrigger,
      // tab
      18: doNothing,
      // tab
      9: doComplete
    };
    var ctrlCodes = {
      // C-a
      65: moveToStart,
      // C-e
      69: moveToEnd,
      // C-d
      68: forwardDelete,
      // C-n
      78: nextHistory,
      // C-p
      80: previousHistory,
      // C-b
      66: moveBackward,
      // C-f
      70: moveForward,
      // C-k
      75: deleteUntilEnd
    };
    if(config.ctrlCodes) {
      $.extend(ctrlCodes, config.ctrlCodes);
    }
    var altCodes = {
      // M-f
      70: moveToNextWord,
      // M-b
      66: moveToPreviousWord,
      // M-d
      68: deleteNextWord
    };
    var shiftCodes = {
      // return
      13: newLine,
    };
    var cursor = '<span class="jquery-console-cursor">&nbsp;</span>';

    ////////////////////////////////////////////////////////////////////////
    // Globals
    var container = $(this);
    var inner = $('<div class="jquery-console-inner"></div>');
    // erjiang: changed this from a text input to a textarea so we
    // can get pasted newlines
    var typer = $('<textarea autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" class="jquery-console-typer"></textarea>');
    // Prompt
    var promptBox;
    var prompt;
    var continuedPromptLabel = config && config.continuedPromptLabel?
      config.continuedPromptLabel : "> ";
    var column = 0;
    var promptText = '';
    var restoreText = '';
    var continuedText = '';
    var fadeOnReset = config.fadeOnReset !== undefined ? config.fadeOnReset : true;
    // Prompt history stack
    var history = [];
    var ringn = 0;
    // For reasons unknown to The Sword of Michael himself, Opera
    // triggers and sends a key character when you hit various
    // keys like PgUp, End, etc. So there is no way of knowing
    // when a user has typed '#' or End. My solution is in the
    // typer.keydown and typer.keypress functions; I use the
    // variable below to ignore the keypress event if the keydown
    // event succeeds.
    var cancelKeyPress = 0;
    // When this value is false, the prompt will not respond to input
    var acceptInput = true;
    // When this value is true, the command has been canceled
    var cancelCommand = false;

    // External exports object
    var extern = {};

    ////////////////////////////////////////////////////////////////////////
    // Main entry point
    (function(){
      extern.promptLabel = config && config.promptLabel? config.promptLabel : "> ";
      container.append(inner);
      inner.append(typer);
      typer.css({position:'absolute',top:0,left:'-9999px'});
      if (config.welcomeMessage)
	message(config.welcomeMessage,'jquery-console-welcome');
      newPromptBox();
      if (config.autofocus) {
	inner.addClass('jquery-console-focus');
	typer.focus();
	setTimeout(function(){
	  inner.addClass('jquery-console-focus');
	  typer.focus();
	},100);
      }
      extern.inner = inner;
      extern.typer = typer;
      extern.scrollToBottom = scrollToBottom;
      extern.report = report;
    })();

    ////////////////////////////////////////////////////////////////////////
    // Reset terminal
    extern.reset = function(){
      var welcome = (typeof config.welcomeMessage != 'undefined');

      var removeElements = function() {
	inner.find('div').each(function(){
	  if (!welcome) {
	    $(this).remove();
	  } else {
	    welcome = false;
	  }
	});
      };

      if (fadeOnReset) {
	inner.parent().fadeOut(function() {
	  removeElements();
	  newPromptBox();
	  inner.parent().fadeIn(focusConsole);
	});
      }
      else {
	removeElements();
	newPromptBox();
	focusConsole();
      }
    };

    var focusConsole = function() {
      inner.addClass('jquery-console-focus');
      typer.focus();
    };

    extern.focus = function(){
      focusConsole();
    }

    ////////////////////////////////////////////////////////////////////////
    // Reset terminal
    extern.notice = function(msg,style){
      var n = $('<div class="notice"></div>').append($('<div></div>').text(msg))
	.css({visibility:'hidden'});
      container.append(n);
      var focused = true;
      if (style=='fadeout')
	setTimeout(function(){
	  n.fadeOut(function(){
	    n.remove();
	  });
	},4000);
      else if (style=='prompt') {
	var a = $('<br/><div class="action"><a href="javascript:">OK</a><div class="clear"></div></div>');
	n.append(a);
	focused = false;
	a.click(function(){ n.fadeOut(function(){ n.remove();inner.css({opacity:1}) }); });
      }
      var h = n.height();
      n.css({height:'0px',visibility:'visible'})
	.animate({height:h+'px'},function(){
	  if (!focused) inner.css({opacity:0.5});
	});
      n.css('cursor','default');
      return n;
    };

    ////////////////////////////////////////////////////////////////////////
    // Make a new prompt box
    function newPromptBox() {
      column = 0;
      promptText = '';
      ringn = 0; // Reset the position of the history ring
      enableInput();
      promptBox = $('<div class="jquery-console-prompt-box"></div>');
      var label = $('<span class="jquery-console-prompt-label"></span>');
      var labelText = extern.continuedPrompt? continuedPromptLabel : extern.promptLabel;
      promptBox.append(label.text(labelText).show());
      label.html(label.html().replace(' ','&nbsp;'));
      prompt = $('<span class="jquery-console-prompt"></span>');
      promptBox.append(prompt);
      inner.append(promptBox);
      updatePromptDisplay();
    };

    ////////////////////////////////////////////////////////////////////////
    // Handle setting focus
    container.click(function(){
      // Don't mess with the focus if there is an active selection
      if (window.getSelection().toString()) {
	return false;
      }

      inner.addClass('jquery-console-focus');
      inner.removeClass('jquery-console-nofocus');
      if (isWebkit) {
	typer.focusWithoutScrolling();
      } else {
	typer.css('position', 'fixed').focus();
      }
      scrollToBottom();
      return false;
    });

    ////////////////////////////////////////////////////////////////////////
    // Handle losing focus
    typer.blur(function(){
      inner.removeClass('jquery-console-focus');
      inner.addClass('jquery-console-nofocus');
    });

    ////////////////////////////////////////////////////////////////////////
    // Bind to the paste event of the input box so we know when we
    // get pasted data
    typer.bind('paste', function(e) {
      // wipe typer input clean just in case
      typer.val("");
      // this timeout is required because the onpaste event is
      // fired *before* the text is actually pasted
      setTimeout(function() {
	typer.consoleInsert(typer.val());
	typer.val("");
      }, 0);
    });

    ////////////////////////////////////////////////////////////////////////
    // Handle key hit before translation
    // For picking up control characters like up/left/down/right

    typer.keydown(function(e){
      cancelKeyPress = 0;
      var keyCode = e.keyCode;
      // C-c: cancel the execution
      if(e.ctrlKey && keyCode == 67) {
	cancelKeyPress = keyCode;
	cancelExecution();
	return false;
      }
      if (acceptInput) {
	if (e.shiftKey && keyCode in shiftCodes) {
	  cancelKeyPress = keyCode;
	  (shiftCodes[keyCode])();
	  return false;
	} else if (e.altKey  && keyCode in altCodes) {
	  cancelKeyPress = keyCode;
	  (altCodes[keyCode])();
	  return false;
	} else if (e.ctrlKey && keyCode in ctrlCodes) {
	  cancelKeyPress = keyCode;
	  (ctrlCodes[keyCode])();
	  return false;
	} else if (keyCode in keyCodes) {
	  cancelKeyPress = keyCode;
	  (keyCodes[keyCode])();
	  return false;
	}
      }
    });

    ////////////////////////////////////////////////////////////////////////
    // Handle key press
    typer.keypress(function(e){
      var keyCode = e.keyCode || e.which;
      if (isIgnorableKey(e)) {
	return false;
      }
      // C-v: don't insert on paste event
      if ((e.ctrlKey || e.metaKey) && String.fromCharCode(keyCode).toLowerCase() == 'v') {
	return true;
      }
      if (acceptInput && cancelKeyPress != keyCode && keyCode >= 32){
	if (cancelKeyPress) return false;
	if (
	  typeof config.charInsertTrigger == 'undefined' || (
	    typeof config.charInsertTrigger == 'function' &&
	      config.charInsertTrigger(keyCode,promptText)
	  )
	){
	  typer.consoleInsert(keyCode);
	}
      }
      if (isWebkit) return false;
    });

    function isIgnorableKey(e) {
      // for now just filter alt+tab that we receive on some platforms when
      // user switches windows (goes away from the browser)
      return ((e.keyCode == keyCodes.tab || e.keyCode == 192) && e.altKey);
    };

    ////////////////////////////////////////////////////////////////////////
    // Rotate through the command history
    function rotateHistory(n){
      if (history.length == 0) return;
      ringn += n;
      if (ringn < 0) ringn = history.length;
      else if (ringn > history.length) ringn = 0;
      var prevText = promptText;
      if (ringn == 0) {
	promptText = restoreText;
      } else {
	promptText = history[ringn - 1];
      }
      if (config.historyPreserveColumn) {
	if (promptText.length < column + 1) {
	  column = promptText.length;
	} else if (column == 0) {
	  column = promptText.length;
	}
      } else {
	column = promptText.length;
      }
      updatePromptDisplay();
    };

    function previousHistory() {
      rotateHistory(-1);
    };

    function nextHistory() {
      rotateHistory(1);
    };

    // Add something to the history ring
    function addToHistory(line){
      history.push(line);
      restoreText = '';
    };

    // Delete the character at the current position
    function deleteCharAtPos(){
      if (column < promptText.length){
	promptText =
	  promptText.substring(0,column) +
	  promptText.substring(column+1);
	restoreText = promptText;
	return true;
      } else return false;
    };

    function backDelete() {
      if (moveColumn(-1)){
	deleteCharAtPos();
	updatePromptDisplay();
      }
    };

    function forwardDelete() {
      if (deleteCharAtPos()){
	updatePromptDisplay();
      }
    };

    function deleteUntilEnd() {
      while(deleteCharAtPos()) {
	updatePromptDisplay();
      }
    };

    function deleteNextWord() {
      // A word is defined within this context as a series of alphanumeric
      // characters.
      // Delete up to the next alphanumeric character
      while(
	column < promptText.length &&
	  !isCharAlphanumeric(promptText[column])
      ) {
	deleteCharAtPos();
	updatePromptDisplay();
      }
      // Then, delete until the next non-alphanumeric character
      while(
	column < promptText.length &&
	  isCharAlphanumeric(promptText[column])
      ) {
	deleteCharAtPos();
	updatePromptDisplay();
      }
    };

    function newLine() {
      var lines = promptText.split("\n");
      var last_line = lines.slice(-1)[0];
      var spaces = last_line.match(/^(\s*)/g)[0];
      var new_line = "\n" + spaces;
      promptText += new_line;
      moveColumn(new_line.length);
      updatePromptDisplay();
    };

    ////////////////////////////////////////////////////////////////////////
    // Validate command and trigger it if valid, or show a validation error
    function commandTrigger() {
      var line = promptText;
      if (typeof config.commandValidate == 'function') {
	var ret = config.commandValidate(line);
	if (ret == true || ret == false) {
	  if (ret) {
	    handleCommand();
	  }
	} else {
	  commandResult(ret,"jquery-console-message-error");
	}
      } else {
	handleCommand();
      }
    };

    // Scroll to the bottom of the view
    function scrollToBottom() {
      var version = jQuery.fn.jquery.split('.');
      var major = parseInt(version[0]);
      var minor = parseInt(version[1]);

      // check if we're using jquery > 1.6
      if ((major == 1 && minor > 6) || major > 1) {
	inner.prop({ scrollTop: inner.prop("scrollHeight") });
      }
      else {
	inner.attr({ scrollTop: inner.attr("scrollHeight") });
      }
    };

    function cancelExecution() {
      if(typeof config.cancelHandle == 'function') {
	config.cancelHandle();
      }
    }

    ////////////////////////////////////////////////////////////////////////
    // Handle a command
    function handleCommand() {
      if (typeof config.commandHandle == 'function') {
	disableInput();
	addToHistory(promptText);
	var text = promptText;
	if (extern.continuedPrompt) {
	  if (continuedText)
	    continuedText += '\n' + promptText;
	  else continuedText = promptText;
	} else continuedText = undefined;
	if (continuedText) text = continuedText;
	var ret = config.commandHandle(text,function(msgs){
	  commandResult(msgs);
	});
	if (extern.continuedPrompt && !continuedText)
	  continuedText = promptText;
	if (typeof ret == 'boolean') {
	  if (ret) {
	    // Command succeeded without a result.
	    commandResult();
	  } else {
	    commandResult(
	      'Command failed.',
	      "jquery-console-message-error"
	    );
	  }
	} else if (typeof ret == "string") {
	  commandResult(ret,"jquery-console-message-success");
	} else if (typeof ret == 'object' && ret.length) {
	  commandResult(ret);
	} else if (extern.continuedPrompt) {
	  commandResult();
	}
      }
    };

    ////////////////////////////////////////////////////////////////////////
    // Disable input
    function disableInput() {
      acceptInput = false;
    };

    // Enable input
    function enableInput() {
      acceptInput = true;
    }

    ////////////////////////////////////////////////////////////////////////
    // Reset the prompt in invalid command
    function commandResult(msg,className) {
      column = -1;
      updatePromptDisplay();
      if (typeof msg == 'string') {
	message(msg,className);
      } else if ($.isArray(msg)) {
	for (var x in msg) {
	  var ret = msg[x];
	  message(ret.msg,ret.className);
	}
      } else { // Assume it's a DOM node or jQuery object.
	inner.append(msg);
      }
      newPromptBox();
    };

    ////////////////////////////////////////////////////////////////////////
    // Report some message into the console
    function report(msg,className) {
      var text = promptText;
      promptBox.remove();
      commandResult(msg,className);
      extern.promptText(text);
    };

    ////////////////////////////////////////////////////////////////////////
    // Display a message
    function message(msg,className) {
      var mesg = $('<div class="jquery-console-message"></div>');
      if (className) mesg.addClass(className);
      mesg.filledText(msg).hide();
      inner.append(mesg);
      mesg.show();
    };

    ////////////////////////////////////////////////////////////////////////
    // Handle normal character insertion
    // data can either be a number, which will be interpreted as the
    // numeric value of a single character, or a string
    typer.consoleInsert = function(data){
      // TODO: remove redundant indirection
      var text = (typeof data == 'number') ? String.fromCharCode(data) : data;
      var before = promptText.substring(0,column);
      var after = promptText.substring(column);
      promptText = before + text + after;
      moveColumn(text.length);
      restoreText = promptText;
      updatePromptDisplay();
    };

    ////////////////////////////////////////////////////////////////////////
    // Move to another column relative to this one
    // Negative means go back, positive means go forward.
    function moveColumn(n){
      if (column + n >= 0 && column + n <= promptText.length){
	column += n;
	return true;
      } else return false;
    };

    function moveForward() {
      if(moveColumn(1)) {
	updatePromptDisplay();
	return true;
      }
      return false;
    };

    function moveBackward() {
      if(moveColumn(-1)) {
	updatePromptDisplay();
	return true;
      }
      return false;
    };

    function moveToStart() {
      if (moveColumn(-column))
	updatePromptDisplay();
    };

    function moveToEnd() {
      if (moveColumn(promptText.length-column))
	updatePromptDisplay();
    };

    function moveToNextWord() {
      while(
	column < promptText.length &&
	  !isCharAlphanumeric(promptText[column]) &&
	  moveForward()
      ) {}
      while(
	column < promptText.length &&
	  isCharAlphanumeric(promptText[column]) &&
	  moveForward()
      ) {}
    };

    function moveToPreviousWord() {
      // Move backward until we find the first alphanumeric
      while(
	column -1 >= 0 &&
	  !isCharAlphanumeric(promptText[column-1]) &&
	  moveBackward()
      ) {}
      // Move until we find the first non-alphanumeric
      while(
	column -1 >= 0 &&
	  isCharAlphanumeric(promptText[column-1]) &&
	  moveBackward()
      ) {}
    };

    function isCharAlphanumeric(charToTest) {
      if(typeof charToTest == 'string') {
	var code = charToTest.charCodeAt();
	return (code >= 'A'.charCodeAt() && code <= 'Z'.charCodeAt()) ||
	  (code >= 'a'.charCodeAt() && code <= 'z'.charCodeAt()) ||
	  (code >= '0'.charCodeAt() && code <= '9'.charCodeAt());
      }
      return false;
    };

    function doComplete() {
      if(typeof config.completeHandle == 'function') {
	var completions = config.completeHandle(promptText);
	var len = completions.length;
	if (len === 1) {
	  extern.promptText(promptText + completions[0]);
	} else if (len > 1 && config.cols) {
	  var prompt = promptText;
	  // Compute the number of rows that will fit in the width
	  var max = 0;
	  for (var i = 0;i < len;i++) {
	    max = Math.max(max, completions[i].length);
	  }
	  max += 2;
	  var n = Math.floor(config.cols / max);
	  var buffer = "";
	  var col = 0;
	  for (i = 0;i < len;i++) {
	    var completion = completions[i];
	    buffer += completions[i];
	    for (var j = completion.length;j < max;j++) {
	      buffer += " ";
	    }
	    if (++col >= n) {
	      buffer += "\n";
	      col = 0;
	    }
	  }
	  commandResult(buffer,"jquery-console-message-value");
	  extern.promptText(prompt);
	}
      }
    };

    function doNothing() {};

    extern.promptText = function(text){
      if (typeof text === 'string') {
	promptText = text;
	column = promptText.length;
	updatePromptDisplay();
      }
      return promptText;
    };

    ////////////////////////////////////////////////////////////////////////
    // Update the prompt display
    function updatePromptDisplay(){
      var line = promptText;
      var html = '';
      if (column > 0 && line == ''){
	// When we have an empty line just display a cursor.
	html = cursor;
      } else if (column == promptText.length){
	// We're at the end of the line, so we need to display
	// the text *and* cursor.
	html = htmlEncode(line) + cursor;
      } else {
	// Grab the current character, if there is one, and
	// make it the current cursor.
	var before = line.substring(0, column);
	var current = line.substring(column,column+1);
	if (current){
	  current =
	    '<span class="jquery-console-cursor">' +
	    htmlEncode(current) +
	    '</span>';
	}
	var after = line.substring(column+1);
	html = htmlEncode(before) + current + htmlEncode(after);
      }
      prompt.html(html);
      scrollToBottom();
    };

    // Simple HTML encoding
    // Simply replace '<', '>' and '&'
    // TODO: Use jQuery's .html() trick, or grab a proper, fast
    // HTML encoder.
    function htmlEncode(text){
      return (
	text.replace(/&/g,'&amp;')
	  .replace(/</g,'&lt;')
	  .replace(/</g,'&lt;')
	  .replace(/ /g,'&nbsp;')
	  .replace(/\n/g,'<br />')
      );
    };

    return extern;
  };
  // Simple utility for printing messages
  $.fn.filledText = function(txt){
    $(this).text(txt);
    $(this).html($(this).html().replace(/\n/g,'<br/>'));
    return this;
  };

  // Alternative method for focus without scrolling
  $.fn.focusWithoutScrolling = function(){
    var x = window.scrollX, y = window.scrollY;
    $(this).focus();
    window.scrollTo(x, y);
  };
})(jQuery);