-
Notifications
You must be signed in to change notification settings - Fork 1
/
jda.el
1852 lines (1669 loc) · 69.9 KB
/
jda.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
;;; jda.el --- Jong-Gyu Development Assistant minor mode for Developers
;; Copyright (C) 2011 Lee Jong-Gyu<[email protected]>
;; Authors: Lee Jong-Gyu<[email protected]>
;; Maintainer: Lee Jong-Gyu<[email protected]>
;; Created: 18 Mar 2011
;; Version: 0.1.0
;; Keywords: languages, tools
;; Repository: git://github.com/jglee1027/jda-minor-mode.git
;; This file is NOT part of GNU Emacs.
;; This file 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 of the License, or
;; (at your option) any later version.
;; It 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 it. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; JDA(Jong Gyu Development Assistant) is a minor mode for developers
;; who use various programming languages. The major aim of JDA is to
;; offer pleasure for developers. No doubt you will always enjoy
;; writing a source code in any programming language if you use it.
;; * Installation
;; Edit your ~/.emacs file to add the line:
;; (add-to-list 'load-path "/path/to/jda-minor-mode")
;; (require 'jda)
;; (jda-minor-mode)
;; * Major commands:
;; ** Build
;; If there is Makefile or .xcodeproj in your project , you can build
;; a project easily by pressing "C-c c" in any subdirectory of your
;; project.
;; ** Find development doucmentation
;; Press "C-c h" at the symbol which you want to find in a development
;; documentation(Android or Xcode).
;; ** Open a counterpart file
;; You can quickly open the counterpart of the current buffer by
;; pressing "C-c j p". In other words, if the current buffer is c or
;; cpp source file, you can easily visit the header file related it by
;; pressing "C-c j p".
;; ** Find a symbol in the project
;; You can simply search positions which a symbol at point is used in
;; your project by pressing "C-c j s". It uses (grep-find).
;; ** Find a file in the project
;; You can rapidly find a file in your project by pressing "C-c j f".
;; ** Visit a file in the project
;; You can easily open the file you want to visit in your project by
;; pressing "C-c j i" or "C-c i". It supports incremental search like
;; TextMate or Visual Assist. As you type text, one or more possible
;; matches for the text are found and immediately displayed.
;; ** Find a symbol in the project
;; If you want to find a symbol in your project, press "C-c j s".
;; After finding it, you can easily navigate the symbols by pressing
;; "M-g p" or "M-g n".
;; ** Go to a symbol in the current buffer
;; Press "C-c j m" or "C-c m" if you want to go to a function. You can
;; see all functions defined in the current buffer. It supports
;; incremental search like TextMate or Visual Assist.
;; ** Replace a string in several files
;; If you want to replace a string in several files, press "C-c j 5"
;; or "C-c j %". You can easily replace a string in specified files in
;; your project.
;; ** Create a TAG in your project
;; Press "C-c j t" and you can easily create TAG file in your project
;; using 'find' and 'etags'.
;; ** Go to previous or next marker
;; Press "C-c ," to go to the previous marker and Press "C-c ." to go
;; to the next marker.
;; Press "C-x <down>" to save the current marker.
;; ** Highlight symbol
;; If you want to see highlighted symbol at point, press "C-c j h".
;; After specified idle time, the current symbol at point is
;; highlighted. It only works in file buffer.
;; ** Insert or Delete a bracket in Objective-C mode
;; Press "C-c ]" to insert a right bracket to pair.
;; Press "C-c [" to delete left and right brackets to pair.
;; See `jda-minor-keymap' for more details.
;;; Code:
(require 'cl)
(require 'imenu)
(require 'ido)
(require 'hi-lock)
(require 'thingatpt)
(require 'dired-aux)
(defgroup jda nil
"Jong-Gyu Development Assistant"
:group 'applications
:prefix "jda-")
(defvar jda-create-tags-command nil)
(defvar jda-create-tags-command-history nil)
(defvar jda-create-tags-directory nil)
(defvar jda-create-tags-directory-history nil)
(defvar jda-etags-tag-info-alist nil)
(defvar jda-get-extensions-alist '(("c" . ("h"))
("cc" . ("h"))
("cpp" . ("h"))
("cxx" . ("h"))
("m" . ("h"))
("mm" . ("h"))
("h" . ("c" "cc" "cpp" "cxx" "m" "mm"))))
(defvar jda-gf-symbol-history nil)
(defvar jda-gf-symbol-command-history nil)
(defvar jda-gf-find-file-history nil)
(defvar jda-gf-find-file-command-history nil)
(defvar jda-gf-replace-file-history nil)
(defvar jda-gf-replace-file-command-history nil)
(defvar jda-gf-project-root nil)
(defvar jda-gf-project-root-history nil)
(defvar jda-gf-grep-query-command-history nil)
(defvar jda-gf-exclusive-path-history nil)
(defvar jda-gf-grep-query-replace-buffers-alist nil)
(defvar jda-git-grep-dir-history nil)
(defvar jda-git-grep-regexp-history nil)
(defvar jda-git-grep-command-history nil)
(defvar jda-ido-find-file-files-alist nil)
(defvar jda-ido-find-file-files-alist-root nil)
(defvar jda-marker-ring-max 26)
(defvar jda-marker-ring (make-ring jda-marker-ring-max))
(defvar jda-marker-ring-iterator -1)
(defvar jda-marker-bookmark-max 26)
(defvar jda-marker-bookmark (make-vector jda-marker-bookmark-max nil))
(defvar jda-make-command-history nil)
(defvar jda-xcode-doc-text-history nil)
(defvar jda-xcode-sdk-history nil)
(defvar jda-xcode-project-history nil)
(defvar jda-xcode-configuration-history nil)
(defvar jda-xcode-target-history nil)
(defvar jda-xcode-scheme-history nil)
(defvar jda-xcode-build-history nil)
(defvar jda-highlight-symbol-color 'hi-blue)
(defvar jda-highlight-symbol-timer-interval 0.7)
(defvar jda-highlight-symbol-timer nil)
(defvar jda-highlight-symbol-buffers-alist nil)
(defvar jda-highlight-symbol-regex nil)
(make-variable-buffer-local 'jda-highlight-symbol-regex)
(defvar jda-kill-ring-save-is-set t)
(defcustom jda-gf-assoc-extension-alist
'(("c" . "*.[cChH] *.[cC][pP][pP] *.[mM] *.[mM][mM]")
("cpp" . "*.[cChH] *.[cC][pP][pP] *.[mM] *.[mM][mM]")
("h" . "*.[cChH] *.[cC][pP][pP] *.[mM] *.[mM][mM]")
("m" . "*.[cChH] *.[cC][pP][pP] *.[mM] *.[mM][mM]")
("mm" . "*.[cChH] *.[cC][pP][pP] *.[mM] *.[mM][mM]")
("java" . "*.java")
("el" . "*.el")
("lisp" . "*.lisp")
("rb" . "*.rb")
(nil . "*.*"))
"Counterpart extensions"
:type 'alist
:group 'jda)
(defcustom jda-gf-exclusive-path
"*.git* *.svn* *.cvs* *.class *.obj *.o *.a *.so *~ *# *.cache *TAGS *cscope.out"
"Paths to exclude while find command runs"
:type 'string
:group 'jda)
(defcustom jda-android-sdk-dir
"~/sdk/android-sdk-linux_86"
"Android SDK directory"
:type 'directory
:group 'jda)
(defcustom jda-xcode-available-sdks
nil
"Lists all available SDKs in Xcode"
:type 'list
:group 'jda)
(defcustom jda-xcodebuild-command
"xcrun xcodebuild"
"Xcodebuild default command"
:type 'string
:group 'jda)
(defcustom jda-compilation-buffer-name
"*compilation*"
"Compilation buffer name"
:type 'string
:group 'jda)
(defcustom jda-find-command
"find -L"
"find command"
:type 'string
:group 'jda)
(defcustom jda-git-grep-command
"cd %s && GIT_PAGER='' git grep -nH --color -i \"%s\"" ; git-grep-dir regexp
"git-grep command"
:type 'string
:group 'jda)
(defcustom jda-visit-file-super-dir-depth
3
"the value of super-dir-depth parameter of (jda-visit-file-in-dirs)"
:type 'integer
:group 'jda)
(defcustom jda-visit-file-sub-dir-depth
3
"the value of sub-dir-depth parameter of (jda-visit-file-in-dirs)"
:type 'integer
:group 'jda)
;;;; common functions
(defun chomp (str)
"Chomp leading and tailing whitespace from STR."
(while (string-match "\\`\n+\\|^\\s-+\\|\\s-+$\\|\n+\\'" str)
(setq str (replace-match "" t t str)))
str)
(defun jda-advice-completing-read (prompt
collection
&optional
predicate
require-match
initial-input
history)
(cond ((functionp 'helm)
(let* ((helm-source
`((name . ,prompt)
(candidates . ,collection)
(action . (lambda (candidate) candidate)))))
(helm :sources '(helm-source))))
((functionp 'ido-completing-read)
(ido-completing-read prompt collection predicate require-match
initial-input history))
(t
(completing-read prompt collection predicate require-match
initial-input history))))
(defun jda-icompleting-read (prompt choices)
(let ((iswitchb-make-buflist-hook
(lambda ()
(setq iswitchb-temp-buflist choices))))
(iswitchb-read-buffer prompt)))
(defun jda-read-shell-command (prompt initial-contents &optional history)
(if (functionp 'read-shell-command)
(read-shell-command prompt
initial-contents
history)
(read-from-minibuffer prompt
initial-contents
nil
nil
history)))
(defun jda-is-directory (path)
(equal (car (file-attributes path)) t))
(defun jda-get-super-directory (path)
(replace-regexp-in-string "/[^/]*$"
""
path))
(defmacro jda-advice-ffap-read-file-or-url (prompt initial-dir &optional history)
`(directory-file-name
(if (functionp 'helm-advice--ffap-read-file-or-url)
(helm-advice--ffap-read-file-or-url ,prompt ,initial-dir)
(completing-read ,prompt
'ffap-read-file-or-url-internal
nil
nil
,initial-dir
,history))))
(defmacro jda-set-default-directory (prompt dir history)
`(or ,dir (setq ,dir default-directory))
`(setq ,dir (jda-advice-ffap-read-file-or-url ,prompt ,dir ,history)))
;;;; jda-highlight
;; callback function
(defun jda-highlight-symbol-callback ()
(save-excursion
(let* ((symbol (symbol-at-point))
(symbol-regex (cond ((and symbol)
(concat "\\<"
(regexp-quote (symbol-name symbol))
"\\>"))
(t
nil))))
(cond ((and symbol-regex
(not (string= symbol-regex jda-highlight-symbol-regex))
(buffer-file-name))
(hi-lock-unface-buffer jda-highlight-symbol-regex)
(hi-lock-face-buffer symbol-regex
jda-highlight-symbol-color)
(setq jda-highlight-symbol-buffers-alist
(assq-delete-all (current-buffer)
jda-highlight-symbol-buffers-alist))
(add-to-list 'jda-highlight-symbol-buffers-alist
(list (current-buffer) symbol-regex) t)
(setq jda-highlight-symbol-regex symbol-regex))))))
;; start function
(defun jda-highlight-symbol-run-toggle ()
(interactive)
(cond ((timerp jda-highlight-symbol-timer)
(cancel-timer jda-highlight-symbol-timer)
(while jda-highlight-symbol-buffers-alist
(let* ((entry (pop jda-highlight-symbol-buffers-alist))
(buffer (pop entry))
(symbol-regex (pop entry)))
(condition-case nil
(progn
(set-buffer buffer)
(hi-lock-unface-buffer jda-highlight-symbol-regex))
(error nil))))
(message "jda-highlight-symbol off.")
(setq jda-highlight-symbol-timer nil))
(t
(unless hi-lock-mode (hi-lock-mode 1))
(setq jda-highlight-symbol-timer
(run-with-idle-timer jda-highlight-symbol-timer-interval
t
'jda-highlight-symbol-callback))
(message "jda-highlight-symbol on."))))
;;;; jda-marker
(defun jda-marker-line-number-at (marker)
(save-excursion
(with-current-buffer (marker-buffer marker)
(line-number-at-pos marker))))
;; jump-to-register in register.el
(defun jda-marker-jump (marker)
(cond
((and (consp marker) (frame-configuration-p (car marker)))
(set-frame-configuration (car marker) (not delete))
(goto-char (cadr marker)))
((and (consp marker) (window-configuration-p (car marker)))
(set-window-configuration (car marker))
(goto-char (cadr marker)))
((markerp marker)
(or (marker-buffer marker)
(error "That marker's buffer no longer exists"))
(switch-to-buffer (marker-buffer marker))
(goto-char marker))
((and (consp marker) (eq (car marker) 'file))
(find-file (cdr marker)))
((and (consp marker) (eq (car marker) 'file-query))
(or (find-buffer-visiting (nth 1 marker))
(y-or-n-p (format "Visit file %s again? " (nth 1 marker)))
(error "marker access aborted"))
(find-file (nth 1 marker))
(goto-char (nth 2 marker)))
(t
(error "marker doesn't contain a buffer position or configuration"))))
(defun jda-marker-push-marker (&optional use-jda-marker-list)
(interactive)
(setq jda-marker-ring-iterator -1)
(let ((last-marker nil)
(curr-marker (point-marker)))
(condition-case nil
(cond ((ring-empty-p jda-marker-ring)
(ring-insert jda-marker-ring curr-marker)
(if use-jda-marker-list
(jda-marker-list)))
(t
(setq last-marker (ring-ref jda-marker-ring 0))
(cond ((not (equal last-marker curr-marker))
(ring-insert jda-marker-ring curr-marker)
(if use-jda-marker-list
(jda-marker-list))))))
(error nil))))
(defun jda-marker-push-marker-menu ()
(interactive)
(jda-marker-push-marker t))
(defun jda-marker-prev ()
(interactive)
(cond ((equal jda-marker-ring-iterator -1)
(jda-marker-push-marker)
(setq jda-marker-ring-iterator 0)))
(condition-case nil
(let* ((prev-iterator (mod (1+ jda-marker-ring-iterator)
(ring-length jda-marker-ring)))
(prev-marker (ring-ref jda-marker-ring prev-iterator)))
(jda-marker-jump prev-marker)
(setq jda-marker-ring-iterator prev-iterator))
(error nil)))
(defun jda-marker-prev-ui ()
(interactive)
(jda-marker-prev)
(jda-marker-list))
(defun jda-marker-next ()
(interactive)
(cond ((equal jda-marker-ring-iterator -1)
(message "Should run the command 'jda-marker-prev'"))
(t
(condition-case nil
(let* ((next-iterator (mod (1- jda-marker-ring-iterator)
(ring-length jda-marker-ring)))
(next-marker (ring-ref jda-marker-ring next-iterator)))
(jda-marker-jump next-marker)
(setq jda-marker-ring-iterator next-iterator))
(error nil)))))
(defun jda-marker-next-ui ()
(interactive)
(jda-marker-next)
(jda-marker-list))
(defun jda-marker-get-line-string (marker)
(interactive)
(cond ((and (markerp marker)
(marker-buffer marker))
(save-excursion
(with-current-buffer (marker-buffer marker)
(goto-char marker)
(let (begin end str)
(beginning-of-line)
(setq begin (point))
(end-of-line)
(setq end (point))
(setq str (buffer-substring begin end))
(chomp str)
(if (> (length str) 0)
str
marker)))))
(t
marker)))
(defun jda-marker-list-message ()
(let* ((length (ring-length jda-marker-ring))
(message (format "Marker list(push: C-x ?, prev: \"C-x ,\", next: \"C-x .\", last: C-x /, jump: a~%c)\n\n"
(+ ?a (- length 1))))
marker)
(dotimes (i length)
(setq marker (ring-ref jda-marker-ring i))
(setq message (concat message
(format "%s[%c] %s:%d:%s\n"
(if (equal i jda-marker-ring-iterator)
"=>"
" ")
(+ ?a i)
(marker-buffer marker)
(jda-marker-line-number-at marker)
(jda-marker-get-line-string marker)))))
message))
(defun jda-marker-list ()
(interactive)
(let ((length (ring-length jda-marker-ring))
index
(c ?,))
(setq max-mini-window-height (+ length 3))
(while (or (char-equal c ?,)
(char-equal c ?.))
(message (jda-marker-list-message))
(setq c (read-char))
(cond ((char-equal c ?,)
(jda-marker-prev))
((char-equal c ?.)
(jda-marker-next))))
(setq index (- c ?a))
(if (and (>= index 0)
(< index length))
(jda-marker-jump (ring-ref jda-marker-ring index)))
(setq max-mini-window-height 0.25)))
(defun jda-marker-finish-jump ()
(interactive)
(setq jda-marker-ring-iterator -1)
(condition-case nil
(progn
(jda-marker-jump (ring-ref jda-marker-ring 0))
(jda-marker-list))
(error nil)))
(defun jda-marker-bookmark-message (mode)
(let ((message (format "Press key(a~%c) to %s the current marker:\n\n"
(+ ?a (- jda-marker-bookmark-max 1))
mode))
marker)
(dotimes (i jda-marker-bookmark-max)
(setq marker (elt jda-marker-bookmark i))
(setq message (concat message
(if (or (null marker) (null (marker-buffer marker)))
(format " [%c]\n" (+ ?a i))
(format " [%c] %s:%d:%s\n"
(+ ?a i)
(marker-buffer marker)
(jda-marker-line-number-at marker)
(jda-marker-get-line-string marker))))))
message))
(defun jda-marker-bookmark-save ()
(interactive)
(let ((c ?a)
(max-mini-window-height-old max-mini-window-height)
(curr-marker (point-marker))
i)
(setq max-mini-window-height (+ jda-marker-bookmark-max 3))
(setq c (read-char-exclusive (jda-marker-bookmark-message "save")))
(setq i (- c ?a))
(setq max-mini-window-height max-mini-window-height-old)
(cond ((and (>= i 0) (< i jda-marker-bookmark-max))
(aset jda-marker-bookmark i curr-marker)
(message (format "[%c] %s was saved" c curr-marker))))))
(defun jda-marker-bookmark-restore ()
(interactive)
(let ((c ?a)
(max-mini-window-height-old max-mini-window-height)
(curr-marker (point-marker))
i)
(setq max-mini-window-height (+ jda-marker-bookmark-max 3))
(setq c (read-char-exclusive (jda-marker-bookmark-message "restore")))
(setq i (- c ?a))
(setq max-mini-window-height max-mini-window-height-old)
(cond ((and (>= i 0) (< i jda-marker-bookmark-max))
(jda-marker-jump (elt jda-marker-bookmark i))))))
;;;; utility functions
(defun jda-file-name (filename)
(if (memq system-type '(ms-dos windows-nt cygwin))
(replace-regexp-in-string "^/\\([a-zA-Z]\\)/" "\\1:/" filename)
filename))
(defun jda-get-sub-directory-list (dir)
(let ((entries (directory-files dir t))
(sub-dirs '()))
(mapcar (lambda (entry) (if (jda-is-directory entry)
(add-to-list 'sub-dirs entry t)))
entries)
sub-dirs))
(defun jda-current-file-name-extension ()
(let (extension)
(cond ((null (buffer-file-name))
nil)
((null (setq extension (file-name-extension (buffer-file-name))))
nil)
(t
(setq extension (downcase extension))))))
(defun jda-get-extensions-to-visit ()
(let ((extension (jda-current-file-name-extension)))
(cond ((null extension)
nil)
(t
(cdr (assoc extension jda-get-extensions-alist))))))
(defun jda-visit-file (file-name-sans-ext extensions)
(let (file-name file-ext)
(while (not (equal (setq file-ext (pop extensions)) nil))
(setq file-name (concat file-name-sans-ext "." file-ext))
(if (file-exists-p file-name)
(progn
(find-file file-name)
(throw 'visit-file-exception file-name))))))
(defun jda-visit-file-in-sub-dirs (sub-dir-list file-name-non-dir extensions)
(let (file-name-sans-ext)
(mapcar (lambda (entry)
(setq file-name-sans-ext (concat entry "/" file-name-non-dir))
(jda-visit-file file-name-sans-ext extensions))
sub-dir-list)))
(defun jda-visit-file-in-dirs (super-dir-depth sub-dir-depth)
(let ((extensions-to-visit (jda-get-extensions-to-visit))
file-name-sans-ext
file-name-non-dir
current-dir
sub-dir-list)
(if (or (equal extensions-to-visit nil)
(equal (buffer-file-name) nil))
(throw 'visit-file-exception "Not supported!"))
(setq file-name-sans-ext (file-name-sans-extension (buffer-file-name)))
(setq file-name-non-dir (file-name-nondirectory file-name-sans-ext))
;; in current directory
;; in super directory
;; in sub-dir of super-dir
;; ...
(setq current-dir (file-name-directory file-name-sans-ext))
(setq sub-dir-list (jda-get-sub-directory-list current-dir))
(dotimes (i super-dir-depth)
(setq current-dir (jda-get-super-directory current-dir))
(if (equal current-dir "")
(return))
(setq sub-dir-list (jda-get-sub-directory-list current-dir))
(jda-visit-file-in-sub-dirs sub-dir-list
file-name-non-dir
extensions-to-visit))
;; in sub-dir of sub-dir
;; ...
(setq current-dir (file-name-directory file-name-sans-ext))
(setq sub-dir-list (jda-get-sub-directory-list current-dir))
(let ((sub-dir-all-list nil))
(dotimes (i sub-dir-depth)
(mapcar (lambda (entry)
(let ((dirs (jda-get-sub-directory-list entry)))
(mapcar (lambda (x)
(delq x sub-dir-all-list)
(add-to-list 'sub-dir-all-list x t))
dirs))
entry)
sub-dir-list)
(setq sub-dir-list sub-dir-all-list))
(jda-visit-file-in-sub-dirs sub-dir-all-list
file-name-non-dir
extensions-to-visit))))
(defun jda-visit-file-in-project ()
(interactive)
(let ((file-name-sans-ext (file-name-sans-extension (buffer-name)))
(same-name-files-list nil)
(extensions-to-visit (jda-get-extensions-to-visit))
(same-name-files-count 0)
file-name
file-ext)
(if (null jda-ido-find-file-files-alist)
(throw 'visit-file-exception "Not found! Set project root directory"))
(while (not (null (setq file-ext (pop extensions-to-visit))))
(setq file-name (concat file-name-sans-ext "." file-ext))
(cond ((not (null (assoc file-name jda-ido-find-file-files-alist)))
(mapcar (lambda (x)
(cond ((equal file-name (car x))
(add-to-list 'same-name-files-list
(car (cdr x)))
(setq same-name-files-count
(1+ same-name-files-count)))))
jda-ido-find-file-files-alist)
(cond ((equal same-name-files-count 1)
(throw 'visit-file-exception
(buffer-file-name
(find-file (car same-name-files-list)))))
((> same-name-files-count 1)
(throw 'visit-file-exception
(buffer-file-name
(find-file
(jda-advice-completing-read "Find file: "
same-name-files-list)))))))))))
(defun jda-open-counterpart-file ()
"open the header or source file related with the current file."
(interactive)
(message (catch 'visit-file-exception
(jda-visit-file-in-dirs jda-visit-file-super-dir-depth
jda-visit-file-sub-dir-depth)
(jda-visit-file-in-project)
(throw 'visit-file-exception "Not found!"))))
(defun jda-kill-ring-save-toggle ()
(interactive)
(cond (jda-kill-ring-save-is-set
(message "jda-kill-ring-save off")
(setq jda-kill-ring-save-is-set nil))
(t
(message "jda-kill-ring-save on")
(setq jda-kill-ring-save-is-set t))))
(defun jda-kill-ring-save (begin end)
(interactive "r")
(cond ((null (functionp 'use-region-p))
(kill-ring-save begin end))
((and jda-kill-ring-save-is-set
(not (use-region-p)))
(let ((bounds (bounds-of-thing-at-point 'symbol)))
(if bounds
(copy-region-as-kill (car bounds) (cdr bounds))
(kill-ring-save begin end))))
(t
(kill-ring-save begin end))))
(defun jda-delete-trailing-whitespace (begin end)
(interactive "r")
(if mark-active
(replace-regexp "^[[:space:]]+$\\|[[:space:]]+$" "" nil begin end)
(query-replace-regexp "^[[:space:]]+$\\|[[:space:]]+$" "")))
(defun jda-gf-get-find-exclusive-path-options ()
(let (path-list path-option)
(if (equal jda-gf-exclusive-path "")
(setq path-option "")
(progn
(setq path-list (mapcar (lambda (x) (format "-path '%s'" x))
(split-string jda-gf-exclusive-path)))
(setq path-option (pop path-list))
(while path-list
(setq path-option (concat path-option " -o " (pop path-list))))
(setq path-option (concat "! \\( " path-option " \\)"))))))
(defun jda-gf-get-find-name-options (files)
(let (name-list name-option)
(if (equal files "")
(setq name-option "")
(progn
(setq name-list (mapcar (lambda (x) (format "-name '%s'" x))
(split-string files)))
(setq name-option (pop name-list))
(while name-list
(setq name-option (concat name-option " -o " (pop name-list))))
(setq name-option (concat "\\( " name-option " \\)"))))))
(defun jda-gf-get-assoc-find-name-options ()
(let (extension assoc-extensions)
(setq extension (jda-current-file-name-extension))
(setq assoc-extensions (cdr (assoc extension jda-gf-assoc-extension-alist)))
(setq assoc-extensions (read-from-minibuffer "Files: " assoc-extensions))
(jda-gf-get-find-name-options assoc-extensions)))
(defun jda-gf-set-exclusive-path ()
(interactive)
(setq jda-gf-exclusive-path
(read-from-minibuffer "Exclusive paths: "
jda-gf-exclusive-path
nil
nil
'jda-gf-exclusive-path-history)))
(defun jda-gf-set-project-root ()
"set a project root directory for grep-find"
(interactive)
(jda-set-default-directory "Project root: "
jda-gf-project-root
'jda-gf-project-root-history))
(defun jda-gf-set-project-root-with-default-directory ()
"set a project root directory for grep-find"
(interactive)
(let ((project-root nil))
(setq jda-gf-project-root
(jda-set-default-directory "Project root: "
project-root
'jda-gf-project-root-history))))
(defun jda-pop-to-compilation-buffer ()
(interactive)
(pop-to-buffer jda-compilation-buffer-name))
(defun jda-pop-to-compilation-buffer-fill ()
(interactive)
(pop-to-buffer jda-compilation-buffer-name)
(delete-other-windows))
(defun jda-gf-select-grep-buffer (current-buffer msg)
(condition-case nil
(progn
(select-window (get-buffer-window current-buffer))
(forward-line 4)
(setq compilation-finish-function nil))
(error nil)))
(defun jda-gf-symbol-at-point ()
"grep-find with symbol at current point."
(interactive)
(jda-marker-push-marker)
(let (symbol)
(jda-gf-set-project-root)
(setq symbol (symbol-at-point))
(if (null symbol)
(setq symbol ""))
(setq symbol (read-from-minibuffer "Find symbol: "
(format "%s" symbol)
nil
nil
'jda-gf-symbol-history))
(setq compilation-finish-function 'jda-gf-select-grep-buffer)
(grep-find (jda-read-shell-command "Command: "
(format "%s %s -type f %s %s -print0 | xargs -0 grep -nH -e '\\<%s\\>'"
jda-find-command
jda-gf-project-root
(jda-gf-get-assoc-find-name-options)
(jda-gf-get-find-exclusive-path-options)
symbol)
'jda-gf-symbol-command-history))))
(defun jda-gf-text-at-point ()
"grep-find with text at current point."
(interactive)
(jda-marker-push-marker)
(let (symbol)
(jda-gf-set-project-root)
(setq symbol (symbol-at-point))
(if (null symbol)
(setq symbol ""))
(setq symbol (read-from-minibuffer "Find text: "
(format "%s" symbol)
nil
nil
'jda-gf-symbol-history))
(setq compilation-finish-function 'jda-gf-select-grep-buffer)
(grep-find (jda-read-shell-command "Command: "
(format "%s %s -type f %s %s -print0 | xargs -0 grep -nH -e '%s'"
jda-find-command
jda-gf-project-root
(jda-gf-get-assoc-find-name-options)
(jda-gf-get-find-exclusive-path-options)
symbol)
'jda-gf-symbol-command-history))))
(defun jda-git-grep ()
"git-grep with regexp in git repository"
(interactive)
(jda-marker-push-marker)
(let (regexp git-grep-dir)
(setq git-grep-dir (chomp
(shell-command-to-string "git rev-parse --show-toplevel")))
(jda-set-default-directory "Git grep dir: "
git-grep-dir
'git-grep-dir-history)
(setq regexp (symbol-at-point))
(if (null regexp)
(setq regexp ""))
(cond ((functionp 'helm-grep-git-1)
(helm-grep-git-1 git-grep-dir t (format "%s" regexp)))
(t
(setq regexp (read-from-minibuffer "Search in git repo: "
(format "%s" regexp)
nil
nil
'jda-git-grep-regexp-history))
(setq compilation-finish-function 'jda-gf-select-grep-buffer)
(grep-find (jda-read-shell-command "$ "
(format jda-git-grep-command
git-grep-dir
regexp)
'jda-git-grep-command-history))))))
(defun jda-gf-grep-query-replace-in-current-line (from to buffer)
(let (begin end)
(with-current-buffer buffer
(save-excursion
(beginning-of-line)
(setq begin (point))
(end-of-line)
(setq end (point)))
(replace-regexp from to nil begin end))))
(defun jda-gf-grep-query-replace-ui (from to &optional delimited)
(interactive
(let ((common
(query-replace-read-args
"Query replace regexp in found files" t t)))
(list (nth 0 common) (nth 1 common) (nth 2 common))))
(condition-case nil
(next-error)
(error "Query replace finished"))
(condition-case nil
(previous-error)
(error nil))
(let ((done nil)
(all nil)
(count 0)
key
buffer)
(message "")
(while (not done)
(with-current-buffer "*grep*"
(setq buffer (get-buffer
(file-name-nondirectory
(car
(car (nth 2 (car (compilation-next-error 0)))))))))
(with-current-buffer buffer
(setq jda-gf-grep-query-replace-buffers-alist
(assq-delete-all buffer
jda-gf-grep-query-replace-buffers-alist))
(add-to-list 'jda-gf-grep-query-replace-buffers-alist
(list buffer from) t)
(hi-lock-face-buffer from 'query-replace))
(message (format "Query replacing '%s' with '%s' (y/n/a/q)?" from to))
(setq key (read-event))
(cond ((equal key ?y)
(setq count (+ 1 count))
(jda-gf-grep-query-replace-in-current-line from to buffer)
(condition-case nil
(next-error)
(error
(setq done t))))
((equal key ?n)
(condition-case nil
(next-error)
(error
(setq done t))))
((equal key ?a)
(setq all t)
(setq done t))
((equal key ?q)
(setq done t))))
(setq done nil)
(cond (all
(while (not done)
(jda-gf-grep-query-replace-in-current-line from to buffer)
(setq count (+ 1 count))
(condition-case nil
(next-error)
(error
(setq done t))))))
(while jda-gf-grep-query-replace-buffers-alist
(let* ((entry (pop jda-gf-grep-query-replace-buffers-alist))
(buffer (pop entry))
(symbol-regex (pop entry)))
(condition-case nil
(progn
(set-buffer buffer)
(hi-lock-unface-buffer symbol-regex))
(error nil))))
(message "Replaced %d occurrences" count)))
(defun jda-gf-grep-query-replace-old (from to &optional delimited)
(interactive
(let ((common
(query-replace-read-args
"Query replace regexp in files" t t)))
(list (nth 0 common) (nth 1 common) (nth 2 common))))
(jda-marker-push-marker)
(jda-gf-set-project-root)
(let (name-option
command
extension)
(cond ((null (buffer-file-name))
(setq name-option ""))
(t
(setq extension (jda-current-file-name-extension))
(setq name-option (cdr (assoc
extension
jda-gf-assoc-extension-alist)))))
(setq name-option (jda-gf-get-find-name-options
(read-from-minibuffer "Find file: "
name-option)))
(setq command (jda-read-shell-command
"Command: "
(format "%s %s -type f %s %s -print0 | xargs -0 grep -nH -e '%s'"
jda-find-command
jda-gf-project-root
name-option
(jda-gf-get-find-exclusive-path-options)
from)
'jda-gf-grep-query-command-history))
(shell-command command "*grep*")
(with-current-buffer "*grep*"
(grep-mode)
(hi-lock-face-buffer from
'match))
(jda-gf-grep-query-replace-ui from to)))