util.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. define(function(require, exports, module) {
  2. var registers = require("../registers");
  3. var dom = require("../../../lib/dom");
  4. dom.importCssString('.insert-mode .ace_cursor{\
  5. border-left: 2px solid #333333;\
  6. }\
  7. .ace_dark.insert-mode .ace_cursor{\
  8. border-left: 2px solid #eeeeee;\
  9. }\
  10. .normal-mode .ace_cursor{\
  11. border: 0!important;\
  12. background-color: red;\
  13. opacity: 0.5;\
  14. }', 'vimMode');
  15. module.exports = {
  16. onVisualMode: false,
  17. onVisualLineMode: false,
  18. currentMode: 'normal',
  19. noMode: function(editor) {
  20. editor.unsetStyle('insert-mode');
  21. editor.unsetStyle('normal-mode');
  22. if (editor.commands.recording)
  23. editor.commands.toggleRecording(editor);
  24. editor.setOverwrite(false);
  25. },
  26. insertMode: function(editor) {
  27. this.currentMode = 'insert';
  28. // Switch editor to insert mode
  29. editor.setStyle('insert-mode');
  30. editor.unsetStyle('normal-mode');
  31. editor.setOverwrite(false);
  32. editor.keyBinding.$data.buffer = "";
  33. editor.keyBinding.$data.state = "insertMode";
  34. this.onVisualMode = false;
  35. this.onVisualLineMode = false;
  36. if(this.onInsertReplaySequence) {
  37. // Ok, we're apparently replaying ("."), so let's do it
  38. editor.commands.macro = this.onInsertReplaySequence;
  39. editor.commands.replay(editor);
  40. this.onInsertReplaySequence = null;
  41. this.normalMode(editor);
  42. } else {
  43. editor._emit("changeStatus");
  44. // Record any movements, insertions in insert mode
  45. if(!editor.commands.recording)
  46. editor.commands.toggleRecording(editor);
  47. }
  48. },
  49. normalMode: function(editor) {
  50. // Switch editor to normal mode
  51. this.currentMode = 'normal';
  52. editor.unsetStyle('insert-mode');
  53. editor.setStyle('normal-mode');
  54. editor.clearSelection();
  55. var pos;
  56. if (!editor.getOverwrite()) {
  57. pos = editor.getCursorPosition();
  58. if (pos.column > 0)
  59. editor.navigateLeft();
  60. }
  61. editor.setOverwrite(true);
  62. editor.keyBinding.$data.buffer = "";
  63. editor.keyBinding.$data.state = "start";
  64. this.onVisualMode = false;
  65. this.onVisualLineMode = false;
  66. editor._emit("changeStatus");
  67. // Save recorded keystrokes
  68. if (editor.commands.recording) {
  69. editor.commands.toggleRecording(editor);
  70. return editor.commands.macro;
  71. }
  72. else {
  73. return [];
  74. }
  75. },
  76. visualMode: function(editor, lineMode) {
  77. if (
  78. (this.onVisualLineMode && lineMode)
  79. || (this.onVisualMode && !lineMode)
  80. ) {
  81. this.normalMode(editor);
  82. return;
  83. }
  84. editor.setStyle('insert-mode');
  85. editor.unsetStyle('normal-mode');
  86. editor._emit("changeStatus");
  87. if (lineMode) {
  88. this.onVisualLineMode = true;
  89. } else {
  90. this.onVisualMode = true;
  91. this.onVisualLineMode = false;
  92. }
  93. },
  94. getRightNthChar: function(editor, cursor, ch, n) {
  95. var line = editor.getSession().getLine(cursor.row);
  96. var matches = line.substr(cursor.column + 1).split(ch);
  97. return n < matches.length ? matches.slice(0, n).join(ch).length : null;
  98. },
  99. getLeftNthChar: function(editor, cursor, ch, n) {
  100. var line = editor.getSession().getLine(cursor.row);
  101. var matches = line.substr(0, cursor.column).split(ch);
  102. return n < matches.length ? matches.slice(-1 * n).join(ch).length : null;
  103. },
  104. toRealChar: function(ch) {
  105. if (ch.length === 1)
  106. return ch;
  107. if (/^shift-./.test(ch))
  108. return ch[ch.length - 1].toUpperCase();
  109. else
  110. return "";
  111. },
  112. copyLine: function(editor) {
  113. var pos = editor.getCursorPosition();
  114. editor.selection.clearSelection();
  115. editor.moveCursorTo(pos.row, pos.column);
  116. editor.selection.selectLine();
  117. registers._default.isLine = true;
  118. registers._default.text = editor.getCopyText().replace(/\n$/, "");
  119. editor.selection.clearSelection();
  120. editor.moveCursorTo(pos.row, pos.column);
  121. }
  122. };
  123. });