bible.el 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505
  1. ;;; bible.el --- A browsing interface for the SWORD Project's Diatheke CLI -*- mode: EMACS-LISP; lexical-binding: t -*-
  2. ;; Copyright (c) 2025 Fred Gilham
  3. ;; Author: Fred Gilham
  4. ;; URL: https://gitbot.homedns.org/fred/bible-mode
  5. ;; Version: 1.0.0
  6. ;; Keywords: files, text, hypermedia
  7. ;; Package-Requires: ((emacs "29.1"))
  8. ;;; Commentary:
  9. ;; Forked and extensively modified from package by Zacalot
  10. ;; Url: https://github.com/Zacalot/bible-mode
  11. ;; This package uses the `diatheke' program to browse and search
  12. ;; Biblical texts provided by the Sword project.
  13. ;; Word study is also supported.
  14. ;;; Usage:
  15. ;; First install `diatheke'. On Debian/Ubuntu it's in the `diatheke'
  16. ;; package. In other distributions it might be in the sword package.
  17. ;; For Windows I found that you can simply install the Xiphos package.
  18. ;; It includes the Sword library and its utilities including diatheke,
  19. ;; installmgr and mkfastmod. Add the "Program Files\Xiphos\bin" path
  20. ;; to your execution path.
  21. ;; Use M-x `bible-open' to open a Bible buffer.
  22. ;; Use C-h f `bible' to see available keybindings.
  23. ;; You may customize `bible-module' to set a default browsing
  24. ;; module, as well as `bible-word-study-enabled' to enable word
  25. ;; study by default.
  26. ;;; Design:
  27. ;; The idea here is to use the diatheke program to insert code from
  28. ;; modules into buffers. The main bible display uses an "internal" XML
  29. ;; format. The whole buffer gets parsed by libxml-parse-html-region to
  30. ;; create a dom tree. This gets parsed by
  31. ;; bible--insert-domnode-recursive to render the text into
  32. ;; reading format.
  33. ;; The text is then decorated using information from the dom format as
  34. ;; necessary along with regular expressions to identify the verse
  35. ;; references. This is for red letters, purple highlighting of the
  36. ;; verse numbers, bold face of the divine name in the OT and so on.
  37. ;; If strongs tags and/or morphological tags are present, they are
  38. ;; looked up in appropriate lexical and morphological modules and used
  39. ;; to add tooltips to the text so that mousing over words will bring
  40. ;; up a tooltip with information about the word. Clicking on a word
  41. ;; with lexical information will display that informatio in a "term"
  42. ;; buffer.
  43. ;;; Code:
  44. ;;;; Requirements
  45. ;;; XXX FMG there are just a few constructs that use this; use elisp versions instead.
  46. ;;(require 'cl-lib)
  47. (require 'dom)
  48. (require 'shr)
  49. ;; Turn off tool bar mode because we want the pixels....
  50. (tool-bar-mode -1)
  51. ;;;; Variables
  52. (defgroup bible nil
  53. "Settings for `bible'."
  54. :group 'tools
  55. :link '(url-link "https://gitbot.homedns.org/fred/bible-mode"))
  56. (defcustom bible-module
  57. "KJV"
  58. "Book module for Diatheke to query."
  59. :type '(choice (const :tag "None" nil)
  60. (string :tag "Module abbreviation (e.g. \"KJV\")"))
  61. ;; :local t
  62. :group 'bible)
  63. ;;;
  64. ;;; XXX Not implememted yet
  65. (defcustom bible-font
  66. "Ezra SIL"
  67. "Default font for bible."
  68. :type '(string :tag "Font family name (e.g. \"Ezra SIL\")")
  69. :local t
  70. :group 'bible)
  71. (defcustom bible-greek-lexicon
  72. "AbbottSmithStrongs"
  73. "Lexicon used for displaying definitions of Greek words using Strong's codes."
  74. :type '(string :tag "Lexicon module (e.g. \"StrongsRealGreek\").")
  75. :local nil
  76. :group 'bible)
  77. (defcustom bible-use-index-for-lexicon nil
  78. "Some lexicons are accessed by lemmas rather than Strong's numbers.
  79. Use an index to look up lemmas from Strong's numbers so these lexicons can
  80. be used. Examples of this type of lexicon are AbbottSmith and
  81. LiddellScott. XXX LiddellScott currently doesn't work. XXX AbbottSmithStrongs
  82. now has the complete entries instead of just links."
  83. :type 'boolean
  84. :local nil
  85. :group 'bible)
  86. (defcustom bible-lexicon-index "AbbottSmithStrongs"
  87. "A module that consists of an index mapping Strong's numbers to Greek lemmas.
  88. The code is written to use the entries in AbbottSmithStrongs
  89. which are of the form
  90. <strong's number>: @LINK <greek lemma>"
  91. :type '(string :tag "Lexicon index.")
  92. :local nil
  93. :group 'bible)
  94. (defcustom bible-greek-lexicon-short
  95. "StrongsRealGreek"
  96. "Lexicon used for displaying definitions of Greek words in tooltips."
  97. :type '(string :tag "Lexicon module (e.g. \"StrongsRealGreek\").")
  98. :local nil
  99. :group 'bible)
  100. (defcustom bible-hebrew-lexicon
  101. "StrongsRealHebrew"
  102. "Lexicon used for displaying definitions of Hebrew words using Strong's codes."
  103. :type '(string :tag "Lexicon module (e.g. \"StrongsRealHebrew\")")
  104. :local nil
  105. :group 'bible)
  106. (defcustom bible-hebrew-lexicon-short
  107. "BDBGlosses_Strongs" ; This seems to work
  108. "Lexicon used for displaying definitions of Hebrew words in tooltips."
  109. :type '(string :tag "Lexicon module (e.g. \"StrongsRealHebrew\")")
  110. :local nil
  111. :group 'bible)
  112. (defcustom bible-word-study-enabled
  113. nil
  114. "Display Strong's Hebrew, Strong's Greek, and Lemma words for study."
  115. :type 'boolean
  116. :local t
  117. :group 'bible)
  118. (defcustom bible-red-letter-enabled
  119. t
  120. "Display words of Jesus in red when module has that information."
  121. :type 'boolean
  122. :local t
  123. :group 'bible)
  124. (defcustom bible-show-diatheke-exec
  125. t
  126. "Show the arguments by which diatheke is executed."
  127. :type 'boolean
  128. :local nil
  129. :group 'bible)
  130. ;;; variable defs
  131. ;;(defvar bible-mode-verse-regexp "([\d ]*[a-zA-Z]+( \d*:\d*)?)(( - )| )?(((\d* )?[a-zA-Z]+ )?\d*([:-]+\d*)?)")
  132. ;; (defvar bible-mode-verse-regexp "/(\d*)\s*([a-z]+)\s*(\d+)(?::(\d+))?(\s*-\s*(\d+)(?:\s*([a-z]+)\s*(\d+))?(?::(\d+))?)?/i")
  133. (defconst bible--verse-regexp "\\(I \\|II \\|III \\)??[a-zA-Z]+?[ \t\n][0-9]+[:][0-9]+")
  134. (defvar bible--modules (lazy-completion-table bible--modules bible--list-biblical-modules))
  135. ;; XXX I believe these chapter counts aren't the same for all modules, e.g. JPS.
  136. (defvar bible--books
  137. '(;; Old Testament
  138. ("Genesis" . 50) ("Exodus" . 40) ("Leviticus" . 27) ("Numbers" . 36)
  139. ("Deuteronomy" . 34) ("Joshua" . 24) ("Judges" . 21) ("Ruth" . 4)
  140. ("I Samuel" . 31) ("II Samuel" . 24) ("I Kings" . 22) ("II Kings" . 25)
  141. ("I Chronicles" . 29) ("II Chronicles" . 36) ("Ezra" . 10) ("Nehemiah" . 13)
  142. ("Esther" . 10) ("Job" . 42) ("Psalms" . 150) ("Proverbs" . 31)
  143. ("Ecclesiastes" . 12) ("Song of Solomon" . 8) ("Isaiah" . 66) ("Jeremiah" . 52)
  144. ("Lamentations" . 5) ("Ezekiel" . 48) ("Daniel" . 12) ("Hosea" . 14)
  145. ("Joel" . 3) ("Amos" . 9) ("Obadiah" . 1) ("Jonah" . 4)
  146. ("Micah" . 7) ("Nahum" . 3) ("Habakkuk" . 3) ("Zephaniah" . 3)
  147. ("Haggai" . 2) ("Zechariah" . 14) ("Malachi" . 4)
  148. ;; New Testament
  149. ("Matthew" . 28) ("Mark" . 16) ("Luke" . 24) ("John" . 21)
  150. ("Acts" . 28) ("Romans" . 16) ("I Corinthians" . 16) ("II Corinthians" . 13)
  151. ("Galatians" . 6) ("Ephesians" . 6) ("Philippians" . 4) ("Colossians" . 4)
  152. ("I Thessalonians" . 5) ("II Thessalonians" . 3) ("I Timothy" . 6) ("II Timothy" . 4)
  153. ("Titus" . 3) ("Philemon" . 1) ("Hebrews" . 13) ("James" . 5)
  154. ("I Peter" . 5) ("II Peter" . 3) ("I John" . 5) ("II John" . 1)
  155. ("III John" . 1) ("Jude" . 1) ("Revelation of John" . 22))
  156. "A-list of name / chapter count for Bible books.")
  157. (defvar bible--book-name-abbreviations-alist
  158. '(;; Old Testament
  159. ("Ge" . "Genesis") ("Ex" . "Exodus") ("Le" . "Leviticus") ("Nu" . "Numbers")
  160. ("De" . "Deuteronomy") ("Js" . "Joshua") ("Jg" . "Judges") ("Judg" . "Judges")
  161. ("Ru" . "Ruth") ("1 Samuel" . "I Samuel") ("I Sa" . "I Samuel") ("1 Sa" . "I Samuel")
  162. ("2 Samuel" . "II Samuel") ("II Sa" . "II Samuel") ("2 Sa" . "II Samuel") ("1 Kings" . "I Kings")
  163. ("I Ki" . "I Kings") ("1 Ki" . "I Kings") ("2 Kings" . "II Kings") ("II Ki" . "II Kings")
  164. ("2 Ki" . "II Kings") ("1 Chronicles" . "I Chronicles") ("I Ch" . "I Chronicles") ("1 Ch" . "I Chronicles")
  165. ("2 Chronicles" . "II Chronicles") ("II Ch" . "II Chronicles") ("2 Ch" . "II Chronicles")
  166. ("Ezr" . "Ezra") ("Ne" . "Nehemiah")
  167. ("Es" . "Esther") ("Jb" . "Job") ("Ps" . "Psalms") ("Pr" . "Proverbs")
  168. ("Ec" . "Ecclesiastes") ("So" . "Song of Solomon") ("Is" . "Isaiah") ("Je" . "Jeremiah")
  169. ("La" . "Lamentations") ("Ez" . "Ezekiel") ("Da" . "Daniel") ("Ho" . "Hosea")
  170. ("Joe" . "Joel") ("Am" . "Amos") ("Ob" . "Obadiah") ("Jon" . "Jonah")
  171. ("Mi" . "Micah") ("Na" . "Nahum") ("Ha" . "Habakkuk") ("Zep" . "Zephaniah")
  172. ("Hag" . "Haggai") ("Ze" . "Zechariah") ("Mal" . "Malachi")
  173. ;; New Testament
  174. ;; Added AbbottSmith lexicon abbreviations to allow proper following of cross references in lexicon buffers.
  175. ("Mt" . "Matthew") ("Matt" . "Matthew")
  176. ("Mk" . "Mark") ("Lk" . "Luke") ("Jo" . "John") ("Ac" . "Acts")
  177. ("Ro" . "Romans") ("Rom" . "Romans")
  178. ("1 Corintihans" . "I Corinthians") ("I Co" . "I Corinthians") ("1 Co" . "I Corinthians") ("ICor" . "I Corinthians")
  179. ("2 Corinthians" . "II Corinthians") ("II Co" . "II Corinthians") ("2 Co" . "II Corinthians") ("IICor" . "II Corinthians")
  180. ("Ga" . "Galatians") ("Gal" . "Galatians")
  181. ("Eph" . "Ephesians")
  182. ("Phl" . "Philippians") ("Phil" . "Philippians")
  183. ("Col" . "Colossians")
  184. ("1 Thessalonians" . "I Thessalonians") ("I Th" . "I Thessalonians") ("1 Th" . "I Thessalonians") ("IThess" . "I Thessalonians")
  185. ("2 Thessalonians" . "II Thessalonians") ("II Th" . "II Thessalonians") ("2 Th" . "II Thessalonians") ("IIThess" . "II Thessalonians")
  186. ("1 Timothy" . "I Timothy") ("I Ti" . "I Timothy") ("1 Ti" . "I Timothy") ("ITim" . "I Timothy")
  187. ("2 Timothy" . "II Timothy") ("II Ti" . "II Timothy") ("2 Ti" . "II Timothy") ("IITim" . "II Timothy")
  188. ("Tit" . "Titus")
  189. ("Phm" . "Philemon") ("Phlm" . "Philemon")
  190. ("He" . "Hebrews") ("Heb" . "Hebrews")
  191. ("Ja" . "James") ("Jas" . "James")
  192. ("1 Peter" . "I Peter") ("I Pe" . "I Peter") ("1 Pe" . "I Peter")
  193. ("2 Peter" . "II Peter") ("II Pe" . "II Peter") ("2 Pe" . "II Peter") ("IIPet" . "II Peter")
  194. ("1 John" . "I John") ("I Jo" . "I John") ("1 Jo" . "I John") ("IJohn" . "I John")
  195. ("2 John" . "II John") ("II Jo" . "II John") ("2 Jo" . "II John") ("IIJohn" . "II John")
  196. ("3 John" . "III John") ("III Jo" . "III John") ("3 Jo" . "III John") ("IIIJohn" . "III John")
  197. ("Ju" . "Jude")
  198. ("Re" . "Revelation of John") ("Rev" . "Revelation of John"))
  199. "A-list of abbreviations for Bible books.")
  200. ;;;; Book / chapter
  201. (defvar-local bible--current-book (assoc "Genesis" bible--books)
  202. "Current book data (name . chapter).")
  203. (defvar-local bible--current-book-name "Genesis"
  204. "Current book name.")
  205. (defvar-local bible--current-chapter 1
  206. "Current book chapter number.")
  207. (defvar-local bible-query nil
  208. "Search query associated with the buffer.")
  209. (defvar-local bible-search-mode "phrase"
  210. "Search mode: either `lucene', `phrase', `regex' or `multiword'.")
  211. (defvar bible-search-range nil)
  212. (defvar-local bible-has-strongs nil
  213. "Set if the module being displayed has strongs numbers availabile.")
  214. (defvar-local bible-has-morphology nil
  215. "Set if the module being displayed has morphology availabile.")
  216. ;; (defvar bible--current-module nil)
  217. ;;;; Keymaps
  218. ;;;; N.B. Bible menu items appear in reverse order of their
  219. ;;;; definition below
  220. (defconst bible-map (make-sparse-keymap)
  221. "Keymap for bible.")
  222. (define-key bible-map [menu-bar bible]
  223. (cons "Bible" (make-sparse-keymap "Bible")))
  224. (define-key bible-map
  225. [menu-bar bible toggle-debug]
  226. '("Toggle debug-on-error" . toggle-debug-on-error))
  227. (defun bible-toggle-display-diatheke ()
  228. "Toggle diatheke args display."
  229. (interactive)
  230. (setq bible-show-diatheke-exec (not bible-show-diatheke-exec)))
  231. (define-key bible-map
  232. [menu-bar bible display-diatheke]
  233. '("Toggle diatheke display" . bible-toggle-display-diatheke))
  234. (defun bible-toggle-display-xml ()
  235. "Toggle XML display."
  236. (interactive)
  237. (setq-local bible-debugme (not bible-debugme))
  238. (bible--display))
  239. (define-key bible-map "d" 'bible-toggle-display-xml)
  240. (define-key bible-map
  241. [menu-bar bible display-xml]
  242. '("Toggle XML Display" . bible-toggle-display-xml))
  243. (define-key bible-map
  244. [menu-bar bible sep]
  245. '(menu-item '"--"))
  246. ;;;;; Misc
  247. (define-key bible-map "m" 'bible-select-module)
  248. (define-key bible-map "w" 'bible-toggle-word-study)
  249. (define-key bible-map "l" 'bible-toggle-red-letter)
  250. (define-key bible-map "x" 'bible-split-display)
  251. (define-key bible-map
  252. [menu-bar bible split-display]
  253. '("Split Display" . bible-split-display))
  254. ;;;;; Search
  255. (define-key bible-map "/" 'bible-search)
  256. (define-key bible-map "s" 'bible-search)
  257. (define-key bible-map
  258. [menu-bar bible search]
  259. '("Search" . bible-search))
  260. (define-key bible-map "r" 'bible-set-search-range)
  261. (define-key bible-map
  262. [menu-bar bible range]
  263. '("Set Search Range" . bible-set-search-range))
  264. ;;;;; Navigation
  265. (define-key bible-map "p" 'bible-previous-chapter)
  266. (define-key bible-map
  267. [menu-bar bible previous-chapter]
  268. '("Previous Chapter" . bible-previous-chapter))
  269. (define-key bible-map "n" 'bible-next-chapter)
  270. (define-key bible-map
  271. [menu-bar bible next-chapter]
  272. '("Next Chapter" . bible-next-chapter))
  273. (define-key bible-map (kbd "TAB") 'bible-next-word)
  274. (define-key bible-map (kbd "M-<tab>") 'bible-previous-word)
  275. ;;;;; Direct jump
  276. (define-key bible-map "c" 'bible-select-chapter)
  277. (define-key bible-map
  278. [menu-bar bible select-chapter]
  279. '("Select Chapter" . bible-select-chapter))
  280. (define-key bible-map "b" 'bible-select-book)
  281. (define-key bible-map
  282. [menu-bar bible select-book]
  283. '("Select Book" . bible-select-book))
  284. (define-key bible-map
  285. [menu-bar bible sep]
  286. '(menu-item '"--"))
  287. ;;;;; Deal with visual-line-mode
  288. (define-key bible-map "\C-n" 'next-logical-line)
  289. (define-key bible-map "\C-p" 'previous-logical-line)
  290. (defun bible-next-search-item ()
  291. "Go to next item in list of found verses."
  292. (interactive)
  293. (search-forward-regexp bible--verse-regexp))
  294. (defun bible-previous-search-item ()
  295. "Go to previous item in list of found verses."
  296. (interactive)
  297. (search-backward-regexp bible--verse-regexp))
  298. (defconst bible-search-mode-map (make-keymap))
  299. (define-key bible-search-mode-map "s" 'bible-search)
  300. (define-key bible-search-mode-map "w" 'bible-toggle-word-study)
  301. (define-key bible-search-mode-map "n" 'bible-next-search-item)
  302. (define-key bible-search-mode-map "p" 'bible-previous-search-item)
  303. (define-key bible-search-mode-map (kbd "RET") 'bible-search-mode-follow-verse)
  304. (defconst bible-term-hebrew-mode-map (make-sparse-keymap))
  305. (defconst bible-term-greek-mode-map (make-sparse-keymap))
  306. (define-key bible-term-greek-mode-map [mouse-1] 'bible-search-mode-follow-xref)
  307. ;;;
  308. ;;; Menu bar items
  309. ;;;
  310. (defvar-local bible-text-direction 'left-to-right)
  311. (defun bible-toggle-text-direction ()
  312. "Switch between left-to-right and right-to-left text direction."
  313. (interactive)
  314. (if (eq bible-text-direction 'left-to-right)
  315. (setq-local bible-text-direction 'right-to-left)
  316. (setq-local bible-text-direction 'left-to-right))
  317. (setq-local bidi-paragraph-direction bible-text-direction))
  318. (defvar-local bible-debugme nil
  319. "Make text show up as XML when set.")
  320. (defvar-local bible-search-query nil
  321. "Query used in toggles (word study and red letter).")
  322. (defvar bible-use-tooltips t)
  323. (defun bible-toggle-tooltips ()
  324. "Toggle use of tooltips to display lexical/morphological items."
  325. (interactive)
  326. (setq bible-use-tooltips (not bible-use-tooltips))
  327. (tooltip-mode 'toggle)
  328. (setq tooltip-resize-echo-area bible-use-tooltips))
  329. (define-key bible-map
  330. [menu-bar bible sepp]
  331. '(menu-item '"--"))
  332. (define-key bible-map
  333. [menu-bar bible toggle-text-direction]
  334. '("Toggle text direction (for Hebrew display)" . bible-toggle-text-direction))
  335. (define-key bible-map
  336. [menu-bar bible toggle-tooltip-display]
  337. '("Toggle Tooltip Display" . bible-toggle-tooltips))
  338. (define-key bible-map
  339. [menu-bar bible sepp]
  340. '(menu-item '"--"))
  341. (define-key bible-map
  342. [menu-bar bible select-biblical-text]
  343. '("Select Module" . bible-display-available-modules))
  344. (defun bible--display-greek ()
  345. "Display Greek text.
  346. This command is run by clicking on text, not directly by the user."
  347. (interactive)
  348. (let ((item (car (split-string (get-text-property (point) 'strong)))))
  349. ;; Remove "strong:G" prefix
  350. (bible-term-greek (replace-regexp-in-string "strong:G" "" item))))
  351. (defconst bible-greek-keymap (make-sparse-keymap))
  352. (define-key bible-greek-keymap (kbd "RET") 'bible--display-greek)
  353. (define-key bible-greek-keymap [mouse-1] 'bible--display-greek)
  354. (defun bible--display-hebrew ()
  355. "Display Hebrew text.
  356. This command is run by clicking on text, not directly by the user."
  357. (interactive)
  358. (let ((item (car (split-string (get-text-property (point) 'strong)))))
  359. ;; Remove "strong:H" prefix and any alphabetic suffixes.
  360. (bible-term-hebrew (replace-regexp-in-string "strong:H" "" item))))
  361. (defconst bible-hebrew-keymap (make-sparse-keymap))
  362. (define-key bible-hebrew-keymap (kbd "RET") 'bible--display-hebrew)
  363. (define-key bible-hebrew-keymap [mouse-1] 'bible--display-hebrew)
  364. (defconst bible-lemma-keymap (make-sparse-keymap))
  365. (define-key bible-lemma-keymap (kbd "RET")
  366. (lambda ()
  367. (interactive)))
  368. (defconst bible-morph-keymap (make-sparse-keymap))
  369. (define-key bible-morph-keymap (kbd "RET")
  370. (lambda ()
  371. (interactive)
  372. ;;; (let ((thing (thing-at-point 'word)))
  373. ;;; (message "thing at point: %s" thing)
  374. ;;; (message "morph property %s" (get-text-property 0 'field thing))
  375. ))
  376. ;;;; Modes
  377. (define-derived-mode bible special-mode "Bible"
  378. "Mode for reading the Bible.
  379. \\{bible-map}"
  380. (buffer-disable-undo)
  381. (font-lock-mode t)
  382. (use-local-map bible-map)
  383. (setq buffer-read-only t)
  384. (visual-line-mode t))
  385. (define-derived-mode bible-search-mode special-mode "Bible Search"
  386. "Mode for performing Bible searches.
  387. \\{bible-search-mode-map}"
  388. (buffer-disable-undo)
  389. (font-lock-mode t)
  390. (use-local-map bible-search-mode-map)
  391. (setq buffer-read-only t)
  392. (visual-line-mode t))
  393. (define-derived-mode bible-term-hebrew-mode special-mode "Bible Term (Hebrew)"
  394. "Mode for researching Hebrew terms in the Bible.
  395. \\{bible-term-hebrew-mode-map}"
  396. (buffer-disable-undo)
  397. (font-lock-mode t)
  398. (use-local-map bible-term-hebrew-mode-map)
  399. (setq buffer-read-only t)
  400. (visual-line-mode t))
  401. (define-derived-mode bible-term-greek-mode special-mode "Bible Term (Greek)"
  402. "Mode for researching Greek terms in the Bible.
  403. \\{bible-term-greek-mode-map}"
  404. (buffer-disable-undo)
  405. (font-lock-mode t)
  406. (use-local-map bible-term-greek-mode-map)
  407. (setq buffer-read-only t)
  408. (visual-line-mode t))
  409. (define-derived-mode bible-module-select-mode special-mode "Select Text Module"
  410. (buffer-disable-undo)
  411. (font-lock-mode t)
  412. (setq buffer-read-only t))
  413. ;;;; Functions
  414. ;;;;; Commands
  415. ;;;###autoload
  416. (defun bible-open (&optional book-name chapter verse)
  417. "Create and open a `bible' buffer with BOOK-NAME, CHAPTER and VERSE.
  418. Arguments BOOK-NAME, CHAPTER and VERSE, when supplied, give the starting
  419. verse reference for the buffer. If no optional arguments are supplied,
  420. Genesis 1:1 is used."
  421. (interactive)
  422. (let ((buf (get-buffer-create (generate-new-buffer-name (concat "*bible*")))))
  423. (set-buffer buf)
  424. (bible)
  425. (bible--set-location
  426. (or (assoc (or book-name "Genesis") bible--books) (list book-name))
  427. (or chapter 1)
  428. verse)
  429. (set-window-buffer (get-buffer-window (current-buffer)) buf)))
  430. ;;;###autoload
  431. (defun bible-next-chapter ()
  432. "Page to the next chapter for the active `bible' buffer."
  433. (interactive)
  434. (let* ((book-chapters (cdr bible--current-book))
  435. (chapter (min book-chapters (+ bible--current-chapter 1))))
  436. (bible--set-location bible--current-book chapter)))
  437. ;;;###autoload
  438. (defun bible-previous-chapter ()
  439. "Page to the previous chapter for the active `bible' buffer."
  440. (interactive)
  441. (bible--set-location bible--current-book (max 1 (- bible--current-chapter 1))))
  442. (defun bible-next-word ()
  443. "Move forward a word, taking into account the relevant text properties."
  444. (interactive)
  445. (unless (eobp)
  446. (let ((next-change (text-property-search-forward 'strong nil nil t)))
  447. (when next-change
  448. (goto-char (1- (prop-match-end next-change)))))))
  449. (defun bible-previous-word ()
  450. "Move back a word, taking into account the relevant text properties."
  451. (interactive)
  452. (unless (bobp)
  453. (let ((previous-change (text-property-search-backward 'strong)))
  454. (when previous-change
  455. (goto-char (prop-match-beginning previous-change))))))
  456. ;;;###autoload
  457. (defun bible-select-book ()
  458. "Ask user for a new book and chapter for the current `bible' buffer."
  459. (interactive)
  460. (let* ((completion-ignore-case t)
  461. (book-data (assoc (completing-read "Book: " bible--books nil t) bible--books))
  462. (chapter (string-to-number (completing-read "Chapter: " (bible--list-number-range 1 (cdr book-data)) nil t))))
  463. (setq-local bible--current-book book-data)
  464. (setq-local bible--current-book-name (car book-data))
  465. (setq-local bible--current-chapter chapter)
  466. (bible--display)))
  467. ;;;###autoload
  468. (defun bible-select-chapter ()
  469. "Ask user for a new chapter for the current `bible' buffer."
  470. (interactive)
  471. (let* ((book-chapters (cdr bible--current-book))
  472. (chapter (string-to-number (completing-read "Chapter: " (bible--list-number-range 1 book-chapters) nil t))))
  473. (when chapter
  474. (bible--set-location bible--current-book chapter))))
  475. (defun bible-set-search-range ()
  476. "Ask user for a new text module for the current `bible' buffer."
  477. (interactive)
  478. (let ((range (read-string "Range (<return> to clear): ")))
  479. (if (string-equal range "")
  480. (setq bible-search-range nil)
  481. (setq bible-search-range range))
  482. ;; (bible--display)
  483. ))
  484. ;;;###autoload
  485. (defun bible-select-module ()
  486. "Ask user for a new text module for the current `bible' buffer."
  487. (interactive)
  488. ;; (let* ((bible-module (completing-read "Module: " bible--modules)))
  489. ;; (bible--display))
  490. (setq-default bible-module (completing-read "Module: " bible--modules))
  491. (bible--display))
  492. ;;;###autoload
  493. (defun bible-toggle-word-study ()
  494. "Toggle the inclusion of word study for the active `bible' buffer."
  495. (interactive)
  496. (setq bible-word-study-enabled (not bible-word-study-enabled))
  497. (if (equal major-mode 'bible-search-mode)
  498. (bible--display-search bible-search-query bible-search-mode)
  499. (bible--display)))
  500. (defun bible-toggle-red-letter ()
  501. "Toggle red letter mode for the active `bible' buffer."
  502. (interactive)
  503. (setq bible-red-letter-enabled (not bible-red-letter-enabled))
  504. (if (equal major-mode 'bible-search-mode)
  505. (bible--display-search bible-search-query bible-search-mode)
  506. (bible--display)))
  507. ;;;###autoload
  508. (defun bible-split-display ()
  509. "Copy the active `bible' buffer into a new buffer in another window."
  510. (interactive)
  511. (split-window-right)
  512. (balance-windows)
  513. (other-window 1)
  514. (bible-open bible--current-book-name bible--current-chapter))
  515. ;;;###autoload
  516. (defun bible-search (query)
  517. "Search for a QUERY: a word or phrase.
  518. Asks the user for type of search: either `lucene', `phrase', `regex'
  519. or `multiword'. `lucene' is the default search.
  520. `lucene' mode requires an index to be built using the `mkfastmod' program."
  521. (interactive "sBible Search: ")
  522. (when (> (length query) 0)
  523. (let* ((bible-module (buffer-local-value 'bible-module (current-buffer)))
  524. (searchmode (completing-read "Search Mode: " '("lucene" "phrase" "regex" "multiword") nil t "lucene")))
  525. (bible--open-search query searchmode))))
  526. ;;;###autoload
  527. (defun bible-search-mode-follow-verse ()
  528. "Follow the hovered verse in a `bible-search-mode' buffer.
  529. Create a new `bible' buffer positioned at the selected verse."
  530. (interactive)
  531. (let* ((text (thing-at-point 'line t))
  532. book
  533. chapter
  534. verse)
  535. (string-match ".+ [0-9]?[0-9]?[0-9]?:[0-9]?[0-9]?[0-9]?:" text)
  536. (setq text (match-string 0 text))
  537. (string-match " [0-9]?[0-9]?[0-9]?:" text)
  538. (setq chapter (replace-regexp-in-string "[^0-9]" "" (match-string 0 text)))
  539. (string-match ":[0-9]?[0-9]?[0-9]?" text)
  540. (setq verse (replace-regexp-in-string "[^0-9]" "" (match-string 0 text)))
  541. (setq book (replace-regexp-in-string "[ ][0-9]?[0-9]?[0-9]?:[0-9]?[0-9]?[0-9]?:$" "" text))
  542. (bible-open (string-trim book) (string-to-number chapter) (string-to-number verse))))
  543. (defun bible-search-mode-follow-xref ()
  544. "Follow the hovered verse in a bible term buffer.
  545. Create a new `bible' buffer positioned at the specified verse.
  546. XXX We use the current module to avoid opening cans of worms regarding
  547. OT/NT etc. If that module doesn't have that verse...???
  548. Handle abbreviations from lexicon module (AbbottSmith)."
  549. (interactive)
  550. (let* ((xref (get-text-property (point) 'xref))
  551. (verse-ref (string-split xref))
  552. book-abbrev
  553. book
  554. chapter-verse
  555. chapter
  556. verse)
  557. (if (= (length verse-ref) 3) ; II Cor 3:17 or the like
  558. (setq book-abbrev (concat (car verse-ref) (cadr verse-ref))
  559. chapter-verse (split-string (caddr verse-ref) ":"))
  560. ;; Mat 5 or the like
  561. (setq book-abbrev (car verse-ref)
  562. chapter-verse (split-string (cadr verse-ref) ":")))
  563. (setq book (cdr (assoc book-abbrev bible--book-name-abbreviations-alist)))
  564. (unless book (setq book (car verse-ref))) ; Didn't find abbreviation
  565. (setq chapter (car chapter-verse)
  566. verse (cadr chapter-verse))
  567. (bible-open (string-trim book) (string-to-number chapter) (string-to-number verse))))
  568. ;;;###autoload
  569. (defun bible-term-hebrew (term)
  570. "Query user for a Strong's Hebrew Lexicon TERM."
  571. (interactive "sTerm: ")
  572. (bible--open-term-hebrew term))
  573. ;;;###autoload
  574. (defun bible-term-greek (term)
  575. "Query user for a Strong's Greek Lexicon TERM."
  576. (interactive "sTerm: ")
  577. (bible--open-term-greek term))
  578. ;;;###autoload
  579. (defun bible-insert ()
  580. "Query user to select a verse for insertion into the current buffer."
  581. (interactive)
  582. (let* ((completion-ignore-case t)
  583. (book-data (assoc (completing-read "Book: " bible--books nil t) bible--books))
  584. (chapter (when book-data (completing-read "Chapter: " (bible--list-number-range 1 (cdr book-data)) nil t)))
  585. (verse (when chapter (read-from-minibuffer "Verse: "))))
  586. (when verse
  587. (insert (string-trim
  588. (replace-regexp-in-string
  589. (regexp-opt `(,(concat "(" bible-module ")")))
  590. ""
  591. (bible--exec-diatheke (concat (car book-data) " " chapter ":" verse) nil "plain")))))))
  592. ;;;;; Support
  593. (defconst bible-diatheke-filter-options " avlnmw")
  594. ;;;
  595. ;;; XXX I've mangled this in an ad-hoc manner. It needs to be
  596. ;;; re-written so it is clearer (and correct, for that matter).
  597. (defun bible--exec-diatheke (query &optional filter format searchtype module)
  598. "Execute `diatheke' with specified QUERY options, returning the output.
  599. FILTER is the Diatheke filter argument. FORMAT is either plain or the
  600. default of internal. SEARCHTYPE is the Diatheke search type and MODULE
  601. is the text module to use."
  602. (let ((module (or module bible-module)))
  603. (with-temp-buffer
  604. (let ((args (list "diatheke" nil (current-buffer) t "-b" module)))
  605. (if searchtype
  606. (progn
  607. (setq filter nil) ; search is not compatible with filters XXX
  608. (setq args (append args (list "-s" (pcase searchtype
  609. ("lucene" "lucene")
  610. ("phrase" "phrase")
  611. ("regex" "regex")
  612. ("multiword" "multiword")))))
  613. (when bible-search-range (setq args (append args (list "-r" bible-search-range)))))
  614. (progn
  615. (if filter
  616. (setq filter (concat filter bible-diatheke-filter-options))
  617. (setq filter bible-diatheke-filter-options))
  618. (setq args (append args (list "-o" filter)))))
  619. (setq args (append args (list "-f" (pcase format ("plain" "plain") (_ "internal")) "-k" query)))
  620. (when bible-show-diatheke-exec
  621. (message "%s" args))
  622. (apply 'call-process args))
  623. (buffer-string))))
  624. (defvar-local bible-chapter-title nil
  625. "Text preceding start of chapter.
  626. Mostly in Psalms, like `Of David' or the like.")
  627. ;;;
  628. ;;; Greek and Hebrew lexicon and morphology tooltip rendering.
  629. ;;;
  630. ;;; Hash tables for Lexical definitions.
  631. (defvar bible-greek-hash (make-hash-table :test 'equal))
  632. (defvar bible-greek-hash-short (make-hash-table :test 'equal))
  633. (defvar bible-hebrew-hash (make-hash-table :test 'equal))
  634. (defvar bible-hebrew-hash-short (make-hash-table :test 'equal))
  635. ;; Do lookups using index to lexicon with lookups by lemma.
  636. (defvar bible-lemma-index-hash (make-hash-table :test 'equal))
  637. ;; (defvar bible-lemma-lex-hash (make-hash-table :test 'equal))
  638. ;;; Hash tables for Morphologies. Three at present.
  639. (defvar bible-robinson-hash (make-hash-table :test 'equal))
  640. (defvar bible-packard-hash (make-hash-table :test 'equal))
  641. (defvar bible-oshm-hash (make-hash-table :test 'equal))
  642. ;;; Use HTMLHREF format with diatheke, post-process to render html.
  643. (defun bible--morph-query (query module)
  644. "Execute `diatheke' to do morph QUERY, using MODULE.
  645. Render HTML, return string. Do some tweaking specific to morphology."
  646. (with-temp-buffer
  647. (let ((args (list "diatheke" nil (current-buffer) t "-b" module "-o" "m" "-f" "HTMLHREF" "-k" query)))
  648. (when bible-show-diatheke-exec
  649. (message "%s" args))
  650. (apply 'call-process args)
  651. (shr-render-region (point-min) (point-max))
  652. (format-replace-strings
  653. '(("\n:" . "") ; This makes the Packard morphology display look better.
  654. ("Part of Speech" . "")) ; This helps the Robinson display look better.
  655. nil (point-min) (point-max))
  656. (substring (buffer-string) (+ (length query) 1))) ; This tries to get rid of unnecessary query identifier.
  657. ))
  658. ;;; Use "plain" format with diatheke.
  659. (defun bible--lex-query (query module)
  660. "Execute `diatheke' for QUERY, using MODULE.
  661. Plain format, returns string."
  662. ;; Get rid of query ID at front of string: ?????:
  663. (bible--exec-diatheke query nil "plain" nil module))
  664. (defun bible--lookup-lemma-index (key)
  665. "Return the Greek lemma from lemma index with a strong's number as KEY."
  666. (or (gethash key bible-lemma-index-hash)
  667. (puthash key
  668. (string-trim
  669. (replace-regexp-in-string
  670. (concat "(" bible-lexicon-index ")") ""
  671. (bible--lex-query key bible-lexicon-index)))
  672. bible-lemma-index-hash)))
  673. ;;;
  674. ;;; The Greek lexical definitions are done using the HTMLHREF output
  675. ;;; format so they come out looking nice and having clickable
  676. ;;; cross-references and/or Strong's references.
  677. (defun bible--process-href ()
  678. "Fix the XML so cross-references are in the right format.
  679. These cross-references get processed later when the term is displayed.
  680. First, find the links put in by diatheke's HTMLHREF output format.
  681. Replace the links with verse references that get changed to clickable
  682. cross-references when the term is displayed.
  683. The verse refs look like this: <bookname>.<chapter>.<verse>. We convert
  684. them to the <bookname> <chapter>:<verse> format."
  685. (goto-char (point-min))
  686. (while (re-search-forward "<a href=\"passagestudy.*?</a>" nil t) ; HTMLHREF cross references.
  687. (let ((match-text (match-string 0)))
  688. ;; Delete original link.
  689. (replace-match "" nil nil)
  690. ;; Get the verse reference from the string we saved. Put it in
  691. ;; good format, then insert it into buffer where href was.
  692. (when (string-match "value=.*?&" match-text)
  693. (let* ((value-string (match-string 0 match-text))
  694. ;; Strip off value= and trailing &.
  695. (verse-ref-string (substring value-string 6 (1- (length value-string))))
  696. (verse-ref-length (length verse-ref-string))
  697. period)
  698. ;; Convert periods
  699. ;; Substitute first period with space
  700. (when (setq period (cl-search "." verse-ref-string))
  701. (aset verse-ref-string period ? ))
  702. ;; Substitute second period with colon
  703. (when (setq period (cl-search "." verse-ref-string))
  704. (aset verse-ref-string period ?:))
  705. ;; Replace numbers (1, 2 or 3) with roman numerals (I, II, III) XXX Fix?
  706. (pcase (aref verse-ref-string 0)
  707. (?1 (setq verse-ref-string (concat "I" (substring verse-ref-string 1))))
  708. (?2 (setq verse-ref-string (concat "II" (substring verse-ref-string 1))))
  709. (?3 (setq verse-ref-string (concat "III" (substring verse-ref-string 1)))))
  710. (set-text-properties 0 verse-ref-length nil verse-ref-string) ; Clear unwanted properties (if any)
  711. (insert verse-ref-string))))))
  712. (defun bible--lookup-greek-def (key)
  713. "Execute `diatheke' to do query on KEY.
  714. Massage output so verse cross references are usable. Returns string.
  715. We use HTMLHREF format output because it may have verse references
  716. as HTML links, depending on the lexicon module."
  717. (with-temp-buffer
  718. (let ((args (list "diatheke" nil (current-buffer) t "-b" bible-greek-lexicon "-o" "m" "-f" "HTMLHREF" "-k" key)))
  719. ;; XXX Change to OSIS? Need to parse OSIS-style references.
  720. ;;; (let ((args (list "diatheke" nil (current-buffer) t "-b" bible-greek-lexicon "-o" "m" "-f" "OSIS" "-k" key)))
  721. (when bible-show-diatheke-exec
  722. (message "%s" args))
  723. (apply 'call-process args)
  724. (bible--process-href) ; Clean up XML so xrefs can work after rendering.
  725. (shr-render-region (point-min) (point-max))
  726. (buffer-string))))
  727. (defun bible--lookup-lex-greek-indexed (key)
  728. "Lookup Greek lemma using Strong's number KEY.
  729. Then look up the definition of that lemma. Used when two-stage
  730. lexical definition is set for a particular lexicon."
  731. (let ((lemma-entry (bible--lookup-lemma-index key))) ; Get lemma from Strong's number
  732. (when lemma-entry
  733. (let ((lemma (caddr (split-string lemma-entry " "))))
  734. (bible--lookup-greek-def lemma)))))
  735. (defun bible--lookup-lex-greek (key)
  736. "Lookup lexical definition using Strong's number KEY.
  737. 1. Check hash table first. If entry found, return.
  738. 2. Otherwise, if a lexicon is accessed by lemmas, do lookup using index method.
  739. 3. Otherwise just use the Strong's number method."
  740. (or (gethash key bible-greek-hash)
  741. (puthash key
  742. (if bible-use-index-for-lexicon
  743. (bible--lookup-lex-greek-indexed key)
  744. (bible--lookup-greek-def key))
  745. bible-greek-hash)))
  746. (defun bible--lookup-strongs-greek (_window object pos)
  747. "Look up Greek lexical entry of OBJECT clicked on in WINDOW at POS.
  748. If not found in hash table, get it from sword database. Stash in hash
  749. table, and return string."
  750. (let ((query (get-text-property pos 'strong object))) ; Get Strong's number from text property
  751. (when (string-match "[0-9]+" query)
  752. (bible--lookup-lex-greek (match-string 0 query)))))
  753. (defun bible--lookup-strongs-greek-short (_window object pos)
  754. "Look up shorter Greek lexical entry of OBJECT clicked on in WINDOW at POS.
  755. If not found in hash table, get it from sword database, stash in hash table,
  756. and return string."
  757. (let ((query (get-text-property pos 'strong object))) ; Get Strong's number from text property
  758. (when (string-match "[0-9]+" query)
  759. (let ((lookup-key (match-string 0 query)))
  760. (or (gethash lookup-key bible-greek-hash-short)
  761. (let ((raw-text (bible--lex-query lookup-key bible-greek-lexicon-short)))
  762. (puthash lookup-key raw-text bible-greek-hash-short)))))))
  763. (defun bible--lookup-strongs-hebrew (_window object pos)
  764. "Look up Hebrew lexical string for OBJECT at point POS.
  765. If not found in hash table, get it from sword database,
  766. stash in hash table, and return string."
  767. (let ((query (get-text-property pos 'strong object)))
  768. (when (string-match "[0-9]+" query)
  769. (let ((lookup-key (concat "H" (match-string 0 query))))
  770. (or (gethash lookup-key bible-hebrew-hash)
  771. (let ((raw-text (bible--lex-query lookup-key bible-hebrew-lexicon)))
  772. (puthash lookup-key raw-text bible-hebrew-hash)))))))
  773. (defun bible--lookup-strongs-hebrew-short (_window object pos)
  774. "Look up Hebrew lexical text for OBJECT at POS from Hebrew lexicon.
  775. If not found in hash table, get it from sword database, stash in hash
  776. table, and return string."
  777. (let ((query (get-text-property pos 'strong object)))
  778. (when (string-match "[0-9]+" query)
  779. (let ((lookup-key (concat "H" (match-string 0 query))))
  780. (or (gethash lookup-key bible-hebrew-hash-short)
  781. (let ((raw-text (bible--lex-query lookup-key bible-hebrew-lexicon-short)))
  782. (puthash lookup-key raw-text bible-hebrew-hash-short)))))))
  783. (defun bible--morph-database-lookup (query database hash)
  784. "Look up morphology QUERY in HASH.
  785. If not found get it from DATABASE and store in HASH."
  786. (or (gethash query hash)
  787. (puthash query (bible--morph-query query database) hash)))
  788. (defvar bible-outline-strings
  789. '((" I." . "\n I.")
  790. (" II." . "\n II.")
  791. (" III." . "\n III.")
  792. (" IV." . "\n IV.")
  793. (" V." . "\n V.")
  794. (" 1." . "\n 1.")
  795. (" 2." . "\n 2.")
  796. (" 3." . "\n 3.")
  797. (" 4." . "\n 4.")
  798. (" 5." . "\n 5.")
  799. (" 6." . "\n 6.")
  800. (" 7." . "\n 7.")
  801. (" 8." . "\n 8.")
  802. (" 9." . "\n 9.")
  803. (" a." . "\n a.")
  804. (" b." . "\n b.")
  805. (" c." . "\n c.")
  806. (". ." . ".")
  807. (" . " . ". ")))
  808. (defun bible-cleanup-tooltip-text (lex-text)
  809. "Reformat tooltip text LEX-TEXT so tooltips look nice."
  810. (dolist (outline-string bible-outline-strings)
  811. (setq lex-text (string-replace (car outline-string) (cdr outline-string) lex-text)))
  812. lex-text)
  813. ;;;
  814. ;;; Get string for tooltip display
  815. ;;;
  816. (defun bible--show-lex-morph (window object pos)
  817. "Get text for tooltip display for OBJECT at POS in WINDOW.
  818. Includes both lex and morph definitions if text module has
  819. both tags, otherwise just get lex definition."
  820. (let* ((lex-morph-text "")
  821. (lex (get-text-property pos 'strong object))
  822. (lex-module nil)
  823. (lex-text
  824. (cond ((string-match "strong:G" lex)
  825. (bible--lookup-strongs-greek-short window object pos))
  826. ((string-match "strong:H" lex)
  827. (bible--lookup-strongs-hebrew-short window object pos)))))
  828. (setq lex-text (string-replace (concat "(" lex-module ")") "" lex-text))
  829. (let* ((morph (get-text-property pos 'morph object))
  830. (morph-module nil)
  831. (morph-text
  832. (cond ((null morph) nil)
  833. ((string-match "robinson:" morph)
  834. (setq morph-module "Robinson")
  835. (bible--morph-database-lookup (replace-regexp-in-string "robinson:" "" morph) morph-module bible-robinson-hash))
  836. ((string-match "packard:" morph)
  837. (setq morph-module "Packard")
  838. (bible--morph-database-lookup (replace-regexp-in-string "packard:" "" morph) morph-module bible-packard-hash))
  839. ((string-match "oshm:" morph)
  840. (setq morph-module "OSHM")
  841. (bible--morph-database-lookup (replace-regexp-in-string "oshm:" "" morph) morph-module bible-oshm-hash)))))
  842. (when lex-text
  843. (setq lex-morph-text (string-trim (bible-cleanup-tooltip-text (string-fill lex-text 75)))))
  844. (when morph-text
  845. (setq lex-morph-text
  846. (concat lex-morph-text "\n\n"
  847. (string-trim (string-replace (concat "(" morph-module ")") "" morph-text)))))
  848. ;; This prevents bogus command substitutions in the tooltip by
  849. ;; removing backslashes. XXX I couldn't figure out a better way
  850. ;; to bypass command substitution in the tooltips.
  851. (setq lex-morph-text (replace-regexp-in-string "\\\\" "" lex-morph-text)))))
  852. (defun bible-handle-divine-name (item)
  853. "When ITEM is divine name, display it as such."
  854. (insert "LORD")
  855. (let* ((refstart (- (point) (length "LORD")))
  856. (refend (point))
  857. (strongs (dom-attr item 'savlm)))
  858. (add-face-text-property refstart refend 'bold)
  859. (put-text-property refstart refend 'keymap bible-hebrew-keymap)
  860. (when (and strongs (string-match "strong:H.*" strongs))
  861. (put-text-property refstart refend 'help-echo 'bible--show-lex-morph)
  862. (put-text-property refstart refend 'strong (match-string 0 strongs)))))
  863. (defun bible--process-word (item iproperties)
  864. "Handle <w ...> fubar </w> tag in ITEM. Check IPROPERTIES for qualifiers.
  865. Add tooltips for definitions and morphology. Also insert lemmas in buffer
  866. (must be done after item is inserted in buffer)."
  867. (let ((word (string-trim (dom-text item)))
  868. (morph (dom-attr item 'morph))
  869. (savlm (dom-attr item 'savlm))
  870. (lemma (dom-attr item 'lemma))
  871. (divinename (dom-by-tag item 'divinename)))
  872. (insert word)
  873. (let ((refstart (- (point) (length word)))
  874. (refend (point)))
  875. ;; Red letter (Yuck, some modules need this below)
  876. (when (plist-get iproperties 'jesus)
  877. (add-face-text-property refstart refend '(:foreground "red")))
  878. ;; Special case this. XXX Some modules do this differently.
  879. (when divinename
  880. (insert " ")
  881. (bible-handle-divine-name item))
  882. ;; lexical definitions
  883. (when (or savlm lemma)
  884. (let ((matched nil)
  885. (item (or savlm lemma)))
  886. (cond ((string-match "strong:G.*" item) ; Greek
  887. (setq matched (match-string 0 item))
  888. (put-text-property refstart refend 'keymap bible-greek-keymap))
  889. ((string-match "strong:H.*" item) ; Hebrew
  890. (setq matched (match-string 0 item))
  891. (put-text-property refstart refend 'keymap bible-hebrew-keymap)))
  892. ;; Add help-echo, strongs reference for tooltips if match.
  893. (when matched
  894. (setq-local bible-has-strongs t)
  895. (put-text-property refstart refend 'help-echo 'bible--show-lex-morph)
  896. (put-text-property refstart refend 'strong matched))))
  897. ;; morphology
  898. (when morph
  899. (let ((matched nil))
  900. (cond ((string-match "robinson:.*" morph) ; Robinson Greek morphology
  901. (setq matched (match-string 0 morph)))
  902. ((string-match "packard:.*" morph) ; Packard Greek morphology --- LXX seems to use this
  903. (setq matched (match-string 0 morph)))
  904. ((string-match "oshm:.*" morph) ; OSHM Hebrew morphology
  905. (setq matched (match-string 0 morph)))
  906. (t nil
  907. ;;(message "Unknown morphology %s" morph)
  908. ))
  909. (when matched
  910. (setq-local bible-has-morphology t)
  911. (put-text-property refstart refend 'morph matched)
  912. (put-text-property refstart refend 'help-echo 'bible--show-lex-morph))))
  913. ;; Insert lemma into buffer. Lemma tag will be part of savlm item.
  914. ;; XXX Should I do lexicon lookups on these lemmas? I don't use
  915. ;; this anyway....
  916. (when (and bible-word-study-enabled savlm (string-match "lemma.*:.*" savlm))
  917. (dolist (word (split-string (match-string 0 savlm) " "))
  918. (setq word (replace-regexp-in-string "[.:a-zA-Z0-9]+" "" word))
  919. (insert " " word)
  920. (let ((refstart (- (point) 1 (length word)))
  921. (refend (point)))
  922. (add-face-text-property refstart refend '(:foreground "blue"))
  923. (put-text-property refstart refend 'keymap bible-lemma-keymap)))))))
  924. (defun bible-new-line ()
  925. "Ensure beginning of line. Try to avoid redundant blank lines."
  926. (unless (= (current-column) 0)
  927. (insert "\n")))
  928. (defun bible--insert-domnode-recursive (node &optional iproperties notitle)
  929. "Recursively parse domnode NODE obtained from `libxml-parse-html-region'.
  930. Inserts resulting text into active buffer with properties specified in
  931. IPROPERTIES. If NOTITLE is true, don't render title headings.
  932. XXX In processing subnodes, each case will prepend a space if it needs it."
  933. (when (and bible-red-letter-enabled (equal (dom-attr node 'who) "Jesus"))
  934. ;; For red-letter display.
  935. (setq iproperties (plist-put iproperties 'jesus t)))
  936. (dolist (subnode (dom-children node))
  937. (cond ((null subnode) nil)
  938. ((stringp subnode)
  939. (let ((search-string (concat (car bible--current-book) " " (number-to-string bible--current-chapter) ":"))
  940. (start 0))
  941. ;; Delete <Book Ch:> (may be more than one)
  942. (setq subnode (string-replace search-string "" subnode))
  943. ;; Highlight verse number(s)
  944. (while (string-match "^ ?[0-9]+:" subnode start)
  945. (setq start (match-end 0))
  946. (add-face-text-property (match-beginning 0) (match-end 0) '(:foreground "purple") nil subnode)))
  947. ;; Red letter
  948. (when (plist-get iproperties 'jesus)
  949. (add-face-text-property 0 (length subnode) '(:foreground "red") nil subnode))
  950. (insert subnode))
  951. ;;;; Maybe process these at some point? Include footnotes etc.
  952. ;;; ((eq (dom-tag subnode) 'node) nil)
  953. ;;; ((eq (dom-tag subnode) 'lb) nil)
  954. ;; These tags appear in ESV modules (and maybe others?) XXX still not right
  955. ((eq (dom-tag subnode) 'l)
  956. (let ((attributes (dom-attributes subnode)))
  957. (cond ((equal (cdr (assoc 'type attributes)) "x-br")
  958. (bible-new-line))
  959. ((equal (cdr (assoc 'type attributes)) "x-indent")
  960. (insert "\t"))
  961. ((assoc 'level attributes)
  962. (let ((indent (string-to-number (cdr (assoc 'level attributes)))))
  963. (when (= indent 2) (insert "\n\t")))))))
  964. ((eq (dom-tag subnode) 'title) (when (not notitle) (setq bible-chapter-title subnode) (bible-new-line)))
  965. ((eq (dom-tag subnode) 'body) (bible--insert-domnode-recursive subnode iproperties notitle))
  966. ;; NASB Module uses this to indicate OT quotations (and others?).
  967. ((eq (dom-tag subnode) 'seg) (bible--insert-domnode-recursive subnode iproperties notitle))
  968. ((eq (dom-tag subnode) 'divinename) (bible-handle-divine-name subnode))
  969. ;; This tag is used for red letter.
  970. ((eq (dom-tag subnode) 'q) (bible--insert-domnode-recursive subnode iproperties notitle))
  971. ((eq (dom-tag subnode) 'p) (bible--insert-domnode-recursive subnode iproperties notitle))
  972. ((eq (dom-tag subnode) 'w) (insert " ") (bible--process-word subnode iproperties))
  973. ;; Some modules use this for line breaks and such.
  974. ((or (and (eq (dom-tag subnode) 'milestone) (equal (cdr (assoc 'type (dom-attributes subnode))) "line"))
  975. (and (eq (dom-tag subnode) 'div) (equal (cdr (assoc 'type (dom-attributes subnode))) "paragraph")))
  976. (bible-new-line))
  977. ((or (eq (dom-tag subnode) 'transchange) (eq (dom-tag subnode) 'hi))
  978. ;; Word inserted by translation, not in original, give visual indication.
  979. (let ((word (dom-text subnode)))
  980. (insert " " word)
  981. (if (plist-get iproperties 'jesus)
  982. (add-face-text-property (- (point) (length word)) (point) '(:foreground "salmon"))
  983. (add-face-text-property (- (point) (length word)) (point) '(:foreground "gray50"))))))))
  984. ;;(defvar bible-debugme nil)
  985. (defun bible--display (&optional verse)
  986. "Render text for `bible'. If VERSE is supplied, set cursor at verse."
  987. ;; Display buffers can have different modules.
  988. (setq-local bible-module (default-value 'bible-module))
  989. ;; Clear buffer and insert the result of calling bible--exec-diatheke.
  990. (setq buffer-read-only nil)
  991. (erase-buffer)
  992. (setq bible-chapter-title nil
  993. bible-has-strongs nil
  994. bible-has-morphology nil)
  995. (insert (bible--exec-diatheke (concat bible--current-book-name ":" (number-to-string bible--current-chapter))))
  996. ;; Parse the xml in the buffer into a DOM tree.
  997. (let ((html-dom-tree (libxml-parse-html-region (point-min) (point-max))))
  998. ;; Render the DOM tree into the buffer.
  999. (unless bible-debugme
  1000. (erase-buffer)
  1001. ;; Looking for the "body" tag in the DOM node.
  1002. (bible--insert-domnode-recursive (dom-by-tag html-dom-tree 'body) nil nil)
  1003. (goto-char (point-min))))
  1004. ;; Remove the module name from the buffer.
  1005. (while (search-forward (concat "(" bible-module ")") nil t)
  1006. (replace-match ""))
  1007. ;; Set the mode line of the biffer.
  1008. (setq mode-name (concat "Bible (" bible--current-book-name " " (number-to-string bible--current-chapter) ") "
  1009. bible-module
  1010. (when bible-has-strongs " Lex")
  1011. (when bible-has-morphology " Morph")
  1012. ")"))
  1013. ;; Deal with chapter titles (i.e. in Psalms)
  1014. ;; N.B. This won't change a title inside a chapter, and so it
  1015. ;; doesn't work with Psalm 119 where the acrostic letters get
  1016. ;; printed as "titles".
  1017. (when bible-chapter-title ; This gets set in bible-insert-domnode-recursive.
  1018. (goto-char (point-min))
  1019. (let ((title-text (dom-texts bible-chapter-title))
  1020. (refstart (point-min))
  1021. refend)
  1022. ;; Insert and make bold the title.
  1023. (when (string-or-null-p title-text)
  1024. (insert title-text "\n")
  1025. (setq refend (point))
  1026. (put-text-property refstart refend 'face 'bold))))
  1027. ;; Get rid of spurious spaces.
  1028. (format-replace-strings '(("." . ". ")
  1029. ("," . ", ")
  1030. (";" . "; ")
  1031. (":" . ": ")
  1032. ("?" . "? ")
  1033. ("!" . "! ")
  1034. (" ." . ". ")
  1035. (" ," . ", ")
  1036. (" ;" . "; ")
  1037. (" :" . ": ")
  1038. (" ?" . "? ")
  1039. (" !" . "! ")
  1040. ("“ " . "“")
  1041. ("‘ " . "‘")
  1042. (" ’" . "’")
  1043. (". ”" . ".”")
  1044. ("? ”" . "?”")
  1045. (" " . " ")
  1046. (" " . " "))
  1047. nil (point-min) (point-max))
  1048. (setq buffer-read-only t)
  1049. (goto-char (point-min))
  1050. ;; If optional verse specification go to that verse.
  1051. (when verse
  1052. (goto-char (+ 1 (string-match (regexp-opt `(,(concat (number-to-string verse) ":"))) (buffer-string))))
  1053. ;; (beginning-of-line)
  1054. ))
  1055. (defun bible--list-biblical-modules ()
  1056. "Return a list of accessible Biblical Text modules."
  1057. (let ((text (bible--exec-diatheke "modulelist" nil nil nil "system"))
  1058. modules)
  1059. (catch 'done
  1060. (dolist (line (split-string text "[\n\r]+"))
  1061. (when (equal line "Commentaries:")
  1062. (throw 'done nil))
  1063. (when (not (equal "Biblical Texts:" line))
  1064. (push (split-string line " : ") modules))))
  1065. modules))
  1066. (defun bible-pick-module ()
  1067. "Keymap action function---select module user chooses."
  1068. (interactive)
  1069. (let ((item (get-text-property (point) 'module)))
  1070. (setq-default bible-module item)
  1071. (bible-open)))
  1072. (defconst bible-module-map (make-keymap))
  1073. (define-key bible-module-map [mouse-1] 'bible-pick-module)
  1074. (define-key bible-module-map (kbd "RET") 'bible-pick-module)
  1075. (defun bible-display-available-modules ()
  1076. "Display available modules, allow user to select."
  1077. (interactive)
  1078. (let ((buf (get-buffer-create "Modules"))
  1079. (mods (bible--list-biblical-modules)))
  1080. (set-buffer buf)
  1081. (bible-module-select-mode)
  1082. (setq buffer-read-only nil)
  1083. (erase-buffer)
  1084. (setq-local tab-stop-list '(25))
  1085. (dolist (mod mods)
  1086. (let ((name (string-trim (car mod)))
  1087. (description (string-trim-left (cadr mod))))
  1088. (insert
  1089. (propertize (string-trim name)
  1090. 'face 'bold
  1091. 'module name
  1092. 'help-echo (concat "Select " name)
  1093. 'keymap bible-module-map))
  1094. (move-to-tab-stop)
  1095. (insert (format "%s\n" description))))
  1096. (setq buffer-read-only t)
  1097. (goto-char (point-min))
  1098. (pop-to-buffer buf nil t)))
  1099. ;;;;; Bible Searching
  1100. (defun bible--open-search (query searchmode)
  1101. "Open a search buffer of QUERY using SEARCHMODE in module MOD."
  1102. (let ((buf (get-buffer-create (concat "*bible-search-" (downcase bible-module) "-" query "*"))))
  1103. (set-buffer buf)
  1104. (bible-search-mode)
  1105. (bible--display-search query searchmode)
  1106. (pop-to-buffer buf nil t)))
  1107. (defun bible--display-search (query searchmode)
  1108. "Render results of search QUERY from SEARCHMODE."
  1109. (setq buffer-read-only nil)
  1110. (erase-buffer)
  1111. (let* ((result (string-trim (replace-regexp-in-string
  1112. "Entries .+?--" ""
  1113. (bible--exec-diatheke query nil "plain" searchmode bible-module))))
  1114. (match 0)
  1115. (matchstr "")
  1116. (verses nil)
  1117. (query-verses "")
  1118. fullverses)
  1119. (if (equal result (concat "none (" bible-module ")"))
  1120. (insert "No results found."
  1121. (if (equal searchmode "lucene")
  1122. " Verify index has been build with mkfastmod."
  1123. ""))
  1124. (progn
  1125. (while match
  1126. (setq match (string-match ".+?:[0-9]?[0-9]?" result (+ match (length matchstr)))
  1127. matchstr (match-string 0 result))
  1128. (when match
  1129. (push
  1130. ;; Massage match to make it more sortable, get rid of some characters.
  1131. (replace-regexp-in-string
  1132. "I " "1"
  1133. (replace-regexp-in-string
  1134. "II " "2"
  1135. (replace-regexp-in-string
  1136. "III " "3"
  1137. (replace-regexp-in-string ".+; " "" matchstr))))
  1138. verses)))
  1139. (setq match 0)
  1140. (setq verses (sort verses))
  1141. (dolist (verse verses)
  1142. (if query-verses
  1143. (setq query-verses (concat query-verses ";" verse))
  1144. (setq query-verses verse)))
  1145. (setq fullverses (let ((bible-show-diatheke-exec nil))
  1146. (bible--exec-diatheke query-verses nil nil nil bible-module)))
  1147. (insert fullverses)
  1148. (let* ((html-dom-tree (libxml-parse-html-region (point-min) (point-max))))
  1149. (erase-buffer)
  1150. (bible--insert-domnode-recursive (dom-by-tag html-dom-tree 'body) nil nil)
  1151. (goto-char (point-min))
  1152. (while (search-forward (concat "(" bible-module ")") nil t)
  1153. (replace-match "")))))
  1154. (setq mode-name (concat "Bible Search (" bible-module))
  1155. (when bible-search-range
  1156. (setq mode-name (concat mode-name " [" bible-search-range "]")))
  1157. (setq mode-name (concat mode-name ")"))
  1158. (setq buffer-read-only t)
  1159. (setq-local bible-search-query query)
  1160. (setq-local bible-search-mode searchmode)
  1161. (goto-char (point-min))))
  1162. ;;;;; Terms
  1163. ;;(defun bible-display-morphology (morph)
  1164. ;; ;; xxx Do something here?
  1165. ;; )
  1166. (defun bible--display-term (termtype)
  1167. "Display a term of language given by TERMTYPE."
  1168. (setq buffer-read-only nil)
  1169. (goto-char (point-min))
  1170. ;; This enables clicking on Strong's numbers in some lexicon definitions.
  1171. (while (search-forward-regexp "[0-9]+" nil t)
  1172. (cond ((eq termtype 'hebrew)
  1173. (put-text-property (match-beginning 0) (match-end 0) 'strong (concat "strong:H" (match-string 0)))
  1174. (put-text-property (match-beginning 0) (match-end 0) 'keymap bible-hebrew-keymap)
  1175. (add-face-text-property (match-beginning 0) (match-end 0) `(:foreground "blue")))
  1176. ((eq termtype 'greek)
  1177. (put-text-property (match-beginning 0) (match-end 0) 'strong (concat "strong:G" (match-string 0)))
  1178. (put-text-property (match-beginning 0) (match-end 0) 'keymap bible-greek-keymap)
  1179. (add-face-text-property (match-beginning 0) (match-end 0) `(:foreground "blue")))))
  1180. (goto-char (point-min))
  1181. ;; This enables clicking on verse references.
  1182. (while (search-forward-regexp bible--verse-regexp nil t)
  1183. (put-text-property (match-beginning 0) (match-end 0) 'xref (match-string 0))
  1184. (put-text-property (match-beginning 0) (match-end 0) 'keymap bible-search-mode-map)
  1185. (put-text-property (match-beginning 0) (match-end 0) 'help-echo (concat "Go to " (match-string 0)))
  1186. (add-face-text-property (match-beginning 0) (match-end 0) '(:foreground "blue")))
  1187. (goto-char (point-min))
  1188. (while (search-forward "()" nil t)
  1189. (replace-match ""))
  1190. (goto-char (point-min))
  1191. (setq buffer-read-only t))
  1192. (defun bible--open-term-hebrew (term)
  1193. "Open a buffer of the Strong's Hebrew TERM's definition."
  1194. (let ((buf (get-buffer-create (concat "*bible-term-hebrew-" term "*"))))
  1195. (set-buffer buf)
  1196. (bible-term-hebrew-mode)
  1197. (bible--display-term-hebrew term)
  1198. (pop-to-buffer buf nil t)
  1199. (fit-window-to-buffer)))
  1200. (defun bible--open-term-greek (term)
  1201. "Open a buffer of the Strong's Greek TERM definition."
  1202. (let ((buf (get-buffer-create (concat "*bible-term-greek-" term "*"))))
  1203. (set-buffer buf)
  1204. (bible-term-greek-mode)
  1205. (bible--display-term-greek term)
  1206. (pop-to-buffer buf nil t)
  1207. (fit-window-to-buffer)))
  1208. ;;;
  1209. ;;; Note: Hebrew display of terms is backwards; set bidi direction to
  1210. ;;; 'left-to-right.
  1211. (defun bible--display-term-hebrew (term)
  1212. "Render the definition of the Strong's Hebrew TERM.
  1213. Sets the variable `bidi-paragraph-direction' so the English text will
  1214. render left-to-right. XXX Why doesn't this work for the tooltips?"
  1215. (setq buffer-read-only nil)
  1216. (erase-buffer)
  1217. (insert (replace-regexp-in-string
  1218. (regexp-opt `(,bible-hebrew-lexicon))
  1219. ""
  1220. (bible--exec-diatheke term nil "plain" nil bible-hebrew-lexicon)
  1221. nil nil nil 7))
  1222. (bible--display-term 'hebrew)
  1223. (setq bidi-paragraph-direction 'left-to-right))
  1224. (defun bible--display-term-greek (term)
  1225. "Render the definition of the Strong's Greek TERM."
  1226. (setq buffer-read-only nil)
  1227. (erase-buffer)
  1228. (insert (replace-regexp-in-string
  1229. (regexp-opt `(,bible-greek-lexicon))
  1230. ""
  1231. (bible--lookup-lex-greek term)))
  1232. (bible--display-term 'greek))
  1233. (defun bible--set-location (book chapter &optional verse)
  1234. "Set the BOOK, CHAPTER and optionally VERSE of the active `bible' buffer."
  1235. (setq-local bible-module (default-value 'bible-module))
  1236. (setq-local bible--current-book book)
  1237. (setq-local bible--current-book-name (car book))
  1238. (setq-local bible--current-chapter chapter)
  1239. (bible--display verse))
  1240. ;;;;; Utilities
  1241. (defun bible--list-number-range (min max &optional prefix)
  1242. "Returns a list containing entries for each integer between MIN and MAX.
  1243. If PREFIX is supplied, prepend PREFIX to the entries.
  1244. Used in tandem with `completing-read' for chapter selection."
  1245. (let ((range-list nil))
  1246. (dotimes (num (1+ max))
  1247. (when (>= num min)
  1248. (push (cons (concat prefix (number-to-string num)) num) range-list)))
  1249. (nreverse range-list)))
  1250. ;;; Provides
  1251. (provide 'bible)
  1252. ;;; bible.el ends here.
  1253. ;; Local Variables:
  1254. ;; End: