Function: narrow-to-defun
narrow-to-defun is an interactive and byte-compiled function defined
in lisp.el.gz.
Signature
(narrow-to-defun &optional INCLUDE-COMMENTS)
Documentation
Make text outside current defun invisible.
The current defun is the one that contains point or follows point.
Preceding comments are included if INCLUDE-COMMENTS is non-nil.
Interactively, the behavior depends on narrow-to-defun-include-comments.
Probably introduced at or before Emacs version 20.1.
Key Bindings
Source Code
;; Defined in /usr/src/emacs/lisp/emacs-lisp/lisp.el.gz
(defun narrow-to-defun (&optional include-comments)
"Make text outside current defun invisible.
The current defun is the one that contains point or follows point.
Preceding comments are included if INCLUDE-COMMENTS is non-nil.
Interactively, the behavior depends on `narrow-to-defun-include-comments'."
(interactive (list narrow-to-defun-include-comments))
(save-excursion
(widen)
(let ((opoint (point))
beg end)
;; Try first in this order for the sake of languages with nested
;; functions where several can end at the same place as with
;; the offside rule, e.g. Python.
;; Finding the start of the function is a bit problematic since
;; `beginning-of-defun' when we are on the first character of
;; the function might go to the previous function.
;;
;; Therefore we first move one character forward and then call
;; `beginning-of-defun'. However now we must check that we did
;; not move into the next function.
(let ((here (point)))
(unless (eolp)
(forward-char))
(beginning-of-defun)
(when (< (point) here)
(goto-char here)
(beginning-of-defun)))
(setq beg (point))
(end-of-defun)
(setq end (point))
(while (looking-at "^\n")
(forward-line 1))
(unless (> (point) opoint)
;; beginning-of-defun moved back one defun
;; so we got the wrong one.
(goto-char opoint)
(end-of-defun)
(setq end (point))
(beginning-of-defun)
(setq beg (point)))
(when include-comments
(goto-char beg)
;; Move back past all preceding comments (and whitespace).
(when (forward-comment -1)
(while (forward-comment -1))
;; Move forwards past any page breaks within these comments.
(when (and page-delimiter (not (string= page-delimiter "")))
(while (re-search-forward page-delimiter beg t)))
;; Lastly, move past any empty lines.
(skip-chars-forward "[:space:]\n")
(beginning-of-line)
(setq beg (point))))
(goto-char end)
(re-search-backward "^\n" (- (point) 1) t)
(narrow-to-region beg end))))