text.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. interactive("insert-double-quotes",
  2. "Insert (or surround the currently selected text with) a pair of double quotes.",
  3. function (I) {
  4. call_on_focused_field(I, function (field) {
  5. modify_region(field,
  6. function (str) {
  7. return ["\""+str+"\"", (str ? str.length+2 : 1)];
  8. });
  9. }, true);
  10. });
  11. ////////////////////////////////////////////////////////////////
  12. /// Cutting, copying
  13. function cut_to_beg_of_line (field, window) {
  14. field.selectionStart = 0;
  15. call_builtin_command(window, 'cmd_cut');
  16. }
  17. interactive("cut-to-beg-of-line",
  18. null,
  19. function (I) {
  20. call_on_focused_field(I, function (field) {
  21. cut_to_beg_of_line(field, I.window);
  22. });
  23. });
  24. function cut_whole_line (field, window) {
  25. field.selectionStart = 0;
  26. var eol = field.value.indexOf("\n");
  27. if (eol == -1)
  28. field.selectionEnd = field.textLength;
  29. else
  30. field.selectionEnd = eol;
  31. call_builtin_command(window, 'cmd_cut');
  32. }
  33. interactive("cut-whole-line",
  34. null,
  35. function (I) {
  36. call_on_focused_field(I, function (field) {
  37. cut_whole_line(field, I.window);
  38. });
  39. });
  40. function copy_whole_line (field, window) {
  41. var st = field.selectionStart;
  42. var en = field.selectionEnd;
  43. field.selectionStart = 0;
  44. var eol = field.value.indexOf("\n");
  45. if (eol == -1)
  46. field.selectionEnd = field.textLength;
  47. else
  48. field.selectionEnd = eol;
  49. call_builtin_command(window, 'cmd_copy');
  50. field.selectionStart = st;
  51. field.selectionEnd = en;
  52. }
  53. interactive("copy-whole-line",
  54. null,
  55. function (I) {
  56. call_on_focused_field(I, function (field) {
  57. copy_whole_line(field, I.window);
  58. });
  59. });
  60. ////////////////////////////////////////////////////////////////
  61. /// Changing the case of the previous word
  62. function modify_word_backward (field, func) {
  63. var point = field.selectionStart;
  64. var str = field.value;
  65. var beg_str = str.substring(0, point);
  66. var beg;
  67. var end = point;
  68. // Skip spaces backward and find the bounds of the word.
  69. for (var i = beg_str.length-1; i >= 0; i--) {
  70. if (" ".indexOf(beg_str.charAt(i)) == -1) {
  71. end = i;
  72. break;
  73. }
  74. }
  75. beg = str.lastIndexOf(" ", end) + 1;
  76. end = str.indexOf(" ", beg);
  77. if (end == -1)
  78. end = str.length;
  79. // Change the value of the text field.
  80. field.value =
  81. str.substring(0, beg) +
  82. func(str.substring(beg, end)) +
  83. str.substring(end);
  84. // Move point.
  85. field.selectionStart = point;
  86. field.selectionEnd = point;
  87. }
  88. interactive("downcase-word-backward",
  89. "Downcase the word backward.",
  90. function (I) {
  91. call_on_focused_field(I, function (field) {
  92. modify_word_backward(field, function (word) {
  93. return word.toLocaleLowerCase();
  94. });
  95. });
  96. });
  97. interactive("upcase-word-backward",
  98. "Upcase the word backward.",
  99. function (I) {
  100. call_on_focused_field(I, function (field) {
  101. modify_word_backward(field, function (word) {
  102. return word.toLocaleUpperCase();
  103. });
  104. });
  105. });
  106. interactive("capitalize-word-backward",
  107. "Capitalize the word backward.",
  108. function (I) {
  109. call_on_focused_field(I, function (field) {
  110. modify_word_backward(field, function (word) {
  111. if (word.length > 0)
  112. return word[0].toLocaleUpperCase() +
  113. word.substring(1).toLocaleLowerCase();
  114. return word;
  115. });
  116. });
  117. });
  118. // overriden from "modules/content-buffer-input.js"
  119. interactive("capitalize-word",
  120. "Capitalize the following word (or arg words), moving over.",
  121. function (I) {
  122. call_on_focused_field(I, function (field) {
  123. modify_word_at_point(field, function (word) {
  124. if (word.length > 0)
  125. return word[0].toLocaleUpperCase() +
  126. word.substring(1).toLocaleLowerCase();
  127. return word;
  128. });
  129. });
  130. });