Last active
September 17, 2024 08:56
-
-
Save xenodium/9273084bbea3f5055ace1c90df3ef9f5 to your computer and use it in GitHub Desktop.
Expand context region interactively
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defun expand-context-region () | |
(interactive) | |
(let* ((buffer (current-buffer)) | |
(saved-location (point)) | |
(origin (save-excursion | |
(forward-line 0) | |
(point)))) | |
(goto-char (line-beginning-position)) | |
(set-mark (save-excursion | |
(forward-line 1) | |
(point))) | |
(activate-mark) | |
(minibuffer-with-setup-hook | |
(lambda () (add-hook | |
'after-change-functions | |
(lambda (_beg _end _len) | |
(when-let* ((content (minibuffer-contents)) | |
(context-line-count (string-to-number content))) | |
(with-current-buffer buffer | |
(with-selected-window (get-buffer-window buffer) | |
(deactivate-mark) | |
(goto-char origin) | |
(goto-char (save-excursion | |
(goto-char origin) | |
(forward-line (- context-line-count)) | |
(point))) | |
(set-mark (save-excursion | |
(goto-char origin) | |
(forward-line (1+ context-line-count)) | |
(point))) | |
(activate-mark)))))nil t)) | |
(condition-case nil | |
(read-number "Select context lines: " 1) | |
(quit | |
(deactivate-mark) | |
(goto-char saved-location)))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment