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.

110 line
3.8 KiB

5 年之前
  1. ;;; mc-edit-lines.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 file contains functions to add multiple cursors to consecutive lines
  17. ;; given an active region.
  18. ;; Please see multiple-cursors.el for more commentary.
  19. ;;; Code:
  20. (require 'multiple-cursors-core)
  21. (defcustom mc/edit-lines-empty-lines nil
  22. "What should be done by `mc/edit-lines' when a line is not long enough."
  23. :type '(radio (const :tag "Pad the line with spaces." pad)
  24. (const :tag "Ignore the line." ignore)
  25. (const :tag "Signal an error." error)
  26. (const :tag "Nothing. Cursor is at end of line." nil))
  27. :group 'multiple-cursors)
  28. ;;;###autoload
  29. (defun mc/edit-lines (&optional arg)
  30. "Add one cursor to each line of the active region.
  31. Starts from mark and moves in straight down or up towards the
  32. line point is on.
  33. What is done with lines which are not long enough is governed by
  34. `mc/edit-lines-empty-lines'. The prefix argument ARG can be used
  35. to override this. If ARG is a symbol (when called from Lisp),
  36. that symbol is used instead of `mc/edit-lines-empty-lines'.
  37. Otherwise, if ARG negative, short lines will be ignored. Any
  38. other non-nil value will cause short lines to be padded."
  39. (interactive "P")
  40. (when (not (and mark-active (/= (point) (mark))))
  41. (error "Mark a set of lines first"))
  42. (mc/remove-fake-cursors)
  43. (let* ((col (current-column))
  44. (point-line (mc/line-number-at-pos))
  45. (mark-line (progn (exchange-point-and-mark) (mc/line-number-at-pos)))
  46. (direction (if (< point-line mark-line) :up :down))
  47. (style (cond
  48. ;; called from lisp
  49. ((and arg (symbolp arg))
  50. arg)
  51. ;; negative argument
  52. ((< (prefix-numeric-value arg) 0)
  53. 'ignore)
  54. (arg 'pad)
  55. (t mc/edit-lines-empty-lines))))
  56. (deactivate-mark)
  57. (when (and (eq direction :up) (bolp))
  58. (previous-logical-line 1 nil)
  59. (move-to-column col))
  60. ;; Add the cursors
  61. (while (not (eq (mc/line-number-at-pos) point-line))
  62. ;; Pad the line
  63. (when (eq style 'pad)
  64. (while (< (current-column) col)
  65. (insert " ")))
  66. ;; Error
  67. (when (and (eq style 'error)
  68. (not (equal col (current-column))))
  69. (error "Short line encountered in `mc/edit-lines'"))
  70. ;; create the cursor
  71. (unless (and (eq style 'ignore)
  72. (not (equal col (current-column))))
  73. (mc/create-fake-cursor-at-point))
  74. ;; proceed to next
  75. (if (eq direction :up)
  76. (previous-logical-line 1 nil)
  77. (next-logical-line 1 nil))
  78. (move-to-column col))
  79. (multiple-cursors-mode)))
  80. ;;;###autoload
  81. (defun mc/edit-ends-of-lines ()
  82. "Add one cursor to the end of each line in the active region."
  83. (interactive)
  84. (mc/edit-lines)
  85. (mc/execute-command-for-all-cursors 'end-of-line))
  86. ;;;###autoload
  87. (defun mc/edit-beginnings-of-lines ()
  88. "Add one cursor to the beginning of each line in the active region."
  89. (interactive)
  90. (mc/edit-lines)
  91. (mc/execute-command-for-all-cursors 'beginning-of-line))
  92. (provide 'mc-edit-lines)
  93. ;;; mc-edit-lines.el ends here