-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprojectIDE.el
2028 lines (1519 loc) · 80 KB
/
projectIDE.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
;;; projectIDE.el --- project management package
;;
;; Copyright (C) 2015 Mola-T
;; Author: Mola-T <[email protected]>
;; URL: https://github.com/mola-T/projectIDE
;; Version: 1.0
;; Package-Requires: ((cl-lib.el "0.5") (fdex.el "1.0"))
;; Keywords: project, convenience
;;
;;; License:
;; This file is NOT part of GNU Emacs.
;;
;; 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:
;;
;; A project management package.
;; See https://github.com/mola-T/projectIDE for all information
;;
;;; code:
(require 'cl-lib)
(require 'fdex)
(require 'projectIDE-header)
(require 'projectIDE-debug)
(require 'projectIDE-fstream)
(require 'projectIDE-module)
(require 'projectIDE-modeline)
(require 'projectIDE-session)
(require 'projectIDE-addfile)
(require 'projectIDE-compile)
(require 'projectIDE-scriptloader)
(require 'projectIDE-cleanup)
;;; Config file function ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;;fff (defun projectIDE-parse-config (file &optional errormessage caller))
;;fff (defun projectIDE-verify-config (&optional prefix)) -I
(defun projectIDE-parse-config (config &optional errormessage caller)
"Parse .projectIDE config FILE.
Return a projectIDE-project object created by the CONFIG.
CONFIG is either a \".projectIDE\" config file
or a \".projectIDE\" buffer.
If .projectIDE is a blank file, return a default projectIDE-project object.
If there is any problem parsing the .projectIDE file, return nil.
Return
Type:\t\t projectIDE-project object or nil
Descrip.:\t Project object created by parsing FILE.
\t\t\t: nil for any error.
CONFIG
Type:\t\t string or buffer
Descrip.:\t Flie path to .projectIDE.
\t\t\t Or emacs buffer to .projectIDE.
ERRORMESSAGE
Type:\t\t bool
Descrip.:\t Display error message to minibuffer if it is t.
CALLER
Type:\t\t symbol list
Descrip.:\t Function list calling this function for debug purpose."
(catch 'parse-error
(let ((project (make-projectIDE-project))
(filename (or (and (bufferp config) (buffer-file-name config)) config))
scope)
(with-temp-buffer
(if (bufferp config)
(insert-buffer-substring config)
(insert-file-contents config))
(goto-char 1)
;; Search keys
(while (search-forward-regexp projectIDE-config-key-string nil t)
(save-excursion
;; Identify key
(beginning-of-line)
(let ((line-end (line-end-position))
(keylist projectIDE-config-key)
(counter 0)
found)
(while (and (not found) (car keylist))
(if (search-forward-regexp (car keylist) line-end t)
(progn
(cond
((= counter 0) ;; "^signature *="
(when (projectIDE-project-signature project)
(projectIDE-message 'Error
(format "Config file corrupt. 'signature' in \"%s\" definded more than once." filename)
errormessage
(projectIDE-caller 'projectIDE-parse-config caller))
(throw 'parse-error nil))
(setf (projectIDE-project-signature project) (projectIDE-trim-string (buffer-substring-no-properties (point) line-end))))
((= counter 1) ;; "^name *="
(when (projectIDE-project-name project)
(projectIDE-message 'Error
(format "Config file corrupt. 'name' in \"%s\" definded more than once." filename)
errormessage
(projectIDE-caller 'projectIDE-parse-config caller))
(throw 'parse-error nil))
(setf (projectIDE-project-name project) (projectIDE-trim-string (buffer-substring-no-properties (point) line-end))))
((= counter 2) ;; "^exclude *="
(let ((exclude-list
(condition-case err
(split-string-and-unquote (buffer-substring-no-properties (point) line-end))
(end-of-file
(projectIDE-message 'Error
(format "Config file corrupt. Unbalance quote on line --%s-- of \"%s\"."
(line-number-at-pos) filename)
errormessage
(projectIDE-caller 'projectIDE-parse-config caller))
(throw 'parse-error nil)))))
(setf (projectIDE-project-exclude project)
(projectIDE-append (projectIDE-project-exclude project) exclude-list))))
((= counter 3) ;; "^whitelist *="
(let ((whitelist
(condition-case err
(split-string-and-unquote (buffer-substring-no-properties (point) line-end))
(end-of-file
(projectIDE-message 'Error
(format "Config file corrupt. Unbalance quote on line --%s-- of \"%s\"."
(line-number-at-pos) filename)
errormessage
(projectIDE-caller 'projectIDE-parse-config caller))
(throw 'parse-error nil)))))
(setf (projectIDE-project-whitelist project)
(projectIDE-append (projectIDE-project-whitelist project) whitelist))))
((= counter 4) ;; "^cachemode *="
(when (projectIDE-project-cachemode project)
(projectIDE-message 'Error
(format "Config file corrupt. 'cachemode' in \"%s\" definded more than once." filename)
errormessage
(projectIDE-caller 'projectIDE-parse-config caller))
(throw 'parse-error nil))
(setf (projectIDE-project-cachemode project)
(string-to-number (projectIDE-trim-string (buffer-substring-no-properties (point) line-end)))))
((= counter 5) ;; "^module *="
(let ((modules (projectIDE-project-module project))
(modules-s (split-string (buffer-substring-no-properties (point) line-end))))
(dolist (module modules-s)
(setq modules (projectIDE-add-to-list modules (intern module))))
(setf (projectIDE-project-module project) modules)))
((= counter 6) ;; "^scope *="
(setq scope (projectIDE-trim-string (buffer-substring-no-properties (point) line-end))))
((= counter 7) ;; "^[[:digit:][:alpha:]]+ *="
(let ((module-var (projectIDE-project-module-var project))
(var (condition-case err
(split-string-and-unquote (buffer-substring-no-properties (point) line-end))
(end-of-file
(projectIDE-message 'Error
(format "Config file corrupt. Unbalance quote on line --%s-- of \"%s\"."
(line-number-at-pos) filename)
errormessage
(projectIDE-caller 'projectIDE-parse-config caller))
(throw 'parse-error nil))))
(name (concat scope (and scope "-")
(projectIDE-trim-string
(string-remove-suffix "="
(buffer-substring-no-properties (line-beginning-position) (point)))))))
(setq module-var (plist-put module-var (intern name) var))
(setf (projectIDE-project-module-var project) module-var))))
(setq found t))
(setq counter (1+ counter))
(setq keylist (cdr keylist))))))))
(unless (projectIDE-project-exclude project)
(setf (projectIDE-project-exclude project) projectIDE-default-exclude))
(unless (projectIDE-project-whitelist project)
(setf (projectIDE-project-whitelist project) projectIDE-default-whitelist))
(unless (projectIDE-project-cachemode project)
(setf (projectIDE-project-cachemode project) projectIDE-default-cachemode))
(when projectIDE-debug-mode
(projectIDE-message 'Info
(format "Parsed config file \"%s\" successfully" filename)
nil
(projectIDE-caller 'projectIDE-parse-config caller)))
project)))
(defun projectIDE-verify-config (&optional prefix)
"An interactive function to verify a \".projectIDE\" config.
If PREFIX is not provided, it will try to verify the current buffer.
If PREFIX is provided or current buffer is not a config file,
it will prompt user for a config file.
This function can only check syntax error!
Return
Type:\t\t bool
Descrip.:\t Returns t if the config file can be parsed successfully.
\t\t\t Otherwise, returns nil.
PREFIX
Type:\t\t any
Descrip:\t Prompt for a config file if it is provided."
(interactive "p")
(let (config)
;; Try to read current buffer first
(when (and (or (not prefix) (= prefix 1))
(buffer-file-name)
(equal (file-name-nondirectory (buffer-file-name)) PROJECTIDE-PROJECTROOT-IDENTIFIER))
(setq config (current-buffer)))
;; Try to prompt for a config file
(when (and prefix (not (= prefix 1)))
(setq config (expand-file-name
(projectIDE-read-file-name "Choose config: " nil nil t nil
(lambda (file)
(or
(file-directory-p file)
(equal (file-name-nondirectory file) PROJECTIDE-PROJECTROOT-IDENTIFIER))))))
(when (file-directory-p config)
(setq config nil)))
(when config
(if (projectIDE-parse-config config)
(progn
(projectIDE-message 'Info
"Project config parse successfully. File saved."
t
(projectIDE-caller 'projectIDE-verify-config))
t)
;; Parsing error
(message "%s\nFile saved." projectIDE-last-message)
nil))))
;;; Config file function ends ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;; Indexing function ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;;fff (defun projectIDE-generate-signature (&optional caller))
;;fff (defun projectIDE-new-project (path &optional caller))
;;fff (defun projectIDE-root-create (path &optional caller))
;;fff (defun projectIDE-record-create (configfile &optional caller))
;;fff (defun projectIDE-cache-create (configfile &optional caller))
;;fff (defun projectIDE-identify-project (&optional buffer caller))
;;fff (defun projectIDE-index-project (path &optional caller))
(defun projectIDE-generate-signature (&optional caller)
"Generate and return a projectIDE signature.
CALLER is the function list calling this function.
It is uesed for debugging purpose.
Return
Type:\t\t string
Descrip.:\t A signature string.
CALLER
Type:\t\t symbol list
Descrip.:\t Function list calling this function for debug purpose."
(secure-hash 'sha1
(concat (current-time-string) (number-to-string (random)))))
(defun projectIDE-new-project (path &optional caller)
;; Ensure PATH is a directory before passing to this function.
;; ie. use file-name-as-directory
"Create project with PATH as project root.
It is a encapsulation of project creation chain.
CALLER is the function list calling this function.
It is uesed for debugging purpose.
PATH
Type:\t\t string
Descrip.:\t Path to project root.
CALLER
Type:\t\t symbol list
Descrip.:\t Function list calling this function for debug purpose."
(let ((project-config (concat path PROJECTIDE-PROJECTROOT-IDENTIFIER)))
;; Generate .projectIDE file
(projectIDE-root-create path (projectIDE-caller 'projectIDE-new-project caller))
;; Create global record
(projectIDE-record-create project-config (projectIDE-caller 'projectIDE-new-project caller))
;; Create individual record
(projectIDE-cache-create project-config (projectIDE-caller 'projectIDE-new-project caller))))
(defun projectIDE-root-create (path &optional caller)
"Create '.projectIDE' at PATH to indicate a project root.
Create basic key like signature, name, exclude and whitelist as well.
If '.projectIDE' has already existed, try to read the '.projectIDE config
and give it a new signature.
CALLER is the function list calling this function.
It is uesed for debugging purpose.
PATH
Type:\t\t string
Descrip.:\t Path to project root.
CALLER
Type:\t\t symbol list
Descrip.:\t Function list calling this function for debug purpose."
;; Generate .projectIDE if not exist
(unless (memq PROJECTIDE-PROJECTROOT-IDENTIFIER (directory-files path))
(write-region "" nil (concat path PROJECTIDE-PROJECTROOT-IDENTIFIER) t 'inhibit))
(let* ((file (concat path PROJECTIDE-PROJECTROOT-IDENTIFIER))
(project (projectIDE-parse-config file nil (projectIDE-caller 'projectIDE-root-creator caller)))
(signature (projectIDE-generate-signature (projectIDE-caller 'projectIDE-root-creator caller)))
(name (projectIDE-project-name project))
(exclude (projectIDE-project-exclude project))
(whitelist (projectIDE-project-whitelist project)))
(with-temp-file file
;; If signature exists in .projectIDE, remove it
(when (projectIDE-project-signature project)
(while (search-forward-regexp "^signature *=" nil t)
(delete-region (line-beginning-position) (line-end-position))))
(goto-char 1)
;; Write to .projectIDE
;; Handle signature
(insert "## This file is generated by projectIDE\n"
"## There are several keys availiable.\n"
"## You can see documentation for all keys.\n"
"## Keys must start on a newline and end with a '='.\n"
"## Below is an example for key 'signature.\n"
"signature = " signature "\n"
"## Signature is unique for each project.\n"
"## ProjectIDE used the signature to trace every data on that project.\n"
"## So never create or change the signature manually!!!\n\n")
;; Handle name
;; If name not exists in projectIDE, create for it
(unless name
(setq name (file-name-nondirectory (directory-file-name (file-name-directory file))))
(insert "name = " name "\n"))
;; Handle exclude
(insert "exclude = " (mapconcat 'identity (projectIDE-project-exclude project) " ") "\n")
(save-excursion
(while (search-forward-regexp "^exclude *=" nil t)
(delete-region (line-beginning-position) (line-end-position))))
;; Handle whitelist
(insert "whitelist =" (mapconcat 'identity (projectIDE-project-whitelist project) " ") "\n")
(save-excursion
(while (search-forward-regexp "^whitelist *=" nil t)
(delete-region (line-beginning-position) (line-end-position)))))
(when projectIDE-debug-mode
(projectIDE-message 'Info
(format "Project root created at \"%s\"" path)
nil
(projectIDE-caller 'projectIDE-root-creator caller)))))
(defun projectIDE-record-create (configfile &optional caller)
"Create projectIDE-record by reading CONFIGFILE.
Write to RECORD file afterward.
CALLER is the function list calling this function.
It is uesed for debugging purpose.
CONFIGFILE
Type:\t\t string
Descrip.:\t A string of path to .projectIDE config file.
CALLER
Type:\t\t symbol list
Descrip.:\t Function list calling this function for debug purpose."
(let ((project (projectIDE-parse-config configfile nil (projectIDE-caller 'projectIDE-record-create caller)))
(record (make-projectIDE-record)))
(setf (projectIDE-record-signature record)(projectIDE-project-signature project)
(projectIDE-record-name record)(projectIDE-project-name project)
(projectIDE-record-path record) (file-name-directory configfile)
(projectIDE-record-create-time record) (current-time)
(projectIDE-record-last-open record) (current-time))
(puthash (projectIDE-project-signature project) record projectIDE-runtime-record)
(fout<<projectIDE PROJECTIDE-RECORD-FILE 'projectIDE-runtime-record (projectIDE-caller 'projectIDE-record-create caller)))
(when projectIDE-debug-mode
(projectIDE-message 'Info
(format "Project record created for \"%s\"" configfile)
nil
(projectIDE-caller 'projectIDE-record-create caller))))
(defun projectIDE-cache-create (configfile &optional caller)
"Create cache by CONFIGFILE.
CALLER is the function list calling this function.
It is uesed for debugging purpose.
CONFIGFILE
Type:\t\t string
Descrip.:\t A string of path to .projectIDE config file.
CALLER
Type:\t\t symbol list
Descrip.:\t Function list calling this function for debug purpose."
(let* ((cache (make-projectIDE-cache))
(project (projectIDE-parse-config configfile nil (projectIDE-caller 'projectIDE-cache-create caller)))
(signature (projectIDE-project-signature project))
(projectRoot (projectIDE-record-path (gethash signature projectIDE-runtime-record)))
(file (concat PROJECTIDE-CACHE-PATH signature))
(exclude (projectIDE-project-exclude project))
(exclude-modified (projectIDE-manipulate-filter projectRoot exclude))
(whitelist (projectIDE-project-whitelist project))
(whitelist-modified (projectIDE-manipulate-filter projectRoot whitelist)))
(setf (projectIDE-cache-project cache) project
(projectIDE-cache-exclude cache) exclude
(projectIDE-cache-whitelist cache) whitelist
(projectIDE-cache-file-cache cache) (fdex-new projectRoot exclude-modified whitelist-modified)
(projectIDE-cache-config-update-time cache) (current-time))
(fout<<projectIDE file 'cache (projectIDE-caller 'projectIDE-cache-create caller)))
(when projectIDE-debug-mode
(projectIDE-message 'Info
(format "Project cache created for \"%s\"" configfile)
nil
(projectIDE-caller 'projectIDE-cache-create caller))))
(defun projectIDE-consolidate-record ()
"Remove invalid or expired projectIDE-record."
(let ((signatures (projectIDE-get-all-signatures)))
(dolist (signature signatures)
(unless (projectIDE-get-project-path signature)
(projectIDE-remove-record signature)))
(setq signatures (projectIDE-get-all-signatures))
(when (>= (length signatures) projectIDE-max-record-number)
;; sorted to older first
(setq signatures (sort signatures (lambda (signature1 signature2) (time-less-p signature1 signature2))))
(dotimes (var (- (length signatures) (- projectIDE-max-record-number projectIDE-record-reduce-number)))
(projectIDE-remove-record (nth var signatures))))
(fout<<projectIDE PROJECTIDE-RECORD-FILE 'projectIDE-runtime-record (projectIDE-caller 'projectIDE-consolidate-record))))
(defun projectIDE-identify-project (&optional buffer caller)
;; If buffer is not provided, it implies this function is possibily call by find-file-hook
;; With buffer provided it means this function is possibilty call by projectIDE-initialize or projectIDE-index-project
;; to check all existing buffers
"This function check whether BUFFER is a indexed project.
CALLER is the function list calling this function.
It is uesed for debugging purpose.
If it is a indexed project, it ensures
1) the project is under projectIDE-runtime-cache
2) it is under opened buffer in the project cache
3) it is under projectIDE-runtime-Btrace
If BUFFER is not provided, current buffer is used.
BUFFER
Type\t\t: buffer
Descrip.:\t The buffer being identified.
CALLER
Type:\t\t symbol list
Descrip.:\t Function list calling this function for debug purpose."
(let (signature)
;; Comment out this so that project in project identified as the inner project
;; Find project in projectIDE-runtime-cache
;; (let ((opened-project (projectIDE-get-all-caching-signature)))
;; (while (and (not signature) (buffer-file-name buffer) (car opened-project))
;; (when (string-prefix-p (projectIDE-get-project-path (car opened-project)) (buffer-file-name buffer))
;; (setq signature (car opened-project)))
;; (setq opened-project (cdr opened-project))))
;; Search in project RECORD
(unless signature
(when (buffer-file-name buffer)
(setq signature (projectIDE-get-signature-by-path (buffer-file-name buffer)))))
;; Search .projectIDE up directory
;; Only apply to call where buffer is not provided
(unless (or buffer signature)
(let ((search-countdown projectIDE-config-file-search-up-level)
(path (file-name-directory (buffer-file-name))))
(while (and (not signature) (> search-countdown 0))
(when (file-exists-p (concat path PROJECTIDE-PROJECTROOT-IDENTIFIER))
(let* ((projectRoot (concat path PROJECTIDE-PROJECTROOT-IDENTIFIER))
(project (projectIDE-parse-config projectRoot nil (projectIDE-caller 'projectIDE-identify-project caller))))
(if (projectIDE-project-signature project)
;; found .projectIDE with signature
(progn
(setq signature (projectIDE-project-signature project))
(projectIDE-record-create projectRoot (projectIDE-caller 'projectIDE-identify-project caller))
(unless (file-exists-p (concat PROJECTIDE-CACHE-PATH signature))
(projectIDE-cache-create projectRoot (projectIDE-caller 'projectIDE-identify-project caller))))
;; found .projectIDE without signature
(if (y-or-n-p
(projectIDE-message
'Info
(format ".projectIDE root file found at \"%s\".\nMake this path as project root and index the project? " path)
nil
(projectIDE-caller 'projectIDE-identify-project '(find-file-hook))))
(progn
(projectIDE-new-project path (and projectIDE-debug-mode (list 'projectIDE-identify-project 'find-file-hook)))
(setq signature (projectIDE-get-signature-by-path (buffer-file-name buffer)))
(projectIDE-message
'Info
(format "Project Indexed\nProject\t\t\t\t: %s\nProject Directory\t: %s"
(file-name-nondirectory (directory-file-name (file-name-directory projectRoot))) path)
t
(projectIDE-caller 'projectIDE-identify-project '(find-file-hook))))
(projectIDE-message 'Info
"File opened without indexing."
t
(projectIDE-caller 'projectIDE-identify-project '(find-file-hook)))
(setq search-countdown -1)))))
(setq search-countdown (1- search-countdown)
path (file-name-directory (directory-file-name path))))))
;; When buffer is identified,
;; adds project to projectIDE-runtime-Btrace
;; ensures the project is in projectIDE-runtime-cache
;; adds to opened buffer in projectIDE-runtime-cache
;; update the last-open time of project record
(when signature
(projectIDE-track-buffer signature buffer (projectIDE-caller 'projectIDE-identify-project (or caller '(find-file-hook))))
(unless buffer
(projectIDE-message
'Info
(format "Opened file from project [%s]" (projectIDE-get-project-name signature))
t
(projectIDE-caller 'projectIDE-identify-project (or caller '(find-file-hook))))))
(when (and projectIDE-debug-mode (not signature))
(projectIDE-message
'Info
(format "Opened buffer \"%s\" is not a indexed project." (or (buffer-file-name) "invalid buffer"))
nil
(projectIDE-caller 'projectIDE-identify-project (or caller '(find-file-hook)))))))
(defun projectIDE-index-project (path &optional caller)
"This is an interactive function to let user indexing a project.
It will ask for a project root (PATH) and indexing starts there.
CALLER is the function list calling this function.
It is uesed for debugging purpose.
PATH
Type:\t\t string of directory
Descrip.:\t Path to be indexed as project root.
CALLER
Type:\t\t symbol list
Descrip.:\t Function list calling this function for debug purpose."
(interactive (list (read-directory-name "Choose the project root: "
(file-name-directory (or (buffer-file-name) user-emacs-directory)))))
(catch 'Error
(unless (projectIDE-initialize-maybe)
(projectIDE-message 'Error
"projectIDE not initialized."
t
(projectIDE-caller 'projectIDE-index-project caller))
(throw 'Error nil))
;; Index project
(if (and (file-exists-p (concat path PROJECTIDE-PROJECTROOT-IDENTIFIER))
(projectIDE-project-signature
(projectIDE-parse-config (concat path PROJECTIDE-PROJECTROOT-IDENTIFIER)
nil
(projectIDE-caller 'projectIDE-index-project caller))))
(if (yes-or-no-p
(concat (projectIDE-message 'Warning
(format ".projectIDE with signature found at \"%s\"" path)
nil
(projectIDE-caller 'projectIDE-index-project caller))
"\nChoose yes if you want to create a new signature for this project.
Choose no if you want to retain current signature.
Press C-g to cancel the operation."))
(projectIDE-new-project (file-name-as-directory path) (projectIDE-caller 'projectIDE-index-project caller))
(projectIDE-record-create (concat path PROJECTIDE-PROJECTROOT-IDENTIFIER) (projectIDE-caller 'projectIDE-index-project caller))
(projectIDE-cache-create (concat path PROJECTIDE-PROJECTROOT-IDENTIFIER) (projectIDE-caller 'projectIDE-index-project caller)))
(projectIDE-new-project (file-name-as-directory path) (projectIDE-caller 'projectIDE-index-project caller)))
;; Scan through buffers to check whether they are memeber of newly indexed project
(let ((buffers (buffer-list)))
(dolist (buffer buffers)
(projectIDE-identify-project buffer (projectIDE-caller 'projectIDE-index-project caller))))
(projectIDE-message 'Info
(format "Project Indexed\nProject\t\t\t\t: %s\nProject Directory\t: %s"
(projectIDE-project-name (projectIDE-parse-config (concat path PROJECTIDE-PROJECTROOT-IDENTIFIER)))
path)
t
(projectIDE-caller 'projectIDE-index-project caller))))
;; Indexing function ends ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;; Caching function ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;;fff (defun projectIDE-config-need-update? (signature &optional caller))
;;fff (defun projectIDE-filter-changed? (signature &optional caller))
;;fff (defun projectIDE-update-project-config (signature &optional ErrorMessage caller))
;;fff (defun projectIDE-update-cache-backend (signature &optional caller))
;;fff (defun projectIDE-background-update-cache ())
;;fff (defun projectIDE-update-cache ()) - I
;;fff (defun projectIDE-track-buffer (signature &optional buffer caller))
;;fff (defun projectIDE-untrack-buffer (&optional buffer caller))
;;fff (defun projectIDE-before-save-new-file ())
;;fff (defun projectIDE-after-save-new-file ())
;;fff (defun projectIDE-before-emacs-kill ())
(defun projectIDE-config-need-update? (signature &optional caller)
"Return t if config file of given SIGNATURE need to be updated.
The config file needs to be updated iif its modification time is
later than the last config update time in cache.
CALLER is the function list calling this function.
It is uesed for debugging purpose.
Return
Type:\t\t bool
Descrip.:\t t if config file needs update, otherwise nil.
SIGNATURE
Type:\t\t string
Descrip.:\t A project based unique ID.
CALLER
Type:\t\t symbol list
Descrip.:\t Function list calling this function for debug purpose."
(catch 'Error
(unless (projectIDE-get-cache signature)
(projectIDE-message 'Warning
(format "Project cache for \"%s\" not in projectIDE-runtime-cache." signature)
nil
(projectIDE-caller 'projectIDE-config-need-update? caller))
(throw 'Error nil))
(let ((config (projectIDE-get-config-file-path signature)))
(if config
(if (time-less-p (projectIDE-get-config-update-time signature)(fdex-modify-time config))
t
nil)
(when projectIDE-debug-mode
(projectIDE-message 'Warning
(format "Unable to find project config file for \"%s\"" signature)
nil
(projectIDE-caller 'projectIDE-config-need-update? caller)))
nil))))
(defun projectIDE-filter-changed? (signature &optional caller)
"Return t if exclude or whitelist changed in project given by SIGNATURE.
CALLER is the function list calling this function.
It is uesed for debugging purpose.
Return
Type:\t\t bool
Descri.:\t t if either exclude or whitelist had changed.
SIGNATURE
Type:\t\t string
Descrip.:\t A project based unique ID.
CALLER
Type:\t\t symbol list
Descrip.:\t Function list calling this function for debug purpose."
(catch 'Error
(unless (projectIDE-get-cache signature)
(projectIDE-message 'Warning
(format "Project cache for %s not in projectIDE-runtime-cache." signature)
nil
(projectIDE-caller 'projectIDE-filter-changed? caller))
(throw 'Error nil))
(if (and (equal (projectIDE-get-project-exclude signature) (projectIDE-get-cache-exclude signature))
(equal (projectIDE-get-project-whitelist signature) (projectIDE-get-cache-whitelist signature)))
nil
t)))
(defun projectIDE-update-project-config (signature &optional ErrorMessage caller)
"Update specific project config in projectIDE-runtime-cache.
The project updated is specified by SIGNATURE.
ERRORMESSAGE indicates whether message is displayed to minibuffer
if there is any error. Error message is not displayed by default.
This function return t if the project config is updated successfully.
CALLER is the function list calling this function.
It is uesed for debugging purpose.
Return
Type:\t\t bool
Descrip.: Return t if config is updated successfully. Otherwise, return nil.
SIGNATURE
Type:\t\t string
Descrip.:\t String of number of signature.
ERRORMESSAGE
Type:\t\t bool
Descrip.: Display error message to minibuffer if it is t.
CALLER
Type:\t\t symbol list
Descrip.:\t Function list calling this function for debug purpose."
(catch 'Error
(let* ((project-config-file (projectIDE-get-config-file-path signature))
(project (projectIDE-parse-config project-config-file nil (projectIDE-caller 'projectIDE-update-project-config caller))))
(unless project
(projectIDE-message 'Error
(format "Update project config failed. Error reading \"%s\"." project-config-file)
ErrorMessage
(projectIDE-caller 'projectIDE-update-project-config caller))
(throw 'Error nil))
(unless (equal (projectIDE-project-name project) (projectIDE-get-project-name signature))
(projectIDE-set-project-name signature (projectIDE-project-name project))
(fout<<projectIDE PROJECTIDE-RECORD-FILE 'projectIDE-runtime-record (projectIDE-caller 'projectIDE-update-project-config caller)))
(projectIDE-set-cache-project signature project))
(when projectIDE-debug-mode
(projectIDE-message 'Info
(format "Project config for project \"%s\" update successfully."
(projectIDE-get-project-name signature))
nil
(projectIDE-caller 'projectIDE-update-project-config caller)))
t))
(defun projectIDE-update-cache-backend (signature &optional caller)
"Perform a complete update on cache given by SIGNATURE.
CALLER is the function list calling this function.
It is uesed for debugging purpose.
SIGNATURE
Type:\t\t string
Descrip.:\t A project based unique ID.
CALLER
Type:\t\t symbol list
Descrip.:\t Function list calling this function for debug purpose."
(catch 'Error
;; Test whether signature is in projectIDE-runtime-cache
(unless (projectIDE-get-cache signature)
(projectIDE-message 'Error
(format "Attempt to update cache \"%s\" not in projectIDE-runtime-cache." signature)
nil
(projectIDE-caller 'projectIDE-update-cache-backend caller))
(throw 'Error nil))
;; Test whether config file need to be update and is able to update
(when (projectIDE-config-need-update? signature (projectIDE-caller 'projectIDE-update-cache-backend caller))
(unless (projectIDE-update-project-config signature nil (projectIDE-caller 'projectIDE-update-cache-backend caller))
(projectIDE-message 'Error
(format "Project config file for project %s not found." signature)
nil
(projectIDE-caller 'projectIDE-update-cache-backend caller))
(throw 'Error nil))
(run-hooks 'projectIDE-config-updated-hook))
(when (projectIDE-filter-changed? signature (projectIDE-caller 'projectIDE-update-cache-backend caller))
(projectIDE-set-cache-filter signature)
(projectIDE-set-file-cache signature)
(projectIDE-set-file-cache-state signature 0))
(with-temp-message
(projectIDE-message 'Info
"Updating cache in progress. May take some time for large project."
nil
(projectIDE-caller 'projectIDE-update-cache-backend caller))
(fdex-update (projectIDE-get-file-cache signature)))
(projectIDE-set-file-cache-state signature 2)
t))
(defun projectIDE-background-update-cache ()
"Update cache of current buffer in background by one step."
(catch 'quit
(let ((signature projectIDE-active-project)
state
cache
filehash)
(when signature
(unless (projectIDE-background-update-cache? signature)
(throw 'quit nil))
(when (projectIDE-config-need-update? signature (projectIDE-caller 'projectIDE-background-update-cache))
(projectIDE-update-project-config signature nil (projectIDE-caller 'projectIDE-background-update-cache))
(run-hooks 'projectIDE-config-updated-hook))
(setq state (projectIDE-get-file-cache-state signature))
(cond
((= state 0)
(setq filehash (projectIDE-get-file-cache signature))
(unless (fdex-updateNext filehash)
(fdex-updateRoot filehash)
(projectIDE-set-file-cache-state signature 1)))
((= state 1)
(setq filehash (projectIDE-get-file-cache signature))
(unless (fdex-updateNext filehash)
(fdex-updateRoot filehash)
(projectIDE-set-file-cache-state signature 2)
(when (projectIDE-background-generate-association? signature)
(projectIDE-flag-association-expired signature))))
((= state 2)
(unless (projectIDE-background-generate-association? signature)
(projectIDE-set-file-cache-state signature 1)
(throw 'quit nil))
(let ((buffers (projectIDE-get-buffer-list signature))
(time (current-time))
done)
(while (and (car buffers) (not done))
(unless (projectIDE-get-file-association-state (car buffers))
(projectIDE-set-file-association (projectIDE-generate-association-list (car buffers)) (car buffers))
(setq done t))
(setq buffers (cdr buffers)))
(unless done
(projectIDE-set-file-cache-state signature 1))
(when (> (time-to-seconds (time-subtract (current-time) time)) 1)
(projectIDE-message 'Warning
(format "projectIDE detects background service has been causing performance issue.
You may try setting \"cachemode = 15\" or even \"cachemode = 7\" in [%s] config file." (projectIDE-get-project-name signature))
t
(projectIDE-caller 'projectIDE-background-update-cache))))))))))
(defun projectIDE-update-cache ()
"An interactive function to update project cache of current buffer.
In simple term, it updates folders and files of the project."
(interactive)
(catch 'Error
(unless (projectIDE-initialize-maybe)
(projectIDE-message 'Error
"projectIDE not initialized."
t
(projectIDE-caller 'projectIDE-update-cache))
(throw 'Error nil))
(let ((signature projectIDE-active-project))
(unless signature
(projectIDE-message 'Warning
"Current buffer not in project record."
t
(projectIDE-caller 'projectIDE-update-cache))
(throw 'Error nil))
(projectIDE-update-cache-backend signature (projectIDE-caller 'projectIDE-update-cache))
(when projectIDE-debug-mode
(projectIDE-message 'Info
(format "Updated project cache for project \"%s\"" signature)
nil
(projectIDE-caller 'projectIDE-update-cache))))
t))
(defun projectIDE-track-buffer (signature &optional buffer caller)
"Track BUFFER as a member of project given by SIGNATURE.
Tracking means
1) put buffer in projectIDE-runtime-BTrace
2) add buffer filename to opened buffer in projectIDE-runtime-record.
If buffer is not provide, current buffer is used.
CALLER is the function list calling this function.
It is uesed for debugging purpose.