@@ -582,27 +582,56 @@ string, are not comma separators."
582
582
; ; inside a comment
583
583
(nth 4 ss))))))
584
584
585
+ (defun haskell-cabal-strip-list-and-detect-style ()
586
+ " Strip commas from a comma-separated list.
587
+ Detect and return the comma style. The possible options are:
585
588
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)))
589
+ before: a comma at the start of each line (except the first), e.g.
590
+ Foo
591
+ , Bar
592
+
593
+ after: a comma at the end of each line (except the last), e.g.
594
+ Foo,
595
+ Bar
596
+
597
+ single: everything on a single line, but comma-separated, e.g.
598
+ Foo, Bar
599
+
600
+ nil: no commas, e.g.
601
+ Foo Bar
602
+
603
+ If the styles are mixed, the position of the first comma
604
+ determines the style."
605
+ (let (comma-style)
606
+ ; ; split list items on single line
607
+ (goto-char (point-min ))
608
+ (while (re-search-forward
609
+ " \\ ([^ \t ,\n ]\\ )[ \t ]*\\ (,\\ )[ \t ]*\\ ([^ \t ,\n ]\\ )" nil t )
610
+ (when (haskell-cabal-comma-separatorp (match-beginning 2 ))
611
+ (setq comma-style 'single )
612
+ (replace-match " \\ 1\n \\ 3" nil nil )))
613
+ ; ; remove commas before
614
+ (goto-char (point-min ))
615
+ (while (re-search-forward " ^\\ ([ \t ]*\\ ),\\ ([ \t ]*\\ )" nil t )
616
+ (setq comma-style 'before )
617
+ (replace-match " " nil nil ))
618
+ ; ; remove trailing commas
619
+ (goto-char (point-min ))
620
+ (while (re-search-forward " ,[ \t ]*$" nil t )
621
+ (unless (eq comma-style 'before )
622
+ (setq comma-style 'after ))
623
+ (replace-match " " nil nil ))
624
+ (goto-char (point-min ))
602
625
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
626
+ (haskell-cabal-each-line (haskell-cabal-chomp-line))
627
+ comma-style))
628
+
629
+ (defun haskell-cabal-listify (comma-style )
630
+ " Add commas so that the buffer contains a comma-separated list.
631
+ Respect the COMMA-STYLE, see
632
+ `haskell-cabal-strip-list-and-detect-style' for the possible
633
+ styles."
634
+ (cl-case comma-style
606
635
('before
607
636
(goto-char (point-min ))
608
637
(while (haskell-cabal-ignore-line-p) (forward-line ))
@@ -618,38 +647,25 @@ string, are not comma separators."
618
647
(forward-line -1 )
619
648
(end-of-line )
620
649
(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
-
650
+ (beginning-of-line ))))
651
+ ('single
652
+ (goto-char (point-min ))
653
+ (while (not (eobp ))
654
+ (end-of-line )
655
+ (unless (eobp )
656
+ (insert " , " )
657
+ (delete-char 1 )
658
+ (just-one-space ))))))
638
659
639
660
(defmacro haskell-cabal-with-cs-list (&rest funs )
640
661
" 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
662
+ Respect the comma style."
663
+ (let ((comma-style (make-symbol " comma-style" )))
664
+ `(let ((, comma-style
645
665
(save-excursion
646
- (prog1
647
- (haskell-cabal-comma-separatedp)
648
- (haskell-cabal-strip-list)))))
666
+ (haskell-cabal-strip-list-and-detect-style))))
649
667
(unwind-protect (progn ,@funs )
650
- ; ; Only reinsert commas when it already was comma-separated.
651
- (when , comma-separatedp
652
- (haskell-cabal-listify))))))
668
+ (haskell-cabal-listify , comma-style )))))
653
669
654
670
655
671
(defun haskell-cabal-sort-lines-key-fun ()
0 commit comments