all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* replace-regexp in lisp function
@ 2010-01-18  9:29 Sven Garbade
  0 siblings, 0 replies; 5+ messages in thread
From: Sven Garbade @ 2010-01-18  9:29 UTC (permalink / raw)
  To: help-gnu-emacs

Hallo List,

I frequently use the following regular-regexp:

M-x replace-regexp <RET> \(<<\) <RET> \1cn\,(+ (line-number-at-pos) \#)

to replace "<<" with "<<cn=line number". I tried to write a function

;; add chunk number
(defun add-chunk-number ()
  (interactive)
  (query-replace-regexp "\(<<\)" "\1cn\,(+ (line-number-at-pos) \#)")
  )

to save typing, but this does not work. What did I wrong?

Thanks, Sven




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: replace-regexp in lisp function
       [not found] <mailman.1632.1263815405.18930.help-gnu-emacs@gnu.org>
@ 2010-01-18 13:01 ` David Kastrup
  2010-01-18 14:06   ` Sven Garbade
  2010-01-18 13:11 ` Pascal J. Bourguignon
  1 sibling, 1 reply; 5+ messages in thread
From: David Kastrup @ 2010-01-18 13:01 UTC (permalink / raw)
  To: help-gnu-emacs

Sven Garbade <sfgarbade@googlemail.com> writes:

> Hallo List,
>
> I frequently use the following regular-regexp:
>
> M-x replace-regexp <RET> \(<<\) <RET> \1cn\,(+ (line-number-at-pos) \#)
>
> to replace "<<" with "<<cn=line number". I tried to write a function
>
> ;; add chunk number
> (defun add-chunk-number ()
>   (interactive)
>   (query-replace-regexp "\(<<\)" "\1cn\,(+ (line-number-at-pos) \#)")
>   )
>
> to save typing, but this does not work. What did I wrong?

Lisp string syntax needs additional quoting for regexps, and things like
\, and \# are not supported outside of interactive use.

I recommend that you first give your command interactively, then use C-x
Esc Esc to get at the corresponding Lisp verbiage.

-- 
David Kastrup


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: replace-regexp in lisp function
       [not found] <mailman.1632.1263815405.18930.help-gnu-emacs@gnu.org>
  2010-01-18 13:01 ` David Kastrup
@ 2010-01-18 13:11 ` Pascal J. Bourguignon
  1 sibling, 0 replies; 5+ messages in thread
From: Pascal J. Bourguignon @ 2010-01-18 13:11 UTC (permalink / raw)
  To: help-gnu-emacs

Sven Garbade <sfgarbade@googlemail.com> writes:

> Hallo List,
>
> I frequently use the following regular-regexp:
>
> M-x replace-regexp <RET> \(<<\) <RET> \1cn\,(+ (line-number-at-pos) \#)
>
> to replace "<<" with "<<cn=line number". I tried to write a function
>
> ;; add chunk number
> (defun add-chunk-number ()
>   (interactive)
>   (query-replace-regexp "\(<<\)" "\1cn\,(+ (line-number-at-pos) \#)")
>   )
>
> to save typing, but this does not work. What did I wrong?

You don't understand the syntax of strings.

How many characters are there in "\(" ?
How many characters are there in "\n" ?
How many characters are there in "\\(" ?
How many characters are there in "\\n" ?


-- 
__Pascal Bourguignon__                     http://www.informatimago.com/


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: replace-regexp in lisp function
  2010-01-18 13:01 ` David Kastrup
@ 2010-01-18 14:06   ` Sven Garbade
  2010-01-18 14:11     ` David Kastrup
  0 siblings, 1 reply; 5+ messages in thread
From: Sven Garbade @ 2010-01-18 14:06 UTC (permalink / raw)
  To: David Kastrup; +Cc: help-gnu-emacs

Hi,

many thanks. C-x Esc Esc gave me:

  (query-replace-regexp "\\(<<\\)"
			(quote (replace-eval-replacement concat "\\1cn" (replace-quote (+
(line-number-at-pos) replace-count)) ",")) nil
			(if (and transient-mark-mode mark-active) (region-beginning))
			(if (and transient-mark-mode mark-active) (region-end)))

which works inside my function. Looks quite complicate for me...

Are there any recommended documents / books for an elisp newbie?

Thanks, Sven




2010/1/18 David Kastrup <dak@gnu.org>:
> Sven Garbade <sfgarbade@googlemail.com> writes:
>
>> Hallo List,
>>
>> I frequently use the following regular-regexp:
>>
>> M-x replace-regexp <RET> \(<<\) <RET> \1cn\,(+ (line-number-at-pos) \#)
>>
>> to replace "<<" with "<<cn=line number". I tried to write a function
>>
>> ;; add chunk number
>> (defun add-chunk-number ()
>>   (interactive)
>>   (query-replace-regexp "\(<<\)" "\1cn\,(+ (line-number-at-pos) \#)")
>>   )
>>
>> to save typing, but this does not work. What did I wrong?
>
> Lisp string syntax needs additional quoting for regexps, and things like
> \, and \# are not supported outside of interactive use.
>
> I recommend that you first give your command interactively, then use C-x
> Esc Esc to get at the corresponding Lisp verbiage.
>
> --
> David Kastrup
>




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: replace-regexp in lisp function
  2010-01-18 14:06   ` Sven Garbade
@ 2010-01-18 14:11     ` David Kastrup
  0 siblings, 0 replies; 5+ messages in thread
From: David Kastrup @ 2010-01-18 14:11 UTC (permalink / raw)
  To: Sven Garbade; +Cc: help-gnu-emacs

Sven Garbade <sfgarbade@googlemail.com> writes:

> Hi,
>
> many thanks. C-x Esc Esc gave me:
>
>   (query-replace-regexp "\\(<<\\)"
> 			(quote (replace-eval-replacement concat "\\1cn" (replace-quote (+
> (line-number-at-pos) replace-count)) ",")) nil
> 			(if (and transient-mark-mode mark-active) (region-beginning))
> 			(if (and transient-mark-mode mark-active) (region-end)))
>
> which works inside my function. Looks quite complicate for me...

Depending on the application, the expression (if (and
transient-mark-mode mark-active) (region-beginning)) and its cousin can
get replaced by known values, such as (point-min) and (point-max).

-- 
David Kastrup




^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2010-01-18 14:11 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-01-18  9:29 replace-regexp in lisp function Sven Garbade
     [not found] <mailman.1632.1263815405.18930.help-gnu-emacs@gnu.org>
2010-01-18 13:01 ` David Kastrup
2010-01-18 14:06   ` Sven Garbade
2010-01-18 14:11     ` David Kastrup
2010-01-18 13:11 ` Pascal J. Bourguignon

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.