help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Q: How to copy a string in a text into a variable?


From: Eric Marsden
Subject: Re: Q: How to copy a string in a text into a variable?
Date: Mon, 16 Sep 2002 12:17:09 +0200
User-agent: Gnus/5.090004 (Oort Gnus v0.04) Emacs/21.2

>>>>> "gd" == gnuist  <address@hidden> 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/>


reply via email to

[Prev in Thread] Current Thread [Next in Thread]