Klimi's new dotfiles with stow.
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

125 行
4.6 KiB

  1. ;;; rectangular-region-mode.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. ;; (global-set-key (kbd "H-SPC") 'set-rectangular-region-anchor)
  17. ;; Think of this one as `set-mark` except you're marking a rectangular region. It is
  18. ;; an exceedingly quick way of adding multiple cursors to multiple lines.
  19. ;;; Code:
  20. (require 'multiple-cursors-core)
  21. (defvar rrm/anchor (make-marker)
  22. "The position in the buffer that anchors the rectangular region.")
  23. (defvar rectangular-region-mode-map (make-sparse-keymap)
  24. "Keymap for rectangular region is mainly for rebinding C-g")
  25. (define-key rectangular-region-mode-map (kbd "C-g") 'rrm/keyboard-quit)
  26. (define-key rectangular-region-mode-map (kbd "<return>") 'rrm/switch-to-multiple-cursors)
  27. (defvar rectangular-region-mode nil)
  28. (defun rrm/keyboard-quit ()
  29. "Exit rectangular-region-mode."
  30. (interactive)
  31. (rectangular-region-mode 0)
  32. (rrm/remove-rectangular-region-overlays)
  33. (deactivate-mark))
  34. ;; Bind this to a key (for instance H-SPC) to start rectangular-region-mode
  35. ;;;###autoload
  36. (defun set-rectangular-region-anchor ()
  37. "Anchors the rectangular region at point.
  38. Think of this one as `set-mark' except you're marking a rectangular region. It is
  39. an exceedingly quick way of adding multiple cursors to multiple lines."
  40. (interactive)
  41. (set-marker rrm/anchor (point))
  42. (push-mark (point))
  43. (rectangular-region-mode 1))
  44. (defun rrm/remove-rectangular-region-overlays ()
  45. "Remove all rectangular-region overlays."
  46. (mc/remove-fake-cursors)
  47. (mapc #'(lambda (o)
  48. (when (eq (overlay-get o 'type) 'additional-region)
  49. (delete-overlay o)))
  50. (overlays-in (point-min) (point-max))))
  51. (defun rrm/repaint ()
  52. "Start from the anchor and draw a rectangle between it and point."
  53. (if (not rectangular-region-mode)
  54. (remove-hook 'post-command-hook 'rrm/repaint t)
  55. ;; else
  56. (rrm/remove-rectangular-region-overlays)
  57. (let* ((annoying-arrows-mode nil)
  58. (point-column (current-column))
  59. (point-line (mc/line-number-at-pos))
  60. (anchor-column (save-excursion (goto-char rrm/anchor) (current-column)))
  61. (anchor-line (save-excursion (goto-char rrm/anchor) (mc/line-number-at-pos)))
  62. (left-column (if (< point-column anchor-column) point-column anchor-column))
  63. (right-column (if (> point-column anchor-column) point-column anchor-column))
  64. (navigation-step (if (< point-line anchor-line) 1 -1)))
  65. (move-to-column anchor-column)
  66. (set-mark (point))
  67. (move-to-column point-column)
  68. (mc/save-excursion
  69. (while (not (= anchor-line (mc/line-number-at-pos)))
  70. (forward-line navigation-step)
  71. (move-to-column anchor-column)
  72. (when (= anchor-column (current-column))
  73. (set-mark (point))
  74. (move-to-column point-column)
  75. (when (= point-column (current-column))
  76. (mc/create-fake-cursor-at-point))))))))
  77. (defun rrm/switch-to-multiple-cursors (&rest forms)
  78. "Switch from rectangular-region-mode to multiple-cursors-mode."
  79. (interactive)
  80. (rectangular-region-mode 0)
  81. (multiple-cursors-mode 1))
  82. (defadvice er/expand-region (before switch-from-rrm-to-mc activate)
  83. (when rectangular-region-mode
  84. (rrm/switch-to-multiple-cursors)))
  85. (defadvice kill-ring-save (before switch-from-rrm-to-mc activate)
  86. (when rectangular-region-mode
  87. (rrm/switch-to-multiple-cursors)))
  88. ;;;###autoload
  89. (define-minor-mode rectangular-region-mode
  90. "A mode for creating a rectangular region to edit"
  91. nil " rr" rectangular-region-mode-map
  92. (if rectangular-region-mode
  93. (progn
  94. (add-hook 'after-change-functions 'rrm/switch-to-multiple-cursors t t)
  95. (add-hook 'post-command-hook 'rrm/repaint t t))
  96. (remove-hook 'after-change-functions 'rrm/switch-to-multiple-cursors t)
  97. (remove-hook 'post-command-hook 'rrm/repaint t)
  98. (set-marker rrm/anchor nil)))
  99. (provide 'rectangular-region-mode)
  100. ;;; rectangular-region-mode.el ends here