-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsort-regions.el
64 lines (51 loc) · 1.7 KB
/
sort-regions.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
;;; sort-regions.el -- Sort regions between a pair of deliminators
;; This is a cute idea that came up in discussions internally.
;;
;; Update terraform configuration values to say:
;;
;; users = [
;; # sort-start
;; "alex..",
;; "bob..",
;; # sort-end
;; ]
;;
;; Then add the hook, or otherwise call the function, and the region will
;; be sorted.
;;
(defgroup sort-regions ()
"Options for `sort-regions'."
:prefix "sort-regions-")
(defcustom sort-regions-marker-start "sort-start"
"The string marking the start of a region that can be auto-sorted."
:type 'string
:group 'sort-regions)
(defcustom sort-regions-marker-end "sort-end"
"The string marking the end of a region that can be auto-sorted."
:type 'string
:group 'sort-regions)
(defun sort-regions()
"Sort the text bound between two lines containing markers."
(interactive)
(let (start end)
(save-excursion
(goto-char (point-min))
;; Search for the starting marker
(while (search-forward sort-regions-marker-start nil t)
(setq start (line-beginning-position 2)) ; line after
;; Search for the ending marker
(when (search-forward sort-regions-marker-end nil t)
(setq end (line-beginning-position)) ; this line
;; Sort the lines between the start and end markers
(sort-lines nil start end))))))
;;;###autoload
(define-minor-mode auto-sort-regions-mode
"Automatically apply `sort-regions` to files in this mode."
:lighter " SR"
(if (eq auto-sort-regions-mode t)
(add-hook 'write-contents-functions
#'sort-regions)
(remove-hook 'write-contents-functions
#'sort-regions)))
;; All done
(provide 'sort-regions)