bible.el 74 KB

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