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 (widen) (setq real-point-min (point-min) real-point-max (point-max)))) (or (/= real-point-min (point-min)) (/= real-point-max (point-max)))))
Re:What's it for?
TorgoX on 2006-02-07T13:47:37
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 &'s. So I select the Perl code that I just pasted in, hit Narrow, use replace-string to change &'s to &, and then hit Widen.
Re:What's it for?
Fletch on 2006-02-07T16:56:55
Yup, that's the most common use I have for it as well. In fact if you're feeling really inspired a replace-regexp-narrowed (or maybe replace-regexp-region) that did just that (narrowed, replace-regexp, widened) could be useful.Re:What's it for?
vjo on 2006-02-09T16:15:42
Can't you use C-x n n and C-x n w for this?Re:What's it for?
TorgoX on 2006-02-09T23:27:34
Yes.