| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527 |
- ;;; -*- mode: EMACS-LISP; lexical-binding: t -*-
- ;;
- ;; bible.el --- A browsing interface for the SWORD Project's Diatheke CLI
- ;;
- ;; Copyright (c) 2025 Fred Gilham
- ;; Author: Fred Gilham
- ;; URL: https://gitbot.homedns.org/fred/bible-mode
- ;; Version: 1.0.0
- ;; Keywords: files, text, hypermedia
- ;; Package-Requires: ((emacs "29.1"))
- ;;; Commentary:
- ;; Forked and extensively modified from package by Zacalot
- ;; Url: https://github.com/Zacalot/bible-mode
- ;; This package uses the `diatheke' program to browse and search
- ;; Biblical texts provided by the Sword project.
- ;; Word study is also supported.
- ;;; Usage:
- ;; First install `diatheke'. On Debian/Ubuntu it's in the `diatheke'
- ;; package. In other distributions it might be in the sword package.
- ;; For Windows I found that you can simply install the Xiphos package.
- ;; It includes the Sword library and its utilities including diatheke,
- ;; installmgr and mkfastmod. Add the "Program Files\Xiphos\bin" path
- ;; to your execution path.
- ;; Use M-x `bible-open' to open a Bible buffer.
- ;; Use C-h f `bible' to see available keybindings.
- ;; You may customize `bible-module' to set a default browsing
- ;; module, as well as `bible-word-study-enabled' to enable word
- ;; study by default.
- ;;; Design:
- ;; The idea here is to use the diatheke program to insert code from
- ;; modules into buffers. The main bible display uses an "internal" XML
- ;; format. The whole buffer gets parsed by libxml-parse-html-region to
- ;; create a dom tree. This gets parsed by
- ;; bible--insert-domnode-recursive to render the text into
- ;; reading format.
- ;; The text is then decorated using information from the dom format as
- ;; necessary along with regular expressions to identify the verse
- ;; references. This is for red letters, purple highlighting of the
- ;; verse numbers, bold face of the divine name in the OT and so on.
- ;; If strongs tags and/or morphological tags are present, they are
- ;; looked up in appropriate lexical and morphological modules and used
- ;; to add tooltips to the text so that mousing over words will bring
- ;; up a tooltip with information about the word. Clicking on a word
- ;; with lexical information will display that informatio in a "term"
- ;; buffer.
- ;;; Code:
- ;;;; Requirements
- (require 'dom)
- (require 'shr)
- ;; Turn off tool bar mode because we want the pixels....
- (tool-bar-mode -1)
- ;;;; Variables
- (defgroup bible nil
- "Settings for `bible'."
- :group 'tools
- :link '(url-link "https://gitbot.homedns.org/fred/bible-mode"))
- (defcustom bible-module
- "KJV"
- "Book module for Diatheke to query."
- :type '(choice (const :tag "None" nil)
- (string :tag "Module abbreviation (e.g. \"KJV\")"))
- ;; :local t
- :group 'bible)
- ;;;
- ;;; XXX Not implememted yet
- (defcustom bible-font
- "Ezra SIL"
- "Default font for bible."
- :type '(string :tag "Font family name (e.g. \"Ezra SIL\")")
- :local t
- :group 'bible)
- (defcustom bible-greek-lexicon
- "AbbottSmith"
- "Lexicon used for displaying definitions of Greek words using Strong's codes."
- :type '(string :tag "Lexicon module (e.g. \"StrongsRealGreek\").")
- :local nil
- :group 'bible)
- (defcustom bible-use-index-for-lexicon t
- "Some lexicons are accessed by lemmas rather than Strong's numbers.
- Use an index to look up lemmas from Strong's numbers so these lexicons can
- be used. Examples of this type of lexicon are AbbottSmith and
- LiddellScott. XXX LiddellScott currently doesn't work. XXX AbbottSmithStrongs
- now has the complete entries instead of just links."
- :type 'boolean
- :local nil
- :group 'bible)
- (defcustom bible-lexicon-index "AbbottSmithStrongs"
- "A module that consists of an index mapping Strong's numbers to Greek lemmas.
- The code is written to use the entries in AbbottSmithStrongs
- which are of the form
- <strong's number>: @LINK <greek lemma>"
- :type '(string :tag "Lexicon index.")
- :local nil
- :group 'bible)
- (defcustom bible-greek-lexicon-short
- "StrongsRealGreek"
- "Lexicon used for displaying definitions of Greek words in tooltips."
- :type '(string :tag "Lexicon module (e.g. \"StrongsRealGreek\").")
- :local nil
- :group 'bible)
- (defcustom bible-hebrew-lexicon
- "StrongsRealHebrew"
- "Lexicon used for displaying definitions of Hebrew words using Strong's codes."
- :type '(string :tag "Lexicon module (e.g. \"StrongsRealHebrew\")")
- :local nil
- :group 'bible)
- (defcustom bible-hebrew-lexicon-short
- "BDBGlosses_Strongs" ; This seems to work
- "Lexicon used for displaying definitions of Hebrew words in tooltips."
- :type '(string :tag "Lexicon module (e.g. \"StrongsRealHebrew\")")
- :local nil
- :group 'bible)
- (defcustom bible-word-study-enabled
- nil
- "Display Strong's Hebrew, Strong's Greek, and Lemma words for study."
- :type 'boolean
- :local t
- :group 'bible)
- (defcustom bible-red-letter-enabled
- t
- "Display words of Jesus in red when module has that information."
- :type 'boolean
- :local t
- :group 'bible)
- (defcustom bible-show-diatheke-exec
- t
- "Show the arguments by which diatheke is executed."
- :type 'boolean
- :local nil
- :group 'bible)
- ;;; variable defs
- (defconst bible--verse-regexp "\\(I \\|1 \\|II \\|2 \\|III \\|3 \\)??[a-zA-Z]+?[ \t\n][0-9]+[:][0-9]+")
- (defvar bible--modules (lazy-completion-table bible--modules bible--list-biblical-modules))
- ;; XXX I believe these chapter counts aren't the same for all modules, e.g. JPS.
- (defvar bible--books
- '(;; Old Testament
- ("Genesis" . 50) ("Exodus" . 40) ("Leviticus" . 27) ("Numbers" . 36)
- ("Deuteronomy" . 34) ("Joshua" . 24) ("Judges" . 21) ("Ruth" . 4)
- ("I Samuel" . 31) ("II Samuel" . 24) ("I Kings" . 22) ("II Kings" . 25)
- ("1 Samuel" . 31) ("2 Samuel" . 24) ("1 Kings" . 22) ("2 Kings" . 25)
- ("I Chronicles" . 29) ("II Chronicles" . 36) ("Ezra" . 10) ("Nehemiah" . 13)
- ("1 Chronicles" . 29) ("2 Chronicles" . 36)
- ("Ezra" . 10) ("Nehemiah" . 13)
- ("Esther" . 10) ("Job" . 42) ("Psalms" . 150) ("Proverbs" . 31)
- ("Ecclesiastes" . 12) ("Song of Solomon" . 8) ("Isaiah" . 66) ("Jeremiah" . 52)
- ("Lamentations" . 5) ("Ezekiel" . 48) ("Daniel" . 12) ("Hosea" . 14)
- ("Joel" . 3) ("Amos" . 9) ("Obadiah" . 1) ("Jonah" . 4)
- ("Micah" . 7) ("Nahum" . 3) ("Habakkuk" . 3) ("Zephaniah" . 3)
- ("Haggai" . 2) ("Zechariah" . 14) ("Malachi" . 4)
- ;; New Testament
- ("Matthew" . 28) ("Mark" . 16) ("Luke" . 24) ("John" . 21)
- ("Acts" . 28) ("Romans" . 16)
- ("I Corinthians" . 16) ("II Corinthians" . 13)
- ("1 Corinthians" . 16) ("2 Corinthians" . 13)
- ("Galatians" . 6) ("Ephesians" . 6) ("Philippians" . 4) ("Colossians" . 4)
- ("I Thessalonians" . 5) ("II Thessalonians" . 3) ("I Timothy" . 6) ("II Timothy" . 4)
- ("1 Thessalonians" . 5) ("2 Thessalonians" . 3) ("1 Timothy" . 6) ("2 Timothy" . 4)
- ("Titus" . 3) ("Philemon" . 1) ("Hebrews" . 13) ("James" . 5)
- ("I Peter" . 5) ("II Peter" . 3) ("I John" . 5) ("II John" . 1)
- ("1 Peter" . 5) ("2 Peter" . 3) ("1 John" . 5) ("2 John" . 1)
- ("III John" . 1) ("Jude" . 1) ("Revelation of John" . 22)
- ("3 John" . 1))
- "A-list of name / chapter count for Bible books.")
- (defvar bible--book-name-abbreviations-alist
- '(;; Old Testament
- ("Ge" . "Genesis") ("Ex" . "Exodus") ("Le" . "Leviticus") ("Nu" . "Numbers")
- ("De" . "Deuteronomy") ("Js" . "Joshua") ("Jg" . "Judges") ("Judg" . "Judges")
- ("Ru" . "Ruth") ("1 Samuel" . "I Samuel") ("I Sa" . "I Samuel") ("1 Sa" . "I Samuel")
- ("2 Samuel" . "II Samuel") ("II Sa" . "II Samuel") ("2 Sa" . "II Samuel") ("1 Kings" . "I Kings")
- ("I Ki" . "I Kings") ("1 Ki" . "I Kings") ("2 Kings" . "II Kings") ("II Ki" . "II Kings")
- ("2 Ki" . "II Kings") ("1 Chronicles" . "I Chronicles") ("I Ch" . "I Chronicles") ("1 Ch" . "I Chronicles")
- ("2 Chronicles" . "II Chronicles") ("II Ch" . "II Chronicles") ("2 Ch" . "II Chronicles")
- ("Ezr" . "Ezra") ("Ne" . "Nehemiah")
- ("Es" . "Esther") ("Jb" . "Job") ("Ps" . "Psalms") ("Pr" . "Proverbs")
- ("Ec" . "Ecclesiastes") ("So" . "Song of Solomon") ("Is" . "Isaiah") ("Je" . "Jeremiah")
- ("La" . "Lamentations") ("Ez" . "Ezekiel") ("Da" . "Daniel") ("Ho" . "Hosea")
- ("Joe" . "Joel") ("Am" . "Amos") ("Ob" . "Obadiah") ("Jon" . "Jonah")
- ("Mi" . "Micah") ("Na" . "Nahum") ("Ha" . "Habakkuk") ("Zep" . "Zephaniah")
- ("Hag" . "Haggai") ("Ze" . "Zechariah") ("Mal" . "Malachi")
- ;; New Testament
- ;; Added AbbottSmith lexicon abbreviations to allow proper following of cross references in lexicon buffers.
- ("Mt" . "Matthew") ("Matt" . "Matthew")
- ("Mk" . "Mark") ("Lk" . "Luke") ("Jo" . "John") ("Ac" . "Acts")
- ("Ro" . "Romans") ("Rom" . "Romans")
- ("1 Corintihans" . "I Corinthians") ("I Co" . "I Corinthians") ("1 Co" . "I Corinthians") ("ICor" . "I Corinthians")
- ("2 Corinthians" . "II Corinthians") ("II Co" . "II Corinthians") ("2 Co" . "II Corinthians") ("IICor" . "II Corinthians")
- ("Ga" . "Galatians") ("Gal" . "Galatians")
- ("Eph" . "Ephesians")
- ("Phl" . "Philippians") ("Phil" . "Philippians")
- ("Col" . "Colossians")
- ("1 Thessalonians" . "I Thessalonians") ("I Th" . "I Thessalonians") ("1 Th" . "I Thessalonians") ("IThess" . "I Thessalonians")
- ("2 Thessalonians" . "II Thessalonians") ("II Th" . "II Thessalonians") ("2 Th" . "II Thessalonians") ("IIThess" . "II Thessalonians")
- ("1 Timothy" . "I Timothy") ("I Ti" . "I Timothy") ("1 Ti" . "I Timothy") ("ITim" . "I Timothy")
- ("2 Timothy" . "II Timothy") ("II Ti" . "II Timothy") ("2 Ti" . "II Timothy") ("IITim" . "II Timothy")
- ("Tit" . "Titus")
- ("Phm" . "Philemon") ("Phlm" . "Philemon")
- ("He" . "Hebrews") ("Heb" . "Hebrews")
- ("Ja" . "James") ("Jas" . "James")
- ("1 Peter" . "I Peter") ("I Pe" . "I Peter") ("1 Pe" . "I Peter")
- ("2 Peter" . "II Peter") ("II Pe" . "II Peter") ("2 Pe" . "II Peter") ("IIPet" . "II Peter")
- ("1 John" . "I John") ("I Jo" . "I John") ("1 Jo" . "I John") ("IJohn" . "I John")
- ("2 John" . "II John") ("II Jo" . "II John") ("2 Jo" . "II John") ("IIJohn" . "II John")
- ("3 John" . "III John") ("III Jo" . "III John") ("3 Jo" . "III John") ("IIIJohn" . "III John")
- ("Ju" . "Jude")
- ("Re" . "Revelation of John") ("Rev" . "Revelation of John"))
- "A-list of abbreviations for Bible books.")
- ;;;; Book / chapter
- (defvar-local bible--current-book (assoc "Genesis" bible--books)
- "Current book data (name . chapter).")
- (defvar-local bible--current-book-name "Genesis"
- "Current book name.")
- (defvar-local bible--current-chapter 1
- "Current book chapter number.")
- (defvar-local bible-query nil
- "Search query associated with the buffer.")
- (defvar-local bible-search-mode "phrase"
- "Search mode: either `lucene', `phrase', `regex' or `multiword'.")
- (defvar bible-search-range nil)
- (defvar-local bible-has-strongs nil
- "Set if the module being displayed has strongs numbers availabile.")
- (defvar-local bible-has-morphology nil
- "Set if the module being displayed has morphology availabile.")
- ;; (defvar bible--current-module nil)
- ;;;; Keymaps
- ;;;; N.B. Bible menu items appear in reverse order of their
- ;;;; definition below
- (defconst bible-map (make-sparse-keymap)
- "Keymap for bible.")
- (define-key bible-map [menu-bar bible]
- (cons "Bible" (make-sparse-keymap "Bible")))
- (define-key bible-map
- [menu-bar bible toggle-debug]
- '("Toggle debug-on-error" . toggle-debug-on-error))
- (defun bible-toggle-display-diatheke ()
- "Toggle diatheke args display."
- (interactive)
- (setq bible-show-diatheke-exec (not bible-show-diatheke-exec)))
- (define-key bible-map
- [menu-bar bible display-diatheke]
- '("Toggle diatheke display" . bible-toggle-display-diatheke))
- (defun bible-toggle-display-xml ()
- "Toggle XML display."
- (interactive)
- (setq-local bible-debugme (not bible-debugme))
- (bible--display))
- (define-key bible-map "d" 'bible-toggle-display-xml)
- (define-key bible-map
- [menu-bar bible display-xml]
- '("Toggle XML Display" . bible-toggle-display-xml))
- (define-key bible-map
- [menu-bar bible sep]
- '(menu-item '"--"))
- ;;;;; Misc
- (define-key bible-map "m" 'bible-select-module)
- (define-key bible-map "w" 'bible-toggle-word-study)
- (define-key bible-map "l" 'bible-toggle-red-letter)
- (define-key bible-map "x" 'bible-split-display)
- (define-key bible-map
- [menu-bar bible split-display]
- '("Split Display" . bible-split-display))
- ;;;;; Search
- (define-key bible-map "/" 'bible-search)
- (define-key bible-map "s" 'bible-search)
- (define-key bible-map
- [menu-bar bible search]
- '("Search" . bible-search))
- (define-key bible-map "r" 'bible-set-search-range)
- (define-key bible-map
- [menu-bar bible range]
- '("Set Search Range" . bible-set-search-range))
- ;;;;; Navigation
- (define-key bible-map "p" 'bible-previous-chapter)
- (define-key bible-map
- [menu-bar bible previous-chapter]
- '("Previous Chapter" . bible-previous-chapter))
- (define-key bible-map "n" 'bible-next-chapter)
- (define-key bible-map
- [menu-bar bible next-chapter]
- '("Next Chapter" . bible-next-chapter))
- (define-key bible-map (kbd "TAB") 'bible-next-word)
- (define-key bible-map (kbd "M-<tab>") 'bible-previous-word)
- ;;;;; Direct jump
- (define-key bible-map "c" 'bible-select-chapter)
- (define-key bible-map
- [menu-bar bible select-chapter]
- '("Select Chapter" . bible-select-chapter))
- (define-key bible-map "b" 'bible-select-book)
- (define-key bible-map
- [menu-bar bible select-book]
- '("Select Book" . bible-select-book))
- (define-key bible-map
- [menu-bar bible sep]
- '(menu-item '"--"))
- ;;;;; Deal with visual-line-mode
- (define-key bible-map "\C-n" 'next-logical-line)
- (define-key bible-map "\C-p" 'previous-logical-line)
- (defun bible-next-search-item ()
- "Go to next item in list of found verses."
- (interactive)
- (search-forward-regexp bible--verse-regexp))
- (defun bible-previous-search-item ()
- "Go to previous item in list of found verses."
- (interactive)
- (search-backward-regexp bible--verse-regexp))
- (defconst bible-search-mode-map (make-keymap))
- (define-key bible-search-mode-map "s" 'bible-search)
- (define-key bible-search-mode-map "w" 'bible-toggle-word-study)
- (define-key bible-search-mode-map "n" 'bible-next-search-item)
- (define-key bible-search-mode-map "p" 'bible-previous-search-item)
- (define-key bible-search-mode-map (kbd "RET") 'bible-search-mode-follow-verse)
- (defconst bible-term-hebrew-mode-map (make-sparse-keymap))
- (defconst bible-term-greek-mode-map (make-sparse-keymap))
- (define-key bible-term-greek-mode-map [mouse-1] 'bible-search-mode-follow-xref)
- ;;;
- ;;; Menu bar items
- ;;;
- (defvar-local bible-text-direction 'left-to-right)
- (defun bible-toggle-text-direction ()
- "Switch between left-to-right and right-to-left text direction."
- (interactive)
- (if (eq bible-text-direction 'left-to-right)
- (setq-local bible-text-direction 'right-to-left)
- (setq-local bible-text-direction 'left-to-right))
- (setq-local bidi-paragraph-direction bible-text-direction))
- (defvar-local bible-debugme nil
- "Make text show up as XML when set.")
- (defvar-local bible-search-query nil
- "Query used in toggles (word study and red letter).")
- (defvar bible-use-tooltips t)
- (defun bible-toggle-tooltips ()
- "Toggle use of tooltips to display lexical/morphological items."
- (interactive)
- (setq bible-use-tooltips (not bible-use-tooltips))
- (tooltip-mode 'toggle)
- (setq tooltip-resize-echo-area bible-use-tooltips))
- (define-key bible-map
- [menu-bar bible sepp]
- '(menu-item '"--"))
- (define-key bible-map
- [menu-bar bible toggle-text-direction]
- '("Toggle text direction (for Hebrew display)" . bible-toggle-text-direction))
- (define-key bible-map
- [menu-bar bible toggle-tooltip-display]
- '("Toggle Tooltip Display" . bible-toggle-tooltips))
- (define-key bible-map
- [menu-bar bible sepp]
- '(menu-item '"--"))
- (define-key bible-map
- [menu-bar bible select-biblical-text]
- '("Select Module" . bible-display-available-modules))
- (defun bible--display-greek ()
- "Display Greek text.
- This command is run by clicking on text, not directly by the user."
- (interactive)
- (let ((item (car (split-string (get-text-property (point) 'strong)))))
- ;; Remove "strong:G" prefix
- (bible-term-greek (replace-regexp-in-string "strong:G" "" item))))
- (defconst bible-greek-keymap (make-sparse-keymap))
- (define-key bible-greek-keymap (kbd "RET") 'bible--display-greek)
- (define-key bible-greek-keymap [mouse-1] 'bible--display-greek)
- (defun bible--display-hebrew ()
- "Display Hebrew text.
- This command is run by clicking on text, not directly by the user."
- (interactive)
- (let ((item (car (split-string (get-text-property (point) 'strong)))))
- ;; Remove "strong:H" prefix and any alphabetic suffixes.
- (bible-term-hebrew (replace-regexp-in-string "strong:H" "" item))))
- (defconst bible-hebrew-keymap (make-sparse-keymap))
- (define-key bible-hebrew-keymap (kbd "RET") 'bible--display-hebrew)
- (define-key bible-hebrew-keymap [mouse-1] 'bible--display-hebrew)
- (defconst bible-lemma-keymap (make-sparse-keymap))
- (define-key bible-lemma-keymap (kbd "RET")
- (lambda ()
- (interactive)))
- (defconst bible-morph-keymap (make-sparse-keymap))
- (define-key bible-morph-keymap (kbd "RET")
- (lambda ()
- (interactive)
- ;;; (let ((thing (thing-at-point 'word)))
- ;;; (message "thing at point: %s" thing)
- ;;; (message "morph property %s" (get-text-property 0 'field thing))
- ))
- ;;;; Modes
- (define-derived-mode bible special-mode "Bible"
- "Mode for reading the Bible.
- \\{bible-map}"
- (buffer-disable-undo)
- (font-lock-mode t)
- (use-local-map bible-map)
- (setq buffer-read-only t)
- (visual-line-mode t))
- (define-derived-mode bible-search-mode special-mode "Bible Search"
- "Mode for performing Bible searches.
- \\{bible-search-mode-map}"
- (buffer-disable-undo)
- (font-lock-mode t)
- (use-local-map bible-search-mode-map)
- (setq buffer-read-only t)
- (visual-line-mode t))
- (define-derived-mode bible-term-hebrew-mode special-mode "Bible Term (Hebrew)"
- "Mode for researching Hebrew terms in the Bible.
- \\{bible-term-hebrew-mode-map}"
- (buffer-disable-undo)
- (font-lock-mode t)
- (use-local-map bible-term-hebrew-mode-map)
- (setq buffer-read-only t)
- (visual-line-mode t))
- (define-derived-mode bible-term-greek-mode special-mode "Bible Term (Greek)"
- "Mode for researching Greek terms in the Bible.
- \\{bible-term-greek-mode-map}"
- (buffer-disable-undo)
- (font-lock-mode t)
- (use-local-map bible-term-greek-mode-map)
- (setq buffer-read-only t)
- (visual-line-mode t))
- (define-derived-mode bible-module-select-mode special-mode "Select Text Module"
- (buffer-disable-undo)
- (font-lock-mode t)
- (setq buffer-read-only t))
- ;;;; Functions
- ;;;;; Commands
- (defun bible-open (&optional book-name chapter verse)
- "Create and open a `bible' buffer with BOOK-NAME, CHAPTER and VERSE.
- Arguments BOOK-NAME, CHAPTER and VERSE, when supplied, give the starting
- verse reference for the buffer. If no optional arguments are supplied,
- Genesis 1:1 is used."
- (interactive)
- (with-current-buffer (get-buffer-create (generate-new-buffer-name (concat "*bible*")))
- (bible)
- (bible--set-location
- (or (assoc (or book-name "Genesis") bible--books) (list book-name))
- (or chapter 1)
- verse)
- (set-window-buffer (get-buffer-window (current-buffer)) (current-buffer))))
- (defun bible-next-chapter ()
- "Page to the next chapter for the active `bible' buffer."
- (interactive)
- (let* ((book-chapters (cdr bible--current-book))
- (chapter (min book-chapters (+ bible--current-chapter 1))))
- (bible--set-location bible--current-book chapter)))
- (defun bible-previous-chapter ()
- "Page to the previous chapter for the active `bible' buffer."
- (interactive)
- (bible--set-location bible--current-book (max 1 (- bible--current-chapter 1))))
- (defun bible-next-word ()
- "Move forward a word, taking into account the relevant text properties."
- (interactive)
- (unless (eobp)
- (let ((next-change (text-property-search-forward 'strong nil nil t)))
- (when next-change
- (goto-char (1- (prop-match-end next-change)))))))
- (defun bible-previous-word ()
- "Move back a word, taking into account the relevant text properties."
- (interactive)
- (unless (bobp)
- (let ((previous-change (text-property-search-backward 'strong)))
- (when previous-change
- (goto-char (prop-match-beginning previous-change))))))
- (defun bible-select-book ()
- "Ask user for a new book and chapter for the current `bible' buffer."
- (interactive)
- (let* ((completion-ignore-case t)
- (book-data (assoc (completing-read "Book: " bible--books nil t) bible--books))
- (book-data-string (car book-data))
- (chapter (string-to-number (completing-read "Chapter [1]: " (bible--list-number-range 1 (cdr book-data)) nil t nil nil "1"))))
- (pcase (aref book-data-string 0)
- (?1 (setq book-data (cons (concat "I" (seq-subseq book-data-string 1)) (cdr book-data))))
- (?2 (setq book-data (cons (concat "II" (seq-subseq book-data-string 1)) (cdr book-data))))
- (?3 (setq book-data (cons (concat "III" (seq-subseq book-data-string 1)) (cdr book-data)))))
- (setq-local bible--current-book book-data)
- (setq-local bible--current-book-name (car book-data))
- (setq-local bible--current-chapter chapter)
- (bible--display)))
- (defun bible-select-chapter ()
- "Ask user for a new chapter for the current `bible' buffer."
- (interactive)
- (let* ((book-chapters (cdr bible--current-book))
- (chapter (string-to-number (completing-read "Chapter [1]: " (bible--list-number-range 1 book-chapters) nil t nil nil "1"))))
- (when chapter
- (bible--set-location bible--current-book chapter))))
- (defun bible-set-search-range ()
- "Ask user for a new text module for the current `bible' buffer."
- (interactive)
- (let ((range (read-string "Range (<return> to clear): ")))
- (if (string-equal range "")
- (setq bible-search-range nil)
- (setq bible-search-range range))))
- (defun bible-select-module ()
- "Ask user for a new text module for the current `bible' buffer."
- (interactive)
- ;; (let* ((bible-module (completing-read "Module: " bible--modules)))
- ;; (bible--display))
- (setq-default bible-module (completing-read "Module: " bible--modules))
- (bible--display))
- (defun bible-toggle-word-study ()
- "Toggle the inclusion of word study for the active `bible' buffer."
- (interactive)
- (setq bible-word-study-enabled (not bible-word-study-enabled))
- ;; (if (equal major-mode 'bible-search-mode)
- ;; (bible--display-search bible-search-query bible-search-mode)
- (bible--display))
- ;;)
- (defun bible-toggle-red-letter ()
- "Toggle red letter mode for the active `bible' buffer."
- (interactive)
- (setq bible-red-letter-enabled (not bible-red-letter-enabled))
- ;; (if (equal major-mode 'bible-search-mode)
- ;; (bible--display-search bible-search-query bible-search-mode)
- (bible--display))
- ;;)
- (defun bible-split-display ()
- "Copy the active `bible' buffer into a new buffer in another window."
- (interactive)
- (split-window-right)
- (balance-windows)
- (other-window 1)
- (bible-open bible--current-book-name bible--current-chapter))
- (defun bible-search (query)
- "Search for a QUERY: a word or phrase.
- Asks the user for type of search: either `lucene', `phrase', `regex'
- or `multiword'. `lucene' is the default search.
- `lucene' mode requires an index to be built using the `mkfastmod' program."
- (interactive "sBible Search: ")
- (when (> (length query) 0)
- (let* ((bible-module (buffer-local-value 'bible-module (current-buffer)))
- (searchmode (completing-read "Search Mode: " '("lucene" "phrase" "regex" "multiword") nil t "lucene")))
- (bible--open-search query searchmode))))
- (defun bible-search-mode-follow-verse ()
- "Follow the hovered verse in a `bible-search-mode' buffer.
- Create a new `bible' buffer positioned at the selected verse."
- (interactive)
- (let* ((text (thing-at-point 'line t))
- book
- chapter
- verse)
- (string-match ".+ [0-9]?[0-9]?[0-9]?:[0-9]?[0-9]?[0-9]?:" text)
- (setq text (match-string 0 text))
- (string-match " [0-9]?[0-9]?[0-9]?:" text)
- (setq chapter (replace-regexp-in-string "[^0-9]" "" (match-string 0 text)))
- (string-match ":[0-9]?[0-9]?[0-9]?" text)
- (setq verse (replace-regexp-in-string "[^0-9]" "" (match-string 0 text)))
- (setq book (replace-regexp-in-string "[ ][0-9]?[0-9]?[0-9]?:[0-9]?[0-9]?[0-9]?:$" "" text))
- (bible-open (string-trim book) (string-to-number chapter) (string-to-number verse))))
- (defun bible-search-mode-follow-xref ()
- "Follow the hovered verse in a bible term buffer.
- Create a new `bible' buffer positioned at the specified verse.
- XXX We use the current module to avoid opening cans of worms regarding
- OT/NT etc. If that module doesn't have that verse...???
- Handle abbreviations from lexicon module (AbbottSmith)."
- (interactive)
- (let* ((xref (get-text-property (point) 'xref))
- (verse-ref (string-split xref))
- book-abbrev
- book
- chapter-verse
- chapter
- verse)
- (if (= (length verse-ref) 3) ; II Cor 3:17 or the like
- (setq book-abbrev (concat (car verse-ref) (cadr verse-ref))
- chapter-verse (split-string (caddr verse-ref) ":"))
- ;; Mat 5 or the like
- (setq book-abbrev (car verse-ref)
- chapter-verse (split-string (cadr verse-ref) ":")))
- (setq book (cdr (assoc book-abbrev bible--book-name-abbreviations-alist)))
- (unless book (setq book (car verse-ref))) ; Didn't find abbreviation
- (setq chapter (car chapter-verse)
- verse (cadr chapter-verse))
- (bible-open (string-trim book) (string-to-number chapter) (string-to-number verse))))
- (defun bible-term-hebrew (term)
- "Query user for a Strong's Hebrew Lexicon TERM."
- (interactive "sTerm: ")
- (bible--open-term-hebrew term))
- (defun bible-term-greek (term)
- "Query user for a Strong's Greek Lexicon TERM."
- (interactive "sTerm: ")
- (bible--open-term-greek term))
- (defun bible-insert ()
- "Query user to select a verse for insertion into the current buffer."
- (interactive)
- (let* ((completion-ignore-case t)
- (book-data (assoc (completing-read "Book: " bible--books nil t) bible--books))
- (chapter (when book-data (completing-read "Chapter: " (bible--list-number-range 1 (cdr book-data)) nil t "1" nil "1")))
- (verse (when chapter (read-from-minibuffer "Verse: "))))
- (when verse
- (insert (string-trim
- (replace-regexp-in-string
- (regexp-opt `(,(concat "(" bible-module ")")))
- ""
- (bible--exec-diatheke (concat (car book-data) " " chapter ":" verse) nil "plain")))))))
- ;;;;; Support
- (defconst bible-diatheke-filter-options " avlnmw")
- ;;;; XXX
- ;;;; Can we avoid returning (buffer-string) and just return the buffer?
- ;;;;
- (defun bible--exec-diatheke (query &optional filter format module)
- "Execute `diatheke' with specified QUERY options, returning output
- buffer. FILTER is the Diatheke filter argument. FORMAT is either plain
- or the default of internal. MODULE is the text module to use."
- (let ((module (or module bible-module)))
- (with-temp-buffer
- (let ((args (list "diatheke" nil (current-buffer) t "-b" module)))
- (if filter
- (setq filter (concat filter bible-diatheke-filter-options))
- (setq filter bible-diatheke-filter-options))
- (setq args (append args (list "-o" filter)))
- (setq args (append args (list "-f" (pcase format ("plain" "plain") (_ "internal")) "-k" query)))
- (when bible-show-diatheke-exec
- (message "%s" args))
- (apply 'call-process args))
- (buffer-string))))
- (defun bible--diatheke-search (query searchtype &optional format module)
- "Execute `diatheke' for searches. Requires the search query and the
- search type. Optional argument FORMAT is either plain or the default of
- internal. MODULE is the text module to use and defaults to the current
- module."
- (with-temp-buffer
- (let ((args (list "diatheke" nil (current-buffer) t "-b" (or module bible-module))))
- (setq args (append args (list "-s" (pcase searchtype
- ("lucene" "lucene")
- ("phrase" "phrase")
- ("regex" "regex")
- ("multiword" "multiword")))))
- (when bible-search-range (setq args (append args (list "-r" bible-search-range))))
- (setq args (append args (list "-f" (pcase format ("plain" "plain") (_ "internal")) "-k" query)))
- (when bible-show-diatheke-exec
- (message "%s" args))
- (apply 'call-process args))
- (buffer-string)))
- (defvar-local bible-chapter-title nil
- "Text preceding start of chapter.
- Mostly in Psalms, like `Of David' or the like.")
- ;;;
- ;;; Greek and Hebrew lexicon and morphology tooltip rendering.
- ;;;
- ;;; Hash tables for Lexical definitions.
- (defvar bible-greek-hash (make-hash-table :test 'equal :size 10000))
- (defvar bible-hebrew-hash (make-hash-table :test 'equal :size 10000))
- ;; Do lookups using index to lexicon with lookups by lemma.
- (defvar bible-lemma-index-hash (make-hash-table :test 'equal :size 10000))
- ;;; Hash tables for tooltips.
- (defvar lex-hash (make-hash-table :test 'equal :size 10000))
- (defvar morph-hash (make-hash-table :test 'equal :size 10000))
- ;;; Use HTMLHREF format with diatheke, post-process to render html.
- (defun bible--morph-query (query module)
- "Execute `diatheke' to do morph QUERY, using MODULE.
- Render HTML, return string. Do some tweaking specific to morphology."
- (with-temp-buffer
- (let ((args (list "diatheke" nil (current-buffer) t "-b" module "-o" "m" "-f" "HTMLHREF" "-k" query)))
- (when bible-show-diatheke-exec
- (message "%s" args))
- (apply 'call-process args)
- (shr-render-region (point-min) (point-max))
- (format-replace-strings
- '(("\n:" . "") ; This makes the Packard morphology display look better.
- ("Part of Speech" . "")) ; This helps the Robinson display look better.
- nil (point-min) (point-max))
- (substring (buffer-string) (+ (length query) 1))) ; This tries to get rid of unnecessary query identifier.
- ))
- ;;; Use "plain" format with diatheke.
- (defun bible--lex-query (query module)
- "Execute `diatheke' for QUERY, using MODULE.
- Plain format, returns string."
- ;; Get rid of query ID at front of string: ?????:
- (bible--exec-diatheke query nil "plain" module))
- (defun bible--lookup-lemma-index (key)
- "Return the Greek lemma from lemma index with a strong's number as KEY."
- (or (gethash key bible-lemma-index-hash)
- (puthash key
- (string-trim
- (replace-regexp-in-string
- (concat "(" bible-lexicon-index ")") ""
- (bible--lex-query key bible-lexicon-index)))
- bible-lemma-index-hash)))
- ;;;
- ;;; The Greek lexical definitions are done using the HTMLHREF output
- ;;; format so they come out looking nice and having clickable
- ;;; cross-references and/or Strong's references.
- (defun bible--process-href ()
- "Fix the XML so cross-references are in the right format.
- These cross-references get processed later when the term is displayed.
- First, find the links put in by diatheke's HTMLHREF output format.
- Replace the links with verse references that get changed to clickable
- cross-references when the term is displayed.
- The verse refs look like this: <bookname>.<chapter>.<verse>. We convert
- them to the <bookname> <chapter>:<verse> format."
- (goto-char (point-min))
- (while (re-search-forward "<a href=\"passagestudy.*?</a>" nil t) ; HTMLHREF cross references.
- (let ((match-text (match-string 0)))
- ;; Delete original link.
- (replace-match "" nil nil)
- ;; Get the verse reference from the string we saved. Put it in
- ;; good format, then insert it into buffer where href was.
- (when (string-match "value=.*?&" match-text)
- (let* ((value-string (match-string 0 match-text))
- ;; Strip off value= and trailing &.
- (verse-ref-string (substring value-string 6 (1- (length value-string))))
- (verse-ref-length (length verse-ref-string))
- period)
- ;; Convert periods
- ;; Substitute first period with space
- (when (setq period (cl-search "." verse-ref-string))
- (aset verse-ref-string period ? ))
- ;; Substitute second period with colon
- (when (setq period (cl-search "." verse-ref-string))
- (aset verse-ref-string period ?:))
- ;; Replace numbers (1, 2 or 3) with roman numerals (I, II, III) XXX Fix?
- (pcase (aref verse-ref-string 0)
- (?1 (setq verse-ref-string (concat "I" (substring verse-ref-string 1))))
- (?2 (setq verse-ref-string (concat "II" (substring verse-ref-string 1))))
- (?3 (setq verse-ref-string (concat "III" (substring verse-ref-string 1)))))
- (set-text-properties 0 verse-ref-length nil verse-ref-string) ; Clear unwanted properties (if any)
- (insert verse-ref-string))))))
- (defun bible--lookup-greek-def (key)
- "Execute `diatheke' to do query on KEY.
- Massage output so verse cross references are usable. Returns string.
- We use HTMLHREF format output because it may have verse references
- as HTML links, depending on the lexicon module."
- (with-temp-buffer
- (let ((args (list "diatheke" nil (current-buffer) t "-b" bible-greek-lexicon "-o" "m" "-f" "HTMLHREF" "-k" key)))
- (when bible-show-diatheke-exec
- (message "%s" args))
- (apply 'call-process args)
- (bible--process-href) ; Clean up XML so xrefs can work after rendering.
- (shr-render-region (point-min) (point-max))
- (buffer-string))))
- (defun bible--lookup-lex-greek-indexed (key)
- "Lookup Greek lemma using Strong's number KEY.
- Then look up the definition of that lemma. Used when two-stage
- lexical definition is set for a particular lexicon."
- (let ((lemma-entry (bible--lookup-lemma-index key))) ; Get lemma from Strong's number
- (when lemma-entry
- (let ((lemma (caddr (split-string lemma-entry " "))))
- (bible--lookup-greek-def lemma)))))
- (defun bible--lookup-lex-greek (key)
- "Lookup lexical definition using Strong's number KEY.
- 1. Check hash table first. If entry found, return.
- 2. Otherwise, if a lexicon is accessed by lemmas, do lookup using index method.
- 3. Otherwise just use the Strong's number method."
- (or (gethash key bible-greek-hash)
- (puthash key
- (if bible-use-index-for-lexicon
- (bible--lookup-lex-greek-indexed key)
- (bible--lookup-greek-def key))
- bible-greek-hash)))
- (defun bible--lookup-strongs-greek (_window object pos)
- "Look up Greek lexical entry of OBJECT clicked on in WINDOW at POS.
- If not found in hash table, get it from sword database. Stash in hash
- table, and return string."
- (let ((query (get-text-property pos 'strong object))) ; Get Strong's number from text property
- (when (string-match "[0-9]+" query)
- (bible--lookup-lex-greek (match-string 0 query)))))
- (defun bible--lookup-strongs-hebrew (_window object pos)
- "Look up Hebrew lexical string for OBJECT at point POS.
- If not found in hash table, get it from sword database,
- stash in hash table, and return string."
- (let ((query (get-text-property pos 'strong object)))
- (when (string-match "[0-9]+" query)
- (let ((lookup-key (concat "H" (match-string 0 query))))
- (or (gethash lookup-key bible-hebrew-hash)
- (let ((raw-text (bible--lex-query lookup-key bible-hebrew-lexicon)))
- (puthash lookup-key raw-text bible-hebrew-hash)))))))
- ;;;
- ;;; We use the shorter lexicons for text in tooltips. We also cache
- ;;; the lex and morph strings, hoping to speed up tooltip rendering.
- ;;;
- (defun bible--lookup-strongs-greek-short (lex)
- "Look up Greek lexical entry for LEX from short Greek lexicon."
- (when (string-match "[0-9]+" lex)
- (bible--lex-query (match-string 0 lex) bible-greek-lexicon-short)))
- (defun bible--lookup-strongs-hebrew-short (lex)
- "Look up Hebrew lexical entry for LEX from short Hebrew lexicon."
- (when (string-match "[0-9]+" lex)
- (bible--lex-query lex bible-hebrew-lexicon-short)))
- (defun bible--lookup-lex (lex)
- "Look up lexical item LEX. This is used for tooltips.
- Return hash table entry if present in lex-hash cache, else look up in
- database and stash in cache."
- (when lex
- (let* ((key (seq-subseq lex 7)) ; strip off "strong:" prefix.
- (lex-text (gethash key lex-hash)))
- (if lex-text
- lex-text
- (setq lex-text
- (cond ((string-prefix-p "G" key)
- (string-replace
- (concat "(" bible-greek-lexicon-short ")")
- ""
- ;; The Greek text doesn't have line breaks, so limit lines to 75 chars.
- (string-fill (bible--lookup-strongs-greek-short key) 75)))
- ((string-prefix-p "H" key)
- (string-replace
- (concat "(" bible-hebrew-lexicon-short ")")
- ""
- (bible--lookup-strongs-hebrew-short key)))))
- (puthash key (bible--cleanup-tooltip-text lex-text) lex-hash)))))
- (defun bible--lookup-morph (morph)
- "Look up morphological item MORPH.
- Return hash table entry if present in morph-hash cache, else look up in
- database and stash in cache."
- (when morph
- (or (gethash morph morph-hash)
- (puthash morph
- (let (morph-module morph-key)
- (cond ((string-prefix-p "robinson:" morph)
- (setq morph-module "Robinson")
- (setq morph-key (seq-subseq morph (length "robinson:"))))
- ((string-prefix-p "packard:" morph)
- (setq morph-module "Packard")
- (setq morph-key (seq-subseq morph (length "packard:"))))
- ((string-prefix-p "oshm:" morph)
- (setq morph-module "OSHM")
- (setq morph-key (seq-subseq morph (length "oshm:")))))
- (string-replace (concat "(" morph-module ")")
- ""
- (bible--morph-query morph-key morph-module)))
- morph-hash))))
- ;; (defvar bible-outline-strings
- ;; '((". ." . ".")
- ;; (" I. ." . "\n I.")
- ;; (" II. ." . "\n II.")
- ;; (" III. ." . "\n III.")
- ;; (" IV. ." . "\n IV.")
- ;; (" V. ." . "\n V.")
- ;; ("1. ." . "\n 1.")
- ;; ("2. ." . "\n 2.")
- ;; ("3. ." . "\n 3.")
- ;; ("4. ." . "\n 4.")
- ;; ("5. ." . "\n 5.")
- ;; ("6. ." . "\n 6.")
- ;; ("7. ." . "\n 7.")
- ;; ("8. ." . "\n 8.")
- ;; ("9. ." . "\n 9.")
- ;; ("a. ." . "\n a.")
- ;; ("b. ." . "\n b.")
- ;; ("c. ." . "\n c.")
- ;; ("d. ." . "\n d.")
- ;; ("e. ." . "\n e.")
- ;; ("f. ." . "\n f.")
- ;; ("g. ." . "\n g.")
- ;; ("h. ." . "\n h.")
- ;; (" . " . ". ")))
- (defvar bible-outline-strings
- '(;;(". ." . ".")
- (" I. ." . "\nI.")
- (" II. ." . " II.")
- (" III. ." . " III.")
- (" IV. ." . " IV.")
- (" V. ." . " V.")
- ("1. ." . "\n1.")
- ("2. ." . "2.")
- ("3. ." . "3.")
- ("4. ." . "4.")
- ("5. ." . "5.")
- ("6. ." . "6.")
- ("7. ." . "7.")
- ("8. ." . "8.")
- ("9. ." . "9.")
- ("a. ." . " a.")
- ("b. ." . " b.")
- ("c. ." . " c.")
- ("d. ." . " d.")
- ("e. ." . " e.")
- ("f. ." . " f.")
- ("g. ." . " g.")
- ("h. ." . " h.")
- (" . " . ". ")))
- (defun bible--cleanup-tooltip-text (lex-text)
- "Reformat tooltip text LEX-TEXT so tooltips look nice."
- (dolist (outline-string bible-outline-strings)
- (setq lex-text (string-replace (car outline-string) (cdr outline-string) lex-text)))
- lex-text)
- ;;;
- ;;; Get string for tooltip display
- ;;;
- (defun bible--show-lex-morph (_window object pos)
- "Get text for tooltip display for OBJECT at POS in WINDOW.
- Includes both lex and morph definitions if text module has
- both tags, otherwise just get lex definition."
- (let* ((lex-morph-text "")
- (lex (get-text-property pos 'strong object))
- (lex-text (bible--lookup-lex lex))
- (morph (get-text-property pos 'morph object))
- (morph-text (bible--lookup-morph morph)))
- (when lex-text
- ;; (setq lex-morph-text (string-trim (string-fill lex-text 75))))
- (setq lex-morph-text (string-trim lex-text)))
- (when morph-text
- (setq lex-morph-text
- (concat lex-morph-text "\n" (string-trim morph-text))))
- ;; This prevents bogus command substitutions in the tooltip by
- ;; removing backslashes. XXX I couldn't figure out a better way
- ;; to bypass command substitution in the tooltips.
- (setq lex-morph-text (subst-char-in-string ?\\ ? lex-morph-text))
- lex-morph-text))
- (defun bible-handle-divine-name (item)
- "When ITEM is divine name, display it as such."
- (insert "LORD")
- (let* ((refstart (- (point) (length "LORD")))
- (refend (point))
- (strongs (dom-attr item 'savlm)))
- (add-face-text-property refstart refend 'bold)
- (put-text-property refstart refend 'keymap bible-hebrew-keymap)
- (when (and strongs (string-match "strong:H" strongs))
- (put-text-property refstart refend 'help-echo 'bible--show-lex-morph)
- (put-text-property refstart refend 'strong (match-string 0 strongs)))))
- (defun bible--process-word (item iproperties)
- "Handle <w ...> fubar </w> tag in ITEM. Check IPROPERTIES for qualifiers.
- Add tooltips for definitions and morphology. Also insert lemmas in buffer
- (must be done after item is inserted in buffer)."
- (let ((word (string-trim (dom-text item)))
- (morph (dom-attr item 'morph))
- (savlm (dom-attr item 'savlm))
- (lemma (dom-attr item 'lemma))
- (divinename (dom-by-tag item 'divinename)))
- (insert word)
- (let ((refstart (- (point) (length word)))
- (refend (point)))
- ;; Red letter (Yuck, some modules need this below)
- (when (plist-get iproperties 'jesus)
- (add-face-text-property refstart refend '(:foreground "red")))
- ;; Special case this. XXX Some modules do this differently.
- (when divinename
- (insert " ")
- (bible-handle-divine-name item))
- ;; lexical definitions
- (when (or savlm lemma)
- (let ((matched nil)
- (item (or savlm lemma)))
- (cond ((string-match "strong:G.*" item) ; Greek
- (setq matched (match-string 0 item))
- (put-text-property refstart refend 'keymap bible-greek-keymap))
- ((string-match "strong:H.*" item) ; Hebrew
- (setq matched (match-string 0 item))
- (put-text-property refstart refend 'keymap bible-hebrew-keymap)))
- ;; Add help-echo, strongs reference for tooltips if match.
- (when matched
- (setq bible-has-strongs t)
- (put-text-property refstart refend 'help-echo 'bible--show-lex-morph)
- (put-text-property refstart refend 'strong matched))))
- ;; morphology
- (when morph
- (let ((matched nil))
- (cond ((string-match "robinson:.*" morph) ; Robinson Greek morphology
- (setq matched (match-string 0 morph)))
- ((string-match "packard:.*" morph) ; Packard Greek morphology --- LXX seems to use this
- (setq matched (match-string 0 morph)))
- ((string-match "oshm:.*" morph) ; OSHM Hebrew morphology
- (setq matched (match-string 0 morph)))
- (t nil
- ;;(message "Unknown morphology %s" morph)
- ))
- (when matched
- (setq bible-has-morphology t)
- (put-text-property refstart refend 'morph matched)
- (put-text-property refstart refend 'help-echo 'bible--show-lex-morph))))
- ;; Insert lemma into buffer. Lemma tag will be part of savlm item.
- ;; XXX Should I do lexicon lookups on these lemmas? I don't use
- ;; this anyway....
- (when (and bible-word-study-enabled savlm (string-match "lemma.*:.*" savlm))
- (dolist (word (split-string (match-string 0 savlm) " "))
- (setq word (replace-regexp-in-string "[.:a-zA-Z0-9]+" "" word))
- (insert " " word)
- (let ((refstart (- (point) 1 (length word)))
- (refend (point)))
- (add-face-text-property refstart refend '(:foreground "blue"))
- (put-text-property refstart refend 'keymap bible-lemma-keymap)))))))
- (defun bible-new-line ()
- "Ensure beginning of line. Try to avoid redundant blank lines."
- (unless (= (current-column) 0)
- (insert "\n")))
- (defun bible--insert-domnode-recursive (node &optional iproperties notitle)
- "Recursively parse domnode NODE obtained from `libxml-parse-html-region'.
- Inserts resulting text into active buffer with properties specified in
- IPROPERTIES. If NOTITLE is true, don't render title headings.
- XXX In processing subnodes, each case will prepend a space if it needs it."
- (when (and bible-red-letter-enabled (equal (dom-attr node 'who) "Jesus"))
- ;; For red-letter display.
- (setq iproperties (plist-put iproperties 'jesus t)))
- (dolist (subnode (dom-children node))
- (cond ((null subnode) nil)
- ((stringp subnode)
- (let ((search-string (concat (car bible--current-book) " " (number-to-string bible--current-chapter) ":"))
- (start 0))
- ;; Delete <Book Ch:> (may be more than one)
- (setq subnode (string-replace search-string "" subnode))
- ;; Highlight verse number(s)
- (while (string-match "^ ?[0-9]+:" subnode start)
- (setq start (match-end 0))
- (add-face-text-property (match-beginning 0) (match-end 0) '(:foreground "purple") nil subnode)))
- ;; Red letter
- (when (plist-get iproperties 'jesus)
- (add-face-text-property 0 (length subnode) '(:foreground "red") nil subnode))
- (insert subnode))
- ((consp subnode)
- (let ((tag (dom-tag subnode)))
- (pcase tag
- ;; Maybe process these at some point? Include footnotes etc.
- ;; ('node nil)
- ;; ('lb nil)
- ;; These tags appear in ESV modules (and maybe others?) XXX still not right
- ('l
- (let ((attributes (dom-attributes subnode)))
- (cond ((equal (cdr (assoc 'type attributes)) "x-br")
- (bible-new-line))
- ((equal (cdr (assoc 'type attributes)) "x-indent")
- (insert "\t"))
- ((assoc 'level attributes)
- (let ((indent (string-to-number (alist-get 'level attributes))))
- (when (= indent 2) (insert "\n\t")))))))
- ('title (when (not notitle) (setq bible-chapter-title subnode) (bible-new-line)))
- ('body (bible--insert-domnode-recursive subnode iproperties notitle))
- ;; NASB Module uses this to indicate OT quotations (and others?).
- ('seg (bible--insert-domnode-recursive subnode iproperties notitle))
- ('divinename (bible-handle-divine-name subnode))
- ;; This tag is used for red letter.
- ((or 'p 'q) (bible--insert-domnode-recursive subnode iproperties notitle))
- ('w (insert " ") (bible--process-word subnode iproperties))
- ;; Some modules use this for line breaks and such.
- ('milestone (when (equal (dom-attr subnode 'type) "line") (bible-new-line)))
- ('div (when (equal (dom-attr subnode 'type) "paragraph") (bible-new-line)))
- ;; Word inserted by translation, not in original, give visual indication.
- ((or 'transchange 'hi)
- (let ((word (dom-text subnode)))
- (insert " " word)
- (if (plist-get iproperties 'jesus)
- (add-face-text-property (- (point) (length word)) (point) '(:foreground "salmon"))
- (add-face-text-property (- (point) (length word)) (point) '(:foreground "gray50")))))))))))
- (defun bible--display (&optional verse)
- "Render text for `bible'. If VERSE is supplied, set cursor at verse."
- ;; Display buffers can have different modules.
- (setq-local bible-module (default-value 'bible-module))
- ;; Clear buffer and insert the result of calling bible--exec-diatheke.
- (let ((buffer-read-only nil)
- (bible-chapter-title nil)
- (bible-has-strongs nil)
- (bible-has-morphology nil))
- (erase-buffer)
- (insert (bible--exec-diatheke (concat bible--current-book-name ":" (number-to-string bible--current-chapter))))
- ;; Parse the xml in the buffer into a DOM tree.
- (let ((html-dom-tree (libxml-parse-html-region (point-min) (point-max))))
- ;; Render the DOM tree into the buffer.
- (unless bible-debugme
- (erase-buffer)
- ;; Looking for the "body" tag in the DOM node.
- (bible--insert-domnode-recursive (dom-by-tag html-dom-tree 'body) nil nil)
- (goto-char (point-min))))
- ;; Remove the module name from the buffer.
- (while (search-forward (concat "(" bible-module ")") nil t)
- (replace-match ""))
- ;; Set the mode line of the biffer.
- (setq mode-name (concat "Bible (" bible--current-book-name " " (number-to-string bible--current-chapter) ") "
- bible-module
- (when bible-has-strongs " Lex")
- (when bible-has-morphology " Morph")
- ")"))
- ;; Deal with chapter titles (i.e. in Psalms)
- ;; N.B. This won't change a title inside a chapter, and so it
- ;; doesn't work with Psalm 119 where the acrostic letters get
- ;; printed as "titles".
- (when bible-chapter-title ; This gets set in bible-insert-domnode-recursive.
- (goto-char (point-min))
- (let ((title-text (dom-texts bible-chapter-title))
- (refstart (point-min))
- refend)
- ;; Insert and make bold the title.
- ;;;; (when (string-or-null-p title-text) ;;; XXXX Maybe wrong
- (when (stringp title-text) ;;; XXXX Maybe wrong
- (insert title-text "\n")
- (setq refend (point))
- (put-text-property refstart refend 'face 'bold))))
- ;; Get rid of spurious spaces.
- (format-replace-strings '(("." . ". ")
- ("," . ", ")
- (";" . "; ")
- (":" . ": ")
- ("?" . "? ")
- ("!" . "! ")
- (" ." . ". ")
- (" ," . ", ")
- (" ;" . "; ")
- (" :" . ": ")
- (" ?" . "? ")
- (" !" . "! ")
- ("“ " . "“")
- ("‘ " . "‘")
- (" ’" . "’")
- (". ”" . ".”")
- ("? ”" . "?”")
- (" " . " ")
- (" " . " "))
- nil (point-min) (point-max)))
- (goto-char (point-min))
- ;; If optional verse specification go to that verse.
- (when verse
- (re-search-forward (regexp-opt `(,(concat (number-to-string verse) ":"))))))
- (defun bible--list-biblical-modules ()
- "Return a list of accessible Biblical Text modules."
- (let ((text (bible--exec-diatheke "modulelist" nil nil "system"))
- modules)
- (catch 'done
- (dolist (line (split-string text "[\n\r]+"))
- (when (equal line "Commentaries:")
- (throw 'done nil))
- (when (not (equal "Biblical Texts:" line))
- (push (split-string line " : ") modules))))
- modules))
- (defun bible-pick-module ()
- "Keymap action function---select module user chooses."
- (interactive)
- (let ((item (get-text-property (point) 'module)))
- (setq-default bible-module item)
- (bible-open)))
- (defconst bible-module-map (make-keymap))
- (define-key bible-module-map [mouse-1] 'bible-pick-module)
- (define-key bible-module-map (kbd "RET") 'bible-pick-module)
- (defun bible-display-available-modules ()
- "Display available modules, allow user to select."
- (interactive)
- (with-current-buffer (get-buffer-create "Modules")
- (bible-module-select-mode)
- (let ((buffer-read-only nil))
- (erase-buffer)
- (setq-local tab-stop-list '(25))
- (dolist (mod (bible--list-biblical-modules))
- (let ((name (string-trim (car mod)))
- (description (string-trim-left (cadr mod))))
- (insert
- (propertize (string-trim name)
- 'face 'bold
- 'module name
- 'help-echo (concat "Select " name)
- 'keymap bible-module-map))
- (move-to-tab-stop)
- (insert (format "%s\n" description)))))
- (goto-char (point-min))
- (pop-to-buffer (current-buffer) nil t)))
- ;;;;; Bible Searching
- (defun bible--open-search (query searchmode)
- "Open a search buffer of QUERY using SEARCHMODE in module MOD."
- (let ((results (string-trim (replace-regexp-in-string
- "Entries .+?--" ""
- (bible--diatheke-search query searchmode "plain" bible-module)))))
- (if (equal results (concat "none (" bible-module ")"))
- (message (concat
- "No results found."
- (when (equal searchmode "lucene")
- " Verify index has been build with mkfastmod.")))
-
- (with-current-buffer (get-buffer-create (concat "*bible-search-" (downcase bible-module) "-" query "*"))
- (bible-search-mode)
- (bible--display-search results)
- (pop-to-buffer (current-buffer) nil t)))))
- (defun bible--display-search (results)
- "Render RESULTS of search query."
- (let ((match 0)
- (matchstr "")
- (verses nil)
- (query-verses "")
- (buffer-read-only nil))
- (erase-buffer)
- (while match
- (setq match (string-match ".+?:[0-9]?[0-9]?" results (+ match (length matchstr)))
- matchstr (match-string 0 results))
- (when match
- (push
- ;; Massage match to make it more sortable, get rid of some characters.
- (replace-regexp-in-string
- "I " "1"
- (replace-regexp-in-string
- "II " "2"
- (replace-regexp-in-string
- "III " "3"
- (replace-regexp-in-string ".+; " "" matchstr))))
- verses)))
- (setq match 0)
- (setq verses (sort verses))
- (dolist (verse verses)
- (if query-verses
- (setq query-verses (concat query-verses ";" verse))
- (setq query-verses verse)))
- (let ((bible-show-diatheke-exec nil))
- (insert (bible--exec-diatheke query-verses nil nil bible-module))
- (let* ((html-dom-tree (libxml-parse-html-region (point-min) (point-max))))
- (erase-buffer)
- (bible--insert-domnode-recursive (dom-by-tag html-dom-tree 'body) nil nil)
- (goto-char (point-min))
- (while (search-forward (concat "(" bible-module ")") nil t)
- (replace-match "")))
- (setq mode-name (concat "Bible Search (" bible-module))
- (when bible-search-range
- (setq mode-name (concat mode-name " [" bible-search-range "]")))
- (setq mode-name (concat mode-name ")"))
- (goto-char (point-min)))))
- ;;;;; Terms
- ;;(defun bible-display-morphology (morph)
- ;; ;; xxx Do something here?
- ;; )
- (defun bible--display-term (termtype)
- "Display a term of language given by TERMTYPE."
- (let ((buffer-read-only nil))
- (goto-char (point-min))
- ;; This enables clicking on Strong's numbers in some lexicon definitions.
- (while (search-forward-regexp "[0-9]+" nil t)
- (cond ((eq termtype 'hebrew)
- (put-text-property (match-beginning 0) (match-end 0) 'strong (concat "strong:H" (match-string 0)))
- (put-text-property (match-beginning 0) (match-end 0) 'keymap bible-hebrew-keymap)
- (add-face-text-property (match-beginning 0) (match-end 0) `(:foreground "blue")))
- ((eq termtype 'greek)
- (put-text-property (match-beginning 0) (match-end 0) 'strong (concat "strong:G" (match-string 0)))
- (put-text-property (match-beginning 0) (match-end 0) 'keymap bible-greek-keymap)
- (add-face-text-property (match-beginning 0) (match-end 0) `(:foreground "blue")))))
- (goto-char (point-min))
- ;; This enables clicking on verse references.
- (while (search-forward-regexp bible--verse-regexp nil t)
- (put-text-property (match-beginning 0) (match-end 0) 'xref (match-string 0))
- (put-text-property (match-beginning 0) (match-end 0) 'keymap bible-search-mode-map)
- (put-text-property (match-beginning 0) (match-end 0) 'help-echo (concat "Go to " (match-string 0)))
- (add-face-text-property (match-beginning 0) (match-end 0) '(:foreground "blue")))
- (goto-char (point-min))
- (while (search-forward "()" nil t)
- (replace-match ""))
- (goto-char (point-min))))
- (defun bible--open-term-hebrew (term)
- "Open a buffer of the Strong's Hebrew TERM's definition."
- (with-current-buffer (get-buffer-create (concat "*bible-term-hebrew-" term "*"))
- (bible-term-hebrew-mode)
- (bible--display-term-hebrew term)
- (pop-to-buffer (current-buffer) nil t)
- (fit-window-to-buffer)))
- (defun bible--open-term-greek (term)
- "Open a buffer of the Strong's Greek TERM definition."
- (with-current-buffer (get-buffer-create (concat "*bible-term-greek-" term "*"))
- (bible-term-greek-mode)
- (bible--display-term-greek term)
- (pop-to-buffer (current-buffer) nil t)
- (fit-window-to-buffer)))
- ;;;
- ;;; Note: Hebrew display of terms is backwards; set bidi direction to
- ;;; 'left-to-right.
- (defun bible--display-term-hebrew (term)
- "Render the definition of the Strong's Hebrew TERM.
- Sets the variable `bidi-paragraph-direction' so the English text will
- render left-to-right. XXX Why doesn't this work for the tooltips?"
- (let ((buffer-read-only nil))
- (erase-buffer)
- (insert (replace-regexp-in-string
- (regexp-opt `(,bible-hebrew-lexicon))
- ""
- (bible--exec-diatheke term nil "plain" bible-hebrew-lexicon)
- nil nil nil 7))
- (bible--display-term 'hebrew)
- (setq bidi-paragraph-direction 'left-to-right)))
- (defun bible--display-term-greek (term)
- "Render the definition of the Strong's Greek TERM."
- (let ((buffer-read-only nil))
- (erase-buffer)
- (insert (replace-regexp-in-string
- (regexp-opt `(,bible-greek-lexicon))
- ""
- (bible--lookup-lex-greek term)))
- (bible--display-term 'greek)))
- (defun bible--set-location (book chapter &optional verse)
- "Set the BOOK, CHAPTER and optionally VERSE of the active `bible' buffer."
- (setq-local bible-module (default-value 'bible-module))
- (setq-local bible--current-book book)
- (setq-local bible--current-book-name (car book))
- (setq-local bible--current-chapter chapter)
- (bible--display verse))
- ;;;;; Utilities
- (defun bible--list-number-range (min max &optional prefix)
- "Returns a list containing entries for each integer between MIN and MAX.
- If PREFIX is supplied, prepend PREFIX to the entries.
- Used in tandem with `completing-read' for chapter selection."
- (let ((range-list nil))
- (dotimes (num (1+ max))
- (when (>= num min)
- (push (cons (concat prefix (number-to-string num)) num) range-list)))
- (nreverse range-list)))
- ;;; Provides
- (provide 'bible)
- ;;; bible.el ends here.
- ;; Local Variables:
- ;; End:
|