-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
csv-mode.el
2001 lines (1815 loc) · 81.9 KB
/
csv-mode.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
;;; csv-mode.el --- Major mode for editing comma/char separated values -*- lexical-binding: t -*-
;; Copyright (C) 2003-2024 Free Software Foundation, Inc
;; Author: "Francis J. Wright" <F.J.Wright@qmul.ac.uk>
;; Maintainer: emacs-devel@gnu.org
;; Version: 1.27
;; Package-Requires: ((emacs "27.1") (cl-lib "0.5"))
;; Keywords: convenience
;; This package 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 package 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. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; This package implements CSV mode, a major mode for editing records
;; in a generalized CSV (character-separated values) format. It binds
;; files with prefix ".csv" to `csv-mode' (and ".tsv" to `tsv-mode') in
;; `auto-mode-alist'.
;; In CSV mode, the following commands are available:
;; - C-c C-s (`csv-sort-fields') and C-c C-n (`csv-sort-numeric-fields')
;; respectively sort lexicographically and numerically on a
;; specified field or column.
;; - C-c C-r (`csv-reverse-region') reverses the order. (These
;; commands are based closely on, and use, code in `sort.el'.)
;; - C-c C-k (`csv-kill-fields') and C-c C-y (`csv-yank-fields') kill
;; and yank fields or columns, although they do not use the normal
;; kill ring. C-c C-k can kill more than one field at once, but
;; multiple killed fields can be yanked only as a fixed group
;; equivalent to a single field.
;; - `csv-align-mode' keeps fields visually aligned, on-the-fly.
;; It truncates fields to a maximum width that can be changed per-column
;; with `csv-align-set-column-width'.
;; Alternatively, C-c C-a (`csv-align-fields') aligns fields into columns
;; and C-c C-u (`csv-unalign-fields') undoes such alignment;
;; separators can be hidden within aligned records (controlled by
;; `csv-invisibility-default' and `csv-toggle-invisibility').
;; - C-c C-t (`csv-transpose') interchanges rows and columns. For
;; details, see the documentation for the individual commands.
;; - `csv-set-separator' sets the CSV separator of the current buffer,
;; while `csv-guess-set-separator' guesses and sets the separator
;; based on the current buffer's contents.
;; `csv-guess-set-separator' can be useful to add to the mode hook
;; to have CSV mode guess and set the separator automatically when
;; visiting a buffer:
;;
;; (add-hook 'csv-mode-hook 'csv-guess-set-separator)
;; CSV mode can recognize fields separated by any of several single
;; characters, specified by the value of the customizable user option
;; `csv-separators'. CSV data fields can be delimited by quote
;; characters (and must if they contain separator characters). This
;; implementation supports quoted fields, where the quote characters
;; allowed are specified by the value of the customizable user option
;; `csv-field-quotes'. By default, the both commas and tabs are considered
;; as separators and the only field quote is a double quote.
;; These user options can be changed ONLY by customizing them, e.g. via M-x
;; customize-variable.
;; CSV mode commands ignore blank lines and comment lines beginning
;; with the value of the buffer local variable `csv-comment-start',
;; which by default is #. The user interface is similar to that of
;; the standard commands `sort-fields' and `sort-numeric-fields', but
;; see the major mode documentation below.
;; The global minor mode `csv-field-index-mode' provides display of
;; the current field index in the mode line, cf. `line-number-mode'
;; and `column-number-mode'. It is on by default.
;;;; See also:
;; the standard GNU Emacs 21 packages align.el, which will align
;; columns within a region, and delim-col.el, which helps to prettify
;; columns in a text region or rectangle;
;; csv.el by Ulf Jasper <ulf.jasper at web.de>, which provides
;; functions for reading/parsing comma-separated value files and is
;; available at http://de.geocities.com/ulf_jasper/emacs.html (and in
;; the gnu.emacs.sources archives).
;;; Installation:
;; Put this file somewhere that Emacs can find it (i.e. in one of the
;; directories in your `load-path' such as `site-lisp'), optionally
;; byte-compile it (recommended), and put this in your .emacs file:
;;
;; (add-to-list 'auto-mode-alist '("\\.[Cc][Ss][Vv]\\'" . csv-mode))
;; (autoload 'csv-mode "csv-mode"
;; "Major mode for editing comma-separated value files." t)
;;; News:
;; Since 1.27:
;; - `csv-end-of-field' no longer errors out in the presence of
;; unclosed quotes.
;; Since 1.26:
;; - `csv-guess-separator' will no longer guess the comment-start
;; character as a potential separator character.
;; Since 1.25:
;; - The ASCII control character 31 Unit Separator can now be
;; recognized as a CSV separator by `csv-guess-separator'.
;; Since 1.24:
;; - New function `csv--unquote-value'.
;; - New function `csv-parse-current-row'.
;; Since 1.21:
;; - New command `csv-insert-column'.
;; - New config var `csv-align-min-width' for `csv-align-mode'.
;; - New option `csv-confirm-region'.
;; Since 1.20:
;; - New command `csv-guess-set-separator' that automatically guesses
;; and sets the CSV separator of the current buffer.
;; - New command `csv-set-separator' for setting the CSV separator
;; manually.
;; Since 1.9:
;; - `csv-align-mode' auto-aligns columns dynamically (on screen).
;; Before that:
;; Begun on 15 November 2003 to provide lexicographic sorting of
;; simple CSV data by field and released as csv.el. Facilities to
;; kill multiple fields and customize separator added on 9 April 2004.
;; Converted to a major mode and renamed csv-mode.el on 10 April 2004,
;; partly at the suggestion of Stefan Monnier <monnier at
;; IRO.UMontreal.CA> to avoid conflict with csv.el by Ulf Jasper.
;; Field alignment, comment support and CSV mode customization group
;; added on 1 May 2004. Support for index ranges added on 6 June
;; 2004. Multiple field separators added on 12 June 2004.
;; Transposition added on 22 June 2004. Separator invisibility added
;; on 23 June 2004.
;;; To do (maybe):
;; Make separators and quotes buffer-local and locally settable.
;; Support (La)TeX tables: set separator and comment; support record
;; end string.
;; Convert comma-separated to space- or tab-separated.
;;; Code:
(eval-when-compile
(require 'cl-lib)
(require 'subr-x))
(defgroup CSV nil
"Major mode for editing files of comma-separated value type."
:group 'convenience)
(defvar csv-separator-chars nil
"Field separators as a list of character.
Set by customizing `csv-separators' -- do not set directly!")
(defvar csv-separator-regexp nil
"Regexp to match a field separator.
Set by customizing `csv-separators' -- do not set directly!")
(defvar csv--skip-chars nil
"Char set used by `skip-chars-forward' etc. to skip fields.
Set by customizing `csv-separators' -- do not set directly!")
(defvar csv-font-lock-keywords nil
"Font lock keywords to highlight the field separators in CSV mode.
Set by customizing `csv-separators' -- do not set directly!")
(defcustom csv-separators '("," "\t")
"Field separators: a list of *single-character* strings.
For example: (\",\"), the default, or (\",\" \";\" \":\").
Neighbouring fields may be separated by any one of these characters.
The first is used when inserting a field separator into the buffer.
All must be different from the field quote characters, `csv-field-quotes'.
Changing this variable with `setq' won't affect the current Emacs
session. Use `customize-set-variable' instead if that is required."
;; Suggested by Eckhard Neber <neber@mwt.e-technik.uni-ulm.de>
:type '(repeat string)
;; FIXME: Character would be better, but in Emacs 21.3 does not display
;; correctly in a customization buffer.
:set (lambda (variable value)
(mapc (lambda (x)
(if (/= (length x) 1)
(error "Non-single-char string %S" x))
(if (and (boundp 'csv-field-quotes)
(member x csv-field-quotes))
(error "%S is already a quote" x)))
value)
(custom-set-default variable value)
(setq csv-separator-chars (mapcar #'string-to-char value))
(setq csv--skip-chars
(apply #'concat "^\n"
(mapcar (lambda (s) (concat "\\" s)) value)))
(setq csv-separator-regexp (regexp-opt value))
(setq csv-font-lock-keywords
;; NB: csv-separator-face variable evaluates to itself.
`((,csv-separator-regexp (0 'csv-separator-face))))))
(defcustom csv-field-quotes '("\"")
"Field quotes: a list of *single-character* strings.
For example: (\"\\\"\"), the default, or (\"\\\"\" \"\\='\" \"\\=`\").
A field can be delimited by a pair of any of these characters.
All must be different from the field separators, `csv-separators'."
:type '(repeat string)
;; Character would be better, but in Emacs 21 does not display
;; correctly in a customization buffer.
:set (lambda (variable value)
(mapc (lambda (x)
(if (/= (length x) 1)
(error "Non-single-char string %S" x))
(if (member x csv-separators)
(error "%S is already a separator" x)))
value)
(when (boundp 'csv-mode-syntax-table)
;; FIRST remove old quote syntax:
(with-syntax-table text-mode-syntax-table
(mapc (lambda (x)
(modify-syntax-entry
(string-to-char x)
(string (char-syntax (string-to-char x)))
;; symbol-value to avoid compiler warning:
(symbol-value 'csv-mode-syntax-table)))
csv-field-quotes))
;; THEN set new quote syntax:
(csv-set-quote-syntax value))
;; BEFORE setting new value of `csv-field-quotes':
(custom-set-default variable value)))
(defun csv-set-quote-syntax (field-quotes)
"Set syntax for field quote characters FIELD-QUOTES to be \"string\".
FIELD-QUOTES should be a list of single-character strings."
(mapc (lambda (x)
(modify-syntax-entry
(string-to-char x) "\""
;; symbol-value to avoid compiler warning:
(symbol-value 'csv-mode-syntax-table)))
field-quotes))
(defvar-local csv-comment-start nil
"String that starts a comment line, or nil if no comment syntax.
Such comment lines are ignored by CSV mode commands.
This variable is buffer local; its default value is that of
`csv-comment-start-default'. It is set by the function
`csv-set-comment-start' -- do not set it directly!")
(defcustom csv-comment-start-default "#"
"String that starts a comment line, or nil if no comment syntax.
Such comment lines are ignored by CSV mode commands.
Default value of buffer-local variable `csv-comment-start'.
Changing this variable does not affect any existing CSV mode buffer."
:type '(choice (const :tag "None" nil) string)
:set (lambda (variable value)
(custom-set-default variable value)
(setq-default csv-comment-start value)))
(defcustom csv-align-style 'left
"Aligned field style: one of `left', `centre', `right' or `auto'.
Alignment style used by `csv-align-mode' and `csv-align-fields'.
Auto-alignment means left align text and right align numbers."
:type '(choice (const left) (const centre)
(const right) (const auto)))
(defcustom csv-align-padding 1
"Aligned field spacing: must be a positive integer.
Number of spaces used by `csv-align-mode' and `csv-align-fields'
after separators."
:type 'integer)
(defcustom csv-header-lines 0
"Header lines to skip when setting region automatically."
:type 'integer)
(defcustom csv-invisibility-default t
"If non-nil, make separators in aligned records invisible."
:type 'boolean)
(defcustom csv-confirm-region t
"If non-nil, confirm that region is OK in interactive commands."
:type 'boolean)
(defface csv-separator-face
'((t :inherit escape-glyph))
"CSV mode face used to highlight separators.")
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Mode definition, key bindings and menu
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defconst csv-mode-line-format
'(csv-field-index-string ("" csv-field-index-string))
"Mode line format string for CSV mode.")
(defvar csv-mode-map
(let ((map (make-sparse-keymap)))
(define-key map [(control ?c) (control ?v)] #'csv-toggle-invisibility)
(define-key map [(control ?c) (control ?t)] #'csv-transpose)
(define-key map [(control ?c) (control ?c)] #'csv-set-comment-start)
(define-key map [(control ?c) (control ?u)] #'csv-unalign-fields)
(define-key map [(control ?c) (control ?a)] #'csv-align-fields)
(define-key map [(control ?c) (control ?z)] #'csv-yank-as-new-table)
(define-key map [(control ?c) (control ?y)] #'csv-yank-fields)
(define-key map [(control ?c) (control ?k)] #'csv-kill-fields)
(define-key map [(control ?c) (control ?d)] #'csv-toggle-descending)
(define-key map [(control ?c) (control ?r)] #'csv-reverse-region)
(define-key map [(control ?c) (control ?n)] #'csv-sort-numeric-fields)
(define-key map [(control ?c) (control ?s)] #'csv-sort-fields)
(define-key map "\t" #'csv-tab-command)
(define-key map [backtab] #'csv-backtab-command)
map))
;;;###autoload
(define-derived-mode csv-mode text-mode "CSV"
"Major mode for editing files of comma-separated value type.
CSV mode is derived from `text-mode', and runs `text-mode-hook' before
running `csv-mode-hook'. It turns `auto-fill-mode' off by default.
CSV mode can be customized by user options in the CSV customization
group. The separators are specified by the value of `csv-separators'.
CSV mode commands ignore blank lines and comment lines beginning with
the value of `csv-comment-start', which delimit \"paragraphs\".
\"Sexp\" is re-interpreted to mean \"field\", so that `forward-sexp'
\(\\[forward-sexp]), `kill-sexp' (\\[kill-sexp]), etc. all apply to fields.
Standard comment commands apply, such as `comment-dwim' (\\[comment-dwim]).
If `font-lock-mode' is enabled then separators, quoted values and
comment lines are highlighted using respectively `csv-separator-face',
`font-lock-string-face' and `font-lock-comment-face'.
The user interface (UI) for CSV mode commands is similar to that of
the standard commands `sort-fields' and `sort-numeric-fields', except
that if there is no prefix argument then the UI prompts for the field
index or indices. In `transient-mark-mode' only: if the region is not
set then the UI attempts to set it to include all consecutive CSV
records around point, and prompts for confirmation; if there is no
prefix argument then the UI prompts for it, offering as a default the
index of the field containing point if the region was not set
explicitly. The region set automatically is delimited by blank lines
and comment lines, and the number of header lines at the beginning of
the region given by the value of `csv-header-lines' are skipped.
Sort order is controlled by `csv-descending'.
CSV mode provides the following specific keyboard key bindings:
\\{csv-mode-map}"
:group 'CSV
;; We used to `turn-off-auto-fill' here instead, but that's not very
;; effective since text-mode-hook is run afterwards anyway!
(setq-local normal-auto-fill-function nil)
;; Set syntax for field quotes:
(csv-set-quote-syntax csv-field-quotes)
;; Make sexp functions apply to fields:
(setq-local forward-sexp-function #'csv-forward-field)
(csv-set-comment-start csv-comment-start)
;; Font locking -- separator plus syntactic:
(setq font-lock-defaults '(csv-font-lock-keywords))
(setq-local jit-lock-contextually nil) ;Each line should be independent.
(if csv-invisibility-default (add-to-invisibility-spec 'csv))
;; Mode line to support `csv-field-index-mode':
(setq-local mode-line-position
(pcase mode-line-position
(`(,(or (pred consp) (pred stringp)) . ,_)
`(,@mode-line-position ,csv-mode-line-format))
(_ `("" ,mode-line-position ,csv-mode-line-format))))
(setq-local truncate-lines t)
;; Enable or disable `csv-field-index-mode' (could probably do this
;; a bit more efficiently):
(csv-field-index-mode (symbol-value 'csv-field-index-mode)))
(defun csv-set-comment-start (string)
"Set comment start for this CSV mode buffer to STRING.
It must be either a string or nil."
(interactive
(list (edit-and-eval-command
"Comment start (string or nil): " csv-comment-start)))
;; Paragraph means a group of contiguous records:
(setq-local paragraph-separate "[[:space:]]*$") ; White space.
(setq-local paragraph-start "\n");Must include \n explicitly!
;; Remove old comment-start/end if available
(with-syntax-table text-mode-syntax-table
(when comment-start
(modify-syntax-entry (string-to-char comment-start)
(string (char-syntax (string-to-char comment-start)))
csv-mode-syntax-table))
(modify-syntax-entry ?\n
(string (char-syntax ?\n))
csv-mode-syntax-table))
(when string
(setq paragraph-separate (concat paragraph-separate "\\|" string)
paragraph-start (concat paragraph-start "\\|" string))
(setq-local comment-start string)
(modify-syntax-entry
(string-to-char string) "<" csv-mode-syntax-table)
(modify-syntax-entry ?\n ">" csv-mode-syntax-table))
(setq csv-comment-start string))
(defvar csv--set-separator-history nil)
(defun csv-set-separator (sep)
"Set the CSV separator in the current buffer to SEP."
(interactive (list (read-char-from-minibuffer
"Separator: " nil 'csv--set-separator-history)))
(when (and (boundp 'csv-field-quotes)
(member (string sep) csv-field-quotes))
(error "%c is already a quote" sep))
(setq-local csv-separators (list (string sep)))
(setq-local csv-separator-chars (list sep))
(setq-local csv--skip-chars (format "^\n\\%c" sep))
(setq-local csv-separator-regexp (regexp-quote (string sep)))
(setq-local csv-font-lock-keywords
`((,csv-separator-regexp (0 'csv-separator-face))))
(font-lock-refresh-defaults))
;;;###autoload
(add-to-list 'auto-mode-alist '("\\.[Cc][Ss][Vv]\\'" . csv-mode))
(defvar csv-descending nil
"If non-nil, CSV mode sort functions sort in order of descending sort key.
Usually they sort in order of ascending sort key.")
(defun csv-toggle-descending ()
"Toggle `csv-descending'."
(interactive)
(setq csv-descending (not csv-descending))
(message "Sort order is %sscending" (if csv-descending "de" "a")))
(defun csv-toggle-invisibility ()
;; FIXME: Make it into a proper minor mode?
"Toggle `buffer-invisibility-spec'."
(interactive)
(if (memq 'csv buffer-invisibility-spec)
(remove-from-invisibility-spec 'csv)
(add-to-invisibility-spec 'csv))
(message "Separators in aligned records will be %svisible \
\(after re-aligning if soft)"
(if (memq 'csv buffer-invisibility-spec) "in" ""))
(redraw-frame (selected-frame)))
(easy-menu-define
csv-menu
csv-mode-map
"CSV major mode menu keymap"
'("CSV"
["Sort By Field Lexicographically" csv-sort-fields :active t
:help "Sort lines in region lexicographically by the specified field"]
["Sort By Field Numerically" csv-sort-numeric-fields :active t
:help "Sort lines in region numerically by the specified field"]
["Reverse Order of Lines" csv-reverse-region :active t
:help "Reverse the order of the lines in the region"]
["Use Descending Sort Order" csv-toggle-descending :active t
:style toggle :selected csv-descending
:help "If selected, use descending order when sorting"]
"--"
["Kill Fields (Columns)" csv-kill-fields :active t
:help "Kill specified fields of each line in the region"]
["Yank Fields (Columns)" csv-yank-fields :active t
:help "Yank killed fields as specified field of each line in region"]
["Yank As New Table" csv-yank-as-new-table :active t
:help "Yank killed fields as a new table at point"]
["Align Fields into Columns" csv-align-fields :active t
:help "Align the start of every field of each line in the region"]
["Unalign Columns into Fields" csv-unalign-fields :active t
:help "Undo soft alignment and optionally remove redundant white space"]
["Transpose Rows and Columns" csv-transpose :active t
:help "Rewrite rows (which may have different lengths) as columns"]
"--"
["Forward Field" forward-sexp :active t
:help "Move forward across one field; with ARG, do it that many times"]
["Backward Field" backward-sexp :active t
:help "Move backward across one field; with ARG, do it that many times"]
["Kill Field Forward" kill-sexp :active t
:help "Kill field following cursor; with ARG, do it that many times"]
["Kill Field Backward" backward-kill-sexp :active t
:help "Kill field preceding cursor; with ARG, do it that many times"]
"--"
("Alignment Style"
["Left" (setq csv-align-style 'left) :active t
:style radio :selected (eq csv-align-style 'left)
:help "If selected, `csv-align' left aligns fields"]
["Centre" (setq csv-align-style 'centre) :active t
:style radio :selected (eq csv-align-style 'centre)
:help "If selected, `csv-align' centres fields"]
["Right" (setq csv-align-style 'right) :active t
:style radio :selected (eq csv-align-style 'right)
:help "If selected, `csv-align' right aligns fields"]
["Auto" (setq csv-align-style 'auto) :active t
:style radio :selected (eq csv-align-style 'auto)
:help "\
If selected, `csv-align' left aligns text and right aligns numbers"]
)
["Set header line" csv-header-line :active t]
["Auto-(re)align fields" csv-align-mode
:style toggle :selected csv-align-mode]
["Show Current Field Index" csv-field-index-mode :active t
:style toggle :selected csv-field-index-mode
:help "If selected, display current field index in mode line"]
["Make Separators Invisible" csv-toggle-invisibility :active t
:style toggle :selected (memq 'csv buffer-invisibility-spec)
:visible (not (tsv--mode-p))
:help "If selected, separators in aligned records are invisible"]
["Set Buffer's Comment Start" csv-set-comment-start :active t
:help "Set comment start string for this buffer"]
["Customize CSV Mode" (customize-group 'CSV) :active t
:help "Open a customization buffer to change CSV mode options"]
))
(require 'sort)
(defsubst csv-not-looking-at-record ()
"Return t if looking at blank or comment line, nil otherwise.
Assumes point is at beginning of line."
(looking-at paragraph-separate))
(defun csv-interactive-args (&optional type)
"Get arg or field(s) and region interactively, offering sensible defaults.
Signal an error if the buffer is read-only.
If TYPE is `noarg' then return a list (beg end).
Otherwise, return a list (ARG BEG END), where ARG is:
the raw prefix argument by default;
a single field index if TYPE is `single';
a list of field indices or index ranges if TYPE is `multiple'.
Field defaults to the current prefix arg; if not set, prompt user.
A field index list consists of positive or negative integers or ranges,
separated by any non-integer characters. A range has the form m-n,
where m and n are positive or negative integers, m < n, and n defaults
to the last field index if omitted.
In transient mark mode, if the mark is not active then automatically
select and highlight CSV records around point, and query user.
The default field when read interactively is the current field."
;; Must be run interactively to activate mark!
(let* ((arg current-prefix-arg) (default-field 1)
(region
(if (not (use-region-p))
;; Set region automatically:
(save-excursion
(unless arg
(setq default-field (csv--field-index)))
(beginning-of-line)
(if (csv-not-looking-at-record)
(error "Point must be within CSV records"))
(let ((startline (point)))
;; Set mark at beginning of region:
(while (not (or (bobp) (csv-not-looking-at-record)))
(forward-line -1))
(if (csv-not-looking-at-record) (forward-line 1))
;; Skip header lines:
(forward-line csv-header-lines)
(set-mark (point)) ; OK since in save-excursion
;; Move point to end of region:
(goto-char startline)
(beginning-of-line)
(while (not (or (eobp) (csv-not-looking-at-record)))
(forward-line 1))
;; Show mark briefly if necessary:
(unless (and (pos-visible-in-window-p)
(pos-visible-in-window-p (mark)))
(exchange-point-and-mark)
(sit-for 1)
(exchange-point-and-mark))
(when csv-confirm-region
(or (y-or-n-p "Region OK? ")
(error "Action aborted by user"))
(message nil)) ; clear y-or-n-p message
(list (region-beginning) (region-end))))
;; Use region set by user:
(list (region-beginning) (region-end)))))
(setq default-field (number-to-string default-field))
(cond
((eq type 'multiple)
(if arg
;; Ensure that field is a list:
(or (consp arg)
(setq arg (list (prefix-numeric-value arg))))
;; Read field interactively, ignoring non-integers:
(setq arg
(mapcar
(lambda (x)
(if (string-match "-" x 1) ; not first character
;; Return a range as a pair - the cdr may be nil:
(let ((m (substring x 0 (match-beginning 0)))
(n (substring x (match-end 0))))
(cons (car (read-from-string m))
(and (not (string= n ""))
(car (read-from-string n)))))
;; Return a number as a number:
(car (read-from-string x))))
(split-string
(read-string
"Fields (sequence of integers or ranges): " default-field)
"[^-+0-9]+")))))
((eq type 'single)
(if arg
(setq arg (prefix-numeric-value arg))
(while (not (integerp arg))
(setq arg (eval-minibuffer "Field (integer): " default-field))))))
(if (eq type 'noarg) region (cons arg region))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Sorting by field
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun csv-nextrecfun ()
"Called by `csv-sort-fields-1' with point at end of previous record.
It moves point to the start of the next record.
It should move point to the end of the buffer if there are no more records."
(forward-line)
(while (and (not (eobp)) (csv-not-looking-at-record))
(forward-line)))
(defun csv-sort-fields-1 (field beg end startkeyfun endkeyfun)
"Modified version of `sort-fields-1' that skips blank or comment lines.
FIELD is a single field index, and BEG and END specify the region to
sort.
STARTKEYFUN moves from the start of the record to the start of the key.
It may return either a non-nil value to be used as the key, or
else the key is the substring between the values of point after
STARTKEYFUN and ENDKEYFUN are called. If STARTKEYFUN is nil, the key
starts at the beginning of the record.
ENDKEYFUN moves from the start of the sort key to the end of the sort key.
ENDKEYFUN may be nil if STARTKEYFUN returns a value or if it would be the
same as ENDRECFUN."
(let ((tbl (syntax-table)))
(if (zerop field) (setq field 1))
(unwind-protect
(save-excursion
(save-restriction
(narrow-to-region beg end)
(goto-char (point-min))
(set-syntax-table sort-fields-syntax-table)
(sort-subr csv-descending
'csv-nextrecfun 'end-of-line
startkeyfun endkeyfun)))
(set-syntax-table tbl))))
(defun csv-sort-fields (field beg end)
"Sort lines in region lexicographically by the ARGth field of each line.
If not set, the region defaults to the CSV records around point.
Fields are separated by `csv-separators' and null fields are allowed anywhere.
Field indices increase from 1 on the left or decrease from -1 on the right.
A prefix argument specifies a single field, otherwise prompt for field index.
Ignore blank and comment lines. The variable `sort-fold-case'
determines whether alphabetic case affects the sort order.
When called non-interactively, FIELD is a single field index;
BEG and END specify the region to sort."
;; (interactive "*P\nr")
(interactive (csv-interactive-args 'single))
(barf-if-buffer-read-only)
(csv-sort-fields-1 field beg end
(lambda () (csv-sort-skip-fields field) nil)
(lambda () (skip-chars-forward csv--skip-chars))))
(defun csv-sort-numeric-fields (field beg end)
"Sort lines in region numerically by the ARGth field of each line.
If not set, the region defaults to the CSV records around point.
Fields are separated by `csv-separators'.
Null fields are allowed anywhere and sort as zeros.
Field indices increase from 1 on the left or decrease from -1 on the right.
A prefix argument specifies a single field, otherwise prompt for field index.
Specified non-null field must contain a number in each line of the region,
which may begin with \"0x\" or \"0\" for hexadecimal and octal values.
Otherwise, the number is interpreted according to sort-numeric-base.
Ignore blank and comment lines.
When called non-interactively, FIELD is a single field index;
BEG and END specify the region to sort."
;; (interactive "*P\nr")
(interactive (csv-interactive-args 'single))
(barf-if-buffer-read-only)
(csv-sort-fields-1 field beg end
(lambda ()
(csv-sort-skip-fields field)
(let* ((case-fold-search t)
(base
(if (looking-at "\\(0x\\)[0-9a-f]\\|\\(0\\)[0-7]")
(cond ((match-beginning 1)
(goto-char (match-end 1))
16)
((match-beginning 2)
(goto-char (match-end 2))
8)
(t nil)))))
(string-to-number (buffer-substring (point)
(save-excursion
(forward-sexp 1)
(point)))
(or base sort-numeric-base))))
nil))
(defun csv-reverse-region (beg end)
"Reverse the order of the lines in the region.
This is just a CSV-mode style interface to `reverse-region', which is
the function that should be used non-interactively. It takes two
point or marker arguments, BEG and END, delimiting the region."
;; (interactive "*P\nr")
(interactive (csv-interactive-args 'noarg))
(barf-if-buffer-read-only)
(reverse-region beg end))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Moving by field
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun csv-end-of-field ()
"Skip forward over one field."
(skip-chars-forward " ")
;; If the first character is a double quote, then we have a quoted
;; value.
(when (eq (char-syntax (following-char)) ?\")
(forward-char)
(let ((ended nil))
(while (and (not ended) (not (eolp)))
(cond ((not (eq (char-syntax (following-char)) ?\"))
(forward-char 1))
;; According to RFC-4180 (sec 2.7), quotes inside quoted strings
;; are quoted by doubling the quote char: a,"b""c,",d
;; FIXME: Maybe we should handle this via syntax-propertize?
((let ((c (char-after (1+ (point)))))
(and c (eq (char-syntax c) ?\")))
(forward-char 2))
(t
(setq ended t))))))
(skip-chars-forward csv--skip-chars))
(defun csv--bof-p ()
(or (bolp)
(memq (preceding-char) csv-separator-chars)))
(defun csv--eof-p ()
(or (eolp)
(memq (following-char) csv-separator-chars)))
(defun csv-beginning-of-field ()
"Skip backward over one field."
(skip-syntax-backward " ")
(if (eq (char-syntax (preceding-char)) ?\")
(goto-char (scan-sexps (point) -1)))
(skip-chars-backward csv--skip-chars))
(defun csv-forward-field (arg)
"Move forward across one field, cf. `forward-sexp'.
With ARG, do it that many times. Negative arg -N means
move backward across N fields."
(interactive "p")
(if (< arg 0)
(csv-backward-field (- arg))
(while (>= (setq arg (1- arg)) 0)
(if (or (bolp)
(when (and (not (eobp)) (eolp)) (forward-char) t))
(while (and (not (eobp)) (csv-not-looking-at-record))
(forward-line 1)))
(if (memq (following-char) csv-separator-chars) (forward-char))
(csv-end-of-field))))
(defun csv-backward-field (arg)
"Move backward across one field, cf. `backward-sexp'.
With ARG, do it that many times. Negative arg -N means
move forward across N fields."
(interactive "p")
(if (< arg 0)
(csv-forward-field (- arg))
(while (>= (setq arg (1- arg)) 0)
(when (or (eolp)
(when (and (not (bobp)) (bolp)) (backward-char) t))
(while (progn
(beginning-of-line)
(csv-not-looking-at-record))
(backward-char))
(end-of-line))
(if (memq (preceding-char) csv-separator-chars) (backward-char))
(csv-beginning-of-field))))
(defun csv-tab-command ()
"Skip to the next field on the same line.
Create a new field at end of line, if needed."
(interactive)
(skip-chars-forward csv--skip-chars)
(if (eolp)
(insert (car csv-separators))
(forward-char 1)))
(defun csv-backtab-command ()
"Skip to the beginning of the previous field."
(interactive)
(skip-chars-backward csv--skip-chars)
(forward-char -1)
(skip-chars-backward csv--skip-chars))
(defun csv-sort-skip-fields (n &optional yank)
"Position point at the beginning of field N on the current line.
Fields are separated by `csv-separators'; null terminal field allowed.
Assumes point is initially at the beginning of the line.
YANK non-nil allows N to be greater than the number of fields, in
which case extend the record as necessary."
(if (> n 0)
;; Skip across N - 1 fields.
(let ((i (1- n)))
(while (> i 0)
(csv-end-of-field)
(if (eolp)
(if yank
(if (> i 1) (insert (car csv-separators)))
(error "Line has too few fields: %s"
(buffer-substring
(save-excursion (beginning-of-line) (point))
(save-excursion (end-of-line) (point)))))
(forward-char)) ; skip separator
(setq i (1- i))))
(end-of-line)
;; Skip back across -N - 1 fields.
(let ((i (1- (- n))))
(while (> i 0)
(csv-beginning-of-field)
(if (bolp)
(error "Line has too few fields: %s"
(buffer-substring
(save-excursion (beginning-of-line) (point))
(save-excursion (end-of-line) (point)))))
(backward-char) ; skip separator
(setq i (1- i)))
;; Position at the front of the field
;; even if moving backwards.
(csv-beginning-of-field))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Field index mode
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Based partly on paren.el
(defcustom csv-field-index-delay 0.125
"Time in seconds to delay before updating field index display."
:type '(number :tag "seconds"))
(defvar csv-field-index-idle-timer nil)
(defvar-local csv-field-index-string nil)
(defvar-local csv-field-index-old nil)
(define-minor-mode csv-field-index-mode
"Toggle CSV-Field-Index mode.
With prefix ARG, turn CSV-Field-Index mode on if and only if ARG is positive.
Returns the new status of CSV-Field-Index mode (non-nil means on).
When CSV-Field-Index mode is enabled, the current field index appears in
the mode line after `csv-field-index-delay' seconds of Emacs idle time."
:global t
:init-value t ; for documentation, since default is t
;; This macro generates a function that first sets the mode
;; variable, then runs the following code, runs the mode hooks,
;; displays a message if interactive, updates the mode line and
;; finally returns the variable value.
;; First, always disable the mechanism (to avoid having two timers):
(when csv-field-index-idle-timer
(cancel-timer csv-field-index-idle-timer)
(setq csv-field-index-idle-timer nil))
;; Now, if the mode is on and any buffer is in CSV mode then
;; re-initialize and enable the mechanism by setting up a new timer:
(if csv-field-index-mode
(if (memq t (mapcar (lambda (buffer)
(with-current-buffer buffer
(when (derived-mode-p 'csv-mode)
(setq csv-field-index-string nil
csv-field-index-old nil)
t)))
(buffer-list)))
(setq csv-field-index-idle-timer
(run-with-idle-timer csv-field-index-delay t
#'csv-field-index)))
;; but if the mode is off then remove the display from the mode
;; lines of all CSV buffers:
(mapc (lambda (buffer)
(with-current-buffer buffer
(when (derived-mode-p 'csv-mode)
(setq csv-field-index-string nil
csv-field-index-old nil)
(force-mode-line-update))))
(buffer-list))))
(defun csv--field-index ()
(save-excursion
(let ((start (point))
(field 0))
(beginning-of-line)
(while (and (<= (point) start)
(not (eolp)))
(csv-end-of-field)
(unless (eolp)
(forward-char 1))
(setq field (1+ field)))
field)))
(defun csv-field-index ()
"Construct `csv-field-index-string' to display in mode line.
Called by `csv-field-index-idle-timer'."
(if (derived-mode-p 'csv-mode)
(let ((field (csv--field-index)))
(when (not (eq field csv-field-index-old))
(setq csv-field-index-old field
csv-field-index-string
(and field (format "F%d" field)))
(force-mode-line-update)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Killing and yanking fields
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defvar csv-killed-fields nil
"A list of the fields or sub-records last killed by `csv-kill-fields'.")
(defun csv-kill-fields (fields beg end)
"Kill specified fields of each line in the region.
If not set, the region defaults to the CSV records around point.
Fields are separated by `csv-separators' and null fields are allowed anywhere.
Field indices increase from 1 on the left or decrease from -1 on the right.
The fields are stored for use by `csv-yank-fields'. Fields can be
specified in any order but are saved in increasing index order.
Ignore blank and comment lines.
When called interactively, a prefix argument specifies a single field,
otherwise prompt for a field list, which may include ranges in the form
m-n, where m < n and n defaults to the last field index if omitted.
When called non-interactively, FIELDS is a single field index or a
list of field indices, with ranges specified as (m.n) or (m), and BEG
and END specify the region to process."
;; (interactive "*P\nr")
(interactive (csv-interactive-args 'multiple))
(barf-if-buffer-read-only)
;; Kill the field(s):
(setq csv-killed-fields nil)
(save-excursion
(save-restriction
(narrow-to-region beg end)
(goto-char (point-min))
(if (or (cdr fields) (consp (car fields)))
(csv-kill-many-columns fields)
(csv-kill-one-column (car fields)))))
(setq csv-killed-fields (nreverse csv-killed-fields)))
(defun csv-kill-one-field (field)
"Kill field with index FIELD in current line.
Return killed text. Assumes point is at beginning of line."
;; Move to start of field to kill:
(csv-sort-skip-fields field)
;; Kill to end of field (cf. `kill-region'):
(prog1 (delete-and-extract-region
(point)
(progn (csv-end-of-field) (point)))
(if (eolp)
(unless (bolp) (delete-char -1)) ; Delete trailing separator at eol
(delete-char 1)))) ; or following separator otherwise.
(defun csv-kill-one-column (field)
"Kill field with index FIELD in all lines in (narrowed) buffer.
Save killed fields in `csv-killed-fields'.
Assumes point is at `point-min'. Called by `csv-kill-fields'.
Ignore blank and comment lines."
(while (not (eobp))
(or (csv-not-looking-at-record)
(push (csv-kill-one-field field) csv-killed-fields))
(forward-line)))
(defun csv-insert-column (field)
"Insert an empty column at point."
(interactive
(let ((cur (csv--field-index)))
(list (if (and (csv--eof-p) (not (csv--bof-p))) (1+ cur) cur))))
(save-excursion
(goto-char (point-min))
(while (not (eobp))
(or (csv-not-looking-at-record)
(progn
(csv-sort-skip-fields field t)
(insert (car csv-separators))))
(forward-line 1))