From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Phil Sainty Newsgroups: gmane.emacs.bugs 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 Message-ID: <52C67B72.3010607@orcon.net.nz> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="------------040307040501030803030506" X-Trace: ger.gmane.org 1388739566 17204 80.91.229.3 (3 Jan 2014 08:59:26 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Fri, 3 Jan 2014 08:59:26 +0000 (UTC) To: 16328@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Fri Jan 03 09:59:32 2014 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1Vz0bI-0007T6-Uv for geb-bug-gnu-emacs@m.gmane.org; Fri, 03 Jan 2014 09:59:29 +0100 Original-Received: from localhost ([::1]:48675 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Vz0bI-000400-Lz for geb-bug-gnu-emacs@m.gmane.org; Fri, 03 Jan 2014 03:59:28 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:37409) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Vz0b5-0003nD-Jr for bug-gnu-emacs@gnu.org; Fri, 03 Jan 2014 03:59:25 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Vz0as-00084g-A5 for bug-gnu-emacs@gnu.org; Fri, 03 Jan 2014 03:59:15 -0500 Original-Received: from debbugs.gnu.org ([140.186.70.43]:45200) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Vz0as-00084c-73 for bug-gnu-emacs@gnu.org; Fri, 03 Jan 2014 03:59:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.80) (envelope-from ) id 1Vz0ar-0004GX-Sh for bug-gnu-emacs@gnu.org; Fri, 03 Jan 2014 03:59:01 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Phil Sainty Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Fri, 03 Jan 2014 08:59:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 16328 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch X-Debbugs-Original-To: bug-gnu-emacs@gnu.org Original-Received: via spool by submit@debbugs.gnu.org id=B.138873948816307 (code B ref -1); Fri, 03 Jan 2014 08:59:01 +0000 Original-Received: (at submit) by debbugs.gnu.org; 3 Jan 2014 08:58:08 +0000 Original-Received: from localhost ([127.0.0.1]:59218 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.80) (envelope-from ) id 1Vz0Zz-0004Ex-Qn for submit@debbugs.gnu.org; Fri, 03 Jan 2014 03:58:08 -0500 Original-Received: from eggs.gnu.org ([208.118.235.92]:35429) by debbugs.gnu.org with esmtp (Exim 4.80) (envelope-from ) id 1Vz0Zw-0004En-4O for submit@debbugs.gnu.org; Fri, 03 Jan 2014 03:58:05 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Vz0Zm-0007qm-1j for submit@debbugs.gnu.org; Fri, 03 Jan 2014 03:58:03 -0500 Original-Received: from lists.gnu.org ([2001:4830:134:3::11]:58907) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Vz0Zl-0007qh-Ui for submit@debbugs.gnu.org; Fri, 03 Jan 2014 03:57:53 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:37123) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Vz0Zd-0003KC-Oj for bug-gnu-emacs@gnu.org; Fri, 03 Jan 2014 03:57:53 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Vz0ZV-0007ny-L1 for bug-gnu-emacs@gnu.org; Fri, 03 Jan 2014 03:57:45 -0500 Original-Received: from nctlincom01.orcon.net.nz ([60.234.4.69]:53880) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Vz0ZV-0007mK-5F for bug-gnu-emacs@gnu.org; Fri, 03 Jan 2014 03:57:37 -0500 Original-Received: from mx3.orcon.net.nz (mx3.orcon.net.nz [219.88.242.53]) by nctlincom01.orcon.net.nz (8.14.3/8.14.3/Debian-9.4) with ESMTP id s039BCsm005756 for ; Fri, 3 Jan 2014 22:11:12 +1300 Original-Received: from Debian-exim by mx3.orcon.net.nz with local (Exim 4.69) (envelope-from ) id 1Vz0ZP-0000uC-IW for bug-gnu-emacs@gnu.org; Fri, 03 Jan 2014 21:57:31 +1300 Original-Received: from [121.99.80.47] (helo=[10.1.1.3]) by mx3.orcon.net.nz with esmtpa (Exim 4.69) (envelope-from ) id 1Vz0ZP-0000tl-BK for bug-gnu-emacs@gnu.org; Fri, 03 Jan 2014 21:57:31 +1300 User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:24.0) Gecko/20100101 Thunderbird/24.2.0 X-DSPAM-Check: by mx3.orcon.net.nz on Fri, 03 Jan 2014 21:57:31 +1300 X-DSPAM-Result: Innocent X-DSPAM-Processed: Fri Jan 3 21:57:31 2014 X-DSPAM-Confidence: 0.5302 X-DSPAM-Probability: 0.0000 X-Bayes-Prob: 0.0001 (Score 0, tokens from: @@RPTN, default) X-CanIt-Geo: ip=121.99.80.47; country=NZ; region=E7; city=Auckland; latitude=-36.8667; longitude=174.7667; http://maps.google.com/maps?q=-36.8667,174.7667&z=6 X-CanItPRO-Stream: base:default X-Canit-Stats-ID: 06L9lbcFR - 6c0fc760a212 - 20140103 X-Scanned-By: CanIt (www . roaringpenguin . com) on 172.16.100.174 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6.x X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.15 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 140.186.70.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.bugs:82853 Archived-At: This is a multi-part message in MIME format. --------------040307040501030803030506 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit 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 --------------040307040501030803030506 Content-Type: text/plain; charset=windows-1252; name="narrow-to-defun-include-comments.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="narrow-to-defun-include-comments.patch" 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)))) --------------040307040501030803030506--