all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Phil Sainty <psainty@orcon.net.nz>
To: 16328@debbugs.gnu.org
Subject: bug#16328: 24.3.50; [PATCH] Enable narrowing to defun with function header comments also visible
Date: Fri, 03 Jan 2014 21:57:22 +1300	[thread overview]
Message-ID: <52C67B72.3010607@orcon.net.nz> (raw)

[-- Attachment #1: Type: text/plain, Size: 1091 bytes --]

For languages where the programmer must resort to function header
comments to describe a function, I've always found it frustrating
that `narrow-to-defun' cuts out this often-critical information.

This patch provides a new function `narrow-to-defun-including-comments'
to keep these comments visible when narrowing.

As there may be multiple per-line comments instead of a single block
comment, I'm skipping back past ALL preceding comments. That seemed
reasonable instead of trying to guess how the author has structured
their comments. Stopping at an empty line would *probably* be okay,
but in the end I figured that potentially showing too much seemed
better than showing too little.

(I've included a check for page breaks within the comments, however,
as I was confident about excluding anything before one of those.)

I didn't think it was wise to encourage users to modify the behaviour
of `narrow-to-defun' itself (I certainly have programmatic uses for
that), so instead I've indicated the way to remap the interactive
bindings for users who wish to use this as standard.


-Phil

[-- Attachment #2: narrow-to-defun-include-comments.patch --]
[-- Type: text/plain, Size: 2183 bytes --]

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))))

             reply	other threads:[~2014-01-03  8:57 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-01-03  8:57 Phil Sainty [this message]
2014-01-03 14:14 ` bug#16328: 24.3.50; [PATCH] Enable narrowing to defun with function header comments also visible Phil Sainty
2014-01-03 16:38 ` Drew Adams
2014-01-04  8:41   ` Josh
2014-07-04  2:04 ` Stefan Monnier

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=52C67B72.3010607@orcon.net.nz \
    --to=psainty@orcon.net.nz \
    --cc=16328@debbugs.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.