From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Eric Abrahamsen Newsgroups: gmane.emacs.help Subject: Re: yank without indentation? Date: Fri, 06 Jun 2014 08:40:02 +0800 Message-ID: <8738fivpjx.fsf@ericabrahamsen.net> References: <87oay7v179.fsf@ericabrahamsen.net> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: ger.gmane.org 1402015252 4203 80.91.229.3 (6 Jun 2014 00:40:52 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Fri, 6 Jun 2014 00:40:52 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Fri Jun 06 02:40:44 2014 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1WsiD6-0004PP-0O for geh-help-gnu-emacs@m.gmane.org; Fri, 06 Jun 2014 02:40:44 +0200 Original-Received: from localhost ([::1]:44457 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WsiD5-0001dB-J9 for geh-help-gnu-emacs@m.gmane.org; Thu, 05 Jun 2014 20:40:43 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:49099) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WsiCd-0001U1-1t for help-gnu-emacs@gnu.org; Thu, 05 Jun 2014 20:40:20 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WsiCX-00009R-E3 for help-gnu-emacs@gnu.org; Thu, 05 Jun 2014 20:40:14 -0400 Original-Received: from plane.gmane.org ([80.91.229.3]:35008) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WsiCX-00009M-7q for help-gnu-emacs@gnu.org; Thu, 05 Jun 2014 20:40:09 -0400 Original-Received: from list by plane.gmane.org with local (Exim 4.69) (envelope-from ) id 1WsiCR-00040K-Se for help-gnu-emacs@gnu.org; Fri, 06 Jun 2014 02:40:03 +0200 Original-Received: from 111.197.153.25 ([111.197.153.25]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Fri, 06 Jun 2014 02:40:03 +0200 Original-Received: from eric by 111.197.153.25 with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Fri, 06 Jun 2014 02:40:03 +0200 X-Injected-Via-Gmane: http://gmane.org/ Original-Lines: 53 Original-X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: 111.197.153.25 User-Agent: Gnus/5.130012 (Ma Gnus v0.12) Emacs/24.4.50 (gnu/linux) Cancel-Lock: sha1:+Y+Ul68PDjO6xShbf5cY0uADJJg= X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 80.91.229.3 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:98047 Archived-At: Drew Adams 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