diff --git a/lisp/emacs-lisp/lisp.el b/lisp/emacs-lisp/lisp.el index 3ff4f64..4e2dd45 100644 --- a/lisp/emacs-lisp/lisp.el +++ b/lisp/emacs-lisp/lisp.el @@ -444,10 +444,32 @@ it marks the next defun after the ones already marked." (beginning-of-defun)) (re-search-backward "^\n" (- (point) 1) t))))) +(defvar narrow-to-defun-include-comments nil + "If non-nil, `narrow-to-defun' will also show comments preceding the defun. + +You should call `narrow-to-defun-including-comments' instead of setting +this value manually.") + +(defun narrow-to-defun-including-comments () + "Make text outside current defun and its preceding comments invisible. +The current defun is the one that contains point or follows point. + +If you wish to always use this interactively instead of `narrow-to-defun', +you should remap its key bindings: + + (global-set-key [remap narrow-to-defun] 'narrow-to-defun-including-comments)" + (interactive) + (let ((narrow-to-defun-include-comments t)) + (narrow-to-defun))) + (defun narrow-to-defun (&optional _arg) "Make text outside current defun invisible. -The defun visible is the one that contains point or follows point. -Optional ARG is ignored." +The current defun is the one that contains point or follows point. + +Optional ARG is ignored. + +To make any comments preceding the defun visible as well, call +`narrow-to-defun-including-comments' instead." (interactive) (save-excursion (widen) @@ -484,6 +506,18 @@ Optional ARG is ignored." (setq end (point)) (beginning-of-defun) (setq beg (point))) + (when narrow-to-defun-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))))