(setq ffap-rfc-directories (list (expand-file-name "~/src/rfc")))

(setq irfc-directory (expand-file-name "~/src/rfc"))

(defcustom irfc-file-regexp
  (rx "rfc" (one-or-more numeric) ".txt")
  "Regexp matching RFC file."
  :group 'irfc
  :type 'regexp)

(add-to-list 'auto-mode-alist `(,irfc-file-regexp . irfc-mode))

(defvar irfc-mode-map
  (let ((map (make-sparse-keymap)))
    (define-key map (kbd "h") 'backward-char)
    (define-key map (kbd "l") 'forward-char)
    (define-key map (kbd "e") 'scroll-down)
    (define-key map (kbd "SPC") 'scroll-up)
    (define-key map (kbd "J") 'irfc-scroll-up-one-line)
    (define-key map (kbd "K") 'irfc-scroll-down-one-line)
    (define-key map (kbd ",") 'end-of-buffer)
    (define-key map (kbd ".") 'beginning-of-buffer)
    (define-key map (kbd "T") 'irfc-render-toggle)
    (define-key map (kbd "q") 'irfc-quit)
    (define-key map (kbd "o") 'irfc-follow)
    (define-key map (kbd "v") 'irfc-visit)
    (define-key map (kbd "r") 'irfc-reference-goto)
    (define-key map (kbd "g") 'irfc-head-goto)
    (define-key map (kbd "F") 'irfc-head-number-goto)
    (define-key map (kbd "G") 'irfc-page-goto)
    (define-key map (kbd "N") 'irfc-page-next)
    (define-key map (kbd "P") 'irfc-page-prev)
    (define-key map (kbd ">") 'irfc-page-last)
    (define-key map (kbd "<") 'irfc-page-first)
    (define-key map (kbd "b") 'irfc-page-table)
    (define-key map (kbd "n") '(lambda ()
                                 (interactive)
                                 (irfc-head-next)
                                 (recenter-top-bottom 0)))
    (define-key map (kbd "p") '(lambda ()
                                 (interactive)
                                 (irfc-head-prev)
                                 (recenter-top-bottom 0)))
    (define-key map (kbd "G") 'irfc-table-jump)
    (define-key map (kbd "<tab>") 'irfc-rfc-link-next)
    (define-key map (kbd "<backtab>") 'irfc-rfc-link-prev)
    (define-key map (kbd "<f7>")
      (lambda ()
        (interactive)
        (call-interactively 'next-line)
        (call-interactively 'other-window)
        (call-interactively '(lambda nil
                               (interactive)
                               (irfc-head-next)
                               (recenter-top-bottom 0)))
        (call-interactively 'other-window)))
    (define-key map (kbd "<f8>")
      (lambda ()
        (interactive)
        (call-interactively 'previous-line)
        (call-interactively 'other-window)
        (call-interactively '(lambda nil
                               (interactive)
                               (irfc-head-prev)
                               (recenter-top-bottom 0)))
        (call-interactively 'other-window)))
    ;; (define-key map (kbd "<f8>") 'show-translation)
    map)
  "Keymap used by `irfc-mode'.")
