reddit / emacs
How to copy code and prefix it with four spaces
pragmat1c1 · 2022-05-24 04:58
As I am posting questions with code samples on stackoverflow or in this forum, I mark my code, prefix it with 4 spaces, copy it.
I'd like to automate it. This is what I came up with, but it won't work, as I don't know the idiomatic ways to write this in Elisp:
(defun ugt-copy-code ()
"Copy code for forums, prefixing it with 4 spaces."
(interactive)
(save-excursion
(let ((my-prefix " ")
(beg (progn
(backward-paragraph)
(set-mark (point))
(region-beginning)))
(end (progn
(forward-paragraph)
(region-end))))
(kill-ring-save beg end)
(with-temp-buffer
(yank)
(beginning-of-buffer)
(set-mark (point))
(end-of-buffer)
(string-insert-rectangle beg end my-prefix)
(kill-ring-save (region-beginning) (region-end))))))
Any help is appreciated.
flexibeast · 2022-05-24 05:39
Not sure if learning ELisp is part of the exercise, but if it's not, there's [copy-as-format](https://github.com/sshaw/copy-as-format).
Ok-Promise-500 · 2022-05-24 05:41
This works for me
(defun ugt-copy-code ()
"Copy code for forums, prefixing it with 4 spaces."
(interactive)
(save-excursion
(let* ((start (progn (backward-paragraph) (point)))
(end (progn (forward-paragraph) (point)))
(code-string (buffer-substring start end)))
(with-temp-buffer
(save-excursion (insert code-string))
(while (not (eobp))
(insert-char ?\s 4)
(forward-line))
(kill-new (buffer-string))
(message "Copied paragraph to the kill ring.")))))
pragmat1c1 · 2022-05-24 06:10
Excellent, thank you very much.
pragmat1c1 · 2022-05-24 06:10
Thank you very much.
vifon · 2022-05-24 11:45
That should do the trick: `C-y C-x C-x C-u C-x TAB`
Basically yank, mark the yanked region, and then use `indent-rigidly` with C-u (aka 4, in this scenario) as its prefix.
oantolin · 2022-05-24 14:17
That `C-TAB` should be either `TAB` or `C-i`, I think you were undecided between them and went with a mix of the two. :P
Also, the `C-x C-x` is unnecessary: the region stays exactly the same whether you swap point and mark or not! The only effect that has, is that if you use `transient-mark-mode`, then the `C-x C-x` will highlight the region, but other than eye candy it is useless here.
MangyCanine · 2022-05-24 14:55
Whenever I need to “indent” some amount of space, I just use `open-rectangle` (this requires that the text-to-be-indented not contain any tabs).
vifon · 2022-05-24 14:58
Oh, I meant `C-x TAB`. :D
EDIT: As for the second part: I wasn't sure whether `indent-rigidly` behaves differently with the transient-mark active, so I included it. Good catch! Usually I'm just as picky about people marking regions for no reason.
oantolin · 2022-05-24 16:24
I generally prefer `TAB` to `C-i`, but this is an exception, I like typing `C-u C-x C-i`, and in my head pronounce it "Control Uxy".
redguardtoo · 2022-05-25 10:17
https://github.com/redguardtoo/emacs.d/blob/master/lisp/init-clipboard.el#L46
See `copy-to-x-clipboard`.
Use mature third party distribution or read their code.
simbledress · 2022-05-28 11:30
> (this requires that the text-to-be-indented not contain any tabs)
Also: `M-x untabify`.