;;; Simple lookup-by-narrowing

;;; You can create a stand-alone dictionary by a script:
;;; #!/bin/bash
;;; emacs -Q -nw -l kamusi.el --eval "(kamusi t)"

;;; Or autoload it in your .emacs file:
;;; (autoload 'kamusi "kamusi" "Swahili Dictionary" t)

(defvar kamusi-dictionary "/path/to/your/sall.txt"
  "*Full path to the swahili dictionary.")

(defun kamusi-lookup (str)
  (interactive "sSwahili word: ")
  (find-file kamusi-dictionary)
  (widen)
  (goto-char (point-min))
  (let ((word (format "\\[Swahili Word\\] %s$" str))
        (line "-\\{60\\}"))
    (search-forward-regexp word)
    (search-backward-regexp line)
    (let ((start (point)))
      (goto-char (point-max))
      (search-backward-regexp word)
      (search-forward-regexp line)
      (narrow-to-region start (point))
      (goto-char (point-min)))))

(defun kamusi (&optional standalonep)
  (interactive)
  (find-file-noselect kamusi-dictionary t)
  (find-file kamusi-dictionary)
  (toggle-read-only 1)
  (use-local-map (make-sparse-keymap))
  (local-set-key (kbd "s") 'kamusi-lookup)
  (when standalonep
    (local-set-key (kbd "q") 'save-buffers-kill-emacs)))