From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Platon Pronko Newsgroups: gmane.emacs.help Subject: Re: Iterating over buffer lines Date: Sun, 18 Jun 2023 09:17:14 +0800 Message-ID: <50e7547c-03ab-4c5d-4c38-83c09194684e@gmail.com> References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="34744"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.12.0 Cc: help-gnu-emacs@gnu.org To: Joshua Lambert Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Sun Jun 18 03:18:09 2023 Return-path: Envelope-to: geh-help-gnu-emacs@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 1qAh3Q-0008lR-Rt for geh-help-gnu-emacs@m.gmane-mx.org; Sun, 18 Jun 2023 03:18:08 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qAh2v-0006G9-Qb; Sat, 17 Jun 2023 21:17:37 -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 1qAh2u-0006Fn-Fu for help-gnu-emacs@gnu.org; Sat, 17 Jun 2023 21:17:36 -0400 Original-Received: from mail-pf1-x42f.google.com ([2607:f8b0:4864:20::42f]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qAh2s-0006TG-TS for help-gnu-emacs@gnu.org; Sat, 17 Jun 2023 21:17:36 -0400 Original-Received: by mail-pf1-x42f.google.com with SMTP id d2e1a72fcca58-666ed230c81so1247067b3a.0 for ; Sat, 17 Jun 2023 18:17:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1687051039; x=1689643039; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=oWWVFYGSVHg5tsgSu6y4eN3Pc8wfSW/n2Aci+/0hO30=; b=EHQ7TYM6K96MbXTcnUKUDldunC9e67tvOxHpmsC17dnqQ+G7PGdx5JnF2YQHiYSD86 Kmsb3QPH2Mn6uoPuMG6IGGvW7qg0neBkeqlI5gVfEDvjGe0jdCSDe+YirDVKpB1uUIHc Q35Pa4h8CzobrXKd932l2vGtwoflj00kxQ5Bli148wEZ5trnqH1iuNge4p5fsZTyqpNB BIUBLBytZWP75HPs+rY7fVxmRptQlkiziEp2OVziCAcSoFGJ+RCmRElRrUd3qSk+dn4d AVY6JCmHVXh68FkYKGcvsjZlojU7ZdQisTmJhjAI2Juyk6M8Zqn+4oCXZF4bvJ8c/3fD /etw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687051039; x=1689643039; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=oWWVFYGSVHg5tsgSu6y4eN3Pc8wfSW/n2Aci+/0hO30=; b=dvm37dbbyAPB51t3fTDVqD3vI60xFbvBO2dnURkpqnji+YbtZzzqOFTSqtPVe7ULUs iQh7qBhde21KjyyBMtbsgrS+jusmY2MDCzCmCQdHneaf3RQkQsqNnLgSqJPQ7U3aqKNH ftbC92P0j2vAW75CUwxhQuFVrr6CZlZ3HtaL5og3NcQRcntTnj3uBlL/tVWmO5Sa3jkW OixEOl9px+g0XK/RgGEbuRkQ0282XYJ+jmXfMs5FgUs345+uley4tlFxwJi6s123ohKb 68NSARD7GgBnQadKTh8YFqVw2CP/GSwFH4y+Ijz575depqBhyZrbMPdkQMsEeWMPHTPS QGKA== X-Gm-Message-State: AC+VfDxFvMIqNEInpb2aLXq5CqLYwnz3w+eZ4SMo0VikTOYjdGVLTfFu 9vdeeNEzmGXNpUKTz0jm+BcFG7Su5yg= X-Google-Smtp-Source: ACHHUZ5aGS64Zbt00AjDGIycMzHvrPXVXolhx5/Q5VPwKTishx9YDepTM6/FottsNrh8xEUEu40eaA== X-Received: by 2002:a05:6a00:1804:b0:667:e17e:85bc with SMTP id y4-20020a056a00180400b00667e17e85bcmr4843670pfa.0.1687051039172; Sat, 17 Jun 2023 18:17:19 -0700 (PDT) Original-Received: from [192.163.0.137] ([103.24.106.35]) by smtp.gmail.com with ESMTPSA id i21-20020aa78b55000000b0064fe06fe712sm15056172pfd.129.2023.06.17.18.17.17 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 17 Jun 2023 18:17:18 -0700 (PDT) Content-Language: en-US In-Reply-To: Received-SPF: pass client-ip=2607:f8b0:4864:20::42f; envelope-from=platon7pronko@gmail.com; helo=mail-pf1-x42f.google.com X-Spam_score_int: -21 X-Spam_score: -2.2 X-Spam_bar: -- X-Spam_report: (-2.2 / 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, FREEMAIL_FROM=0.001, NICE_REPLY_A=-0.091, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.help:143979 Archived-At: On 2023-06-17 11:28, Joshua Lambert wrote: > OK. See below. I'm trying to avoid reusing most of the code in > function my-act-on-region-by-line. There are multiple functions that > correct data errors in a csv file and I want to be able to > interactively run one of those corrections at a time. The resulting > string will be inserted at the beginning of each line. That all works, > I'm just trying not to have so much code repetition. > > (defun my-act-on-region-by-line (some-function &optional beg end) > "Perform a function on the current line or each line of the region. > Each SOME-FUNCTION must return a string." > (let ((beg2 (if (region-active-p) > beg > (line-beginning-position))) > (end2 (if (region-active-p) > end > (line-end-position)))) > (save-excursion > (save-restriction > (narrow-to-region beg2 end2) > (goto-char (point-min)) > (while (not (eobp)) > (goto-char (line-beginning-position)) > (insert some-chosen-function) ;; Inserts text after a > function transforms it. > (insert my-separator) > (forward-line))))) > > (defun my-paste-corrected-spacing (&optional field-num beg end) > "Correct spacing in a string and paste it at the beginning of a line. > Interactively, BEG and END are the region." > (interactive "*p\nr") > (my-act-on-region-by-line (some-function-to-correct-spacing field-num) > beg > end)) > > (defun some-function-to-correct-spacing (field-num) > "Function that creates a correctly spaced string." > ........ > The code you pasted doesn't seem to work, for example because some-chosen-function doesn't exist. So I have trouble understanding what your problem is - is the pasted code fragment the example of duplication, or your approach to reducing duplication in code that I don't see? If it's example of duplication (between my-paste-corrected-spacing and some other similar functions), then it looks like the duplication is minimal - function name, docstring, and underlying function are different. So actual duplication is 2-3 lines - `(&optional field-num beg end)`, `(interactive "*p\nr")`, and `my-act-on-region-by-line` parts. You can create a macro to reduce that duplication even further, but it seems that we are hitting diminishing returns here. If the code as shown is already an attempt to reduce duplication then it looks like you are almost there - you just need to use `(apply some-function)` to call the function that you pass to my-act-on-region-by-line.