1

Given the following buffer:

foo bar

    nee nope

If I place point on the 2nd line and execute

(just-one-space -1)

the result will be:

foo bar nee nope

Is there a way to modify the behavior such that the newline after 'bar' will be kept, e.g. with this result:

foo bar
neee nope

?

Or do I have to write my own function for this?

Thanks in advance, Tom

Tom
  • 45
  • 5

2 Answers2

1

The command delete-blank-lines (bound to C-x C-o by default) does what you want, at least in this case. Of course, it doesn't act like just-one-space in other cases, so you may want to write a function that dispatches appropriately for your preferences.

Aaron Harris
  • 2,684
  • 17
  • 22
  • delete-blank-lines will not remove the leading whitespace, so you still need a couple commands (e.g. delete-blank-lines and delete-horizontal-space). In the OP's specific example I'd probably put point at the beginning ofneee and hit C-^. – glucas May 18 '16 at 20:57
  • Thanks for the answer. I wrote something myself, see updated question above. – Tom May 19 '16 at 11:19
0

Here's what I came up with so far:

  (defun viking--kill-space()
  "Kill space around point, including newline(s), but the first."
  (interactive)
  (let* ((lineA 0) (lineB 0)
         (beg (save-excursion
                (skip-chars-backward " \t\r\n")
                (cond ((looking-at "\r\n")
                       (forward-char 2))
                      ((looking-at "\n")
                       (forward-char 1)))
                (setq lineA (line-number-at-pos))
                (point)))
         (end (save-excursion
                (skip-chars-forward " \t\r\n")
                (setq lineB (line-number-at-pos))
                (point)))
         )
    (viking--blink-region beg end)
    (if (= lineA lineB)
        (just-one-space)
      (delete-region beg end))
    (message "spaces cleared")))

This accomplishes the job, it could a little bit more efficient though.

Tom
  • 45
  • 5