aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichal Nazarewicz2014-03-28 17:26:15 +0100
committerMichal Nazarewicz2014-03-28 17:26:15 +0100
commit687e0e19ae4dbd1a6fc3f6d3d21fd6b754e7589d (patch)
treed8bb4a9be7427f3dd5102b7f33934f817f04cca2
parent82a863c134dcab10ec1049272caf5d14557994fc (diff)
downloademacs-687e0e19ae4dbd1a6fc3f6d3d21fd6b754e7589d.tar.gz
emacs-687e0e19ae4dbd1a6fc3f6d3d21fd6b754e7589d.zip
Make `cycle-spacing' behave more like `just-one-space' if colled once.
* simple.el (cycle-spacing): Never delete spaces on first run by default, but do so in a new 'fast mode and if there are already N spaces (the previous behaviour). Compare N with its value in previous invocation so that changing prefix argument restarts `cycle-spacing' sequence. The idea is that with this change, binding M-SPC to `cycle-spacing' should not introduce any changes in behaviour of the binding so long as users do not type M-SPC twice in a raw with the same prefix argument or lack thereof.
-rw-r--r--lisp/ChangeLog14
-rw-r--r--lisp/simple.el59
2 files changed, 47 insertions, 26 deletions
diff --git a/lisp/ChangeLog b/lisp/ChangeLog
index 2cd29d8a5c9..ee50bed6209 100644
--- a/lisp/ChangeLog
+++ b/lisp/ChangeLog
@@ -1,3 +1,17 @@
12014-03-28 Michal Nazarewicz <mina86@mina86.com>
2
3 * simple.el (cycle-spacing): Never delete spaces on first run by
4 default, but do so in a new 'fast mode and if there are already
5 N spaces (the previous behaviour).
6
7 Compare N with its value in previous invocation so that changing
8 prefix argument restarts `cycle-spacing' sequence.
9
10 The idea is that with this change, binding M-SPC to
11 `cycle-spacing' should not introduce any changes in behaviour of
12 the binding so long as users do not type M-SPC twice in a raw with
13 the same prefix argument or lack thereof.
14
12014-03-28 Glenn Morris <rgm@gnu.org> 152014-03-28 Glenn Morris <rgm@gnu.org>
2 16
3 * faces.el (term-file-aliases): New variable. 17 * faces.el (term-file-aliases): New variable.
diff --git a/lisp/simple.el b/lisp/simple.el
index ea9ba8fa9a5..6cc9c6bfd6b 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -801,44 +801,51 @@ If BACKWARD-ONLY is non-nil, only delete them before point."
801If N is negative, delete newlines as well, leaving -N spaces. 801If N is negative, delete newlines as well, leaving -N spaces.
802See also `cycle-spacing'." 802See also `cycle-spacing'."
803 (interactive "*p") 803 (interactive "*p")
804 (cycle-spacing n nil t)) 804 (cycle-spacing n nil 'single-shot))
805 805
806(defvar cycle-spacing--context nil 806(defvar cycle-spacing--context nil
807 "Store context used in consecutive calls to `cycle-spacing' command. 807 "Store context used in consecutive calls to `cycle-spacing' command.
808The first time this function is run, it saves the original point 808The first time this function is run, it saves N argument, the
809position and original spacing around the point in this 809original point position and original spacing around the point in
810variable.") 810this variable.")
811 811
812(defun cycle-spacing (&optional n preserve-nl-back single-shot) 812(defun cycle-spacing (&optional n preserve-nl-back mode)
813 "Manipulate whitespace around point in a smart way. 813 "Manipulate whitespace around point in a smart way.
814In interactive use, this function behaves differently in successive 814In interactive use, this function behaves differently in
815consecutive calls. 815successive consecutive calls.
816 816
817The first call in a sequence acts like `just-one-space'. 817The first call in a sequence acts like `just-one-space'. It
818It deletes all spaces and tabs around point, leaving one space 818deletes all spaces and tabs around point, leaving one space \(or
819\(or N spaces). N is the prefix argument. If N is negative, 819N spaces). N is the prefix argument. If N is negative, it
820it deletes newlines as well, leaving -N spaces. 820deletes newlines as well leaving -N spaces. (If PRESERVE-NL-BACK
821\(If PRESERVE-NL-BACK is non-nil, it does not delete newlines before point.) 821is non-nil, it does not delete newlines before point.)
822 822
823The second call in a sequence (or the first call if the above does 823The second call in a sequence deletes all spaces.
824not result in any changes) deletes all spaces.
825 824
826The third call in a sequence restores the original whitespace (and point). 825The third call in a sequence restores the original
826whitespace (and point).
827 827
828If SINGLE-SHOT is non-nil, it only performs the first step in the sequence." 828If MODE is 'single-shot only the first step is performed. If
829MODE is 'fast and the first step did not result in any
830change (i.e. there was exactly (abs N) spaces around point)
831function goes to the second step immediately.
832
833Running the function with different N arguments initiates a new
834sequence each time."
829 (interactive "*p") 835 (interactive "*p")
830 (let ((orig-pos (point)) 836 (let ((orig-pos (point))
831 (skip-characters (if (and n (< n 0)) " \t\n\r" " \t")) 837 (skip-characters (if (and n (< n 0)) " \t\n\r" " \t"))
832 (n (abs (or n 1)))) 838 (num (abs (or n 1))))
833 (skip-chars-backward (if preserve-nl-back " \t" skip-characters)) 839 (skip-chars-backward (if preserve-nl-back " \t" skip-characters))
834 (constrain-to-field nil orig-pos) 840 (constrain-to-field nil orig-pos)
835 (cond 841 (cond
836 ;; Command run for the first time or single-shot is non-nil. 842 ;; Command run for the first time, single-shot mode or different argument
837 ((or single-shot 843 ((or (eq 'single-shot mode)
838 (not (equal last-command this-command)) 844 (not (equal last-command this-command))
839 (not cycle-spacing--context)) 845 (not cycle-spacing--context)
846 (not (eq (car cycle-spacing--context) n)))
840 (let* ((start (point)) 847 (let* ((start (point))
841 (n (- n (skip-chars-forward " " (+ n (point))))) 848 (num (- num (skip-chars-forward " " (+ num (point)))))
842 (mid (point)) 849 (mid (point))
843 (end (progn 850 (end (progn
844 (skip-chars-forward skip-characters) 851 (skip-chars-forward skip-characters)
@@ -846,12 +853,12 @@ If SINGLE-SHOT is non-nil, it only performs the first step in the sequence."
846 (setq cycle-spacing--context ;; Save for later. 853 (setq cycle-spacing--context ;; Save for later.
847 ;; Special handling for case where there was no space at all. 854 ;; Special handling for case where there was no space at all.
848 (unless (= start end) 855 (unless (= start end)
849 (cons orig-pos (buffer-substring start (point))))) 856 (cons n (cons orig-pos (buffer-substring start (point))))))
850 ;; If this run causes no change in buffer content, delete all spaces, 857 ;; If this run causes no change in buffer content, delete all spaces,
851 ;; otherwise delete all excess spaces. 858 ;; otherwise delete all excess spaces.
852 (delete-region (if (and (not single-shot) (zerop n) (= mid end)) 859 (delete-region (if (and (eq mode 'fast) (zerop num) (= mid end))
853 start mid) end) 860 start mid) end)
854 (insert (make-string n ?\s)))) 861 (insert (make-string num ?\s))))
855 862
856 ;; Command run for the second time. 863 ;; Command run for the second time.
857 ((not (equal orig-pos (point))) 864 ((not (equal orig-pos (point)))
@@ -859,8 +866,8 @@ If SINGLE-SHOT is non-nil, it only performs the first step in the sequence."
859 866
860 ;; Command run for the third time. 867 ;; Command run for the third time.
861 (t 868 (t
862 (insert (cdr cycle-spacing--context)) 869 (insert (cddr cycle-spacing--context))
863 (goto-char (car cycle-spacing--context)) 870 (goto-char (cadr cycle-spacing--context))
864 (setq cycle-spacing--context nil))))) 871 (setq cycle-spacing--context nil)))))
865 872
866(defun beginning-of-buffer (&optional arg) 873(defun beginning-of-buffer (&optional arg)