bible.el 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824
  1. ;;; bible.el --- A Bible browsing application -*- lexical-binding: t; mode: EMACS-LISP; -*-
  2. ;; Copyright (c) 2025-2026 Fred Gilham
  3. ;; Author: Fred Gilham <fmgilham@gmail.com>
  4. ;; Version: 1.2.0
  5. ;; Keywords: files, text, hypermedia
  6. ;; Package-Requires: ((emacs "29.1") cl-lib dom shr)
  7. ;; URL: https://gitbot.homedns.org/fred/bible
  8. ;; This file is not part of GNU Emacs.
  9. ;; bible.el is free software; you can redistribute it and/or modify it
  10. ;; under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation; either version 3, or (at your option)
  12. ;; any later version.
  13. ;; bible.el is distributed in the hope that it will be useful, but
  14. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. ;; General Public License for more details.
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with this file; see the file LICENSE. If not, see
  19. ;; <https://www.gnu.org/licenses/>.
  20. ;;; Commentary:
  21. ;; Forked and extensively modified from package by Zacalot
  22. ;; Url: https://github.com/Zacalot/bible-mode
  23. ;; This package uses the `diatheke' program to browse and search
  24. ;; Biblical texts provided by the Sword project (https://crosswire.org).
  25. ;; Word study is also supported.
  26. ;;;; Usage
  27. ;; Use M-x `bible-open' to open a Bible buffer.
  28. ;; Use C-h f `bible' to see available keybindings.
  29. ;; The program also installs a Bible menu with keybindings and other
  30. ;; commands.
  31. ;; You may customize `bible-text' to set a default browsing
  32. ;; module, as well as `bible-word-study-enabled' to enable word
  33. ;; study by default.
  34. ;;;; Design
  35. ;; The idea here is to use the diatheke program to lookup text from
  36. ;; modules (biblical texts), then insert this text into buffers. The
  37. ;; main bible display uses diatheke's internal XML format. The whole
  38. ;; buffer gets parsed by `libxml-parse-html-region' to create a dom
  39. ;; tree. This gets parsed by `bible--insert-domnode-recursive' to render
  40. ;; the text into reading format.
  41. ;; The text is then decorated using information from the dom format as
  42. ;; necessary along with regular expressions to identify the verse
  43. ;; references. This is for red letters, purple highlighting of the
  44. ;; verse numbers, bold face of the divine name in the OT and so on.
  45. ;; If Strongs tags and/or morphological tags are present, they are
  46. ;; looked up in appropriate lexical and morphological modules and used
  47. ;; to add tooltips to the text so that hovering over words will bring
  48. ;; up a tooltip with information about the word. Clicking on a word
  49. ;; with lexical information will display that information in a "term"
  50. ;; buffer.
  51. ;;; Code:
  52. ;;;; Environment stuff
  53. ;; Turn off modes because we are greedy for pixels....
  54. (tool-bar-mode -1)
  55. (scroll-bar-mode -1)
  56. ;; eldoc isn't meaningful in this program, and this saves space in the
  57. ;; mode line.
  58. (global-eldoc-mode -1)
  59. ;;;; Requirements
  60. (require 'cl-lib)
  61. (require 'dom)
  62. (require 'shr)
  63. ;;;; Aliases for obsolete functions
  64. ;; dom-text and dom-texts declared obsolescent in Emacs 31. Check for
  65. ;; new function, retain backward compatibility.
  66. (defalias 'bible-dom-text (if (fboundp 'dom-inner-text) 'dom-inner-text 'dom-text))
  67. (defalias 'bible-dom-texts (if (fboundp 'dom-inner-text) 'dom-inner-text 'dom-texts))
  68. ;;;; Customization Variables
  69. (defgroup bible nil
  70. "Settings for `bible'."
  71. :group 'tools
  72. :link '(url-link "https://gitbot.homedns.org/fred/bible-mode"))
  73. (defcustom bible-text
  74. "KJV"
  75. "Customize default bible text module for Diatheke to query.
  76. \(For full list of installed modules, run `diatheke -b system -l bibliography'\)"
  77. :type '(choice (const :tag "None" nil)
  78. (string :tag "Bible text (e.g. \"KJV\")"))
  79. :local nil
  80. :group 'bible)
  81. (defcustom bible-commentary
  82. "Clarke"
  83. "Customize default commentary module for Diatheke to query.
  84. \(For full list of installed modules, run `diatheke -b system -l bibliography'\)"
  85. :type '(choice (const :tag "None" nil)
  86. (string :tag "Commentary (e.g. \"Clarke\")"))
  87. :local nil
  88. :group 'bible)
  89. ;; TODO: Not implememted yet (FMG 5-Mar-2026)
  90. (defcustom bible-font
  91. "Ezra SIL"
  92. "Default font for bible (not yet implemented)."
  93. :type '(string :tag "Font family name (e.g. \"Ezra SIL\")")
  94. :local t
  95. :group 'bible)
  96. (defcustom bible-sword-query
  97. "diatheke"
  98. "Specify program used to query sword modules.
  99. Must be some version of the sword library's diatheke program."
  100. :type '(string :tag "Sword library query executable (e.g. \"/usr/local/bin/diatheke\").")
  101. :local nil
  102. :group 'bible)
  103. (defcustom bible-greek-lexicon
  104. ;; AbbottSmithStrongs now has both links to lemmas and definitions
  105. ;; keyed by lemma. So we only need the AbbottSmithStrongs lexicon
  106. ;; and not the AbbottSmith lexicon.
  107. "AbbottSmithStrongs"
  108. "Lexicon used for displaying definitions of Greek words using Strong's codes."
  109. :type '(string :tag "Lexicon module (e.g. \"StrongsRealGreek\").")
  110. :local nil
  111. :group 'bible)
  112. (defcustom bible-use-index-for-lexicon t
  113. "Some lexicons are accessed by lemmas rather than Strong's numbers.
  114. Use an index to look up lemmas from Strong's numbers so these lexicons
  115. can be used."
  116. :type 'boolean
  117. :local nil
  118. :group 'bible)
  119. (defcustom bible-lexicon-index "AbbottSmithStrongs"
  120. "A module that consists of an index mapping Strong's numbers to Greek lemmas.
  121. The code is written to use the entries in AbbottSmithStrongs
  122. which are of the form
  123. <strong's number>: @LINK <greek lemma>"
  124. :type '(string :tag "Lexicon index.")
  125. :local nil
  126. :group 'bible)
  127. (defcustom bible-greek-lexicon-short
  128. "StrongsRealGreek"
  129. "Lexicon used for displaying definitions of Greek words in tooltips."
  130. :type '(string :tag "Lexicon module (e.g. \"StrongsRealGreek\").")
  131. :local nil
  132. :group 'bible)
  133. ;; HACK: The Hebrew lexicons differ on whether they accept keys of the
  134. ;; form `Hnnnn' or `nnnn'. The code does not yet handle this
  135. ;; correctly, so stick with the following. (FMG 5-Mar-2026)
  136. (defcustom bible-hebrew-lexicon
  137. "BDBGlosses_Strongs" ; This seems to work
  138. "Specify Lexicon used to display definitions of Hebrew words.
  139. Note that changing this may require changing some code.
  140. See `bible--display-lemma-hebrew'."
  141. :type '(string :tag "Lexicon module (e.g. \"BDBGlosses_Strongs\")")
  142. :local nil
  143. :group 'bible)
  144. (defcustom bible-hebrew-lexicon-short
  145. "StrongsRealHebrew"
  146. ;; "StrongsHebrew"
  147. "Lexicon used for displaying definitions of Hebrew words in tooltips."
  148. :type '(string :tag "Lexicon module (e.g. \"StrongsRealHebrew\")")
  149. :local nil
  150. :group 'bible)
  151. (defcustom bible-word-study-enabled
  152. nil
  153. "Display original language Lemma words if present in module.
  154. \(KJV New Testament has this.\)"
  155. :type 'boolean
  156. :local t
  157. :group 'bible)
  158. (defcustom bible-red-letter-enabled
  159. t
  160. "Display words of Jesus in red when module has that information."
  161. :type 'boolean
  162. :local t
  163. :group 'bible)
  164. (defcustom bible-show-diatheke-exec
  165. nil
  166. "Show the arguments by which diatheke is executed (mostly for debugging)."
  167. :type 'boolean
  168. :local nil
  169. :group 'bible)
  170. ;;;; Mode line formats for different kinds of buffers.
  171. (defvar bible-mode-line-format
  172. '("%e" mode-line-front-space
  173. mode-line-frame-identification mode-line-buffer-identification " "
  174. bible-text
  175. " "
  176. bible--current-book-name
  177. " " (:eval (number-to-string bible--current-chapter))
  178. " " (:eval (if bible--synced-p "Sync" ""))
  179. (:eval (when bible-search-range (concat " <" bible-search-range ">")))
  180. " " mode-line-modes mode-line-misc-info
  181. mode-line-end-spaces)
  182. "Mode line format for bible buffers.")
  183. (defvar bible-search-mode-line-format
  184. '("%e" mode-line-front-space
  185. mode-line-frame-identification mode-line-buffer-identification " "
  186. bible-search-text-this-query " " bible-search-word-this-query " "
  187. (:eval (when bible-search-range-this-query (concat "<" bible-search-range-this-query "> ")))
  188. (:eval (number-to-string bible-search-matches)) " matches"
  189. " " mode-line-modes mode-line-misc-info
  190. mode-line-end-spaces)
  191. "Mode line format for bible search buffers.")
  192. (defvar bible-term-mode-line-format
  193. '("%e" mode-line-front-space
  194. mode-line-frame-identification mode-line-buffer-identification " "
  195. ;; bible-term-language
  196. ;; " " (:eval bible-term-lemma)
  197. " " mode-line-modes mode-line-misc-info
  198. mode-line-end-spaces)
  199. "Mode line format for bible search buffers.")
  200. ;;;; Modes
  201. (define-derived-mode bible special-mode "Bible"
  202. "Mode for reading the Bible.
  203. \\{bible-map}"
  204. (buffer-disable-undo)
  205. (font-lock-mode t)
  206. (use-local-map bible-map)
  207. (setq-local mode-line-format bible-mode-line-format)
  208. (setq buffer-read-only t)
  209. (visual-line-mode t))
  210. (define-derived-mode bible-search-mode special-mode "Bible Search"
  211. "Mode for performing Bible searches.
  212. \\{bible-search-mode-map}"
  213. (buffer-disable-undo)
  214. (font-lock-mode t)
  215. (use-local-map bible-search-mode-map)
  216. (setq-local mode-line-format bible-search-mode-line-format)
  217. (setq buffer-read-only t)
  218. (visual-line-mode t))
  219. (define-derived-mode bible-term-mode special-mode "Bible Term"
  220. "Mode for researching terms in the Bible.
  221. \\{bible-term-mode-map}"
  222. (buffer-disable-undo)
  223. (font-lock-mode t)
  224. (use-local-map bible-term-mode-map)
  225. (setq-local mode-line-format bible-term-mode-line-format)
  226. (setq buffer-read-only t)
  227. (visual-line-mode t))
  228. (define-derived-mode bible-term-hebrew-mode bible-term-mode "Bible Term"
  229. "Mode for researching Hebrew terms in the Bible.
  230. \\{bible-term-hebrew-mode-map}"
  231. (setq-local bible-term-language "Hebrew"))
  232. (define-derived-mode bible-term-greek-mode bible-term-mode "Bible Term"
  233. "Mode for researching Greek terms in the Bible.
  234. \\{bible-term-greek-mode-map}"
  235. (setq-local bible-term-language "Greek"))
  236. (define-derived-mode bible-text-select-mode special-mode "Select Module"
  237. (buffer-disable-undo)
  238. (font-lock-mode t)
  239. (setq buffer-read-only t))
  240. ;;;; Keymaps
  241. ;; N.B. Bible Menu items appear in reverse order of their definition
  242. ;; below
  243. (defconst bible-map (make-sparse-keymap)
  244. "Keymap for bible.")
  245. (define-key bible-map [menu-bar bible]
  246. (cons "Bible" (make-sparse-keymap "Bible")))
  247. (define-key bible-map
  248. [menu-bar bible toggle-debug]
  249. '("Toggle debug-on-error" . toggle-debug-on-error))
  250. (define-key bible-map
  251. [menu-bar bible display-diatheke]
  252. '("Toggle diatheke display" . bible-toggle-display-diatheke))
  253. (define-key bible-map "d" 'bible-toggle-display-xml)
  254. (define-key bible-map
  255. [menu-bar bible display-xml]
  256. '("Toggle XML Display" . bible-toggle-display-xml))
  257. (define-key bible-map
  258. [menu-bar bible toggle-text-direction]
  259. '("Toggle text direction (for Hebrew display)" . bible-toggle-text-direction))
  260. (define-key bible-map
  261. [menu-bar bible toggle-tooltip-display]
  262. '("Toggle Tooltip Display" . bible-toggle-tooltips))
  263. (define-key bible-map
  264. [menu-bar bible sep]
  265. '(menu-item '"--"))
  266. ;;;;; Misc key bindings
  267. (define-key bible-map "T" 'bible-select-text)
  268. (define-key bible-map "C" 'bible-select-commentary)
  269. (define-key bible-map "w" 'bible-toggle-word-study)
  270. (define-key bible-map "l" 'bible-toggle-red-letter)
  271. (define-key bible-map "z" 'text-scale-adjust)
  272. (define-key bible-map
  273. [menu-bar bible zoom-text]
  274. '("Zoom Text" . text-scale-adjust))
  275. (define-key bible-map "x" 'bible-split-display)
  276. (define-key bible-map
  277. [menu-bar bible split-display]
  278. '("Split Display" . bible-split-display))
  279. (define-key bible-map "S" 'bible-toggle-buffer-sync)
  280. (define-key bible-map [menu-bar bible sync]
  281. '("Toggle Synchronize Buffer" . bible-toggle-buffer-sync))
  282. ;;;;; Navigation
  283. (define-key bible-map "p" 'bible-previous-chapter)
  284. (define-key bible-map
  285. [menu-bar bible previous-chapter]
  286. '("Previous Chapter" . bible-previous-chapter))
  287. (define-key bible-map "n" 'bible-next-chapter)
  288. (define-key bible-map
  289. [menu-bar bible next-chapter]
  290. '("Next Chapter" . bible-next-chapter))
  291. (define-key bible-map (kbd "TAB") 'bible-next-word)
  292. (define-key bible-map (kbd "M-<tab>") 'bible-previous-word)
  293. ;;;;; Direct jump
  294. (define-key bible-map "c" 'bible-select-chapter)
  295. (define-key bible-map
  296. [menu-bar bible select-chapter]
  297. '("Select Chapter" . bible-select-chapter))
  298. (define-key bible-map "b" 'bible-select-book)
  299. (define-key bible-map
  300. [menu-bar bible select-book]
  301. '("Select Book" . bible-select-book))
  302. (define-key bible-map
  303. [menu-bar bible sep]
  304. '(menu-item '"--"))
  305. ;; Deal with visual-line-mode navigation.
  306. (define-key bible-map "\C-n" 'next-logical-line)
  307. (define-key bible-map "\C-p" 'previous-logical-line)
  308. ;;;;; Search
  309. (define-key bible-map "/" 'bible-search)
  310. (define-key bible-map "s" 'bible-search)
  311. (define-key bible-map
  312. [menu-bar bible search]
  313. '("Search" . bible-search))
  314. (define-key bible-map "r" 'bible-set-search-range)
  315. (define-key bible-map
  316. [menu-bar bible range]
  317. '("Set Search Range" . bible-set-search-range))
  318. (define-key bible-map
  319. [menu-bar bible sepp]
  320. '(menu-item '"--"))
  321. (define-key bible-map
  322. [menu-bar bible sepp]
  323. '(menu-item '"--"))
  324. (define-key bible-map
  325. [menu-bar bible select-biblical-commentary]
  326. '("Select Commentary" . bible-display-available-commentaries))
  327. (define-key bible-map
  328. [menu-bar bible select-biblical-text]
  329. '("Select Text" . bible-display-available-texts))
  330. (defconst bible-search-mode-map (make-keymap))
  331. (define-key bible-search-mode-map "s" 'bible-search)
  332. (define-key bible-search-mode-map "w" 'bible-toggle-word-study)
  333. (define-key bible-search-mode-map "n" 'bible-next-search-item)
  334. (define-key bible-search-mode-map "p" 'bible-previous-search-item)
  335. (define-key bible-search-mode-map (kbd "RET") 'bible-search-mode-follow-verse)
  336. (define-key bible-search-mode-map [mouse-1] 'bible-search-mode-follow-verse)
  337. ;;;;; Term mode keymaps
  338. (defconst bible-term-mode-map (make-sparse-keymap))
  339. (define-key bible-term-mode-map "z" 'text-scale-adjust)
  340. (define-key bible-term-mode-map [mouse-1] 'bible-search-mode-follow-xref)
  341. (defconst bible-greek-keymap (make-sparse-keymap))
  342. (define-key bible-greek-keymap (kbd "RET") 'bible--display-greek)
  343. (define-key bible-greek-keymap [mouse-1] 'bible--display-greek)
  344. (defconst bible-hebrew-keymap (make-sparse-keymap))
  345. (define-key bible-hebrew-keymap (kbd "RET") 'bible--display-hebrew)
  346. (define-key bible-hebrew-keymap [mouse-1] 'bible--display-hebrew)
  347. (defconst bible-lemma-keymap (make-sparse-keymap))
  348. (define-key bible-lemma-keymap (kbd "RET")
  349. (lambda ()
  350. (interactive)))
  351. ;; Not used. Not really sure what to do here or if it's useful to do anything.
  352. (defconst bible-morph-keymap (make-sparse-keymap))
  353. (define-key bible-morph-keymap (kbd "RET")
  354. (lambda ()
  355. (interactive)
  356. ;; (let ((thing (thing-at-point 'word)))
  357. ;; (message "thing at point: %s" thing)
  358. ;; (message "morph property %s" (get-text-property 0 'field thing))
  359. ))
  360. ;;;;; Module choice keymaps.
  361. (defconst bible-text-map (make-keymap))
  362. (define-key bible-text-map [mouse-1] 'bible-pick-module)
  363. (define-key bible-text-map (kbd "RET") 'bible-pick-module)
  364. ;;;; Variable definitions
  365. (defconst bible--verse-regexp "\\(I \\|1 \\|II \\|2 \\|III \\|3 \\)??[a-zA-Z]+?[ \t\n][0-9]+[:][0-9]+")
  366. ;; Don't know how to get footnotes and scripture cross references yet.
  367. ;;(defconst bible-diatheke-filter-options " afilmnsvw")
  368. (defconst bible-diatheke-filter-options " almnvw")
  369. (defvar bible--text-buffers nil
  370. "List of Bible text buffers.")
  371. (defvar bible--commentary-buffers nil
  372. "List of commentary buffers.")
  373. (defvar bible--synced-buffers nil
  374. "List of buffers that are synchronized so that navigation in one applies
  375. to all of them.")
  376. (defvar-local bible--synced-p nil
  377. "Is this buffer syncronized?")
  378. (defvar bible--texts (lazy-completion-table bible--texts bible--list-biblical-texts))
  379. (defvar bible--commentaries (lazy-completion-table bible--commentaries bible--list-biblical-commentaries))
  380. ;; REVIEW: I believe these chapter counts aren't the same for all modules, e.g. JPS. (FMG 5-Mar-2026)
  381. (defvar bible--books
  382. '(;; Old Testament
  383. ("Genesis" . 50) ("Exodus" . 40) ("Leviticus" . 27) ("Numbers" . 36)
  384. ("Deuteronomy" . 34) ("Joshua" . 24) ("Judges" . 21) ("Ruth" . 4)
  385. ("I Samuel" . 31) ("II Samuel" . 24) ("I Kings" . 22) ("II Kings" . 25)
  386. ("1 Samuel" . 31) ("2 Samuel" . 24) ("1 Kings" . 22) ("2 Kings" . 25)
  387. ("I Chronicles" . 29) ("II Chronicles" . 36) ("Ezra" . 10) ("Nehemiah" . 13)
  388. ("1 Chronicles" . 29) ("2 Chronicles" . 36)
  389. ("Ezra" . 10) ("Nehemiah" . 13)
  390. ("Esther" . 10) ("Job" . 42) ("Psalms" . 150) ("Proverbs" . 31)
  391. ("Ecclesiastes" . 12) ("Song of Solomon" . 8) ("Isaiah" . 66) ("Jeremiah" . 52)
  392. ("Lamentations" . 5) ("Ezekiel" . 48) ("Daniel" . 12) ("Hosea" . 14)
  393. ("Joel" . 3) ("Amos" . 9) ("Obadiah" . 1) ("Jonah" . 4)
  394. ("Micah" . 7) ("Nahum" . 3) ("Habakkuk" . 3) ("Zephaniah" . 3)
  395. ("Haggai" . 2) ("Zechariah" . 14) ("Malachi" . 4)
  396. ;; New Testament
  397. ("Matthew" . 28) ("Mark" . 16) ("Luke" . 24) ("John" . 21)
  398. ("Acts" . 28) ("Romans" . 16)
  399. ("I Corinthians" . 16) ("II Corinthians" . 13)
  400. ("1 Corinthians" . 16) ("2 Corinthians" . 13)
  401. ("Galatians" . 6) ("Ephesians" . 6) ("Philippians" . 4) ("Colossians" . 4)
  402. ("I Thessalonians" . 5) ("II Thessalonians" . 3) ("I Timothy" . 6) ("II Timothy" . 4)
  403. ("1 Thessalonians" . 5) ("2 Thessalonians" . 3) ("1 Timothy" . 6) ("2 Timothy" . 4)
  404. ("Titus" . 3) ("Philemon" . 1) ("Hebrews" . 13) ("James" . 5)
  405. ("I Peter" . 5) ("II Peter" . 3) ("I John" . 5) ("II John" . 1)
  406. ("1 Peter" . 5) ("2 Peter" . 3) ("1 John" . 5) ("2 John" . 1)
  407. ("III John" . 1) ("Jude" . 1) ("Revelation of John" . 22)
  408. ("3 John" . 1))
  409. "A-list of name / chapter count for Bible books.")
  410. ;; These abbreviations are used to follow cross-references in commentaries and lexicons.
  411. ;; Abbreviations from NETnote module and Clarke module (commentaries).
  412. ;; Abbreviations from some lexicons.
  413. ;; Standard abbreviations come first.
  414. (defvar bible--book-name-abbreviations
  415. '(;; Old Testament
  416. ("Gen" . "Genesis") ("Ge" . "Genesis")
  417. ("Exod" . "Exodus") ("Ex" . "Exodus") ("Exo" . "Exodus")
  418. ("Lev" . "Leviticus") ("Le" . "Leviticus")
  419. ("Num" . "Numbers") ("Nu" . "Numbers")
  420. ("Deut" . "Deuteronomy") ("De" . "Deuteronomy") ("Deu" . "Deuteronomy")
  421. ("Josh" . "Joshua") ("Js" . "Joshua") ("Jos" . "Joshua")
  422. ("Judg" . "Judges") ("Jg" . "Judges") ("Jdg" . "Judges")
  423. ("Ru" . "Ruth") ("Rut" . "Ruth")
  424. ("1 Sam" . "I Samuel") ("1 Samuel" . "I Samuel") ("I Sa" . "I Samuel") ("1 Sa" . "I Samuel") ("1Sam" . "I Samuel")
  425. ("2 Sam" . "II Samuel") ("2 Samuel" . "II Samuel") ("II Sa" . "II Samuel") ("2 Sa" . "II Samuel") ("2Sam" . "II Samuel")
  426. ("1 Kgs" . "I Kings") ("1 Kings" . "I Kings") ("I Ki" . "I Kings") ("1 Ki" . "I Kings") ("1Ki" . "I Kings")
  427. ("2 Kgs" . "II Kings") ("2 Kings" . "II Kings") ("II Ki" . "II Kings") ("2 Ki" . "II Kings") ("2Ki" . "II Kings")
  428. ("1 Chr" . "I Chronicles") ("1 Chronicles" . "I Chronicles") ("I Ch" . "I Chronicles") ("1 Ch" . "I Chronicles")
  429. ("2 Chr" . "II Chronicles") ("2 Chronicles" . "II Chronicles") ("II Ch" . "II Chronicles") ("2 Ch" . "II Chronicles")
  430. ("Ezr" . "Ezra")
  431. ("Neh" . "Nehemiah") ("Ne" . "Nehemiah")
  432. ("Esth" . "Esther") ("Es" . "Esther") ("Est" . "Esther")
  433. ("Jb" . "Job")
  434. ("Ps" . "Psalms") ("Psa" . "Psalms")
  435. ("Prov" . "Proverbs") ("Pr" . "Proverbs") ("Pro" . "Proverbs")
  436. ("Eccl" . "Ecclesiastes") ("Ec" . "Ecclesiastes") ("Ecc" . "Ecclesiastes")
  437. ("Song" . "Song of Solomon") ("So" . "Song of Solomon") ("Sol" . "Song of Solomon")
  438. ("Isa" . "Isaiah") ("Is" . "Isaiah")
  439. ("Jer" . "Jeremiah") ("Je" . "Jeremiah")
  440. ("Lam" . "Lamentations") ("La" . "Lamentations")
  441. ("Ezek" . "Ezekiel") ("Ez" . "Ezekiel") ("Eze" . "Ezekiel")
  442. ("Dan" . "Daniel") ("Da" . "Daniel")
  443. ("Hos" . "Hosea") ("Ho" . "Hosea")
  444. ("Joe" . "Joel")
  445. ("Am" . "Amos") ("Amo" . "Amos")
  446. ("Obad" . "Obadiah") ("Ob" . "Obadiah") ("Oba" . "Obadiah")
  447. ("Jon" . "Jonah")
  448. ("Mic" . "Micah") ("Mi" . "Micah")
  449. ("Nah" . "Nahum") ("Na" . "Nahum")
  450. ("Hab" . "Habakkuk") ("Ha" . "Habakkuk")
  451. ("Zeph" . "Zephaniah") ("Zep" . "Zephaniah")
  452. ("Hag" . "Haggai")
  453. ("Zech" . "Zechariah") ("Ze" . "Zechariah") ("Zac" . "Zechariah") ;; Is the last one correct??
  454. ("Mal" . "Malachi")
  455. ;; New Testament
  456. ("Mt" . "Matthew") ("Mat" . "Matthew") ("Matt" . "Matthew")
  457. ("Mk" . "Mark") ("Mar" . "Mark")
  458. ("Lk" . "Luke") ("Luk" . "Luke")
  459. ("Jn" . "John") ("Jo" . "John") ("Joh" . "John")
  460. ("Ac" . "Acts") ("Act" . "Acts")
  461. ("Rom" . "Romans") ("Ro" . "Romans")
  462. ("1 Cor" . "I Corinthians") ("1 Corintihans" . "I Corinthians") ("I Co" . "I Corinthians") ("1 Co" . "I Corinthians")
  463. ("ICor" . "I Corinthians") ("1Cor" . "I Corinthians")
  464. ("2 Cor" . "II Corinthians") ("2 Corinthians" . "II Corinthians") ("II Co" . "II Corinthians") ("2 Co" . "II Corinthians")
  465. ("IICor" . "II Corinthians") ("2Cor" . "II Corinthians")
  466. ("Gal" . "Galatians") ("Ga" . "Galatians")
  467. ("Eph" . "Ephesians")
  468. ("Phil" . "Philippians") ("Phl" . "Philippians")
  469. ("Col" . "Colossians")
  470. ("1 Thess" . "I Thessalonians") ("1 Thessalonians" . "I Thessalonians") ("I Th" . "I Thessalonians") ("1 Th" . "I Thessalonians")
  471. ("IThess" . "I Thessalonians") ("1Thes" . "I Thessalonians") ("1Thess" . "I Thessalonians")
  472. ("2 Thess" . "II Thessalonians") ("2 Thessalonians" . "II Thessalonians") ("II Th" . "II Thessalonians") ("2 Th" . "II Thessalonians")
  473. ("IIThess" . "II Thessalonians") ("2Thes" . "II Thessalonians") ("2Thess" . "II Thessalonians")
  474. ("1 Tim" . "I Timothy") ("1 Timothy" . "I Timothy") ("I Ti" . "I Timothy") ("1 Ti" . "I Timothy") ("ITim" . "I Timothy")
  475. ("1Tim" . "I Timothy")
  476. ("2 Tim" . "II Timothy") ("2 Timothy" . "II Timothy") ("II Ti" . "II Timothy") ("2 Ti" . "II Timothy") ("IITim" . "II Timothy")
  477. ("2Tim" . "II Timothy")
  478. ("Tit" . "Titus")
  479. ("Phlm" . "Philemon") ("Phm" . "Philemon") ("Plm" . "Philemon")
  480. ("Heb" . "Hebrews") ("He" . "Hebrews")
  481. ("Jas" . "James") ("Ja" . "James") ("Jam" . "James")
  482. ("1 Pet" . "I Peter") ("1 Peter" . "I Peter") ("I Pe" . "I Peter") ("1 Pe" . "I Peter") ("IPet" . "I Peter") ("1Pet" . "I Peter")
  483. ("2 Pet" . "II Peter") ("2 Peter" . "II Peter") ("II Pe" . "II Peter") ("2 Pe" . "II Peter") ("IIPet" . "II Peter") ("2Pet" . "II Peter")
  484. ("1 Jn" . "I John") ("1 John" . "I John") ("I Jo" . "I John") ("1 Jo" . "I John") ("IJohn" . "I John") ("1Jn" . "I John")
  485. ("2 Jn" . "I John") ("2 John" . "II John") ("II Jo" . "II John") ("2 Jo" . "II John") ("IIJohn" . "II John") ("2Jn" . "I John")
  486. ("3 Jn" . "I John") ("3 John" . "III John") ("III Jo" . "III John") ("3 Jo" . "III John") ("IIIJohn" . "III John") ("3Jn" . "I John")
  487. ("Ju" . "Jude") ("Jde" . "Jude")
  488. ("Rev" . "Revelation of John") ("Re" . "Revelation of John"))
  489. "A-list of abbreviations for Bible books.")
  490. ;;;;; Book / chapter
  491. (defvar-local bible--current-book (assoc "Genesis" bible--books)
  492. "Current book data (name . chapter).")
  493. (defvar-local bible--current-book-name "Genesis"
  494. "Current book name.")
  495. (defvar-local bible--current-chapter 1
  496. "Current book chapter number.")
  497. ;;;;; Search / query
  498. (defvar-local bible-query nil
  499. "Search query associated with the buffer.")
  500. (defvar-local bible-search-mode "phrase"
  501. "Search mode: either `lucene', `phrase', `regex' or `multiword'.")
  502. (defvar bible-search-range nil)
  503. ;;;;; Lexemes / morphemes
  504. (defvar-local bible-term-language nil
  505. "Displaying terms of this language.")
  506. (defvar-local bible-term-lemma nil
  507. "Lemma for term mode line.")
  508. (defvar-local bible-has-lexemes nil
  509. "Set if the module being displayed has lexical entries availabile.")
  510. (defvar-local bible-has-morphemes nil
  511. "Set if the module being displayed has morphemes availabile.")
  512. (defvar-local bible-text-direction 'left-to-right)
  513. (defvar-local bible-debugme nil
  514. "Make text show up as XML when set.")
  515. (defvar bible-use-tooltips t)
  516. (setq tooltip-delay 1)
  517. (setq tooltip-short-delay .5)
  518. (setq use-system-tooltips nil)
  519. (defvar-local bible-search-query nil
  520. "Query used in toggles (word study and red letter).")
  521. (defvar-local bible-chapter-title nil
  522. "Text preceding start of chapter.
  523. Mostly in Psalms, like `Of David' or the like.")
  524. (defvar-local bible-level "0"
  525. "Used by some modules for indentation and line breaks.")
  526. ;;;; Greek and Hebrew lexeme and morpheme tooltip rendering.
  527. ;;;;; Hash tables for Lexical definitions.
  528. (defvar bible-hash-greek (make-hash-table :test 'equal :size 10000))
  529. (defvar bible-hash-hebrew (make-hash-table :test 'equal :size 10000))
  530. ;;;;; Hash tables for tooltips.
  531. (defvar lex-hash (make-hash-table :test 'equal :size 10000))
  532. (defvar morph-hash (make-hash-table :test 'equal :size 10000))
  533. (defvar bible-outline-strings
  534. '(;;(". ." . ".")
  535. (" I. ." . "\nI.")
  536. (" II. ." . " II.")
  537. (" III. ." . " III.")
  538. (" IV. ." . " IV.")
  539. (" V. ." . " V.")
  540. ("1. ." . "\n 1.")
  541. ("2. ." . "2.")
  542. ("3. ." . "3.")
  543. ("4. ." . "4.")
  544. ("5. ." . "5.")
  545. ("6. ." . "6.")
  546. ("7. ." . "7.")
  547. ("8. ." . "8.")
  548. ("9. ." . "9.")
  549. ("a. ." . "\n a.")
  550. ("(a)." . "\n (a).")
  551. ("b. ." . " b.")
  552. ("c. ." . " c.")
  553. ("d. ." . " d.")
  554. ("e. ." . " e.")
  555. ("f. ." . " f.")
  556. ("g. ." . " g.")
  557. ("h. ." . " h.")
  558. (" . " . ". ")
  559. ("\n\n" . "\n")))
  560. ;;;;; Variables for mode-line-format for search buffers.
  561. (defvar-local bible-search-word-this-query "")
  562. (defvar-local bible-search-text-this-query "")
  563. (defvar-local bible-search-range-this-query nil)
  564. (defvar-local bible-search-matches 0)
  565. ;;;; Functions
  566. ;;;;; Keymap helpers
  567. (defun bible-toggle-display-diatheke ()
  568. "Toggle diatheke args display."
  569. (interactive)
  570. (setq bible-show-diatheke-exec (not bible-show-diatheke-exec))
  571. (message ""))
  572. (defun bible-next-search-item ()
  573. "Go to next item in list of found verses."
  574. (interactive)
  575. (search-forward-regexp bible--verse-regexp))
  576. (defun bible-previous-search-item ()
  577. "Go to previous item in list of found verses."
  578. (interactive)
  579. (search-backward-regexp bible--verse-regexp))
  580. (defun bible-toggle-display-xml ()
  581. "Toggle XML display."
  582. (interactive)
  583. (setq-local bible-debugme (not bible-debugme))
  584. (bible--display))
  585. (defun bible-toggle-text-direction ()
  586. "Switch between left-to-right and right-to-left text direction."
  587. (interactive)
  588. (if (eq bible-text-direction 'left-to-right)
  589. (setq-local bible-text-direction 'right-to-left)
  590. (setq-local bible-text-direction 'left-to-right))
  591. (setq-local bidi-paragraph-direction bible-text-direction))
  592. (defun bible-toggle-tooltips ()
  593. "Toggle use of tooltips to display lexical/morphological items."
  594. (interactive)
  595. (setq bible-use-tooltips (not bible-use-tooltips))
  596. (tooltip-mode 'toggle)
  597. (setq tooltip-resize-echo-area (not bible-use-tooltips))
  598. (setq bible-show-diatheke-exec (and bible-show-diatheke-exec bible-use-tooltips)) ; Don't conflict with echo area
  599. (message ""))
  600. ;;;;; Commands (interactive)
  601. (defun bible-open (&optional book-name chapter verse module)
  602. "Create and open a `bible' buffer.
  603. Optional arguments BOOK-NAME, CHAPTER and VERSE, when supplied, give the
  604. starting verse reference for the buffer. If no optional location
  605. arguments are supplied, Genesis 1:1 is used. Optional argument MODULE
  606. specifies the module to use."
  607. (interactive)
  608. (with-current-buffer (get-buffer-create (generate-new-buffer-name "*bible*"))
  609. (bible)
  610. (when module (setq-default bible-text module))
  611. (setq-local bible-text (default-value 'bible-text))
  612. (bible--set-location
  613. (assoc (or book-name "Genesis") bible--books)
  614. (or chapter 1)
  615. verse)
  616. (cl-pushnew (current-buffer) bible--text-buffers)
  617. (set-window-buffer (get-buffer-window (current-buffer)) (current-buffer))))
  618. ;;;;;; Navigation
  619. (defun bible--do-set-location (book chapter &optional verse)
  620. (setq-local bible--current-book book)
  621. (setq-local bible--current-book-name (car book))
  622. (setq-local bible--current-chapter chapter)
  623. (bible--display verse))
  624. (defun bible--set-location (book chapter &optional verse)
  625. "Set the BOOK, CHAPTER and optionally VERSE of the active `bible' buffer."
  626. (let ((buffer (current-buffer)))
  627. (bible--do-set-location book chapter verse)
  628. (when (cl-find buffer bible--synced-buffers)
  629. (save-excursion
  630. (dolist (buf bible--synced-buffers)
  631. (unless (eq buf buffer)
  632. (with-current-buffer buf
  633. (bible--do-set-location book chapter verse))))))))
  634. (defun bible-next-chapter ()
  635. "Page to the next chapter for the active `bible' buffer and
  636. for any synchronized buffers."
  637. (interactive)
  638. (let* ((book-chapters (cdr bible--current-book))
  639. (chapter (min book-chapters (1+ bible--current-chapter))))
  640. (bible--set-location bible--current-book chapter)))
  641. (defun bible-previous-chapter ()
  642. "Page to the previous chapter for the active `bible' buffer and
  643. for any synchronized buffers."
  644. (interactive)
  645. (bible--set-location bible--current-book (max 1 (1- bible--current-chapter))))
  646. (defun bible-next-word ()
  647. "Move forward a word, taking into account the relevant text properties."
  648. (interactive)
  649. (unless (eobp)
  650. (let ((next-change (text-property-search-forward 'strong nil nil t)))
  651. (when next-change
  652. (goto-char (1- (prop-match-end next-change)))))))
  653. (defun bible-previous-word ()
  654. "Move back a word, taking into account the relevant text properties."
  655. (interactive)
  656. (unless (bobp)
  657. (let ((previous-change (text-property-search-backward 'strong)))
  658. (when previous-change
  659. (goto-char (prop-match-beginning previous-change))))))
  660. ;;;;;; Select Location
  661. (defun bible-select-book ()
  662. "Ask user for a new book and chapter for the current `bible' buffer."
  663. (interactive)
  664. (let* ((completion-ignore-case t)
  665. (book-data (assoc (completing-read "Book: " bible--books nil t) bible--books))
  666. (book-data-string (car book-data))
  667. (chapter (string-to-number
  668. (completing-read
  669. "Chapter [1]: "
  670. (bible--list-number-range 1 (cdr book-data)) nil t nil nil "1"))))
  671. (pcase (aref book-data-string 0)
  672. (?1 (setq book-data (cons (concat "I" (substring book-data-string 1)) (cdr book-data))))
  673. (?2 (setq book-data (cons (concat "II" (substring book-data-string 1)) (cdr book-data))))
  674. (?3 (setq book-data (cons (concat "III" (substring book-data-string 1)) (cdr book-data)))))
  675. (bible--set-location book-data chapter)))
  676. (defun bible-select-chapter ()
  677. "Ask user for a new chapter for the current `bible' buffer."
  678. (interactive)
  679. (let* ((book-chapters (cdr bible--current-book))
  680. (chapter (string-to-number
  681. (completing-read
  682. "Chapter [1]: "
  683. (bible--list-number-range 1 book-chapters) nil t nil nil "1"))))
  684. (when chapter
  685. (bible--set-location bible--current-book chapter))))
  686. ;;;;;; Select modules
  687. ;; Choose a module.
  688. (defun bible-pick-module ()
  689. "Keymap action function---select module that the user chooses."
  690. (interactive)
  691. (let ((item (get-text-property (point) 'module)))
  692. (bible-open bible--current-book-name bible--current-chapter 1 item)))
  693. (defun bible-select-text ()
  694. "Ask user for a new text module for the current `bible' buffer."
  695. (interactive)
  696. (let ((text (completing-read "Text: " bible--texts)))
  697. (unless (string= text "")
  698. (setq-default bible-text text)
  699. (setq-local bible-text text)
  700. (bible--display))))
  701. (defun bible-select-commentary ()
  702. "Ask user for a new text module for the current `bible' buffer."
  703. (interactive)
  704. (let ((commentary (completing-read "Commentary: " bible--commentaries)))
  705. (unless (string= commentary "")
  706. (setq-local bible-text commentary)
  707. (bible--display))))
  708. ;;;;;; Toggles
  709. (defun bible-toggle-word-study ()
  710. "Toggle the inclusion of word study for the active `bible' buffer."
  711. (interactive)
  712. (setq bible-word-study-enabled (not bible-word-study-enabled))
  713. (bible--display))
  714. (defun bible-toggle-red-letter ()
  715. "Toggle red letter mode for the active `bible' buffer."
  716. (interactive)
  717. (setq bible-red-letter-enabled (not bible-red-letter-enabled))
  718. (bible--display))
  719. (defun bible-toggle-buffer-sync ()
  720. "Either add or remove the current buffer from the
  721. `bible--synced-buffers' list."
  722. (interactive)
  723. (let ((buffer (current-buffer)))
  724. (if bible--synced-p
  725. (progn
  726. (setq bible--synced-buffers (cl-delete buffer bible--synced-buffers))
  727. (setq-local bible--synced-p nil))
  728. (cl-pushnew buffer bible--synced-buffers)
  729. (setq-local bible--synced-p t))
  730. (force-mode-line-update)))
  731. (defun bible-split-display ()
  732. "Copy the active `bible' buffer into a new buffer in another window."
  733. (interactive)
  734. (split-window-right)
  735. (balance-windows)
  736. (other-window 1)
  737. (bible-open bible--current-book-name bible--current-chapter 1 bible-text))
  738. ;;;;;; Search helpers
  739. (defun bible-set-search-range ()
  740. "Ask user for a new text module for the current `bible' buffer."
  741. (interactive)
  742. (let ((range (read-string "Range (<return> to clear): ")))
  743. (if (string-equal range "")
  744. (setq bible-search-range nil)
  745. (setq bible-search-range range))))
  746. (defun bible-search (query)
  747. "Search for a QUERY: a word or phrase.
  748. Asks the user for type of search: either `lucene', `phrase', `regex'
  749. or `multiword'. `lucene' is the default search.
  750. `lucene' mode requires an index to be built using the `mkfastmod' program."
  751. (interactive "sBible Search: ")
  752. (when (> (length query) 0)
  753. (let ((searchmode (completing-read "Search Mode: " '("lucene" "phrase" "regex" "multiword") nil t "lucene")))
  754. (bible--open-search query searchmode (buffer-local-value 'bible-text (current-buffer))))))
  755. (defun bible-search-mode-follow-verse ()
  756. "Follow the hovered verse in a `bible-search-mode' buffer.
  757. Create a new `bible' buffer positioned at the selected verse."
  758. (interactive)
  759. (let* ((text (thing-at-point 'line t))
  760. book
  761. chapter
  762. verse)
  763. (when (string-match bible--verse-regexp text)
  764. (setq text (match-string 0 text))
  765. (string-match "I?I?I? ?[A-Z]?[a-z]* " text)
  766. (setq book (match-string 0 text))
  767. (string-match "[0-9]?[0-9]?[0-9]?:" text)
  768. (setq chapter (substring (match-string 0 text) 0 (1- (length (match-string 0 text)))))
  769. (string-match ":[0-9]?[0-9]?[0-9]?" text)
  770. (setq verse (substring (match-string 0 text) 1))
  771. (bible-open (string-trim book) (string-to-number chapter) (string-to-number verse)) bible-text)))
  772. (defun bible-search-mode-follow-xref ()
  773. "Follow the hovered verse in a bible term buffer.
  774. Create a new `bible' buffer positioned at the specified verse.
  775. Handle abbreviations."
  776. (interactive)
  777. (let* ((xref (get-text-property (point) 'xref))
  778. (verse-ref (split-string xref))
  779. book-abbrev
  780. chapter-verse)
  781. (message "xref: %s" xref)
  782. (message "Verse-ref: %s" verse-ref)
  783. (cond ((= (length verse-ref) 2) ; Mat 5 or the like
  784. (setq book-abbrev (car verse-ref)
  785. chapter-verse (split-string (cadr verse-ref) ":")))
  786. ((= (length verse-ref) 3) ; II Cor 3:17 or the like
  787. (setq book-abbrev (concat (car verse-ref) " " (cadr verse-ref))
  788. chapter-verse (split-string (caddr verse-ref) ":"))))
  789. ;; Use book abbreviation if present or try whatever is in verse-ref.
  790. (let ((book (or (alist-get book-abbrev bible--book-name-abbreviations nil nil #'string-equal-ignore-case) (car verse-ref)))
  791. (chapter (car chapter-verse))
  792. (verse (cadr chapter-verse)))
  793. (bible-open (string-trim book) (string-to-number chapter) (string-to-number verse) (default-value 'bible-text)))))
  794. ;;;;;; User visible actions.
  795. ;; These can be called interactively if you know the Strong's number
  796. ;; you want to look up.
  797. (defun bible-term-hebrew (term)
  798. "Query user for a Strong's Hebrew Lexicon TERM."
  799. (interactive "sTerm: ")
  800. (bible--open-term-hebrew term))
  801. (defun bible-term-greek (term)
  802. "Query user for a Strong's Greek Lexicon TERM."
  803. (interactive "sTerm: ")
  804. (bible--open-term-greek term))
  805. ;; Interactively insert a verse into an arbitrary current buffer.
  806. (defun bible-insert ()
  807. "Query user to select a verse for insertion into the current buffer."
  808. (interactive)
  809. (let* ((completion-ignore-case t)
  810. (book-data (assoc (completing-read "Book: " bible--books nil t) bible--books))
  811. (chapter (when book-data (completing-read "Chapter: " (bible--list-number-range 1 (cdr book-data)) nil t "1" nil "1")))
  812. (verse (when chapter (read-from-minibuffer "Verse: ")))
  813. (query (concat (car book-data) " " chapter ":" verse))
  814. (args (list bible-sword-query nil (current-buffer) t "-b" bible-text "-f" "plain" "-k" query)))
  815. (apply #'call-process args)))
  816. ;;;;;; Support (internal)
  817. ;;;;;;; Diatheke interface
  818. (defun bible--exec-diatheke (query &optional filter format module)
  819. "Execute `diatheke' with specified QUERY options.
  820. FILTER is the Diatheke filter argument. FORMAT is either plain or
  821. the default of internal. MODULE is the text module to use. Returns
  822. string containing query result."
  823. (let ((module (or module bible-text)))
  824. (with-temp-buffer
  825. (let ((args (list bible-sword-query nil (current-buffer) t "-b" module)))
  826. (if filter
  827. (setq filter (concat filter bible-diatheke-filter-options))
  828. (setq filter bible-diatheke-filter-options))
  829. (setq args (append args (list "-o" filter)))
  830. (setq args (append args (list "-f" (pcase format ("plain" "plain") (_ "internal")) "-k" query)))
  831. (when bible-show-diatheke-exec
  832. (message "%s" args))
  833. (apply #'call-process args))
  834. (buffer-string))))
  835. (defun bible--diatheke-search (query searchtype &optional format module)
  836. "Execute `diatheke' on QUERY with SEARCHTYPE.
  837. Optional argument FORMAT is either plain or the default of internal.
  838. MODULE is the text module to use and defaults to the current module."
  839. (with-temp-buffer
  840. (let ((args (list bible-sword-query nil (current-buffer) t "-b" (or module bible-text))))
  841. (setq args (append args (list "-s" (pcase searchtype
  842. ("lucene" "lucene")
  843. ("phrase" "phrase")
  844. ("regex" "regex")
  845. ("multiword" "multiword")))))
  846. (when bible-search-range (setq args (append args (list "-r" bible-search-range))))
  847. (setq args (append args (list "-f" (pcase format ("plain" "plain") (_ "internal")) "-k" query)))
  848. (when bible-show-diatheke-exec
  849. (message "%s" args))
  850. (apply #'call-process args))
  851. (buffer-string)))
  852. ;; Use HTMLHREF format with diatheke, post-process to render html.
  853. (defun bible--morph-query (query module)
  854. "Execute `diatheke' to do morph QUERY, using MODULE.
  855. Render HTML, return string. Do some tweaking specific to morphology."
  856. (with-temp-buffer
  857. (let ((args (list bible-sword-query nil (current-buffer) t "-b" module "-o" "m" "-f" "HTMLHREF" "-k" query)))
  858. (when bible-show-diatheke-exec
  859. (message "%s" args))
  860. (apply #'call-process args)
  861. (shr-render-region (point-min) (point-max))
  862. (format-replace-strings
  863. '(("\n:" . "") ; This makes the Packard morphology display look better.
  864. ("Part of Speech" . "")) ; This helps the Robinson display look better.
  865. nil (point-min) (point-max))
  866. (substring (buffer-string) (1+ (length query)))))) ; This tries to get rid of unnecessary query identifier.
  867. ;; Use "plain" format with diatheke.
  868. (defun bible--lex-query (query module)
  869. "Execute `diatheke' for QUERY, using MODULE.
  870. Plain format, returns string."
  871. (bible--exec-diatheke query nil "plain" module))
  872. ;;;;;; Lexicon processing
  873. ;; The Greek lexical definitions are done using the HTMLHREF output
  874. ;; format so they come out looking nice and having clickable
  875. ;; cross-references and/or Strong's references.
  876. (defun bible--process-href ()
  877. "Fix the XML so cross-references are in the right format.
  878. These cross-references get processed later when the term is displayed.
  879. First, find the links put in by diatheke's HTMLHREF output format.
  880. Replace the links with verse references that get changed to clickable
  881. cross-references when the term is displayed.
  882. The verse refs look like this: <bookname>.<chapter>.<verse>. We convert
  883. them to the <bookname> <chapter>:<verse> format."
  884. (goto-char (point-min))
  885. (while (re-search-forward "<a href=\"passagestudy.*?</a>" nil t) ; HTMLHREF cross references.
  886. (let ((match-text (match-string 0)))
  887. ;; Delete original link.
  888. (replace-match "" nil nil)
  889. ;; Get the verse reference from the string we saved. Put it in
  890. ;; good format, then insert it into buffer where href was.
  891. (when (string-match "value=.*?&" match-text)
  892. (let* ((value-string (match-string 0 match-text))
  893. ;; Strip off value= and trailing &.
  894. (verse-ref-string (substring value-string 6 (1- (length value-string))))
  895. (verse-ref-length (length verse-ref-string))
  896. period)
  897. ;; Convert periods
  898. ;; Substitute first period with space
  899. (when (setq period (cl-search "." verse-ref-string))
  900. (aset verse-ref-string period ? ))
  901. ;; Substitute second period with colon
  902. (when (setq period (cl-search "." verse-ref-string))
  903. (aset verse-ref-string period ?:))
  904. ;; Replace numbers (1, 2 or 3) with roman numerals (I, II, III).
  905. (pcase (aref verse-ref-string 0)
  906. (?1 (setq verse-ref-string (concat "I" (substring verse-ref-string 1))))
  907. (?2 (setq verse-ref-string (concat "II" (substring verse-ref-string 1))))
  908. (?3 (setq verse-ref-string (concat "III" (substring verse-ref-string 1)))))
  909. (set-text-properties 0 verse-ref-length nil verse-ref-string) ; Clear unwanted properties (if any)
  910. (insert verse-ref-string))))))
  911. (defun bible--cleanup-lex-text (lex-text)
  912. "Reformat tooltip text LEX-TEXT so tooltips look nice."
  913. (dolist (outline-string bible-outline-strings)
  914. (setq lex-text (string-replace (car outline-string) (cdr outline-string) lex-text)))
  915. lex-text)
  916. (defun bible--lookup-def-greek (key)
  917. "Execute `diatheke' to do query on KEY.
  918. Massage output so verse cross references are usable. Returns string."
  919. (with-temp-buffer
  920. (let ((args (list bible-sword-query nil (current-buffer) t "-b" bible-greek-lexicon "-o" "m" "-f" "plain" "-k" key)))
  921. (when bible-show-diatheke-exec
  922. (message "%s" args))
  923. (apply #'call-process args)
  924. (bible--cleanup-lex-text (bible--remove-module-name bible-greek-lexicon (buffer-string))))))
  925. (defun bible--lookup-lemma-index (key)
  926. "Return the Greek lemma from lemma index with a strong's number as KEY."
  927. (string-trim
  928. (bible--remove-module-name bible-lexicon-index (bible--lex-query key bible-lexicon-index))))
  929. (defun bible--lookup-lemma-greek-indexed (key)
  930. "Lookup Greek lemma using Strong's number KEY.
  931. Then look up the definition of that lemma. Used when two-stage
  932. lexical definition is set for a particular lexicon."
  933. (let ((lemma-entry (bible--lookup-lemma-index key))) ; Get lemma from Strong's number
  934. (when lemma-entry
  935. (let ((lemma (caddr (split-string lemma-entry " "))))
  936. (bible--lookup-def-greek lemma)))))
  937. (defun bible--lookup-lemma-greek (key)
  938. "Lookup lexical definition using Strong's number KEY.
  939. 1. Check hash table first. If entry found, return.
  940. 2. Otherwise, if a lexicon is accessed by lemmas, do lookup using index method.
  941. 3. Otherwise just use the Strong's number method."
  942. (or (gethash key bible-hash-greek)
  943. (puthash key
  944. (if bible-use-index-for-lexicon
  945. (bible--lookup-lemma-greek-indexed key)
  946. (bible--lookup-def-greek key))
  947. bible-hash-greek)))
  948. (defun bible--lookup-def-hebrew (key)
  949. "Execute `diatheke' to do query on KEY.
  950. Massage output so various cross references are usable. Returns string."
  951. (with-temp-buffer
  952. (let ((args (list bible-sword-query nil (current-buffer) t "-b" bible-hebrew-lexicon "-f" "plain" "-k" key)))
  953. (when bible-show-diatheke-exec
  954. (message "%s" args))
  955. (apply #'call-process args)
  956. (bible--process-href)
  957. (concat (string ?\x200e)
  958. (bible--remove-module-name bible-hebrew-lexicon (substring (buffer-string) 7))))))
  959. (defun bible--lookup-lemma-hebrew (key)
  960. "Lookup lexical definition using Strong's number KEY.
  961. 1. Check hash table first. If entry found, return.
  962. 2. Otherwise, if a lexicon is accessed by lemmas, do lookup using index method.
  963. 3. Otherwise just use the Strong's number method."
  964. (or (gethash key bible-hash-hebrew)
  965. (puthash key
  966. (bible--lookup-def-hebrew key)
  967. bible-hash-hebrew)))
  968. ;; We use the shorter lexicons for text in tooltips. We also cache the
  969. ;; lex and morph strings, hoping to speed up tooltip rendering.
  970. (defun bible--lookup-lemma-short (lemma lexicon)
  971. "Look up lexical entry for LEMMA in short LEXICON.
  972. Returns a string that is intended to be displayed in a tooltip.
  973. Uses short lexicon (e.g. StrongsRealHebrew or StrongsRealGreek)."
  974. (when (string-match "[0-9]+" lemma)
  975. (let ((result
  976. ;; Get rid of unnecessary strongs codes at the beginning.
  977. (replace-regexp-in-string
  978. ".*[0-9]+ [0-9]+ " ""
  979. (bible--lex-query (concat (match-string 0 lemma)) lexicon))))
  980. ;; Remove parenthesized module name.
  981. (bible--remove-module-name lexicon result))))
  982. (defun bible--lookup-lex (lex)
  983. "Look up lexical item LEX. This is used for tooltips.
  984. Return hash table entry if present in `lex-hash' cache, else look up in
  985. database and stash in cache."
  986. (when lex
  987. (let* ((key (substring lex 7)) ; strip off "strong:" prefix.
  988. (lex-text (gethash key lex-hash)))
  989. (if lex-text
  990. lex-text
  991. (setq lex-text
  992. (cond ((string-prefix-p "G" key)
  993. (bible--lookup-lemma-short key bible-greek-lexicon-short))
  994. ((string-prefix-p "H" key)
  995. (concat (string ?\x200e)
  996. (bible--lookup-lemma-short key bible-hebrew-lexicon-short)))))
  997. (puthash key (string-fill (bible--cleanup-lex-text lex-text) 75) lex-hash)))))
  998. (defun bible--lookup-morph-entry (morph)
  999. "Look up entry for morphological item MORPH.
  1000. Return hash table entry if present in `morph-hash' cache, else look up in
  1001. database and stash in cache."
  1002. (when morph
  1003. (or (gethash morph morph-hash)
  1004. (puthash morph
  1005. (let (morph-module morph-key)
  1006. (cond ((string-prefix-p "robinson:" morph)
  1007. (setq morph-module "Robinson")
  1008. (setq morph-key (substring morph (length "robinson:"))))
  1009. ((string-prefix-p "packard:" morph)
  1010. (setq morph-module "Packard")
  1011. (setq morph-key (substring morph (length "packard:"))))
  1012. ((string-prefix-p "oshm:" morph)
  1013. (setq morph-module "OSHM")
  1014. (setq morph-key (substring morph (length "oshm:")))))
  1015. (bible--remove-module-name morph-module (bible--morph-query morph-key morph-module)))
  1016. morph-hash))))
  1017. ;; Get string for tooltip display
  1018. (defun bible--show-lex-morph (_window object pos)
  1019. "Get text for tooltip display for OBJECT at POS in WINDOW.
  1020. Includes both lex and morph definitions if text module has
  1021. both tags, otherwise just get lex definition."
  1022. (let* ((lex (get-text-property pos 'strong object))
  1023. (lex-text (bible--lookup-lex lex))
  1024. (morph (get-text-property pos 'morph object))
  1025. (morph-text (bible--lookup-morph-entry morph)))
  1026. (when lex-text
  1027. ;; This removes backslashes to prevent bogus command
  1028. ;; substitutions (that is, Emacs mistakenly filling in a key
  1029. ;; binding for some command---see Info doc on Substituting Key
  1030. ;; Bindings) in the tooltip.
  1031. ;; REVIEW: I couldn't figure out a better way to bypass command
  1032. ;; substitution in the tooltips. (FMG 5-Mar-2026)
  1033. (subst-char-in-string
  1034. ?\\
  1035. ?
  1036. (if morph-text
  1037. (concat (string-trim lex-text) "\n" (string-trim morph-text))
  1038. (string-trim lex-text))))))
  1039. ;;;; Display module text
  1040. (defun bible-handle-divine-name (item)
  1041. "When ITEM is divine name, display it as such."
  1042. (let ((start (point))
  1043. (strongs (dom-attr item 'savlm)))
  1044. (insert "LORD")
  1045. (let ((end (point)))
  1046. (add-face-text-property start end 'bold)
  1047. (put-text-property start end 'keymap bible-hebrew-keymap)
  1048. (when (and strongs (string-match "strong:H" strongs))
  1049. (put-text-property start end 'help-echo 'bible--show-lex-morph)
  1050. (put-text-property start end 'strong (match-string 0 strongs))))))
  1051. (defun bible--process-word (item iproperties)
  1052. "Handle <w ...> fubar </w> tag in ITEM. Check IPROPERTIES for qualifiers.
  1053. Add tooltips for definitions and morphology. Also insert lemmas in
  1054. buffer if `word study' is turned on (must be done after item is inserted
  1055. in buffer)."
  1056. (let ((word (string-trim (bible-dom-text item)))
  1057. (morph (dom-attr item 'morph))
  1058. (savlm (dom-attr item 'savlm))
  1059. (lemma (dom-attr item 'lemma))
  1060. (divinename (dom-by-tag item 'divinename)))
  1061. (let ((start (point))
  1062. (end (+ (point) (length word))))
  1063. (insert word)
  1064. ;; REVIEW: Special case this. Some modules do this differently.
  1065. ;; (FMG 5-Mar-2026)
  1066. (when divinename
  1067. (just-one-space)
  1068. (bible-handle-divine-name item)
  1069. (just-one-space))
  1070. ;; Red letter.
  1071. (when (plist-get iproperties 'jesus)
  1072. (add-face-text-property start end '(:foreground "red")))
  1073. ;; lexical definitions
  1074. ;; N.B. There are some severe issues with Strongs numbers in some modules.
  1075. (when (or savlm lemma)
  1076. (let* ((matched nil)
  1077. (lexemes (split-string (or savlm lemma)))
  1078. (lexeme
  1079. ;; HACK: Kludge alert. KJV module conflates Greek
  1080. ;; articles with nouns. Deal with this.
  1081. ;; (FMG 5-Mar-2026)
  1082. (let ((lexeme-list
  1083. (if (string= bible-text "KJV")
  1084. (reverse lexemes) ; Use the last `strong:' entry.
  1085. lexemes)))
  1086. (catch 'loop
  1087. (dolist (item lexeme-list)
  1088. (when (string-prefix-p "strong:" item)
  1089. (throw 'loop item)))))))
  1090. (when lexeme
  1091. (cond ((string-match "strong:G.*" lexeme) ; Greek
  1092. (setq matched (match-string 0 lexeme))
  1093. (put-text-property start end 'keymap bible-greek-keymap))
  1094. ((string-match "strong:H.*" lexeme) ; Hebrew
  1095. (setq matched (match-string 0 lexeme))
  1096. (put-text-property start end 'keymap bible-hebrew-keymap)))
  1097. ;; Add help-echo, strongs reference for tooltips if match.
  1098. (when matched
  1099. (setq bible-has-lexemes " Lex")
  1100. (put-text-property start end 'help-echo 'bible--show-lex-morph)
  1101. (put-text-property start end 'strong matched))))
  1102. ;; morphology
  1103. (when morph
  1104. (let* ((matched nil)
  1105. (morphemes (split-string morph))
  1106. (morpheme (car (last morphemes)))) ; KJV kludge as above
  1107. (if (or
  1108. (string-match "robinson:.*" morpheme) ; Robinson Greek morphology
  1109. (string-match "packard:.*" morpheme) ; Packard Greek morphology --- LXX seems to use this
  1110. (string-match "oshm:.*" morpheme)) ; OSHM Hebrew morphology
  1111. (setq matched (match-string 0 morpheme)))
  1112. (when matched
  1113. (setq bible-has-morphemes " Morph")
  1114. (put-text-property start end 'morph matched)
  1115. (put-text-property start end 'help-echo 'bible--show-lex-morph))))
  1116. ;; Insert lemma into buffer. Lemma tag will be part of lemma/savelm item.
  1117. ;; TODO: Should I enable lexicon lookups on these lemmas? I
  1118. ;; don't use this anyway.... (FMG 5-Mar-2026)
  1119. (when (and bible-word-study-enabled lemma (string-match "lemma.*:.*" lemma))
  1120. (dolist (word (split-string (match-string 0 lemma) " "))
  1121. (setq word (replace-regexp-in-string "[.:a-zA-Z0-9]+" "" word))
  1122. (just-one-space)
  1123. (let ((refstart (point)))
  1124. (insert word)
  1125. (add-face-text-property refstart (point) '(:foreground "blue"))
  1126. (put-text-property refstart (point) 'keymap bible-lemma-keymap))))))))
  1127. (defun bible--insert-title (title-node)
  1128. "Insert the text in TITLE-NODE into buffer as a chapter title.
  1129. Since each verse will have a `title' tag, keep track and only emit a
  1130. title when the new title in `title-node' is different from the one
  1131. stored in `bible-chapter-title'."
  1132. (unless (equal bible-chapter-title title-node)
  1133. (setq-local bible-chapter-title title-node)
  1134. (let ((title-text
  1135. (replace-regexp-in-string ; Clear out XML.
  1136. "<.*?>" ""
  1137. (bible-dom-texts bible-chapter-title)))
  1138. (start (point)))
  1139. (bible-new-line)
  1140. ;; Insert the LRM character to make the text render left-to-right.
  1141. ;; This is necessary in the KJV module when displaying psalm 119.
  1142. (insert (string ?\x200e) title-text)
  1143. (put-text-property start (point) 'face 'bold)
  1144. (newline)
  1145. (delete-blank-lines))))
  1146. ;; These tags appear in ESV modules (and maybe others?)
  1147. ;; REVIEW: Is this right? (FMG 5-Mar-2026)
  1148. (defun bible--level-tag (node)
  1149. "Indent or break line as dictated by NODE."
  1150. (let ((type (dom-attr node 'type))
  1151. (level (dom-attr node 'level)))
  1152. (cond ((and type (string-equal-ignore-case type "x-br"))
  1153. (newline))
  1154. ((and type (string-equal-ignore-case type "x-indent"))
  1155. (insert "\t"))
  1156. ;; REVIEW: Some modules use `level' tag but
  1157. ;; not in a consistent way. (FMG 7-Mar-2026)
  1158. ((equal level "1")
  1159. (just-one-space))
  1160. ((equal level "2")
  1161. (newline)
  1162. (delete-blank-lines)))))
  1163. (defun bible--insert-xref (node)
  1164. "Insert a cross reference specified by NODE."
  1165. ;; TODO: Finish handling multiple consecutive cross references. (FMG 25-Mar-2026)
  1166. ;; TODO: Fix punctuation. (FMG 25-Mar-2026)
  1167. (let* ((word (bible-dom-text node))
  1168. (refs (split-string word ";" t)))
  1169. (message "ref-word: %s" word)
  1170. (dolist (ref refs)
  1171. (message "ref %s" ref)
  1172. (just-one-space)
  1173. (let ((start (point)))
  1174. (insert ref)
  1175. (let ((end (point)))
  1176. (put-text-property start end 'xref ref)
  1177. (put-text-property start end 'keymap bible-term-mode-map)
  1178. (put-text-property start end 'help-echo (concat "Go to " ref))
  1179. (add-face-text-property start end '(:foreground "blue")))))))
  1180. (defun bible--insert-domnode-recursive (node &optional iproperties)
  1181. "Recursively parse domnode NODE obtained from `libxml-parse-html-region'.
  1182. Inserts resulting text into active buffer with properties specified in
  1183. IPROPERTIES.
  1184. In processing subnodes, each case will prepend a space if it needs it."
  1185. (when (and bible-red-letter-enabled (equal (dom-attr node 'who) "Jesus"))
  1186. ;; For red-letter display.
  1187. (setq iproperties (plist-put iproperties 'jesus t)))
  1188. (dolist (subnode (dom-children node))
  1189. (cond ((null subnode) nil)
  1190. ((stringp subnode)
  1191. ;; Red letter
  1192. (when (plist-get iproperties 'jesus)
  1193. (add-face-text-property 0 (length subnode) '(:foreground "red") nil subnode))
  1194. (insert subnode))
  1195. ((consp subnode)
  1196. (let ((tag (dom-tag subnode)))
  1197. (pcase tag
  1198. ;; TODO: There are lots of tags we don't handle, especially in commentaries.
  1199. ;; Maybe process these at some point? Include footnotes etc.
  1200. ;; (FMG 5-Mar-2026)
  1201. ;; 'w is usual case.
  1202. ('w (insert " ") (bible--process-word subnode iproperties))
  1203. ('title
  1204. ;; This mess is to deal with the possibility that the
  1205. ;; title might change in the middle of the chapter. I'm
  1206. ;; talking about YOU, Psalm 119.
  1207. (if bible-chapter-title
  1208. (bible--insert-title subnode) ; Middle of chapter.
  1209. (save-excursion ; Beginning of chapter.
  1210. (goto-char (point-min))
  1211. (bible--insert-title subnode))))
  1212. ;; Font tag ignored for now, treat as if 'w.
  1213. ('font (insert " ") (bible--process-word subnode iproperties))
  1214. ('hi (when (equal (dom-attr subnode 'type) "bold")
  1215. (just-one-space)
  1216. (let ((word (bible-dom-text subnode))
  1217. (start (point)))
  1218. (insert word)
  1219. (put-text-property start (point) 'face 'bold))))
  1220. ;; Italic face (special case for certain module)
  1221. ('i
  1222. (just-one-space)
  1223. (let ((word (bible-dom-text subnode))
  1224. (start (point)))
  1225. (insert word)
  1226. (put-text-property start (point) 'face 'bold)
  1227. (add-face-text-property start (point) '(:foreground "orange"))))
  1228. ;; 'q is used for red letter.
  1229. ;; NASB Module uses 'seg to indicate OT quotations (and others?).
  1230. ((or 'body 'seg 'p 'q) (bible--insert-domnode-recursive subnode iproperties))
  1231. ('l (bible--level-tag subnode))
  1232. ;; REVIEW: divine name handling doesn't seem to work the same
  1233. ;; with all modules. (FMG 26-Mar-2026)
  1234. ('divinename (bible-handle-divine-name subnode))
  1235. ;; Some modules use this for line breaks and such.
  1236. ('milestone
  1237. (pcase (dom-attr subnode 'type)
  1238. ("line" (bible-new-line))
  1239. ;; ("x-PN" (bible-new-line)) ; REVIEW: Don't yet understand this one. (FMG 26-Mar-2026)
  1240. ("x-p" (insert (dom-attr subnode 'marker) " "))))
  1241. ('br (bible-new-line))
  1242. ('lb (when (equal (dom-attr subnode 'type) "x-begin-paragraph") (bible-new-line)))
  1243. ('div (when (or (equal (dom-attr subnode 'type) "paragraph")
  1244. (equal (dom-attr subnode 'type) "x-p"))
  1245. (bible-new-line)))
  1246. ;; For commentaries and the like.
  1247. ((or 'scripref 'reference) (bible--insert-xref subnode))
  1248. ;; Various text properties---ignore for now. REVIEW: (FMG 26-Mar-2026)
  1249. ((or 'b 'u) (bible--insert-domnode-recursive subnode iproperties))
  1250. ;; Word inserted by translation, not in original, give visual indication.
  1251. ('transchange
  1252. (insert " ")
  1253. (let ((word (bible-dom-text subnode))
  1254. (start (point))
  1255. (face (if (plist-get iproperties 'jesus) '(:foreground "salmon") '(:foreground "gray50"))))
  1256. (insert word)
  1257. (add-face-text-property start (point) face)))))))))
  1258. (defun bible--display (&optional verse)
  1259. "Render a page (chapter) of a Bible module.
  1260. Defaults to using `bible-text'.
  1261. If optional argument VERSE is supplied, set cursor at verse."
  1262. (let ((buffer-read-only nil)
  1263. (bible-has-lexemes nil)
  1264. (bible-has-morphemes nil))
  1265. (erase-buffer)
  1266. (insert (bible--exec-diatheke (concat bible--current-book-name ":" (number-to-string bible--current-chapter))))
  1267. ;; Parse the xml in the buffer into a DOM tree.
  1268. (let ((html-dom-tree (libxml-parse-html-region (point-min) (point-max))))
  1269. ;; Render the DOM tree into the buffer.
  1270. (unless bible-debugme ; If this is true, display the XML.
  1271. (erase-buffer)
  1272. (setq-local bible-chapter-title nil)
  1273. ;; Looking for the "body" tag in the DOM node.
  1274. (bible--insert-domnode-recursive (dom-by-tag html-dom-tree 'body))
  1275. (goto-char (point-min))))
  1276. (save-excursion
  1277. (let ((search-string (concat " *" (car bible--current-book) " " (number-to-string bible--current-chapter) ":")))
  1278. ;; Delete <Book Ch:> at beginning of verse, just leave verse number.
  1279. (while (re-search-forward search-string nil t)
  1280. (replace-match "")
  1281. (bible-new-line)
  1282. ;; Highlight verse number
  1283. ;; (when (re-search-forward "^ *[0-9]+" nil t 1)
  1284. (when (re-search-forward " *[0-9]+:" nil t 1)
  1285. (add-face-text-property (match-beginning 0) (match-end 0) '(:foreground "purple"))))))
  1286. (save-excursion
  1287. ;; Fix divine name lossage.
  1288. (while (re-search-forward "Lord LORD" nil t)
  1289. (replace-match "LORD")
  1290. (add-face-text-property (point) (- (point) 4) 'bold))
  1291. (while (re-search-forward "Lord.+s LORD" nil t -1)
  1292. (replace-match "LORD's")
  1293. (add-face-text-property (1- (point)) (- (point) 5) 'bold))
  1294. ;; Remove the module name from the buffer.
  1295. (while (re-search-forward (concat "^.*" bible-text ".*$") nil t)
  1296. (replace-match "" nil t)))
  1297. (save-excursion
  1298. (format-replace-strings '(("." . ". ")
  1299. ("," . ", ")
  1300. (";" . "; ")
  1301. (":" . ": ")
  1302. ("?" . "? ")
  1303. ("!" . "! ")
  1304. (" ." . ". ")
  1305. (" ," . ", ")
  1306. (" ;" . "; ")
  1307. (" :" . ": ")
  1308. (" ?" . "? ")
  1309. (" !" . "! ")
  1310. ("“ " . "“")
  1311. ("‘ " . "‘")
  1312. (" ’" . "’")
  1313. (". ”" . ".”")
  1314. ("? ”" . "?”"))
  1315. nil (point-min) (point-max)))
  1316. ;; Get rid of multiple consecutive spaces.
  1317. (save-excursion
  1318. (while (re-search-forward " *" nil t) ; More than one space in a row
  1319. (just-one-space)))
  1320. ;; Set the mode line of the biffer.
  1321. (if bible-has-lexemes
  1322. (unless (string-match " Lex" mode-name) (setq mode-name (concat mode-name " Lex")))
  1323. (setq mode-name (replace-regexp-in-string " Lex" "" mode-name)))
  1324. (if bible-has-morphemes
  1325. (unless (string-match " Morph" mode-name) (setq mode-name (concat mode-name " Morph")))
  1326. (setq mode-name (replace-regexp-in-string " Morph" "" mode-name)))
  1327. (force-mode-line-update))
  1328. ;; If optional verse specification go to that verse.
  1329. (when verse
  1330. (re-search-forward (concat " ?" (number-to-string verse)) nil t)))
  1331. ;;;; Modules (Bible texts, commentaries)
  1332. (defun compare-module-names (n1 n2)
  1333. "Compare N1 and N2, ignoring case, using collation order."
  1334. (string-collate-lessp n1 n2 nil t))
  1335. (defun bible--get-biblical-modules ()
  1336. "Populate `bible--texts' and `bible--commentaries' lists."
  1337. (let ((lines
  1338. (split-string
  1339. (bible--exec-diatheke "modulelist" nil "plain" "system")
  1340. "[\n\r]+"))
  1341. (texts nil)
  1342. (commentaries nil)
  1343. (doing-texts nil)
  1344. (doing-commentaries nil))
  1345. (setq bible--texts nil)
  1346. (setq bible--commentaries nil)
  1347. (catch 'done
  1348. (dolist (line lines)
  1349. (when doing-texts
  1350. (push (split-string line " : ") texts))
  1351. (when doing-commentaries
  1352. (push (split-string line " : ") commentaries))
  1353. (when (string-equal line "Biblical Texts:")
  1354. (setq doing-texts t))
  1355. (when (string-equal line "Commentaries:")
  1356. (setq doing-texts nil)
  1357. (pop texts) ; Remove `Commentaries:' line from `bible--texts'.
  1358. (setq doing-commentaries t))
  1359. (when (string-equal line "Lexicons / Dictionaries:")
  1360. (pop commentaries) ; Remove `Lexicons / Dictionaries:' line
  1361. ; from bible--commentaries.
  1362. (throw 'done nil))))
  1363. (setq bible--texts (cl-sort texts #'compare-module-names :key #'car))
  1364. (setq bible--commentaries (cl-sort commentaries #'compare-module-names :key #'car)))
  1365. nil)
  1366. (defun bible--list-biblical-texts ()
  1367. "Return a list of accessible Biblical Text modules."
  1368. (bible--get-biblical-modules) ; Make sure the lists are fresh.
  1369. bible--texts)
  1370. (defun bible--list-biblical-commentaries ()
  1371. "Return a list of accessible Biblical Text modules."
  1372. (bible--get-biblical-modules) ; Make sure the lists are fresh.
  1373. bible--commentaries)
  1374. (defun bible-display-available-texts ()
  1375. "Display available modules, allow user to select."
  1376. (interactive)
  1377. (bible--get-biblical-modules) ; Make sure lists are fresh.
  1378. (with-current-buffer (get-buffer-create "Texts")
  1379. (bible-text-select-mode)
  1380. (let ((buffer-read-only nil))
  1381. (erase-buffer)
  1382. (setq-local tab-stop-list '(25))
  1383. (dolist (text bible--texts)
  1384. (let ((name (string-trim (car text)))
  1385. (description (string-trim-left (cadr text))))
  1386. (insert
  1387. (propertize (string-trim name)
  1388. 'face 'bold
  1389. 'module name
  1390. 'help-echo (concat "Select " name)
  1391. 'keymap bible-text-map))
  1392. (move-to-tab-stop)
  1393. (insert (format "%s\n" description)))))
  1394. (goto-char (point-min))
  1395. (pop-to-buffer (current-buffer) nil t)))
  1396. (defun bible-display-available-commentaries ()
  1397. "Display available modules, allow user to select."
  1398. (interactive)
  1399. (bible--get-biblical-modules) ; Make sure lists are fresh.
  1400. (with-current-buffer (get-buffer-create "Commentaries")
  1401. (bible-text-select-mode)
  1402. (let ((buffer-read-only nil))
  1403. (erase-buffer)
  1404. (setq-local tab-stop-list '(25))
  1405. (dolist (commentary bible--commentaries)
  1406. (let ((name (string-trim (car commentary)))
  1407. (description (string-trim-left (cadr commentary))))
  1408. (insert
  1409. (propertize (string-trim name)
  1410. 'face 'bold
  1411. 'module name
  1412. 'help-echo (concat "Select " name)
  1413. 'keymap bible-text-map))
  1414. (move-to-tab-stop)
  1415. (insert (format "%s\n" description)))))
  1416. (goto-char (point-min))
  1417. (pop-to-buffer (current-buffer) nil t)))
  1418. ;;;; Bible Searching
  1419. (defun bible--open-search (query searchmode module)
  1420. "Open a search buffer of QUERY using SEARCHMODE in module MODULE."
  1421. (let ((results (string-trim (replace-regexp-in-string
  1422. "Entries .+?--" ""
  1423. (bible--diatheke-search query searchmode "plain" module)))))
  1424. (if (equal results (concat "none (" module ")"))
  1425. (message (concat
  1426. "No results found."
  1427. (when (equal searchmode "lucene")
  1428. " Verify index has been build with mkfastmod.")))
  1429. (with-current-buffer (get-buffer-create (generate-new-buffer-name (concat "*bible-search*")))
  1430. (bible-search-mode)
  1431. (bible--display-search results module)
  1432. (setq-local bible-search-word-this-query query
  1433. bible-search-text-this-query module
  1434. bible-search-range-this-query bible-search-range)
  1435. ;; (setq-local mode-line-format bible-search-mode-line-format)
  1436. (pop-to-buffer (current-buffer) nil t)))))
  1437. (defun bible--display-search (results module)
  1438. "Render RESULTS of search query with MODULE."
  1439. (let ((match 0)
  1440. (matchstr "")
  1441. (verses nil)
  1442. (query-verses "")
  1443. (buffer-read-only nil))
  1444. ;; (message "display-search %s" module)
  1445. (setq-default bible-text module)
  1446. (erase-buffer)
  1447. (while match
  1448. (setq match (string-match ".+?:[0-9]?[0-9]?" results (+ match (length matchstr)))
  1449. matchstr (match-string 0 results))
  1450. (when match
  1451. (push
  1452. ;; Massage match to make it more sortable, get rid of some characters.
  1453. (replace-regexp-in-string
  1454. ".+; " ""
  1455. (string-replace
  1456. "I " "1"
  1457. (string-replace
  1458. "II " "2"
  1459. (string-replace
  1460. "III " "3"
  1461. matchstr))))
  1462. verses)))
  1463. (setq verses (cl-sort verses #'string-version-lessp))
  1464. (dolist (verse verses)
  1465. (if query-verses
  1466. (setq query-verses (concat query-verses ";" verse))
  1467. (setq query-verses verse)))
  1468. (let ((bible-show-diatheke-exec nil))
  1469. (insert (bible--exec-diatheke query-verses nil nil module)))
  1470. (let* ((html-dom-tree (libxml-parse-html-region (point-min) (point-max))))
  1471. (erase-buffer)
  1472. (bible--insert-domnode-recursive (dom-by-tag html-dom-tree 'body)))
  1473. (goto-char (point-min))
  1474. ;; Remove module name from buffer.
  1475. (save-excursion
  1476. (while (re-search-forward (concat "^.*" module ".*$") nil t)
  1477. (replace-match "")))
  1478. (setq mode-name "Bible Search ")
  1479. (setq-local bible-search-matches (length verses))))
  1480. ;;;; Terms (lemmas, morphemes)
  1481. (defun bible--get-lemma (language strongs)
  1482. "Get the lemma from lexicon for LANGUAGE for strong's term STRONGS.
  1483. Used to display lemmas in mode lines."
  1484. (let ((lemma-entry
  1485. (pcase language
  1486. ('hebrew
  1487. ;; Use Strong's Hebrew lexicon to look up Hebrew lemma.
  1488. (bible--lex-query strongs "StrongsHebrew" ))
  1489. ('greek
  1490. ;; Use Strong's Greek lexicon to look up Greek lemma.
  1491. (bible--lex-query strongs "StrongsGreek")))))
  1492. (unless (equal lemma-entry "")
  1493. ;; Entry will look like <num>: <num>. <lemma> <definition>. Get
  1494. ;; rid of everything before and after <lemma>.
  1495. (let* ((lemma-line (split-string lemma-entry))
  1496. (lemma (caddr lemma-line)))
  1497. lemma))))
  1498. (defun bible--display-greek ()
  1499. "Display Greek term.
  1500. This command is run by clicking on text, not directly by the user."
  1501. (interactive)
  1502. (let ((item (car (split-string (get-text-property (point) 'strong)))))
  1503. ;; Remove "strong:G" prefix
  1504. (bible-term-greek (replace-regexp-in-string "strong:G" "" item))))
  1505. (defun bible--display-hebrew ()
  1506. "Display Hebrew term.
  1507. This command is run by clicking on text, not directly by the user."
  1508. (interactive)
  1509. (let ((item (car (split-string (get-text-property (point) 'strong)))))
  1510. ;; Remove "strong:H" prefix and any alphabetic suffixes.
  1511. (bible-term-hebrew (replace-regexp-in-string "strong:H" "" item))))
  1512. ;;(defun bible-display-morphology (morph)
  1513. ;; ;; REVIEW: Do something here? (FMG 5-Mar-2026)
  1514. ;; )
  1515. (defun bible--fixup-lexicon-display (_termtype)
  1516. "Fixup the display of a lexical entry whose language is given by TERMTYPE."
  1517. (let ((buffer-read-only nil))
  1518. (goto-char (point-min))
  1519. ;; This enables clicking on verse references.
  1520. (save-excursion
  1521. (while (search-forward-regexp bible--verse-regexp nil t)
  1522. (let ((match (match-string 0))
  1523. (start (match-beginning 0))
  1524. (end (match-end 0)))
  1525. (put-text-property start end 'xref match)
  1526. (put-text-property start end 'keymap bible-search-mode-map)
  1527. (put-text-property start end 'help-echo (concat "Go to " (substring-no-properties match)))
  1528. (add-face-text-property start end '(:foreground "blue")))))
  1529. (save-excursion
  1530. (while (search-forward "()" nil t)
  1531. (replace-match "")))))
  1532. (defun bible--open-term-hebrew (term)
  1533. "Open a buffer of the Strong's Hebrew TERM's definition."
  1534. (with-current-buffer (get-buffer-create (generate-new-buffer-name "*bible-term*"))
  1535. (bible-term-hebrew-mode)
  1536. (setq-local bidi-paragraph-direction 'left-to-right)
  1537. (setq-local mode-name (concat (bible--get-lemma 'hebrew term) " Term (Hebrew)"))
  1538. (bible--display-lemma-hebrew term)
  1539. (pop-to-buffer (current-buffer) nil t)
  1540. (fit-window-to-buffer)))
  1541. (defun bible--display-lemma-hebrew (lemma)
  1542. "Render the definition of the Strong's Hebrew LEMMA.
  1543. This code is customized for the BDBGlosses_Strongs lexicon."
  1544. (let ((buffer-read-only nil))
  1545. (erase-buffer)
  1546. ;; BDBGlosses_Strongs needs the prefixed `H'.
  1547. (insert (substring (bible--cleanup-lex-text (bible--lookup-lemma-hebrew (concat "H" lemma))) 7))
  1548. (bible--fixup-lexicon-display 'hebrew)))
  1549. (defun bible--open-term-greek (term)
  1550. "Open a buffer of the Strong's Greek TERM definition."
  1551. (with-current-buffer (get-buffer-create (generate-new-buffer-name "*bible-term*"))
  1552. (bible-term-greek-mode)
  1553. (setq-local mode-name (concat (bible--get-lemma 'greek term) " Term (Greek)"))
  1554. (bible--display-lemma-greek term)
  1555. (pop-to-buffer (current-buffer) nil t)
  1556. (fit-window-to-buffer)))
  1557. (defun bible--display-lemma-greek (lemma)
  1558. "Render the definition of the Strong's Greek LEMMA."
  1559. (let ((buffer-read-only nil))
  1560. (erase-buffer)
  1561. (insert (bible--lookup-lemma-greek lemma))
  1562. (bible--fixup-lexicon-display 'greek)))
  1563. ;;;; Utilities
  1564. (defun bible-new-line ()
  1565. "Ensure beginning of line. Try to avoid redundant blank lines."
  1566. (unless (bolp)
  1567. (newline)))
  1568. (defun bible--remove-module-name (module-name string)
  1569. "Remove parenthesized MODULE-NAME from STRING.
  1570. Also deals with bug where some versions of diatheke return string that
  1571. is missing close parenthesis."
  1572. (replace-regexp-in-string (concat "^(" module-name ".*$") "" string))
  1573. (defun bible--list-number-range (min max &optional prefix)
  1574. "Returns a list containing entries for each integer between MIN and MAX.
  1575. If PREFIX is supplied, prepend PREFIX to the entries.
  1576. Used in tandem with `completing-read' for chapter selection."
  1577. (let ((range-list nil))
  1578. (dotimes (num (1+ max))
  1579. (when (>= num min)
  1580. (push (cons (concat prefix (number-to-string num)) num) range-list)))
  1581. (nreverse range-list)))
  1582. ;;; Provides
  1583. (provide 'bible)
  1584. ;;; bible.el ends here.