about summary refs log tree commit diff stats
path: root/lisp/+lisp.el
blob: a78e40ef9501754697a27965ec52ecc9f467616e (plain)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
;;; +lisp.el --- extra lisp functionality -*- lexical-binding: t -*-

;;; Code:

;;; Sort sexps in a region.
;; https://github.com/alphapapa/unpackaged.el

(defun +lisp-skip-whitespace ()
  (while (looking-at (rx (1+ (or space "\n"))))
                                  (goto-char (match-end 0))))

(defun +lisp-skip-both ()
  (while (cond ((or (nth 4 (syntax-ppss))
                    (ignore-errors
                      (save-excursion
                        (forward-char 1)
                        (nth 4 (syntax-ppss)))))
                (forward-line 1))
               ((looking-at (rx (1+ (or space "\n"))))
                (goto-char (match-end 0))))))

(defun +lisp-sort-sexps (beg end &optional key-fn sort-fn)
  "Sort sexps between BEG and END.
Comments stay with the code below.

Optional argument KEY-FN will determine where in each sexp to
start sorting.  e.g. (lambda (sexp) (symbol-name (car sexp)))

Optional argument SORT-FN will determine how to sort two sexps'
strings.  It's passed to `sort'.  By default, it sorts the sexps
with `string<' starting with the key determined by KEY-FN."
  (interactive "r")
  (save-excursion
    (save-restriction
      (narrow-to-region beg end)
      (goto-char beg)
      (+lisp-skip-both)
      (cl-destructuring-bind (sexps markers)
          (cl-loop do (+lisp-skip-whitespace)
                   for start = (point-marker)
                   for sexp = (ignore-errors
                                (read (current-buffer)))
                   for end = (point-marker)
                   while sexp
                   ;; Collect the real string, then one used for sorting.
                   collect (cons (buffer-substring (marker-position start)
                                                   (marker-position end))
                                 (save-excursion
                                   (goto-char (marker-position start))
                                   (+lisp-skip-both)
                                   (if key-fn
                                       (funcall key-fn sexp)
                                     (buffer-substring
                                      (point)
                                      (marker-position end)))))
                   into sexps
                   collect (cons start end)
                   into markers
                   finally return (list sexps markers))
        (setq sexps (sort sexps (if sort-fn sort-fn
                                  (lambda (a b)
                                    (string< (cdr a) (cdr b))))))
        (cl-loop for (real . sort) in sexps
                 for (start . end) in markers
                 do (progn
                      (goto-char (marker-position start))
                      (insert-before-markers real)
                      (delete-region (point) (marker-position end))))))))

;;; Comment-or-uncomment-sexp
;; from https://endlessparentheses.com/a-comment-or-uncomment-sexp-command.html

(defun +lisp-uncomment-sexp (&optional n)
  "Uncomment N sexps around point."
  (interactive "P")
  (let* ((initial-point (point-marker))
         (inhibit-field-text-motion t)
         (p)
         (end (save-excursion
                (when (elt (syntax-ppss) 4)
                  (re-search-backward comment-start-skip
                                      (line-beginning-position)
                                      t))
                (setq p (point-marker))
                (comment-forward (point-max))
                (point-marker)))
         (beg (save-excursion
                (forward-line 0)
                (while (and (not (bobp))
                            (= end (save-excursion
                                     (comment-forward (point-max))
                                     (point))))
                  (forward-line -1))
                (goto-char (line-end-position))
                (re-search-backward comment-start-skip
                                    (line-beginning-position)
                                    t)
                (ignore-errors
                  (while (looking-at-p comment-start-skip)
                    (forward-char -1)))
                (point-marker))))
    (unless (= beg end)
      (uncomment-region beg end)
      (goto-char p)
      ;; Indentify the "top-level" sexp inside the comment.
      (while (and (ignore-errors (backward-up-list) t)
                  (>= (point) beg))
        (skip-chars-backward (rx (syntax expression-prefix)))
        (setq p (point-marker)))
      ;; Re-comment everything before it.
      (ignore-errors
        (comment-region beg p))
      ;; And everything after it.
      (goto-char p)
      (forward-sexp (or n 1))
      (skip-chars-forward "\r\n[:blank:]")
      (if (< (point) end)
          (ignore-errors
            (comment-region (point) end))
        ;; If this is a closing delimiter, pull it up.
        (goto-char end)
        (skip-chars-forward "\r\n[:blank:]")
        (when (eq 5 (car (syntax-after (point))))
          (delete-indentation))))
    ;; Without a prefix, it's more useful to leave point where
    ;; it was.
    (unless n
      (goto-char initial-point))))

(defun +lisp-comment-sexp--raw ()
  "Comment the sexp at point or ahead of point."
  (pcase (or (bounds-of-thing-at-point 'sexp)
             (save-excursion
               (skip-chars-forward "\r\n[:blank:]")
               (bounds-of-thing-at-point 'sexp)))
    (`(,l . ,r)
     (goto-char r)
     (skip-chars-forward "\r\n[:blank:]")
     (save-excursion
       (comment-region l r))
     (skip-chars-forward "\r\n[:blank:]"))))

(defun +lisp-comment-or-uncomment-sexp (&optional n)
  "Comment the sexp at point and move past it.
If already inside (or before) a comment, uncomment instead.
With a prefix argument N, (un)comment that many sexps."
  (interactive "P")
  (if (or (elt (syntax-ppss) 4)
          (< (save-excursion
               (skip-chars-forward "\r\n[:blank:]")
               (point))
             (save-excursion
               (comment-forward 1)
               (point))))
      (+lisp-uncomment-sexp n)
    (dotimes (_ (or n 1))
      (+lisp-comment-sexp--raw))))

;;; Sort `setq' constructs
;;https://emacs.stackexchange.com/questions/33039/

(defun +lisp-sort-setq ()
  (interactive)
  (save-excursion
    (save-restriction
      (let ((sort-end (progn
                        (end-of-defun)
                        (backward-char)
                        (point-marker)))
            (sort-beg (progn
                        (beginning-of-defun)
                        (or (re-search-forward "[ \\t]*(" (point-at-eol) t)
                            (point-at-eol))
                        (forward-sexp)
                        (or (re-search-forward "\\<" (point-at-eol) t)
                            (point-at-eol))
                        (point-marker))))
        (narrow-to-region (1- sort-beg) (1+ sort-end))
        (sort-subr nil #'+lisp-sort-setq-next-record
                   #'+lisp-sort-setq-end-record)))))

(defun +lisp-sort-setq-next-record ()
  (condition-case nil
      (progn
        (forward-sexp 1)
        (backward-sexp))
    ('scan-error (end-of-buffer))))

(defun +lisp-sort-setq-end-record ()
  (condition-case nil
      (forward-sexp 2)
    ('scan-error (end-of-buffer))))

(provide '+lisp)
;;; +lisp.el ends here