; Some minor mods to mh mode, mostly key bindings and the like.
; My-mh-rmail is just mh-rmail, with all the ESC-prefixed bindings also bound
; to be ^X-prefixed, plus some other rebindings.  I originally thought to put
; the ESC-prefixed bindings onto the ` prefix, to be consistent with how ` is
; used as the pain-in-the- you-know-what escape prefix.  However, since
; mh-rmail-modemap rebinds ESC-n and ESC-p, which annoyingly interfere next-
; and prev-window, I put the mh-rmail-mode-map ESC bindings on ^X.  Another
; solution would be to get rid of the ESC-n,p (cum ^X-n,p) bindings, but I
; chose not to for now.
;
; Against my better judgement, I went ahead and figured out how to do things
; right with the keymap.  It's just like the aset used in pain-in-the-you-know-
; what-term defun, but here we're plugging in a whole keymap for the ^X entry,
; where the ASCII value, and hence keymap array position, of ^X is 24.  The
; keymap we plug in is extracted from the ESC (= 27th) entry of the
; mh-folder-mode-map.
;
; Two discoveries were made in the course of this time-wasting (though
; emacswise productive) exercise.  (1) In order to have ` act like the
; ESC-prefix in custom mode maps, we'll have to do this kind of thing for each
; mode map that does custom bindings for ESC, since they won't do custom
; bindings for `.  I'm not exactly clear on all of what globally binding ` to
; ESC-prefix does, since it works globally, but not within custom mode maps.
; Hmm.  (2) I discovered that aref is the function for indexing an array; it's
; not exactly my first guess for the name of this function :(.
;
(defun my-mh-rmail ()
"Call mh-rmail and do some key rebindings.  See the function definition for
further information."
    (interactive)
    (mh-rmail)
    (let ((km (cadr mh-folder-mode-map)))

        ; The next line copies the ESC keymap into the the keymap entry for ^X.
        (aset km 24 (copy-alist (aref km 27)))
	;		     ^^
        ;                    ^^ ESC 
        ;           ^^^^^^^^^^^^
        ;           ^^^^^^^^^^^^ ESC-prefix keymap, which from pp of mh-folder
	;                        -mode-map is the following:
	;			   (keymap
	;			    (62 . mh-last-msg)
	;			    (60 . mh-first-msg)
	;			    (114 . mh-rescan-folder)
	;			    (115 . mh-search-folder)
	;			    (113 . mh-list-sequences)
	;			    (112 . mh-pack-folder)
	;			    (110 . mh-store-msg)
	;			    (108 . mh-list-folders)
	;			    (107 . mh-kill-folder)
	;			    (102 . mh-visit-folder)
	;			    (101 . mh-extract-rejected-mail)
	;			    (100 . mh-redistribute)
	;			    (127 . mh-page-digest-backwards)
	;			    (32 . mh-page-digest)
	;			    (117 . mh-undo-folder)
	;			    (98 . mh-burst-digest)
	;			    (35 . mh-delete-seq)
	;			    (37 . mh-delete-msg-from-seq)
	;			    (97 . mh-edit-again))

	(let ((ctrl-x-km (aref km 24)))
	    ; Put switch-to-buffer badk on ^X-b, getting rid of the obscure (at
	    ; least to me) mh-burst-digest.
	    (rplacd (assoc ?b ctrl-x-km) 'switch-to-buffer)

	    ; Leave mh-send on just 'm', not both 'm' and 's'.  Put
	    ; my-refile-msg on 's', to be compat with shell-level of mh UI.
	    (aset km ?s 'mh-refile-msg)

	    ; For further compat with shell-level UI, bind '+' and '-'
	    ; accordingly.
	    (aset km ?+ 'mh-next-undeleted-msg)
	    (aset km ?- 'mh-previous-undeleted-msg)

	    ; Put mh-show on 'p'; leave 'n', since that's the way it is in the
	    ; shell ui.
	    ; LET'S NOT FOR HOW TO SEE HOW IT FEELS.
	    ;(aset km ?p 'mh-show)

	    ; Put mh-visit-folder, which is the most scan-like command in the
	    ; emacs interface, on 'h'.
	    (aset km ?h 'mh-visit-folder)

	    ; Put mh-narrow-to-seq and widen-seq on ^X-n and ^X-w, resp.
	    (rplacd (assoc ?n ctrl-x-km) 'mh-narrow-to-seq)	; ^X-n 
	    (nconc ctrl-x-km (list (cons ?w 'mh-widen)))
	)
    )

    ; Do this after copying the ESC keymap so that the ^X keymap will keep
    ; stuff that restore-window-nav-keys unbinds, such as ESC-p, cum ^X-p,
    ; which we want to stay bound to mh-pack-folder in the ^X keymap.
    (restore-window-nav-keys)

    ; For some funky reason, I'm losing the mh-pack-folder binding on ^X-p, as
    ; well as my-search-folder on ^Xs.  Let's just put them back manually.
    (local-set-key "p" 'mh-pack-folder)
    (local-set-key "s" 'mh-search-folder)
    (local-set-key "S" 'mh-sort-folder)
)

(defun mh-letter-mode-hook ()
"Unbind ESC-^C in mn-letter-mode since I'm too used to it as the way to leave
an mh message edit session."
    (interactive)
    (local-unset-key "")
    (local-unset-key "`")
)