unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* How to prepare a regexp as arg for a funcall?
@ 2013-02-22 17:38 Thorsten Jolitz
  0 siblings, 0 replies; 3+ messages in thread
From: Thorsten Jolitz @ 2013-02-22 17:38 UTC (permalink / raw)
  To: help-gnu-emacs



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
'\'. 

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 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)



;; * 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 ..))?]


-- 
cheers,
Thorsten





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

* Re: How to prepare a regexp as arg for a funcall?
       [not found] <mailman.20625.1361559810.855.help-gnu-emacs@gnu.org>
@ 2013-02-22 20:01 ` Barry Margolin
  2013-02-23  9:10   ` Thorsten Jolitz
  0 siblings, 1 reply; 3+ messages in thread
From: Barry Margolin @ 2013-02-22 20:01 UTC (permalink / raw)
  To: help-gnu-emacs

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 ***


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

* Re: How to prepare a regexp as arg for a funcall?
  2013-02-22 20:01 ` How to prepare a regexp as arg for a funcall? Barry Margolin
@ 2013-02-23  9:10   ` Thorsten Jolitz
  0 siblings, 0 replies; 3+ messages in thread
From: Thorsten Jolitz @ 2013-02-23  9:10 UTC (permalink / raw)
  To: help-gnu-emacs

Barry Margolin <barmar@alum.mit.edu> writes:

>> 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).  

You are right, I became confused between the list I evaluate and the
actual function involved. 

> (defun call-occur ()
>   (let ((rgxp (regexp-quote ";; * ")))
>     (occur rgxp)))

That solves my problem - thank you!

-- 
cheers,
Thorsten




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

end of thread, other threads:[~2013-02-23  9:10 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <mailman.20625.1361559810.855.help-gnu-emacs@gnu.org>
2013-02-22 20:01 ` How to prepare a regexp as arg for a funcall? Barry Margolin
2013-02-23  9:10   ` Thorsten Jolitz
2013-02-22 17:38 Thorsten Jolitz

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).