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

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

How to prepare a regexp as arg for a funcall?


From: Thorsten Jolitz
Subject: How to prepare a regexp as arg for a funcall?
Date: Fri, 22 Feb 2013 18:38:58 +0100
User-agent: Gnus/5.130002 (Ma Gnus v0.2) Emacs/24.2 (gnu/linux)


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





reply via email to

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