Update sort-lines-by-column, improvements

Limit the lines to only get sorted when a block or rectangle
selection is active, on 2 or more lines. Otherwise show a
message stating the requirements for sorting by column.
This commit is contained in:
duianto 2017-03-10 21:00:20 +01:00 committed by syl20bnr
parent 89f8d679ea
commit 642c987623
1 changed files with 15 additions and 7 deletions

View File

@ -842,16 +842,24 @@ A non-nil argument sorts in reverse order."
(spacemacs/sort-lines -1))
(defun spacemacs/sort-lines-by-column (&optional reverse)
"Sort lines by the selected column.
A non-nil argument sorts in reverse order."
"Sort lines by the selected column,
using a visual block/rectangle selection.
A non-nil argument sorts in REVERSE order."
(interactive "P")
(let* ((region-active (or (region-active-p) (evil-visual-state-p)))
(beg (if region-active (region-beginning) (point-min)))
(end (if region-active (region-end) (point-max))))
(sort-columns reverse beg end)))
(if (and
;; is there an active selection
(or (region-active-p) (evil-visual-state-p))
;; is it a block or rectangle selection
(or (eq evil-visual-selection 'block) (eq rectangle-mark-mode t))
;; is the selection height 2 or more lines
(>= (1+ (- (line-number-at-pos (region-end))
(line-number-at-pos (region-beginning)))) 2))
(sort-columns reverse (region-beginning) (region-end))
(error "Sorting by column requires a block/rect selection on 2 or more lines.")))
(defun spacemacs/sort-lines-by-column-reverse ()
"Sort lines by the selected column in reverse order."
"Sort lines by the selected column in reverse order,
using a visual block/rectangle selection."
(interactive)
(spacemacs/sort-lines-by-column -1))