/
erefactor.el
1296 lines (1133 loc) · 44.2 KB
/
erefactor.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
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
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; erefactor.el --- Emacs-Lisp refactoring utilities
;; Author: Masahiro Hayashi <mhayashi1120@gmail.com>
;; Keywords: extensions, tools, maint
;; URL: https://github.com/mhayashi1120/Emacs-erefactor
;; Emacs: GNU Emacs 24 or later
;; Package-Requires: ((cl-lib "0.3"))
;; Version: 0.7.1
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation; either version 3, or (at
;; your option) any later version.
;; This program is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; Commentary:
;; Simple refactoring, linting utilities for Emacs-Lisp.
;; ## Install:
;; Put this file into load-path'ed directory,
;; and byte compile its if desired.
;; And put the following expression into your ~/.emacs.
;;
;; (eval-after-load 'lisp-mode
;; '(progn
;; (require 'erefactor)
;; (define-key emacs-lisp-mode-map "\C-c\C-v" erefactor-map)))
;;
;; And set these variables correctly.
;; `erefactor-lint-path-alist`, `erefactor-lint-by-emacsen`
;; Put the following in your .emacs, if you desire highlighting local variable.
;;
;; (add-hook 'emacs-lisp-mode-hook 'erefactor-lazy-highlight-turn-on)
;; (add-hook 'lisp-interaction-mode-hook 'erefactor-lazy-highlight-turn-on)
;; ## Usage:
;; `C-c C-v l` : elint current buffer in clean environment.
;; `C-c C-v L` : elint current buffer by multiple emacs binaries.
;; See `erefactor-lint-emacsen`
;; `C-c C-v r` : Rename symbol in current buffer.
;; Resolve `let` binding as long as i can.
;; `C-c C-v R` : Rename symbol in requiring modules and current buffer.
;; `C-c C-v h` : Highlight current symbol in this buffer
;; and suppress `erefacthr-highlight-mode`.
;; `C-c C-v d` : Dehighlight all by above command.
;; `C-c C-v c` : Switch prefix bunch of symbols.
;; ex: '(hoge-var hoge-func) -> '(foo-var foo-func)
;; `C-c C-v ?` : Display flymake elint warnings/errors
;; * To show compilation warnings when evaluate `defun` form.
;;
;; M-x erefactor-check-eval-mode
;;; TODO:
;; * Change only same case if symbol. But docstring is not.
;;
;; * `.' is not a separator of lisp symbol.
;; rename `region` symbol and `REGION.` in docstring
;; don't use re-search while idiom,
;; gather symbols in code, string, comment each context.
;; (defun hoge (region)
;; "REGION is REGION."
;; region)
;;
;; don't forget about `quoted` symbol.
;; * macroexpand misunderstand local variable
;;
;; (defmacro hogemacro (&rest form)
;; `(progn
;; ,@form
;; (let (a))))
;; =>
;; (hogemacro a) <= `a' is not bounded in this context.
;;
;; * Separate elint and flymake to other elisp file.
;;
;; * flymake not works emacs-22
;; * Slowing down the Emacs when too many macroexpand in highlight-symbol-mode
;;; Code:
(require 'cl-lib)
(eval-when-compile
(require 'easy-mmode))
;; externals
(defvar load-path)
(defvar shell-command-switch)
(defvar shell-file-name)
(defvar load-history)
(defvar idle-update-delay) ;; after Emacs 22
(defvar this-command)
(defvar timer-idle-list)
(defvar current-prefix-arg)
(defvar unread-command-events)
(defvar buffer-file-coding-system)
(defgroup erefactor nil
"Emacs Lisp Refactoring utilities"
:group 'lisp
:prefix "erefactor-")
;;;
;;; Basic utilities
;;;
(defun erefactor-macrop (symbol)
(and
(symbolp symbol)
(fboundp symbol)
(eq (car-safe (indirect-function symbol)) 'macro)))
(defun erefactor-ref (key list)
(cdr (assoc key list)))
(defun erefactor-emacs-version (command &optional major-only)
(with-temp-buffer
(call-process command nil (current-buffer) nil "-version")
(let ((output (buffer-string)))
(unless (string-match "Emacs *\\(\\([0-9]+\\)\\.[0-9]+\\.[0-9]+\\)" output)
(error "Unable get version"))
(if major-only
(string-to-number (match-string 2 output))
(match-string 1 output)))))
(defun erefactor-mapconcat (func list)
(apply 'append (mapcar func list)))
(defun erefactor--union (list1 list2)
(cl-loop for a in (append list1 list2)
unless (member a res)
collect a into res
finally return res))
(defun erefactor--find (pred list)
(cl-loop for a in list
if (funcall pred a)
return a))
(defmacro erefactor-interactive-p ()
(cond
((= emacs-major-version 21)
`(interactive-p))
((or (= emacs-major-version 22)
(and (version<= emacs-version "23.2")
(not (version= emacs-version "23.2"))))
`(called-interactively-p))
(t
`(called-interactively-p 'any))))
(defun erefactor-create-regexp (symbol)
"Create SYMBOL exclusive regexp."
(format "\\_<\\(%s\\)\\_>" (regexp-quote symbol)))
(defun erefactor-create-prefixed-regexp (prefix)
"Create matching to PREFIX exclusive regexp."
(format "\\_<\\(\\(%s\\)\\(\\(?:\\s_\\|\\sw\\)*\\)\\)\\_>"
(regexp-quote prefix)))
;;
;; Inner functions
;;
(defun erefactor--symbol-group-prefix (symbol)
(let ((symbol-name (symbol-name symbol))
most-prefix len p)
(mapatoms
(lambda (s)
(when (and (setq p (get s 'custom-prefix))
(stringp p)
(string-match (concat "^" (regexp-quote p)) symbol-name)
(or (null len) (< len (match-end 0))))
(setq len (match-end 0))
(setq most-prefix p)))
obarray)
most-prefix))
(defun erefactor--guessed-using-files (symbol)
(let (ret file)
(let* ((prefix (erefactor--symbol-group-prefix symbol))
(prefix-regexp (concat "^" (regexp-quote prefix))))
(mapatoms
(lambda (s)
(when (and (string-match prefix-regexp (symbol-name s))
(setq file (symbol-file s)))
;; if byte compiled file
(when (string-match "^\\(.*\\.el\\)c$" file)
(setq file (match-string 1 file)))
(add-to-list 'ret file)))
obarray))
(let ((files (append
(erefactor--symbol-using-sources 'defun symbol)
(erefactor--symbol-using-sources 'defvar symbol)
(erefactor--symbol-using-sources 'defface symbol))))
(setq ret (erefactor--union files ret)))
ret))
(defun erefactor--find-local-binding (name)
(let* ((first (point))
(symbol (intern name))
(history (cons first nil))
previous)
(save-excursion
(catch 'found
(condition-case nil
(while t
(backward-up-list)
(let* ((start (point-marker))
(form (read (current-buffer)))
(end (point-marker))
(special-bind (erefactor--special-binding
symbol form history)))
;; detect looping
;; list start char by syntax-table
(when (and previous (= (car previous) start))
(signal 'scan-error nil))
(when special-bind
(throw 'found special-bind))
(when (or
(erefactor--local-binding-p symbol form)
(erefactor--macroexpand-contains-p symbol form))
(throw 'found (cons start end)))
(setq previous (cons start end))
(setq history (cons previous history))))
(scan-error nil))))))
(defun erefactor--special-binding (name form history)
"NAME scope is not single sexp."
(erefactor--local-fbinding name form history))
(defun erefactor--local-fbinding (name form history)
(when (memq (car-safe form) '(flet macrolet labels))
(save-excursion
;; ignore all error because `flet' case is special!!
(condition-case nil
(let ((region (cadr history))
(first (car (last history))))
(when (and (consp region)
(< (car region) first)
(> first (car region)))
;; at start definition of local function
;; (flet ((func (a b) (list a b))))
;; ^^
(goto-char (car region))
(forward-char)
(forward-sexp) ;; end of function name
(let ((args (read (current-buffer))))
(when (erefactor--lambda-binding-contains-p args name)
region))))
(error nil)))))
(defun erefactor--local-binding-p (name form)
(or
;; FIXME: difference between let and let*
(and (memq (car-safe form) '(let let* lexical-let lexical-let*))
(erefactor--let-binding-contains-p (cadr form) name))
(and (memq (car-safe form) '(defun defmacro))
(erefactor--lambda-binding-contains-p (cl-caddr form) name))
(and (eq (car-safe form) 'lambda)
(erefactor--lambda-binding-contains-p (cadr form) name))
(and (eq (car-safe form) 'defadvice)
(erefactor--defadvice-binding-contains-p (cl-caddr form) name))
(and (eq (car-safe form) 'catch)
(erefactor--catch-binding-contains-p (cdr form) name))
(and (eq (car-safe form) 'condition-case)
(erefactor--condition-case-contains-p (cdr form) name))
(and (eq (car-safe form) 'eieio-defmethod)
(erefactor--eieio-defmethod-contains-p (cl-caadr (cl-caddr form)) name))))
;; To avoid too many recursion.
;; Slow down the emacs if form contains a lot of macro. (ex: ert-deftest)
(defvar erefactor--macroexpand-depth 0)
;; This default value means to adequate the `loop' macro
(defvar erefactor--macroexpand-max-depth 3)
(defun erefactor--macroexpand-contains-p (name form)
;; `lambda' is macro expanded like (function (lambda () ...))
(when (and (< erefactor--macroexpand-depth erefactor--macroexpand-max-depth)
(not (memq (car-safe form) '(lambda)))
(erefactor-macrop (car-safe form)))
(condition-case nil
(let ((erefactor--macroexpand-depth (1+ erefactor--macroexpand-depth))
(expand-form (macroexpand form)))
(catch 'found
(when (erefactor--local-binding-p name expand-form)
(throw 'found t))
(when (erefactor--binding-exists-p name expand-form)
(throw 'found t))
nil))
(error nil))))
(defun erefactor--binding-exists-p (name form)
(catch 'found
(dolist (f form)
(when (or (erefactor--local-binding-p name f)
(erefactor--macroexpand-contains-p name f))
(throw 'found t))
(when (and (listp f)
(erefactor--binding-exists-p name f))
(throw 'found t)))))
(defun erefactor--condition-case-contains-p (form name)
(let ((var (car-safe form)))
;; error binded variable and must be non-nil value
(when (and (atom var) var)
(eq var name))))
(defun erefactor--let-binding-contains-p (let-arg name)
(or (memq name let-arg)
(assq name let-arg)))
(defun erefactor--lambda-binding-contains-p (lambda-arg name)
(and (not (memq name '(&optional &rest)))
(memq name lambda-arg)))
(defun erefactor--eieio-defmethod-contains-p (method-arg name)
(and (not (memq name '(&optional &rest)))
(or (memq name method-arg)
(assq name method-arg))))
(defun erefactor--defadvice-binding-contains-p (ad-args name)
(let* ((rest (cddr ad-args))
;; consider optional position arg
(args (cond
((consp (car rest))
(car rest))
((consp (cadr rest))
(cadr rest))
(t nil))))
(erefactor--lambda-binding-contains-p args name)))
(defun erefactor--catch-binding-contains-p (catch-arg name)
"Consider (catch variable ...) like form."
(and (listp (car catch-arg))
(eq (caar catch-arg) 'quote)
(symbolp (cl-cadar catch-arg))
(eq (cl-cadar catch-arg) name)))
(defun erefactor-context-code-p (&optional point)
(save-excursion
(let ((parses (parse-partial-sexp (point-min) (or point (point)))))
(and (not (nth 3 parses))
(not (nth 4 parses))))))
(defun erefactor-context-string-p (&optional point)
(save-excursion
(let ((parses (parse-partial-sexp (point-min) (or point (point)))))
(nth 3 parses))))
(defun erefactor-context-comment-p (&optional point)
(save-excursion
(let ((parses (parse-partial-sexp (point-min) (or point (point)))))
(nth 4 parses))))
;; list only generally using
(defvar erefactor-def-alist
'(
;; Variable cell
(defvar defvar)
(defcustom defvar)
(defconst defvar)
;; Function cell
(defun defun)
(defmacro defun)
(defun* defun)
(defmacro* defun)
;; Face
(defface defface)
;; Misc
(define-minor-mode defun defvar)
))
(defun erefactor--current-fnsym ()
(save-excursion
(let (ret)
(condition-case nil
(while (not (bobp))
(let ((sym (thing-at-point 'symbol)))
(backward-sexp)
(when sym
(push (intern sym) ret))
(skip-syntax-backward " ")))
(scan-error nil))
ret)))
(defun erefactor--symbol-defined-alist (symbol)
;; FUNCS FACES VARS have file names.
(let (funcs faces vars)
(cl-loop for (file . entries) in load-history
do (let (tmp)
(when (memq symbol entries)
(push file vars))
(when (and (setq tmp (rassq symbol entries))
(eq (car tmp) 'defface))
(push file faces))
(when (and (setq tmp (rassq symbol entries))
(eq (car tmp) 'defun))
(push file funcs))))
`((defun . ,funcs)
(defface . ,faces)
(defvar . ,vars))))
(defun erefactor--add-load-name (file type symbol)
;; When duplicated definition exists, `load-history' simply have
;; duplicated values.
(let ((hist (assoc file load-history)))
(unless hist
(error "%s is not loaded" file))
(setcdr (last hist)
(cond
((memq type '(defun defface))
(list (cons type symbol)))
((eq type 'defvar)
(list symbol))))))
(defun erefactor--change-load-name (old-symbol new-symbol type)
(let ((defs (erefactor--find-load-history type old-symbol)))
(dolist (def defs)
(cond
((memq type '(defun defface))
(setcdr def new-symbol))
(t
(setcar def new-symbol))))))
(defun erefactor--find-load-history (type symbol)
(let* ((defs (erefactor--symbol-defined-alist symbol))
(files (cdr (assq type defs)))
(res '()))
(dolist (file files)
(let ((def (cdr (assoc file load-history))))
(cond
((memq type '(defun defface))
(let ((tmp (rassq symbol def)))
(when (and tmp (car tmp) type)
(setq res (cons tmp res)))))
(t
(let ((tmp (memq symbol def)))
(when tmp
(setq res (cons tmp res))))))))
res))
(defun erefactor--symbol-package (type symbol)
(let* ((defs (erefactor--symbol-defined-alist symbol))
(files (cdr (assq type defs))))
(catch 'done
(dolist (file files)
(let ((tmp (cdr (assq 'provide (cdr (assoc file load-history))))))
(when tmp
(throw 'done tmp)))))))
;; get a sources that is defined SYMBOL as TYPE
(defun erefactor--symbol-using-sources (type symbol)
(let ((package (erefactor--symbol-package type symbol)))
(cl-loop for defs in load-history
when (cl-loop for def in (cdr defs)
when (and (listp def)
(eq (car def) 'require)
(eq package (cdr def)))
collect def)
collect (car defs))))
(defun erefactor-after-rename-symbol (old-name new-name)
(save-match-data
(let ((fnsym (erefactor--current-fnsym))
(old (intern old-name))
(new (intern new-name)))
;; re-define definition.
;; if `defvar' or `defcustom' current value will be cleared.
(condition-case err
(eval-defun nil)
(error
(message "%s" err)
(sit-for 0.1)))
;; after renaming is succeeded
(when (eq (cadr fnsym) new)
(let ((types (cdr (assq (car fnsym) erefactor-def-alist))))
;; try to change `load-history'
(dolist (type types)
(erefactor--change-load-name old new type)))))))
(defvar erefactor--read-symbol-history nil)
(defun erefactor-rename-symbol-read-args ()
(let (current-name prompt new-name)
(barf-if-buffer-read-only)
(unless (setq current-name (thing-at-point 'symbol))
(error "No symbol at point"))
(setq prompt (format "%s -> New name: " current-name))
(setq new-name
(read-string
prompt current-name
'erefactor--read-symbol-history))
(when (string= current-name new-name)
(error "No difference"))
(list current-name new-name)))
(defvar erefactor--read-prefix-history nil)
(defun erefactor-change-prefix-read-args ()
(let (current-prefix prompt new-prefix)
(barf-if-buffer-read-only)
(setq current-prefix (thing-at-point 'symbol))
(setq current-prefix
(read-string
"Changing prefix: " current-prefix
'erefactor--read-prefix-history))
(setq prompt (format "Changing prefix: %s -> New prefix: " current-prefix))
(setq new-prefix
(read-string
prompt current-prefix 'erefactor--read-prefix-history))
(when (string= current-prefix new-prefix)
(error "No difference"))
(list current-prefix new-prefix)))
(defvar erefactor--overlay nil)
(defvar erefactor--region-start nil)
(defvar erefactor--region-end nil)
(defun erefactor-dehighlight-in-interactive ()
"Dehighlight text by `erefactor-re-highlight-in-interactive'."
(when erefactor--overlay
(delete-overlay erefactor--overlay))
(erefactor-hl--dehighlight-all))
(defun erefactor-re-highlight-in-interactive (regexp beg fin)
"Highlight REGEXP between BEG and FIN in region
`erefactor--region-start' to `erefactor--region-end'."
;; highlight replacing text
(if (overlayp erefactor--overlay)
(move-overlay erefactor--overlay beg fin (current-buffer))
(setq erefactor--overlay (make-overlay beg fin))
;; higher than erefactor-highlight-face
(overlay-put erefactor--overlay 'priority 100)
(overlay-put erefactor--overlay 'face 'query-replace))
;; highlight scheduled replacing text.
(erefactor-hl--update-region
erefactor--region-start erefactor--region-end
regexp t))
(defface erefactor-highlight-face
'((t (:inherit match)))
"Face for highlighting of matches."
:group 'erefactor)
(defvar erefactor-highlight-face 'erefactor-highlight-face)
(defvar erefactor-hl--overlays nil)
(make-variable-buffer-local 'erefactor-hl--overlays)
(defvar erefactor-highlight-map nil)
(unless erefactor-highlight-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "M-<left>") 'erefactor-highlight-previous-symbol)
(define-key map (kbd "M-<right>") 'erefactor-highlight-next-symbol)
(setq erefactor-highlight-map map)))
(defun erefactor-hl--update-region (start end regexp &optional ignore-case check)
"highlight START to END word that match to REGEXP.
CHECK is function that accept no arg and return boolean."
(save-match-data
(save-excursion
(goto-char start)
(let ((case-fold-search ignore-case))
(setq erefactor-hl--overlays nil)
(while (and (re-search-forward regexp nil t)
(< (point) end))
(when (or (null check)
(save-match-data (funcall check)))
(let ((ov (make-overlay (match-beginning 0) (match-end 0))))
(overlay-put ov 'priority 1) ;; few value
(overlay-put ov 'face erefactor-highlight-face)
(overlay-put ov 'erefactor-overlay-p t)
;;FIXME not activated immediately if be in the timer.
(overlay-put ov 'keymap erefactor-highlight-map)
(setq erefactor-hl--overlays
(cons ov erefactor-hl--overlays)))))))))
(defun erefactor-hl--dehighlight-all ()
(save-match-data
(dolist (ov (overlays-in (point-min) (point-max)))
(when (overlay-get ov 'erefactor-overlay-p)
(delete-overlay ov))))
(setq erefactor-hl--overlays nil))
(defmacro erefactor--with-file (file &rest form)
(declare (indent 1))
`(let ((win (selected-window))
buffer opened)
(if (setq buffer (get-file-buffer file))
(setq opened t)
(setq buffer (find-file-noselect file)))
(unwind-protect
(with-current-buffer buffer
(save-window-excursion
(set-window-buffer win buffer)
(let (buffer-read-only)
,@form)))
(unless opened
(when (buffer-live-p buffer)
(unless (buffer-modified-p buffer)
(kill-buffer buffer)))))))
(defun erefactor--already-bounded (symbol start end)
"SYMBOL is already bounded or not in region START END."
(save-excursion
(goto-char start)
;; search only symbol. (if possible...)
(let (case-fold-search)
(and (re-search-forward (erefactor-create-regexp symbol) nil t)
(< (point) end)))))
(defun erefactor-rename-region (symbol new-symbol &optional region)
"Rename SYMBOL to NEW-SYMBOL in REGION."
(let ((start (if region (car region) (point-min)))
(end (save-excursion
(goto-char (if region (cdr region) (point-max)))
(point-marker)))
regexp)
(when (or (not (erefactor--already-bounded new-symbol start end))
(y-or-n-p (format "%s is already bound. Continue? " new-symbol)))
(save-excursion
(setq erefactor--region-start start)
(setq erefactor--region-end end)
(goto-char start)
(setq regexp (erefactor-create-regexp symbol))
;; cannot use narrow-to-region because is unnatural while
;; interactive loop
(while (and (re-search-forward regexp nil t)
(< (point) end))
;; to protect destroying match
(let ((target (match-data)))
(goto-char (match-end 1))
(erefactor-re-highlight-in-interactive
regexp (match-beginning 1) (match-end 1))
(unwind-protect
(when (y-or-n-p "Rename? ")
;; restore match data (timer maybe destroy match-data)
(set-match-data target)
(replace-match new-symbol t nil nil 1)
(erefactor-after-rename-symbol symbol new-symbol))
(erefactor-dehighlight-in-interactive))))))))
(defun erefactor-change-symbol-prefix (prefix new-prefix)
"Switch symbol PREFIX to NEW-PREFIX in buffer."
(save-excursion
(setq erefactor--region-start (point-min))
(setq erefactor--region-end (point-max))
(goto-char (point-min))
(let ((regexp (erefactor-create-prefixed-regexp prefix)))
;; cannot use narrow-to-region because is unnatural while
;; interactive loop
(while (re-search-forward regexp nil t)
(let ((target (match-data))
(suffix (match-string 3)))
(goto-char (match-end 1))
(erefactor-re-highlight-in-interactive
regexp (match-beginning 2) (match-end 2))
(let* ((old-name (concat prefix suffix))
(new-name (concat new-prefix suffix)))
(unwind-protect
(when (y-or-n-p "Rename? ")
;; restore match data (timer maybe destroy match-data)
(set-match-data target)
(replace-match new-prefix t nil nil 2)
(erefactor-after-rename-symbol old-name new-name))
(erefactor-dehighlight-in-interactive))))))))
;;;
;;; lazy highlight local variable
;;;
(defvar ahs-face-check-include-overlay)
(defvar erefactor-lhl--timer nil)
(defvar erefactor-lhl--suspended nil)
(make-variable-buffer-local 'erefactor-lhl--suspended)
(defvar erefactor-highlight-mode)
(defun erefactor-hl--dehighlight-after-change (start end old-len)
(ignore-errors
(remove-hook 'after-change-functions 'erefactor-hl--dehighlight-after-change)
(erefactor-dehighlight-all-symbol)))
(defun erefactor-lhl--stop ()
(when erefactor-lhl--timer
(unless (catch 'done
(dolist (buf (buffer-list))
(with-current-buffer buf
(when erefactor-highlight-mode
(throw 'done t)))))
(cancel-timer erefactor-lhl--timer)
(setq erefactor-lhl--timer nil))))
(defun erefactor-lhl--start ()
(or
(and erefactor-lhl--timer
(memq erefactor-lhl--timer timer-idle-list))
(setq erefactor-lhl--timer
(run-with-idle-timer idle-update-delay t
'erefactor-lhl--highlight))))
(defun erefactor-lhl--dehihglight ()
(erefactor-hl--dehighlight-all))
(defun erefactor-lhl--post-command ()
(erefactor-lhl--dehihglight)
(remove-hook 'post-command-hook 'erefactor-lhl--post-command))
(defun erefactor-lhl--highlight ()
(save-match-data
(with-local-quit
(condition-case nil
(cond
;; ignore when other command executing.
;; ex: erefactor-rename-symbol-*
(this-command)
((not erefactor-highlight-mode))
;; t means suppress lazy highlight
((eq erefactor-lhl--suspended t))
(t
(save-match-data
(erefactor-lhl--dehihglight)
(let ((symbol (thing-at-point 'symbol)))
(when symbol
(let ((region (erefactor--find-local-binding symbol)))
(when region
(erefactor-hl--update-region
(car region) (cdr region)
(erefactor-create-regexp symbol)
nil 'erefactor-context-code-p)
;;FIXME keymap not updated
;; (add-hook 'post-command-hook 'erefactor-lhl--post-command)
)))))))
;; completely ignore all errors
(error nil)))))
(defun erefactor-hl--move-symbol (forward-p)
(let* ((ovs (sort (copy-sequence erefactor-hl--overlays)
`(lambda (x y)
(,(if forward-p '< '>)
(overlay-start x)
(overlay-start y)))))
(ov (erefactor--find (lambda (x) (overlay-get x 'erefactor-overlay-p))
(overlays-at (point))))
(ov2 (cadr (memq ov ovs))))
(when (or ov2 ovs)
(let ((next (overlay-start (or ov2 (car ovs)))))
(when next
(goto-char next))))))
(define-minor-mode erefactor-highlight-mode
"Toggle highlight mode on or off.
In highlight mode, the highlight the current symbol if recognize
as a local variable.
"
:group 'erefactor
(cond
(erefactor-highlight-mode
(erefactor-lhl--start)
;; FIXME suppress auto-highlight (auto-highlight-symbol.el)
(set (make-local-variable 'ahs-face-check-include-overlay) t))
(t
(erefactor-lhl--stop)
(erefactor-lhl--dehihglight)
(kill-local-variable 'ahs-face-check-include-overlay))))
;;;###autoload
(defun erefactor-lazy-highlight-turn-on ()
(erefactor-highlight-mode 1))
(defun erefactor-lazy-highlight-suspend ()
(setq erefactor-lhl--suspended t))
(defun erefactor-lazy-highlight-resume ()
(setq erefactor-lhl--suspended nil))
(defun erefactor-highlight-previous-symbol ()
"Move to previous highlighting symbol."
(interactive)
(erefactor-hl--move-symbol nil))
(defun erefactor-highlight-next-symbol ()
"Move to next highlighting symbol."
(interactive)
(erefactor-hl--move-symbol t))
;;;
;;; Simple check for evaluating function
;;;
(defvar erefactor-check--eval-alist
'((eval-last-sexp after erefactor-check-eval-last-sexp)
(eval-defun after erefactor-check-eval-defun)))
;; hack function ;-)
(defun erefactor-check--expand-closure (closure)
(unless (eq (car-safe closure) 'closure)
(error "Not a closure"))
(let ((env (nth 1 closure))
(vars (nth 2 closure))
(body (nthcdr 3 closure)))
(let ((let-vars nil)
(def-vars nil))
(mapc
(lambda (v)
(cond
((consp v)
(push (list (car v) (cdr v)) let-vars))
((eq v t))
((symbolp v)
(push v def-vars))
(t (error "Not yet supported %s" v))))
env)
(append
(mapcar (lambda (v) `(defvar ,v)) def-vars)
`((let ,let-vars (defun dummy ,vars ,@body)))))))
(defun erefactor-check--closure-pseudo-source (closure)
(let ((code (erefactor-check--expand-closure closure)))
(with-output-to-string
(dolist (c code)
(pp c)))))
(defun erefactor-check--form (form)
(cond
((memq (car-safe form) '(defun defun* defsubst))
(let ((func (cadr form)))
(when (and (symbolp func)
(functionp func))
(erefactor-check--function func))))))
(defun erefactor-check--function (function)
(let ((warns (erefactor-check--function-warnings function)))
(when warns
(let ((msg (mapconcat
(lambda (x)
(propertize x 'face font-lock-warning-face))
warns ", ")))
(message "%s -> %s" (current-message) msg)
(ding)))))
(defun erefactor-check--function-warnings (function)
(let ((raw (symbol-function function)))
(unwind-protect
(let ((buf (get-buffer "*Compile-Log*"))
end)
(when buf
(with-current-buffer buf
(setq end (point-max))))
;; inhibit displaying *Compile-Log* window
(save-window-excursion
(let ((inhibit-redisplay t)
(byte-compile-warnings t)
(byte-compile-unresolved-functions))
(cond
((eq (car-safe raw) 'closure)
(let ((code (erefactor-check--closure-pseudo-source raw)))
(with-temp-buffer
(insert code)
(let ((lexical-binding t))
(byte-compile-from-buffer (current-buffer))))))
(t
(byte-compile function)))
(byte-compile-warn-about-unresolved-functions)))
(erefactor-check--gather-warnings end))
;; restore function body
(fset function raw))))
(defun erefactor-check--gather-warnings (end)
(let ((buf (get-buffer "*Compile-Log*"))
res)
(when buf
(with-current-buffer buf
(save-excursion
(goto-char end)
(while (re-search-forward "\\(?:Warning\\|Error\\): \\(.*\\)\\(?:\n +\\(.*\\)\\)?" nil t)
(let ((line (concat (match-string 1) (match-string 2))))
(setq res (cons line res))))))
(nreverse res))))
;;;###autoload
(define-minor-mode erefactor-check-eval-mode
"Display compiling warnings when \\[eval-last-sexp], \\[eval-defun]
"
t nil nil
(dolist (x erefactor-check--eval-alist)
(let ((enabler
(if erefactor-check-eval-mode
'ad-enable-advice
'ad-disable-advice)))
(funcall enabler (nth 0 x) (nth 1 x) (nth 2 x))
(funcall 'ad-activate (nth 0 x)))))
(defadvice eval-last-sexp
(after erefactor-check-eval-last-sexp (edebug-it) activate)
(when (erefactor-interactive-p)
;; call `preceding-sexp' same as `eval-last-sexp'
(erefactor-check--form (preceding-sexp))))
(defadvice eval-defun
(after erefactor-check-eval-defun (edebug-it) activate)
(when (erefactor-interactive-p)
(unless edebug-it
;; get FORM same as `eval-defun'
(let ((form (save-excursion
(end-of-defun)
(beginning-of-defun)
(read (current-buffer)))))
(erefactor-check--form form)))))
;;;
;;; Asynchronous Elint
;;;
(defcustom erefactor-lint-emacsen nil
"*Emacs executables.
Examples:
\(setq erefactor-lint-emacsen
'\(\"emacs-21\" \"emacs-22.1\" \"emacs-23.2\" \"emacs-current\"))
"
:group 'erefactor
:type '(list file))
(defcustom erefactor-lint-path-alist nil
"*Associate list key is file name of Elisp.
value is `load-path' that required by key file if key file require some module.
Examples:
\(setq erefactor-lint-path-alist
'\((\"/home/bob/.emacs.d/linting-file.el\"
\"/home/bob/.emacs.d/misc\"))
\"/home/bob/.emacs.d/misc\" directory have some requiring module(s).
"
:group 'erefactor
:type '(list (list file)))
(defun erefactor-lint--running-p ()
(let ((buffer (erefactor-lint--get-buffer)))
(get-buffer-process buffer)))
(defun erefactor-lint--async (file commands)
(let ((command (car commands))
(rest (cdr commands)))
(let ((proc (erefactor-lint--internal command file)))
(set-process-sentinel
proc
`(lambda (p e)
(when (eq (process-status p) 'exit)
(with-current-buffer (process-buffer p)
(erefactor-lint--append "\n\n")
(erefactor-lint--exit-mode-line p))
(when ',rest
(erefactor-lint--async ,file ',rest))))))))
(defun erefactor-lint--exit-mode-line (process)
(with-current-buffer (process-buffer process)
(let* ((code (process-exit-status process))
(msg (format " (Exit [%d])" code)))
(setq mode-line-process
(propertize msg 'face
(if (= code 0)
'compilation-info 'compilation-error))))))
(defun erefactor-lint--internal (command file)
(let* ((args (erefactor-lint--command-args command file))
(buffer (erefactor-lint--get-buffer)))
(display-buffer buffer)
(with-current-buffer buffer
(erefactor-lint--append (format "----- Linting by %s -----\n" command))
(let ((proc (apply 'start-process "Async Elint" (current-buffer)
command args)))
(set-process-sentinel proc (lambda (p e)))
(setq mode-line-process
(propertize " (Running)" 'face 'compilation-warning))
proc))))