[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Iterating over buffer lines
From: |
tomas |
Subject: |
Re: Iterating over buffer lines |
Date: |
Sat, 17 Jun 2023 06:48:24 +0200 |
On Fri, Jun 16, 2023 at 10:28:45PM -0500, 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)
The above means that you are inserting stuff at the beginning of the
current line, right?
> (forward-line)))))
... and this assumes that your `some-chosen-function' doesn't mess
"too much" with point.
>
> (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."
> ........
I can't /see/ it, but I have a hunch that this function expects
to /replace/ the line by something else. The was it is called
above, its result is /inserted/ in front of the line in question,
right?
Perhaps it's clear to you, but if yes, it doesn't come across:
what is the exact interface between your outer function and
the one called by it?
I.e.: do you need to (save-excursion ...) around `some-chosen-function'?
What do you do with its result? That kind of things.
Cheers
--
t
signature.asc
Description: PGP signature