operators.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Distributed under the BSD license:
  3. *
  4. * Copyright (c) 2010, Ajax.org B.V.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * * Neither the name of Ajax.org B.V. nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL AJAX.ORG B.V. BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * ***** END LICENSE BLOCK ***** */
  30. define(function(require, exports, module) {
  31. "use strict";
  32. var util = require("./util");
  33. var registers = require("../registers");
  34. module.exports = {
  35. "d": {
  36. selFn: function(editor, range, count, param) {
  37. registers._default.text = editor.getCopyText();
  38. registers._default.isLine = util.onVisualLineMode;
  39. if(util.onVisualLineMode)
  40. editor.removeLines();
  41. else
  42. editor.session.remove(range);
  43. util.normalMode(editor);
  44. },
  45. fn: function(editor, range, count, param) {
  46. count = count || 1;
  47. switch (param) {
  48. case "d":
  49. registers._default.text = "";
  50. registers._default.isLine = true;
  51. for (var i = 0; i < count; i++) {
  52. editor.selection.selectLine();
  53. registers._default.text += editor.getCopyText();
  54. var selRange = editor.getSelectionRange();
  55. // check if end of the document was reached
  56. if (!selRange.isMultiLine()) {
  57. var row = selRange.start.row - 1;
  58. var col = editor.session.getLine(row).length
  59. selRange.setStart(row, col);
  60. editor.session.remove(selRange);
  61. editor.selection.clearSelection();
  62. break;
  63. }
  64. editor.session.remove(selRange);
  65. editor.selection.clearSelection();
  66. }
  67. registers._default.text = registers._default.text.replace(/\n$/, "");
  68. break;
  69. default:
  70. if (range) {
  71. editor.selection.setSelectionRange(range);
  72. registers._default.text = editor.getCopyText();
  73. registers._default.isLine = false;
  74. editor.session.remove(range);
  75. editor.selection.clearSelection();
  76. }
  77. }
  78. }
  79. },
  80. "c": {
  81. selFn: function(editor, range, count, param) {
  82. editor.session.remove(range);
  83. util.insertMode(editor);
  84. },
  85. fn: function(editor, range, count, param) {
  86. count = count || 1;
  87. switch (param) {
  88. case "c":
  89. for (var i = 0; i < count; i++) {
  90. editor.removeLines();
  91. util.insertMode(editor);
  92. }
  93. break;
  94. default:
  95. if (range) {
  96. // range.end.column ++;
  97. editor.session.remove(range);
  98. util.insertMode(editor);
  99. }
  100. }
  101. }
  102. },
  103. "y": {
  104. selFn: function(editor, range, count, param) {
  105. registers._default.text = editor.getCopyText();
  106. registers._default.isLine = util.onVisualLineMode;
  107. editor.selection.clearSelection();
  108. util.normalMode(editor);
  109. },
  110. fn: function(editor, range, count, param) {
  111. count = count || 1;
  112. switch (param) {
  113. case "y":
  114. var pos = editor.getCursorPosition();
  115. editor.selection.selectLine();
  116. for (var i = 0; i < count - 1; i++) {
  117. editor.selection.moveCursorDown();
  118. }
  119. registers._default.text = editor.getCopyText().replace(/\n$/, "");
  120. editor.selection.clearSelection();
  121. registers._default.isLine = true;
  122. editor.moveCursorToPosition(pos);
  123. break;
  124. default:
  125. if (range) {
  126. var pos = editor.getCursorPosition();
  127. editor.selection.setSelectionRange(range);
  128. registers._default.text = editor.getCopyText();
  129. registers._default.isLine = false;
  130. editor.selection.clearSelection();
  131. editor.moveCursorTo(pos.row, pos.column);
  132. }
  133. }
  134. }
  135. },
  136. ">": {
  137. selFn: function(editor, range, count, param) {
  138. count = count || 1;
  139. for (var i = 0; i < count; i++) {
  140. editor.indent();
  141. }
  142. util.normalMode(editor);
  143. },
  144. fn: function(editor, range, count, param) {
  145. count = parseInt(count || 1, 10);
  146. switch (param) {
  147. case ">":
  148. var pos = editor.getCursorPosition();
  149. editor.selection.selectLine();
  150. for (var i = 0; i < count - 1; i++) {
  151. editor.selection.moveCursorDown();
  152. }
  153. editor.indent();
  154. editor.selection.clearSelection();
  155. editor.moveCursorToPosition(pos);
  156. editor.navigateLineEnd();
  157. editor.navigateLineStart();
  158. break;
  159. }
  160. }
  161. },
  162. "<": {
  163. selFn: function(editor, range, count, param) {
  164. count = count || 1;
  165. for (var i = 0; i < count; i++) {
  166. editor.blockOutdent();
  167. }
  168. util.normalMode(editor);
  169. },
  170. fn: function(editor, range, count, param) {
  171. count = count || 1;
  172. switch (param) {
  173. case "<":
  174. var pos = editor.getCursorPosition();
  175. editor.selection.selectLine();
  176. for (var i = 0; i < count - 1; i++) {
  177. editor.selection.moveCursorDown();
  178. }
  179. editor.blockOutdent();
  180. editor.selection.clearSelection();
  181. editor.moveCursorToPosition(pos);
  182. editor.navigateLineEnd();
  183. editor.navigateLineStart();
  184. break;
  185. }
  186. }
  187. }
  188. };
  189. });