0

I have written the following lisp code to search for a regex pattern and append new line to it:

(defun readmode ()
  (interactive)
  (goto-char 1)
  (while (search-forward-regex "\(</[a-z]+>\)" nil t) (replace-match "\1C-q C-j" t nil)))
(global-set-key (kbd "C-m") 'readmode)        

When I run, I am getting error:

command-execute: Wrong type argument: commandp, readmode

Input text:
<pre> Line 1 </pre> <p> Line 2 </p>

Expected text:
<pre> Line 1 </pre>
<p> Line 2 </p>
  • Wrong type argument: commandp, readmode suggests that you initially wrote and evaluated the function definition without the (interactive) declaration, and have not then re-evaluated it since adding it. – phils Oct 17 '19 at 10:17
  • You also have a typo, as search-forward-regexp has that trailing p. Note that's an alias for re-search-forward (which is the more common form). – phils Oct 17 '19 at 10:17
  • I corrected the typo, executed it by C-x C-e and restarted emacs and now when I run nothing is happening. In the Messages buffer, I see nothing. – emacsbeginner Oct 17 '19 at 11:38
  • The question should now be closed as unclear. You posed a question. It was answered in a comment. You posed another question in a comment, etc. That's not how this site works. The question could be kept open if the answer in a comment were moved to an answer (missing interactive spec). But in that case the question is a duplicate. – Drew Oct 17 '19 at 15:59
  • 1
    The regex is wrong as to both usages: As to the first, you need two (2) backslashes in front of each parenthesis. As to the second, a new line is specified with a single backslash followed by a lowercase n; i.e., \n -- we do not use a keyboard shortcut spelled out here. As to replace-match, I was unable to figure how to use the \N, in this case \1 .... to get it work -- (concat (match-string 1) "\n"). And, further, I had to modify your first regex to add a \s outside of the grouping because there is a space between </pre> <p> which needs to be eliminated programmatically. – lawlist Oct 17 '19 at 17:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.