forked from haskell/haskell-mode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
haskell-cabal.el
1191 lines (1042 loc) · 44.7 KB
/
haskell-cabal.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
;;; haskell-cabal.el --- Support for Cabal packages -*- lexical-binding: t -*-
;; Copyright © 2007, 2008 Stefan Monnier
;; 2016 Arthur Fayzrakhmanov
;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
;; 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, or (at your option)
;; any later version.
;; This file 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:
;; Todo:
;; - distinguish continued lines from indented lines.
;; - indent-line-function.
;; - outline-minor-mode.
;;; Code:
;; (defun haskell-cabal-extract-fields-from-doc ()
;; (require 'xml)
;; (let ((section (completing-read
;; "Section: "
;; '("general-fields" "library" "executable" "buildinfo"))))
;; (goto-char (point-min))
;; (search-forward (concat "<sect3 id=\"" section "\">")))
;; (let* ((xml (xml-parse-region
;; (progn (search-forward "<variablelist>") (match-beginning 0))
;; (progn (search-forward "</variablelist>") (point))))
;; (varlist (cl-remove-if-not 'consp (cl-cddar xml)))
;; (syms (mapcar (lambda (entry) (cl-caddr (assq 'literal (assq 'term entry))))
;; varlist))
;; (fields (mapcar (lambda (sym) (substring-no-properties sym 0 -1)) syms)))
;; fields))
(require 'cl-lib)
(require 'haskell-utils)
(defcustom haskell-hasktags-path "hasktags"
"Path to `hasktags' executable."
:group 'haskell
:type 'string)
(defcustom haskell-hasktags-arguments '("-e" "-x")
"Additional arguments for `hasktags' executable.
By default these are:
-e - generate ETAGS file
-x - generate additional information in CTAGS file."
:group 'haskell
:type '(list string))
(defconst haskell-cabal-general-fields
;; Extracted with (haskell-cabal-extract-fields-from-doc "general-fields")
'("name" "version" "cabal-version" "license" "license-file" "copyright"
"author" "maintainer" "stability" "homepage" "package-url" "synopsis"
"description" "category" "tested-with" "build-depends" "data-files"
"extra-source-files" "extra-tmp-files" "import"))
(defconst haskell-cabal-library-fields
;; Extracted with (haskell-cabal-extract-fields-from-doc "library")
'("exposed-modules"))
(defconst haskell-cabal-executable-fields
;; Extracted with (haskell-cabal-extract-fields-from-doc "executable")
'("executable" "main-is"))
(defconst haskell-cabal-buildinfo-fields
;; Extracted with (haskell-cabal-extract-fields-from-doc "buildinfo")
'("buildable" "other-modules" "hs-source-dirs" "extensions" "ghc-options"
"ghc-prof-options" "hugs-options" "nhc-options" "includes"
"install-includes" "include-dirs" "c-sources" "extra-libraries"
"extra-lib-dirs" "cc-options" "ld-options" "frameworks"))
(defvar haskell-cabal-mode-syntax-table
(let ((st (make-syntax-table)))
;; The comment syntax can't be described simply in syntax-table.
;; We could use font-lock-syntactic-keywords, but is it worth it?
;; (modify-syntax-entry ?- ". 12" st)
(modify-syntax-entry ?\n ">" st)
(modify-syntax-entry ?- "w" st)
st))
(defvar haskell-cabal-font-lock-keywords
;; The comment syntax can't be described simply in syntax-table.
;; We could use font-lock-syntactic-keywords, but is it worth it?
'(("^[ \t]*--.*" . font-lock-comment-face)
("^ *\\([^ \t:]+\\):" (1 font-lock-keyword-face))
("^\\(Library\\)[ \t]*\\({\\|$\\)" (1 font-lock-keyword-face))
("^\\(Executable\\|Test-Suite\\|Benchmark\\|Common\\|package\\)[ \t]+\\([^\n \t]*\\)"
(1 font-lock-keyword-face) (2 font-lock-function-name-face))
("^\\(Flag\\|install-dirs\\|repository\\)[ \t]+\\([^\n \t]*\\)"
(1 font-lock-keyword-face) (2 font-lock-constant-face))
("^\\(Source-Repository\\)[ \t]+\\(head\\|this\\)"
(1 font-lock-keyword-face) (2 font-lock-constant-face))
("^\\(haddock\\|source-repository-package\\|program-locations\\|program-default-options\\)\\([ \t]\\|$\\)"
(1 font-lock-keyword-face))
("^ *\\(if\\)[ \t]+.*\\({\\|$\\)" (1 font-lock-keyword-face))
("^ *\\(}[ \t]*\\)?\\(else\\)[ \t]*\\({\\|$\\)"
(2 font-lock-keyword-face))
("\\<\\(?:True\\|False\\)\\>"
(0 font-lock-constant-face))))
(defvar haskell-cabal-buffers nil
"List of Cabal buffers.")
(defun haskell-cabal-buffers-clean (&optional buffer)
"Refresh list of known cabal buffers.
Check each buffer in variable `haskell-cabal-buffers' and remove
it from list if one of the following conditions are hold:
+ buffer is killed;
+ buffer's mode is not derived from `haskell-cabal-mode';
+ buffer is a BUFFER (if given)."
(let ((bufs ()))
(dolist (buf haskell-cabal-buffers)
(if (and (buffer-live-p buf)
(not (eq buf buffer))
(with-current-buffer buf (derived-mode-p 'haskell-cabal-mode)))
(push buf bufs)))
(setq haskell-cabal-buffers bufs)))
(defun haskell-cabal-unregister-buffer ()
"Exclude current buffer from global list of known cabal buffers."
(haskell-cabal-buffers-clean (current-buffer)))
;;;###autoload
(add-to-list 'auto-mode-alist '("\\.cabal\\'\\|/cabal\\.project\\|/\\.cabal/config\\'" . haskell-cabal-mode))
(defvar haskell-cabal-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "C-c C-s") 'haskell-cabal-subsection-arrange-lines)
(define-key map (kbd "C-M-n") 'haskell-cabal-next-section)
(define-key map (kbd "C-M-p") 'haskell-cabal-previous-section)
(define-key map (kbd "M-n") 'haskell-cabal-next-subsection)
(define-key map (kbd "M-p") 'haskell-cabal-previous-subsection)
(define-key map (kbd "C-<down>") 'haskell-cabal-next-subsection)
(define-key map (kbd "C-<up>") 'haskell-cabal-previous-subsection)
(define-key map (kbd "C-c C-f") 'haskell-cabal-find-or-create-source-file)
(define-key map (kbd "M-g l") 'haskell-cabal-goto-library-section)
(define-key map (kbd "M-g e") 'haskell-cabal-goto-executable-section)
(define-key map (kbd "M-g b") 'haskell-cabal-goto-benchmark-section)
(define-key map (kbd "M-g o") 'haskell-cabal-goto-common-section)
(define-key map (kbd "M-g t") 'haskell-cabal-goto-test-suite-section)
map))
;;;###autoload
(define-derived-mode haskell-cabal-mode fundamental-mode "Haskell-Cabal"
"Major mode for Cabal package description files."
(setq-local font-lock-defaults
'(haskell-cabal-font-lock-keywords t t nil nil))
(add-to-list 'haskell-cabal-buffers (current-buffer))
(add-hook 'change-major-mode-hook 'haskell-cabal-unregister-buffer nil 'local)
(add-hook 'kill-buffer-hook 'haskell-cabal-unregister-buffer nil 'local)
(setq-local comment-start "-- ")
(setq-local comment-start-skip "\\(^[ \t]*\\)--[ \t]*")
(setq-local comment-end "")
(setq-local comment-end-skip "[ \t]*\\(\\s>\\|\n\\)")
(setq-local indent-line-function 'haskell-cabal-indent-line)
(setq indent-tabs-mode nil)
)
(make-obsolete 'haskell-cabal-get-setting
'haskell-cabal--get-field
"March 14, 2016")
(defalias 'haskell-cabal-get-setting 'haskell-cabal--get-field
"Try to read value of field with NAME from current buffer.
Obsolete function. Defined for backward compatibility. Use
`haskell-cabal--get-field' instead.")
(defun haskell-cabal--get-field (name)
"Try to read value of field with NAME from current buffer."
(save-excursion
(let ((case-fold-search t))
(goto-char (point-min))
(when (re-search-forward
(concat "^[ \t]*" (regexp-quote name)
":[ \t]*\\(.*\\(\n[ \t]+[ \t\n].*\\)*\\)")
nil t)
(let ((val (match-string 1))
(start 1))
(when (match-end 2) ;Multiple lines.
;; The documentation is not very precise about what to do about
;; the \n and the indentation: are they part of the value or
;; the encoding? I take the point of view that \n is part of
;; the value (so that values can span multiple lines as well),
;; and that only the first char in the indentation is part of
;; the encoding, the rest is part of the value (otherwise, lines
;; in the value cannot start with spaces or tabs).
(while (string-match "^[ \t]\\(?:\\.$\\)?" val start)
(setq start (1+ (match-beginning 0)))
(setq val (replace-match "" t t val))))
val)))))
(make-obsolete 'haskell-cabal-guess-setting
'haskell-cabal-get-field
"March 14, 2016")
(defalias 'haskell-cabal-guess-setting 'haskell-cabal-get-field
"Read the value of field with NAME from project's cabal file.
Obsolete function. Defined for backward compatibility. Use
`haskell-cabal-get-field' instead.")
;;;###autoload
(defun haskell-cabal-get-field (name)
"Read the value of field with NAME from project's cabal file.
If there is no valid .cabal file to get the setting from (or
there is no corresponding setting with that name in the .cabal
file), then this function returns nil."
(interactive)
(when (and name buffer-file-name)
(let ((cabal-file (haskell-cabal-find-file)))
(when (and cabal-file (file-readable-p cabal-file))
(with-temp-buffer
(insert-file-contents cabal-file)
(haskell-cabal--get-field name))))))
;;;###autoload
(defun haskell-cabal-get-dir (&optional use-defaults)
"Get the Cabal dir for a new project. Various ways of figuring this out,
and indeed just prompting the user. Do them all."
(let* ((file (haskell-cabal-find-file))
(dir (if file (file-name-directory file) default-directory)))
(if use-defaults
dir
(haskell-utils-read-directory-name
(format "Cabal dir%s: " (if file (format " (guessed from %s)" (file-relative-name file)) ""))
dir))))
(defun haskell-cabal-compute-checksum (dir)
"Compute MD5 checksum of package description file in DIR.
Return nil if no Cabal description file could be located via
`haskell-cabal-find-pkg-desc'."
(let ((cabal-file (haskell-cabal-find-pkg-desc dir)))
(when cabal-file
(with-temp-buffer
(insert-file-contents cabal-file)
(md5 (buffer-string))))))
(defun haskell-cabal-find-file (&optional dir)
"Search for package description file upwards starting from DIR.
If DIR is nil, `default-directory' is used as starting point for
directory traversal. Upward traversal is aborted if file owner
changes. Uses `haskell-cabal-find-pkg-desc' internally."
(let ((use-dir (or dir default-directory)))
(while (and use-dir (not (file-directory-p use-dir)))
(setq use-dir (file-name-directory (directory-file-name use-dir))))
(when use-dir
(catch 'found
(let ((user (nth 2 (file-attributes use-dir)))
;; Abbreviate, so as to stop when we cross ~/.
(root (abbreviate-file-name use-dir)))
;; traverse current dir up to root as long as file owner doesn't change
(while (and root (equal user (nth 2 (file-attributes root))))
(let ((cabal-file (haskell-cabal-find-pkg-desc root)))
(when cabal-file
(throw 'found cabal-file)))
(let ((proot (file-name-directory (directory-file-name root))))
(if (equal proot root) ;; fix-point reached?
(throw 'found nil)
(setq root proot))))
nil)))))
(defun haskell-cabal-find-pkg-desc (dir &optional allow-multiple)
"Find a package description file in the directory DIR.
Returns nil if none or multiple \".cabal\" files were found. If
ALLOW-MULTIPLE is non nil, in case of multiple \".cabal\" files,
a list is returned instead of failing with a nil result."
;; This is basically a port of Cabal's
;; Distribution.Simple.Utils.findPackageDesc function
;; http://hackage.haskell.org/packages/archive/Cabal/1.16.0.3/doc/html/Distribution-Simple-Utils.html
;; but without the exception throwing.
(let* ((cabal-files
(cl-remove-if 'file-directory-p
(cl-remove-if-not 'file-exists-p
(directory-files dir t ".\\.cabal\\'")))))
(cond
((= (length cabal-files) 1) (car cabal-files)) ;; exactly one candidate found
(allow-multiple cabal-files) ;; pass-thru multiple candidates
(t nil))))
(defun haskell-cabal-find-dir (&optional dir)
"Like `haskell-cabal-find-file' but returns directory instead.
See `haskell-cabal-find-file' for meaning of DIR argument."
(let ((cabal-file (haskell-cabal-find-file dir)))
(when cabal-file
(file-name-directory cabal-file))))
;;;###autoload
(defun haskell-cabal-visit-file (other-window)
"Locate and visit package description file for file visited by current buffer.
This uses `haskell-cabal-find-file' to locate the closest
\".cabal\" file and open it. This command assumes a common Cabal
project structure where the \".cabal\" file is in the top-folder
of the project, and all files related to the project are in or
below the top-folder. If called with non-nil prefix argument
OTHER-WINDOW use `find-file-other-window'."
(interactive "P")
;; Note: We aren't allowed to rely on haskell-session here (which,
;; in pathological cases, can have a different .cabal file
;; associated with the current buffer)
(if buffer-file-name
(let ((cabal-file (haskell-cabal-find-file (file-name-directory buffer-file-name))))
(if cabal-file
(if other-window
(find-file-other-window cabal-file)
(find-file cabal-file))
(error "Could not locate \".cabal\" file for %S" buffer-file-name)))
(error "Cannot locate \".cabal\" file for buffers not visiting any file")))
(defvar haskell-cabal-commands
'("install"
"update"
"list"
"info"
"upgrade"
"fetch"
"unpack"
"check"
"sdist"
"upload"
"report"
"init"
"configure"
"build"
"copy"
"haddock"
"clean"
"hscolour"
"register"
"test"
"help"
"run"))
;;;###autoload
(defgroup haskell-cabal nil
"Haskell cabal files"
:group 'haskell
)
(defconst haskell-cabal-section-header-regexp "^[[:alnum:]]" )
(defconst haskell-cabal-subsection-header-regexp "^[ \t]*[[:alnum:]]\\w*:")
(defconst haskell-cabal-comment-regexp "^[ \t]*--")
(defconst haskell-cabal-empty-regexp "^[ \t]*$")
(defconst haskell-cabal-conditional-regexp "^[ \t]*\\(\\if\\|else\\|}\\)")
(defun haskell-cabal-classify-line ()
"Classify the current line into 'section-header 'subsection-header 'section-data 'comment and 'empty '"
(save-excursion
(beginning-of-line)
(cond
((looking-at haskell-cabal-subsection-header-regexp ) 'subsection-header)
((looking-at haskell-cabal-section-header-regexp) 'section-header)
((looking-at haskell-cabal-comment-regexp) 'comment)
((looking-at haskell-cabal-empty-regexp ) 'empty)
((looking-at haskell-cabal-conditional-regexp ) 'conditional)
(t 'section-data))))
(defun haskell-cabal-header-p ()
"Is the current line a section or subsection header?"
(cl-case (haskell-cabal-classify-line)
((section-header subsection-header) t)))
(defun haskell-cabal-section-header-p ()
"Is the current line a section or subsection header?"
(cl-case (haskell-cabal-classify-line)
((section-header) t)))
(defun haskell-cabal-section-beginning ()
"Find the beginning of the current section"
(save-excursion
(while (not (or (bobp) (haskell-cabal-section-header-p)))
(forward-line -1))
(point)))
(defun haskell-cabal-beginning-of-section ()
"go to the beginning of the section"
(interactive)
(goto-char (haskell-cabal-section-beginning))
)
(defun haskell-cabal-section-end ()
"Find the end of the current section"
(interactive)
(save-excursion
(if (re-search-forward "\n\\([ \t]*\n\\)*[[:alnum:]]" nil t)
(match-beginning 0)
(point-max))))
(defun haskell-cabal-end-of-section ()
"go to the end of the section"
(interactive)
(goto-char (haskell-cabal-section-end)))
(defun haskell-cabal-next-section ()
"Go to the next section"
(interactive)
(when (haskell-cabal-section-header-p) (forward-line))
(while (not (or (eobp) (haskell-cabal-section-header-p)))
(forward-line)))
(defun haskell-cabal-previous-section ()
"Go to the next section"
(interactive)
(when (haskell-cabal-section-header-p) (forward-line -1))
(while (not (or (bobp) (haskell-cabal-section-header-p)))
(forward-line -1)))
(defun haskell-cabal-subsection-end ()
"find the end of the current subsection"
(save-excursion
(haskell-cabal-beginning-of-subsection)
(forward-line)
(while (and (not (eobp))
(member (haskell-cabal-classify-line) '(empty section-data)))
(forward-line))
(unless (eobp) (forward-line -1))
(while (and (equal (haskell-cabal-classify-line) 'empty)
(not (bobp)))
(forward-line -1))
(end-of-line)
(point)))
(defun haskell-cabal-end-of-subsection ()
"go to the end of the current subsection"
(interactive)
(goto-char (haskell-cabal-subsection-end)))
(defun haskell-cabal-section ()
"Get the name and data of the associated section"
(save-excursion
(haskell-cabal-beginning-of-section)
(when (and (haskell-cabal-section-header-p)
(looking-at "^\\(\\w+\\)[ \t]*\\(.*\\)$"))
(list :name (match-string-no-properties 1)
:value (match-string-no-properties 2)
:beginning (match-beginning 0)
:end (haskell-cabal-section-end)))))
(defun haskell-cabal-subsection ()
"Get the name and bounds of of the current subsection"
(save-excursion
(haskell-cabal-beginning-of-subsection)
(when (looking-at "\\([ \t]*\\(\\w*\\):\\)[ \t]*")
(list :name (match-string-no-properties 2)
:beginning (match-end 0)
:end (save-match-data (haskell-cabal-subsection-end))
:data-start-column (save-excursion (goto-char (match-end 0))
(current-column))
:data-indent-column (save-excursion (goto-char (match-end 0))
(when (looking-at "\n +\\(\\w*\\)") (goto-char (match-beginning 1)))
(current-column)
)))))
(defun haskell-cabal-section-name (section)
(plist-get section :name))
(defun haskell-cabal-section-value (section)
(plist-get section :value))
(defun haskell-cabal-section-start (section)
(plist-get section :beginning))
(defun haskell-cabal-section-data-start-column (section)
(plist-get section :data-start-column))
(defun haskell-cabal-section-data-indent-column (section)
(plist-get section :data-indent-column))
(defun haskell-cabal-map-component-type (component-type)
"Map from cabal file COMPONENT-TYPE to build command component-type."
(let ((component-type (downcase component-type)))
(cond ((equal component-type "executable") "exe")
((equal component-type "test-suite") "test")
((equal component-type "benchmark") "bench"))))
(defun haskell-cabal-enum-targets (&optional process-type)
"Enumerate .cabal targets. PROCESS-TYPE determines the format of the returned target."
(let ((cabal-file (haskell-cabal-find-file))
(process-type (if process-type process-type 'ghci)))
(when (and cabal-file (file-readable-p cabal-file))
(with-temp-buffer
(insert-file-contents cabal-file)
(haskell-cabal-mode)
(goto-char (point-min))
(let ((matches)
(package-name (haskell-cabal--get-field "name")))
(haskell-cabal-next-section)
(while (not (eobp))
(if (haskell-cabal-source-section-p (haskell-cabal-section))
(let* ((section (haskell-cabal-section))
(component-type (haskell-cabal-section-name section))
(val (car (split-string
(haskell-cabal-section-value section)))))
(if (equal (downcase component-type) "library")
(let ((lib-target (if (eq 'stack-ghci process-type)
(concat package-name ":lib")
(concat "lib:" package-name))))
(push lib-target matches))
(push (concat (when (eq 'stack-ghci process-type)
(concat package-name ":"))
(haskell-cabal-map-component-type component-type)
":"
val)
matches))))
(haskell-cabal-next-section))
(reverse matches))))))
(defmacro haskell-cabal-with-subsection (subsection replace &rest funs)
"Copy subsection data into a temporary buffer, save indentation
and execute FORMS
If REPLACE is non-nil the subsection data is replaced with the
resulting buffer-content"
(let ((section (make-symbol "section"))
(beg (make-symbol "beg"))
(end (make-symbol "end"))
(start-col (make-symbol "start-col"))
(section-data (make-symbol "section-data")))
`(let* ((,section ,subsection)
(,beg (plist-get ,section :beginning))
(,end (plist-get ,section :end))
(,start-col (plist-get ,section :data-start-column))
(,section-data (buffer-substring ,beg ,end)))
(save-excursion
(prog1
(with-temp-buffer
(setq indent-tabs-mode nil)
(indent-to ,start-col)
(insert ,section-data)
(goto-char (point-min))
(prog1
(progn (haskell-cabal-save-indentation ,@funs))
(goto-char (point-min))
(when (looking-at (format "[ ]\\{0,%d\\}" (1+ ,start-col)))
(replace-match ""))
(setq ,section-data (buffer-substring (point-min) (point-max)))))
,@(when replace
`((delete-region ,beg ,end)
(goto-char ,beg)
(insert ,section-data))))))))
(defmacro haskell-cabal-each-line (&rest fun)
"Execute FORMS on each line"
`(save-excursion
(while (< (point) (point-max))
,@fun
(forward-line))))
(defun haskell-cabal-chomp-line ()
"Remove leading and trailing whitespaces from current line"
(beginning-of-line)
(when (looking-at "^[ \t]*\\([^ \t]\\|\\(?:[^ \t].*[^ \t]\\)\\)[ \t]*$")
(replace-match (match-string 1) nil t)
t))
(defun haskell-cabal-min-indentation (&optional beg end)
"Compute largest common whitespace prefix of each line in between BEG and END"
(save-excursion
(goto-char (or beg (point-min)))
(let ((min-indent nil))
(while (< (point) (or end (point-max)))
(let ((indent (current-indentation)))
(if (and (not (haskell-cabal-ignore-line-p))
(or (not min-indent)
(< indent min-indent)))
(setq min-indent indent)))
(forward-line))
min-indent)))
(defun haskell-cabal-ignore-line-p ()
"Does line only contain whitespaces and comments?"
(save-excursion
(beginning-of-line)
(looking-at "^[ \t]*\\(?:--.*\\)?$")))
(defun haskell-cabal-kill-indentation ()
"Remove longest common whitespace prefix from each line"
(goto-char (point-min))
(let ((indent (haskell-cabal-min-indentation)))
(haskell-cabal-each-line (unless (haskell-cabal-ignore-line-p)
(delete-char indent)) )
indent))
(defun haskell-cabal-add-indentation (indent)
(goto-char (point-min))
(haskell-cabal-each-line
(unless (haskell-cabal-ignore-line-p)
(indent-to indent))))
(defmacro haskell-cabal-save-indentation (&rest funs)
"Strip indentation from each line, execute FORMS and reinstate indentation
so that the indentation of the FIRST LINE matches"
(let ((old-l1-indent (make-symbol "new-l1-indent"))
(new-l1-indent (make-symbol "old-l1-indent")))
`(let ( (,old-l1-indent (save-excursion
(goto-char (point-min))
(current-indentation))))
(unwind-protect
(progn
(haskell-cabal-kill-indentation)
,@funs)
(progn
(goto-char (point-min))
(let ((,new-l1-indent (current-indentation)))
(haskell-cabal-add-indentation (- ,old-l1-indent
,new-l1-indent))))))))
(defun haskell-cabal-comma-separatorp (pos)
"Return non-nil when the char at POS is a comma separator.
Characters that are not a comma, or commas inside a commment or
string, are not comma separators."
(when (eq (char-after pos) ?,)
(let ((ss (syntax-ppss pos)))
(not
(or
;; inside a string
(nth 3 ss)
;; inside a comment
(nth 4 ss))))))
(defun haskell-cabal-strip-list-and-detect-style ()
"Strip commas from a comma-separated list.
Detect and return the comma style. The possible options are:
before: a comma at the start of each line (except the first), e.g.
Foo
, Bar
after: a comma at the end of each line (except the last), e.g.
Foo,
Bar
single: everything on a single line, but comma-separated, e.g.
Foo, Bar
nil: no commas, e.g.
Foo Bar
If the styles are mixed, the position of the first comma
determines the style. If there is only one element then `after'
style is assumed."
(let (comma-style)
;; split list items on single line
(goto-char (point-min))
(while (re-search-forward
"\\([^ \t,\n]\\)[ \t]*\\(,\\)[ \t]*\\([^ \t,\n]\\)" nil t)
(when (haskell-cabal-comma-separatorp (match-beginning 2))
(setq comma-style 'single)
(replace-match "\\1\n\\3" nil nil)))
;; remove commas before
(goto-char (point-min))
(while (re-search-forward "^\\([ \t]*\\),\\([ \t]*\\)" nil t)
(setq comma-style 'before)
(replace-match "" nil nil))
;; remove trailing commas
(goto-char (point-min))
(while (re-search-forward ",[ \t]*$" nil t)
(unless (eq comma-style 'before)
(setq comma-style 'after))
(replace-match "" nil nil))
;; if there is just one line then set default as 'after
(unless comma-style
(goto-char (point-min))
(forward-line)
(when (eobp)
(setq comma-style 'after)))
(goto-char (point-min))
(haskell-cabal-each-line (haskell-cabal-chomp-line))
comma-style))
(defun haskell-cabal-listify (comma-style)
"Add commas so that the buffer contains a comma-separated list.
Respect the COMMA-STYLE, see
`haskell-cabal-strip-list-and-detect-style' for the possible
styles."
(cl-case comma-style
('before
(goto-char (point-min))
(while (haskell-cabal-ignore-line-p) (forward-line))
(indent-to 2)
(forward-line)
(haskell-cabal-each-line
(unless (haskell-cabal-ignore-line-p)
(insert ", "))))
('after
(goto-char (point-max))
(while (equal 0 (forward-line -1))
(unless (haskell-cabal-ignore-line-p)
(end-of-line)
(insert ",")
(beginning-of-line))))
('single
(goto-char (point-min))
(while (not (eobp))
(end-of-line)
(unless (eobp)
(insert ", ")
(delete-char 1)
(just-one-space))))))
(defmacro haskell-cabal-with-cs-list (&rest funs)
"Format the buffer so that each line contains a list element.
Respect the comma style."
(let ((comma-style (make-symbol "comma-style")))
`(let ((,comma-style
(save-excursion
(haskell-cabal-strip-list-and-detect-style))))
(unwind-protect (progn ,@funs)
(haskell-cabal-listify ,comma-style)))))
(defun haskell-cabal-sort-lines-key-fun ()
(when (looking-at "[ \t]*--[ \t,]*")
(goto-char (match-end 0)))
nil)
(defmacro haskell-cabal-save-position (&rest forms)
"Save position as mark, execute FORMs and go back to mark"
`(prog2
(haskell-cabal-mark)
(progn ,@forms)
(haskell-cabal-goto-mark)
(haskell-cabal-remove-mark)))
(defun haskell-cabal-sort-lines-depends-compare (key1 key2)
(let* ((key1str (buffer-substring (car key1) (cdr key1)))
(key2str (buffer-substring (car key2) (cdr key2)))
(base-regex "^[ \t]*base\\($\\|[^[:alnum:]-]\\)"))
(cond
((string-match base-regex key1str) t)
((string-match base-regex key2str) nil)
(t (string< key1str key2str)))))
(defun haskell-cabal-subsection-arrange-lines ()
"Sort lines of current subsection"
(interactive)
(haskell-cabal-save-position
(let* ((subsection (haskell-cabal-section-name (haskell-cabal-subsection)))
(compare-lines (if (string= (downcase subsection) "build-depends")
'haskell-cabal-sort-lines-depends-compare
nil)))
(haskell-cabal-with-subsection
(haskell-cabal-subsection) t
(haskell-cabal-with-cs-list
(sort-subr nil 'forward-line 'end-of-line
'haskell-cabal-sort-lines-key-fun
'end-of-line
compare-lines
))))))
(defun haskell-cabal-subsection-beginning ()
"find the beginning of the current subsection"
(save-excursion
(while (and (not (bobp))
(not (haskell-cabal-header-p)))
(forward-line -1))
(back-to-indentation)
(point)))
(defun haskell-cabal-beginning-of-subsection ()
"go to the beginning of the current subsection"
(interactive)
(goto-char (haskell-cabal-subsection-beginning)))
(defun haskell-cabal-next-subsection ()
"go to the next subsection"
(interactive)
(if (haskell-cabal-header-p) (forward-line))
(while (and (not (eobp))
(not (haskell-cabal-header-p)))
(forward-line))
(haskell-cabal-forward-to-line-entry))
(defun haskell-cabal-previous-subsection ()
"go to the previous subsection"
(interactive)
(if (haskell-cabal-header-p) (forward-line -1))
(while (and (not (bobp))
(not (haskell-cabal-header-p)))
(forward-line -1))
(haskell-cabal-forward-to-line-entry)
)
(defun haskell-cabal-find-subsection-by (section pred)
"Find subsection with name NAME"
(save-excursion
(when section (goto-char (haskell-cabal-section-start section)))
(let* ((end (if section (haskell-cabal-section-end) (point-max)))
(found nil))
(while (and (< (point) end)
(not found))
(let ((subsection (haskell-cabal-subsection)))
(when (and subsection (funcall pred subsection))
(setq found subsection)))
(haskell-cabal-next-subsection))
found)))
(defun haskell-cabal-find-subsection (section name)
"Find subsection with name NAME"
(let ((downcase-name (downcase name)))
(haskell-cabal-find-subsection-by
section
`(lambda (subsection)
(string= (downcase (haskell-cabal-section-name subsection))
,downcase-name)))))
(defun haskell-cabal-goto-subsection (name)
(let ((subsection (haskell-cabal-find-subsection (haskell-cabal-section) name)))
(when subsection
(goto-char (haskell-cabal-section-start subsection)))))
(defun haskell-cabal-goto-exposed-modules ()
(interactive)
(haskell-cabal-goto-subsection "exposed-modules"))
(defun haskell-cabal-subsection-entry-list (section name)
"Get the data of a subsection as a list"
(let ((subsection (haskell-cabal-find-subsection section name)))
(when subsection
(haskell-cabal-with-subsection
subsection nil
(haskell-cabal-with-cs-list
(delete-matching-lines
(format "\\(?:%s\\)\\|\\(?:%s\\)"
haskell-cabal-comment-regexp
haskell-cabal-empty-regexp)
(point-min) (point-max))
(split-string (buffer-substring-no-properties (point-min) (point-max))
"\n" t))))))
(defun haskell-cabal-remove-mark ()
(remove-list-of-text-properties (point-min) (point-max)
'(haskell-cabal-marker)))
(defun haskell-cabal-mark ()
"Mark the current position with the text property haskell-cabal-marker"
(haskell-cabal-remove-mark)
(put-text-property (line-beginning-position) (line-end-position)
'haskell-cabal-marker 'marked-line)
(put-text-property (point) (1+ (point))
'haskell-cabal-marker 'marked))
(defun haskell-cabal-goto-mark ()
"Go to marked line"
(let ((marked-pos (text-property-any (point-min) (point-max)
'haskell-cabal-marker
'marked))
(marked-line (text-property-any (point-min) (point-max)
'haskell-cabal-marker
'marked-line) )
)
(cond (marked-pos (goto-char marked-pos))
(marked-line (goto-char marked-line)))))
(defmacro haskell-cabal-with-subsection-line (replace &rest forms)
"Mark line, copy subsection data into a temporary buffer, save indentation
and execute FORMS at the marked line.
If REPLACE is non-nil the subsection data is replaced with the
resulting buffer-content. Unmark line at the end."
`(progn
(haskell-cabal-mark)
(unwind-protect
(haskell-cabal-with-subsection (haskell-cabal-subsection) ,replace
(haskell-cabal-goto-mark)
,@forms)
(haskell-cabal-remove-mark))))
(defun haskell-cabal-get-line-content ()
(haskell-cabal-with-subsection-line
nil
(haskell-cabal-with-cs-list
(haskell-cabal-goto-mark)
(buffer-substring-no-properties (line-beginning-position)
(line-end-position)))))
(defun haskell-cabal-module-to-filename (module)
(concat (replace-regexp-in-string "[.]" "/" module ) ".hs"))
(defconst haskell-cabal-module-sections '("exposed-modules" "other-modules")
"List of sections that contain module names"
)
(defconst haskell-cabal-file-sections
'("main-is" "c-sources" "data-files" "extra-source-files"
"extra-doc-files" "extra-tmp-files" )
"List of subsections that contain filenames"
)
(defconst haskell-cabal-source-bearing-sections
'("library" "executable" "test-suite" "benchmark"))
(defun haskell-cabal-source-section-p (section)
(not (not (member (downcase (haskell-cabal-section-name section))
haskell-cabal-source-bearing-sections))))
(defun haskell-cabal-line-filename ()
"Expand filename in current line according to the subsection type
Module names in exposed-modules and other-modules are expanded by replacing each dot (.) in the module name with a forward slash (/) and appending \".hs\"
Example: Foo.Bar.Quux ==> Foo/Bar/Quux.hs
Source names from main-is and c-sources sections are left untouched
"
(let ((entry (haskell-cabal-get-line-content))
(subsection (downcase (haskell-cabal-section-name
(haskell-cabal-subsection)))))
(cond ((member subsection haskell-cabal-module-sections)
(haskell-cabal-module-to-filename entry))
((member subsection haskell-cabal-file-sections) entry))))
(defun haskell-cabal-join-paths (&rest args)
"Crude hack to replace f-join"
(mapconcat 'identity args "/")
)
(defun haskell-cabal-find-or-create-source-file ()
"Open the source file this line refers to."
(interactive)
(let* ((src-dirs (append (haskell-cabal-subsection-entry-list
(haskell-cabal-section) "hs-source-dirs")
'("")))
(base-dir (file-name-directory (buffer-file-name)))
(filename (haskell-cabal-line-filename)))
(when filename
(let ((candidates
(delq nil (mapcar
(lambda (dir)
(let ((file (haskell-cabal-join-paths base-dir
dir
filename)))
(when (and (file-readable-p file)
(not (file-directory-p file)))
file)))
src-dirs))))
(if (null candidates)
(unwind-protect
(progn
(haskell-mode-toggle-interactive-prompt-state)
(let* ((src-dir
(haskell-cabal-join-paths base-dir
(or (car src-dirs) "")))
(newfile (haskell-cabal-join-paths src-dir filename))
(do-create-p (y-or-n-p (format "Create file %s ?" newfile))))
(when do-create-p
(find-file-other-window newfile ))))
(haskell-mode-toggle-interactive-prompt-state t))
(find-file-other-window (car candidates)))))))
(defun haskell-cabal-find-section-type (type &optional wrap)
(save-excursion
(haskell-cabal-next-section)
(while
(not
(or
(eobp)
(string=
(downcase type)
(downcase (haskell-cabal-section-name (haskell-cabal-section))))))
(haskell-cabal-next-section))
(if (eobp)
(if wrap (progn
(goto-char (point-min))
(haskell-cabal-find-section-type type nil) )
nil)
(point))))
(defun haskell-cabal-goto-section-type (type)