From: Eric Abrahamsen <eric@ericabrahamsen.net>
To: help-gnu-emacs@gnu.org
Subject: Re: yank without indentation?
Date: Fri, 06 Jun 2014 08:40:02 +0800 [thread overview]
Message-ID: <8738fivpjx.fsf@ericabrahamsen.net> (raw)
In-Reply-To: db812a7c-f598-4918-bfe6-23316706af79@default
Drew Adams <drew.adams@oracle.com> writes:
>> Has anyone come up with a yank-without-indentation routine? Why am I
>> always killing multi-line text with enormous left-hand whitespace, and
>> wanting to yank it without that whitespace in place? Has anyone dealt
>> with this?
>
> This should do pretty much what you want.
>
> (defun yank-sans-indent (&optional arg)
> "Like `yank', but remove indentation."
> (interactive "*P")
> (setq yank-window-start (window-start)
> this-command t)
> (push-mark (point))
> (insert-for-yank (string-trim (current-kill (cond ((listp arg) 0)
> ((eq arg '-) -2)
> (t (1- arg))))))
> (when (consp arg)
> (goto-char (prog1 (mark t)
> (set-marker (mark-marker) (point) (current-buffer)))))
> (when (eq this-command t) (setq this-command 'yank-sans-indent))
> nil)
>
> (defun string-trim (string)
> "Trim SPC and TAB chars from ends of STRING and each of its eols."
> (let* ((start (progn (string-match "\\`[ \t]*" string)
> (match-end 0)))
> (end (progn (string-match "[ \t]*\\'" string start)
> (match-beginning 0)))
> (trimmed (substring string start end)))
> (replace-regexp-in-string "\\(\n\\)[ \t]+" "\\1" trimmed)))
>
> (global-set-key "\C-y" 'yank-sans-indent)
>
> (You can also bind `M-y' to a command that is `yank-pop' but with
> references to `yank' replaced by `yank-sans-indent'.)
>
> [If you want the string that is placed on the `kill-ring' to be the
> complete selected text, instead of that text trimmed to remove
> indentation, then you can use an alternative approach of placing
> a `yank-handler' property on the first character, and define a
> yank handler that trims the indenting whitespace at the time of
> insertion.]
Whoa, that was pretty intense -- I didn't know anything about yank
handlers or insert-for-yank, etc. As usual, what seems to be a
straightforward operation has a lot of stuff going on in the background!
Thanks a lot for this code, I'll probably end up using it or something
like it, though I'm also going to go learn about yank handlers first.
Thanks again,
E
next prev parent reply other threads:[~2014-06-06 0:40 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-06-05 15:13 yank without indentation? Eric Abrahamsen
2014-06-05 15:18 ` Thorsten Jolitz
2014-06-05 15:31 ` Nicolas Richard
2014-06-06 0:36 ` Eric Abrahamsen
2014-06-05 19:19 ` Grant Rettke
2014-06-05 19:25 ` Jacob Gerlach
2014-06-05 22:26 ` Robert Thorpe
2014-06-06 6:27 ` Eli Zaretskii
2014-06-05 23:29 ` Drew Adams
2014-06-06 0:40 ` Eric Abrahamsen [this message]
2014-06-10 0:00 ` Ken Goldman
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=8738fivpjx.fsf@ericabrahamsen.net \
--to=eric@ericabrahamsen.net \
--cc=help-gnu-emacs@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.