Your question isn't a great one for this site. Instead of "Please debug this for me" you should ask a simple, specific how-to question.
Anyway, this will get you started.
(defun myconcat(start end)
"Split region on `,' chars and concatenate the substrings."
(interactive "r")
(let* ((x (buffer-substring start end))
(final "")
(y (split-string x ",")))
(while y
(setq final (concat final (car y))
y (cdr y)))
(message "FINAL: %S" final)
(delete-region start end)
(insert final)
final))
I'm assuming you really want to replace the active region (the selected text), not the whole buffer. You say "selected buffer", which doesn't mean anything (unless the whole buffer is selected).
If you really want to replace the whole buffer by the selected text with its ,
chars removed, then just replace (delete start end)
by (delete (point-min) (point-max))
in the code here.
Things to note:
You never defined y
. Presumably your vars list
and y
were meant to be the same.
No need for progn
. The body of a function is an implicit progn
.
Don't use global vars (especially undeclared - no defvar
). Use let
or let*
.
Use the interactive
spec to give you the region limits.
split-string
returns a list of substrings. You need to iterate over that list, which means provide a substring to concat
in each iteration.
Your loop never diminished the list of substrings.
You never deleted the original region or inserted the calculated text.
This is a bit better - the above tried to retain as much as possible of your approach, so you could more easily see the fixes.
(defun myconcat(start end)
"Split region on `,' chars and concatenate the substrings."
(interactive "r")
(let ((final ""))
(dolist (str (split-string (buffer-substring start end) ","))
(setq final (concat final str)))
(message "FINAL: %S" final)
(delete-region start end)
(insert final)
final))
The answer above was accepted. But if what's wanted is really just to remove the comma chars (,
) in the region then there are better ways. Here's one:
(defun remove-commas-in-region (start end)
"Remove the commas in the region."
(interactive "r")
(save-excursion
(save-restriction
(narrow-to-region start end)
(goto-char 1)
(while (search-forward "," nil t) (replace-match "")))))
C-h i g(emacs)
.