bug-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#20723: 24.4; narrow-to-line


From: Nicolas Richard
Subject: bug#20723: 24.4; narrow-to-line
Date: Tue, 02 Jun 2015 20:32:44 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/25.0.50 (gnu/linux)

Ed Avis <eda@waniasset.com> writes:
> It would be handy to have M-x narrow-to-line to narrow the buffer to the
> line point is currently on.

I'll give my own experience : I wanted a similar command and first wrote
a naive command along the lines of :
  (narrow-to-region (point-at-bol)
                    (save-excursion
                      (forward-line 1)
                      (point)))
but then, when widening the view, the window-start is usually modified
and I found this annoying. What I now have in my .emacs is what follows.
It keeps track of window-start and resets it when widening. Not very
clean code, but it worked good enough for me until now.

(defvar-local yf/narrow-to-line--state nil)
(defun yf/narrow-to-line ()
  (interactive)
  (setq yf/narrow-to-line--state
        (list (selected-window) (window-start)))
  (narrow-to-region (point-at-bol)
                    (save-excursion
                      (forward-line 1)
                      (point)))
  (add-hook 'post-command-hook #'yf/unnarrow-to-line nil t))
(defun yf/unnarrow-to-line ()
  (when (and yf/narrow-to-line--state
             (not (buffer-narrowed-p)))
    (apply #'set-window-start yf/narrow-to-line--state)
    (setq yf/narrow-to-line--state nil)
    (remove-hook 'post-command-hook #'yf/unnarrow-to-line t)))
(bind-key "l" 'yf/narrow-to-line narrow-map)

FWIW, totally unrelated, but I also have the following :
(defun yf/narrow-to-window-view (printmsg)
  (interactive "p")
  (narrow-to-region (window-start)
                    (save-excursion
                      (goto-char (window-end nil t))
                      (when (not (pos-visible-in-window-p))
                        ;; Line is not fully visible.
                        (forward-visible-line -1))
                      (point)))
  (when printmsg
    (message "Narrowed to visible portion of buffer in current
    window.")))
(bind-key "v" 'yf/narrow-to-window-view narrow-map)

-- 
Nico





reply via email to

[Prev in Thread] Current Thread [Next in Thread]