views:

129

answers:

3

Some lanaguages, like Perl, support printing preformatted code:

print <<EOL
  a line
  another line
  and another.
EOL


Some languages don't.

For the ones that don't, I'd like to be able to write my text and then convert it to a bunch of printfs:

printf "a line\n";
printf "another line\n";
printf "and another\n";

What's a good way to do this conversion? (preferably in emacs)

+2  A: 
(defun my-add-printf (b e)
  "Add printf's to region"
  (interactive "r")
  (let ((comment-start "\tprintf \"")
        (comment-end "\\n\";"))
    (comment-region b e)))
Trey Jackson
(comment-region b e comment-end)?
kanja
The 3rd argument to comment-region controls the behavior - either commenting (with multiples of comment characters) or un-commenting. I don't know what your suggestion/comment is trying to do.
Trey Jackson
+1  A: 

Unless I had to do this many times a day, I'd probably just do it interactively:

M-x query-replace-regexp RET .* RET printf "\&\\n"; RET
Gareth Rees
+1  A: 

just felt like suggesting another two ways, ensure that cua-mode is enabled then you do C-enter on the start, press down until you reach the last line (same column) and type ' printf "'.

Another is to make a macro. Type in the following:

C-x ( 
printf "
C-e
";
C-a
C-n
C-x )
C-x e
e e e e e e e e ....
James Brooks