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: [patch] Run occur command restricted to a region Date: Thu, 29 Dec 2016 15:36:56 +0900 Message-ID: <87vau3jl6f.fsf@gmail.com> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: blaine.gmane.org 1482993484 25841 195.159.176.226 (29 Dec 2016 06:38:04 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Thu, 29 Dec 2016 06:38:04 +0000 (UTC) Cc: tino.calancha@gmail.com To: Emacs developers Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Thu Dec 29 07:37:56 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 1cMULb-0005Zi-Ne for ged-emacs-devel@m.gmane.org; Thu, 29 Dec 2016 07:37:55 +0100 Original-Received: from localhost ([::1]:34034 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cMULg-00082b-FA for ged-emacs-devel@m.gmane.org; Thu, 29 Dec 2016 01:38:00 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:51625) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cMUL0-00082I-Em for emacs-devel@gnu.org; Thu, 29 Dec 2016 01:37:19 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cMUKx-0008J8-A7 for emacs-devel@gnu.org; Thu, 29 Dec 2016 01:37:18 -0500 Original-Received: from mail-pg0-x241.google.com ([2607:f8b0:400e:c05::241]:35039) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1cMUKx-0008J2-2Q for emacs-devel@gnu.org; Thu, 29 Dec 2016 01:37:15 -0500 Original-Received: by mail-pg0-x241.google.com with SMTP id i5so17995343pgh.2 for ; Wed, 28 Dec 2016 22:37:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version; bh=MtXtSVJlQWIxHrnya2P3iZiQohyVzY3xV2jx+GmudNw=; b=XrX+KFG+XNElrennMNmX+PkpylxwbhWCOn5AFyh+fIr5u+NCE5p2V6govwcOMuUzUb u1ZMjFzvlwKdme3bMqMWWxxHj/LnLRc+IY0wCwhOIEXu+Bmkb8jOGdXBgD1uano9H2s1 313kmmCQH43/zafAjQSMT+BCACOroOsQIf5mASWbxuHWniw03Phc7Hb2S4e1znvkfm7C 7KYQh7lgaI/qvrW8gxEpnSTXdy7hY2BNLnZIu0UqA4X+fSu7g9J9g7+hLSDpUAo4aHvD S2HiXXcvqrkfj+b4J31f8nYqerJhIkPHKBk3Join8ffe5ugqTXG7bSh4NpgCudKIb+PO lruw== 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:date:message-id:mime-version; bh=MtXtSVJlQWIxHrnya2P3iZiQohyVzY3xV2jx+GmudNw=; b=eprxk9o2TwAWSZX2lJPPl6DPWL3XbPs2znWhhVLUWV86n08Kk3ORqKMCCxpIAg+e3I rneW0sX7kFTOQsWPj26G8wAw0FKFWAI6VcI+W/YWPGtDGOjb2CCYFuoPR771HMLn5s3O NuTEAl5WH+fY3WoECvzeeaK5eKTM8rjRoodjmR35Xy1W2+E0mbu3d+WVcYONG6p8g1fH iGWApjJFG2aJJ0AfRwqZYlrGfx5TE943vXCC4Ut0PcOjHRSQ+lxoz3+fFckyEDMZqZK+ 3p4IXoth4oa9qgfZGicSLp6XbIqI3OpQYdQ6MWhbdmMK+dizSxiPhu+V+D07cFdgmk7c Ghlw== X-Gm-Message-State: AIkVDXJnjNlSG9GEAhYWddzXR6mdlLJ/2+fJ4dJA6BBULsAL6LOQ+QXXWYpsb9YLn3Gnxw== X-Received: by 10.84.234.1 with SMTP id m1mr55176891plk.155.1482993432452; Wed, 28 Dec 2016 22:37:12 -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 p68sm101596157pfd.11.2016.12.28.22.37.10 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 28 Dec 2016 22:37:12 -0800 (PST) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2607:f8b0:400e:c05::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:210947 Archived-At: Hi, is it worth to have versions of `occur' restricting the search to lines before (after) the current line? Maybe there is already a way to do that, and i just ignore it. I usually just i copy the region into a temporary buffer; then i run `occur' there. I just wrote a patch to show my point; it adds the following commands: 1) occur-backward: for lines before the current one. 2) occur-forward: for lines after the current one. 3) occur-in-region: for lines within the active region. Not sure if we want to bind them to keys. Tentatively, i have assigned `M-s b' and `M-s f' for 1) and 2) in the `search-map'. That means, these bindings are not available in all modes; OTOH command `occur' is in the `global-map', so it's always available. Any comment about this proposal? Thanks, Tino ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; >From 61097b346737aa1960da568c54fc2b9211e5fe5c Mon Sep 17 00:00:00 2001 From: Tino Calancha Date: Thu, 29 Dec 2016 15:02:47 +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'. --- lisp/bindings.el | 2 ++ lisp/replace.el | 51 ++++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 48 insertions(+), 5 deletions(-) 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..09e0bf1e1f 100644 --- a/lisp/replace.el +++ b/lisp/replace.el @@ -1322,11 +1322,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 +1390,42 @@ 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 &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")) + (let ((occur-matches-threshold + (line-number-at-pos (region-beginning)))) + (narrow-to-region (region-beginning) (region-end)) + (occur regexp nlines)) + (widen)) + (defvar ido-ignore-item-temp-list) (defun multi-occur (bufs regexp &optional nlines) @@ -1539,6 +1576,9 @@ occur-1 (set-buffer-modified-p nil) (run-hooks 'occur-hook))))))) +;; Let binded in `occur-forward' and `occur-in-region'. +(defvar occur-matches-threshold nil) + (defun occur-engine (regexp buffers out-buf nlines case-fold title-face prefix-face match-face keep-props) (with-current-buffer out-buf @@ -1551,7 +1591,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-28 Repository revision: 112460da705c2a6716d7b6bc72501de0a3757259