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

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

Re: help with regexp function


From: Kendall Shaw
Subject: Re: help with regexp function
Date: Wed, 22 Nov 2017 09:05:53 -0800
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.4.0

On 11/21/2017 03:30 PM, B. T. Raven wrote:
Dear Emacs gurus:

I can perform this inteactive substitution
CM-%: \(^[0-9]+ \)\(.+\) -> \2 \1)
in order to change a buffer line prefixed with a number into one post-fixed with the same number but I can't figue out how to do the same programatically to a whole region. I started with this code:

(defun verse-num-move-beg-to-end (beg end)
"Move int-string and following space from beginning of line to end of line throughout region."
(interactive "r")
(goto-char beg)
(while (<= (point) end)
   (re-search-forward "^[0-9]+ ")
   (setq num (substring (match-string 0) 0 -1)) ;; should be a string of ;;digits without trailing space
   (print num)

;; here the value generates a wrong argument error:
setq: Wrong type argument: listp, #("234" 0 3 (fontified t))
(type-of  #("234" 0 3 (fontified t)))

;; I have a function which is a black box to to me but it works in the larger context I have it in. Does match-string do something like this implicitly (casting a list as a string?)
...
(substring (match-string 0) 0 -1)
(replace-match "" nil t)


234 asentuhasneothu ;; example buffer-line

re-search-forward signals an error if the search string isn't matched. I don't know if that could explain the error you are seeing, but this would prevent a non-local exit:

(cond
  ((re-search-forward "^[0-9]+ " nil t)
   (setq num ...)
   (print num))
  (t
   (goto-char end)))

Kendall






reply via email to

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