* Q: How to copy a string in a text into a variable?
@ 2002-09-16 3:54 gnuist
2002-09-16 10:17 ` Eric Marsden
0 siblings, 1 reply; 4+ messages in thread
From: gnuist @ 2002-09-16 3:54 UTC (permalink / raw)
Q: How to copy contents of kill ring into a string variable?
The purpose of these two questions above and the previous post is the
same as below:
-------------------------------
My problem is very simple for an emacs guru. More than one solution is
very welcome.
I have a list of numbers in a file as follows:
ABC98789
DDE90898889
FRE9090909
that is, first three letters and then a string of numbers and nothing else.
I want to write a lisp function (not a macro) that can read the first
three letter substring into a variable and the rest of the substring into
another variable. Then I want to use these substrings to generate my
final string. I know that can be done using the "insert" command. The problem
is how to put the text strings in a file into a variable? I know setq can
do this but then if I construct the string in sexp such as:
(setq letters-variable "ABC")
how do I eval it? I have tried eval-last-sexp IN THE LISP FUNCTION
and it gives some strange result in the minibuffer when the function is run.
Any and all help is appreciated.
With a macro using C-k and yank this is trivial but the kill buffer can only
hold (memorize) one piece at a time not the two pieces. Still it can be done
by moving to the two lines where they are separately held, but it is not a
readible solution. It is at the turing machine level of copy and erase one
at a time, not store in named variables.
Once I have written this function for one string, I can run it on the
whole list by C-u 3 M-x my-function.
Cheers
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Q: How to copy a string in a text into a variable?
2002-09-16 3:54 Q: How to copy a string in a text into a variable? gnuist
@ 2002-09-16 10:17 ` Eric Marsden
2002-09-18 18:07 ` gnuist
0 siblings, 1 reply; 4+ messages in thread
From: Eric Marsden @ 2002-09-16 10:17 UTC (permalink / raw)
>>>>> "gd" == gnuist <gnuist007@hotmail.com> writes:
gd> Q: How to copy contents of kill ring into a string variable?
the kill ring is a list of strings: say C-h v kill-ring RET.
The string that would be inserted by C-y is available as
(car kill-ring-yank-pointer)
(though you shouldn't normally be manipulating the kill ring from an
elisp function; the kill ring is reserved for user interaction.)
gd> I have a list of numbers in a file as follows:
gd>
gd> ABC98789
gd> DDE90898889
gd> FRE9090909
gd>
gd> that is, first three letters and then a string of numbers and nothing else.
gd>
gd> I want to write a lisp function (not a macro) that can read the first
gd> three letter substring into a variable and the rest of the substring into
gd> another variable. Then I want to use these substrings to generate my
gd> final string.
the buffer-substring function allows you to access the characters
between certain positions in a buffer. You probably want something
like the following:
(defun my-manipulation-function ()
(interactive "*")
(let (bol eol numbers letters)
(save-excursion
(save-match-data
(beginning-of-line)
(when (looking-at "[A-Z]\\{3\\}[0-9]+")
(setq bol (point)
eol (progn (end-of-line) (point))
letters (buffer-substring bol (+ 3 bol))
numbers (buffer-substring (+ 3 bol) eol))
(beginning-of-line)
(delete-region (point) eol)
(insert numbers letters))))))
gd> With a macro using C-k and yank this is trivial but the kill buffer can only
gd> hold (memorize) one piece at a time not the two pieces.
actually, the kill ring contains a list of elements, so in theory it
could do this.
gd> Once I have written this function for one string, I can run it on the
gd> whole list by C-u 3 M-x my-function.
if you can invoke it with M-x, it's a command rather than a function.
I suggest you read the Emacs Lisp Introduction; it's a good way to get
the feel of the language and how to program the Emacs.
--
Eric Marsden <URL:http://www.laas.fr/~emarsden/>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Q: How to copy a string in a text into a variable?
2002-09-16 10:17 ` Eric Marsden
@ 2002-09-18 18:07 ` gnuist
2002-09-19 11:27 ` Eric Marsden
0 siblings, 1 reply; 4+ messages in thread
From: gnuist @ 2002-09-18 18:07 UTC (permalink / raw)
Eric Marsden <emarsden@laas.fr> wrote in message
> the buffer-substring function allows you to access the characters
> ^^^^^^^^^^^^^^^^
> between certain positions in a buffer
This was what I am looking for. But you have a number of pearls or
possibly bugs that I could not verify. Specifically, I am interested in a
dissection of the regular expression
> (when (looking-at "[A-Z]\\{3\\}[0-9]+")
and the conundrum is in \\{3\\}, a construct, which has no reference in
the info page on emacs regular expressions.
You may as well explain the wisdom in the use of
(save-excursion ; ???
(save-match-data ; ???
which I fail to see despite seeing description on them.
Thanks a lot!!!!
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Q: How to copy a string in a text into a variable?
2002-09-18 18:07 ` gnuist
@ 2002-09-19 11:27 ` Eric Marsden
0 siblings, 0 replies; 4+ messages in thread
From: Eric Marsden @ 2002-09-19 11:27 UTC (permalink / raw)
>>>>> "gd" == gnuist <gnuist007@hotmail.com> writes:
gd> But you have a number of pearls or possibly bugs that I could
gd> not verify. Specifically, I am interested in a dissection of the
gd> regular expression
ecm> (when (looking-at "[A-Z]\\{3\\}[0-9]+")
gd> and the conundrum is in \\{3\\}, a construct, which has no
gd> reference in the info page on emacs regular expressions.
my Emacs manual has the following:
,----
| `\{N\}'
| is a postfix operator that specifies repetition N times--that is,
| the preceding regular expression must match exactly N times in a
| row. For example, `x\{4\}' matches the string `xxxx' and nothing
| else.
`----
(You also have to know that in an elisp string, the '\' character is
an escape character, so you have to double it to get a literal '\'.)
gd> You may as well explain the wisdom in the use of
gd>
gd> (save-excursion ; ???
gd> (save-match-data ; ???
gd>
gd> which I fail to see despite seeing description on them.
the save-excursion is to avoid having command execution move point,
which is more polite. The save-match-data is due to me having used a
regexp function (looking-at), which sets the match data (some global
variables that allow functions like match-string to work); it's more
polite to save the match data to avoid changing it in the back of
another function.
(The save-match-data is probably not necessary, since this was inside
a command run from the keyboard, so it's unlikely to interfere with
other functions.)
--
Eric Marsden <URL:http://www.laas.fr/~emarsden/>
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2002-09-19 11:27 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2002-09-16 3:54 Q: How to copy a string in a text into a variable? gnuist
2002-09-16 10:17 ` Eric Marsden
2002-09-18 18:07 ` gnuist
2002-09-19 11:27 ` Eric Marsden
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).