-
Notifications
You must be signed in to change notification settings - Fork 2
/
pjb-browser.el
228 lines (197 loc) · 7.66 KB
/
pjb-browser.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
;;;; -*- mode:emacs-lisp;coding:utf-8 -*-
;;;;**************************************************************************
;;;;FILE: pjb-browser.el
;;;;LANGUAGE: emacs lisp
;;;;SYSTEM: POSIX
;;;;USER-INTERFACE: NONE
;;;;DESCRIPTION
;;;;
;;;; A column-based browser.
;;;;
;;;;AUTHORS
;;;; <PJB> Pascal J. Bourguignon <[email protected]>
;;;;MODIFICATIONS
;;;; 2021-05-08 <PJB> Created.
;;;;BUGS
;;;;LEGAL
;;;; AGPL3
;;;;
;;;; Copyright Pascal J. Bourguignon 2021 - 2021
;;;;
;;;; This program is free software: you can redistribute it and/or modify
;;;; it under the terms of the GNU Affero 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 Affero General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU Affero General Public License
;;;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;;;**************************************************************************
(require 'pjb-cl)
(require 'org)
(require 'org-table)
;;;---------------------------------------------------------------------
;;; Utilities
;;;---------------------------------------------------------------------
(defun emptyp (sequence)
"Predicate: the sequence is empty."
(or (null sequence)
(and (not (consp sequence))
(zerop (length sequence)))))
(defmacro with-temp-mode (mode &body body)
(let ((saved-mode (gensym)))
`(let ((,saved-mode major-mode))
(unless (eq ,saved-mode ',mode)
(,mode))
(unwind-protect
(progn ,@body)
(unless (eq ,saved-mode ',mode)
(funcall ,saved-mode))))))
;;;---------------------------------------------------------------------
;;; Functions missing from org-table
;;;---------------------------------------------------------------------
(defun org-table-columns ()
"Return the number of column in the current org-table."
(org-table-analyze)
org-table-current-ncol)
(defun org-table-clean-column (&optional column)
"Empty all the data cells in the `column` number or the current column."
(interactive)
;; TODO: perhaps org-table would want to update computed stuff from the new table?
(let ((column (org-table-current-column)))
(org-table-goto-column column nil t)
(org-table-insert-column)
(org-table-goto-column (+ 1 column) nil t)
(org-table-delete-column)))
(defun org-table-delete-row (&optional row)
"Delete the `row` number or the current row."
(interactive)
;; TODO: perhaps org-table would want to update computed stuff from the new table?
(when row
(org-table-goto-line row))
(beginning-of-line)
(kill-line)
(org-table-next-field))
(defun org-table-remove-empty-rows ()
"Delete all the rows in the current table that have only empty cells."
(interactive)
;; TODO: perhaps org-table would want to update computed stuff from the new table?
(loop
with row = 1
while (ignore-errors (org-table-goto-line row))
if (loop for column from 1 to (org-table-columns)
for cell = (org-table-get row column)
;; do (message "cell[%d,%d]= %S" row column cell)
always (emptyp cell))
do (org-table-delete-row row)
else
do (incf row)))
;;;---------------------------------------------------------------------
;;; Browser functions
;;;---------------------------------------------------------------------
(defun pjb-insert-browser-table ()
"Insert at the point a new empty browser table."
(org-table-create "1x1")
(org-table-next-field)
(org-table-hline-and-move nil)
(org-table-goto-line 2))
(defun pjb-insert-browser-column (column title browser-items)
"Insert the `browser-items` in the `column` number in the current org-table, under the `title`.
If the table dosn't have that number of columns, they're automatically created."
;; browser-item ::= (title action data)
(org-table-clean-column column)
(when title
(org-table-goto-line 1)
(org-table-goto-column column)
(org-table-blank-field)
(insert title))
(org-table-goto-line 2)
(org-table-goto-column column)
(let ((head-pos (point)))
(loop
with first-row = t
for (title action data) in browser-items
do (if first-row
(setf first-row nil)
(org-table-next-row))
(org-table-blank-field)
(insert-text-button title
'action action
'button-data (cons title data)))
(org-table-remove-empty-rows)
(goto-char head-pos)
(org-table-align)))
(defun pjb-browser-browser-window (name)
(switch-to-buffer (get-buffer-create (format "*%s Browser*" name)))
(org-mode)
(let ((map (make-sparse-keymap)))
(suppress-keymap map t)
(local-set-key "n" 'org-table-next-row)
(define-key map "p" 'org-table-previous-row)
(define-key map "n" 'next-item)
(define-key map "C-n" 'next-item)
(define-key map "p" 'previous-item)
(define-key map "C-p" 'previous-item)
map))
;; (local-set-key (kbd "C-c i") *clage-mode-map*)
(defun pjb-browser-item-window (fname)
"Locate the window for the file fname, or split the current window for it."
(let ((buffer (find fname (buffer-list)
:key (function buffer-file-name)
:test (function equal)))
(size -10))
(select-window
(or
;; If there is already a window with the buffer for the file:
(and buffer (find buffer (window-list) :key (function window-buffer)))
;; otherwise, find a good window:
(let ((window (or (window-in-direction 'below)
(split-window (selected-window) size 'above))))
;; and visit the buffer there:
(if buffer
(switch-to-buffer buffer)
(find-file fname))
window)))))
(defun pjb-select-category-item (data)
(destructuring-bind (category-title &rest items-browser-items) data
(pjb-insert-browser-column 2 category-title items-browser-items)))
(defun pjb-select-item (data)
(let ((item (cdr data)))
(pjb-browser-item-window (item-file item))
(with-current-buffer (current-buffer)
(goto-char (point-min))
(search-forward (item-code item) nil t))))
(defvar *categories* '(requirement specification analysis
design code unit-test integration-test
documentation))
(defun make-item-browser-tree (buffer)
(let ((categories (make-hash-table))
(result '()))
(dolist (category *categories*)
(setf (gethash category categories) '()))
(dolist (item (extract-trace-items-from-buffer buffer))
(push (list (item-identification item) 'pjb-select-item item)
(gethash (item-category item) categories '())))
(maphash (lambda (category items)
(push (list (symbol-name category)
'pjb-select-category-item
(sort* items
(function string<)
:key (function first)))
result))
categories)
(nreverse result)))
(defun pjb-browse-items ()
(interactive)
(let ((item-buffer (current-buffer)))
(pjb-browser-browser-window (format "Items of %s" (buffer-name)))
(erase-buffer)
(pjb-insert-browser-table)
(pjb-insert-browser-column
1 "categories"
(make-item-browser-tree item-buffer))))
;;;; THE END ;;;;