unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Michal Nazarewicz <mpn@google.com>
To: emacs-devel@gnu.org, Drew Adams <drew.adams@oracle.com>
Subject: [PATCHv2] Add cycle-spacing command.
Date: Mon, 10 Dec 2012 15:57:45 +0100	[thread overview]
Message-ID: <ed2869c4c949a979e564e627ee41a96306d62969.1355151325.git.mina86@mina86.com> (raw)
In-Reply-To: <xa1tobi930o1.fsf@mina86.com>

From: Michal Nazarewicz <mina86@mina86.com>

This commit adds a cycle-spacing command which is, in a sense,
generalisation of the just-on-space command.  When run continuously,
it cycles between having only one space, having no spaces and original
spacing.
---
 etc/NEWS       |    5 +++
 lisp/ChangeLog |    4 ++
 lisp/simple.el |   86 ++++++++++++++++++++++++++++++++++++++++++++++----------
 3 files changed, 80 insertions(+), 15 deletions(-)

 v2: changed name to cycle-spacing

diff --git a/etc/NEWS b/etc/NEWS
index 77e7e47..8112573 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -92,6 +92,11 @@ when its arg ADJACENT is non-nil (when called interactively with C-u C-u)
 it works like the utility `uniq'.  Otherwise by default it deletes
 duplicate lines everywhere in the region without regard to adjacency.
 
+** New `cycle-spacing' command allows cycling between having just one
+space, no spaces, or reverting to the original spacing.  Like
+`just-one-space' command it can handle or ignore newlines and use
+leave different number of spaces.
+
 ** Tramp
 +++
 *** New connection method "adb", which allows to access Android
diff --git a/lisp/ChangeLog b/lisp/ChangeLog
index 7f05613..7ac779a 100644
--- a/lisp/ChangeLog
+++ b/lisp/ChangeLog
@@ -1,3 +1,7 @@
+2012-12-10  Michal Nazarewicz  <mina86@mina86.com>
+
+	* simple.el: Add cycle-spacing command.
+
 2012-12-10  Eli Zaretskii  <eliz@gnu.org>
 
 	* subr.el (w32notify-handle-event): New function.
diff --git a/lisp/simple.el b/lisp/simple.el
index 78b7657..dcb8cf7 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -742,25 +742,81 @@ If BACKWARD-ONLY is non-nil, only delete them before point."
        (skip-chars-backward " \t")
        (constrain-to-field nil orig-pos)))))
 
+(defvar cycle-spacing--context nil
+  "Store context used in consecutive calls to `cycle-spacing' command.
+The first time this function is run, it saves the original point
+position and original spacing around the point in this
+variable.")
+
+(defun cycle-spacing (&optional n preserve-nl-back single-shot)
+  "Manipulate spaces around the point in a smart way.
+
+When run as an interactive command, the first time it's called
+in a sequence, deletes all spaces and tabs around point leaving
+one (or N spaces).  If this does not change content of the
+buffer, skips to the second step:
+
+When run for the second time in a sequence, deletes all the
+spaces it has previously inserted.
+
+When run for the third time, returns the whitespace and point in
+a state encountered when it had been run for the first time.
+
+For example, if buffer contains \"foo ^ bar\" with \"^\" donating the
+point, calling `cycle-spacing' command will replace two spaces with
+a single space, calling it again immediately after, will remove all
+spaces, and calling it for the third time will bring two spaces back
+together.
+
+If N is negative, delete newlines as well.  However, if
+PRESERVE-NL-BACK is t new line characters prior to the point
+won't be removed.
+
+If SINGLE-SHOT is non-nil, will only perform the first step.  In
+other words, it will work just like `just-on-space' command."
+  (interactive "*p")
+  (let ((orig-pos	 (point))
+	(skip-characters (if (and n (< n 0)) " \t\n\r" " \t"))
+	(n		 (abs (or n 1))))
+    (skip-chars-backward (if preserve-nl-back " \t" skip-characters))
+    (constrain-to-field nil orig-pos)
+    (cond
+     ;; Command run for the first time or single-shot is non-nil
+     ((or single-shot
+	  (not (equal last-command this-command))
+	  (not cycle-spacing--context))
+      (let* ((start (point))
+	     (n	    (- n (skip-chars-forward " " (+ n (point)))))
+	     (mid   (point))
+	     (end   (progn
+		      (skip-chars-forward skip-characters)
+		      (constrain-to-field nil orig-pos t))))
+	(setq cycle-spacing--context  ;; Save for later
+	      ;; Special handling for case where there was no space at all
+	      (unless (= start end)
+		(cons orig-pos (buffer-substring start (point)))))
+	;; If this run causes no change in buffer content, delete all spaces,
+	;; otherwise delete all excees spaces.
+	(delete-region (if (and (not single-shot) (zerop n) (= mid end))
+			   start mid) end)
+	(dotimes (_ n)
+	  (insert ?\s))))
+
+     ;; Command run for the second time
+     ((not (equal orig-pos (point)))
+      (delete-region (point) orig-pos))
+
+     ;; Command run for the third time
+     (t
+      (insert (cdr cycle-spacing--context))
+      (goto-char (car cycle-spacing--context))
+      (setq cycle-spacing--context nil)))))
+
 (defun just-one-space (&optional n)
   "Delete all spaces and tabs around point, leaving one space (or N spaces).
 If N is negative, delete newlines as well, leaving -N spaces."
   (interactive "*p")
-  (unless n (setq n 1))
-  (let ((orig-pos (point))
-        (skip-characters (if (< n 0) " \t\n\r" " \t"))
-        (n (abs n)))
-    (skip-chars-backward skip-characters)
-    (constrain-to-field nil orig-pos)
-    (dotimes (i n)
-      (if (= (following-char) ?\s)
-	  (forward-char 1)
-	(insert ?\s)))
-    (delete-region
-     (point)
-     (progn
-       (skip-chars-forward skip-characters)
-       (constrain-to-field nil orig-pos t)))))
+  (cycle-spacing n nil t))
 \f
 (defun beginning-of-buffer (&optional arg)
   "Move point to the beginning of the buffer.
-- 
1.7.7.3




  parent reply	other threads:[~2012-12-10 14:57 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-12-04 22:36 [PATCH] Add smart-space command Michal Nazarewicz
2012-12-04 22:55 ` Drew Adams
2012-12-04 23:11   ` Michal Nazarewicz
2012-12-10 14:57 ` Michal Nazarewicz [this message]
2013-01-17 11:41   ` [PATCHv3] Add cycle-spacing command Michal Nazarewicz
2013-01-17 13:06     ` Lele Gaifax
2013-01-26 15:26       ` [PATCHv4] " Michal Nazarewicz
2013-01-26 17:14         ` Stephen J. Turnbull

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

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ed2869c4c949a979e564e627ee41a96306d62969.1355151325.git.mina86@mina86.com \
    --to=mpn@google.com \
    --cc=drew.adams@oracle.com \
    --cc=emacs-devel@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 public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

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).