-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbartuer-erlang.el
More file actions
executable file
·717 lines (668 loc) · 25.7 KB
/
bartuer-erlang.el
File metadata and controls
executable file
·717 lines (668 loc) · 25.7 KB
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
(setq erlang-skel nil)
(require 'erlang nil t)
(require 'erlang-start nil t)
(require 'distel nil t)
(distel-setup)
(eval-and-compile
(defconst erlang-block-keyword-regexp "[ (\[{]\\(begin\\|case\\|fun *(\\|if\\|end\\|receive\\|try\\)"
))
(eval-and-compile
(defconst erlang-block-beg-regexp "\\(begin\\|case\\|fun\\|if\\|end\\|receive\\|try\\)"
))
(eval-and-compile
(defconst erlang-pattern-match-end-regexp "[a-zA-Z0-9_-]"
))
(defun erlang-current-function-beg ()
"get current function beginning position"
(save-excursion
(erlang-beginning-of-function)
(point)
)
)
(defun erlang-current-function-end ()
"get current function end position"
(save-excursion
(erlang-end-of-function)
(point)
)
)
(defun erlang-at-comment-p ()
"current point in a comment?"
(let* ((from (point))
(bol (save-excursion
(beginning-of-line)
(point)))
(in-comment (search-backward-regexp "% " bol t)))
(goto-char from)
(integerp in-comment))
)
(defun erlang-at-string-p ()
"current point in a string?"
(let* ((from (point))
(bol (save-excursion
(beginning-of-line)
(point)))
(eol (save-excursion
(end-of-line)
(point)))
(string-head (search-backward-regexp "['\"]" bol t))
(string-tail (progn
(goto-char from)
(search-forward-regexp "['\"]" eol t)
)))
(goto-char from)
(and (integerp string-head)
(integerp string-tail)))
)
(defun erlang-word-at-point ()
"return erlang block keyword under point"
(let ((bounds (bounds-of-thing-at-point 'word)))
(if bounds
(buffer-substring-no-properties (car bounds) (cdr bounds))))
)
(defun erlang-skip-blank-and-brace ()
"backward search and find a keyword, remove outer brace at the point"
(erlang-skip-blank)
(when (or
(= (following-char) 91) ;[
(= (following-char) 40) ;(
(= (following-char) 123) ;{
)
(forward-char 1))
)
(defun erlang-backdelete-invoke-brace ()
"forward search and find a fun keyword, back delete invoke part"
(when (= (following-char) 40) ;(
(backward-char)
(while (= (following-char) 32) ;SPACE
(backward-char)))
)
(defun erlang-find-block-keyword (&optional direct)
"direct, if set, will search backward"
(interactive)
(setq case-setting case-fold-search)
(setq case-fold-search nil)
(let ((beg (point))
(end (save-excursion
(if direct
(search-backward-regexp erlang-block-keyword-regexp)
(search-forward-regexp erlang-block-keyword-regexp))
(while (or
(erlang-at-comment-p)
(erlang-at-string-p)
)
(if direct
(search-backward-regexp erlang-block-keyword-regexp)
(search-forward-regexp erlang-block-keyword-regexp)))
(if direct
(erlang-skip-blank-and-brace)
(erlang-backdelete-invoke-brace))
(point)
)))
(goto-char end))
(setq case-fold-search case-setting)
)
(defun erlang-find-block-beg ()
"return begin of most inner block include current position"
(setq erlang-current-block-beg nil)
(let ((fun-beg (erlang-current-function-beg)))
(save-excursion
(let ((stack nil))
(while (and (null erlang-current-block-beg)
(> (point) fun-beg))
(erlang-find-block-keyword t)
(if (string-equal "end" (erlang-word-at-point))
(erlang-push (erlang-word-at-point) stack)
(if (null stack)
(setq erlang-current-block-beg (point))
(erlang-pop stack))
))
)))
erlang-current-block-beg)
(defun erlang-find-block-end ()
"return end of most inner block include current position"
(setq erlang-current-block-end nil)
(let ((fun-end (erlang-current-function-end)))
(save-excursion
(when erlang-current-block-beg
(goto-char erlang-current-block-beg)
(let ((stack nil))
(while (and (null erlang-current-block-end)
(< (point) fun-end))
(erlang-find-block-keyword)
(if (string-equal "end" (erlang-word-at-point))
(if (null stack)
(setq erlang-current-block-end (point))
(erlang-pop stack))
(erlang-push (erlang-word-at-point) stack)
))
)
)))
erlang-current-block-end)
(defun erlang-mark-block ()
"mark most inner block include current position"
(interactive)
(let ((beg (erlang-find-block-beg))
(end (erlang-find-block-end)))
(when (and beg end)
(set-mark end)
(goto-char beg)
))
)
(defun erlang-find-pattern-match-beg ()
"return begin of most meaningful pattern matching include(or behind) current position"
(setq current-pattern-match-point nil)
(save-excursion
(let ((fun-head
(save-excursion (erlang-beginning-of-function 1)
(point)))
(bol (save-excursion (beginning-of-line)
(point))))
(end-of-line)
(search-backward-regexp " ->" fun-head)
(setq current-pattern-match-point (point))
)
(backward-sexp)
(setq erlang-pattern-match-beg (point)))
erlang-pattern-match-beg
)
(defun erlang-pattern-match-data-end-p ()
"left of current position is atom/var/number/macro?
see `erlang-pattern-match-end-regexp' "
(let ((word-at-point (erlang-word-at-point)))
(if (null word-at-point)
nil
(and (stringp word-at-point)
(eq (string-match
erlang-pattern-match-end-regexp word-at-point
) 0)))))
;;; cases covered
;;; YES cases
;; atom end.
;; end;
;; end
;; var Error.
;; Error;
;; Error
;; number 0.
;; 1;
;; 19
;; macro ?MAX_INT.
;; ?MAX_INT;
;; ?MAX_INT
;; list [].
;; [];
;; []
;; tuple {}.
;; {};
;; {}
;; call ().
;; ();
;; ()
;; string "".
;; "";
;; ""
;; ''.
;; '';
;; ''
;;; NO cases
;; not end (end.)
;; , end,
(defun erlang-pattern-match-end-p ()
"current position at pattern match end?"
(setq erlang-current-at-pattern-match-end nil)
(let* ((char-at-point (following-char))
(maybe-end (or
(and
(= (save-excursion
(forward-char)
(following-char))
10)
(= char-at-point 46)) ;.
(and
(= (save-excursion
(forward-char)
(following-char))
10)
(= char-at-point 59)) ;;
(= char-at-point 10) ;\n
)
))
(if maybe-end
(let* ((word-like-list (save-excursion
(backward-char)
(= (following-char) 93) ;]
))
(word-like-tuple (save-excursion
(backward-char)
(= (following-char) 125) ;}
))
(word-like-call (save-excursion
(backward-char)
(= (following-char) 41) ;)
))
(word-like-string (save-excursion
(backward-char)
(or
(= (following-char) 34) ;"
(= (following-char) 39)) ;'
))
;; var, atom, number, macro
(word-like-data (erlang-pattern-match-data-end-p))
)
(setq erlang-current-at-pattern-match-end (or
word-like-list
word-like-tuple
word-like-call
word-like-string
word-like-data
)))
(setq erlang-current-at-pattern-match-end nil)
)
)
erlang-current-at-pattern-match-end
)
(defun erlang-at-block-beg-p ()
"current position just behind block begin keyword?"
(let ((word-at-point (erlang-word-at-point)))
(if (null word-at-point)
nil
(and (stringp word-at-point)
(eq 0
(string-match
erlang-block-beg-regexp
word-at-point))
(not (string-equal "Fun" word-at-point)))))
)
(defun erlang-find-pattern-match-end ()
"return end of most meaningful pattern match include(or behind) current position"
(setq erlang-pattern-match-end nil)
(let ((fun-end (erlang-current-function-end)))
(save-excursion
(when current-pattern-match-point
(goto-char current-pattern-match-point)
(while (and (not (erlang-pattern-match-end-p))
(< (point) fun-end))
(forward-sexp)
(when (erlang-at-block-beg-p)
(goto-char (erlang-find-block-beg))
(goto-char (erlang-find-block-end))
)
)
(setq erlang-pattern-match-end (point))
)))
erlang-pattern-match-end
)
(defun erlang-mark-pattern-match ()
"mark current pattern match"
(interactive)
(let ((beg (erlang-find-pattern-match-beg))
(end (erlang-find-pattern-match-end)))
(when (and beg end)
(set-mark end)
(goto-char beg)
)
)
)
(defun erlang-mark-sexp ()
"select most inner pattern match expression or block to mark"
(interactive)
(let ((block-beg (erlang-find-block-beg))
(block-end (erlang-find-block-end))
(pattern-beg (erlang-find-pattern-match-beg))
(pattern-end (erlang-find-pattern-match-end)))
(cond
((or (and block-beg
block-end
(null pattern-beg)
(null pattern-end))
(and (< block-beg (point))
(> block-end (point))
(or (> pattern-beg (point))
(< pattern-end (point))))) ; block only
(erlang-mark-block)
)
((or (and pattern-beg
pattern-end
(null block-beg)
(null block-end))
(and (< pattern-beg (point))
(> pattern-end (point))
(or (> block-beg (point))
(< block-end (point))))) ; pattern match only
(erlang-mark-pattern-match))
((and (null block-beg)
(null block-end)
(null pattern-beg)
(null pattern-end))
(message "can not find anything to mark; point: %d block: (%d, %d) pattern matching: (%d, %d)"
(point) block-beg block-end pattern-beg pattern-end))
((and (<= block-beg pattern-beg)
(>= block-end pattern-end)) ; mark pattern first, second, outer block, third, outer pattern...
(if (and mark-active
(= (point) pattern-beg))
(erlang-mark-block)
(erlang-mark-pattern-match)))
((and (<= pattern-beg block-beg)
(>= pattern-end block-end)) ; press mark binding again will automatically mark outer pattern match
(erlang-mark-block))
(t
(message "point: %d block: (%d, %d) pattern matching: (%d, %d)"
(point) block-beg block-end pattern-beg pattern-end)))
)
)
(defun erlang-forward-sexp ()
"pick suitable unit from sexp, block and pattern matching, forward to end"
(interactive)
(cond
((erlang-at-block-beg-p)
(forward-sexp)
(let ((block-beg (erlang-find-block-beg))
(block-end (erlang-find-block-end))
)
(goto-char block-end))
)
((= (point) (erlang-find-pattern-match-beg))
(goto-char (erlang-find-pattern-match-end))
)
(t
(forward-sexp)))
)
(defun erlang-backward-sexp ()
"pick suitable unit from sexp, block and pattern matching, backward to beginning"
(interactive)
(cond
((string-equal "end" (erlang-word-at-point))
(backward-sexp)
(let ((block-beg (erlang-find-block-beg))
(block-end (erlang-find-block-end))
)
(goto-char block-beg))
)
((erlang-pattern-match-end-p)
(backward-char)
(let ((pattern-beg (erlang-find-pattern-match-beg))
(pattern-end (erlang-find-pattern-match-end)))
(goto-char pattern-beg)
)
)
(t
(backward-sexp)))
)
(defun erlang-backward-up-list ()
"move to outer level of pattern matching or block"
(interactive)
(let ((block-beg (erlang-find-block-beg))
(block-end (erlang-find-block-end))
(pattern-beg (erlang-find-pattern-match-beg))
(pattern-end (erlang-find-pattern-match-end)))
(cond
((or (and block-beg
block-end
(null pattern-beg)
(null pattern-end))
(and (< block-beg (point))
(> block-end (point))
(or (> pattern-beg (point))
(< pattern-end (point))))) ; block only
(erlang-mark-block)
(deactivate-mark)
)
((or (and pattern-beg
pattern-end
(null block-beg)
(null block-end))
(and (< pattern-beg (point))
(> pattern-end (point))
(or (> block-beg (point))
(< block-end (point))))) ; pattern match only
(erlang-mark-pattern-match)
(deactivate-mark))
((and (null block-beg)
(null block-end)
(null pattern-beg)
(null pattern-end))
(message "can not find anything to mark; point: %d block: (%d, %d) pattern matching: (%d, %d)"
(point) block-beg block-end pattern-beg pattern-end))
((and (<= block-beg pattern-beg)
(>= block-end pattern-end)) ; mark pattern first, second, outer block, third, outer pattern...
(if (= (point) pattern-beg)
(progn (erlang-mark-block)
(deactivate-mark))
(erlang-mark-pattern-match)
(deactivate-mark)))
((and (<= pattern-beg block-beg)
(>= pattern-end block-end)) ; press mark binding again will automatically mark outer pattern match
(erlang-mark-block)
(deactivate-mark)
)
(t
(message "point: %d block: (%d, %d) pattern matching: (%d, %d)"
(point) block-beg block-end pattern-beg pattern-end)))
)
)
(defun has-ect-in-region-p (beg end)
"search %# => in region beg end
"
(goto-char beg)
(if (search-forward-regexp "%#=>" end t)
t
nil)
)
(defun ect-dwim ()
"if there is %# =>, remove it, otherwise insert it
2 + 3 %
docstring as fixture, try above line.
"
(setq current-pos (point))
(setq beg (progn (beginning-of-line) (point)))
(setq end (progn (end-of-line) (point)))
(if (has-ect-in-region-p beg end)
(progn
(goto-char beg)
(search-forward "%")
(backward-char)
(kill-line))
(goto-char current-pos)
(insert "#=>"))
)
(defadvice comment-dwim (around jct-hack activate)
"If comment-dwim is successively called, add //#=> mark."
(if (and (eq major-mode 'erlang-mode)
(eq last-command 'comment-dwim)
)
(ect-dwim)
ad-do-it))
(defun otp-grep (command-args)
"Run grep over otp documents"
(interactive
(progn
(grep-compute-defaults)
(list (read-shell-command "grep otp docs: "
"find ~/local/share/doc/erlang/OTP13B04_TEXT -type f |grep -vE \"BROWSE|TAGS|.svn|drw|Binary|.bzr|svn-base|*.pyc\" |xargs grep -niHE " 'grep-find-history))))
(when command-args
(let ((null-device nil)) ; see grep
(grep command-args))))
(defun erl-choose-emacs-node ()
"set default nodename emacs@bartuer ."
(interactive)
(let* ((nodename-string (if erl-nodename-cache
(symbol-name erl-nodename-cache)
nil))
(name-string "emacs@bartuer")
(name (intern (if (string-match "@" name-string)
name-string
(concat name-string
"@" (erl-determine-hostname))))))
(setq erl-nodename-cache name)
(pushnew erl-nodename-cache erl-nodes)
(setq distel-modeline-node name-string)
(force-mode-line-update))
erl-nodename-cache)
(defun inferior-erlang-without-switch-buffer ()
"Run an inferior Erlang.
This is just like running Erlang in a normal shell, except that
an Emacs buffer is used for input and output.
\\<comint-mode-map>
The command line history can be accessed with \\[comint-previous-input] and \\[comint-next-input].
The history is saved between sessions.
Entry to this mode calls the functions in the variables
`comint-mode-hook' and `erlang-shell-mode-hook' with no arguments.
The following commands imitate the usual Unix interrupt and
editing control characters:
\\{erlang-shell-mode-map}"
(interactive)
(require 'comint)
(let ((opts inferior-erlang-machine-options))
(cond ((eq inferior-erlang-shell-type 'oldshell)
(setq opts (cons "-oldshell" opts)))
((eq inferior-erlang-shell-type 'newshell)
(setq opts (append '("-newshell" "-env" "TERM" "vt100") opts))))
(setq inferior-erlang-buffer
(apply 'make-comint
inferior-erlang-process-name inferior-erlang-machine
nil opts)))
(setq inferior-erlang-process
(get-buffer-process inferior-erlang-buffer))
(if (> 21 erlang-emacs-major-version) ; funcalls to avoid compiler warnings
(funcall (symbol-function 'set-process-query-on-exit-flag)
inferior-erlang-process nil)
(funcall (symbol-function 'process-kill-without-query) inferior-erlang-process))
(with-current-buffer inferior-erlang-buffer
(if (and (not (eq system-type 'windows-nt))
(eq inferior-erlang-shell-type 'newshell))
(setq comint-process-echoes t))
;; `rename-buffer' takes only one argument in Emacs 18.
(condition-case nil
(rename-buffer inferior-erlang-buffer-name t)
(error (rename-buffer inferior-erlang-buffer-name)))
(erlang-shell-mode))
)
(defun start-default-emacs-node ()
"start node for distel"
(interactive)
(if (null (inferior-erlang-running-p))
(progn
(inferior-erlang-without-switch-buffer)
(sleep-for 0.5)
(erl-choose-emacs-node)
(erl-ping erl-nodename-cache)
)
(erl-ping erl-nodename-cache))
)
(defun erl-prepare-debugger (node module file)
"Toggle debug-interpreting of the current buffer's module."
(remove-hook 'erl-backend-loaded 'erl-prepare-debugger)
(when (edb-ensure-monitoring node)
(erl-spawn
(erl-set-name "EDB RPC to toggle interpretation of %S on %S"
module node)
(erl-send-rpc node 'distel 'debug_toggle (list module file))
(erl-receive (module file)
((['rex 'interpreted]
(progn
;; restart monitor
(kill-buffer edb-monitor-buffer)
(edb-monitor erl-nodename-cache)
;; show next steps
(progn (find-file-other-window "~/scripts/cheat-sheet")
(goto-char (point-min))
(search-forward-regexp "redebug distel erlang")
(recenter-top-bottom 0)
(split-window-vertically)
(find-file-other-window file))
(message "Interpreting: %S" module)))
(['rex 'uninterpreted]
(message "Stopped interpreting: %S" module))
(['rex ['badrpc reason]]
(message "Failed to interpret-toggle: %S" reason))))))
)
(defun erdebug ()
"quick start debug session
emacs erlang
|
compile------------------------------------------>|
|
distel load . |
. |
code:ensure_load(distel)---------------->|
. |
. |
. |
.<-------module distel--------------|
. |
. |
erl_backend_loaded . |
. |
distel:debug_toggle---->|
. |
. |
. |
<--interpreted----|
. |
. |
restart monitor |
"
(interactive)
(setq erl-current-debug-module (intern (erlang-get-module)))
(setq erl-current-debug-file (buffer-file-name))
;; will clean the hook in `erl-prepare-debugger`
(add-hook 'erl-backend-loaded 'erl-prepare-debugger)
(save-excursion
;; compile with debug info
(let ((prefix-setting current-prefix-arg))
;; compile with debug info
(setq current-prefix-arg t)
(inferior-erlang-compile '-)
(setq current-prefix-arg prefix-setting))
)
;; check distel loaded
(let ((node erl-nodename-cache)
(module erl-current-debug-module)
(file erl-current-debug-file)
)
(unless distel-inhibit-backend-check
(erl-spawn ; first point maybe invoke hook erl-backend-loaded
(erl-send `[rex ,node]
`[,erl-self [call
code ensure_loaded (distel)
,(erl-group-leader)]])
;; ensure distel loaded
(erl-receive (node module file)
((['rex ['error _]]
(&erl-load-backend node)) ; second point maybe invoke hook erl-backend-loaded
(_ (erl-prepare-debugger node module file))) ; third point maybe invoke hook erl-backend-loaded
)))))
(defun bartuer-erlang-load ()
"for erlang language"
(setq erlang-root-dir "/usr/local/otp")
(setq inferior-erlang-machine-options '("-sname" "emacs"))
(add-hook 'erlang-shell-mode-hook (lambda ()
(define-key erlang-shell-mode-map "\C-\M-i" 'erl-complete)
(define-key erlang-shell-mode-map "\M-." 'erl-find-source_under-point)
(define-key erlang-shell-mode-map "\M-," 'erl-find-source_unwind)
))
(define-key erlang-mode-map "\C-c\C-e" 'erl-eval-expression)
(define-key erlang-mode-map "\C-c\C-i" 'erl-session-minor-mode)
(define-key erlang-mode-map "\C-\M-f" 'erlang-forward-sexp)
(define-key erlang-mode-map "\C-\M-b" 'erlang-backward-sexp)
(define-key erlang-mode-map "\C-\M-u" 'erlang-backward-up-list)
(define-key erlang-mode-map "\M-h" 'erlang-mark-sexp)
(define-key erlang-mode-map "\M-q" (lambda ()
(erlang-indent-function)
(erlang-align-arrows)))
(define-key erlang-mode-map "\C-\M-h" 'erlang-mark-function)
(define-key erlang-mode-map "\C-\M-e" 'erlang-end-of-function)
(define-key erlang-mode-map "\C-\M-a" 'erlang-beginning-of-function)
(define-key erlang-mode-map "\M--" (lambda ()
(interactive)
(insert " -> ")) )
(save-excursion
(start-default-emacs-node)
)
(flymake-mode t)
(sleep-for 0.5)
(erl-ie-ensure-registered erl-nodename-cache)
(erl-session-minor-mode t)
)
(provide 'bartuer-erlang)