-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgridlock-csv.el
120 lines (103 loc) · 4.08 KB
/
gridlock-csv.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
;;; gridlock-csv.el --- Gridlock for csv-mode
;; Copyright (C) 2018-2019 Dan Harms (dharms)
;; Author: Dan Harms <[email protected]>
;; Created: Tuesday, February 20, 2018
;; Version: 1.0
;; Modified Time-stamp: <2019-07-26 15:58:43 dan.harms>
;; Modified by: Dan Harms
;; Keywords: tools
;; URL: https://github.com/articuluxe/gridlock.git
;; Package-Requires: ((emacs "24.4"))
;; This program 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 of the License, or
;; (at your option) any later version.
;; This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Defines a minor mode for gridlock with csv files.
;;
;;; Code:
(require 'gridlock)
(defcustom gridlock-csv-preferred-display-schemes
'("popup" "pos-tip" "quick-peek" "echo")
"Preferred display schemes for `gridlock-csv-mode'.
Display schemes will be loaded in this order."
:group 'gridlock-group)
(defvar gridlock-csv-init-hook '()
"Hooks run after setting up `gridlock-csv-mode' but before inspecting buffer.")
(defvar-local gridlock-csv-metadata nil
"The metadata for this csv buffer.")
(defun gridlock-csv--get-buffer-metadata ()
"Get the metadata for the current buffer."
(let ((i 0)
(gridlock-field-regex-begin "#")
anchor fields len)
(gridlock-csv--reset-metadata)
(save-excursion
(goto-char (point-min))
(and (setq anchor (search-forward-regexp gridlock-anchor-regex (line-end-position) t))
(setq fields (gridlock--check-anchor anchor))
(setq len (length fields))
(setq gridlock-csv-metadata
(make-vector len ""))
(while (< i len)
(aset gridlock-csv-metadata i
(string-trim (gridlock-get-str (aref fields i))))
(setq i (1+ i)))))))
(defun gridlock-csv--reset-metadata ()
"Reset metadata associated with current buffer."
(setq gridlock-csv-metadata nil))
(defun gridlock-csv-get-title (field)
"Return the title associated with FIELD."
(let* ((idx (gridlock-get-index field))
(len (length gridlock-csv-metadata))
(default (format "Heading %d" (1+ idx)))
str)
(if (>= idx len)
default ;start ot 1 for user-visible names
(setq str (aref gridlock-csv-metadata idx))
(if (string-empty-p str) default str))))
(defun gridlock-csv-gather-titles (fields)
"Return a list of all field headings in field list FIELDS."
(mapcar 'identity gridlock-csv-metadata))
(defvar gridlock-csv-mode-map
(let ((map gridlock-mode-map))
(define-key map [t] 'gridlock-csv-turn-off)
map)
"Keymap for `gridlock-csv-mode'.")
(defun gridlock-csv-reset()
"Reset `gridlock-csv' state."
(gridlock-reset)
(gridlock-csv--reset-metadata)
)
(defun gridlock-csv-turn-off ()
"Turn off `gridlock-csv-mode'."
(interactive)
(gridlock-csv-mode -1))
(define-minor-mode gridlock-csv-mode
"Navigate between and explicate CSV fields."
:init-value nil
:lighter " GRID"
:keymap gridlock-csv-mode-map
(if gridlock-csv-mode
(progn
(setq gridlock-anchor-regex "^")
(setq gridlock-field-delimiter ",")
(setq gridlock-field-regex-begin nil)
(setq gridlock-field-regex-end nil)
(setq gridlock-metadata-get-title-func #'gridlock-csv-get-title)
(setq gridlock-metadata-gather-titles-func #'gridlock-csv-gather-titles)
(run-hooks 'gridlock-csv-init-hook)
(gridlock-csv-reset)
(gridlock-csv--get-buffer-metadata)
(gridlock-activate-one-of-display-schemes
gridlock-csv-preferred-display-schemes)
(gridlock-show-title))
(gridlock-csv-reset)))
(provide 'gridlock-csv)
;;; gridlock-csv.el ends here