Klimi's new dotfiles with stow.
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

107 рядки
3.9 KiB

4 роки тому
  1. ;;; mc-hide-unmatched-lines.el
  2. ;; Copyright (C) 2014 Aleksey Fedotov
  3. ;; Author: Aleksey Fedotov <lexa@cfotr.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 minor mode when enabled hides all lines where no cursors (and
  17. ;; also hum/lines-to-expand below and above) To make use of this mode
  18. ;; press "C-'" while multiple-cursor-mode is active. You can still
  19. ;; edit lines while you are in mc-hide-unmatched-lines mode. To leave
  20. ;; this mode press "<return>" or "C-g"
  21. ;;
  22. ;;; Code:
  23. (require 'multiple-cursors-core)
  24. (require 'mc-mark-more)
  25. (defvar hum/hide-unmatched-lines-mode-map (make-sparse-keymap)
  26. "Keymap for hide unmatched lines is mainly for rebinding C-g")
  27. (define-key hum/hide-unmatched-lines-mode-map (kbd "C-g") 'hum/keyboard-quit)
  28. (define-key hum/hide-unmatched-lines-mode-map (kbd "<return>") 'hum/keyboard-quit)
  29. (defun hum/keyboard-quit ()
  30. "Leave hide-unmatched-lines mode"
  31. (interactive)
  32. (mc-hide-unmatched-lines-mode 0))
  33. ;; used only in in multiple-cursors-mode-disabled-hook
  34. (defun hum/disable-hum-mode ()
  35. (mc-hide-unmatched-lines-mode 0))
  36. ;;;###autoload
  37. (define-minor-mode mc-hide-unmatched-lines-mode
  38. "Minor mode when enabled hides all lines where no cursors (and
  39. also hum/lines-to-expand below and above) To make use of this
  40. mode press \"C-'\" while multiple-cursor-mode is active. You can
  41. still edit lines while you are in mc-hide-unmatched-lines
  42. mode. To leave this mode press <return> or \"C-g\""
  43. nil " hu"
  44. hum/hide-unmatched-lines-mode-map
  45. (if mc-hide-unmatched-lines-mode
  46. ;;just in case if mc mode will be disabled while hide-unmatched-lines is active
  47. (progn
  48. (hum/hide-unmatched-lines)
  49. (add-hook 'multiple-cursors-mode-disabled-hook 'hum/disable-hum-mode t t))
  50. (progn
  51. (hum/unhide-unmatched-lines)
  52. (remove-hook 'multiple-cursors-mode-disabled-hook 'hum/disable-hum-mode))))
  53. (defconst hum/invisible-overlay-name 'hum/invisible-overlay-name)
  54. (defcustom hum/lines-to-expand 2
  55. "How many lines below and above cursor to show"
  56. :type '(integer)
  57. :group 'multiple-cursors)
  58. (defcustom hum/placeholder "..."
  59. "Placeholder which will be placed insted of hiden text"
  60. :type '(string)
  61. :group 'multiple-cursors)
  62. (defun hum/add-invisible-overlay (begin end)
  63. (let ((overlay (make-overlay begin
  64. end
  65. (current-buffer)
  66. t
  67. nil
  68. )))
  69. (overlay-put overlay hum/invisible-overlay-name t)
  70. (overlay-put overlay 'invisible t)
  71. (overlay-put overlay 'intangible t)
  72. (overlay-put overlay 'evaporate t)
  73. (overlay-put overlay 'after-string hum/placeholder)))
  74. (defun hum/hide-unmatched-lines ()
  75. (let ((begin (point-min)))
  76. (mc/for-each-cursor-ordered
  77. (save-excursion
  78. (goto-char (mc/cursor-beg cursor))
  79. (if (< begin (line-beginning-position (- hum/lines-to-expand)))
  80. (hum/add-invisible-overlay begin (line-end-position (- hum/lines-to-expand))))
  81. (setq begin (line-beginning-position (+ 2 hum/lines-to-expand)))))
  82. (hum/add-invisible-overlay begin (point-max))))
  83. (defun hum/unhide-unmatched-lines ()
  84. (remove-overlays nil nil hum/invisible-overlay-name t))
  85. (provide 'mc-hide-unmatched-lines-mode)
  86. (define-key mc/keymap (kbd "C-'") 'mc-hide-unmatched-lines-mode)