Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

TorgoX (1933)


"Il est beau comme la retractilité des serres des oiseaux rapaces [...] et surtout, comme la rencontre fortuite sur une table de dissection d'une machine à coudre et d'un parapluie !" -- Lautréamont

Journal of TorgoX (1933)

Tuesday February 07, 2006
05:27 AM

Emacs fun: toggle-narrow

[ #28604 ]
Dear Log,

I've always wanted narrowing in emacs to be something I could just toggle, but there seemed no particularly easy would I could implement that. But the other day I finally sat down and banged this out:

(global-set-key "\M-n" 'toggle-narrow)

(defun toggle-narrow (beg end)
  "If narrow, widen; if not narrowed, narrow!"
  (interactive "r") ; "r" for region
  (if (narrow-p)
    (progn (widen)
      (message "Un-narrowing."))
    (progn (narrow-to-region beg end)
       (message "Narrowing to c%s - c%s." beg end))))

(defun narrow-p ()
  "Whether narrow is in effect for the current buffer"

  (let (real-point-min real-point-max)
    (save-excursion (save-restriction
    (setq real-point-min (point-min)
            real-point-max (point-max))))
     (/= real-point-min (point-min))
     (/= real-point-max (point-max)))))

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.
  • I've seen the command in the manual but have never really used it. It sounds clever, but it's not something I ever felt I whish I could use.

    Can you give a few real-life (as in "actual") scenarios when it's useful to use narrow?


    • I think I typically use narrow for restricting the range of a search-and-replace.

      Example: I'm working on an HTML document and I want to paste in some Perl code or something. I type <pre>, paste the code, and </pre>, and then I realize I need to turn the &'s in the Perl code into &amp;'s. So I select the Perl code that I just pasted in, hit Narrow, use replace-string to change &'s to &amp;, and then hit Widen.