Klimi's new dotfiles with stow.
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

150 rindas
5.0 KiB

pirms 4 gadiem
  1. ;;; mc-separate-operations.el - functions that work differently on each cursor
  2. ;; Copyright (C) 2012-2016 Magnar Sveen
  3. ;; Author: Magnar Sveen <magnars@gmail.com>
  4. ;; Keywords: editing cursors
  5. ;; This program is free software; you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; This program is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;; This file contains functions that work differently on each cursor,
  17. ;; instead of treating all of them the same.
  18. ;; Please see multiple-cursors.el for more commentary.
  19. ;;; Code:
  20. (require 'multiple-cursors-core)
  21. (defcustom mc/insert-numbers-default 0
  22. "The default number at which to start counting for
  23. `mc/insert-numbers'"
  24. :type 'integer
  25. :group 'multiple-cursors)
  26. (defvar mc--insert-numbers-number 0)
  27. ;;;###autoload
  28. (defun mc/insert-numbers (arg)
  29. "Insert increasing numbers for each cursor, starting at
  30. `mc/insert-numbers-default' or ARG."
  31. (interactive "P")
  32. (setq mc--insert-numbers-number (or (and arg (prefix-numeric-value arg))
  33. mc/insert-numbers-default))
  34. (mc/for-each-cursor-ordered
  35. (mc/execute-command-for-fake-cursor 'mc--insert-number-and-increase cursor)))
  36. (defun mc--insert-number-and-increase ()
  37. (interactive)
  38. (insert (number-to-string mc--insert-numbers-number))
  39. (setq mc--insert-numbers-number (1+ mc--insert-numbers-number)))
  40. (defun mc--ordered-region-strings ()
  41. (let (strings)
  42. (save-excursion
  43. (mc/for-each-cursor-ordered
  44. (setq strings (cons (buffer-substring-no-properties
  45. (mc/cursor-beg cursor)
  46. (mc/cursor-end cursor)) strings))))
  47. (nreverse strings)))
  48. (defvar mc--insert-letters-number 0)
  49. ;;;###autoload
  50. (defun mc/insert-letters (arg)
  51. "Insert increasing letters for each cursor, starting at 0 or ARG.
  52. Where letter[0]=a letter[2]=c letter[26]=aa"
  53. (interactive "P")
  54. (setq mc--insert-letters-number (or (and arg (prefix-numeric-value arg))
  55. 0))
  56. (mc/for-each-cursor-ordered
  57. (mc/execute-command-for-fake-cursor 'mc--insert-letter-and-increase cursor)))
  58. (defun mc--number-to-letters (number)
  59. (let ((letter
  60. (char-to-string
  61. (+ (mod number 26) ?a)))
  62. (number2 (/ number 26)))
  63. (if (> number2 0)
  64. (concat (mc--number-to-letters (- number2 1)) letter)
  65. letter)))
  66. (defun mc--insert-letter-and-increase ()
  67. (interactive)
  68. (insert (mc--number-to-letters mc--insert-letters-number))
  69. (setq mc--insert-letters-number (1+ mc--insert-letters-number)))
  70. (defvar mc--strings-to-replace nil)
  71. (defun mc--replace-region-strings-1 ()
  72. (interactive)
  73. (delete-region (region-beginning) (region-end))
  74. (save-excursion (insert (car mc--strings-to-replace)))
  75. (setq mc--strings-to-replace (cdr mc--strings-to-replace)))
  76. (defun mc--replace-region-strings ()
  77. (mc/for-each-cursor-ordered
  78. (mc/execute-command-for-fake-cursor 'mc--replace-region-strings-1 cursor)))
  79. ;;;###autoload
  80. (defun mc/reverse-regions ()
  81. (interactive)
  82. (if (not multiple-cursors-mode)
  83. (progn
  84. (mc/mark-next-lines 1)
  85. (mc/reverse-regions)
  86. (multiple-cursors-mode 0))
  87. (unless (use-region-p)
  88. (mc/execute-command-for-all-cursors 'mark-sexp))
  89. (setq mc--strings-to-replace (nreverse (mc--ordered-region-strings)))
  90. (mc--replace-region-strings)))
  91. ;;;###autoload
  92. (defun mc/sort-regions ()
  93. (interactive)
  94. (unless (use-region-p)
  95. (mc/execute-command-for-all-cursors 'mark-sexp))
  96. (setq mc--strings-to-replace (sort (mc--ordered-region-strings) 'string<))
  97. (mc--replace-region-strings))
  98. ;;;###autoload
  99. (defun mc/vertical-align (character)
  100. "Aligns all cursors vertically with a given CHARACTER to the one with the
  101. highest colum number (the rightest).
  102. Might not behave as intended if more than one cursors are on the same line."
  103. (interactive "c")
  104. (let ((rightest-column (current-column)))
  105. (mc/execute-command-for-all-cursors
  106. (lambda () "get the rightest cursor"
  107. (interactive)
  108. (setq rightest-column (max (current-column) rightest-column))
  109. ))
  110. (mc/execute-command-for-all-cursors
  111. (lambda ()
  112. (interactive)
  113. (let ((missing-spaces (- rightest-column (current-column))))
  114. (save-excursion (insert (make-string missing-spaces character)))
  115. (forward-char missing-spaces))))))
  116. ;;;###autoload
  117. (defun mc/vertical-align-with-space ()
  118. "Aligns all cursors with whitespace like `mc/vertical-align' does"
  119. (interactive)
  120. (mc/vertical-align 32))
  121. (provide 'mc-separate-operations)
  122. ;;; mc-separate-operations.el ends here