mc-hide-unmatched-lines-mode.el (4003B)
1 ;;; mc-hide-unmatched-lines.el 2 3 ;; Copyright (C) 2014 Aleksey Fedotov 4 5 ;; Author: Aleksey Fedotov <lexa@cfotr.com> 6 ;; Keywords: editing cursors 7 8 ;; This program is free software; you can redistribute it and/or modify 9 ;; it under the terms of the GNU General Public License as published by 10 ;; the Free Software Foundation, either version 3 of the License, or 11 ;; (at your option) any later version. 12 13 ;; This program is distributed in the hope that it will be useful, 14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 ;; GNU General Public License for more details. 17 18 ;; You should have received a copy of the GNU General Public License 19 ;; along with this program. If not, see <http://www.gnu.org/licenses/>. 20 21 ;;; Commentary: 22 23 ;; This minor mode when enabled hides all lines where no cursors (and 24 ;; also hum/lines-to-expand below and above) To make use of this mode 25 ;; press "C-'" while multiple-cursor-mode is active. You can still 26 ;; edit lines while you are in mc-hide-unmatched-lines mode. To leave 27 ;; this mode press "<return>" or "C-g" 28 ;; 29 30 ;;; Code: 31 32 (require 'multiple-cursors-core) 33 (require 'mc-mark-more) 34 35 (defvar hum/hide-unmatched-lines-mode-map (make-sparse-keymap) 36 "Keymap for hide unmatched lines is mainly for rebinding C-g") 37 38 (define-key hum/hide-unmatched-lines-mode-map (kbd "C-g") 'hum/keyboard-quit) 39 (define-key hum/hide-unmatched-lines-mode-map (kbd "<return>") 'hum/keyboard-quit) 40 41 (defun hum/keyboard-quit () 42 "Leave hide-unmatched-lines mode" 43 (interactive) 44 (mc-hide-unmatched-lines-mode 0)) 45 46 ;; used only in in multiple-cursors-mode-disabled-hook 47 (defun hum/disable-hum-mode () 48 (mc-hide-unmatched-lines-mode 0)) 49 50 ;;;###autoload 51 (define-minor-mode mc-hide-unmatched-lines-mode 52 "Minor mode when enabled hides all lines where no cursors (and 53 also hum/lines-to-expand below and above) To make use of this 54 mode press \"C-'\" while multiple-cursor-mode is active. You can 55 still edit lines while you are in mc-hide-unmatched-lines 56 mode. To leave this mode press <return> or \"C-g\"" 57 nil " hu" 58 hum/hide-unmatched-lines-mode-map 59 (if mc-hide-unmatched-lines-mode 60 ;;just in case if mc mode will be disabled while hide-unmatched-lines is active 61 (progn 62 (hum/hide-unmatched-lines) 63 (add-hook 'multiple-cursors-mode-disabled-hook 'hum/disable-hum-mode t t)) 64 (progn 65 (hum/unhide-unmatched-lines) 66 (remove-hook 'multiple-cursors-mode-disabled-hook 'hum/disable-hum-mode)))) 67 68 (defconst hum/invisible-overlay-name 'hum/invisible-overlay-name) 69 70 (defcustom hum/lines-to-expand 2 71 "How many lines below and above cursor to show" 72 :type '(integer) 73 :group 'multiple-cursors) 74 75 (defcustom hum/placeholder "..." 76 "Placeholder which will be placed insted of hiden text" 77 :type '(string) 78 :group 'multiple-cursors) 79 80 (defun hum/add-invisible-overlay (begin end) 81 (let ((overlay (make-overlay begin 82 end 83 (current-buffer) 84 t 85 nil 86 ))) 87 (overlay-put overlay hum/invisible-overlay-name t) 88 (overlay-put overlay 'invisible t) 89 (overlay-put overlay 'intangible t) 90 (overlay-put overlay 'evaporate t) 91 (overlay-put overlay 'after-string hum/placeholder))) 92 93 (defun hum/hide-unmatched-lines () 94 (let ((begin (point-min))) 95 (mc/for-each-cursor-ordered 96 (save-excursion 97 (goto-char (mc/cursor-beg cursor)) 98 (if (< begin (line-beginning-position (- hum/lines-to-expand))) 99 (hum/add-invisible-overlay begin (line-end-position (- hum/lines-to-expand)))) 100 (setq begin (line-beginning-position (+ 2 hum/lines-to-expand))))) 101 (hum/add-invisible-overlay begin (point-max)))) 102 103 (defun hum/unhide-unmatched-lines () 104 (remove-overlays nil nil hum/invisible-overlay-name t)) 105 106 (provide 'mc-hide-unmatched-lines-mode) 107 (define-key mc/keymap (kbd "C-'") 'mc-hide-unmatched-lines-mode)