Simple image host.
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.

rectangular-region-mode.el 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. ;;; rectangular-region-mode.el
  2. ;; Copyright (C) 2012 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 (line-number-at-pos))
  60. (anchor-column (save-excursion (goto-char rrm/anchor) (current-column)))
  61. (anchor-line (save-excursion (goto-char rrm/anchor) (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 (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. (define-minor-mode rectangular-region-mode
  89. "A mode for creating a rectangular region to edit"
  90. nil " rr" rectangular-region-mode-map
  91. (if rectangular-region-mode
  92. (progn
  93. (add-hook 'after-change-functions 'rrm/switch-to-multiple-cursors t t)
  94. (add-hook 'post-command-hook 'rrm/repaint t t))
  95. (remove-hook 'after-change-functions 'rrm/switch-to-multiple-cursors t)
  96. (remove-hook 'post-command-hook 'rrm/repaint t)
  97. (set-marker rrm/anchor nil)))
  98. (provide 'rectangular-region-mode)
  99. ;;; rectangular-region-mode.el ends here