all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Toomas Rosin <toomas@rosin.ee>
To: 15821@debbugs.gnu.org
Subject: bug#15821: a better M-SPC
Date: Wed, 06 Nov 2013 11:36:55 +0200	[thread overview]
Message-ID: <2866409.xsAelUrJXd@toomas> (raw)

Hello,

I hope this is the right place for the following suggestion.

My version of M-SPC is an improvement over the original Emacs one in two respects: it operates both horizontally and vertically, and it takes a numeric argument.  See the docstring for details.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun adjust-space-2d (&optional arg)
  "Adjust horizontal or vertical whitespace.

On a non-empty blank line, leave exactly ARG spaces on it (without
ARG, make it empty).

On an empty line, delete all blank lines above and below it, leaving
ARG empty lines.  When there is no ARG, the default is to leave no
empty lines at the beginning or the end of the buffer or at a single
blank line, and leave one empty line elsewhere.

At the beginning or end of a non-blank line, delete all leading
resp. trailing whitespace, leaving ARG (by default, zero) spaces.

Elsewhere, insert and/or delete blanks, leaving ARG spaces (by
default, one space)."
  
  (interactive "*P")
  (let ((num (if arg (prefix-numeric-value arg)))
        (pos (point)))
    (cond
     ;;; Empty line at the beginning of the buffer.
     ((and (bobp) (looking-at "$"))
      (delete-blank-lines)
      (if (looking-at "[ \t]*$") (kill-line))
      (when num (open-line num)))
     ;;; Empty line elsewhere.
     ((save-excursion (beginning-of-line) (looking-at "$"))
      (delete-blank-lines)
      (when num (delete-blank-lines) (open-line num)))
     ;;; Beginning of line.
     ((save-excursion (skip-chars-backward " \t") (bolp))
      (beginning-of-line)
      (while (looking-at "[ \t]") (delete-char 1))
      (when num (insert (make-string num ? )) (beginning-of-line)))
     ;;; End of line.
     ((save-excursion (skip-chars-forward " \t") (eolp))
      (skip-chars-backward " \t")
      (while (not (eolp)) (delete-char 1))
      (when num (insert (make-string num ? ))))
     ;;; Anywhere else.
     (t
      (just-one-space)
      (when num (delete-char -1) (insert (make-string num ? )))))))

(global-set-key (kbd "M-SPC") 'adjust-space-2d)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Best regards,
T.






             reply	other threads:[~2013-11-06  9:36 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-11-06  9:36 Toomas Rosin [this message]
2013-11-16 21:42 ` bug#15821: a better M-SPC Stefan Monnier
2020-01-21  1:20   ` Stefan Kangas
2020-01-21  9:24     ` Andreas Schwab
2020-01-22  3:13     ` Richard Stallman
2020-02-28 23:21     ` Stefan Kangas

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=2866409.xsAelUrJXd@toomas \
    --to=toomas@rosin.ee \
    --cc=15821@debbugs.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.