-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdiogenes-browser.el
345 lines (305 loc) · 12 KB
/
diogenes-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
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
;;; diogenes-browser.el --- Corpus browser for diogenes.el -*- lexical-binding: t -*-
;; Copyright (C) 2024 Michael Neidhart
;;
;; Author: Michael Neidhart <[email protected]>
;; Keywords: classics, tools, philology, humanities
;;; Commentary:
;; This file contains functions for browsing the Corpora that Diogenes can read
;;; Code:
(require 'cl-lib)
(require 'seq)
(require 'diogenes-lisp-utils)
(require 'diogenes-perl-interface)
;;;; --------------------------------------------------------------------
;;;; BROWSER
;;;; --------------------------------------------------------------------
;;; Simple commands
(defun diogenes--send-cmd-to-browser (cmd)
(let ((diogenes-process (or (get-buffer-process (current-buffer))
(error (format "No process in buffer %s!"
(current-buffer))))))
(process-send-string diogenes-process (concat cmd "\n"))))
(defun diogenes--browser-set-height (height)
(interactive "NLines to display: ")
(diogenes--send-cmd-to-browser (number-to-string height)))
(defun diogenes-browser-forward ()
"Load the next page from the Diogenes browser."
(interactive "p")
(setq diogenes--browser-backwards nil)
(goto-char (point-max))
(diogenes--send-cmd-to-browser
(concat (number-to-string (- (floor (window-screen-lines))
next-screen-context-lines))
"n")))
(defun diogenes-browser-backward ()
"Load the previous page from the Diogenes browser."
(interactive "p")
(setq diogenes--browser-backwards t)
(goto-char (point-min))
(diogenes--send-cmd-to-browser
(concat (number-to-string (- (floor (window-screen-lines))
next-screen-context-lines))
"p")))
(defun diogenes-browser-quit ()
(interactive)
(diogenes--send-cmd-to-browser "q"))
(defun diogenes-browser-forward-line (&optional N)
(interactive "p")
(forward-line N)
(when (eobp) (diogenes-browser-forward)))
(defun diogenes-browser-backward-line (&optional N)
(interactive "p")
(forward-line (- N))
(when (bobp) (diogenes-browser-backward)))
(defun diogenes-browser-beginning-of-buffer (&optional N)
(interactive "^P")
(when (and (not N) (bobp))
(diogenes-browser-backward))
(beginning-of-buffer N))
(defun diogenes-browser-end-of-buffer (&optional N)
(interactive "^P")
(when (and (not N) (eobp))
(diogenes-browser-forward))
(end-of-buffer N))
;;; Utility commands
(defun diogenes-browser-toggle-citations ()
"Toggle display of the embedded citations in the Diogenes Browser."
(interactive)
(save-excursion
(cond (diogenes-browser-show-citations
(setq diogenes-browser-show-citations nil)
(goto-char (point-min))
(let (cit-match)
(while (setq cit-match
(text-property-search-forward 'diogenes-citation))
(delete-region (prop-match-beginning cit-match)
(prop-match-end cit-match)))))
(t
(setq diogenes-browser-show-citations t)
(goto-char (point-min))
(let (prop-change)
(while (and (setq prop-change
(next-single-property-change (point) 'cit))
(goto-char prop-change)
(not (eobp)))
(when-let ((citation (get-text-property (point) 'cit)))
(insert (diogenes--browser-format-citation citation)))))))))
(defun diogenes-browser-remove-hyphenation (&optional mark-with-vertical-bar)
"Join all hyphenated words in the current Diogenes Browser Buffer."
(interactive "P")
(unless (eq major-mode 'diogenes-browser-mode)
(error "Not in a Diogenes Browser buffer!"))
(with-undo-amalgamate
(save-excursion
(goto-char (point-min))
(let (pos-a)
(while (setq pos-a (and (re-search-forward "\\([^ <-]+\\)-\\s-*$" nil t)
(cons (match-beginning 1)
(match-end 1))))
(when-let* ((line-a (text-property-search-backward 'cit))
(line-b (text-property-search-forward 'cit nil nil t))
(pos-b (and (goto-char (prop-match-beginning line-b))
(re-search-forward "\\S-+"
(point-max) t)
(cons (match-beginning 0)
(match-end 0))))
(word-a (buffer-substring-no-properties (car pos-a)
(cdr pos-a)))
(word-b (buffer-substring-no-properties (car pos-b)
(cdr pos-b))))
(put-text-property (prop-match-beginning line-a)
(prop-match-end line-a)
'hyphen-start word-a)
(put-text-property (prop-match-beginning line-b)
(prop-match-end line-b)
'hyphen-end word-b)
(delete-region (car pos-b) (1+ (cdr pos-b)))
(goto-char (cdr pos-a))
(delete-char 1)
(when mark-with-vertical-bar (insert-and-inherit "|"))
(insert-and-inherit word-b))
(goto-char (cdr pos-a)))))))
(defun diogenes-browser-reinsert-hyphenation ()
(interactive)
(unless (eq major-mode 'diogenes-browser-mode)
(error "Not in a Diogenes Browser buffer!"))
(with-undo-amalgamate
(save-excursion
(goto-char (point-min))
(let (line-a line-b)
(while (and (setq line-a (text-property-search-forward 'hyphen-start))
(setq line-b (text-property-search-forward 'hyphen-end)))
(let ((word-a (prop-match-value line-a))
(word-b (prop-match-value line-b))
(bol-a (prop-match-beginning line-a))
(bol-b (prop-match-beginning line-b))
(eol-a (prop-match-end line-a))
(eol-b (prop-match-end line-b)))
(remove-text-properties bol-a eol-a '(hyphen-start nil))
(remove-text-properties bol-b eol-b '(hyphen-end nil))
(let ((prop-a (text-properties-at bol-a))
(prop-b (text-properties-at bol-b)))
(goto-char bol-b)
(insert (apply #'propertize (concat word-b " ")
prop-b))
(goto-char eol-a)
(re-search-backward (regexp-quote word-b))
(delete-char (length word-b))
(when (string= (buffer-substring (1- (point)) (point))
"|")
(delete-char -1))
(insert (apply #'propertize "-"
prop-a)))))))))
(defun diogenes-browser-lookup ()
"Lookup word at point."
(interactive)
(funcall (intern (concat "diogenes-parse-and-lookup-"
diogenes--browser-language))
(replace-regexp-in-string "[^[:alpha:]]" ""
(thing-at-point 'word))))
;;; Browser Mode
(defvar diogenes-browser-mode-map
(let ((map (nconc (make-sparse-keymap) text-mode-map)))
;; Overrides of movement keys
(keymap-set map "<remap> <previous-line>" #'diogenes-browser-backward-line)
(keymap-set map "<remap> <next-line>" #'diogenes-browser-forward-line)
(keymap-set map "<remap> <beginning-of-buffer>" #'diogenes-browser-beginning-of-buffer)
(keymap-set map "<remap> <end-of-buffer>" #'diogenes-browser-end-of-buffer)
(keymap-set map "q" #'quit-window)
(keymap-set map "C-c C-n" #'diogenes-browser-forward)
(keymap-set map "C-c C-p" #'diogenes-browser-backward)
;; Actions
(keymap-set map "C-c C-c" #'diogenes-browser-lookup)
(keymap-set map "C-c C-q" #'diogenes-browser-quit)
;; Utilities
(keymap-set map "C-c C--" #'diogenes-browser-remove-hyphenation)
(keymap-set map "C-c C-+" #'diogenes-browser-reinsert-hyphenation)
(keymap-set map "C-c C-t" #'diogenes-browser-toggle-citations)
map)
"Basic mode map for the Diogenes Browser.")
(define-derived-mode diogenes-browser-mode text-mode "Diogenes Browser"
"Major mode to browse Diogenes' databases."
(make-local-variable 'diogenes--browser-backwards)
(make-local-variable 'diogenes--browser-language)
(make-local-variable 'diogenes--browser-first-insertion))
;;; Browser process filter
(defun diogenes--browser-format-citation (citation)
(propertize (format "%-14s"
(mapconcat (lambda (x) (format "%s" x))
citation
"."))
'diogenes-citation t
'face 'font-lock-comment-face
'font-lock-face 'font-lock-comment-face
'rear-nonsticky t))
(defun diogenes--browser-format-header (header-lines)
(propertize (concat (string-join header-lines
"\n")
"\n\n")
'diogenes-header t
'face 'info-title-1
'font-lock-face 'info-title-1
;; 'read-only t
'front-sticky t
'rear-nonsticky t))
(defvar-local diogenes--browser-output-buffer ""
"Buffers the output of the diogenes browser output, if it is an
incomplete lisp expression.")
(defun diogenes--read-browser-output (str)
"Try to read a lisp expression from browser output.
If it is incomplete, buffer it and prepend it when called again."
(let ((form (ignore-errors (read (concat diogenes--browser-output-buffer
str)))))
(cond ((and form (listp form))
(setq diogenes--browser-output-buffer "") form)
(t (setq diogenes--browser-output-buffer
(concat diogenes--browser-output-buffer str))
nil))))
(defun diogenes--browser-filter (proc string)
(when (buffer-live-p (process-buffer proc))
(when-let ((data (diogenes--read-browser-output string)))
(with-current-buffer (process-buffer proc)
(seq-let (cit header &rest lines) data
(unless lines (error "No input received!"))
(cond ((and (boundp 'diogenes--browser-backwards)
diogenes--browser-backwards)
(cond (header (goto-char (point-min))
(newline)
(goto-char (point-min)))
(t (or (text-property-search-forward 'diogenes-header)
(goto-char (point-min))))))
(t (goto-char (point-max))))
(when header (insert (diogenes--browser-format-header header)))
(let ((pos (point)))
(dolist (alist lines)
(when diogenes-browser-show-citations
(insert (diogenes--browser-format-citation (car alist))))
(insert (propertize (format "%s\n" (cdr alist))
'cit (car alist))))
(set-marker (process-mark proc) (point-max))
(cond (diogenes-browser-first-insertion
(setq diogenes-browser-first-insertion nil)
(goto-char pos))
(t (recenter -1 t)))))))))
(defun diogenes--browse-work (options passage)
"Function that browses a work from the Diogenes Databases.
Passage has to be a list of strings containing the four digit
number of the author and the number of the work."
(diogenes--start-perl "browser"
(diogenes--browse-interactively-script options passage)
#'diogenes--browser-filter)
(diogenes-browser-mode)
(setq diogenes-browser-first-insertion t)
(setq diogenes--browser-language
(pcase (plist-get options :type)
("tlg" "greek")
("phi" "latin"))))
(defun diogenes--browse-database (type &optional author work)
"Select a specific passage in a work from a diogenes database for browsing.
Uses the Diogenes Perl module."
(let* ((author (or author
(diogenes--select-author-num (list :type type))))
(work (or work
(diogenes--select-work-num (list :type type)
author)))
(passage (when (y-or-n-p "Specify passage? ")
(diogenes--select-passage (list :type type)
author
work))))
(diogenes--browse-work (list :type type) (nconc (list author work)
passage))))
;;;; --------------------------------------------------------------------
;;;; DUMPER
;;;; --------------------------------------------------------------------
(defun diogenes--dump-from-database-sentinel (process event)
"Sentinel for the Diogenes Dumper. Its main function is to
initialize post-processing after termination."
(with-current-buffer (process-buffer process)
(pcase event
("finished\n"
(goto-char (point-max))
(set-mark (point))
(re-search-backward "^[[:alpha:]]+")
(beginning-of-line)))))
(defun diogenes--dump-work (options passage)
"Function that dumps a work from the Diogenes Databases.
Passage has to be a list of strings containing the four digit
number of the author and the number of the work."
(diogenes--start-perl "dump"
(diogenes--browser-script
(append options '(:browse-lines 100000000))
passage)
nil
#'diogenes--dump-from-database-sentinel))
;; $query->{browser_multiple} = 100000000
(defun diogenes--dump-from-database (type &optional author work)
"Dump a work from a Diogenes database in its entirety.
Uses the Diogenes Perl module."
(let* ((author (or author
(diogenes--select-author-num `(:type ,type))))
(work (or work
(diogenes--select-work-num `(:type ,type)
author))))
(diogenes--dump-work `(:type ,type) (list author work))))
(provide 'diogenes-browser)
;;; diogenes-browser.el ends here