* yank without indentation? @ 2014-06-05 15:13 Eric Abrahamsen 2014-06-05 15:18 ` Thorsten Jolitz ` (2 more replies) 0 siblings, 3 replies; 11+ messages in thread From: Eric Abrahamsen @ 2014-06-05 15:13 UTC (permalink / raw) To: help-gnu-emacs 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? E ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: yank without indentation? 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-05 19:19 ` Grant Rettke 2014-06-05 19:25 ` Jacob Gerlach 2 siblings, 1 reply; 11+ messages in thread From: Thorsten Jolitz @ 2014-06-05 15:18 UTC (permalink / raw) To: help-gnu-emacs Eric Abrahamsen <eric@ericabrahamsen.net> 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? I have 3 chomp function loaded right now: ,---------------- | org-babel-chomp | outshine-chomp | s-chomp `---------------- and outshine-chomp seems to be the only one that handles leading whitespace to. You could write your own chomp-and-yank function based on these, but I'm sure more than one exists out there ... -- cheers, Thorsten ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: yank without indentation? 2014-06-05 15:18 ` Thorsten Jolitz @ 2014-06-05 15:31 ` Nicolas Richard 2014-06-06 0:36 ` Eric Abrahamsen 0 siblings, 1 reply; 11+ messages in thread From: Nicolas Richard @ 2014-06-05 15:31 UTC (permalink / raw) To: Thorsten Jolitz; +Cc: help-gnu-emacs Thorsten Jolitz <tjolitz@gmail.com> writes: > Eric Abrahamsen <eric@ericabrahamsen.net> 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? > > I have 3 chomp function loaded right now: > > ,---------------- > | org-babel-chomp > | outshine-chomp > | s-chomp > `---------------- > > and outshine-chomp seems to be the only one that handles leading > whitespace to. That's because removing both leading and trailing whitespace is usually called trim :) So you have org-trim, s-trim, ... OTOH, I think the question is not only about leading space, but also about space after newline chars. Does your function handle them ? FWIW, if I had to write such a function, I would do as follows : if the first line is indented, remove that amount of space on every line. If the second line is indented, remove that amount of space on every line (except the first). (And eventually yank.) The reason for checking the 2nd line is that I often have text like: > <prompt> bla bla bla > bla bla bla > bla bla bla and I kill the text from the end of the prompt to the end of the "bla bla". Yanking it then gives: > bla bla bla > bla bla bla > bla bla bla which is ugly. Not sure if that's what Eric had in mind though. -- Nico. ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: yank without indentation? 2014-06-05 15:31 ` Nicolas Richard @ 2014-06-06 0:36 ` Eric Abrahamsen 0 siblings, 0 replies; 11+ messages in thread From: Eric Abrahamsen @ 2014-06-06 0:36 UTC (permalink / raw) To: help-gnu-emacs Nicolas Richard <theonewiththeevillook@yahoo.fr> writes: > Thorsten Jolitz <tjolitz@gmail.com> writes: > >> Eric Abrahamsen <eric@ericabrahamsen.net> 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? >> >> I have 3 chomp function loaded right now: >> >> ,---------------- >> | org-babel-chomp >> | outshine-chomp >> | s-chomp >> `---------------- >> >> and outshine-chomp seems to be the only one that handles leading >> whitespace to. > > That's because removing both leading and trailing whitespace is usually > called trim :) > So you have org-trim, s-trim, ... > > OTOH, I think the question is not only about leading space, but also > about space after newline chars. Does your function handle them ? > > FWIW, if I had to write such a function, I would do as follows : if the > first line is indented, remove that amount of space on every line. If > the second line is indented, remove that amount of space on every line > (except the first). (And eventually yank.) > > The reason for checking the 2nd line is that I often have text like: >> <prompt> bla bla bla >> bla bla bla >> bla bla bla > and I kill the text from the end of the prompt to the end of the "bla > bla". Yanking it then gives: >> bla bla bla >> bla bla bla >> bla bla bla > which is ugly. > > Not sure if that's what Eric had in mind though. That's actually exactly the situation that prompted my question, but I seem to run into similar situations all the time: yanking a multi-line address from a BBDB record, copying a few sexps out of the middle of a deeply-indented function and yanking them into an email, etc. I'm going to give Drew's solution test-drive, but I also like your idea of "cascading" indentation deletion. Thanks, Eric ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: yank without indentation? 2014-06-05 15:13 yank without indentation? Eric Abrahamsen 2014-06-05 15:18 ` Thorsten Jolitz @ 2014-06-05 19:19 ` Grant Rettke 2014-06-05 19:25 ` Jacob Gerlach 2 siblings, 0 replies; 11+ messages in thread From: Grant Rettke @ 2014-06-05 19:19 UTC (permalink / raw) To: Eric Abrahamsen; +Cc: Emacs Help Please provide a simple and illustrative example. Sorry I'm not sure what you are getting at. Grant Rettke | AAAS, ACM, ASA, FSF, IEEE, SIAM, Sigma Xi gcr@wisdomandwonder.com | http://www.wisdomandwonder.com/ “Wisdom begins in wonder.” --Socrates ((λ (x) (x x)) (λ (x) (x x))) “Life has become immeasurably better since I have been forced to stop taking it seriously.” --Thompson On Thu, Jun 5, 2014 at 10:13 AM, Eric Abrahamsen <eric@ericabrahamsen.net> wrote: > 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? > > E > > > ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: yank without indentation? 2014-06-05 15:13 yank without indentation? Eric Abrahamsen 2014-06-05 15:18 ` Thorsten Jolitz 2014-06-05 19:19 ` Grant Rettke @ 2014-06-05 19:25 ` Jacob Gerlach 2014-06-05 22:26 ` Robert Thorpe ` (2 more replies) 2 siblings, 3 replies; 11+ messages in thread From: Jacob Gerlach @ 2014-06-05 19:25 UTC (permalink / raw) To: Eric Abrahamsen; +Cc: help-gnu-emacs I run in to this problem sometimes when yanking a piece of code with certain indentation into a new place where it should have different (more or less) indentation. My solution is: C-y C-x C-x (exchange-point-and-mark) to re-highlight the yanked text <TAB> to properly indent it for the new contexxt On Thu, Jun 5, 2014 at 11:13 AM, Eric Abrahamsen <eric@ericabrahamsen.net> wrote: > 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? > > E > > > ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: yank without indentation? 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-10 0:00 ` Ken Goldman 2 siblings, 1 reply; 11+ messages in thread From: Robert Thorpe @ 2014-06-05 22:26 UTC (permalink / raw) To: Jacob Gerlach; +Cc: eric, help-gnu-emacs Jacob Gerlach <jacobgerlach@gmail.com> writes: > I run in to this problem sometimes when yanking a piece of code with > certain indentation into a new place where it should have different (more > or less) indentation. My solution is: > > C-y > C-x C-x (exchange-point-and-mark) to re-highlight the yanked text > <TAB> to properly indent it for the new contexxt That's what I do. Another possibility is using the region rectange commands C-x r k and C-x r y. BR, Robert Thorpe ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: yank without indentation? 2014-06-05 22:26 ` Robert Thorpe @ 2014-06-06 6:27 ` Eli Zaretskii 0 siblings, 0 replies; 11+ messages in thread From: Eli Zaretskii @ 2014-06-06 6:27 UTC (permalink / raw) To: help-gnu-emacs > From: Robert Thorpe <rt@robertthorpeconsulting.com> > Date: Thu, 05 Jun 2014 23:26:39 +0100 > Cc: eric@ericabrahamsen.net, help-gnu-emacs@gnu.org > > Jacob Gerlach <jacobgerlach@gmail.com> writes: > > > I run in to this problem sometimes when yanking a piece of code with > > certain indentation into a new place where it should have different (more > > or less) indentation. My solution is: > > > > C-y > > C-x C-x (exchange-point-and-mark) to re-highlight the yanked text > > <TAB> to properly indent it for the new contexxt > > That's what I do. This is shorter: "C-y C-M-\". And if you need to change the indentation, "C-y C-u 2 C-x TAB" (in this case will increase indentation by 2 columns, but the number 2 is just an example). ^ permalink raw reply [flat|nested] 11+ messages in thread
* RE: yank without indentation? 2014-06-05 19:25 ` Jacob Gerlach 2014-06-05 22:26 ` Robert Thorpe @ 2014-06-05 23:29 ` Drew Adams 2014-06-06 0:40 ` Eric Abrahamsen 2014-06-10 0:00 ` Ken Goldman 2 siblings, 1 reply; 11+ messages in thread From: Drew Adams @ 2014-06-05 23:29 UTC (permalink / raw) To: Jacob Gerlach, Eric Abrahamsen; +Cc: help-gnu-emacs > 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.] ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: yank without indentation? 2014-06-05 23:29 ` Drew Adams @ 2014-06-06 0:40 ` Eric Abrahamsen 0 siblings, 0 replies; 11+ messages in thread From: Eric Abrahamsen @ 2014-06-06 0:40 UTC (permalink / raw) To: help-gnu-emacs 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 ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: yank without indentation? 2014-06-05 19:25 ` Jacob Gerlach 2014-06-05 22:26 ` Robert Thorpe 2014-06-05 23:29 ` Drew Adams @ 2014-06-10 0:00 ` Ken Goldman 2 siblings, 0 replies; 11+ messages in thread From: Ken Goldman @ 2014-06-10 0:00 UTC (permalink / raw) To: help-gnu-emacs How about an automated version? I've been using this: ; automatically indenting yanked text if in programming-modes (defadvice yank (after indent-region activate) (if (member major-mode '(emacs-lisp-mode c-mode c++-mode tcl-mode sql-mode perl-mode cperl-mode java-mode jde-mode LaTeX-mode TeX-mode)) (indent-region (region-beginning) (region-end) nil))) On 6/5/2014 3:25 PM, Jacob Gerlach wrote: > I run in to this problem sometimes when yanking a piece of code with > certain indentation into a new place where it should have different (more > or less) indentation. My solution is: > > C-y > C-x C-x (exchange-point-and-mark) to re-highlight the yanked text > <TAB> to properly indent it for the new contexxt ^ permalink raw reply [flat|nested] 11+ messages in thread
end of thread, other threads:[~2014-06-10 0:00 UTC | newest] Thread overview: 11+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 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 2014-06-10 0:00 ` Ken Goldman
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).