unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Nicolas Richard <youngfrog@members.fsf.org>
To: Ed Avis <eda@waniasset.com>
Cc: 20723@debbugs.gnu.org
Subject: bug#20723: 24.4; narrow-to-line
Date: Tue, 02 Jun 2015 20:32:44 +0200	[thread overview]
Message-ID: <87bngyvu43.fsf@members.fsf.org> (raw)
In-Reply-To: <loom.20150602T185507-311@post.gmane.org> (Ed Avis's message of "Tue, 2 Jun 2015 16:55:27 +0000 (UTC)")

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





      parent reply	other threads:[~2015-06-02 18:32 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-06-02 16:55 bug#20723: 24.4; narrow-to-line Ed Avis
2015-06-02 18:14 ` Drew Adams
2015-06-02 18:22   ` Ed Avis
2015-06-02 19:35     ` Drew Adams
2019-11-06  2:05   ` Stefan Kangas
2019-11-06 14:06     ` Drew Adams
2019-11-06 14:14       ` Stefan Kangas
2022-01-29 17:05     ` Lars Ingebrigtsen
2015-06-02 18:32 ` Nicolas Richard [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87bngyvu43.fsf@members.fsf.org \
    --to=youngfrog@members.fsf.org \
    --cc=20723@debbugs.gnu.org \
    --cc=eda@waniasset.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).