bible-mode.el 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  1. ;;;; -*- mode: EMACS-LISP; lexical-binding: t -*-
  2. ;;
  3. ;; bible-mode.el --- A browsing interface for the SWORD Project's Diatheke CLI
  4. ;; Time-stamp: <2024-05-15 19:32:59 fred>
  5. ;; Author: Zacalot
  6. ;; Fixes and modifications by Fred Gilham
  7. ;; Url: https://gitbot.homedns.org/fred/bible-mode
  8. ;; Forked from
  9. ;; Url: https://github.com/Zacalot/bible-mode
  10. ;; Version: 1.0.0
  11. ;; Package-Requires: ((emacs "24.1"))
  12. ;; Keywords: diatheke, sword, research, bible
  13. ;;; Commentary:
  14. ;; This package uses the `diatheke' program to browse and search
  15. ;; Biblical texts provided by the Sword project.
  16. ;; Word study is also supported.
  17. ;;; Usage:
  18. ;; First install `diatheke'. On Debian/Ubuntu it's in the `diatheke'
  19. ;; package. In other distributions it might be in the sword package.
  20. ;; For Windows I found that you can simply install the Xiphos package.
  21. ;; It includes the Sword library and its utilities including diatheke,
  22. ;; installmgr and mkfastmod. Add the "Program Files\Xiphos\bin" path
  23. ;; to your execution path.
  24. ;; Use M-x `bible-open' to open a Bible buffer.
  25. ;; Use C-h f `bible-mode' to see available keybindings.
  26. ;; You may customize `bible-mode-module' to set a default browsing
  27. ;; module, as well as `bible-mode-word-study-enabled' to enable word
  28. ;; study by default.
  29. ;;; Design:
  30. ;; The idea here is to use the diatheke program to insert code from
  31. ;; modules into buffers. The main bible display uses an "internal" XML
  32. ;; format. The whole buffer gets parsed by libxml-parse-html-region to
  33. ;; create a dom tree. This gets parsed by
  34. ;; bible-mode--insert-domnode-recursive to render the text into
  35. ;; reading format.
  36. ;; The text is then decorated using information from the dom format as
  37. ;; necessary along with regular expressions to identify the verse
  38. ;; references. This is for red letters, purple highlighting of the
  39. ;; verse numbers, bold face of the divine name in the OT and so on.
  40. ;; If strongs tags and/or morphological tags are present, they are
  41. ;; looked up in appropriate lexical and morphological modules and used
  42. ;; to add tooltips to the text so that mousing over words will bring
  43. ;; up a tooltip with information about the word. Clicking on a word
  44. ;; with lexical information will display that informatio in a "term"
  45. ;; buffer.
  46. ;;;
  47. ;;; bm- is used as shorthand (see Local Variables) for bible-mode-
  48. ;;; Code:
  49. ;;;; Requirements
  50. (require 'cl-lib) ; XXX FMG there are just a few constructs that use this; use elisp versions instead.
  51. ;; (require 'bidi)
  52. (require 'dom)
  53. (require 'shr)
  54. ;; Turn off tool bar mode because we want the pixels....
  55. (tool-bar-mode -1)
  56. ;;;; Variables
  57. (defgroup bible-mode nil
  58. "Settings for `bible-mode'."
  59. :group 'tools
  60. :link '(url-link "https://gitbot.homedns.org/fred/bible-mode"))
  61. (defcustom bm-module
  62. "KJV"
  63. "Book module for Diatheke to query."
  64. :type '(choice (const :tag "None" nil)
  65. (string :tag "Module abbreviation (e.g. \"KJV\")"))
  66. :local t
  67. :group 'bible-mode)
  68. (defcustom bm-font
  69. "Ezra SIL"
  70. "Default font for bible-mode."
  71. :type '(string :tag "Font family name (e.g. \"Ezra SIL\")")
  72. :local t
  73. :group 'bible-mode)
  74. (defcustom bm-greek-lexicon
  75. "MLStrong"
  76. "Lexicon used for displaying definitions of Greek words using Strong's codes."
  77. :type '(string :tag "Lexicon module (e.g. \"StrongsRealGreek\").")
  78. :local nil
  79. :group 'bible-mode)
  80. (defcustom bm-short-greek-lexicon
  81. "StrongsRealGreek"
  82. "Lexicon used for displaying definitions of Greek words in tooltips."
  83. :type '(string :tag "Lexicon module (e.g. \"StrongsRealGreek\").")
  84. :local nil
  85. :group 'bible-mode)
  86. (defcustom bm-hebrew-lexicon
  87. "StrongsRealHebrew" ; Nice to use BDBGlosses_Strongs but it needs to be special-cased
  88. "Lexicon used for displaying definitions of Hebrew words using Strong's codes."
  89. :type '(string :tag "Lexicon module (e.g. \"StrongsRealHebrew\")")
  90. :local nil
  91. :group 'bible-mode)
  92. (defcustom bm-short-hebrew-lexicon
  93. "StrongsRealHebrew"
  94. "Lexicon used for displaying definitions of Hebrew words in tooltips."
  95. :type '(string :tag "Lexicon module (e.g. \"StrongsRealHebrew\")")
  96. :local nil
  97. :group 'bible-mode)
  98. (defcustom bm-word-study-enabled
  99. nil
  100. "Display Strong's Hebrew, Strong's Greek, and Lemma words for study."
  101. :type 'boolean
  102. :local t
  103. :group 'bible-mode)
  104. (defcustom bm-red-letter-enabled
  105. t
  106. "Display words of Jesus in red when module has that information."
  107. :type 'boolean
  108. :local t
  109. :group 'bible-mode)
  110. ;;; defvars
  111. (defvar bm-modules (lazy-completion-table bm-modules bm-list-biblical-modules))
  112. ;; XXX I believe these chapter counts aren't the same for all modules, e.g. JPS.
  113. (defvar bm-books
  114. '(;; Old Testament
  115. ("Genesis" . 50) ("Exodus" . 40) ("Leviticus" . 27) ("Numbers" . 36)
  116. ("Deuteronomy" . 34) ("Joshua" . 24) ("Judges" . 21) ("Ruth" . 4)
  117. ("I Samuel" . 31) ("II Samuel" . 24) ("I Kings" . 22) ("II Kings" . 25)
  118. ("I Chronicles" . 29) ("II Chronicles" . 36) ("Ezra" . 10) ("Nehemiah" . 13)
  119. ("Esther" . 10) ("Job" . 42) ("Psalms" . 150) ("Proverbs" . 31)
  120. ("Ecclesiastes" . 12) ("Song of Solomon" . 8) ("Isaiah" . 66) ("Jeremiah" . 52)
  121. ("Lamentations" . 5) ("Ezekiel" . 48) ("Daniel" . 12) ("Hosea" . 14)
  122. ("Joel" . 3) ("Amos" . 9) ("Obadiah" . 1) ("Jonah" . 4)
  123. ("Micah" . 7) ("Nahum" . 3) ("Habakkuk" . 3) ("Zephaniah" . 3)
  124. ("Haggai" . 2) ("Zechariah" . 14) ("Malachi" . 4)
  125. ;; New Testament
  126. ("Matthew" . 28) ("Mark" . 16) ("Luke" . 24) ("John" . 21)
  127. ("Acts" . 28) ("Romans" . 16) ("I Corinthians" . 16) ("II Corinthians" . 13)
  128. ("Galatians" . 6) ("Ephesians" . 6) ("Philippians" . 4) ("Colossians" . 4)
  129. ("I Thessalonians" . 5) ("II Thessalonians" . 3) ("I Timothy" . 6) ("II Timothy" . 4)
  130. ("Titus" . 3) ("Philemon" . 1) ("Hebrews" . 13) ("James" . 5)
  131. ("I Peter" . 5) ("II Peter" . 3) ("I John" . 5) ("II John" . 1)
  132. ("III John" . 1) ("Jude" . 1) ("Revelation of John" . 22))
  133. "A-list of name / chapter count for Bible books.")
  134. ;;;; Book / chapter
  135. (defvar-local bm-current-book (assoc "Genesis" bm-books)
  136. "Current book data (name . chapter).")
  137. (defvar-local bm-current-book-name "Genesis"
  138. "Current book name.")
  139. (defvar-local bm-current-chapter 1
  140. "Current book chapter number.")
  141. (defvar-local bm-search-query nil
  142. "Search query associated with the buffer.")
  143. (defvar-local bm-search-mode "phrase"
  144. "Search mode: either `lucene' or `phrase'.")
  145. (defvar-local bm-has-strongs nil
  146. "Set if the module being displayed has strongs numbers availabile.")
  147. (defvar-local bm-has-morphology nil
  148. "Set if the module being displayed has morphology availabile.")
  149. ;; (defvar bm-current-module nil)
  150. ;;;; Keymaps
  151. (defconst bm-map (make-keymap))
  152. ;;;;; Navigation
  153. (define-key bm-map "n" 'bm-next-chapter)
  154. (define-key bm-map "p" 'bm-previous-chapter)
  155. (define-key bm-map (kbd "TAB") 'bm-forward-word) ; TODO: bm-forward-word
  156. ;;;;; Direct jump
  157. (define-key bm-map "b" 'bm-select-book)
  158. (define-key bm-map "c" 'bm-select-chapter)
  159. ;;;;; Search
  160. (define-key bm-map "s" 'bible-search)
  161. (define-key bm-map "/" 'bible-search)
  162. ;;;; Not yet
  163. ;;(define-key bm-map "" 'bm-set-search-range)
  164. ;;;;; Misc
  165. (define-key bm-map "m" 'bm-select-module)
  166. (define-key bm-map "w" 'bm-toggle-word-study)
  167. (define-key bm-map "x" 'bm-split-display)
  168. ;;;;; Deal with visual-line-mode
  169. (define-key bm-map "\C-n" 'next-logical-line)
  170. (define-key bm-map "\C-p" 'previous-logical-line)
  171. (defconst bible-search-mode-map (make-keymap))
  172. (define-key bible-search-mode-map "s" 'bible-search)
  173. (define-key bible-search-mode-map "w" 'bm-toggle-word-study)
  174. (define-key bible-search-mode-map (kbd "RET") 'bible-search-mode-follow-verse)
  175. (defconst bible-term-hebrew-mode-map (make-keymap))
  176. (defconst bible-term-greek-mode-map (make-keymap))
  177. (defconst bible-term-morph-mode-map (make-keymap))
  178. ;;;
  179. ;;; Menu bar items
  180. ;;;
  181. ;;; Right now just convenience items. More as I think of them.
  182. ;;;
  183. (define-key global-map [menu-bar bible-mode]
  184. (cons "Bible Mode" (make-sparse-keymap "Bible Mode")))
  185. (defun bm-set-left-to-right ()
  186. (interactive)
  187. (setq-local bidi-paragraph-direction 'left-to-right))
  188. (defun bm-set-right-to-left ()
  189. (interactive)
  190. (setq-local bidi-paragraph-direction 'right-to-left))
  191. (define-key global-map
  192. [menu-bar bible-mode left-to-right]
  193. '("Left-to-right" . bm-set-left-to-right))
  194. (define-key global-map
  195. [menu-bar bible-mode right-to-left]
  196. '("Right-to-left" . bm-set-right-to-left))
  197. (defvar-local bm-debugme nil)
  198. (defun bm-set-display-xml ()
  199. (interactive)
  200. (setq-local bm-debugme t)
  201. (bm-display))
  202. (defun bm-set-display-text ()
  203. (interactive)
  204. (setq-local bm-debugme nil)
  205. (bm-display))
  206. (define-key global-map
  207. [menu-bar bible-mode display-xml]
  208. '("Display XML" . bm-set-display-xml))
  209. (define-key global-map
  210. [menu-bar bible-mode display-text]
  211. '("Display Text" . bm-set-display-text))
  212. (define-key global-map
  213. [menu-bar bible-mode select-biblical-text]
  214. '("Select Module" . bm-display-available-modules))
  215. (defun bm-display-greek ()
  216. (interactive)
  217. (let ((item (car (split-string (get-text-property (point) 'strong)))))
  218. ;; Remove "strong:G" prefix
  219. (bible-term-greek (replace-regexp-in-string "strong:G" "" item))))
  220. (defconst bm-greek-keymap (make-sparse-keymap))
  221. (define-key bm-greek-keymap (kbd "RET") 'bm-display-greek)
  222. (define-key bm-greek-keymap [mouse-1] 'bm-display-greek)
  223. (defun bm-display-hebrew ()
  224. (interactive)
  225. (let ((item (car (split-string (get-text-property (point) 'strong)))))
  226. ;; Remove "strong:H" prefix and any alphabetic suffixes.
  227. (bible-term-hebrew (replace-regexp-in-string "[a-zA-Z]" "" item nil nil nil 8))))
  228. (defconst bm-hebrew-keymap (make-sparse-keymap))
  229. (define-key bm-hebrew-keymap (kbd "RET") 'bm-display-hebrew)
  230. (define-key bm-hebrew-keymap [mouse-1] 'bm-display-hebrew)
  231. (defconst bm-lemma-keymap (make-sparse-keymap))
  232. (define-key bm-lemma-keymap (kbd "RET")
  233. (lambda ()
  234. (interactive)
  235. ))
  236. (defconst bm-morph-keymap (make-sparse-keymap))
  237. (define-key bm-morph-keymap (kbd "RET")
  238. (lambda ()
  239. (interactive)
  240. ;;; (let ((thing (thing-at-point 'word)))
  241. ;;; (message "thing at point: %s" thing)
  242. ;;; (message "morph property %s" (get-text-property 0 'field thing))
  243. ))
  244. ;;;; Modes
  245. (define-derived-mode bible-mode special-mode "Bible"
  246. "Mode for reading the Bible.
  247. \\{bm-map}"
  248. (buffer-disable-undo)
  249. (font-lock-mode t)
  250. (use-local-map bm-map)
  251. (setq buffer-read-only t)
  252. (visual-line-mode t))
  253. (define-derived-mode bible-search-mode special-mode "Bible Search"
  254. "Mode for performing Bible searches.
  255. \\{bible-search-mode-map}"
  256. (buffer-disable-undo)
  257. (font-lock-mode t)
  258. (use-local-map bible-search-mode-map)
  259. (setq buffer-read-only t)
  260. (visual-line-mode t)
  261. )
  262. (define-derived-mode bible-term-hebrew-mode special-mode "Bible Term (Hebrew)"
  263. "Mode for researching Hebrew terms in the Bible.
  264. \\{bible-term-hebrew-mode-map}"
  265. (buffer-disable-undo)
  266. (font-lock-mode t)
  267. (use-local-map bible-term-hebrew-mode-map)
  268. (setq buffer-read-only t)
  269. (visual-line-mode t))
  270. (define-derived-mode bible-term-greek-mode special-mode "Bible Term (Greek)"
  271. "Mode for researching Greek terms in the Bible.
  272. \\{bible-term-greek-mode-map}"
  273. (buffer-disable-undo)
  274. (font-lock-mode t)
  275. (use-local-map bible-term-greek-mode-map)
  276. (setq buffer-read-only t)
  277. (visual-line-mode t))
  278. (define-derived-mode module-select-mode special-mode "Select Text Module"
  279. (buffer-disable-undo)
  280. (font-lock-mode t)
  281. (setq buffer-read-only t))
  282. ;;;; Functions
  283. ;;;;; Commands
  284. ;;;###autoload
  285. (defun bible-open (&optional book-name chapter verse)
  286. "Creates and opens a `bible-mode' buffer"
  287. (interactive)
  288. (let ((buf (get-buffer-create (generate-new-buffer-name (concat "*bible*")))))
  289. (set-buffer buf)
  290. (bible-mode)
  291. (bm-set-location (assoc (or book-name "Genesis") bm-books) (or chapter 1) verse)
  292. (set-window-buffer (get-buffer-window (current-buffer)) buf)))
  293. ;;;###autoload
  294. (defun bm-next-chapter ()
  295. "Pages to the next chapter for the active `bible-mode' buffer."
  296. (interactive)
  297. (let* ((book-chapters (cdr bm-current-book))
  298. (chapter (min book-chapters (+ bm-current-chapter 1))))
  299. (bm-set-location bm-current-book chapter)))
  300. ;;;###autoload
  301. (defun bm-previous-chapter ()
  302. "Pages to the previous chapter for the active `bible-mode' buffer."
  303. (interactive)
  304. (bm-set-location bm-current-book (max 1 (- bm-current-chapter 1))))
  305. (defun bm-forward-word ()
  306. "Moves forward a word, taking into account the relevant text properties.
  307. XXX Doesn't work yet."
  308. (interactive)
  309. (field-end))
  310. ;;;###autoload
  311. (defun bm-select-book ()
  312. "Queries user to select a new book and chapter for the current
  313. `bible-mode' buffer."
  314. (interactive)
  315. (let* ((completion-ignore-case t)
  316. (book-data (assoc (completing-read "Book: " bm-books nil t) bm-books))
  317. (chapter (string-to-number (completing-read "Chapter: " (bm-list-number-range 1 (cdr book-data)) nil t))))
  318. (setq-local bm-current-book book-data)
  319. (setq-local bm-current-book-name (car book-data))
  320. (setq-local bm-current-chapter chapter)
  321. (bm-display)))
  322. ;;;###autoload
  323. (defun bm-select-chapter ()
  324. "Queries user to select a new chapter for the current `bible-mode' buffer."
  325. (interactive)
  326. (let* ((book-chapters (cdr bm-current-book))
  327. (chapter (string-to-number (completing-read "Chapter: " (bm-list-number-range 1 book-chapters) nil t))))
  328. (when chapter
  329. (bm-set-location bm-current-book chapter))))
  330. ;;;###autoload
  331. (defun bm-select-module ()
  332. "Queries user to select a new reading module for the current `bible-mode' buffer."
  333. (interactive)
  334. (let ((module (completing-read "Module: " bm-modules)))
  335. (setq-local bm-module module)
  336. (bm-display)))
  337. ;;;###autoload
  338. (defun bm-toggle-word-study()
  339. "Toggles the inclusion of word study for the active `bible-mode' buffer."
  340. (interactive)
  341. (setq bm-word-study-enabled (not bm-word-study-enabled))
  342. (if (equal major-mode 'bible-search-mode)
  343. (bm-display-search bm-search-query bm-search-mode bm-module)
  344. (bm-display)))
  345. ;;;###autoload
  346. (defun bm-split-display ()
  347. "Copies the active `bible-mode' buffer into a new buffer in another window."
  348. (interactive)
  349. (split-window-right)
  350. (balance-windows)
  351. (other-window 1)
  352. (bible-open bm-current-book-name bm-current-chapter))
  353. ;;;###autoload
  354. (defun bible-search (query)
  355. "Prompts the user for a Bible search query: word or phrase and type of
  356. search: either `lucene' or `phrase'. `lucene' mode requires an index
  357. to be built using the `mkfastmod' program. `lucene' is the default
  358. search."
  359. (interactive "sBible Search: ")
  360. (when (> (length query) 0)
  361. (let* ((searchmode (completing-read "Search Mode: " '("lucene" "phrase") nil t "lucene")))
  362. (bm-open-search query searchmode))))
  363. ;;;###autoload
  364. (defun bible-search-mode-follow-verse ()
  365. "Follows the hovered verse in a `bible-search-mode' buffer,
  366. creating a new `bible-mode' buffer positioned at the specified verse."
  367. (interactive)
  368. (let* ((text (thing-at-point 'line t))
  369. book
  370. chapter
  371. verse)
  372. (string-match ".+ [0-9]?[0-9]?[0-9]?:[0-9]?[0-9]?[0-9]?:" text)
  373. (setq text (match-string 0 text))
  374. (string-match " [0-9]?[0-9]?[0-9]?:" text)
  375. (setq chapter (replace-regexp-in-string "[^0-9]" "" (match-string 0 text)))
  376. (string-match ":[0-9]?[0-9]?[0-9]?" text)
  377. (setq verse (replace-regexp-in-string "[^0-9]" "" (match-string 0 text)))
  378. (setq book (replace-regexp-in-string "[ ][0-9]?[0-9]?[0-9]?:[0-9]?[0-9]?[0-9]?:$" "" text))
  379. (bible-open book (string-to-number chapter) (string-to-number verse))))
  380. ;;;###autoload
  381. (defun bible-term-hebrew (term)
  382. "Queries user for a Strong's Hebrew Lexicon term."
  383. (interactive "sTerm: ")
  384. (bm-open-term-hebrew term))
  385. ;;;###autoload
  386. (defun bible-term-greek (term)
  387. "Queries user for a Strong's Greek Lexicon term."
  388. (interactive "sTerm: ")
  389. (bm-open-term-greek term))
  390. ;; (defun bible-term-morph (term morph-type)
  391. ;; "Queries user for a Strong's Greek Lexicon term."
  392. ;; (interactive "sTerm: ")
  393. ;; ;;; (message "bible-term-morph: %s:%s" term morph-type)
  394. ;; ;;; (bm-open-term-greek term)
  395. ;; )
  396. ;;;###autoload
  397. (defun bible-insert ()
  398. "Queries user to select a verse for insertion into the current buffer."
  399. (interactive)
  400. (let* ((completion-ignore-case t)
  401. (book-data (assoc (completing-read "Book: " bm-books nil t) bm-books))
  402. (chapter (when book-data (completing-read "Chapter: " (bm-list-number-range 1 (cdr book-data)) nil t)))
  403. (verse (when chapter (read-from-minibuffer "Verse: "))))
  404. (when verse
  405. (insert (string-trim
  406. (replace-regexp-in-string
  407. (regexp-opt `(,(concat "(" bm-module ")")))
  408. ""
  409. (bm-exec-diatheke (concat (car book-data) " " chapter ":" verse) nil "plain")))))))
  410. ;;;;; Support
  411. ;;;
  412. ;;; XXX I've magled this in an ad-hoc manner. It needs to be
  413. ;;; re-written so it is clearer (and correct, for that matter).
  414. (defun bm-exec-diatheke (query &optional filter format searchtype module)
  415. "Executes `diatheke' with specified query options, returning the output."
  416. (let ((module (or module bm-module)))
  417. (with-temp-buffer
  418. (let ((args (list "diatheke" nil (current-buffer) t "-b" module)))
  419. (if filter
  420. (setq filter (concat filter " avmws"))
  421. (setq filter "avmws"))
  422. (when filter (setq args (append args (list "-o" filter))))
  423. (when searchtype
  424. (setq args (append args (list "-s" (pcase searchtype ("lucene" "lucene") ("phrase" "phrase"))))))
  425. (setq args (append args (list "-f" (pcase format ("plain" "plain") (_ "internal")) "-k" query)))
  426. (message "%s" args)
  427. (apply 'call-process args))
  428. (buffer-string))))
  429. (defvar-local bm-chapter-title nil
  430. "Document text at start of chapter, mostly in Psalms,
  431. like `Of David' or the like.")
  432. ;;;
  433. ;;; Greek and Hebrew lexicon and morphology tooltip rendering.
  434. ;;;
  435. ;;; Hash tables for STRONGS definitions.
  436. (defvar greek-hash (make-hash-table :test 'equal))
  437. (defvar hebrew-hash (make-hash-table :test 'equal))
  438. ;;; Hash tables for morphologies. Three at present.
  439. (defvar robinson-hash (make-hash-table :test 'equal))
  440. (defvar packard-hash (make-hash-table :test 'equal))
  441. (defvar oshm-hash (make-hash-table :test 'equal))
  442. ;;; Use HTMLHREF format with diatheke, post-process to render html.
  443. (defun bm-morph-query (query module)
  444. "Executes `diatheke' to do morph query, renders HTML, returns string.
  445. Does some tweaking specific to morphology."
  446. (with-temp-buffer
  447. (let ((args (list "diatheke" nil (current-buffer) t "-b" module "-o" "m" "-f" "HTMLHREF" "-k" query)))
  448. (apply 'call-process args)
  449. (shr-render-region (point-min) (point-max))
  450. (replace-regexp-in-string
  451. "\n:" "" ; This makes the Packard morphology display look better.
  452. (replace-regexp-in-string
  453. "Part of Speech" "" ; This helps the Robinson display look better.
  454. (substring (buffer-string) (+ (length query) 1)) ; This tries to get rid of unnecessary query identifier.
  455. )))))
  456. ;;; Use "plain" format with diatheke.
  457. (defun bm-lex-query (query module)
  458. "Executes `diatheke' for query, plain format, returns string."
  459. ;; Get rid of query ID at front of string: ?????:
  460. (bm-exec-diatheke query nil "plain" nil module))
  461. (defun bm-lookup-strongs-greek (window object pos)
  462. "Look up Greek lexical data for object at point. If not found in hash table,
  463. get it from sword database, stash in hash table, and return data.
  464. Note: compiler warns about unused argument `window'."
  465. (let* ((query (get-text-property pos 'strong object))
  466. (match (string-match "[0-9]+" query)) ; Compiler warns about match.
  467. (lookup-key (match-string 0 query)))
  468. (and lookup-key
  469. (or (gethash lookup-key greek-hash)
  470. (puthash lookup-key (bm-lex-query lookup-key bm-short-greek-lexicon) greek-hash)))))
  471. (defun bm-hebrew-lex-query (query module)
  472. "Executes `diatheke' to do hebrew query, renders HTML, returns string."
  473. (with-temp-buffer
  474. (let ((args (list "diatheke" nil (current-buffer) t "-b" module "-o" "m" "-f" "HTMLHREF" "-k" query)))
  475. (apply 'call-process args)
  476. (shr-render-region (point-min) (point-max)))))
  477. (defun bm-lookup-strongs-hebrew (window object pos)
  478. "Look up Hebrew lexical data for object at point. If not found in hash table,
  479. get it from sword database, stash in hash table, and return data.
  480. Note: compiler warns about unused `window' argument."
  481. (let* ((query (get-text-property pos 'strong object))
  482. (match (string-match "[0-9]+" query)) ; Compiler warns about match.
  483. (lookup-key (match-string 0 query)))
  484. (and lookup-key
  485. (or (gethash lookup-key hebrew-hash)
  486. ;; Use PLAIN format for lookup. XXX directionality problems.
  487. (puthash lookup-key (bm-lex-query lookup-key bm-short-hebrew-lexicon) hebrew-hash)))))
  488. (defun bm-morph-database-lookup (query database hash)
  489. (or (gethash query hash)
  490. (puthash query (bm-morph-query query database) hash)))
  491. (defun bm-show-lex-morph (window object pos)
  492. (let* ((lex-morph-text "")
  493. (lex (get-text-property pos 'strong object))
  494. (lex-text
  495. (cond ((string-match "strong:G" lex)
  496. (bm-lookup-strongs-greek window object pos))
  497. ((string-match "strong:H" lex)
  498. (bm-lookup-strongs-hebrew window object pos)))))
  499. (let* ((morph (get-text-property pos 'morph object))
  500. (morph-text
  501. (cond ((null morph) "")
  502. ((string-match "robinson:" morph)
  503. (bm-morph-database-lookup (replace-regexp-in-string "robinson:" "" morph) "Robinson" robinson-hash))
  504. ((string-match "packard:" morph)
  505. (bm-morph-database-lookup (replace-regexp-in-string "packard:" "" morph) "Packard" packard-hash))
  506. ((string-match "oshm:" morph)
  507. (bm-morph-database-lookup (replace-regexp-in-string "oshm:" "" morph) "OSHM" oshm-hash)))))
  508. (when lex-text
  509. (setq lex-morph-text lex-text))
  510. (when morph-text
  511. (setq lex-morph-text (concat lex-morph-text "\n" morph-text)))
  512. ;; This prevents bogus command substitutions in the tooltip by
  513. ;; removing backslashes.
  514. (setq lex-morph-text (replace-regexp-in-string "\\\\" "" lex-morph-text))
  515. lex-morph-text)))
  516. (defun bm-process-word (item iproperties)
  517. "Word study. Add tooltips for definitions and morphologyl.
  518. Insert lemmas in buffer. Must be done after item is inserted in buffer."
  519. (let ((word (dom-text item))
  520. (morph (dom-attr item 'morph))
  521. (savlm (dom-attr item 'savlm))
  522. (divinename (dom-by-tag item 'divinename)))
  523. (insert word)
  524. (let ((refstart (- (point) (length word)))
  525. (refend (point)))
  526. ;; Red letter
  527. (when (plist-get iproperties 'jesus)
  528. (add-face-text-property refstart refend '(:foreground "red")))
  529. ;; Special case this. XXX Some modules do this differently.
  530. (when divinename
  531. (insert "LORD")
  532. (let* ((refstart (- (point) (length "LORD")))
  533. (refend (point))
  534. (strongs (dom-attr item 'savlm)))
  535. (string-match "strong:H.*" strongs)
  536. (let ((strongs-ref (match-string 0 strongs)))
  537. (add-face-text-property refstart refend 'bold)
  538. (put-text-property refstart refend 'keymap bm-hebrew-keymap)
  539. (put-text-property refstart refend 'help-echo 'bm-show-lex-morph)
  540. (put-text-property refstart refend 'strong strongs-ref))))
  541. ;; lexical definitions
  542. (when savlm
  543. (let ((matched nil))
  544. (cond ((string-match "strong:G.*" savlm) ; Greek
  545. (setq matched (match-string 0 savlm))
  546. (put-text-property refstart refend 'keymap bm-greek-keymap))
  547. ((string-match "strong:H.*" savlm) ; Hebrew
  548. (setq matched (match-string 0 savlm))
  549. (put-text-property refstart refend 'keymap bm-hebrew-keymap)))
  550. ;; Add help-echo, strongs reference for tooltips if match.
  551. (when matched
  552. (setq-local bm-has-strongs t)
  553. (put-text-property refstart refend 'help-echo 'bm-show-lex-morph)
  554. (put-text-property refstart refend 'strong matched))))
  555. ;; morphology
  556. (when morph
  557. (let ((matched nil))
  558. (cond ((string-match "robinson:.*" morph) ; Robinson Greek morphology
  559. (setq matched (match-string 0 morph)))
  560. ((string-match "packard:.*" morph) ; Packard Greek morphology --- LXX seems to use this
  561. (setq matched (match-string 0 morph)))
  562. ((string-match "oshm:.*" morph) ; OSHM Hebrew morphology
  563. (setq matched (match-string 0 morph)))
  564. (t nil
  565. ;;(message "Unknown morphology %s" morph)
  566. ))
  567. (when matched
  568. (setq-local bm-has-morphology t)
  569. (put-text-property refstart refend 'morph matched)
  570. (put-text-property refstart refend 'help-echo 'bm-show-lex-morph))))
  571. ;; Insert lemma into buffer. Lemma tag will be part of savlm item.
  572. (when (and bm-word-study-enabled savlm (string-match "lemma.*:.*" savlm))
  573. (dolist (word (split-string (match-string 0 savlm) " "))
  574. (setq word (replace-regexp-in-string "[.:a-zA-Z0-9]+" "" word))
  575. (insert " " word)
  576. (let ((refstart (- (point) 1 (length word)))
  577. (refend (point)))
  578. (add-face-text-property refstart refend '(:foreground "blue"))
  579. (put-text-property refstart refend 'keymap bm-lemma-keymap)))))))
  580. (defun bm-insert-domnode-recursive (node &optional iproperties notitle)
  581. "Recursively parses a domnode from `libxml-parse-html-region's usage on text
  582. produced by `bm-exec-diatheke'. Outputs text to active buffer
  583. with properties.
  584. In processing subnodes, each case will prepend a space if it needs it."
  585. (if (and bm-red-letter-enabled (equal (dom-attr node 'who) "Jesus"))
  586. ;; For red-letter display.
  587. (setq iproperties (plist-put iproperties 'jesus t))
  588. (setq iproperties nil))
  589. ;; (when (equal (dom-tag node) 'title)
  590. ;; ;; Space one line down so there's room for the title at the beginning.
  591. ;; (insert "\n"))
  592. (dolist (subnode (dom-children node))
  593. (cond ((null subnode) nil)
  594. ((stringp subnode)
  595. ;; Insert the subnode. Highlight the verse references.
  596. (insert subnode)
  597. ;; XXX this is still not quite right
  598. (let ((verse-start (string-match ".+?:[0-9]?[0-9]?[0-9]?:" subnode)))
  599. (when verse-start
  600. (let* ((verse-match (string-trim (match-string 0 subnode)))
  601. (verse-start-text (string-trim-left (substring subnode verse-start (length subnode))))
  602. ;; (subnode (concat (substring subnode 0 verse-start) verse-start-text))
  603. (start (- (point) 1 (length (string-trim-right verse-start-text)))))
  604. (add-face-text-property start (+ start (length (string-trim-right verse-match))) '(:foreground "purple"))))))
  605. ((eq (dom-tag subnode) 'title)
  606. (if notitle nil
  607. (progn
  608. (setq bm-chapter-title subnode))))
  609. ((eq (dom-tag subnode) 'body) (bm-insert-domnode-recursive subnode iproperties notitle))
  610. ((eq (dom-tag subnode) 'seg) ; NASB Module uses this to indicate OT quotations (and others?).
  611. (bm-insert-domnode-recursive subnode iproperties notitle))
  612. ((eq (dom-tag subnode) 'q) (bm-insert-domnode-recursive subnode iproperties notitle))
  613. ((eq (dom-tag subnode) 'p) (bm-insert-domnode-recursive subnode iproperties notitle))
  614. ((eq (dom-tag subnode) 'w) (insert " ") (bm-process-word subnode iproperties))
  615. ((eq (dom-tag subnode) 'milestone) (insert "\n"))
  616. ((eq (dom-tag subnode) 'transchange) ; Word inserted by translation, not in original, give visual indication.
  617. (let ((word (dom-text subnode)))
  618. (insert " " word)
  619. (add-face-text-property (- (point) (length word)) (point) '(:foreground "gray50")))))))
  620. (defvar bm-debugme nil)
  621. (setf bm-debugme nil)
  622. (defun bm-display (&optional verse)
  623. "Renders text for `bible-mode'"
  624. ;; Clear buffer and insert the result of calling bm-exec-diatheke.
  625. (setq buffer-read-only nil)
  626. (erase-buffer)
  627. (setq bm-chapter-title nil
  628. bm-has-strongs nil
  629. bm-has-morphology nil)
  630. (insert (bm-exec-diatheke (concat bm-current-book-name ":" (number-to-string bm-current-chapter))))
  631. ;; Parse the xml in the buffer into a DOM tree.
  632. (let ((html-dom-tree (libxml-parse-html-region (point-min) (point-max))))
  633. ;; Render the DOM tree into the buffer.
  634. (if (not bm-debugme)
  635. (progn
  636. (erase-buffer)
  637. ;; Looking for the "body" tag in the DOM node.
  638. (bm-insert-domnode-recursive (dom-by-tag html-dom-tree 'body) nil nil)
  639. (goto-char (point-min)))
  640. ;;; (shr-render-region (point-min) (point-max))
  641. ))
  642. ;; Remove the module name from the buffer.
  643. (while (search-forward (concat "(" bm-module ")") nil t)
  644. (replace-match ""))
  645. ;; Set the mode line of the biffer.
  646. (setq mode-name (concat "Bible ("
  647. bm-module
  648. (when bm-has-strongs " Lex")
  649. (when bm-has-morphology " Morph")
  650. ")"))
  651. ;; Deal with chapter titles (i.e. in Psalms)
  652. ;; N.B. This won't change a title inside a chapter, and so it
  653. ;; doesn't work with Psalm 119 where the acrostic letters get
  654. ;; printed as "titles".
  655. (when bm-chapter-title ; This gets set in bm-insert-domnode-recursive.
  656. (goto-char (point-min))
  657. (let ((title-text (dom-texts bm-chapter-title))
  658. (refstart (point-min))
  659. refend)
  660. ;; Insert and make bold the title.
  661. (when (string-or-null-p title-text)
  662. (insert title-text "\n")
  663. (setq refend (point))
  664. (put-text-property refstart refend 'face 'bold))))
  665. (setq buffer-read-only t)
  666. (goto-char (point-min))
  667. ;; If optional verse specification go to that verse.
  668. (when verse
  669. (goto-char (string-match (regexp-opt `(,(concat ":" (number-to-string verse) ":"))) (buffer-string)))
  670. (beginning-of-line)))
  671. (defun bm-list-biblical-modules ()
  672. "Returns a list of accessible Biblical Text modules."
  673. (let ((text (bm-exec-diatheke "modulelist" nil nil nil "system"))
  674. modules)
  675. (catch 'done
  676. (dolist (line (split-string text "\n"))
  677. (when (equal line "Commentaries:")
  678. (throw 'done nil))
  679. (when (not (equal "Biblical Texts:" line))
  680. (push (split-string line " : ") modules))))
  681. modules))
  682. (defun bm-pick-module ()
  683. (interactive)
  684. (message "Picking module at %s" (point))
  685. (let ((item (get-text-property (point) 'module)))
  686. (setq-default bm-module item)
  687. (bible-open)))
  688. (defconst bm-module-map (make-keymap))
  689. (define-key bm-module-map [mouse-1] 'bm-pick-module)
  690. (defun bm-display-available-modules ()
  691. (interactive)
  692. (let ((buf (get-buffer-create "Modules"))
  693. (mods (bm-list-biblical-modules)))
  694. (set-buffer buf)
  695. (module-select-mode)
  696. (setq buffer-read-only nil)
  697. (erase-buffer)
  698. (dolist (mod mods)
  699. (insert
  700. (propertize (car mod)
  701. 'face 'bold
  702. 'module (car mod)
  703. 'help-echo (concat "Select " (car mod))
  704. 'keymap bm-module-map)
  705. "\t\t"
  706. (format "%s\n" (cadr mod))))
  707. (setq buffer-read-only t)
  708. (goto-char (point-min))
  709. (pop-to-buffer buf nil t)))
  710. ;;;;; Bible Searching
  711. (defun bm-open-search (query searchmode)
  712. "Opens a search buffer of QUERY using SEARCHMODE."
  713. (let ((buf (get-buffer-create (concat "*bible-search-" (downcase bm-module) "-" query "*"))))
  714. (set-buffer buf)
  715. (bible-search-mode)
  716. (bm-display-search query searchmode bm-module)
  717. (pop-to-buffer buf nil t)))
  718. (defun bm-display-search (query searchmode mod)
  719. "Renders results of search QUERY from SEARHCMODE"
  720. (setq buffer-read-only nil)
  721. (erase-buffer)
  722. (let* ((result (string-trim (replace-regexp-in-string
  723. "Entries .+?--" ""
  724. (bm-exec-diatheke query nil "plain" searchmode mod))))
  725. (match 0)
  726. (matchstr "")
  727. (verses "")
  728. fullverses)
  729. (if (equal result (concat "none (" bm-module ")"))
  730. (insert "No results found." (when (equal searchmode "lucene") " Verify index has been build with mkfastmod."))
  731. (progn
  732. (while match
  733. (setq match (string-match ".+?:[0-9]?[0-9]?" result (+ match (length matchstr)))
  734. matchstr (match-string 0 result))
  735. (when match
  736. (setq verses (concat verses (replace-regexp-in-string ".+; " "" matchstr) ";"))))
  737. (setq match 0)
  738. (setq fullverses (bm-exec-diatheke verses))
  739. (insert fullverses)
  740. (sort-lines nil (point-min) (point-max))
  741. (let* ((html-dom-tree (libxml-parse-html-region (point-min) (point-max))))
  742. (erase-buffer)
  743. (bm-insert-domnode-recursive (dom-by-tag html-dom-tree 'body) nil nil)
  744. (goto-char (point-min))
  745. (while (search-forward (concat "(" bm-module ")") nil t)
  746. (replace-match "")))))
  747. (setq mode-name (concat "Bible Search (" bm-module ")"))
  748. (setq buffer-read-only t)
  749. (setq-local bm-search-query query)
  750. (setq-local bm-search-mode searchmode)
  751. (goto-char (point-min))))
  752. ;;;;; Terms
  753. ;;(defun bm-display-morphology (morph)
  754. ;; ;; xxx Do something here?
  755. ;; )
  756. (defun bm-display-term (termtype)
  757. (cl-do* ((text (buffer-string))
  758. (match (string-match "[0-9]+" text) (string-match "[0-9]+" text (match-end 0))))
  759. ((not match))
  760. (let* ((matchstr (match-string 0 text))
  761. (matchstrlen (length matchstr))
  762. (refstart (+ match 1))
  763. (refend (+ match 1 matchstrlen)))
  764. ;; This enables clicking on the Strong's numbers inside the term display.
  765. (add-face-text-property refstart refend `(:foreground "blue"))
  766. (cond ((eq termtype 'hebrew)
  767. (put-text-property refstart refend 'strong (concat "strong:H" matchstr))
  768. (put-text-property refstart refend 'keymap bm-hebrew-keymap))
  769. ((eq termtype 'greek)
  770. (put-text-property refstart refend 'strong (concat "strong:G" matchstr))
  771. (put-text-property refstart refend 'keymap bm-greek-keymap)))))
  772. (goto-char (point-min))
  773. (while (search-forward (concat "(" bm-module ")") nil t)
  774. (replace-match ""))
  775. (while (search-forward "()" nil t)
  776. (replace-match ""))
  777. (goto-char (point-min))
  778. (setq buffer-read-only t))
  779. (defun bm-open-term-hebrew (term)
  780. "Opens a buffer of the Strong's Hebrew TERM's definition"
  781. (let ((buf (get-buffer-create (concat "*bible-term-hebrew-" term "*"))))
  782. (set-buffer buf)
  783. (bible-term-hebrew-mode)
  784. (bm-display-term-hebrew term)
  785. (pop-to-buffer buf nil t)
  786. (fit-window-to-buffer)))
  787. (defun bm-open-term-greek (term)
  788. "Opens a buffer of the Strong's Greek TERM's definition"
  789. (let ((buf (get-buffer-create (concat "*bible-term-greek-" term "*"))))
  790. (set-buffer buf)
  791. (bible-term-greek-mode)
  792. (bm-display-term-greek term)
  793. (pop-to-buffer buf nil t)
  794. (fit-window-to-buffer)))
  795. ;;;
  796. ;;; Note: Hebrew display of terms is backwards; set bidi direction to
  797. ;;; 'left-to-right.
  798. (defun bm-display-term-hebrew (term)
  799. "Render the definition of the Strong's Hebrew TERM. Use
  800. bidi-paragraph-direction so the English text will render
  801. left-to-right. XXX Why doesn't this work for the tooltips?"
  802. (setq buffer-read-only nil)
  803. (erase-buffer)
  804. (insert (replace-regexp-in-string
  805. (regexp-opt `(,bm-hebrew-lexicon))
  806. ""
  807. (bm-exec-diatheke term nil "plain" nil bm-hebrew-lexicon)
  808. nil nil nil 7
  809. ))
  810. (bm-display-term 'hebrew)
  811. (setq bidi-paragraph-direction 'left-to-right))
  812. (defun bm-display-term-greek (term)
  813. "Render the definition of the Strong's Greek TERM."
  814. (setq buffer-read-only nil)
  815. (erase-buffer)
  816. (insert (replace-regexp-in-string
  817. (regexp-opt `(,bm-greek-lexicon))
  818. ""
  819. (bm-exec-diatheke term nil "plain" nil bm-greek-lexicon)
  820. nil nil nil 7
  821. ))
  822. ;; (insert "\n")
  823. (bm-display-term 'greek))
  824. (defun bm-set-location (book chapter &optional verse)
  825. "Sets the global chapter of the active `bible-mode' buffer."
  826. (setq-local bm-current-book book)
  827. (setq-local bm-current-book-name (car book))
  828. (setq-local bm-current-chapter chapter)
  829. (bm-display verse))
  830. ;;;;; Utilities
  831. (defun bm-list-number-range (min max &optional prefix)
  832. "Returns a list containing entries for each integer between min and max.
  833. Used in tandem with `completing-read' for chapter selection."
  834. (let ((range-list nil))
  835. (dotimes (num (1+ max))
  836. (when (>= num min)
  837. (push (cons (concat prefix (number-to-string num)) num) range-list)))
  838. (nreverse range-list)))
  839. ;;; Provides
  840. (provide 'bible-mode)
  841. ;; Local Variables:
  842. ;; read-symbol-shorthands: (("bm-" . "bible-mode-"))
  843. ;; End: