From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Tino Calancha Newsgroups: gmane.emacs.devel Subject: Re: [patch] Run occur command restricted to a region Date: Fri, 30 Dec 2016 01:54:20 +0900 Message-ID: <87shp665hf.fsf@gmail.com> References: <87vau3jl6f.fsf@gmail.com> <8337h6vhr7.fsf@gnu.org> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: blaine.gmane.org 1483030517 31082 195.159.176.226 (29 Dec 2016 16:55:17 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Thu, 29 Dec 2016 16:55:17 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.0.50 (gnu/linux) Cc: tino.calancha@gmail.com, emacs-devel@gnu.org To: Eli Zaretskii Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Thu Dec 29 17:55:09 2016 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cMdyu-0006YK-5U for ged-emacs-devel@m.gmane.org; Thu, 29 Dec 2016 17:55:08 +0100 Original-Received: from localhost ([::1]:36390 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cMdyu-00029i-30 for ged-emacs-devel@m.gmane.org; Thu, 29 Dec 2016 11:55:08 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:59178) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cMdyJ-00029c-LQ for emacs-devel@gnu.org; Thu, 29 Dec 2016 11:54:33 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cMdyE-0007wu-LK for emacs-devel@gnu.org; Thu, 29 Dec 2016 11:54:31 -0500 Original-Received: from mail-pf0-x241.google.com ([2607:f8b0:400e:c00::241]:33177) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1cMdyE-0007up-DS; Thu, 29 Dec 2016 11:54:26 -0500 Original-Received: by mail-pf0-x241.google.com with SMTP id 127so11038959pfg.0; Thu, 29 Dec 2016 08:54:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:references:date:in-reply-to:message-id :user-agent:mime-version; bh=GKvQmdDpjXLoqWcFrQPcWtLUr1irzGDL267Xj3mes6A=; b=C1uaTm1l2E3KJ37PgOfBEeIvzdZZtvE7HSNn6x+75SXwcXxBht4edwCgOpWIPWA4AA CszPl7GQwcvNVU0nODC8NH4Y1uXRh7PXsM90557lAgsaAjxYVng73UltRYMO1e1Ap0wG Geelsm/Bh1zTww25QSspPPI2pJpD2A46hC0OZg1wW1mTfhLZnYWHiEl/rPva7FfGQYOJ /RqR33aqXD3ZEfST/v5B2hT+HArRtcmjnF8OI5A29hgk2x8IpJXSws9xDo1FBFLDnv1m Q4LisU5RlM4s2/J33jVJg4FoPFZtP3TjclhmviVZUjXwf+1XUHwQz7XBzO5TwVvrZqVz t0UQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:references:date:in-reply-to :message-id:user-agent:mime-version; bh=GKvQmdDpjXLoqWcFrQPcWtLUr1irzGDL267Xj3mes6A=; b=fOJIODwutpU7dVQ4AaDAz3XxQFrpcI7he4Krj5V+I3QUmFFLWuru/kcsNIa4O2ncQt uDHzfJTLHJFh8KoWQvNGDaF9z81Q5y226PavwKTW8zd88380h1poVcd59juxOfGSd3NR efaWdAJmfAaa/nj+a+DNFm9UWX5U4xbnVJtEh3BGY9ApbqF/WuWDJeay5VG0vvgDixSQ o8CW4C6x17/MjtXAg9ULETxx1ml1Fyr2QuuyCjTv2WDfKXSF0InFvqWqoMsL7k+aKwCh NisSXLNO9iEQKvaYn5YxEyDghLUuS78qWQ+/reQSQkGzTgCAbeJIDXxpA/3KZUOyuEpA ToWg== X-Gm-Message-State: AIkVDXKn6ttPzZ1/uHbvS+BkKlUGT47pVh4iy8xhm7ImE+JPM7fjR8LAGAkjk0GBC7N7Ng== X-Received: by 10.99.156.2 with SMTP id f2mr79628113pge.20.1483030464815; Thu, 29 Dec 2016 08:54:24 -0800 (PST) Original-Received: from calancha-pc (217.225.128.101.dy.bbexcite.jp. [101.128.225.217]) by smtp.gmail.com with ESMTPSA id u3sm106297175pfk.3.2016.12.29.08.54.23 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 29 Dec 2016 08:54:24 -0800 (PST) In-Reply-To: <8337h6vhr7.fsf@gnu.org> (Eli Zaretskii's message of "Thu, 29 Dec 2016 18:10:04 +0200") X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2607:f8b0:400e:c00::241 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.org gmane.emacs.devel:210961 Archived-At: Eli Zaretskii writes: >> +(defun occur-in-region (regexp &optional nlines) >> + "Show all lines in the active region containing a match for REGEXP. >> +Each line is displayed with NLINES lines before and after, or -NLINES >> +before if NLINES is negative. >> +As `occur' but restricted to lines within the active region." >> + (interactive (occur-read-primary-args "in region")) >> + (unless (use-region-p) >> + (error "Region not active")) > > I think our convention for functions that act on the region is to > accept BEG and END arguments, so that the function could be called > from Lisp. The 'interactive' form/spec should then provide these > arguments for the interactive use. Right! Thank you. > Also, this will need a NEWS entry, and possibly also an addition to > the user manual. Sure, i have added a NEWS entry for the commands. I don't see a clear place in the manual where to mention about these commands: it seems `occur' is just defined in the intro as an example on how to make keybindings. Here is the updated patch: ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; >From fbee1d786c35b6148112f32717d30e17836fd4ae Mon Sep 17 00:00:00 2001 From: Tino Calancha Date: Fri, 30 Dec 2016 01:42:00 +0900 Subject: [PATCH] New commands: occur-forward, occur-backward, occur-in-region * lisp/replace.el (occur-matches-threshold): New variable. (occur-in-region, occur-forward, occur-backward): New commands. * lisp/bindings.el (search-map): Bind occur-backward to 'M-s b' and occur-forward to 'M-s f'. ; etc/DEBUG: Add entry for the new commands. --- etc/NEWS | 5 +++++ lisp/bindings.el | 2 ++ lisp/replace.el | 54 +++++++++++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 56 insertions(+), 5 deletions(-) diff --git a/etc/NEWS b/etc/NEWS index e2ada7c1be..9cfd5ed340 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -294,6 +294,11 @@ substituted by a home directory by writing it as "/foo:/:/~/file". * Editing Changes in Emacs 26.1 +--- +** New commands 'occur-in-region', 'occur-forward' and 'occur-backward' +run 'occur' restricted to a region of the current buffer; 'occur-backward' +bound to 'M-s b' and 'occur-forward' bound to 'M-s f'. + +++ ** New bindings for 'query-replace-map'. 'undo', undo the last replacement; bound to 'u'. diff --git a/lisp/bindings.el b/lisp/bindings.el index c13f4b156a..4b1c88a3c5 100644 --- a/lisp/bindings.el +++ b/lisp/bindings.el @@ -928,6 +928,8 @@ search-map (define-key esc-map "s" search-map) (define-key search-map "o" 'occur) +(define-key search-map "b" 'occur-backward) +(define-key search-map "f" 'occur-forward) (define-key search-map "\M-w" 'eww-search-words) (define-key search-map "hr" 'highlight-regexp) (define-key search-map "hp" 'highlight-phrase) diff --git a/lisp/replace.el b/lisp/replace.el index a172174633..95af63b108 100644 --- a/lisp/replace.el +++ b/lisp/replace.el @@ -1092,6 +1092,9 @@ occur-mode (set (make-local-variable 'revert-buffer-function) 'occur-revert-function) (setq next-error-function 'occur-next-error)) +;; Let binded in `occur-forward' and `occur-in-region'. +(defvar occur-matches-threshold nil) + ;;; Occur Edit mode @@ -1322,11 +1325,12 @@ occur-excluded-properties :group 'matching :version "22.1") -(defun occur-read-primary-args () - (let* ((perform-collect (consp current-prefix-arg)) +(defun occur-read-primary-args (&optional where) + (let* ((str (if where (concat " " where) "")) + (perform-collect (consp current-prefix-arg)) (regexp (read-regexp (if perform-collect - "Collect strings matching regexp" - "List lines matching regexp") + (concat "Collect strings matching regexp" str) + (concat "List lines matching regexp" str)) 'regexp-history-last))) (list regexp (if perform-collect @@ -1389,6 +1393,45 @@ occur (interactive (occur-read-primary-args)) (occur-1 regexp nlines (list (current-buffer)))) +(defun occur-backward (regexp &optional nlines) + "Show all lines before current line containing a match for REGEXP. +Each line is displayed with NLINES lines before and after, or -NLINES +before if NLINES is negative. +As `occur' but restricted to lines before the current one." + (interactive (occur-read-primary-args "backward")) + (narrow-to-region (point-min) (point)) + (occur regexp nlines) + (widen)) + +(defun occur-forward (regexp &optional nlines) + "Show all lines after current line containing a match for REGEXP. +Each line is displayed with NLINES lines before and after, or -NLINES +before if NLINES is negative. +As `occur' but restricted to lines after the current one." + (interactive (occur-read-primary-args "forward")) + (let ((occur-matches-threshold + (line-number-at-pos (point)))) + (narrow-to-region (point) (point-max)) + (occur regexp nlines)) + (widen)) + +(defun occur-in-region (regexp beg end &optional nlines) + "Show all lines in the active region containing a match for REGEXP. +BEG and END define the region. +Each line is displayed with NLINES lines before and after, or -NLINES +before if NLINES is negative. +As `occur' but restricted to lines within the active region." + (interactive + (if (null (use-region-p)) + (error "Region not active") + (let ((args (occur-read-primary-args "in region"))) + (list (car args) (region-beginning) (region-end) (cadr args))))) + (let ((occur-matches-threshold + (line-number-at-pos beg))) + (narrow-to-region beg end) + (occur regexp nlines)) + (widen)) + (defvar ido-ignore-item-temp-list) (defun multi-occur (bufs regexp &optional nlines) @@ -1551,7 +1594,8 @@ occur-engine (when (buffer-live-p buf) (let ((lines 0) ;; count of matching lines (matches 0) ;; count of matches - (curr-line 1) ;; line count + (curr-line ;; line count + (or occur-matches-threshold 1)) (prev-line nil) ;; line number of prev match endpt (prev-after-lines nil) ;; context lines of prev match (matchbeg 0) -- 2.11.0 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; In GNU Emacs 26.0.50.1 (x86_64-pc-linux-gnu, GTK+ Version 3.22.5) of 2016-12-30 Repository revision: 11b81a54d538e8deca3cd64521cf85409efb617b