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)))))
What's it for? (Score:1)
Can you give a few real-life (as in "actual") scenarios when it's useful to use narrow?
Re:What's it for? (Score:2)
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? (Score:2)
Re:What's it for? (Score:1)
Re:What's it for? (Score:2)