diff options
| author | Michal Nazarewicz | 2013-01-30 21:57:35 -0500 |
|---|---|---|
| committer | Stefan Monnier | 2013-01-30 21:57:35 -0500 |
| commit | ad4de702e19bf1a8065cb84b6eefbc68190d9c3f (patch) | |
| tree | 8a5aca2205b7f3bc5352a31fdad960eb645120ce | |
| parent | c4f268a1373f4f247e22e103a8655e06c7129c22 (diff) | |
| download | emacs-ad4de702e19bf1a8065cb84b6eefbc68190d9c3f.tar.gz emacs-ad4de702e19bf1a8065cb84b6eefbc68190d9c3f.zip | |
* lisp/simple.el (cycle-spacing): New command.
(just-one-space): Use it.
| -rw-r--r-- | etc/NEWS | 5 | ||||
| -rw-r--r-- | lisp/ChangeLog | 5 | ||||
| -rw-r--r-- | lisp/simple.el | 83 |
3 files changed, 79 insertions, 14 deletions
| @@ -163,6 +163,11 @@ when its arg ADJACENT is non-nil (when called interactively with C-u C-u) | |||
| 163 | it works like the utility `uniq'. Otherwise by default it deletes | 163 | it works like the utility `uniq'. Otherwise by default it deletes |
| 164 | duplicate lines everywhere in the region without regard to adjacency. | 164 | duplicate lines everywhere in the region without regard to adjacency. |
| 165 | 165 | ||
| 166 | ** New `cycle-spacing' command allows cycling between having just one | ||
| 167 | space, no spaces, or reverting to the original spacing. Like | ||
| 168 | `just-one-space' command it can handle or ignore newlines and | ||
| 169 | leave 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 @@ | |||
| 1 | 2013-01-31 Michal Nazarewicz <mina86@mina86.com> | ||
| 2 | |||
| 3 | * simple.el (cycle-spacing): New command. | ||
| 4 | (just-one-space): Use it. | ||
| 5 | |||
| 1 | 2013-01-31 Stefan Monnier <monnier@iro.umontreal.ca> | 6 | 2013-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). |
| 747 | If N is negative, delete newlines as well, leaving -N spaces." | 747 | If 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) | 753 | The first time this function is run, it saves the original point |
| 754 | position and original spacing around the point in this | ||
| 755 | variable.") | ||
| 756 | |||
| 757 | (defun cycle-spacing (&optional n preserve-nl-back single-shot) | ||
| 758 | "Manipulate spaces around the point in a smart way. | ||
| 759 | |||
| 760 | When run as an interactive command, the first time it's called | ||
| 761 | in a sequence, deletes all spaces and tabs around point leaving | ||
| 762 | one (or N spaces). If this does not change content of the | ||
| 763 | buffer, skips to the second step: | ||
| 764 | |||
| 765 | When run for the second time in a sequence, deletes all the | ||
| 766 | spaces it has previously inserted. | ||
| 767 | |||
| 768 | When run for the third time, returns the whitespace and point in | ||
| 769 | a state encountered when it had been run for the first time. | ||
| 770 | |||
| 771 | For example, if buffer contains \"foo ^ bar\" with \"^\" denoting the | ||
| 772 | point, calling `cycle-spacing' command will replace two spaces with | ||
| 773 | a single space, calling it again immediately after, will remove all | ||
| 774 | spaces, and calling it for the third time will bring two spaces back | ||
| 775 | together. | ||
| 776 | |||
| 777 | If N is negative, delete newlines as well. However, if | ||
| 778 | PRESERVE-NL-BACK is t new line characters prior to the point | ||
| 779 | won't be removed. | ||
| 780 | |||
| 781 | If SINGLE-SHOT is non-nil, will only perform the first step. In | ||
| 782 | other 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. |