From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Philip Kaludercic Newsgroups: gmane.emacs.devel Subject: Re: A function to take the regexp-matched subsring directly Date: Sun, 30 Oct 2022 17:26:48 +0000 Message-ID: <87leox9q93.fsf@posteo.net> References: <87wn8h9uy4.fsf@posteo.net> <831245da-8f0f-85b7-0cb7-e088157012d2@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="26208"; mail-complaints-to="usenet@ciao.gmane.io" Cc: emacs-devel@gnu.org To: daanturo Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Sun Oct 30 18:27:57 2022 Return-path: Envelope-to: ged-emacs-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1opC6H-0006e4-0M for ged-emacs-devel@m.gmane-mx.org; Sun, 30 Oct 2022 18:27:57 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1opC5J-00065T-A8; Sun, 30 Oct 2022 13:26:57 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1opC5H-00065H-8Q for emacs-devel@gnu.org; Sun, 30 Oct 2022 13:26:55 -0400 Original-Received: from mout02.posteo.de ([185.67.36.66]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1opC5E-0000eW-75 for emacs-devel@gnu.org; Sun, 30 Oct 2022 13:26:55 -0400 Original-Received: from submission (posteo.de [185.67.36.169]) by mout02.posteo.de (Postfix) with ESMTPS id 77639240105 for ; Sun, 30 Oct 2022 18:26:49 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.net; s=2017; t=1667150809; bh=fpoQs+WRlEQxB7y0i/VnVeCWwfGCmNqfkBJgz+X+qts=; h=From:To:Cc:Subject:Autocrypt:Date:From; b=f13fX5eCI1D8tnKpVuuqLqDhfqCn8xDj5ARFK/OemRpsfz+Rlvm7svJyrG/bdWLM/ C8w+EmdO6s1hORHeddkmOvpI/QhLOz2eNao4mPTzBA/pnlmdiyNqVjFJVpch8vAimS KKCPkllbUIRWBtZTnTTcurAEYlzazhiW8HjIZ6ne8ERvtnpp2t0pJ8CDgwE/yx84ik djEAWyUsg++dK79TkjNtGOLPSglqnWFL7e3/dJmhyx1hBK0nSPQGzLDtRRZ4QNjwgR bcdzEMJkAhmTFehYmVIzaI/lkBTOZnIriMwCM9uexJy+sW2FpjqBjSRTfrViR5uy/r gIKri7NacGPrA== Original-Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4N0jrS67hYz6tmN; Sun, 30 Oct 2022 18:26:48 +0100 (CET) In-Reply-To: <831245da-8f0f-85b7-0cb7-e088157012d2@gmail.com> (daanturo@gmail.com's message of "Sun, 30 Oct 2022 23:46:10 +0700") Autocrypt: addr=philipk@posteo.net; prefer-encrypt=nopreference; keydata= mDMEYHHqUhYJKwYBBAHaRw8BAQdAp3GdmYJ6tm5McweY6dEvIYIiry+Oz9rU4MH6NHWK0Ee0QlBo aWxpcCBLYWx1ZGVyY2ljIChnZW5lcmF0ZWQgYnkgYXV0b2NyeXB0LmVsKSA8cGhpbGlwa0Bwb3N0 ZW8ubmV0PoiQBBMWCAA4FiEEDM2H44ZoPt9Ms0eHtVrAHPRh1FwFAmBx6lICGwMFCwkIBwIGFQoJ CAsCBBYCAwECHgECF4AACgkQtVrAHPRh1FyTkgEAjlbGPxFchvMbxzAES3r8QLuZgCxeAXunM9gh io0ePtUBALVhh9G6wIoZhl0gUCbQpoN/UJHI08Gm1qDob5zDxnIHuDgEYHHqUhIKKwYBBAGXVQEF AQEHQNcRB+MUimTMqoxxMMUERpOR+Q4b1KgncDZkhrO2ql1tAwEIB4h4BBgWCAAgFiEEDM2H44Zo Pt9Ms0eHtVrAHPRh1FwFAmBx6lICGwwACgkQtVrAHPRh1Fw1JwD/Qo7kvtib8jy7puyWrSv0MeTS g8qIxgoRWJE/KKdkCLEA/jb9b9/g8nnX+UcwHf/4VfKsjExlnND3FrBviXUW6NcB Received-SPF: pass client-ip=185.67.36.66; envelope-from=philipk@posteo.net; helo=mout02.posteo.de X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: "Emacs-devel" Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.devel:298827 Archived-At: daanturo writes: > On 30/10/2022 22:45, Philip Kaludercic wrote: >> Is there a reason you find yourself working with strings as opposed to >> buffers? I've seen people try to force functional paradigms on Emacs >> when they do stuff like creating a list of lines in a buffer then >> iterating over these instead of using the (faster) buffer searching >> mechanisms. My worry is that functions like these, while useful per se, >> might make more people inclined to write unideomatic and wasteful code. > > > In my case, strings are usually file names and shell command outputs > > ```elisp > > ;; Get the commit hash returned by git blame > > (shell-command-to-string "git blame -L 1,1 -- README") > > "19dcb237b5b (Eli Zaretskii 2022-01-01 02:45:51 -0500 1) Copyright (C) 20= 01-2022 Free Software Foundation, Inc. > " > > ;; parse: > (regexp-match "^[^ ]+" > "19dcb237b5b (Eli Zaretskii 2022-01-01 02:45:51 -0500 1) Copyright (C) 20= 01-2022 Free Software Foundation, Inc. > ") > =3D> "19dcb237b5b" I would argue that this is more robust (though more verbose) --8<---------------cut here---------------start------------->8--- (with-temp-buffer (call-process "git" nil t nil "blame" "-L1,1" "--" "README") (goto-char (point-min)) (if (looking-at "\\`[[:alnum:]]+") (match-string 0) 'some-other-value)) --8<---------------cut here---------------end--------------->8--- > > ;; From `vc-revision-other-window''s file name, find the original name an= d the revision > (regexp-match* > =C2=A0=C2=A0=C2=A0 "\\(.*?\\)\\(?:\\.~\\(.*?\\)~\\)?\\'" > =C2=A0=C2=A0=C2=A0 "/foo/bar.el.~main~") > =C2=A0=C2=A0 =C2=A0 > =3D> ("/foo/bar.el.~main~" "/foo/bar.el" "main") And the file-name-* functions ought to be used here instead. > ``` > > > And many possible cases where the strings maybe buffer names or any not s= o long > strings that are not the size of a buffer. What do you mean by "size of a buffer"? Come to think of it, a macro like `with-string-as-buffer' would be a good addition, to make it easier to use the text editing functionality instead of string handling.