  ;; me2.mut - The ME2 initialization file
  ;; To compile this file: "mc2 -tme2 me2"
  ;; C Durland	Public Domain

(include me2.h)

(defun
  insert-space  { (insert-text " ") (previous-character) }
  center-cursor { (arg-prefix 0)(reposition-window) }
  CR->CR&indent
  {
    (bind-local-key "newline-and-indent" "^M")
    (msg "CR mapped to newline-and-indent")
  }
  CR->CR  { (bind-local-key "newline" "C-M") (msg "CR mapped to newline") }
  cut-the-line	{ (beginning-of-line)(arg-prefix 1)(cut-line) }
  insert-line	{ (beginning-of-line)(open-line) }
  delete-current-window
	{ (if (not (free-window -1)) (msg "Could not delete window")) }
  describe-Key	;; what is a key bound to
  {
    (string key bind)

    (key (ask "Key: "))
    (if (== "" (bind (key-bound-to key)))(msg key " is not bound.")
    (msg key " is bound to " bind))
  }
  rename-buffer
  {
    (if (not (file-name -1 (complete CC_FNAME "New file name for buffer: ")))
       (msg "Could not rename buffer."))
  }
  not-modified { (buffer-modified -1 FALSE) }
  Goto-line
  {
    (goto-line
	(if (arg-flag) (arg-prefix) (convert-to NUMBER (ask "Goto line: "))))
  }
  use-existing-buffer
  {
    (arg-prefix 1)
    (switch-to-buffer (complete CC_BUF "Use existing buffer: "))
  }
  toggle-view-mode
  {
    (int cb)
    (int bf)

    (bf (buffer-flags (cb (current-buffer))))
    (if (!= 0 (bit-and bf BFView))
      (buffer-flags cb (bit-and bf (bit-xor 0xffffffff BFView)))
      (buffer-flags cb (bit-or bf BFView))
    )
    (buffer-flags cb BFMode)
  }
  toggle-exact-mode
  {
    (if (== 1 (case-fold-search))
      (case-fold-search 0)
      (case-fold-search 1)
    )
    (buffer-flags (current-buffer) BFMode)
  }
)

(defun
  shell-region
  {
    (int bag-id)

    (bag-id (create-bag))
    (append-to-bag bag-id APPEND-REGION)
    (shell-command (bag-to-string bag-id))
    (free-bag)

    (msg "[Return to resume editing]")(get-key)
  }
)

(const RECTANGLE-BAG 0)
(defun		;; rectangle commands
  copy-rectangle  { (append-to-bag RECTANGLE-BAG APPEND-RECTANGLE) }
  cut-rectangle
      { (append-to-bag RECTANGLE-BAG APPEND-RECTANGLE) (erase-rectangle TRUE) }
  delete-rectangle { (erase-rectangle TRUE) }
  clear-rectangle  { (erase-rectangle FALSE) }
)

(int paste-bag-id)
(defun		;; my paste buffer
  MAIN { (paste-bag-id (create-bag TRUE)) }
  yank-paste-buffer { (insert-bag paste-bag-id) }
  fill-paste-buffer
    { (clear-bag paste-bag-id)(append-to-bag paste-bag-id APPEND-REGION) }
)

(defun insert-file-name { (insert-text (complete CC_FNAME "file name: ")) })

(include modeline.mut)
(include hook.mut)
(include require.mut)

(include nextfile.mut)	;; read next file from command line
(include sysvar.mut)	;; allow user to view or modify system vars
(include bstats.mut)	;; buffer stats

(include alamode.mut)	;; set modes
(include pmatch.mut)	;; paren matching
(include indent.mut)	;; shift a region right or left
(include markring.mut)	;; a ring of marks
(include hidebuf.mut)	;; a ring of marks

(include isearch.mut)	;; incremental search
(include search.mut)
(include transpos.mut)	;; transpose characters
(include undo.mut)	;; undo support

(include case.mut)	;;
(include window.mut)
(include delbuf.mut)
(include wspace.mut)
(include autoload.mut)
(include savebufs.mut)

(defun
  query-replace    { (autoload "query-replace"    "qr"	     (push-args 0)) }
  re-query-replace { (autoload "re-query-replace" "qr"	     (push-args 0)) }
  adjust-lines	   { (autoload "adjust-lines"	  "adjust"   (push-args 0)) }
  calculator	   { (autoload "calculator"	  "calc")     }
  c-mode	   { (autoload "c-mode"		  "cmode")    }
  mutt-mode	   { (autoload "mutt-mode"	  "muttmode") }
  text-mode	   { (autoload "text-mode"	  "textmode") }
  pushd		   { (autoload "pu"		  "dir"      (push-args 0)) }
  cd		   { (autoload "cd"		  "dir"      (push-args 0)) }
  pwd		   { (autoload "pwd"		  "dir")      }
  edit-picture	   { (autoload "edit-picture"	  "picture")  }
  mail-mode        { (autoload "mail-mode"	  "mailmode") }
  buffer-nomunge   { (autoload "buffer-nomunge"	  "nomunge")  }
  filter	   { (autoload "filter"		  "filter"   (push-args 0)) }
  compile	   { (autoload "compile"	  "gcc"      (push-args 0)) }
  findit	   { (autoload "findit"		  "findit")   }
  pipe-command 	   { (autoload "pipe-command"  	  "spawn")    }
  goto-tag	   { (autoload "goto-tag"	  "tags")     }
  narrow-buffer	   { (autoload "narrow-buffer"	  "narrow")   }
  widen-buffer     { (autoload "widen-buffer"     "narrow")   }
)

(defun MAIN	;; and now the init time code
{
  (small-int n)
  (bind-to-key "compile"		"S-F-1")
  (bind-to-key "spawn-shell"		"S-F-2")
  (bind-to-key "toggle-exact-mode"	"S-F-3")
  (bind-to-key "goto-matching-fence"	"S-F-9")	;; in pmatch.mut
  (bind-to-key "execute-macro"		"S-F-0")
;;  (bind-to-key "CR->CR&indent"		"F-7")
;;  (bind-to-key "CR->CR"			"F-8")
;;  (bind-to-key "set-the-mark"		"F-0")

  (bind-to-key "next-buffer"	"C-XX")
  (bind-to-key "cut-the-line"	"F-L")		;; shift delete
  (bind-to-key "insert-space"	"F-G")		;; insert key
  (bind-to-key "not-modified"	"M-~")
  (bind-to-key "undo"		"F-N")		;; undo
  (bind-to-key "center-cursor"  "F-M")		;; shift insert key

  (bind-to-key "transpose-chars"	"C-T")		;; in transpos.mut
  (bind-to-key "next-file"		"C-XF")		;; in nextfile.mut
  (bind-to-key "query-replace"		"M-%")
  (bind-to-key "re-query-replace"	"M-C-Q")
  (bind-to-key "Goto-line"		"M-G")
  (bind-to-key "delete-current-window"	"C-x0")
  (bind-to-key "list-buffers"		"C-xC-b")	;; in bstats.mut
  (bind-to-key "show-buffer-stats"	"C-x=")		;; in bstats.mut
  (bind-to-key "use-existing-buffer"	"C-xC-o")
  (bind-to-key "save-buffers-and-exit"	"M-Z")		;; in savebufs.mut
  (bind-to-key "goto-tag"		"M-.")		;; GNU binding
  (bind-to-key "pipe-command"		"C-X@")		;; in spawn.mut
  (bind-to-key "toggle-view-mode"	"C-XC-Q")
  (bind-to-key "narrow-buffer"          "C-XN")		;; in narrow.mut
  (bind-to-key "widen-buffer"           "C-XW")		;; in narrow.mut

;;  (bind-to-key "spell-word"		"M-$")		;; in spell.mut


;  (arg-prefix 1)(load "osstuff")	;; load os specific ME extensions
;  (arg-prefix 1)(load "./mext")		;; load directory local ME extensions

;  (arg-prefix 1)(load "myme")		;; load your personal ME extensions

  (if (not (command-line-done))	;; Gotta process the command line myself
    {
      (update)		;; so messages from the next stuff stick around
;      (if (!= 1 (argc))		;; there are files on command line
;	(next-file)		;; edit first file on command line
;	(version))		;; stick something in the message line
	(n (argc))
	(while (> (n) 1)
	{
	  (next-file)
	  (-= n 1)
	})
    })

    ;; Delete the *scratch* buffer, even if it is the only buffer.
    ;; If it is the only buffer, this will cause (create-buffer-hook) to be
    ;;   called.  Since *scratch* was created before this code was loaded,
    ;;   (create-buffer-hook) hadn't been called.
    ;; Gotta be careful - *scratch* might already be gone if this file is
    ;;   loaded twice.
  (if (!= -2 (attached-buffer "*scratch*"))
	(free-buffer (attached-buffer "*scratch*")))
  (version)
})


(bool hoodoo)
(defun command-line-done ;; [(bool it-has-been-processed)]
{
  (if (!= 0 (nargs)) (hoodoo (arg 0)))
  hoodoo
})
