Klimi's new dotfiles with stow.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
4.1 KiB

5 years ago
  1. ;;; mc-cycle-cursors.el
  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 scrolls the buffer to center each cursor in turn.
  17. ;; Scroll down with C-v, scroll up with M-v
  18. ;; This is nice when you have cursors that's outside of your view.
  19. ;;; Code:
  20. (require 'multiple-cursors-core)
  21. (defun mc/next-fake-cursor-after-point ()
  22. (let ((pos (point))
  23. (next-pos (1+ (point-max)))
  24. next)
  25. (mc/for-each-fake-cursor
  26. (let ((cursor-pos (overlay-get cursor 'point)))
  27. (when (and (< pos cursor-pos)
  28. (< cursor-pos next-pos))
  29. (setq next-pos cursor-pos)
  30. (setq next cursor))))
  31. next))
  32. (defun mc/prev-fake-cursor-before-point ()
  33. (let ((pos (point))
  34. (prev-pos (1- (point-min)))
  35. prev)
  36. (mc/for-each-fake-cursor
  37. (let ((cursor-pos (overlay-get cursor 'point)))
  38. (when (and (> pos cursor-pos)
  39. (> cursor-pos prev-pos))
  40. (setq prev-pos cursor-pos)
  41. (setq prev cursor))))
  42. prev))
  43. (defcustom mc/cycle-looping-behaviour 'continue
  44. "What to do if asked to cycle beyond the last cursor or before the first cursor."
  45. :type '(radio (const :tag "Loop around to beginning/end of document." continue)
  46. (const :tag "Warn and then loop around." warn)
  47. (const :tag "Signal an error." error)
  48. (const :tag "Don't loop." stop))
  49. :group 'multiple-cursors)
  50. (defun mc/handle-loop-condition (error-message)
  51. (cl-ecase mc/cycle-looping-behaviour
  52. (error (error error-message))
  53. (warn (message error-message))
  54. (continue 'continue)
  55. (stop 'stop)))
  56. (defun mc/first-fake-cursor-after (point)
  57. "Very similar to mc/furthest-cursor-before-point, but ignores (mark) and (point)."
  58. (let* ((cursors (mc/all-fake-cursors))
  59. (cursors-after-point (cl-remove-if (lambda (cursor)
  60. (< (mc/cursor-beg cursor) point))
  61. cursors))
  62. (cursors-in-order (cl-sort cursors-after-point '< :key 'mc/cursor-beg)))
  63. (car cursors-in-order)))
  64. (defun mc/last-fake-cursor-before (point)
  65. "Very similar to mc/furthest-cursor-before-point, but ignores (mark) and (point)."
  66. (let* ((cursors (mc/all-fake-cursors))
  67. (cursors-before-point (cl-remove-if (lambda (cursor)
  68. (> (mc/cursor-end cursor) point))
  69. cursors))
  70. (cursors-in-order (cl-sort cursors-before-point '> :key 'mc/cursor-end)))
  71. (car cursors-in-order)))
  72. (cl-defun mc/cycle (next-cursor fallback-cursor loop-message)
  73. (when (null next-cursor)
  74. (when (eql 'stop (mc/handle-loop-condition loop-message))
  75. (return-from mc/cycle nil))
  76. (setf next-cursor fallback-cursor))
  77. (mc/create-fake-cursor-at-point)
  78. (mc/pop-state-from-overlay next-cursor)
  79. (recenter))
  80. (defun mc/cycle-forward ()
  81. (interactive)
  82. (mc/cycle (mc/next-fake-cursor-after-point)
  83. (mc/first-fake-cursor-after (point-min))
  84. "We're already at the last cursor."))
  85. (defun mc/cycle-backward ()
  86. (interactive)
  87. (mc/cycle (mc/prev-fake-cursor-before-point)
  88. (mc/last-fake-cursor-before (point-max))
  89. "We're already at the last cursor"))
  90. (define-key mc/keymap (kbd "C-v") 'mc/cycle-forward)
  91. (define-key mc/keymap (kbd "M-v") 'mc/cycle-backward)
  92. (provide 'mc-cycle-cursors)
  93. ;; Local Variables:
  94. ;; coding: utf-8
  95. ;; End:
  96. ;;; mc-cycle-cursors.el ends here