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

View file

@ -842,16 +842,24 @@ A non-nil argument sorts in reverse order."
(spacemacs/sort-lines -1)) (spacemacs/sort-lines -1))
(defun spacemacs/sort-lines-by-column (&optional reverse) (defun spacemacs/sort-lines-by-column (&optional reverse)
"Sort lines by the selected column. "Sort lines by the selected column,
A non-nil argument sorts in reverse order." using a visual block/rectangle selection.
A non-nil argument sorts in REVERSE order."
(interactive "P") (interactive "P")
(let* ((region-active (or (region-active-p) (evil-visual-state-p))) (if (and
(beg (if region-active (region-beginning) (point-min))) ;; is there an active selection
(end (if region-active (region-end) (point-max)))) (or (region-active-p) (evil-visual-state-p))
(sort-columns reverse beg end))) ;; 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 () (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) (interactive)
(spacemacs/sort-lines-by-column -1)) (spacemacs/sort-lines-by-column -1))