aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--etc/NEWS5
-rw-r--r--lisp/ChangeLog5
-rw-r--r--lisp/simple.el83
3 files changed, 79 insertions, 14 deletions
diff --git a/etc/NEWS b/etc/NEWS
index 2584751fe00..2128be01b65 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -163,6 +163,11 @@ when its arg ADJACENT is non-nil (when called interactively with C-u C-u)
163it works like the utility `uniq'. Otherwise by default it deletes 163it works like the utility `uniq'. Otherwise by default it deletes
164duplicate lines everywhere in the region without regard to adjacency. 164duplicate lines everywhere in the region without regard to adjacency.
165 165
166** New `cycle-spacing' command allows cycling between having just one
167space, no spaces, or reverting to the original spacing. Like
168`just-one-space' command it can handle or ignore newlines and
169leave different number of spaces.
170
166** Tramp 171** Tramp
167+++ 172+++
168*** New connection method "adb", which allows to access Android 173*** New connection method "adb", which allows to access Android
diff --git a/lisp/ChangeLog b/lisp/ChangeLog
index fff6e772c42..6ac51724328 100644
--- a/lisp/ChangeLog
+++ b/lisp/ChangeLog
@@ -1,3 +1,8 @@
12013-01-31 Michal Nazarewicz <mina86@mina86.com>
2
3 * simple.el (cycle-spacing): New command.
4 (just-one-space): Use it.
5
12013-01-31 Stefan Monnier <monnier@iro.umontreal.ca> 62013-01-31 Stefan Monnier <monnier@iro.umontreal.ca>
2 7
3 * progmodes/opascal.el: Rename from delphi.el. Use lexical-binding. 8 * progmodes/opascal.el: Rename from delphi.el. Use lexical-binding.
diff --git a/lisp/simple.el b/lisp/simple.el
index 847c07a5c26..68409a098d7 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -746,21 +746,76 @@ If BACKWARD-ONLY is non-nil, only delete them before point."
746 "Delete all spaces and tabs around point, leaving one space (or N spaces). 746 "Delete all spaces and tabs around point, leaving one space (or N spaces).
747If N is negative, delete newlines as well, leaving -N spaces." 747If N is negative, delete newlines as well, leaving -N spaces."
748 (interactive "*p") 748 (interactive "*p")
749 (unless n (setq n 1)) 749 (cycle-spacing n nil t))
750 (let ((orig-pos (point)) 750
751 (skip-characters (if (< n 0) " \t\n\r" " \t")) 751(defvar cycle-spacing--context nil
752 (n (abs n))) 752 "Store context used in consecutive calls to `cycle-spacing' command.
753 (skip-chars-backward skip-characters) 753The first time this function is run, it saves the original point
754position and original spacing around the point in this
755variable.")
756
757(defun cycle-spacing (&optional n preserve-nl-back single-shot)
758 "Manipulate spaces around the point in a smart way.
759
760When run as an interactive command, the first time it's called
761in a sequence, deletes all spaces and tabs around point leaving
762one (or N spaces). If this does not change content of the
763buffer, skips to the second step:
764
765When run for the second time in a sequence, deletes all the
766spaces it has previously inserted.
767
768When run for the third time, returns the whitespace and point in
769a state encountered when it had been run for the first time.
770
771For example, if buffer contains \"foo ^ bar\" with \"^\" denoting the
772point, calling `cycle-spacing' command will replace two spaces with
773a single space, calling it again immediately after, will remove all
774spaces, and calling it for the third time will bring two spaces back
775together.
776
777If N is negative, delete newlines as well. However, if
778PRESERVE-NL-BACK is t new line characters prior to the point
779won't be removed.
780
781If SINGLE-SHOT is non-nil, will only perform the first step. In
782other words, it will work just like `just-one-space' command."
783 (interactive "*p")
784 (let ((orig-pos (point))
785 (skip-characters (if (and n (< n 0)) " \t\n\r" " \t"))
786 (n (abs (or n 1))))
787 (skip-chars-backward (if preserve-nl-back " \t" skip-characters))
754 (constrain-to-field nil orig-pos) 788 (constrain-to-field nil orig-pos)
755 (dotimes (_ n) 789 (cond
756 (if (= (following-char) ?\s) 790 ;; Command run for the first time or single-shot is non-nil.
757 (forward-char 1) 791 ((or single-shot
758 (insert ?\s))) 792 (not (equal last-command this-command))
759 (delete-region 793 (not cycle-spacing--context))
760 (point) 794 (let* ((start (point))
761 (progn 795 (n (- n (skip-chars-forward " " (+ n (point)))))
762 (skip-chars-forward skip-characters) 796 (mid (point))
763 (constrain-to-field nil orig-pos t))))) 797 (end (progn
798 (skip-chars-forward skip-characters)
799 (constrain-to-field nil orig-pos t))))
800 (setq cycle-spacing--context ;; Save for later.
801 ;; Special handling for case where there was no space at all.
802 (unless (= start end)
803 (cons orig-pos (buffer-substring start (point)))))
804 ;; If this run causes no change in buffer content, delete all spaces,
805 ;; otherwise delete all excees spaces.
806 (delete-region (if (and (not single-shot) (zerop n) (= mid end))
807 start mid) end)
808 (insert (make-string ?\s n))))
809
810 ;; Command run for the second time.
811 ((not (equal orig-pos (point)))
812 (delete-region (point) orig-pos))
813
814 ;; Command run for the third time.
815 (t
816 (insert (cdr cycle-spacing--context))
817 (goto-char (car cycle-spacing--context))
818 (setq cycle-spacing--context nil)))))
764 819
765(defun beginning-of-buffer (&optional arg) 820(defun beginning-of-buffer (&optional arg)
766 "Move point to the beginning of the buffer. 821 "Move point to the beginning of the buffer.