bible-mode.el 35 KB

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