;;; bible.el --- A Bible browsing application -*- lexical-binding: t; mode: EMACS-LISP; indent-tabs-mode: nil -*- ;; Copyright (c) 2025-2026 Fred Gilham ;; Author: Fred Gilham ;; Version: 1.2.0 ;; Keywords: files, text, hypermedia ;; Package-Requires: ((emacs "29.1") cl-lib dom shr) ;; URL: https://gitbot.homedns.org/fred/bible ;; This file is not part of GNU Emacs. ;; bible.el is free software; you can redistribute it and/or modify it ;; under the terms of the GNU General Public License as published by ;; the Free Software Foundation; either version 3, or (at your option) ;; any later version. ;; bible.el is distributed in the hope that it will be useful, but ;; WITHOUT ANY WARRANTY; without even the implied warranty of ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ;; General Public License for more details. ;; You should have received a copy of the GNU General Public License ;; along with this file; see the file LICENSE. If not, see ;; . ;;; 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 (https://crosswire.org). ;; Word study is also supported. ;;;; Usage ;; Use M-x `bible-open' to open a Bible buffer. ;; Use C-h f `bible' to see available keybindings. ;; The program also installs a Bible menu with keybindings and other ;; commands. ;; You may customize `bible-text' to set a default browsing module, as ;; well as `bible-word-study-enabled' to enable word study by default. ;; NB: Currently this just shows the lemmas in the original language ;; if present. Tooltips will display whenever there are strongs ;; numbers in the module. ;;;; Design ;; The idea here is to use the diatheke program to lookup text from ;; modules (biblical texts), then insert this text into buffers. The ;; main bible display uses diatheke's 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 hovering over words will bring ;; up a tooltip with information about the word. Clicking on a word ;; with lexical information will display that information in a "term" ;; buffer. ;;; Code: ;;;; Environment stuff ;; Turn off modes because we are greedy for pixels.... (tool-bar-mode -1) (scroll-bar-mode -1) ;; eldoc isn't meaningful in this program, and this saves space in the ;; mode line. (global-eldoc-mode -1) ;;;; Requirements (require 'cl-lib) (require 'dom) (require 'shr) (require 'menu+ nil t) ; If you have it, it looks nice. ;;;; Aliases for obsolete functions ;; dom-text and dom-texts declared obsolescent in Emacs 31. Check for ;; new function, retain backward compatibility. ;; Note that the following is the simplest way I found to avoid compile warnings. (defalias 'bible-dom-text (if (fboundp 'dom-inner-text) 'dom-inner-text 'dom-text)) (defalias 'bible-dom-texts (if (fboundp 'dom-inner-texts) 'dom-inner-text 'dom-texts)) ;;;; Customization Variables (defgroup bible nil "Settings for `bible'." :group 'tools :link '(url-link "https://gitbot.homedns.org/fred/bible-mode")) (defcustom bible-text "KJV" "Customize default bible text module for Diatheke to query. \(For full list of installed modules, run `diatheke -b system -l bibliography'\)" :type '(choice (const :tag "None" nil) (string :tag "Bible text (e.g. \"KJV\")")) :local nil :group 'bible) (defcustom bible-commentary "Clarke" "Customize default commentary module for Diatheke to query. \(For full list of installed modules, run `diatheke -b system -l bibliography'\)" :type '(choice (const :tag "None" nil) (string :tag "Commentary (e.g. \"Clarke\")")) :local nil :group 'bible) ;; TODO: Not implememted yet (FMG 5-Mar-2026) (defcustom bible-font "Ezra SIL" "Default font for bible (not yet implemented)." :type '(string :tag "Font family name (e.g. \"Ezra SIL\")") :local t :group 'bible) (defcustom bible-sword-query "diatheke" "Specify program used to query sword modules. Must be some version of the sword library's diatheke program." :type '(string :tag "Sword library query executable (e.g. \"/usr/local/bin/diatheke\").") :local nil :group 'bible) (defcustom bible-greek-lexicon ;; AbbottSmithStrongs now has both links to lemmas and definitions ;; keyed by lemma. So we only need the AbbottSmithStrongs lexicon ;; and not the AbbottSmith lexicon. "AbbottSmithStrongs" "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." :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 : @LINK " :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) ;; HACK: The Hebrew lexicons differ on whether they accept keys of the ;; form `Hnnnn' or `nnnn'. The code does not yet handle this ;; correctly, so stick with the following. (FMG 5-Mar-2026) (defcustom bible-hebrew-lexicon "BDBGlosses_Strongs" ; This seems to work "Specify Lexicon used to display definitions of Hebrew words. Note that changing this may require changing some code. See `bible--display-lemma-hebrew'." :type '(string :tag "Lexicon module (e.g. \"BDBGlosses_Strongs\")") :local nil :group 'bible) (defcustom bible-hebrew-lexicon-short "StrongsRealHebrew" ;; "StrongsHebrew" "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 original language Lemma words if present in module. \(KJV New Testament has this.\)" :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 nil "Show the arguments by which diatheke is executed (mostly for debugging)." :type 'boolean :local nil :group 'bible) ;;;; Mode line formats for different kinds of buffers. (defvar bible-mode-line-format '("%e" mode-line-front-space mode-line-frame-identification mode-line-buffer-identification " " ;; bible-text " " bible--current-book-name " " (:eval (number-to-string bible--current-chapter)) " " (:eval (if bible--synced-p "Sync" "")) (:eval (when bible-search-range (concat " <" bible-search-range ">"))) " " mode-line-modes mode-line-misc-info mode-line-end-spaces) "Mode line format for bible buffers.") (defvar bible-search-mode-line-format '("%e" mode-line-front-space mode-line-frame-identification mode-line-buffer-identification " " bible-search-text-this-query " " bible-search-word-this-query " " (:eval (when bible-search-range-this-query (concat "<" bible-search-range-this-query "> "))) (:eval (number-to-string bible-search-matches)) " matches" " " mode-line-modes mode-line-misc-info mode-line-end-spaces) "Mode line format for bible search buffers.") (defvar bible-term-mode-line-format '("%e" mode-line-front-space mode-line-frame-identification mode-line-buffer-identification " " ;; bible-term-language ;; " " (:eval bible-term-lemma) " " mode-line-modes mode-line-misc-info mode-line-end-spaces) "Mode line format for bible search buffers.") ;;;; 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-local mode-line-format bible-mode-line-format) (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-local mode-line-format bible-search-mode-line-format) (setq buffer-read-only t) (visual-line-mode t)) (define-derived-mode bible-term-mode special-mode "Bible Term" "Mode for researching terms in the Bible. \\{bible-term-mode-map}" (buffer-disable-undo) (font-lock-mode t) (use-local-map bible-term-mode-map) (setq-local mode-line-format bible-term-mode-line-format) (setq buffer-read-only t) (visual-line-mode t)) (define-derived-mode bible-term-hebrew-mode bible-term-mode "Bible Term" "Mode for researching Hebrew terms in the Bible. \\{bible-term-hebrew-mode-map}" (setq-local bible-term-language "Hebrew")) (define-derived-mode bible-term-greek-mode bible-term-mode "Bible Term" "Mode for researching Greek terms in the Bible. \\{bible-term-greek-mode-map}" (setq-local bible-term-language "Greek")) (define-derived-mode bible-text-select-mode special-mode "Select Module" (buffer-disable-undo) (font-lock-mode t) (setq buffer-read-only t)) ;;;; 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)) (define-key bible-map [menu-bar bible display-diatheke] '("Toggle diatheke display" . bible-toggle-display-diatheke)) (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 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 sep] '(menu-item '"--")) ;;;;; Misc key bindings (define-key bible-map "T" 'bible-select-text) (define-key bible-map "C" 'bible-select-commentary) (define-key bible-map "w" 'bible-toggle-word-study) (define-key bible-map "l" 'bible-toggle-red-letter) (define-key bible-map "z" 'text-scale-adjust) (define-key bible-map [menu-bar bible zoom-text] '("Zoom Text" . text-scale-adjust)) (define-key bible-map "x" 'bible-split-display) (define-key bible-map [menu-bar bible split-display] '("Split Display" . bible-split-display)) (define-key bible-map "S" 'bible-toggle-buffer-sync) (define-key bible-map [menu-bar bible sync] '("Toggle Synchronize Buffer" . bible-toggle-buffer-sync)) ;;;;; 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-") '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 navigation. (define-key bible-map "\C-n" 'next-logical-line) (define-key bible-map "\C-p" 'previous-logical-line) ;;;;; 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)) (define-key bible-map [menu-bar bible sepp] '(menu-item '"--")) (define-key bible-map [menu-bar bible sepp] '(menu-item '"--")) (define-key bible-map [menu-bar bible select-biblical-commentary] '("Select Commentary" . bible-display-available-commentaries)) (define-key bible-map [menu-bar bible select-biblical-text] '("Select Text" . bible-display-available-texts)) (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-follow-verse) (define-key bible-search-mode-map [mouse-1] 'bible-follow-verse) ;;;;; Term mode keymaps (defconst bible-term-mode-map (make-sparse-keymap)) (define-key bible-term-mode-map "z" 'text-scale-adjust) (define-key bible-term-mode-map [mouse-1] 'bible-follow-xref) (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) (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))) ;; Not used. Not really sure what to do here or if it's useful to do anything. (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)) )) ;;;;; Module choice keymaps. (defconst bible-text-map (make-keymap)) (define-key bible-text-map [mouse-1] 'bible-pick-text) (define-key bible-text-map (kbd "RET") 'bible-pick-text) (defconst bible-commentary-map (make-keymap)) (define-key bible-commentary-map [mouse-1] 'bible-pick-commentary) (define-key bible-commentary-map (kbd "RET") 'bible-pick-commentary) ;;;; Variable definitions (defconst bible--verse-regexp "\\(I \\|1 \\|II \\|2 \\|III \\|3 \\)??[a-zA-Z]+?[ \t\n][0-9]+[:][0-9]+") ;; Don't know how to get footnotes and scripture cross references yet. ;;(defconst bible-diatheke-filter-options " afilmnsvw") (defconst bible-diatheke-filter-options " almnvw") (defvar bible--text-buffers nil "List of Bible text buffers.") (defvar bible--commentary-buffers nil "List of commentary buffers.") (defvar bible--synced-buffers nil "List of buffers that are synchronized so that navigation in one applies to all of them.") (defvar-local bible--synced-p nil "Is this buffer syncronized?") (defvar bible--texts (lazy-completion-table bible--texts bible--list-biblical-texts)) (defvar bible--commentaries (lazy-completion-table bible--commentaries bible--list-biblical-commentaries)) ;; REVIEW: I believe these chapter counts aren't the same for all modules, e.g. JPS. (FMG 5-Mar-2026) (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.") ;; These abbreviations are used to follow cross-references in commentaries and lexicons. ;; Abbreviations from NETnote module and Clarke module (commentaries). ;; Abbreviations from some lexicons. ;; Standard abbreviations come first. (defvar bible--book-name-abbreviations '(;; Old Testament ("Gen" . "Genesis") ("Ge" . "Genesis") ("Exod" . "Exodus") ("Ex" . "Exodus") ("Exo" . "Exodus") ("Lev" . "Leviticus") ("Le" . "Leviticus") ("Num" . "Numbers") ("Nu" . "Numbers") ("Deut" . "Deuteronomy") ("De" . "Deuteronomy") ("Deu" . "Deuteronomy") ("Josh" . "Joshua") ("Js" . "Joshua") ("Jos" . "Joshua") ("Judg" . "Judges") ("Jg" . "Judges") ("Jdg" . "Judges") ("Ru" . "Ruth") ("Rut" . "Ruth") ("1 Sam" . "I Samuel") ("1 Samuel" . "I Samuel") ("I Sa" . "I Samuel") ("1 Sa" . "I Samuel") ("1Sam" . "I Samuel") ("2 Sam" . "II Samuel") ("2 Samuel" . "II Samuel") ("II Sa" . "II Samuel") ("2 Sa" . "II Samuel") ("2Sam" . "II Samuel") ("1 Kgs" . "I Kings") ("1 Kings" . "I Kings") ("I Ki" . "I Kings") ("1 Ki" . "I Kings") ("1Ki" . "I Kings") ("2 Kgs" . "II Kings") ("2 Kings" . "II Kings") ("II Ki" . "II Kings") ("2 Ki" . "II Kings") ("2Ki" . "II Kings") ("1 Chr" . "I Chronicles") ("1 Chronicles" . "I Chronicles") ("I Ch" . "I Chronicles") ("1 Ch" . "I Chronicles") ("2 Chr" . "II Chronicles") ("2 Chronicles" . "II Chronicles") ("II Ch" . "II Chronicles") ("2 Ch" . "II Chronicles") ("Ezr" . "Ezra") ("Neh" . "Nehemiah") ("Ne" . "Nehemiah") ("Esth" . "Esther") ("Es" . "Esther") ("Est" . "Esther") ("Jb" . "Job") ("Ps" . "Psalms") ("Psa" . "Psalms") ("Prov" . "Proverbs") ("Pr" . "Proverbs") ("Pro" . "Proverbs") ("Eccl" . "Ecclesiastes") ("Ec" . "Ecclesiastes") ("Ecc" . "Ecclesiastes") ("Song" . "Song of Solomon") ("So" . "Song of Solomon") ("Sol" . "Song of Solomon") ("Isa" . "Isaiah") ("Is" . "Isaiah") ("Jer" . "Jeremiah") ("Je" . "Jeremiah") ("Lam" . "Lamentations") ("La" . "Lamentations") ("Ezek" . "Ezekiel") ("Ez" . "Ezekiel") ("Eze" . "Ezekiel") ("Dan" . "Daniel") ("Da" . "Daniel") ("Hos" . "Hosea") ("Ho" . "Hosea") ("Joe" . "Joel") ("Am" . "Amos") ("Amo" . "Amos") ("Obad" . "Obadiah") ("Ob" . "Obadiah") ("Oba" . "Obadiah") ("Jon" . "Jonah") ("Mic" . "Micah") ("Mi" . "Micah") ("Nah" . "Nahum") ("Na" . "Nahum") ("Hab" . "Habakkuk") ("Ha" . "Habakkuk") ("Zeph" . "Zephaniah") ("Zep" . "Zephaniah") ("Hag" . "Haggai") ("Zech" . "Zechariah") ("Ze" . "Zechariah") ("Zac" . "Zechariah") ;; Is the last one correct?? ("Mal" . "Malachi") ;; New Testament ("Mt" . "Matthew") ("Mat" . "Matthew") ("Matt" . "Matthew") ("Mk" . "Mark") ("Mar" . "Mark") ("Lk" . "Luke") ("Luk" . "Luke") ("Jn" . "John") ("Jo" . "John") ("Joh" . "John") ("Ac" . "Acts") ("Act" . "Acts") ("Rom" . "Romans") ("Ro" . "Romans") ("1 Cor" . "I Corinthians") ("1 Corintihans" . "I Corinthians") ("I Co" . "I Corinthians") ("1 Co" . "I Corinthians") ("ICor" . "I Corinthians") ("1Cor" . "I Corinthians") ("2 Cor" . "II Corinthians") ("2 Corinthians" . "II Corinthians") ("II Co" . "II Corinthians") ("2 Co" . "II Corinthians") ("IICor" . "II Corinthians") ("2Cor" . "II Corinthians") ("Gal" . "Galatians") ("Ga" . "Galatians") ("Eph" . "Ephesians") ("Phil" . "Philippians") ("Phl" . "Philippians") ("Col" . "Colossians") ("1 Thess" . "I Thessalonians") ("1 Thessalonians" . "I Thessalonians") ("I Th" . "I Thessalonians") ("1 Th" . "I Thessalonians") ("IThess" . "I Thessalonians") ("1Thes" . "I Thessalonians") ("1Thess" . "I Thessalonians") ("2 Thess" . "II Thessalonians") ("2 Thessalonians" . "II Thessalonians") ("II Th" . "II Thessalonians") ("2 Th" . "II Thessalonians") ("IIThess" . "II Thessalonians") ("2Thes" . "II Thessalonians") ("2Thess" . "II Thessalonians") ("1 Tim" . "I Timothy") ("1 Timothy" . "I Timothy") ("I Ti" . "I Timothy") ("1 Ti" . "I Timothy") ("ITim" . "I Timothy") ("1Tim" . "I Timothy") ("2 Tim" . "II Timothy") ("2 Timothy" . "II Timothy") ("II Ti" . "II Timothy") ("2 Ti" . "II Timothy") ("IITim" . "II Timothy") ("2Tim" . "II Timothy") ("Tit" . "Titus") ("Phlm" . "Philemon") ("Phm" . "Philemon") ("Plm" . "Philemon") ("Heb" . "Hebrews") ("He" . "Hebrews") ("Jas" . "James") ("Ja" . "James") ("Jam" . "James") ("1 Pet" . "I Peter") ("1 Peter" . "I Peter") ("I Pe" . "I Peter") ("1 Pe" . "I Peter") ("IPet" . "I Peter") ("1Pet" . "I Peter") ("2 Pet" . "II Peter") ("2 Peter" . "II Peter") ("II Pe" . "II Peter") ("2 Pe" . "II Peter") ("IIPet" . "II Peter") ("2Pet" . "II Peter") ("1 Jn" . "I John") ("1 John" . "I John") ("I Jo" . "I John") ("1 Jo" . "I John") ("IJohn" . "I John") ("1Jn" . "I John") ("2 Jn" . "I John") ("2 John" . "II John") ("II Jo" . "II John") ("2 Jo" . "II John") ("IIJohn" . "II John") ("2Jn" . "I John") ("3 Jn" . "I John") ("3 John" . "III John") ("III Jo" . "III John") ("3 Jo" . "III John") ("IIIJohn" . "III John") ("3Jn" . "I John") ("Ju" . "Jude") ("Jde" . "Jude") ("Rev" . "Revelation of John") ("Re" . "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.") ;;;;; Search / query (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) ;;;;; Lexemes / morphemes (defvar-local bible-term-language nil "Displaying terms of this language.") (defvar-local bible-term-lemma nil "Lemma for term mode line.") (defvar-local bible-has-lexemes nil "Set if the module being displayed has lexical entries availabile.") (defvar-local bible-has-morphemes nil "Set if the module being displayed has morphemes availabile.") (defvar-local bible-text-direction 'left-to-right) (defvar-local bible-debugme nil "Make text show up as XML when set.") (defvar bible-use-tooltips t) (setq tooltip-delay 1) (setq tooltip-short-delay .5) (setq use-system-tooltips nil) (defvar-local bible-search-query nil "Query used in toggles (word study and red letter).") (defvar-local bible-chapter-title nil "Text preceding start of chapter. Mostly in Psalms, like `Of David' or the like.") (defvar-local bible-level "0" "Used by some modules for indentation and line breaks.") ;;;; Greek and Hebrew lexeme and morpheme tooltip rendering. ;;;;; Hash tables for Lexical definitions. (defvar bible-hash-greek (make-hash-table :test 'equal :size 10000)) (defvar bible-hash-hebrew (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)) (defvar bible-outline-strings '(;;(". ." . ".") (" I. ." . "\nI.") (" II. ." . " II.") (" III. ." . " III.") (" IV. ." . " IV.") (" V. ." . " V.") ("1. ." . "\n 1.") ("2. ." . "2.") ("3. ." . "3.") ("4. ." . "4.") ("5. ." . "5.") ("6. ." . "6.") ("7. ." . "7.") ("8. ." . "8.") ("9. ." . "9.") ("a. ." . "\n a.") ("(a)." . "\n (a).") ("b. ." . " b.") ("c. ." . " c.") ("d. ." . " d.") ("e. ." . " e.") ("f. ." . " f.") ("g. ." . " g.") ("h. ." . " h.") (" . " . ". ") ("\n\n" . "\n"))) ;;;;; Variables for mode-line-format for search buffers. (defvar-local bible-search-word-this-query "") (defvar-local bible-search-text-this-query "") (defvar-local bible-search-range-this-query nil) (defvar-local bible-search-matches 0) ;;;; Functions ;;;;; Keymap helpers (defun bible-toggle-display-diatheke () "Toggle diatheke args display." (interactive) (setq bible-show-diatheke-exec (not bible-show-diatheke-exec)) (message "")) (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)) (defun bible-toggle-display-xml () "Toggle XML display." (interactive) (setq-local bible-debugme (not bible-debugme)) (bible--display)) (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)) (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 (not bible-use-tooltips)) (setq bible-show-diatheke-exec (and bible-show-diatheke-exec bible-use-tooltips)) ; Don't conflict with echo area (message "")) ;;;;; Commands (interactive) (defun bible-open (&optional buffer book-name chapter verse module) "Create and open a `bible' buffer. Optional arguments BOOK-NAME, CHAPTER and VERSE, when supplied, give the starting verse reference for the buffer. If no optional location arguments are supplied, Genesis 1:1 is used. Optional argument MODULE specifies the module to use." (interactive) (with-current-buffer (or buffer (get-buffer-create (generate-new-buffer-name "*bible*"))) (bible) (when module (setq-default bible-text module)) (setq-local bible-text (default-value 'bible-text)) (setq-local mode-name (concat "Text " bible-text)) (bible--set-location (assoc (or book-name "Genesis") bible--books) (or chapter 1) verse) (cl-pushnew (current-buffer) bible--text-buffers) (set-window-buffer (get-buffer-window (current-buffer)) (current-buffer)))) (defvar-local associated-buffer nil) (defun commentary-open (&optional module book-name chapter verse) "Create and open a `commentary' buffer. Optional argument MODULE specifies the commentary module to use. Optional arguments BOOK-NAME, CHAPTER and VERSE, when supplied, give the starting verse reference for the buffer. If no optional location arguments are supplied, Genesis 1:1 is used." (interactive) (let ((old-buffer (current-buffer))) (with-current-buffer (get-buffer-create (generate-new-buffer-name "*commentary*")) (bible) (when module (setq-default bible-commentary module)) (setq-local associated-buffer old-buffer) (setq-local bible-text (default-value 'bible-commentary)) (setq-local mode-name (concat "Commentary " bible-text)) (bible--set-location (assoc (or book-name "Genesis") bible--books) (or chapter 1) verse) (cl-pushnew (current-buffer) bible--commentary-buffers) (set-window-buffer (get-buffer-window (current-buffer)) (current-buffer))))) ;;;;;; Navigation (defun bible--do-set-location (book chapter &optional verse) (setq-local bible--current-book book) (setq-local bible--current-book-name (car book)) (setq-local bible--current-chapter chapter) (bible--display verse)) (defun bible--set-location (book chapter &optional verse) "Set the BOOK, CHAPTER and optionally VERSE of the active `bible' buffer." (let ((buffer (current-buffer))) (bible--do-set-location book chapter verse) (when (cl-find buffer bible--synced-buffers) (save-excursion (dolist (buf bible--synced-buffers) (unless (eq buf buffer) (with-current-buffer buf (bible--do-set-location book chapter verse)))))))) (defun bible-next-chapter () "Page to the next chapter for the active `bible' buffer and for any synchronized buffers." (interactive) (let* ((book-chapters (cdr bible--current-book)) (chapter (min book-chapters (1+ bible--current-chapter)))) (bible--set-location bible--current-book chapter))) (defun bible-previous-chapter () "Page to the previous chapter for the active `bible' buffer and for any synchronized buffers." (interactive) (bible--set-location bible--current-book (max 1 (1- bible--current-chapter)))) (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)))))) ;;;;;; Select Location (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" (substring book-data-string 1)) (cdr book-data)))) (?2 (setq book-data (cons (concat "II" (substring book-data-string 1)) (cdr book-data)))) (?3 (setq book-data (cons (concat "III" (substring book-data-string 1)) (cdr book-data))))) (bible--set-location book-data chapter))) (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)))) ;;;;;; Select modules ;; Choose a module. (defun bible-pick-text () "Keymap action function---select text that the user chooses." (interactive) (let ((item (get-text-property (point) 'module))) (bible-open nil bible--current-book-name bible--current-chapter 1 item))) (defun bible-pick-commentary (&optional module) "Keymap action function---select commentary that the user chooses. Use optional argument MODULE as commentary if given." (interactive) (let ((item (or module (get-text-property (point) 'module)))) (setq-local bible-commentary item) (commentary-open item bible--current-book-name bible--current-chapter 1))) (defun bible-select-text () "Ask user for a new text module for the current `bible' buffer." (interactive) (let ((text (completing-read "Text: " bible--texts))) (unless (string= text "") (setq-default bible-text text) (setq-local bible-text text) (bible--display)))) (defun bible-select-commentary () "Ask user for a new commentary module for the current `bible' buffer." (interactive) (let ((commentary (completing-read "Commentary: " bible--commentaries))) (unless (string= commentary "") (setq-local bible-commentary commentary) (bible-pick-commentary bible-commentary)))) ;;;;;; Toggles (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)) (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)) (bible--display)) (defun bible-toggle-buffer-sync () "Either add or remove the current buffer from the `bible--synced-buffers' list." (interactive) (let ((buffer (current-buffer))) (if bible--synced-p (progn (setq bible--synced-buffers (cl-delete buffer bible--synced-buffers)) (setq-local bible--synced-p nil)) (cl-pushnew buffer bible--synced-buffers) (setq-local bible--synced-p t)) (force-mode-line-update))) (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 nil bible--current-book-name bible--current-chapter 1 bible-text)) ;;;;;; Search helpers (defun bible-set-search-range () "Ask user for a new text module for the current `bible' buffer." (interactive) (let ((range (read-string "Range ( to clear): "))) (if (string-equal range "") (setq bible-search-range nil) (setq bible-search-range range)))) (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 ((searchmode (completing-read "Search Mode: " '("lucene" "phrase" "regex" "multiword") nil t "lucene"))) (bible--open-search query searchmode (buffer-local-value 'bible-text (current-buffer)))))) (defun bible-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) (when (string-match bible--verse-regexp text) (setq text (match-string 0 text)) (string-match "I?I?I? ?[A-Z]?[a-z]* " text) (setq book (match-string 0 text)) (string-match "[0-9]?[0-9]?[0-9]?:" text) (setq chapter (substring (match-string 0 text) 0 (1- (length (match-string 0 text))))) (string-match ":[0-9]?[0-9]?[0-9]?" text) (setq verse (substring (match-string 0 text) 1)) (bible-open associated-buffer (string-trim book) (string-to-number chapter) (string-to-number verse) bible-text)))) (defun bible-follow-xref () "Follow the hovered verse in a bible term buffer. Create a new `bible' buffer positioned at the specified verse. Handle abbreviations." (interactive) (let ((xref (get-text-property (point) 'xref))) (when xref (let ((verse-ref (split-string xref)) book-abbrev chapter-verse) (cond ((= (length verse-ref) 2) ; Mat 5 or the like (setq book-abbrev (car verse-ref) chapter-verse (split-string (cadr verse-ref) ":"))) ((= (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) ":")))) ;; Use book abbreviation if present or try whatever is in verse-ref. (let ((book (or (alist-get book-abbrev bible--book-name-abbreviations nil nil #'string-equal-ignore-case) (car verse-ref))) (chapter (car chapter-verse)) (verse (cadr chapter-verse))) (bible-open associated-buffer (string-trim book) (string-to-number chapter) (string-to-number verse) (default-value 'bible-text))))))) ;;;;;; User visible actions. ;; These can be called interactively if you know the Strong's number ;; you want to look up. (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)) ;; Interactively insert a verse into an arbitrary current buffer. (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: "))) (query (concat (car book-data) " " chapter ":" verse)) (args (list bible-sword-query nil (current-buffer) t "-b" bible-text "-f" "plain" "-k" query))) (apply #'call-process args))) ;;;;;; Support (internal) ;;;;;;; Diatheke interface (defun bible--exec-diatheke (query &optional filter format module) "Execute `diatheke' with specified QUERY options. FILTER is the Diatheke filter argument. FORMAT is either plain or the default of internal. MODULE is the text module to use. Returns string containing query result." (let ((module (or module bible-text))) (with-temp-buffer (let ((args (list bible-sword-query 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' on QUERY with SEARCHTYPE. 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 bible-sword-query nil (current-buffer) t "-b" (or module bible-text)))) (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))) ;; 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 bible-sword-query 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) (1+ (length query)))))) ; 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." (bible--exec-diatheke query nil "plain" module)) ;;;;;; Lexicon processing ;; 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: ... We convert them to the : format." (goto-char (point-min)) (while (re-search-forward "" 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). (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--cleanup-lex-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) (defun bible--lookup-def-greek (key) "Execute `diatheke' to do query on KEY. Massage output so verse cross references are usable. Returns string." (with-temp-buffer (let ((args (list bible-sword-query nil (current-buffer) t "-b" bible-greek-lexicon "-o" "m" "-f" "plain" "-k" key))) (when bible-show-diatheke-exec (message "%s" args)) (apply #'call-process args) (bible--cleanup-lex-text (bible--remove-module-name bible-greek-lexicon (buffer-string)))))) (defun bible--lookup-lemma-index (key) "Return the Greek lemma from lemma index with a strong's number as KEY." (string-trim (bible--remove-module-name bible-lexicon-index (bible--lex-query key bible-lexicon-index)))) (defun bible--lookup-lemma-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-def-greek lemma))))) (defun bible--lookup-lemma-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-hash-greek) (puthash key (if bible-use-index-for-lexicon (bible--lookup-lemma-greek-indexed key) (bible--lookup-def-greek key)) bible-hash-greek))) (defun bible--lookup-def-hebrew (key) "Execute `diatheke' to do query on KEY. Massage output so various cross references are usable. Returns string." (with-temp-buffer (let ((args (list bible-sword-query nil (current-buffer) t "-b" bible-hebrew-lexicon "-f" "plain" "-k" key))) (when bible-show-diatheke-exec (message "%s" args)) (apply #'call-process args) (bible--process-href) (concat (string ?\x200e) (bible--remove-module-name bible-hebrew-lexicon (substring (buffer-string) 7)))))) (defun bible--lookup-lemma-hebrew (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-hash-hebrew) (puthash key (bible--lookup-def-hebrew key) bible-hash-hebrew))) ;; 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-lemma-short (lemma lexicon) "Look up lexical entry for LEMMA in short LEXICON. Returns a string that is intended to be displayed in a tooltip. Uses short lexicon (e.g. StrongsRealHebrew or StrongsRealGreek)." (when (string-match "[0-9]+" lemma) (bible--remove-module-name lexicon ;; Get rid of unnecessary strongs codes at the beginning. (replace-regexp-in-string ".*[0-9]+ [0-9]+ " "" (bible--lex-query (concat (match-string 0 lemma)) lexicon))))) (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 (substring 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) (bible--lookup-lemma-short key bible-greek-lexicon-short)) ((string-prefix-p "H" key) (concat (string ?\x200e) (bible--lookup-lemma-short key bible-hebrew-lexicon-short))))) (puthash key (string-fill (bible--cleanup-lex-text lex-text) 75) lex-hash))))) (defun bible--lookup-morph-entry (morph) "Look up entry for 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) ;; We know about these modules. (Assume they're installed.) (cond ((string-prefix-p "robinson:" morph) (setq morph-module "Robinson") (setq morph-key (substring morph (length "robinson:")))) ((string-prefix-p "packard:" morph) (setq morph-module "Packard") (setq morph-key (substring morph (length "packard:")))) ((string-prefix-p "oshm:" morph) (setq morph-module "OSHM") (setq morph-key (substring morph (length "oshm:"))))) (bible--remove-module-name morph-module (bible--morph-query morph-key morph-module))) morph-hash)))) ;; 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 (get-text-property pos 'strong object)) (lex-text (bible--lookup-lex lex)) (morph (get-text-property pos 'morph object)) (morph-text (bible--lookup-morph-entry morph))) (when lex-text ;; This removes backslashes to prevent bogus command ;; substitutions (that is, Emacs mistakenly filling in a key ;; binding for some command---see Info doc on Substituting Key ;; Bindings) in the tooltip. ;; REVIEW: I couldn't figure out a better way to bypass command ;; substitution in the tooltips. (FMG 5-Mar-2026) (subst-char-in-string ?\\ ? (if morph-text (concat (string-trim lex-text) "\n" (string-trim morph-text)) (string-trim lex-text)))))) ;;;; Display module text (defun bible-handle-divine-name (item) "When ITEM is divine name, display it as such." (let ((start (point)) (strongs (dom-attr item 'savlm))) (insert "LORD") (let ((end (point))) (add-face-text-property start end 'bold) (put-text-property start end 'keymap bible-hebrew-keymap) (when (and strongs (string-match "strong:H" strongs)) (put-text-property start end 'help-echo 'bible--show-lex-morph) (put-text-property start end 'strong (match-string 0 strongs)))))) (defun bible--process-word (item iproperties) "Handle fubar tag in ITEM. Check IPROPERTIES for qualifiers. Add tooltips for definitions and morphology. Also insert lemmas in buffer if `word study' is turned on (must be done after item is inserted in buffer)." (let ((word (string-trim (bible-dom-text item))) (morph (dom-attr item 'morph)) (savlm (dom-attr item 'savlm)) (lemma (dom-attr item 'lemma)) (divinename (dom-by-tag item 'divinename))) (let ((start (point)) (end (+ (point) (length word)))) (insert word) ;; REVIEW: Special case this. Some modules do this differently. ;; (FMG 5-Mar-2026) (when divinename (just-one-space) (bible-handle-divine-name item) (just-one-space)) ;; Red letter. (when (plist-get iproperties 'jesus) (add-face-text-property start end '(:foreground "red"))) ;; lexical definitions ;; N.B. There are some severe issues with Strongs numbers in some modules. (when (or savlm lemma) (let* ((matched nil) (lexemes (split-string (or savlm lemma))) (lexeme ;; HACK: Kludge alert. KJV module conflates Greek ;; articles with nouns. Deal with this. ;; (FMG 5-Mar-2026) (let ((lexeme-list (if (string= bible-text "KJV") (reverse lexemes) ; Use the last `strong:' entry. lexemes))) (catch 'loop (dolist (item lexeme-list) (when (string-prefix-p "strong:" item) (throw 'loop item))))))) (when lexeme (cond ((string-match "strong:G.*" lexeme) ; Greek (setq matched (match-string 0 lexeme)) (put-text-property start end 'keymap bible-greek-keymap)) ((string-match "strong:H.*" lexeme) ; Hebrew (setq matched (match-string 0 lexeme)) (put-text-property start end 'keymap bible-hebrew-keymap))) ;; Add help-echo, strongs reference for tooltips if match. (when matched (setq bible-has-lexemes " Lex") (put-text-property start end 'help-echo 'bible--show-lex-morph) (put-text-property start end 'strong matched)))) ;; morphology (when morph (let* ((matched nil) (morphemes (split-string morph)) (morpheme (car (last morphemes)))) ; KJV kludge as above (if (or (string-match "robinson:.*" morpheme) ; Robinson Greek morphology (string-match "packard:.*" morpheme) ; Packard Greek morphology --- LXX seems to use this (string-match "oshm:.*" morpheme)) ; OSHM Hebrew morphology (setq matched (match-string 0 morpheme))) (when matched (setq bible-has-morphemes " Morph") (put-text-property start end 'morph matched) (put-text-property start end 'help-echo 'bible--show-lex-morph)))) ;; Insert lemma into buffer. Lemma tag will be part of lemma/savelm item. ;; TODO: Should I enable lexicon lookups on these lemmas? I ;; don't use this anyway.... (FMG 5-Mar-2026) (when (and bible-word-study-enabled lemma (string-match "lemma.*:.*" lemma)) (dolist (word (split-string (match-string 0 lemma) " ")) (setq word (replace-regexp-in-string "[.:a-zA-Z0-9]+" "" word)) (just-one-space) (let ((refstart (point))) (insert word) (add-face-text-property refstart (point) '(:foreground "blue")) (put-text-property refstart (point) 'keymap bible-lemma-keymap)))))))) (defun bible--insert-title (title-node) "Insert the text in TITLE-NODE into buffer as a chapter title. Since each verse will have a `title' tag, keep track and only emit a title when the new title in `title-node' is different from the one stored in `bible-chapter-title'." (unless (equal bible-chapter-title title-node) (setq-local bible-chapter-title title-node) (let ((title-text (replace-regexp-in-string ; Clear out XML. "<.*?>" "" (bible-dom-texts bible-chapter-title))) (start (point))) (bible-new-line) ;; Insert the LRM character to make the text render left-to-right. ;; This is necessary in the KJV module when displaying psalm 119. (insert (string ?\x200e) title-text) (put-text-property start (point) 'face 'bold) (newline) (delete-blank-lines)))) ;; These tags appear in ESV modules (and maybe others?) ;; REVIEW: Is this right? (FMG 5-Mar-2026) (defun bible--level-tag (node) "Indent or break line as dictated by NODE." (let ((type (dom-attr node 'type)) (level (dom-attr node 'level))) (cond ((and type (string-equal-ignore-case type "x-br")) (newline)) ((and type (string-equal-ignore-case type "x-indent")) (insert "\t")) ;; REVIEW: Some modules use `level' tag but ;; not in a consistent way. (FMG 7-Mar-2026) ((equal level "1") (just-one-space)) ((equal level "2") (newline) (delete-blank-lines))))) (defvar-local bible-current-xref-book nil) (defvar-local bible-current-xref-chapter nil) (defun bible--insert-xref (node) "Insert a cross reference specified by NODE. This format is used by the NETnote module." ;; HACK: What a mess! There are still some broken edge cases! (FMG 29-Mar-2026) (let* ((refs-text (bible-dom-text node)) (refs (split-string refs-text "," t " "))) (dolist (ref refs) (let ((a-ref (split-string ref ";" t " "))) (dolist (b-ref a-ref) ;; b-ref is an individual reference. ;; At this point b-ref will, we hope, look like one of the following: ;; : (or maybe ) ;; :verse ;; (or maybe -) ;; Books may look like this: <1 Cor> or so we have to deal with the possible space. ;; We ignore verse ranges and hope for the best (it seems to do the right thing). (let ((set-chapter-p nil)) (when (string-match ".* " b-ref) (setq-local bible-current-xref-book (match-string 0 b-ref))) (when (string-match "[0-9]*:" b-ref) (setq-local bible-current-xref-chapter (string-trim (substring (match-string 0 b-ref) 0 (1- (length (match-string 0 b-ref)))))) (setq set-chapter-p t)) (let* ((match (string-match "[0-9]*" b-ref (if set-chapter-p (match-end 0) 0))) (verse (string-trim (substring b-ref match (match-end 0))))) (just-one-space) (let ((start (point)) (the-ref (concat bible-current-xref-book bible-current-xref-chapter))) (when verse (setq the-ref (concat the-ref ":" verse))) (insert b-ref) (put-text-property start (point) 'xref the-ref) (put-text-property start (point) 'keymap bible-term-mode-map) (put-text-property start (point) 'help-echo (concat "Go to " the-ref)) (add-face-text-property start (point) '(:foreground "blue")))))))))) (defun bible--insert-osis-xref (node) "Insert a cross reference specified by NODE. The node should have the `osisref' attribute." (let* ((ref-text (bible-dom-text node)) (ref-ref (dom-attr node 'osisref)) (ref-split (split-string ref-ref "[.]" t " ")) (ref-book (cl-first ref-split)) (ref-chapter (cl-second ref-split)) (ref-verse (cl-third ref-split)) (the-ref (concat ref-book " " ref-chapter ":" ref-verse))) (just-one-space) (let ((start (point))) (insert ref-text) (put-text-property start (point) 'xref the-ref) (put-text-property start (point) 'keymap bible-term-mode-map) (put-text-property start (point) 'help-echo (concat "Go to " the-ref)) (add-face-text-property start (point) '(:foreground "blue"))))) (defun bible--insert-domnode-recursive (node &optional iproperties) "Recursively parse domnode NODE obtained from `libxml-parse-html-region'. Inserts resulting text into active buffer with properties specified in IPROPERTIES. 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) ;; 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 ;; TODO: There are lots of tags we don't handle, especially in commentaries. ;; Maybe process these at some point? Include footnotes etc. ;; (FMG 5-Mar-2026) ;; 'w is usual case. ('w (insert " ") (bible--process-word subnode iproperties)) ('title ;; This mess is to deal with the possibility that the ;; title might change in the middle of the chapter. I'm ;; talking about YOU, Psalm 119. (if bible-chapter-title (bible--insert-title subnode) ; Middle of chapter. (save-excursion ; Beginning of chapter. (goto-char (point-min)) (bible--insert-title subnode)))) ;; Font tag ignored for now, treat as if 'w. ('font (insert " ") (bible--process-word subnode iproperties)) ('hi (when (equal (dom-attr subnode 'type) "bold") (just-one-space) (let ((word (bible-dom-text subnode)) (start (point))) (insert word) (put-text-property start (point) 'face 'bold)))) ;; Italic face (special case for certain module) ('i (just-one-space) (let ((word (bible-dom-text subnode)) (start (point))) (insert word) (put-text-property start (point) 'face 'bold) (add-face-text-property start (point) '(:foreground "orange")))) ;; 'q is used for red letter. ;; NASB Module uses 'seg to indicate OT quotations (and others?). ((or 'body 'seg 'p 'q) (bible--insert-domnode-recursive subnode iproperties)) ('l (bible--level-tag subnode)) ;; REVIEW: divine name handling doesn't seem to work the same ;; with all modules. (FMG 26-Mar-2026) ('divinename (bible-handle-divine-name subnode)) ;; Some modules use this for line breaks and such. ('milestone (pcase (dom-attr subnode 'type) ("line" (bible-new-line)) ;; ("x-PN" (bible-new-line)) ; REVIEW: Don't yet understand this one. (FMG 26-Mar-2026) ("x-p" (insert (dom-attr subnode 'marker) " ")))) ('br (bible-new-line)) ('lb (when (equal (dom-attr subnode 'type) "x-begin-paragraph") (bible-new-line))) ('div (when (or (equal (dom-attr subnode 'type) "paragraph") (equal (dom-attr subnode 'type) "x-p")) (bible-new-line))) ;; For commentaries and the like. ;; This is used by the NETnote module. ('scripref (bible--insert-xref subnode)) ;; This is used by many commentaries. ('reference (bible--insert-osis-xref subnode)) ;; Various text properties---ignore for now. REVIEW: (FMG 26-Mar-2026) ((or 'b 'u) (bible--insert-domnode-recursive subnode iproperties)) ;; Word inserted by translation, not in original, give visual indication. ('transchange (insert " ") (let ((word (bible-dom-text subnode)) (start (point)) (face (if (plist-get iproperties 'jesus) '(:foreground "salmon") '(:foreground "gray50")))) (insert word) (add-face-text-property start (point) face))))))))) (defun bible--display (&optional verse) "Render a page (chapter) of a Bible module. Defaults to using `bible-text'. If optional argument VERSE is supplied, set cursor at verse." (let ((buffer-read-only nil) (bible-has-lexemes nil) (bible-has-morphemes 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 ; If this is true, display the XML. (erase-buffer) (setq-local bible-chapter-title nil) ;; Looking for the "body" tag in the DOM node. (bible--insert-domnode-recursive (dom-by-tag html-dom-tree 'body)) (goto-char (point-min)))) (save-excursion (let ((search-string (concat " *" (car bible--current-book) " " (number-to-string bible--current-chapter) ":"))) ;; Delete at beginning of verse, just leave verse number. (while (re-search-forward search-string nil t) (replace-match "") (bible-new-line) ;; Highlight verse number (when (re-search-forward " *[0-9]+:" nil t 1) (add-face-text-property (match-beginning 0) (match-end 0) '(:foreground "purple")))))) (save-excursion ;; Fix divine name lossage. (while (re-search-forward "Lord LORD" nil t) (replace-match "LORD") (add-face-text-property (point) (- (point) 4) 'bold)) (while (re-search-forward "Lord.+s LORD" nil t -1) (replace-match "LORD's") (add-face-text-property (1- (point)) (- (point) 5) 'bold)) ;; Remove the module name from the buffer. (while (re-search-forward (concat "^.*" bible-text ".*$") nil t) (replace-match "")) (delete-blank-lines)) (save-excursion (format-replace-strings '(("." . ". ") ("," . ", ") (";" . "; ") (":" . ": ") ("?" . "? ") ("!" . "! ") (" ." . ". ") (" ," . ", ") (" ;" . "; ") (" :" . ": ") (" ?" . "? ") (" !" . "! ") ("“ " . "“") ("‘ " . "‘") (" ’" . "’") (". ”" . ".”") ("? ”" . "?”")) nil (point-min) (point-max))) ;; Get rid of multiple consecutive spaces. (save-excursion (while (re-search-forward " *" nil t) ; More than one space in a row (just-one-space))) ;; Set the mode line of the biffer. (if bible-has-lexemes (unless (string-match " Lex" mode-name) (setq mode-name (concat mode-name " Lex"))) (setq mode-name (replace-regexp-in-string " Lex" "" mode-name))) (if bible-has-morphemes (unless (string-match " Morph" mode-name) (setq mode-name (concat mode-name " Morph"))) (setq mode-name (replace-regexp-in-string " Morph" "" mode-name))) (force-mode-line-update)) ;; If optional verse specification go to that verse. (when verse (re-search-forward (concat " ?" (number-to-string verse)) nil t))) ;;;; Modules (Bible texts, commentaries) (defun compare-module-names (n1 n2) "Compare N1 and N2, ignoring case, using collation order." (string-collate-lessp n1 n2 nil t)) (defun bible--get-biblical-modules () "Populate `bible--texts' and `bible--commentaries' lists." (let ((lines (split-string (bible--exec-diatheke "modulelist" nil "plain" "system") "[\n\r]+")) (texts nil) (commentaries nil) (doing-texts nil) (doing-commentaries nil)) (setq bible--texts nil) (setq bible--commentaries nil) (catch 'done (dolist (line lines) (when doing-texts (push (split-string line " : ") texts)) (when doing-commentaries (push (split-string line " : ") commentaries)) (when (string-equal line "Biblical Texts:") (setq doing-texts t)) (when (string-equal line "Commentaries:") (setq doing-texts nil) (pop texts) ; Remove `Commentaries:' line from `bible--texts'. (setq doing-commentaries t)) (when (string-equal line "Lexicons / Dictionaries:") (pop commentaries) ; Remove `Lexicons / Dictionaries:' line ; from bible--commentaries. (throw 'done nil)))) (setq bible--texts (cl-sort texts #'compare-module-names :key #'car)) (setq bible--commentaries (cl-sort commentaries #'compare-module-names :key #'car))) nil) (defun bible--list-biblical-texts () "Return a list of accessible Biblical Text modules." (bible--get-biblical-modules) ; Make sure the lists are fresh. bible--texts) (defun bible--list-biblical-commentaries () "Return a list of accessible Biblical Text modules." (bible--get-biblical-modules) ; Make sure the lists are fresh. bible--commentaries) (defun bible-display-available-texts () "Display available modules, allow user to select." (interactive) (bible--get-biblical-modules) ; Make sure lists are fresh. (with-current-buffer (get-buffer-create "Texts") (bible-text-select-mode) (let ((buffer-read-only nil)) (erase-buffer) (setq-local tab-stop-list '(25)) (dolist (text bible--texts) (let ((name (string-trim (car text))) (description (string-trim-left (cadr text)))) (insert (propertize (string-trim name) 'face 'bold 'module name 'help-echo (concat "Select " name) 'keymap bible-text-map)) (move-to-tab-stop) (insert (format "%s\n" description))))) (goto-char (point-min)) (pop-to-buffer (current-buffer) nil t))) (defun bible-display-available-commentaries () "Display available modules, allow user to select." (interactive) (bible--get-biblical-modules) ; Make sure lists are fresh. (with-current-buffer (get-buffer-create "Commentaries") (bible-text-select-mode) (let ((buffer-read-only nil)) (erase-buffer) (setq-local tab-stop-list '(25)) (dolist (commentary bible--commentaries) (let ((name (string-trim (car commentary))) (description (string-trim-left (cadr commentary)))) (insert (propertize (string-trim name) 'face 'bold 'module name 'help-echo (concat "Select " name) 'keymap bible-commentary-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 module) "Open a search buffer of QUERY using SEARCHMODE in module MODULE." (let ((results (string-trim (replace-regexp-in-string "Entries .+?--" "" (bible--diatheke-search query searchmode "plain" module))))) (if (equal results (concat "none (" module ")")) (message (concat "No results found." (when (equal searchmode "lucene") " Verify index has been build with mkfastmod."))) (with-current-buffer (get-buffer-create (generate-new-buffer-name (concat "*bible-search*"))) (bible-search-mode) (bible--display-search results module) (setq-local bible-search-word-this-query query bible-search-text-this-query module bible-search-range-this-query bible-search-range) (pop-to-buffer (current-buffer) nil t))))) (defun bible--display-search (results module) "Render RESULTS of search query with MODULE." (let ((match 0) (matchstr "") (verses nil) (query-verses "") (buffer-read-only nil)) ;; (message "display-search %s" module) (setq-default bible-text module) (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 ".+; " "" (string-replace "I " "1" (string-replace "II " "2" (string-replace "III " "3" matchstr)))) verses))) (setq verses (cl-sort verses #'string-version-lessp)) (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 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))) (goto-char (point-min)) (save-excursion ;; Remove module name from buffer. (while (re-search-forward (concat "^.*" module ".*$") nil t) (replace-match "")) (delete-blank-lines)) (setq mode-name "Bible Search ") (setq-local bible-search-matches (length verses)))) ;;;; Terms (lemmas, morphemes) (defun bible--get-lemma (language strongs) "Get the lemma from lexicon for LANGUAGE for strong's term STRONGS. Used to display lemmas in mode lines. Assumes that StrongsHebrew and StrongsGreek lexicons have been installed." (let ((lemma-entry (pcase language ('hebrew ;; Use Strong's Hebrew lexicon to look up Hebrew lemma. (bible--lex-query strongs "StrongsHebrew" )) ('greek ;; Use Strong's Greek lexicon to look up Greek lemma. (bible--lex-query strongs "StrongsGreek"))))) (unless (equal lemma-entry "") ;; Entry will look like : . . Get ;; rid of everything before and after . (let* ((lemma-line (split-string lemma-entry)) (lemma (caddr lemma-line))) lemma)))) (defun bible--display-greek () "Display Greek term. 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--open-term-greek (replace-regexp-in-string "strong:G" "" item)))) (defun bible--display-hebrew () "Display Hebrew term. 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--open-term-hebrew (replace-regexp-in-string "strong:H" "" item)))) ;;(defun bible-display-morphology (morph) ;; ;; REVIEW: Do something here? (FMG 5-Mar-2026) ;; ) (defun bible--fixup-lexicon-display (_termtype) "Fixup the display of a lexical entry whose language is given by TERMTYPE." (let ((buffer-read-only nil)) (goto-char (point-min)) ;; This enables clicking on verse references. (save-excursion (while (search-forward-regexp bible--verse-regexp nil t) (let ((match (match-string 0)) (start (match-beginning 0)) (end (match-end 0))) (put-text-property start end 'xref match) (put-text-property start end 'keymap bible-term-mode-map) (put-text-property start end 'help-echo (concat "Go to " (substring-no-properties match))) (add-face-text-property start end '(:foreground "blue"))))) (save-excursion (while (search-forward "()" nil t) (replace-match "")) (delete-blank-lines)))) (defun bible--open-term-hebrew (term) "Open a buffer of the Strong's Hebrew TERM's definition." (with-current-buffer (get-buffer-create (generate-new-buffer-name "*bible-term*")) (bible-term-hebrew-mode) (setq-local bidi-paragraph-direction 'left-to-right) (setq-local mode-name (concat (bible--get-lemma 'hebrew term) " Term (Hebrew)")) (bible--display-lemma-hebrew term) (pop-to-buffer (current-buffer) nil t) (fit-window-to-buffer))) (defun bible--display-lemma-hebrew (lemma) "Render the definition of the Strong's Hebrew LEMMA. This code is customized for the BDBGlosses_Strongs lexicon." (let ((buffer-read-only nil)) (erase-buffer) ;; BDBGlosses_Strongs needs the prefixed `H'. (insert (substring (bible--cleanup-lex-text (bible--lookup-lemma-hebrew (concat "H" lemma))) 7)) (bible--fixup-lexicon-display 'hebrew))) (defun bible--open-term-greek (term) "Open a buffer of the Strong's Greek TERM definition." (with-current-buffer (get-buffer-create (generate-new-buffer-name "*bible-term*")) (bible-term-greek-mode) (setq-local mode-name (concat (bible--get-lemma 'greek term) " Term (Greek)")) (bible--display-lemma-greek term) (pop-to-buffer (current-buffer) nil t) (fit-window-to-buffer))) (defun bible--display-lemma-greek (lemma) "Render the definition of the Strong's Greek LEMMA." (let ((buffer-read-only nil)) (erase-buffer) (insert (bible--lookup-lemma-greek lemma)) (bible--fixup-lexicon-display 'greek))) ;;;; Utilities (defun bible-new-line () "Ensure beginning of line. Try to avoid redundant blank lines." (unless (bolp) (newline))) (defun bible--remove-module-name (module-name string) "Remove parenthesized MODULE-NAME from STRING. Also deals with bug where some versions of diatheke return string that is missing close parenthesis." (replace-regexp-in-string (concat "^(" module-name ".*$") "" string)) (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.