bootstrap.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // Tab Complete
  2. //
  3. // Douglas Thrift
  4. //
  5. // bootstrap.js
  6. /* ***** BEGIN LICENSE BLOCK *****
  7. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  8. *
  9. * The contents of this file are subject to the Mozilla Public License Version
  10. * 1.1 (the "License"); you may not use this file except in compliance with
  11. * the License. You may obtain a copy of the License at
  12. * http://www.mozilla.org/MPL/
  13. *
  14. * Software distributed under the License is distributed on an "AS IS" basis,
  15. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  16. * for the specific language governing rights and limitations under the
  17. * License.
  18. *
  19. * The Original Code is Tab Complete.
  20. *
  21. * The Initial Developer of the Original Code is
  22. * Douglas Thrift <douglas@douglasthrift.net>.
  23. * Portions created by the Initial Developer are Copyright (C) 2011
  24. * the Initial Developer. All Rights Reserved.
  25. *
  26. * Contributor(s):
  27. *
  28. * Alternatively, the contents of this file may be used under the terms of
  29. * either the GNU General Public License Version 2 or later (the "GPL"), or
  30. * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  31. * in which case the provisions of the GPL or the LGPL are applicable instead
  32. * of those above. If you wish to allow use of your version of this file only
  33. * under the terms of either the GPL or the LGPL, and not to allow others to
  34. * use your version of this file under the terms of the MPL, indicate your
  35. * decision by deleting the provisions above and replace them with the notice
  36. * and other provisions required by the GPL or the LGPL. If you do not delete
  37. * the provisions above, a recipient may use your version of this file under
  38. * the terms of any one of the MPL, the GPL or the LGPL.
  39. *
  40. * ***** END LICENSE BLOCK ***** */
  41. Components.utils.import("resource:///modules/imServices.jsm");
  42. function generatorFromArray(array)
  43. {
  44. for (var index = 0; index != array.length; ++index)
  45. yield array[index];
  46. }
  47. function generatorFromEnumerator(enumerator)
  48. {
  49. while (enumerator.hasMoreElements())
  50. yield enumerator.getNext();
  51. }
  52. let observer = {
  53. observe: function(subject, topic, data)
  54. {
  55. if (topic != "conversation-loaded")
  56. return;
  57. this.addTabListener(subject);
  58. },
  59. addTabListener: function(browser)
  60. {
  61. var binding = browser.ownerDocument.getBindingParent(browser);
  62. if (!binding || !("editor" in binding) || !binding.editor)
  63. return;
  64. binding.editor.addEventListener("keypress", this.onKeyPress, true);
  65. },
  66. removeTabListener: function(browser)
  67. {
  68. var binding = browser.ownerDocument.getBindingParent(browser);
  69. if (!binding || !("editor" in binding) || !binding.editor)
  70. return;
  71. binding.editor.removeEventListener("keypress", this.onKeyPress, true);
  72. },
  73. onKeyPress: function(event)
  74. {
  75. if ((event.keyCode != event.DOM_VK_TAB) || event.altKey || event.ctrlKey || event.metaKey || event.shiftKey)
  76. return;
  77. event.preventDefault();
  78. var editor = event.target;
  79. var binding = editor.ownerDocument.getBindingParent(editor);
  80. var conversation = binding._conv;
  81. var completions = [];
  82. var word = editor.value.substring(0, editor.selectionStart).match(/\S*$/)[0];
  83. var [first, before, after] = [editor.selectionStart - word.length == 0, editor.value.substring(0, editor.selectionStart - word.length), editor.value.substring(editor.selectionEnd)];
  84. function completable(completion)
  85. {
  86. return completion.toLowerCase().indexOf(word.toLowerCase()) == 0;
  87. }
  88. if (first && (word.length == 0 || word.indexOf("/") == 0))
  89. for each (let command in Services.cmd.listCommandsForConversation(conversation))
  90. {
  91. var completion = "/" + command.name;
  92. if (completable(completion) && completions.indexOf(completion) == -1)
  93. completions.push(completion);
  94. }
  95. if (conversation.isChat)
  96. for each (let participant in generatorFromEnumerator(conversation.getParticipants()))
  97. {
  98. var completion = participant.name + (first ? ":" : "");
  99. if (completable(completion))
  100. completions.push(completion);
  101. }
  102. else
  103. {
  104. var completion = conversation.buddy.userName + (first ? ":" : "");
  105. if (completable(completion))
  106. completions.push(completion);
  107. }
  108. completions.sort(function(one, two)
  109. {
  110. [one, two] = [string.toLowerCase() for each (string in [one, two])];
  111. if (one < two)
  112. return -1;
  113. else if (one > two)
  114. return 1;
  115. else
  116. return 0;
  117. });
  118. var [completion, full] = [undefined, undefined];
  119. switch (completions.length)
  120. {
  121. case 0:
  122. return;
  123. case 1:
  124. [completion, full] = [completions[0], true];
  125. break;
  126. default:
  127. let [first, last] = [string.toLowerCase() for each (string in [completions[0], completions[completions.length - 1]])];
  128. let length = 0;
  129. for (; length != first.length; ++length)
  130. if (first[length] != last[length])
  131. break;
  132. [completion, full] = [first.substring(0, length), false];
  133. conversation.systemMessage(completions.join(" "));
  134. }
  135. if (completion.length != 0)
  136. {
  137. editor.value = before + completion + (full ? " " : "") + after;
  138. var cursor = before.length + completion.length + (full ? 1 : 0);
  139. editor.setSelectionRange(cursor, cursor);
  140. }
  141. },
  142. };
  143. function startup(data, reason)
  144. {
  145. for each (let window in generatorFromEnumerator(Services.wm.getEnumerator("Messenger:convs")))
  146. for each (let tabconversation in generatorFromArray(window.document.getElementsByTagName("tabconversation")))
  147. for each (let browser in tabconversation.browsers)
  148. observer.addTabListener(browser);
  149. Services.obs.addObserver(observer, "conversation-loaded", false);
  150. }
  151. function shutdown(data, reason)
  152. {
  153. Services.obs.removeObserver(observer, "conversation-loaded");
  154. for each (let window in generatorFromEnumerator(Services.wm.getEnumerator("Messenger:convs")))
  155. for each (let tabconversation in generatorFromArray(window.document.getElementsByTagName("tabconversation")))
  156. for each (let browser in tabconversation.browsers)
  157. observer.removeTabListener(browser);
  158. }
  159. function install(data, reason) {}
  160. function uninstall(data, reason) {}
  161. // vim: expandtab