bible-mode.el 55 KB

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