From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: "Basil L. Contovounesios" Newsgroups: gmane.emacs.devel Subject: Re: isearch region or thing at point. Date: Tue, 30 Apr 2019 23:39:11 +0100 Message-ID: <87h8af15kg.fsf@tcd.ie> References: <20190427001453.isjx247kc3lu5fe4@Ergus> <87a7gcp51i.fsf@tcd.ie> <20190429004135.rn5tp2gnmbjovrxj@Ergus> <87h8agy4yf.fsf@mail.linkov.net> <20190430162501.xmqh5r5h57sjjlq5@Ergus> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="94860"; mail-complaints-to="usenet@blaine.gmane.org" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux) Cc: emacs-devel@gnu.org, Juri Linkov To: Ergus Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Wed May 01 00:39:33 2019 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([209.51.188.17]) by blaine.gmane.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hLbPQ-000OXW-7y for ged-emacs-devel@m.gmane.org; Wed, 01 May 2019 00:39:32 +0200 Original-Received: from localhost ([127.0.0.1]:54541 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hLbPP-00070x-6P for ged-emacs-devel@m.gmane.org; Tue, 30 Apr 2019 18:39:31 -0400 Original-Received: from eggs.gnu.org ([209.51.188.92]:54937) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hLbPF-00070s-8L for emacs-devel@gnu.org; Tue, 30 Apr 2019 18:39:22 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hLbPE-00061E-1f for emacs-devel@gnu.org; Tue, 30 Apr 2019 18:39:21 -0400 Original-Received: from mail-ed1-x536.google.com ([2a00:1450:4864:20::536]:34105) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hLbPD-00060Y-GE for emacs-devel@gnu.org; Tue, 30 Apr 2019 18:39:19 -0400 Original-Received: by mail-ed1-x536.google.com with SMTP id a6so13717547edv.1 for ; Tue, 30 Apr 2019 15:39:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tcd-ie.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:references:date:in-reply-to:message-id :user-agent:mime-version; bh=77KUQpWQvv+lilt2GsSj4+FWDCWqT6wOTV8HBf9s9Fo=; b=US9nFQfVF8YN4LrTLRi/a1WjM5frjgg4O1OnEAf7IbqLCjip5Rj2pLMMQ0q1lAlQCU O1PmmDVefadPOwvp3xIEuOvNSwPDt/BVj+TOqSVTGL/kIj5Q7mfbJaKfkCC3wczzCwX4 OK2QXOWuUHPZ+RHiWfHM6NqOq+VugczCiZF+GjzXzx33six6LSF9X/35LqCQaFgqHQfz TRO4IOqwympGHWijVc6xY3A/sQQbGesAuvoqeFIj28X9Z1Ch9dEHp39/ZzuaCxM1KOI8 VKSofLhvZISaTNauCbAx4Sa5Ojs5cFYjD8/D9WYWeited6Vk4RS5bCIZhU3ZhZrHnE7D tA1w== 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=77KUQpWQvv+lilt2GsSj4+FWDCWqT6wOTV8HBf9s9Fo=; b=REpQupaoneZYlMlyRvyLAzKdDnnNHZsSyHzoGm9fo9jgiPLOoE8wvPj6J2TifE0hCg cAez7ndSyB1oneaSrn1BkBEgmglrRwJxKW9OJRlDJVjnQFVtrW4I1/SLSjk23LkjCJUj 3+BFyNA5B6/9pTjcE07q/U/vC5giN8CmeIdiqbVA6aT9GskBqzJRR4Qo9Rfzs98LTfyq VHhv3Bx2rbOvZoZaFXiK0YKMlv2N3WfRETmqZjgkW7/9ahSZBp6BMa9tB2fVylpmTmJM D9CpeaMEtXBcrTZ/RsmmWZPec+uXREB5ZO2vOdfFYwuU6bZdyEhTrJ/7FsOw/YKuZ9uT OxPQ== X-Gm-Message-State: APjAAAX3eRMD5c6+c9SnddxVLCOTeMVppWELlNte4igVwGKuqnl+tkxd 3K6HR0k8KvWCoBNGu6yvr5zbAw== X-Google-Smtp-Source: APXvYqxjLEn50GUtwgz7IHClTe3+UbKgDZh9iaRd1moE8Hp1eKZhwgYB6J2enE1LWhjbDsaVUdpq3w== X-Received: by 2002:aa7:d9d1:: with SMTP id v17mr35388417eds.41.1556663957968; Tue, 30 Apr 2019 15:39:17 -0700 (PDT) Original-Received: from localhost ([2a02:8084:20e2:c380:8cad:ae29:555d:852d]) by smtp.gmail.com with ESMTPSA id g51sm419181edb.12.2019.04.30.15.39.16 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Tue, 30 Apr 2019 15:39:17 -0700 (PDT) In-Reply-To: <20190430162501.xmqh5r5h57sjjlq5@Ergus> (Ergus's message of "Tue, 30 Apr 2019 18:25:01 +0200") X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:4864:20::536 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:236080 Archived-At: Ergus writes: > I wrote this code today to do a search auto-insert text from region like > behavior. Thanks, I have some questions and minor comments: > (defun isearch-forward-region (&optional arg) > "Do incremental search forward for text in active region. > Like ordinary incremental search except that the text in the active > region is added to the search string initially if variable > `transient-mark-mode' is non nil. Perhaps "if `transient-mark-mode' is enabled" instead. > See the command `isearch-forward-symbol' for more information. Which information is that? Why not refer to isearch-forward instead? > With a prefix argument, search for ARGth symbol forward if ARG is > positive, or search for ARGth symbol backward if ARG is negative." I don't think this should mention symbols. > (interactive "P") > (isearch-forward nil 1) > (if-let* ((bounds (and (use-region-p) ;; Region and transient non-nil RHS comments usually start with a single semicolon; see (info "(elisp) Comment Tips"). > (string-empty-p isearch-string) Why must this be empty? > (region-bounds))) > (contiguous (= (length bounds) 1)) ;; Region is contiguous Better to use the function region-noncontiguous-p. Also, you shouldn't leave bound symbols unused. The most common way to short-circuit conditionals is via the special forms 'and' and 'or'. In the case of if-let et al., you can also use the following syntax: (if-let* ((len (length foo)) ((zerop len))) 'empty 'nonempty) > (region-beg (car (car bounds))) > (region-end (cdr (car bounds))) Nitpick: caar/cdar, also not important. > (region-string (and (= (count-lines region-beg region-end) 1) > (buffer-substring-no-properties > region-beg region-end))) Why can't the region span multiple lines? Better to use region-extract-function for this, as it is more flexible. > (noempty (not (string-blank-p region-string)))) Why can't the search string be blank? > (progn > (goto-char region-beg) > (setq mark-active nil > isearch--deactivated-mark t) Where is isearch--deactivated-mark defined? What does it do? Shouldn't this set/call deactivate-mark or similar instead? > (isearch-yank-string region-string) > > (when-let (count (and arg (prefix-numeric-value arg))) > (isearch-repeat-forward count))) This can be simplified as per Noam's suggestion. > (setq isearch-error "Invalid region for isearch") > (isearch-push-state) > (isearch-update))) Here's a suggestion which addresses some of my comments: (defun isearch-forward-region (&optional arg) "Do incremental search forward for text in active region. Like ordinary incremental search except that the text in the active region is added to the search string initially if`transient-mark-mode' is enabled. See the command `isearch-forward' for more information. With a prefix argument, search for ARGth occurrence forward if ARG is positive, or ARGth occurrence backward if ARG is negative." (interactive "P") (isearch-forward nil 1) (let ((region (and (use-region-p) (string-empty-p isearch-string) (funcall region-extract-function nil)))) (cond ((and (stringp region) (not (string-empty-p region))) (goto-char (region-beginning)) (deactivate-mark) (isearch-yank-string region) (when arg (isearch-repeat-forward (prefix-numeric-value arg)))) (t (setq isearch-error "Invalid region") (isearch-push-state) (isearch-update))))) Feel free to adapt it as you please. Thanks, -- Basil