[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: How to prepare a regexp as arg for a funcall?
From: |
Barry Margolin |
Subject: |
Re: How to prepare a regexp as arg for a funcall? |
Date: |
Fri, 22 Feb 2013 15:01:15 -0500 |
User-agent: |
MT-NewsWatcher/3.5.3b3 (Intel Mac OS X) |
In article <mailman.20625.1361559810.855.help-gnu-emacs@gnu.org>,
Thorsten Jolitz <tjolitz@gmail.com> wrote:
> Hi List,
>
> I want to use (funcall `(occur ,REGEXP)) in a program, but encounter
> unexpected problems in preparing the regexp with the right numbers of
> '\'.
Forgetting the regexp problem, what do you expect that to do? The first
argument to funcall should be a function, not a list like (occur
whatever).
>
> I have a string that contains one or several "*" which must be escaped
> with '\' before using it as regexp. I would think that
> 'replace-regexp-in-string' is the right tool for this - but no matter
> how many '\' I use, either 'occur or 'replace-regexp-in-string gives me
> an error.
The error in Backtrace 1 has nothing to do with how you fix up the
regexp. It's complaining about what I wrote above: (occur ";; \\* ") is
not a function, so you can't use it as the first argument to funcall.
>
> The attachment below demonstrates the problem, I hope it speaks for
> itself (assume the following is the content of the *scratch* buffer):
>
> ;; * Function
>
> (defun call-occur ()
> (let ((rgxp
> (replace-regexp-in-string
> "*" ;REGEXP
> "\\\\*" ;REP
> ";; * "))) ;STRING
> (funcall `(occur ,rgxp))))
>
> (call-occur)
(defun call-occur ()
(let ((rgxp (regexp-quote ";; * ")))
(occur rgxp)))
>
>
>
> ;; * Backtrace 1 (REP = "\\\\*")
>
> ,-------------------------------------------------------------------
> | Debugger entered--Lisp error: (invalid-function (occur ";; \\* "))
> | (occur ";; \\* ")()
> | funcall((occur ";; \\* "))
> | (let ((rgxp (replace-regexp-in-string "*" "\\\\*" ";;
> | * "))) (funcall (\` (occur (\, rgxp)))))
> | call-occur()
> | [...]
> `-------------------------------------------------------------------
>
>
> ;; * Backtrace 2 (REP = "\\*)
>
> ,-----------------------------------------------------------------------------
> ---
> | Debugger entered--Lisp error: (error "Invalid use of `\\' in replacement
> text")
> | replace-match("\\*" nil nil "*" nil)
> | replace-regexp-in-string("*" "\\*" ";; * ")
> | (let ((rgxp (replace-regexp-in-string "*" "\\*" ";;
> | * "))) (funcall (\` (occur (\, rgxp)))))
> | call-occur()
> | [...]
> `-----------------------------------------------------------------------------
> --
>
> ;; * M-: (occur REGEXP)
>
> in this buffer with REGEXP:
>
> 1. ";; \\\\* "
>
> ,--
> | t
> `--
>
> 2. ";; \\* "
>
> ,--------------------------------------------------------
> | 6 matches for ";; \* " in buffer: *scratch*
> | 1:;; * Function
> | 8: ";; * "))) ;STRING
> | 15:;; * Backtrace 1 (REP = "\\\\*")
> | 28:;; * Backtrace 2 (REP = "\\*)
> | 33:| replace-regexp-in-string("*" "\\*" ";; * ")
> | 40:;; * M-: (occur REGEXP)
> `--------------------------------------------------------
>
> [This is what I want. Why doesn't it work with '(funcall '(occur ..))?]
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***