forked from lassik/emacs-format-all-the-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformat-all.el
1224 lines (1082 loc) · 44.4 KB
/
format-all.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
;;; format-all.el --- Auto-format C, C++, JS, Python, Ruby and 50 other languages -*- lexical-binding: t -*-
;;
;; Author: Lassi Kortela <[email protected]>
;; URL: https://github.com/lassik/emacs-format-all-the-code
;; Version: 0.3.0
;; Package-Requires: ((emacs "24.3") (inheritenv "0.1") (language-id "0.12"))
;; Keywords: languages util
;; SPDX-License-Identifier: MIT
;;
;; This file is not part of GNU Emacs.
;;
;;; Commentary:
;;
;; Lets you auto-format source code in many languages using the same
;; command for all languages, instead of learning a different Emacs
;; package and formatting command for each language.
;;
;; Just do M-x format-all-buffer and it will try its best to do the
;; right thing. To auto-format code on save, use the minor mode
;; format-all-mode. Please see the documentation for that function
;; for instructions.
;;
;; Supported languages:
;;
;; - Angular/Vue (prettier)
;; - Assembly (asmfmt)
;; - ATS (atsfmt)
;; - Bazel Starlark (buildifier)
;; - BibTeX (Emacs)
;; - C/C++/Objective-C (clang-format, astyle)
;; - C# (clang-format, astyle)
;; - Cabal (cabal-fmt)
;; - Clojure/ClojureScript (node-cljfmt)
;; - CMake (cmake-format)
;; - Crystal (crystal tool format)
;; - CSS/Less/SCSS (prettier)
;; - D (dfmt)
;; - Dart (dartfmt)
;; - Dhall (dhall format)
;; - Dockerfile (dockfmt)
;; - Elixir (mix format)
;; - Elm (elm-format)
;; - Emacs Lisp (Emacs)
;; - Fish Shell (fish_indent)
;; - Fortran 90 (fprettify)
;; - Gleam (gleam format)
;; - GLSL (clang-format)
;; - Go (gofmt, goimports)
;; - GraphQL (prettier)
;; - Haskell (brittany, hindent, ormolu, stylish-haskell)
;; - HTML/XHTML/XML (tidy)
;; - Java (clang-format, astyle)
;; - JavaScript/JSON/JSX (prettier, standard)
;; - Jsonnet (jsonnetfmt)
;; - Kotlin (ktlint)
;; - LaTeX (latexindent)
;; - Ledger (ledger-mode)
;; - Lua (lua-fmt, prettier plugin-lua)
;; - Markdown (prettier)
;; - Nix (nixpkgs-fmt, nixfmt)
;; - OCaml (ocp-indent)
;; - Perl (perltidy)
;; - PHP (prettier plugin-php)
;; - Protocol Buffers (clang-format)
;; - PureScript (purty)
;; - Python (black, yapf)
;; - R (styler)
;; - Reason (bsrefmt)
;; - ReScript (resfmt)
;; - Ruby (rufo)
;; - Rust (rustfmt)
;; - Scala (scalafmt)
;; - Shell script (beautysh, shfmt)
;; - Snakemake (snakefmt)
;; - Solidity (prettier prettier-plugin-solidity)
;; - SQL (pgformatter, sqlformat)
;; - Swift (swiftformat)
;; - Terraform (terraform fmt)
;; - TOML (prettier prettier-plugin-toml)
;; - TypeScript/TSX (prettier)
;; - Verilog (iStyle)
;; - YAML (prettier)
;;
;; You will need to install external programs to do the formatting.
;; If `format-all-buffer` can't find the right program, it will try to
;; tell you how to install it.
;;
;; Many of the external formatters support configuration files in the
;; source code directory to control their formatting. Please see the
;; documentation for each formatter.
;;
;; New external formatters can be added easily if they can read code
;; from standard input and format it to standard output. Feel free to
;; submit a pull request or ask for help in GitHub issues.
;;
;;; Code:
(require 'cl-lib)
(require 'inheritenv)
(require 'language-id)
(defgroup format-all nil
"Lets you auto-format source code."
:group 'format-all)
(defcustom format-all-debug nil
"When non-nil, troubleshooting info is written into the *Messages* buffer."
:type 'boolean
:group 'format-all)
(defcustom format-all-default-formatters
'(("Assembly" asmfmt)
("ATS" atsfmt)
("Bazel" buildifier)
("BibTeX" bibtex-mode)
("C" clang-format)
("C#" clang-format)
("C++" clang-format)
("Cabal Config" cabal-fmt)
("Clojure" cljfmt)
("CMake" cmake-format)
("Crystal" crystal)
("CSS" prettier)
("D" dfmt)
("Dart" dartfmt)
("Dhall" dhall)
("Dockerfile" dockfmt)
("Elixir" mix-format)
("Elm" elm-format)
("Emacs Lisp" emacs-lisp)
("Fish" fish-indent)
("GLSL" clang-format)
("Go" gofmt)
("GraphQL" prettier)
("Haskell" brittany)
("HTML" html-tidy)
("Java" clang-format)
("JavaScript" prettier)
("JSON" prettier)
("Jsonnet" jsonnetfmt)
("JSX" prettier)
("Kotlin" ktlint)
("LaTeX" latexindent)
("Less" prettier)
("Literate Haskell" brittany)
("Lua" lua-fmt)
("Markdown" prettier)
("Nix" nixpkgs-fmt)
("Objective-C" clang-format)
("OCaml" ocp-indent)
("Perl" perltidy)
("PHP" prettier)
("Protocol Buffer" clang-format)
("PureScript" purty)
("Python" black)
("R" styler)
("Reason" bsrefmt)
("ReScript" resfmt)
("Ruby" rufo)
("Rust" rustfmt)
("Scala" scalafmt)
("SCSS" prettier)
("Shell" shfmt)
("Solidity" prettier)
("SQL" sqlformat)
("Swift" swiftformat)
("Terraform" terraform-fmt)
("TOML" prettier)
("TSX" prettier)
("TypeScript" prettier)
("Verilog" istyle-verilog)
("Vue" prettier)
("XML" html-tidy)
("YAML" prettier)
("_Angular" prettier)
("_Flow" prettier)
("_Fortran 90" fprettify)
("_Gleam" gleam)
("_Ledger" ledger-mode)
("_Snakemake" snakefmt))
"Default formatter to use for each language."
:type '(repeat (list string symbol))
:group 'format-all)
(defcustom format-all-always-show-errors nil
"When non-nil, warnings are shown even when formatting is successful."
:type 'boolean
:group 'format-all)
(defvar format-all-after-format-functions nil
"Hook run after each time `format-all-buffer' has formatted a buffer.
The value is a list of hook functions. Use `add-hook' to add a
function. The function is called with two arguments: (FORMATTER
STATUS). FORMATTER is a symbol naming the formatter, as given to
`define-format-all-formatter'. STATUS is one of the following
keywords:
* :reformatted -- The formatter made changes to the buffer.
* :already-formatted -- The buffer was already formatted
correctly so the formatter didn't make any changes to it.
* :error -- The formatter encountered an error (usually a syntax
error). The buffer contents are the same as before formatting.
The current buffer is the buffer that was just formatted. Point
is not guaranteed to be in any particular place, so `goto-char'
before editing the buffer. Narrowing may be in effect unless
STATUS is :reformatted.")
(defvar format-all--user-args nil
"Internal variable to temporarily store arguments for formatters.")
(defvar-local format-all-formatters nil
"Rules to select which formatter format-all uses.
The value is an association list.
The first item of each association is the name of a programming
language. (GitHub Linguist names are used.)
The remaining items are one or more formatters to use for that
language. Each formatter is either:
* a symbol (e.g. black, clang-format, rufo)
* a list whose first item is that symbol, and any remaining items
are extra command line arguments to pass to the formatter
If more than one formatter is given for the same language, all of
them are run as a chain, with the code from each formatter passed
to the next. The final code is from the last formatter. In case
any formatter in the chain is missing or fails to format the
code, the entire chain fails and the old code before formatting
is preserved.
You'll probably want to set this in a \".dir-locals.el\" file or
in a hook function. Any number of buffers can share the same
association list. Using \".dir-locals.el\" is convenient since
the rules for an entire source tree can be given in one file.")
(defun format-all--proper-list-p (object)
"Return t if OBJECT is a proper list, nil otherwise."
;; If we could depend on Emacs 27.1 this function would be built in.
(and (listp object) (not (null (cl-list-length object)))))
(defun format-all--normalize-formatter (formatter)
"Internal function to convert FORMATTER spec into normal form."
(let ((formatter (if (listp formatter) formatter (list formatter))))
(when (cdr (last formatter))
(error "Formatter is not a proper list: %S" formatter))
(when (null formatter)
(error "Formatter name missing"))
(unless (symbolp (car formatter))
(error "Formatter name is not a symbol: %S" (car formatter)))
(unless (cl-every #'stringp (cdr formatter))
(error "Formatter command line arguments are not all strings: %S"
formatter))
formatter))
(defun format-all--normalize-chain (chain)
"Internal function to convert CHAIN spec into normal form."
(when (or (not (listp chain)) (cdr (last chain)))
(error "Formatter chain is not a proper list: %S" chain))
(mapcar #'format-all--normalize-formatter chain))
(defun format-all-valid-formatters-p (formatters)
"Return t if FORMATTERS is a valid value for `format-all-formatters'."
(and (format-all--proper-list-p formatters)
(cl-every
(lambda (chain)
(and (not (null chain))
(format-all--proper-list-p chain)
(stringp (car chain))
(cl-every
(lambda (formatter)
(and (not (null formatter))
(or (symbolp formatter)
(and (format-all--proper-list-p formatter)
(and (symbolp (car formatter))
(not (null (car formatter))))
(cl-every #'stringp (cdr formatter))))))
(cdr chain))))
formatters)))
(put 'format-all-formatters 'safe-local-variable
'format-all-valid-formatters-p)
(eval-and-compile
(defconst format-all--system-type
(cl-case system-type
(windows-nt 'windows)
(cygwin 'windows)
(darwin 'macos)
(gnu/linux 'linux)
(berkeley-unix
(save-match-data
(let ((case-fold-search t))
(cond ((string-match "freebsd" system-configuration) 'freebsd)
((string-match "openbsd" system-configuration) 'openbsd)
((string-match "netbsd" system-configuration) 'netbsd))))))
"Current operating system according to the format-all package."))
(eval-and-compile
(defun format-all--resolve-system (choices)
"Get first choice matching `format-all--system-type' from CHOICES."
(cl-dolist (choice choices)
(cond ((atom choice)
(cl-return choice))
((eql format-all--system-type (car choice))
(cl-return (cadr choice)))))))
(defun format-all--fix-trailing-whitespace ()
"Fix trailing whitespace since some formatters don't do that."
(save-match-data
(goto-char (point-min))
(while (re-search-forward "[ \t]+$" nil t)
(replace-match ""))
(goto-char (point-max))
(delete-region
(if (re-search-backward "[^ \t\n]" nil t) (match-end 0) (point-min))
(point-max))
(unless (= (point-min) (point-max))
(goto-char (point-max))
(insert "\n"))))
(defun format-all--remove-ansi-color (string)
"Internal helper function to remove terminal color codes from STRING."
(save-match-data (replace-regexp-in-string "\x1b\\[[0-9]+m" "" string t)))
(defun format-all--flatten-once (list)
"Internal helper function to remove nested lists in LIST."
(cl-mapcan (lambda (x) (if (listp x) x (list x)))
list))
(defun format-all--buffer-extension-p (&rest extensions)
"Internal helper function to test file name EXTENSIONS."
(and (buffer-file-name)
(save-match-data
(let ((case-fold-search t))
(cl-some (lambda (ext)
(string-match (concat "\\." (regexp-quote ext) "\\'")
(buffer-file-name)))
extensions)))))
(defun format-all--buffer-thunk (thunk)
"Internal helper function to implement formatters.
THUNK is a function that implements a particular formatter. It
takes INPUT (the unformatted source code as a string). THUNK is
invoked such that the current buffer is an empty temp buffer. It
should call the formatter on INPUT and write the formatted source
code output to the temp buffer. It should return (ERRORP
ERROR-OUTPUT). ERRORP is a boolean indicating whether the formatter
caused an error and hence the contents of the temp buffer should
be discarded. ERROR-OUTPUT is a string containing all error/warning
output from the formatter.
Note that in some cases we can use the output of the formatter
even if it produced warnings. Not all warnings are errors."
(save-excursion
(save-restriction
(widen)
(let ((inbuf (current-buffer))
(input (buffer-string)))
(inheritenv
(with-temp-buffer
(cl-destructuring-bind (errorp error-output) (funcall thunk input)
(let* ((no-chg (or errorp
(= 0 (let ((case-fold-search nil))
(compare-buffer-substrings
inbuf nil nil nil nil nil)))))
(output (cond (errorp nil)
(no-chg t)
(t (buffer-string)))))
(list output error-output)))))))))
(defun format-all--buffer-native (mode &rest funcs)
"Internal helper function to implement formatters.
In a new temp buffer, switches to MODE then calls FUNCS in order
to format the code. MODE and FUNCS should be symbols instead of
functions to avoid warnings from the Emacs byte compiler."
(format-all--buffer-thunk
(lambda (input)
(funcall mode)
(insert input)
(mapc #'funcall funcs)
(format-all--fix-trailing-whitespace)
(list nil ""))))
(defun format-all--locate-file (filename)
"Internal helper to locate dominating copy of FILENAME for current buffer."
(let* ((dir (and (buffer-file-name)
(locate-dominating-file (buffer-file-name) filename))))
(when dir (expand-file-name (concat dir filename)))))
(defun format-all--locate-default-directory (root-files)
"Internal helper function to find working directory for formatter.
ROOT-FILES is a list of strings which are the filenames to look
for using `locate-dominating-file'. Details in documentation for
`format-all--buffer-hard'."
(let ((found-dirs
(when (and root-files (buffer-file-name))
(mapcan (lambda (root-file)
(let ((found-file (locate-dominating-file
(buffer-file-name) root-file)))
(when found-file
(list (file-name-directory found-file)))))
root-files))))
(or (car (sort found-dirs (lambda (a b) (> (length a) (length b)))))
(and (buffer-file-name) (file-name-directory (buffer-file-name)))
default-directory)))
(defun format-all--buffer-hard
(ok-statuses error-regexp root-files executable &rest args)
"Internal helper function to implement formatters.
Runs the external program EXECUTABLE. The program shall read
unformatted code from stdin, write its formatted equivalent to
stdout, and write errors/warnings to stderr.
The program should exit with status zero for the formatting to be
considered successful. If a list of OK-STATUSES is given, all of
those are actually considered successful. But if ERROR-REGEXP is
given, and the program's stderr contains that regexp, then the
formatting is considered failed even if the exit status is in
OK-STATUSES. OK-STATUSES and ERROR-REGEXP are hacks to work
around formatter programs that don't make sensible use of their
exit status.
If ARGS are given, those are arguments to EXECUTABLE. They should
not be shell-quoted.
If ROOT-FILES are given, the working directory of the formatter
will be the deepest directory (starting from the file being
formatted) containing one of these files. If ROOT-FILES is nil,
or none of ROOT-FILES are found in any parent directories, the
working directory will be the one where the formatted file is.
ROOT-FILES is ignored for buffers that are not visiting a file."
(let ((ok-statuses (or ok-statuses '(0)))
(args (append format-all--user-args (format-all--flatten-once args)))
(default-directory (format-all--locate-default-directory root-files)))
(when format-all-debug
(message "Format-All: Running: %s"
(mapconcat #'shell-quote-argument (cons executable args) " "))
(message "Format-All: Directory: %s" default-directory))
(format-all--buffer-thunk
(lambda (input)
(let* ((errfile (make-temp-file "format-all-"))
(status (apply #'call-process-region input nil
executable nil (list t errfile)
nil args))
(error-output (with-temp-buffer
(insert-file-contents errfile)
(delete-file errfile)
(buffer-string)))
(errorp (or (not (member status ok-statuses))
(and error-regexp
(save-match-data
(string-match error-regexp error-output))))))
(list errorp error-output))))))
(defun format-all--buffer-easy (executable &rest args)
"Internal helper function to implement formatters.
Runs the external program EXECUTABLE. The program shall read
unformatted code from stdin, write its formatted equivalent to
stdout, write errors/warnings to stderr, and exit zero/non-zero
on success/failure.
If ARGS are given, those are arguments to EXECUTABLE. They don't
need to be shell-quoted."
(apply 'format-all--buffer-hard nil nil nil executable args))
(defvar format-all--executable-table (make-hash-table)
"Internal table of formatter executable names for format-all.")
(defvar format-all--install-table (make-hash-table)
"Internal table of formatter install commands for format-all.")
(defvar format-all--language-table (make-hash-table :test 'equal)
"Internal table of major mode formatter lists for format-all.")
(defvar format-all--format-table (make-hash-table)
"Internal table of formatter formatting functions for format-all.")
(defun format-all--pushhash (key value table)
"Push VALUE onto the list under KEY in hash table TABLE."
(puthash key (cons value (remove value (gethash key table))) table))
(defmacro define-format-all-formatter (formatter &rest body)
"Define a new source code formatter for use with format-all.
FORMATTER is a symbol naming the formatter. The name of the
command used to run the formatter is usually a good choice.
Consult the existing formatters for examples of BODY."
(let (executable install languages format)
(cl-assert
(equal (mapcar 'car body) '(:executable :install :languages :format)))
(cl-dolist (part body)
(cl-ecase (car part)
(:executable
(setq executable
(unless (null (cdr part))
(or (format-all--resolve-system (cdr part))
(error "Executable not specified for %S system %S"
formatter format-all--system-type)))))
(:install
(setq install (format-all--resolve-system (cdr part))))
(:languages
(setq languages
(mapcar (lambda (language)
`(format-all--pushhash
',language ',formatter format-all--language-table))
(cdr part))))
(:format
(setq format `(lambda (executable language)
(ignore language ,@(unless executable '(executable)))
,(cadr part))))))
`(progn (puthash ',formatter ,executable format-all--executable-table)
(puthash ',formatter ,install format-all--install-table)
,@languages
(puthash ',formatter ,format format-all--format-table)
',formatter)))
(define-format-all-formatter asmfmt
(:executable "asmfmt")
(:install)
(:languages "Assembly")
(:format (format-all--buffer-easy executable)))
(define-format-all-formatter astyle
(:executable "astyle")
(:install (macos "brew install astyle"))
(:languages "C" "C++" "C#" "Java")
(:format (format-all--buffer-easy
executable
(let ((astylerc (format-all--locate-file ".astylerc")))
(when astylerc (concat "--options=" astylerc))))))
(define-format-all-formatter atsfmt
(:executable "atsfmt")
(:install "cabal new-install ats-format --happy-options='-gcsa' -O2")
(:languages "ATS")
(:format (format-all--buffer-easy executable)))
(define-format-all-formatter beautysh
(:executable "beautysh")
(:install "pip install beautysh")
(:languages "Shell")
(:format (format-all--buffer-easy executable "-")))
(define-format-all-formatter bibtex-mode
(:executable)
(:install)
(:languages "BibTeX")
(:format (format-all--buffer-native
'bibtex-mode 'bibtex-reformat 'bibtex-sort-buffer)))
(define-format-all-formatter black
(:executable "black")
(:install "pip install black")
(:languages "Python")
(:format (format-all--buffer-easy
executable "-q"
(when (format-all--buffer-extension-p "pyi") "--pyi")
"-")))
(define-format-all-formatter brittany
(:executable "brittany")
(:install "stack install brittany")
(:languages "Haskell" "Literate Haskell")
(:format (format-all--buffer-easy executable)))
(define-format-all-formatter bsrefmt
(:executable "bsrefmt")
(:install "npm install --global bs-platform")
(:languages "Reason")
(:format (format-all--buffer-easy executable)))
(define-format-all-formatter buildifier
(:executable "buildifier")
(:install
(macos "brew install buildifier")
"go get github.com/bazelbuild/buildtools/buildifier")
(:languages "Bazel")
(:format (format-all--buffer-easy executable)))
(define-format-all-formatter cabal-fmt
(:executable "cabal-fmt")
(:install "cabal install cabal-fmt")
(:languages "Cabal Config")
(:format (format-all--buffer-easy executable)))
(define-format-all-formatter clang-format
(:executable "clang-format")
(:install
(macos "brew install clang-format")
(windows "scoop install llvm"))
(:languages "C" "C#" "C++" "GLSL" "Java" "Objective-C" "Protocol Buffer")
(:format
(format-all--buffer-easy
executable
(concat "-assume-filename="
(or (buffer-file-name)
(cdr (assoc language
'(("C" . ".c")
("C#" . ".cs")
("C++" . ".cpp")
("GLSL" . ".glsl")
("Java" . ".java")
("Objective-C" . ".m")
("Protocol Buffer" . ".proto")))))))))
(define-format-all-formatter cljfmt
(:executable "cljfmt")
(:install "npm install --global node-cljfmt")
(:languages "Clojure")
(:format (format-all--buffer-easy executable)))
(define-format-all-formatter cmake-format
(:executable "cmake-format")
(:install "pip install cmake-format")
(:languages "CMake")
(:format (format-all--buffer-easy executable "-")))
(define-format-all-formatter crystal
(:executable "crystal")
(:install (macos "brew install crystal"))
(:languages "Crystal")
(:format (format-all--buffer-easy executable "tool" "format" "-")))
(define-format-all-formatter dartfmt
(:executable "dartfmt")
(:install (macos "brew tap dart-lang/dart && brew install dart"))
(:languages "Dart")
(:format
(format-all--buffer-easy
executable
(when (buffer-file-name)
(list "--stdin-name" (buffer-file-name))))))
(define-format-all-formatter dfmt
(:executable "dfmt")
(:install (macos "brew install dfmt"))
(:languages "D")
(:format
(format-all--buffer-hard nil (regexp-quote "[error]") nil executable)))
(define-format-all-formatter dhall
(:executable "dhall")
(:install (macos "brew install dhall"))
(:languages "Dhall")
(:format (format-all--buffer-easy executable "format")))
(define-format-all-formatter dockfmt
(:executable "dockfmt")
(:install "go get github.com/jessfraz/dockfmt")
(:languages "Dockerfile")
(:format (format-all--buffer-easy executable "fmt")))
(define-format-all-formatter elm-format
(:executable "elm-format")
(:install (macos "brew install elm"))
(:languages "Elm")
(:format
(cl-destructuring-bind (output error-output)
(format-all--buffer-hard nil nil '("elm.json" "elm-package.json")
executable "--yes" "--stdin")
(let ((error-output (format-all--remove-ansi-color error-output)))
(list output error-output)))))
(define-format-all-formatter emacs-lisp
(:executable)
(:install)
(:languages "Emacs Lisp")
(:format
(format-all--buffer-native
'emacs-lisp-mode
(lambda () (indent-region (point-min) (point-max))))))
(define-format-all-formatter fish-indent
(:executable "fish_indent")
(:install (macos "brew install fish OR port install fish"))
(:languages "Fish")
(:format (format-all--buffer-easy executable)))
(define-format-all-formatter fprettify
(:executable "fprettify")
(:install "pip install fprettify")
(:languages "_Fortran 90")
(:format (format-all--buffer-easy executable "--silent")))
(define-format-all-formatter gleam
(:executable "gleam")
(:install (macos "brew install gleam"))
(:languages "_Gleam")
(:format (format-all--buffer-easy executable "format" "--stdin")))
(define-format-all-formatter gofmt
(:executable "gofmt")
(:install
(macos "brew install go")
(windows "scoop install go"))
(:languages "Go")
(:format (format-all--buffer-easy executable)))
(define-format-all-formatter goimports
(:executable "goimports")
(:install "go get golang.org/x/tools/cmd/goimports")
(:languages "Go")
(:format (format-all--buffer-easy executable)))
(define-format-all-formatter hindent
(:executable "hindent")
(:install "stack install hindent")
(:languages "Haskell" "Literate Haskell")
(:format (format-all--buffer-easy executable)))
(define-format-all-formatter html-tidy
(:executable "tidy")
(:install
(macos "brew install tidy-html5")
(windows "scoop install tidy"))
(:languages "HTML" "XML")
(:format
(format-all--buffer-hard
'(0 1) nil nil
executable
"-q"
"--tidy-mark" "no"
"-indent"
(when (equal language "XML") "-xml"))))
(define-format-all-formatter istyle-verilog
(:executable "iStyle")
(:install)
(:languages "Verilog")
(:format (format-all--buffer-easy executable)))
(define-format-all-formatter jsonnetfmt
(:executable "jsonnetfmt")
(:install (macos "brew install jsonnet"))
(:languages "Jsonnet")
(:format (format-all--buffer-easy executable "-")))
(define-format-all-formatter ktlint
(:executable "ktlint")
(:install (macos "brew install ktlint"))
(:languages "Kotlin")
(:format (format-all--buffer-easy executable "--format" "--stdin")))
(define-format-all-formatter latexindent
(:executable "latexindent")
(:install)
(:languages "LaTeX")
(:format (format-all--buffer-easy executable)))
(define-format-all-formatter ledger-mode
(:executable)
(:install)
(:languages "_Ledger")
(:format
(format-all--buffer-native 'ledger-mode 'ledger-mode-clean-buffer)))
(define-format-all-formatter lua-fmt
(:executable "luafmt")
(:install "npm install --global lua-fmt")
(:languages "Lua")
(:format (format-all--buffer-easy executable "--stdin")))
(define-format-all-formatter mix-format
(:executable "mix")
(:install (macos "brew install elixir"))
(:languages "Elixir")
(:format
(format-all--buffer-hard
nil nil '("mix.exs")
executable
"format"
(let ((config-file (format-all--locate-file ".formatter.exs")))
(when config-file (list "--dot-formatter" config-file)))
"-")))
(define-format-all-formatter nixfmt
(:executable "nixfmt")
(:install "nix-env -f https://github.com/serokell/nixfmt/archive/master.tar.gz -i")
(:languages "Nix")
(:format (format-all--buffer-easy executable)))
(define-format-all-formatter nixpkgs-fmt
(:executable "nixpkgs-fmt")
(:install "nix-env -f https://github.com/nix-community/nixpkgs-fmt/archive/master.tar.gz -i")
(:languages "Nix")
(:format (format-all--buffer-easy executable)))
(define-format-all-formatter ocp-indent
(:executable "ocp-indent")
(:install "opam install ocp-indent")
(:languages "OCaml")
(:format (format-all--buffer-easy executable)))
(define-format-all-formatter ormolu
(:executable "ormolu")
(:install "stack install ormolu")
(:languages "Haskell" "Literate Haskell")
(:format (format-all--buffer-easy executable)))
(define-format-all-formatter perltidy
(:executable "perltidy")
(:install "cpan install Perl::Tidy")
(:languages "Perl")
(:format (format-all--buffer-easy executable)))
(define-format-all-formatter pgformatter
(:executable "pg_format")
(:install)
(:languages "SQL")
(:format (format-all--buffer-easy executable)))
(define-format-all-formatter prettier
(:executable "prettier")
(:install "npm install --global prettier @prettier/plugin-lua @prettier/plugin-php prettier-plugin-solidity prettier-plugin-toml")
(:languages
"CSS" "GraphQL" "HTML" "JavaScript" "JSON" "JSX" "Less" "Lua" "Markdown"
"PHP" "SCSS" "Solidity" "TOML" "TSX" "TypeScript" "Vue" "YAML"
"_Angular" "_Flow")
(:format
(format-all--buffer-easy
executable
"--parser" (let ((pair (assoc language
'(("_Angular" . "angular")
("_Flow" . "flow")
("JavaScript" . "babel")
("JSX" . "babel")
("Solidity" . "solidity-parse")
("TSX" . "typescript")))))
(if pair (cdr pair) (downcase language)))
(when (buffer-file-name) (list "--stdin-filepath" (buffer-file-name)))
(let ((ignore-file (format-all--locate-file ".prettierignore")))
(when ignore-file (list "--ignore-path" ignore-file))))))
(define-format-all-formatter purty
(:executable "purty")
(:install "npm install --global purty")
(:languages "PureScript")
(:format (format-all--buffer-easy executable "-")))
(define-format-all-formatter resfmt
(:executable "resfmt")
(:install "pip install resfmt")
(:languages "ReScript")
(:format (format-all--buffer-easy executable)))
(define-format-all-formatter rufo
(:executable "rufo")
(:install "gem install rufo")
(:languages "Ruby")
(:format
(format-all--buffer-easy
executable
"--simple-exit"
(when (buffer-file-name)
(list "--filename" (buffer-file-name))))))
(define-format-all-formatter rustfmt
(:executable "rustfmt")
(:install "rustup component add rustfmt")
(:languages "Rust")
(:format (format-all--buffer-easy executable)))
(define-format-all-formatter scalafmt
(:executable "scalafmt")
(:install "coursier bootstrap org.scalameta:scalafmt-cli_2.12:2.4.0-RC1 -r sonatype:snapshots -o /usr/local/bin/scalafmt --standalone --main org.scalafmt.cli.Cli")
(:languages "Scala")
(:format
(format-all--buffer-easy
executable "--stdin" "--non-interactive" "--quiet")))
(define-format-all-formatter shfmt
(:executable "shfmt")
(:install
(macos "brew install shfmt")
(windows "scoop install shfmt"))
(:languages "Shell")
(:format
(format-all--buffer-easy
executable
(if (buffer-file-name)
(list "-filename" (buffer-file-name))
(list "-ln" (cl-case (and (eql major-mode 'sh-mode)
(boundp 'sh-shell)
(symbol-value 'sh-shell))
(bash "bash")
(mksh "mksh")
(t "posix")))))))
(define-format-all-formatter snakefmt
(:executable "snakefmt")
(:install)
(:languages "_Snakemake")
(:format (format-all--buffer-easy executable "-")))
(define-format-all-formatter sqlformat
(:executable "sqlformat")
(:install "pip install sqlparse")
(:languages "SQL")
(:format
(let* ((ic (car default-process-coding-system))
(oc (cdr default-process-coding-system))
(ienc (symbol-name (or (coding-system-get ic :mime-charset)
'utf-8)))
(oenc (symbol-name (or (coding-system-get oc :mime-charset)
'utf-8)))
(process-environment (cons (concat "PYTHONIOENCODING=" oenc)
process-environment)))
(format-all--buffer-easy executable "--encoding" ienc "-"))))
(define-format-all-formatter standard
(:executable "standard")
(:install "npm install --global standard")
(:languages "JavaScript" "JSX")
(:format
;; `standard --stdin` properly uses zero vs non-zero exit codes to
;; indicate success vs error. However, it checks for quite a broad
;; range of errors, all the way up to undeclared identifiers and
;; such. To catch only syntax errors, we need to look specifically
;; for the text "Parsing error:".
(format-all--buffer-hard
'(0 1) ".*?:.*?:[0-9]+:[0-9]+: Parsing error:" nil
executable "--fix" "--stdin")))
(define-format-all-formatter styler
(:executable "Rscript")
(:install "Rscript -e 'install.packages(\"styler\")'")
(:languages "R")
(:format
(format-all--buffer-easy
executable "--vanilla"
"-e" (concat
"options(styler.colored_print.vertical=FALSE);"
" con <- file(\"stdin\");"
" out <- styler::style_text(readLines(con));"
" close(con);"
" out"))))
(define-format-all-formatter stylish-haskell
(:executable "stylish-haskell")
(:install "stack install stylish-haskell")
(:languages "Haskell")
(:format (format-all--buffer-easy executable)))
(define-format-all-formatter swiftformat
(:executable "swiftformat")
(:install (macos "brew install swiftformat"))
(:languages "Swift")
(:format
(format-all--buffer-easy
executable "--quiet"
(let ((config (format-all--locate-file ".swiftformat")))
(when config (list "--config" config))))))
(define-format-all-formatter terraform-fmt
(:executable "terraform")
(:install (macos "brew install terraform"))
(:languages "Terraform")
(:format (format-all--buffer-easy executable "fmt" "-no-color" "-")))
(define-format-all-formatter yapf
(:executable "yapf")
(:install "pip install yapf")
(:languages "Python")
(:format (format-all--buffer-easy executable)))
(defun format-all--language-id-buffer ()
"Return the language used in the current buffer, or NIL.
Prefer getting the ID from the language-id library. Some
languages do not yet have official GitHub Linguist identifiers,
yet format-all needs to know about them anyway. That's why we
have this custom language-id function in format-all. The
unofficial languages IDs are prefixed with \"_\"."
(or (and (or (equal major-mode 'angular-html-mode)
(and (equal major-mode 'web-mode)
(equal (symbol-value 'web-mode-content-type) "html")
(equal (symbol-value 'web-mode-engine) "angular")))
"_Angular")
(and (member major-mode '(js-mode js2-mode js3-mode))
(boundp 'flow-minor-mode)
(not (null (symbol-value 'flow-minor-mode)))
"_Flow")
(and (equal major-mode 'f90-mode) "_Fortran 90")
(and (equal major-mode 'gleam-mode) "_Gleam")
(and (equal major-mode 'ledger-mode) "_Ledger")
(and (equal major-mode 'snakemake-mode) "_Snakemake")