@@ -309,14 +309,6 @@ OTHER-WINDOW use `find-file-other-window'."
309
309
:group 'haskell
310
310
)
311
311
312
- (defcustom haskell-cabal-list-comma-position
313
- 'before
314
- " Where to put the comma in lists"
315
- :safe t
316
- :group 'haskell-cabal
317
- :type '(choice (const before)
318
- (const after)))
319
-
320
312
(defconst haskell-cabal-section-header-regexp " ^[[:alnum:]]" )
321
313
(defconst haskell-cabal-subsection-header-regexp " ^[ \t ]*[[:alnum:]]\\ w*:" )
322
314
(defconst haskell-cabal-comment-regexp " ^[ \t ]*--" )
@@ -582,27 +574,56 @@ string, are not comma separators."
582
574
; ; inside a comment
583
575
(nth 4 ss))))))
584
576
577
+ (defun haskell-cabal-strip-list-and-detect-style ()
578
+ " Strip commas from a comma-separated list.
579
+ Detect and return the comma style. The possible options are:
585
580
586
- (defun haskell-cabal-strip-list ()
587
- " Strip commas from a comma-separated list."
588
- (goto-char (point-min ))
589
- ; ; split list items on single line
590
- (while (re-search-forward
591
- " \\ ([^ \t ,\n ]\\ )[ \t ]*\\ (,\\ )[ \t ]*\\ ([^ \t ,\n ]\\ )" nil t )
592
- (when (haskell-cabal-comma-separatorp (match-beginning 2 ))
593
- (replace-match " \\ 1\n \\ 3" nil nil )))
594
- (goto-char (point-min ))
595
- (while (re-search-forward " ^\\ ([ \t ]*\\ ),\\ ([ \t ]*\\ )" nil t )
596
- (replace-match " " nil nil ))
597
- (goto-char (point-min ))
598
- (while (re-search-forward " ,[ \t ]*$" nil t )
599
- (replace-match " " nil nil ))
600
- (goto-char (point-min ))
601
- (haskell-cabal-each-line (haskell-cabal-chomp-line)))
581
+ before: a comma at the start of each line (except the first), e.g.
582
+ Foo
583
+ , Bar
602
584
603
- (defun haskell-cabal-listify ()
604
- " Add commas so that the buffer contains a comma-seperated list"
605
- (cl-case haskell-cabal-list-comma-position
585
+ after: a comma at the end of each line (except the last), e.g.
586
+ Foo,
587
+ Bar
588
+
589
+ single: everything on a single line, but comma-separated, e.g.
590
+ Foo, Bar
591
+
592
+ nil: no commas, e.g.
593
+ Foo Bar
594
+
595
+ If the styles are mixed, the position of the first comma
596
+ determines the style."
597
+ (let (comma-style)
598
+ ; ; split list items on single line
599
+ (goto-char (point-min ))
600
+ (while (re-search-forward
601
+ " \\ ([^ \t ,\n ]\\ )[ \t ]*\\ (,\\ )[ \t ]*\\ ([^ \t ,\n ]\\ )" nil t )
602
+ (when (haskell-cabal-comma-separatorp (match-beginning 2 ))
603
+ (setq comma-style 'single )
604
+ (replace-match " \\ 1\n \\ 3" nil nil )))
605
+ ; ; remove commas before
606
+ (goto-char (point-min ))
607
+ (while (re-search-forward " ^\\ ([ \t ]*\\ ),\\ ([ \t ]*\\ )" nil t )
608
+ (setq comma-style 'before )
609
+ (replace-match " " nil nil ))
610
+ ; ; remove trailing commas
611
+ (goto-char (point-min ))
612
+ (while (re-search-forward " ,[ \t ]*$" nil t )
613
+ (unless (eq comma-style 'before )
614
+ (setq comma-style 'after ))
615
+ (replace-match " " nil nil ))
616
+ (goto-char (point-min ))
617
+
618
+ (haskell-cabal-each-line (haskell-cabal-chomp-line))
619
+ comma-style))
620
+
621
+ (defun haskell-cabal-listify (comma-style )
622
+ " Add commas so that the buffer contains a comma-separated list.
623
+ Respect the COMMA-STYLE, see
624
+ `haskell-cabal-strip-list-and-detect-style' for the possible
625
+ styles."
626
+ (cl-case comma-style
606
627
('before
607
628
(goto-char (point-min ))
608
629
(while (haskell-cabal-ignore-line-p) (forward-line ))
@@ -618,38 +639,25 @@ string, are not comma separators."
618
639
(forward-line -1 )
619
640
(end-of-line )
620
641
(insert " ," )
621
- (beginning-of-line ))))))
622
-
623
- (defun haskell-cabal-comma-separatedp ()
624
- " Return non-nil when the current buffer contains a comma-separated list.
625
- When the buffer contains at least one comma separator (checked
626
- with `haskell-cabal-comma-separatorp' ), the buffer is considered
627
- to be a comma-separated list."
628
- (let ((comma-separatedp nil ))
629
- (goto-char (point-min ))
630
- (while (and (not comma-separatedp)
631
- (search-forward " ," (point-max ) t ))
632
- (when (haskell-cabal-comma-separatorp (match-beginning 0 ))
633
- (setq comma-separatedp t ))
634
- ; ; Make sure we don't find the same comma every time
635
- (forward-char 1 ))
636
- comma-separatedp))
637
-
642
+ (beginning-of-line ))))
643
+ ('single
644
+ (goto-char (point-min ))
645
+ (while (not (eobp ))
646
+ (end-of-line )
647
+ (unless (eobp )
648
+ (insert " , " )
649
+ (delete-char 1 )
650
+ (just-one-space ))))))
638
651
639
652
(defmacro haskell-cabal-with-cs-list (&rest funs )
640
653
" Format the buffer so that each line contains a list element.
641
- Keep the lines comma-separated if and only if they were in the
642
- first place."
643
- (let ((comma-separatedp (make-symbol " comma-separatedp" )))
644
- `(let ((, comma-separatedp
654
+ Respect the comma style."
655
+ (let ((comma-style (make-symbol " comma-style" )))
656
+ `(let ((, comma-style
645
657
(save-excursion
646
- (prog1
647
- (haskell-cabal-comma-separatedp)
648
- (haskell-cabal-strip-list)))))
658
+ (haskell-cabal-strip-list-and-detect-style))))
649
659
(unwind-protect (progn ,@funs )
650
- ; ; Only reinsert commas when it already was comma-separated.
651
- (when , comma-separatedp
652
- (haskell-cabal-listify))))))
660
+ (haskell-cabal-listify , comma-style )))))
653
661
654
662
655
663
(defun haskell-cabal-sort-lines-key-fun ()
0 commit comments