Source

emacs.d / dss-lisps.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
 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
(require 'skeleton)
(require 'diminish)
(require 'paredit)
(require 'eldoc)
(diminish 'eldoc-mode "")
(diminish 'paredit-mode "PE")

;; (eldoc-add-command
;;  'paredit-backward-delete
;;  'paredit-close-round)

(eval-when-compile
  (require 'cl))
(require 'dss-codenav-helpers)

(eldoc-add-command
 'paredit-backward-delete
 'paredit-close-round)

(defvar dss-lisp-modes-hook nil)

(defun dss/lisp-modes-init ()
  (linum-mode t)
  (paredit-mode +1)
  (dss/highlight-watchwords)
  (run-hooks 'dss-lisp-modes-hook))


(defun conditionally-enable-paredit-mode ()
  "enable paredit-mode during eval-expression"
  (if (eq this-command 'eval-expression)
      (progn
        (paredit-mode 1))))
(add-hook 'minibuffer-setup-hook 'conditionally-enable-paredit-mode)


;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;  idea from esk-paren-face in emacs starter Kit
(defface dss-paren-face
  '((((class color))
     ;; 9e9e9e or a8a8a8 are also good
     (:foreground "#8a8a8a")))
  "Face used to dim parentheses."
  :group 'dss-faces)

(defface dss-end-paren-face
  '((((class color))
     (:foreground "#8a8a8a")))         ;#9e9e9e
  "Face used to dim parentheses."
  :group 'dss-faces)

;; this form also from emacs starter kit
(dolist (x '(scheme emacs-lisp lisp slime-repl lisp-interaction clojure))
  (font-lock-add-keywords
   (intern (concat (symbol-name x) "-mode"))
   '(("(" . 'dss-paren-face)      ;("(\\|)" . 'dss-paren-face)
     (")" . 'dss-end-paren-face)
     ))
  (add-hook
   (intern (concat (symbol-name x) "-mode-hook")) 'dss/lisp-modes-init))

(setq dss-greyscale
      ["#080808"
       "#121212"
       "#1c1c1c"
       "#262626"
       "#303030"
       "#3a3a3a"
       "#444444"
       "#4e4e4e"
       "#585858"
       "#626262"
       "#6c6c6c"
       "#767676"
       "#808080"
       "#8a8a8a"
       "#949494"
       "#9e9e9e"
       "#a8a8a8"
       "#b2b2b2"
       "#bcbcbc"
       "#c6c6c6"
       "#d0d0d0"
       "#dadada"
       "#e4e4e4"
       "#eeeeee"])

(defun dss/greyscale-pos (colour)
  (interactive)
  (or
   (position colour dss-greyscale :test 'string=)
   (1- (length dss-greyscale))))

(defun dss/greyscale-incr (colour &optional i)
  (interactive)
  (let* ((i (or i 1))
         (min-or-max (if (> i 0) 'min 'max))
         (arg2 (if (> i 0) (1- (length dss-greyscale)) 0)))
    (elt dss-greyscale
         (funcall min-or-max (+ (dss/greyscale-pos colour) i) arg2))))

(defun dss/greyscale-decr (colour &optional i)
  (interactive)
  (dss/greyscale-incr colour (or i -1)))

;; (dss/greyscale-incr (elt dss-greyscale 0) 25)
;; (dss/greyscale-incr (elt dss-greyscale (1- (length dss-greyscale))) 4)

                                        ;rainbow-delimiters-depth-1-face

;; (loop for i from 1 to 9
;;       do (message (symbol-name (intern (concat "rainbow-delimiters-depth-" (number-to-string i) "-face")))))
(require 'rainbow-delimiters)
(setq rainbow-delimiters-max-face-count 8)

(defun dss/lisp-setup-rainbow-delimeters ()
  (let ((dss-rainbow-delim-colors
         [
          "red"
          "brightblue"
          "yellow"
          "purple"
          "cyan"
          "#626262"
          "#6c6c6c"
          "#767676"
          "#808080"
          "#8a8a8a"
          "#949494"
          "#9e9e9e"
          "#a8a8a8"
          "#b2b2b2"
          "#bcbcbc"
          "#c6c6c6"
          "#d0d0d0"
          "#dadada"
          "#e4e4e4"
          "#eeeeee"]))
    (loop for i from 1 to rainbow-delimiters-max-face-count
          do (set-face-foreground
              (intern (concat "rainbow-delimiters-depth-" (number-to-string i) "-face"))
              (elt dss-rainbow-delim-colors (1- i))))))

(dss/lisp-setup-rainbow-delimeters)

(defun dss/lisp-rainbow-flash ()
  (interactive)
  (let* ((depth 1)
         (beg (save-excursion
                (cond
                 ((looking-at "\\s\(") (point))
                 (t
                  (progn
                    (call-interactively 'dss/goto-match-paren)
                    (point))))))
         (end (save-excursion
                (cond
                 ((looking-at "\\s\(") (forward-list 1))
                 (t
                  (call-interactively 'dss/goto-match-paren)
                  (forward-list 1)))
                (point))))
    (save-excursion
      (goto-char beg)
      (while (and (< (point) end)
                  (re-search-forward rainbow-delimiters-delim-regex end t))
        (backward-char) ; re-search-forward places point after delim; go back.
        (unless (rainbow-delimiters-char-ineligible-p (point))
          (let ((delim (char-after (point))))
            (cond ((eq ?\( delim)       ; (
                   (setq depth (1+ depth))
                   (rainbow-delimiters-apply-color "paren" depth (point)))
                  ((eq ?\) delim)       ; )
                   (rainbow-delimiters-apply-color "paren" depth (point))
                   (setq depth (or (and (<= depth 0) 0) ; unmatched paren
                                   (1- depth))))
                  ((eq ?\[ delim)       ; [
                   (setq depth (1+ depth))
                   (rainbow-delimiters-apply-color "bracket" depth (point)))
                  ((eq ?\] delim)       ; ]
                   (rainbow-delimiters-apply-color "bracket" depth (point))
                   (setq depth (or (and (<= depth 0) 0) ; unmatched bracket
                                   (1- depth))))
                  ((eq ?\{ delim)       ; {
                   (setq depth (1+ depth))
                   (rainbow-delimiters-apply-color "brace" depth (point)))
                  ((eq ?\} delim)       ; }
                   (rainbow-delimiters-apply-color "brace" depth (point))
                   (setq depth (or (and (<= depth 0) 0) ; unmatched brace
                                   (1- depth)))))))
        ;; move past delimiter so re-search-forward doesn't pick it up again
        (forward-char)))
    (save-excursion (font-lock-fontify-region beg end))
    (sit-for 3)
    (save-excursion
      (rainbow-delimiters-unpropertize-region beg end)
      (font-lock-fontify-region beg end))))

(defun dss/lisp-set-parens-color (i)
  (interactive "n1-23: ")
  (dolist (face '(dss-paren-face dss-end-paren-face))
    (set-face-foreground face (elt dss-greyscale i))))

(defun dss/lisp-brighten-parens ()
  (interactive)
  (dolist (face '(dss-paren-face dss-end-paren-face))
    (set-face-foreground face (dss/greyscale-incr (face-foreground face) 2))))

(defun dss/lisp-dim-parens ()
  (interactive)
  (dolist (face '(dss-paren-face dss-end-paren-face))
    (set-face-foreground face (dss/greyscale-decr (face-foreground face) -2))))

(defun dss/goto-match-paren (arg)
  "Go to the matching parenthesis if on parenthesis. Else go to the
   opening parenthesis one level up.

  Copied from: http://www.emacswiki.org/emacs/ParenthesisMatching"
  (interactive "p")
  (cond ((looking-at "\\s\(") (forward-list 1))
        (t
         (backward-char 1)
         (cond ((looking-at "\\s\)")
                (forward-char 1) (backward-list 1))
               (t
                (while (not (looking-at "\\s("))
                  (backward-char 1)
                  (cond ((looking-at "\\s\)")
                         (forward-char 1)
                         (backward-list 1)
                         (backward-char 1)))))))))


(defun dss/paredit-backward-delete ()
  (interactive)
  (if mark-active
      (call-interactively 'delete-region)
    (paredit-backward-delete)))

(define-key paredit-mode-map (kbd "DEL") 'dss/paredit-backward-delete)

(defun dss/paredit-open-parenthesis ()
  (interactive)
  (cond ((and (not (or mark-active (dss/in-string-p)))
              (looking-at-p "[\(a-z\"#\\[{]"))
         (progn
           (mark-sexp)
           (paredit-open-parenthesis)
           (if (looking-at-p "[\(\"#\\[{]")
               (save-excursion (insert " ")))))
        (t (paredit-open-parenthesis))))
(define-key paredit-mode-map "(" 'dss/paredit-open-parenthesis)

(defun dss/paredit-semicolon ()
  (interactive)
  (if (looking-at-p " +\(")
      (progn
        (search-forward "(")
        (backward-char)))
  (cond ((and (not mark-active) (looking-at-p "\("))
         (progn
           (mark-sexp)
           (paredit-comment-dwim)
           ;; (save-excursion (reindent-then-newline-and-indent))
           (indent-according-to-mode)))
        ((and (not mark-active)
              (looking-at-p "^[[:blank:]]*$"))
         (insert ";;; "))
        ((and (not mark-active)
              (and (looking-back "^[[:blank:]]*")
                   (looking-at-p "[[:blank:]]*$")))
         (insert ";; "))
        (t (paredit-semicolon))))
(define-key paredit-mode-map ";" 'dss/paredit-semicolon)

(defun dss/paredit-open-line ()
  (interactive)
  (save-excursion
    (reindent-then-newline-and-indent))
  (indent-according-to-mode))
(define-key paredit-mode-map (kbd "M-o") 'dss/paredit-open-line)

(defun dss/replace-sexp ()
  (interactive)
  (if (dss/in-string-p)
      (dss/mark-string)
    (mark-sexp))
  (delete-region (point) (mark))
  (yank))
(define-key paredit-mode-map (kbd "C-M-y") 'dss/replace-sexp)

(defun dss/paredit-kill-ring-save ()
  (interactive)
  (if (not mark-active)
      (save-excursion
        (if (looking-at-p " +\(")
            (progn
              (search-forward "(")
              (backward-char)))
        (mark-sexp)
        (call-interactively 'kill-ring-save))
    (call-interactively 'kill-ring-save)))

(define-key paredit-mode-map (kbd "M-w") 'dss/paredit-kill-ring-save)

(define-skeleton dss/elisp-let-skeleton
  "A simple e-lisp let skeleton"
  nil
  "(let ((" @ - "))" \n >
  @ _ ")")

(setq dss-let-skeleton-func 'dss/elisp-let-skeleton)

(defun dss/paredit-l-or-sexp-wrap-let ()
  (interactive)
  (if (and (not (dss/in-string-p))
           (looking-at-p "\("))
      (progn
        (if (not mark-active)
            (mark-sexp))
        (funcall dss-let-skeleton-func))
    (self-insert-command 1)))
(define-key paredit-mode-map "l" 'dss/paredit-l-or-sexp-wrap-let)

(define-skeleton dss/elisp-defun-skeleton
  "A simple e-lisp defun skeleton"
  nil
  "(defun dss/" @ - " (" @ ")" \n >
  "(interactive" @ ")" \n >
  @ _
    ")")
(setq dss-defun-skeleton-func 'dss/elisp-defun-skeleton)

(defun dss/in-slime-repl-p ()
  (equal mode-name "REPL"))

(defun dss/paredit-d-or-defun ()
  (interactive)
  (if (and (not (dss/in-string-p))
           (or (and (dss/in-slime-repl-p)
                    (slime-repl-at-prompt-start-p))
               (looking-at-p "^")))
      (progn (if (and (not (looking-at "\(def"))
                      (looking-at "\("))
                 (if (not mark-active)
                     (mark-sexp)))
             (funcall dss-defun-skeleton-func)
             (dss/indent-defun))
    (self-insert-command 1)))
(define-key paredit-mode-map "d" 'dss/paredit-d-or-defun)

(defun dss/paredit-9-or-paren ()
  (interactive)
  (if (and (not (dss/in-string-p))
           (or (looking-at-p "^")
               (looking-at-p "[\(\"#\\[{]")
               (and (dss/in-slime-repl-p)
                    (slime-repl-at-prompt-start-p))))
      (dss/paredit-open-parenthesis)
    (self-insert-command 1)))
(define-key paredit-mode-map "9" 'dss/paredit-9-or-paren)

(define-skeleton dss-elisp-progn-skeleton
  "A simple e-lisp progn skeleton"
  nil
  "(progn" @ \n >
  @ _ - ")"
    (dss/indent-defun))

(defun dss/paredit-p-or-progn ()
  (interactive)
  (if (and (not (dss/in-string-p))
           (looking-at-p "\("))
      (progn
        (if (not mark-active)
            (mark-sexp))
        (dss-elisp-progn-skeleton))
    (self-insert-command 1)))
(define-key paredit-mode-map "p" 'dss/paredit-p-or-progn)

(defun dss/paredit-yank ()
  (interactive)
  (if (not mark-active)
      (progn
        (call-interactively 'yank)
        (if (and (looking-back "\)")
                 (looking-at-p "\("))
            (progn
              (reindent-then-newline-and-indent)
              (if (looking-at-p "^")
                  (newline)))))
    (call-interactively 'yank))
  (condition-case nil (dss/indent-defun)))

(define-key paredit-mode-map (kbd "C-y") 'dss/paredit-yank)

(define-key paredit-mode-map (kbd "C-M-s") 'paredit-backward-up)

(define-key paredit-mode-map (kbd "C-M-k") 'paredit-forward-slurp-sexp)
(define-key paredit-mode-map (kbd "C-M-j") 'paredit-backward-slurp-sexp)

;; (define-key paredit-mode-map (kbd "M-b") 'paredit-for)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; elisp
(defun dss/remove-elc-on-save ()
  "If you're saving an elisp file, likely the .elc is no longer valid.
  Comes from the emacs starter kit"
  (add-hook 'after-save-hook
            (lambda ()
              (if (file-exists-p (concat buffer-file-name "c"))
                  (delete-file (concat buffer-file-name "c"))))
            nil t))

(defun dss/elisp-mode-hook ()
  (dss/remove-elc-on-save)
  (eldoc-mode 1)
  (setq mode-name "EL:")
  (setq hippie-expand-try-functions-list
        '(try-expand-dabbrev-visible
          try-complete-lisp-symbol
          try-complete-lisp-symbol-partially
          try-expand-dabbrev)))
(add-hook 'emacs-lisp-mode-hook 'dss/elisp-mode-hook)

(defun dss/find-function-at-point ()
  "Find directly the function at point in the current window."
  (interactive)
  (let ((symb (function-called-at-point)))
    (when symb
      (find-function symb))))

(define-key emacs-lisp-mode-map (kbd "M-.") 'dss/find-function-at-point)

;; ;; http://www.emacswiki.org/emacs/FlymakeElisp
;; (defun flymake-elisp-init ()
;;   (if (string-match "^ " (buffer-name))
;;       nil
;;     (let* ((temp-file   (flymake-init-create-temp-buffer-copy
;;                          'flymake-create-temp-inplace))
;;            (local-file  (file-relative-name
;;                          temp-file
;;                          (file-name-directory buffer-file-name))))
;;       (list
;;        (expand-file-name invocation-name invocation-directory)
;;        (list
;;         "-Q" "--batch" "--eval"
;;         (prin1-to-string
;;          (quote
;;           (dolist (file command-line-args-left)
;;             (with-temp-buffer
;;               (insert-file-contents file)
;;               (condition-case data
;;                   (scan-sexps (point-min) (point-max))
;;                 (scan-error
;;                  (goto-char (nth 2 data))
;;                  (princ (format "%setq:%setq: error: Unmatched bracket or quote\n"
;;                                 file (line-number-at-pos)))))))))
;;         local-file)))))
;; (push '("\\.el$" flymake-elisp-init) flymake-allowed-file-name-masks)
;; (add-hook 'emacs-lisp-mode-hook 'flymake-mode)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; real lisp hackers use the lambda character
;; courtesy of stefan monnier on c.l.l
(defun sm-lambda-mode-hook ()
  (font-lock-add-keywords
   nil `(("\\<lambda\\>"
          (0 (progn (compose-region (match-beginning 0) (match-end 0)
                                    ,(make-char 'greek-iso8859-7 107))
                    nil))))))

(defun dss/pretty-lambda-enable ()
  (interactive)
  (add-hook 'dss-lisp-modes-hook 'sm-lambda-mode-hook)
  (add-hook 'python-mode-hook 'sm-lambda-mode-hook))

(defun dss/pretty-lambda-disable ()
  (interactive)
  (remove-hook 'dss-lisp-modes-hook 'sm-lambda-mode-hook)
  (remove-hook 'python-mode-hook 'sm-lambda-mode-hook))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(provide 'dss-lisps)