home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / elisp / packages / macedit.el < prev    next >
Encoding:
Text File  |  1990-07-22  |  20.1 KB  |  644 lines

  1. ;From utkcs2!emory!swrinde!zaphod.mps.ohio-state.edu!usc!elroy.jpl.nasa.gov!cit-vax!daveg Tue Jun 12 10:08:45 EDT 1990
  2. ;Article 4426 of comp.emacs:
  3. ;Xref: utkcs2 gnu.emacs:2948 comp.emacs:4426
  4. ;Path: utkcs2!emory!swrinde!zaphod.mps.ohio-state.edu!usc!elroy.jpl.nasa.gov!cit-vax!daveg
  5. ;>From: daveg@cit-vax.Caltech.Edu (David Gillespie)
  6. ;Newsgroups: gnu.emacs,comp.emacs
  7. ;Subject: Keyboard macro editor
  8. ;Message-ID: <15241@cit-vax.Caltech.Edu>
  9. ;Date: 11 Jun 90 22:44:48 GMT
  10. ;Reply-To: daveg@cit-vax.UUCP (David Gillespie)
  11. ;Organization: California Institute of Technology
  12. ;Lines: 628
  13. ;
  14. ;The recent posting of my GNU Emacs calculator on comp.sources.misc omitted
  15. ;the file "macedit.el".  For your keyboard-macro-programming enjoyment,
  16. ;here it is now.
  17. ;
  18. ;MacEdit is not directly related to Calc---anybody who uses keyboard macros
  19. ;in Emacs can use MacEdit.
  20. ;
  21. ;                                -- Dave
  22.  
  23.  
  24. ;; Keyboard macro editor for GNU Emacs.  Version 1.01.
  25. ;; Copyright (C) 1990 Dave Gillespie, daveg@csvax.caltech.edu.
  26.  
  27. ;; This file is part of GNU Emacs.
  28.  
  29. ;; GNU Emacs is distributed in the hope that it will be useful,
  30. ;; but WITHOUT ANY WARRANTY.  No author or distributor
  31. ;; accepts responsibility to anyone for the consequences of using it
  32. ;; or for whether it serves any particular purpose or works at all,
  33. ;; unless he says so in writing.  Refer to the GNU Emacs General Public
  34. ;; License for full details.
  35.  
  36. ;; Everyone is granted permission to copy, modify and redistribute
  37. ;; GNU Emacs, but only under the conditions described in the
  38. ;; GNU Emacs General Public License.   A copy of this license is
  39. ;; supposed to have been given to you along with GNU Emacs so you
  40. ;; can know your rights and responsibilities.  It should be in a
  41. ;; file named COPYING.  Among other things, the copyright notice
  42. ;; and this notice must be preserved on all copies.
  43.  
  44. ;; Installation:
  45. ;;   (autoload 'edit-kbd-macro "macedit" "Edit a named keyboard macro" t)
  46. ;;   (autoload 'edit-last-kbd-macro "macedit" "Edit a keyboard macro" t)
  47.  
  48.  
  49.  
  50. ;; To use, type `M-x edit-last-kbd-macro' to edit the most recently
  51. ;; defined keyboard macro.  If you have used `M-x name-last-kbd-macro'
  52. ;; to give a keyboard macro a name, type `M-x edit-kbd-macro' to edit
  53. ;; the macro by name.  When you are done editing, type `C-c C-c' to
  54. ;; record your changes back into the original keyboard macro.
  55.  
  56.  
  57.  
  58.  
  59. ;;; The user-level commands for editing macros.
  60.  
  61. (defun edit-last-kbd-macro (&optional prefix buffer hook)
  62.   "Edit the most recently defined keyboard macro."
  63.   (interactive "P")
  64.   (MacEdit-edit-macro last-kbd-macro
  65.               (function (lambda (x arg) (setq last-kbd-macro x)))
  66.               prefix buffer hook)
  67. )
  68.  
  69. (defun edit-kbd-macro (cmd &optional prefix buffer hook in-hook out-hook)
  70.   "Edit a keyboard macro which has been assigned a name by name-last-kbd-macro.
  71. \(See also edit-last-kbd-macro.)"
  72.   (interactive "CCommand name: \nP")
  73.   (and cmd
  74.        (MacEdit-edit-macro (if in-hook
  75.                    (funcall in-hook cmd)
  76.                  (symbol-function cmd))
  77.                (or out-hook
  78.                    (list 'lambda '(x arg)
  79.                      (list 'fset
  80.                        (list 'quote cmd)
  81.                        'x)))
  82.                prefix buffer hook cmd))
  83. )
  84.  
  85.  
  86.  
  87.  
  88. ;;; Formatting a keyboard macro as human-readable text.
  89.  
  90. (defun MacEdit-print-macro (macro-str local-map)
  91.   (let ((save-map (current-local-map))
  92.     (print-escape-newlines t)
  93.     key-symbol key-str key-last prefix-arg this-prefix)
  94.     (unwind-protect
  95.     (progn
  96.       (use-local-map local-map)
  97.       (while (MacEdit-peek-char)
  98.         (MacEdit-read-key)
  99.         (setq this-prefix prefix-arg)
  100.         (or (memq key-symbol '(digit-argument
  101.                    negative-argument
  102.                    universal-argument))
  103.         (null prefix-arg)
  104.         (progn
  105.           (cond ((consp prefix-arg)
  106.              (insert (format "prefix-arg (%d)\n"
  107.                      (car prefix-arg))))
  108.             ((eq prefix-arg '-)
  109.              (insert "prefix-arg -\n"))
  110.             ((numberp prefix-arg)
  111.              (insert (format "prefix-arg %d\n" prefix-arg))))
  112.           (setq prefix-arg nil)))
  113.         (cond ((null key-symbol)
  114.            (insert "type \"")
  115.            (MacEdit-insert-string macro-str)
  116.            (insert "\"\n")
  117.            (setq macro-str ""))
  118.           ((eq key-symbol 'digit-argument)
  119.            (MacEdit-prefix-arg key-last nil prefix-arg))
  120.           ((eq key-symbol 'negative-argument)
  121.            (MacEdit-prefix-arg ?- nil prefix-arg))
  122.           ((eq key-symbol 'universal-argument)
  123.            (let* ((c-u 4) (argstartchar last-command-char)
  124.               (char (MacEdit-read-char)))
  125.              (while (= char argstartchar)
  126.                (setq c-u (* 4 c-u)
  127.                  char (MacEdit-read-char)))
  128.              (MacEdit-prefix-arg char c-u nil)))
  129.           ((eq key-symbol 'self-insert-command)
  130.            (insert "insert ")
  131.            (if (and (>= key-last 32) (<= key-last 126))
  132.                (let ((str ""))
  133.              (while (or (and (eq key-symbol
  134.                          'self-insert-command)
  135.                      (< (length str) 60)
  136.                      (>= key-last 32)
  137.                      (<= key-last 126))
  138.                     (and (memq key-symbol
  139.                            '(backward-delete-char
  140.                          delete-backward-char
  141.                          backward-delete-char-untabify))
  142.                      (> (length str) 0)))
  143.                (if (eq key-symbol 'self-insert-command)
  144.                    (setq str (concat str
  145.                          (char-to-string key-last)))
  146.                  (setq str (substring str 0 -1)))
  147.                (MacEdit-read-key))
  148.              (insert "\"" str "\"\n")
  149.              (MacEdit-unread-chars key-str))
  150.              (insert "\"")
  151.              (MacEdit-insert-string (char-to-string key-last))
  152.              (insert "\"\n")))
  153.           ((and (eq key-symbol 'quoted-insert)
  154.             (MacEdit-peek-char))
  155.            (insert "quoted-insert\n")
  156.            (let ((ch (MacEdit-read-char))
  157.              ch2)
  158.              (if (and (>= ch ?0) (<= ch ?7))
  159.              (progn
  160.                (setq ch (- ch ?0)
  161.                  ch2 (MacEdit-read-char))
  162.                (if ch2
  163.                    (if (and (>= ch2 ?0) (<= ch2 ?7))
  164.                    (progn
  165.                      (setq ch (+ (* ch 8) (- ch2 ?0))
  166.                        ch2 (MacEdit-read-char))
  167.                      (if ch2
  168.                      (if (and (>= ch2 ?0) (<= ch2 ?7))
  169.                          (setq ch (+ (* ch 8) (- ch2 ?0)))
  170.                        (MacEdit-unread-chars ch2))))
  171.                  (MacEdit-unread-chars ch2)))))
  172.              (if (or (and (>= ch ?0) (<= ch ?7))
  173.                  (< ch 32) (> ch 126))
  174.              (insert (format "type \"\\%03o\"\n" ch))
  175.                (insert "type \"" (char-to-string ch) "\"\n"))))
  176.           ((memq key-symbol '(isearch-forward
  177.                       isearch-backward
  178.                       isearch-forward-regexp
  179.                       isearch-backward-regexp))
  180.            (insert (symbol-name key-symbol) "\n")
  181.            (MacEdit-isearch-argument))
  182.           ((eq key-symbol 'execute-extended-command)
  183.            (MacEdit-read-argument obarray 'commandp))
  184.           (t
  185.            (let ((cust (get key-symbol 'MacEdit-print)))
  186.              (if cust
  187.              (funcall cust)
  188.                (insert (symbol-name key-symbol))
  189.                (indent-to 30)
  190.                (insert " # ")
  191.                (MacEdit-insert-string key-str)
  192.                (insert "\n")
  193.                (let ((int (MacEdit-get-interactive key-symbol)))
  194.              (if (string-match "\\`\\*" int)
  195.                  (setq int (substring int 1)))
  196.              (while (> (length int) 0)
  197.                (cond ((= (aref int 0) ?a)
  198.                   (MacEdit-read-argument
  199.                    obarray nil))
  200.                  ((memq (aref int 0) '(?b ?B ?D ?f ?F ?n
  201.                               ?s ?S ?x ?X))
  202.                   (MacEdit-read-argument))
  203.                  ((and (= (aref int 0) ?c)
  204.                        (MacEdit-peek-char))
  205.                   (insert "type \"")
  206.                   (MacEdit-insert-string
  207.                    (char-to-string
  208.                     (MacEdit-read-char)))
  209.                   (insert "\"\n"))
  210.                  ((= (aref int 0) ?C)
  211.                   (MacEdit-read-argument
  212.                    obarray 'commandp))
  213.                  ((= (aref int 0) ?k)
  214.                   (MacEdit-read-key)
  215.                   (if key-symbol
  216.                       (progn
  217.                     (insert "type \"")
  218.                     (MacEdit-insert-string key-str)
  219.                     (insert "\"\n"))
  220.                     (MacEdit-unread-chars key-str)))
  221.                  ((= (aref int 0) ?N)
  222.                   (or this-prefix
  223.                       (MacEdit-read-argument)))
  224.                  ((= (aref int 0) ?v)
  225.                   (MacEdit-read-argument
  226.                    obarray 'user-variable-p)))
  227.                (let ((nl (string-match "\n" int)))
  228.                  (setq int (if nl
  229.                        (substring int (1+ nl))
  230.                      "")))))))))))
  231.       (use-local-map save-map)))
  232. )
  233.  
  234. (defun MacEdit-prefix-arg (char c-u value)
  235.   (let ((sign 1))
  236.     (if (and (numberp value) (< value 0))
  237.     (setq sign -1 value (- value)))
  238.     (if (eq value '-)
  239.     (setq sign -1 value nil))
  240.     (while (and char (= ?- char))
  241.       (setq sign (- sign) c-u nil)
  242.       (setq char (MacEdit-read-char)))
  243.     (while (and char (>= char ?0) (<= char ?9))
  244.       (setq value (+ (* (if (numberp value) value 0) 10) (- char ?0)) c-u nil)
  245.       (setq char (MacEdit-read-char)))
  246.     (setq prefix-arg
  247.       (cond (c-u (list c-u))
  248.         ((numberp value) (* value sign))
  249.         ((= sign -1) '-)))
  250.     (MacEdit-unread-chars char))
  251. )
  252.  
  253. (defun MacEdit-insert-string (str)
  254.   (let ((i 0) j ch)
  255.     (while (< i (length str))
  256.       (if (and (> (setq ch (aref str i)) 127)
  257.            (< ch 160))
  258.       (progn
  259.         (setq ch (- ch 128))
  260.         (insert "\\M-")))
  261.       (if (< ch 32)
  262.       (cond ((= ch 8)  (insret "\\b"))
  263.         ((= ch 9)  (insert "\\t"))
  264.         ((= ch 10) (insert "\\n"))
  265.         ((= ch 13) (insert "\\r"))
  266.         ((= ch 27) (insert "\\e"))
  267.         (t (insert "\\C-" (char-to-string (downcase (+ ch 64))))))
  268.     (if (< ch 127)
  269.         (if (or (= ch 34) (= ch 92))
  270.         (insert "\\" (char-to-string ch))
  271.           (setq j i)
  272.           (while (and (< (setq i (1+ i)) (length str))
  273.               (>= (setq ch (aref str i)) 32)
  274.               (/= ch 34) (/= ch 92)
  275.               (< ch 127)))
  276.           (insert (substring str j i))
  277.           (setq i (1- i)))
  278.       (if (memq ch '(127 255))
  279.           (insert (format "\\%03o" ch))
  280.         (insert "\\M-" (char-to-string (- ch 128))))))
  281.       (setq i (1+ i))))
  282. )
  283.  
  284. (defun MacEdit-lookup-key (map)
  285.   (let ((loc (and map (lookup-key map macro-str)))
  286.     (glob (lookup-key (current-global-map) macro-str))
  287.     (loc-str macro-str)
  288.     (glob-str macro-str))
  289.     (and (integerp loc)
  290.      (setq loc-str (substring macro-str 0 loc)
  291.            loc (lookup-key map loc-str)))
  292.     (and (consp loc)
  293.      (setq loc nil))
  294.     (or loc
  295.     (setq loc-str ""))
  296.     (and (integerp glob)
  297.      (setq glob-str (substring macro-str 0 glob)
  298.            glob (lookup-key (current-global-map) glob-str)))
  299.     (and (consp glob)
  300.      (setq glob nil))
  301.     (or glob
  302.     (setq glob-str ""))
  303.     (if (> (length glob-str) (length loc-str))
  304.     (setq key-symbol glob
  305.           key-str glob-str)
  306.       (setq key-symbol loc
  307.         key-str loc-str))
  308.     (setq key-last (and (> (length key-str) 0)
  309.             (logand (aref key-str (1- (length key-str))) 127)))
  310.     key-symbol)
  311. )
  312.  
  313. (defun MacEdit-read-argument (&optional obarray pred)   ;; currently ignored
  314.   (let ((str "")
  315.     (min-bsp 0)
  316.     (exec (eq key-symbol 'execute-extended-command))
  317.     str-base)
  318.     (while (progn
  319.          (MacEdit-lookup-key (current-global-map))
  320.          (or (and (eq key-symbol 'self-insert-command)
  321.               (< (length str) 60))
  322.          (memq key-symbol
  323.                 '(backward-delete-char
  324.                   delete-backward-char
  325.                   backward-delete-char-untabify))
  326.          (eq key-last 9)))
  327.       (setq macro-str (substring macro-str (length key-str)))
  328.       (or (and (eq key-last 9)
  329.            obarray
  330.            (let ((comp (try-completion str obarray pred)))
  331.          (and (stringp comp)
  332.               (> (length comp) (length str))
  333.               (setq str comp))))
  334.       (if (or (eq key-symbol 'self-insert-command)
  335.           (and (or (eq key-last 9)
  336.                (<= (length str) min-bsp))
  337.                (setq min-bsp (+ (length str) (length key-str)))))
  338.           (setq str (concat str key-str))
  339.         (setq str (substring str 0 -1)))))
  340.     (setq str-base str
  341.       str (concat str key-str)
  342.       macro-str (substring macro-str (length key-str)))
  343.     (if exec
  344.     (let ((comp (try-completion str-base obarray pred)))
  345.       (if (if (stringp comp)
  346.           (and (commandp (intern comp))
  347.                (setq str-base comp))
  348.         (commandp (intern str-base)))
  349.           (insert str-base "\n")
  350.         (insert "execute-extended-command\n")
  351.         (insert "type \"")
  352.         (MacEdit-insert-string str)
  353.         (insert "\"\n")))
  354.       (if (> (length str) 0)
  355.       (progn
  356.         (insert "type \"")
  357.         (MacEdit-insert-string str)
  358.         (insert "\"\n")))))
  359. )
  360.  
  361. (defun MacEdit-isearch-argument ()
  362.   (let ((str "")
  363.     (min-bsp 0)
  364.     ch)
  365.     (while (and (setq ch (MacEdit-read-char))
  366.         (or (<= ch 127) (not search-exit-option))
  367.         (not (eq ch search-exit-char))
  368.         (or (eq ch search-repeat-char)
  369.             (eq ch search-reverse-char)
  370.             (eq ch search-delete-char)
  371.             (eq ch search-yank-word-char)
  372.             (eq ch search-yank-line-char)
  373.             (eq ch search-quote-char)
  374.             (eq ch ?\r)
  375.             (eq ch ?\t)
  376.             (not search-exit-option)
  377.             (and (/= ch 127) (>= ch 32))))
  378.       (if (and (eq ch search-quote-char)
  379.            (MacEdit-peek-char))
  380.       (setq str (concat str (char-to-string ch)
  381.                 (char-to-string (MacEdit-read-char)))
  382.         min-bsp (length str))
  383.     (if (or (and (< ch 127) (>= ch 32))
  384.         (eq ch search-yank-word-char)
  385.         (eq ch search-yank-line-char)
  386.         (and (or (not (eq ch search-delete-char))
  387.              (<= (length str) min-bsp))
  388.              (setq min-bsp (1+ (length str)))))
  389.         (setq str (concat str (char-to-string ch)))
  390.       (setq str (substring str 0 -1)))))
  391.     (if (eq ch search-exit-char)
  392.     (if (= (length str) 0)  ;; non-incremental search
  393.         (progn
  394.           (setq str (concat str (char-to-string ch)))
  395.           (and (eq (MacEdit-peek-char) ?\C-w)
  396.            (progn
  397.              (setq str (concat str "\C-w"))
  398.              (MacEdit-read-char)))
  399.           (if (> (length str) 0)
  400.           (progn
  401.             (insert "type \"")
  402.             (MacEdit-insert-string str)
  403.             (insert "\"\n")))
  404.           (MacEdit-read-argument)
  405.           (setq str "")))
  406.       (MacEdit-unread-chars ch))
  407.     (if (> (length str) 0)
  408.     (progn
  409.       (insert "type \"")
  410.       (MacEdit-insert-string str)
  411.       (insert "\"\n"))))
  412. )
  413.  
  414. ;;; Get the next keystroke-sequence from the input stream.
  415. ;;; Sets key-symbol, key-str, and key-last as a side effect.
  416. (defun MacEdit-read-key ()
  417.   (MacEdit-lookup-key (current-local-map))
  418.   (and key-symbol
  419.        (setq macro-str (substring macro-str (length key-str))))
  420. )
  421.  
  422. (defun MacEdit-peek-char ()
  423.   (and (> (length macro-str) 0)
  424.        (aref macro-str 0))
  425. )
  426.  
  427. (defun MacEdit-read-char ()
  428.   (and (> (length macro-str) 0)
  429.        (prog1
  430.        (aref macro-str 0)
  431.      (setq macro-str (substring macro-str 1))))
  432. )
  433.  
  434. (defun MacEdit-unread-chars (chars)
  435.   (and (integerp chars)
  436.        (setq chars (char-to-string chars)))
  437.   (and chars
  438.        (setq macro-str (concat chars macro-str)))
  439. )
  440.  
  441. (defun MacEdit-dump (mac)
  442.   (set-mark-command nil)
  443.   (insert "\n\n")
  444.   (MacEdit-print-macro mac (current-local-map))
  445. )
  446.  
  447.  
  448.  
  449. ;;; Parse a keyboard macro description in MacEdit-print-macro's format.
  450.  
  451. (defun MacEdit-read-macro (&optional map)
  452.   (or map (setq map (current-local-map)))
  453.   (let ((macro-str ""))
  454.     (while (not (progn
  455.           (skip-chars-forward " \t\n")
  456.           (eobp)))
  457.       (cond ((looking-at "#"))   ;; comment
  458.         ((looking-at "prefix-arg[ \t]+-[ \t]+\n")
  459.          (MacEdit-append-chars "\C-u-"))
  460.         ((looking-at "prefix-arg[ \t]+\\(-?[0-9]+\\)[ \t]+\n")
  461.          (MacEdit-append-chars (concat "\C-u" (MacEdit-match-string 1))))
  462.         ((looking-at "prefix-arg[ \t]+(\\([0-9]+\\))[ \t]+\n")
  463.          (let ((val (string-to-int (MacEdit-match-string 1))))
  464.            (while (/= val 0)
  465.          (or (and (> val 0) (= (% val 4) 0))
  466.              (error "Bad prefix argument value"))
  467.          (MacEdit-append-chars "\C-u")
  468.          (setq val (/ val 4)))))
  469.         ((looking-at "prefix-arg")
  470.          (error "Bad prefix argument syntax"))
  471.         ((looking-at "insert ")
  472.          (forward-char 7)
  473.          (MacEdit-append-chars (read (current-buffer)))
  474.          (if (< (current-column) 7)
  475.          (forward-line -1)))
  476.         ((looking-at "type ")
  477.          (forward-char 5)
  478.          (MacEdit-append-chars (read (current-buffer)))
  479.          (if (< (current-column) 5)
  480.          (forward-line -1)))
  481.         ((looking-at "\\([-a-zA-z0-9_]+\\)[ \t]*\\(.*\\)\n")
  482.          (let* ((func (intern (MacEdit-match-string 1)))
  483.             (arg (MacEdit-match-string 2))
  484.             (cust (get func 'MacEdit-read)))
  485.            (if cust
  486.            (funcall cust arg)
  487.          (or (commandp func)
  488.              (error "Not an Emacs command"))
  489.          (or (equal arg "")
  490.              (string-match "\\`#" arg)
  491.              (error "Unexpected argument to command"))
  492.          (let ((keys
  493.             (or (where-is-internal func map t)
  494.                 (where-is-internal func (current-global-map) t))))
  495.            (if keys
  496.                (MacEdit-append-chars keys)
  497.              (MacEdit-append-chars (concat "\ex"
  498.                            (symbol-name func)
  499.                            "\n")))))))
  500.         (t (error "Syntax error")))
  501.       (forward-line 1))
  502.     macro-str)
  503. )
  504.  
  505. (defun MacEdit-append-chars (chars)
  506.   (setq macro-str (concat macro-str chars))
  507. )
  508.  
  509. (defun MacEdit-match-string (n)
  510.   (if (match-beginning n)
  511.       (buffer-substring (match-beginning n) (match-end n))
  512.     "")
  513. )
  514.  
  515.  
  516.  
  517. (defun MacEdit-get-interactive (func)
  518.   (if (symbolp func)
  519.       (let ((cust (get func 'MacEdit-interactive)))
  520.     (if cust
  521.         cust
  522.       (MacEdit-get-interactive (symbol-function func))))
  523.     (or (and (eq (car-safe func) 'lambda)
  524.          (let ((int (if (consp (nth 2 func))
  525.                 (nth 2 func)
  526.               (nth 3 func))))
  527.            (and (eq (car-safe int) 'interactive)
  528.             (stringp (nth 1 int))
  529.             (nth 1 int))))
  530.     ""))
  531. )
  532.  
  533. (put 'search-forward           'MacEdit-interactive "s")
  534. (put 'search-backward          'MacEdit-interactive "s")
  535. (put 'word-search-forward      'MacEdit-interactive "s")
  536. (put 'word-search-backward     'MacEdit-interactive "s")
  537. (put 're-search-forward        'MacEdit-interactive "s")
  538. (put 're-search-backward       'MacEdit-interactive "s")
  539. (put 'switch-to-buffer         'MacEdit-interactive "B")
  540. (put 'kill-buffer              'MacEdit-interactive "B")
  541. (put 'rename-buffer            'MacEdit-interactive "B\nB")
  542. (put 'goto-char                'MacEdit-interactive "N")
  543. (put 'global-set-key           'MacEdit-interactive "k\nC")
  544. (put 'global-unset-key         'MacEdit-interactive "k")
  545. (put 'local-set-key            'MacEdit-interactive "k\nC")
  546. (put 'local-unset-key          'MacEdit-interactive "k")
  547.  
  548. ;;; Think about kbd-macro-query
  549.  
  550.  
  551.  
  552. ;;; Edit a keyboard macro in another buffer.
  553. ;;; (Prefix argument is currently ignored.)
  554.  
  555. (defun MacEdit-edit-macro (mac repl &optional prefix buffer hook arg)
  556.   (or (stringp mac)
  557.       (error "Not a keyboard macro"))
  558.   (let ((oldbuf (current-buffer))
  559.     (local (current-local-map))
  560.     (buf (get-buffer-create (or buffer "*Edit Macro*"))))
  561.     (set-buffer buf)
  562.     (kill-all-local-variables)
  563.     (use-local-map MacEdit-mode-map)
  564.     (setq buffer-read-only nil)
  565.     (setq major-mode 'MacEdit-mode)
  566.     (setq mode-name "Edit Macro")
  567.     (make-local-variable 'MacEdit-original-buffer)
  568.     (setq MacEdit-original-buffer oldbuf)
  569.     (make-local-variable 'MacEdit-replace-function)
  570.     (setq MacEdit-replace-function repl)
  571.     (make-local-variable 'MacEdit-replace-argument)
  572.     (setq MacEdit-replace-argument arg)
  573.     (make-local-variable 'MacEdit-finish-hook)
  574.     (setq MacEdit-finish-hook hook)
  575.     (erase-buffer)
  576.     (insert "# Keyboard Macro Editor.  Press C-c C-c to finish; press C-x k RET to cancel.\n")
  577.     (insert "# Original keys: " (key-description mac) "\n\n")
  578.     (message "Formatting keyboard macro...")
  579.     (MacEdit-print-macro mac local)
  580.     (switch-to-buffer buf)
  581.     (goto-char (point-min))
  582.     (forward-line 3)
  583.     (recenter '(4))
  584.     (set-buffer-modified-p nil)
  585.     (message "Formatting keyboard macro...done")
  586.     (run-hooks 'MacEdit-format-hook))
  587. )
  588.  
  589. (defun MacEdit-finish-edit ()
  590.   (interactive)
  591.   (or (and (boundp 'MacEdit-original-buffer)
  592.        (boundp 'MacEdit-replace-function)
  593.        (boundp 'MacEdit-replace-argument)
  594.        (boundp 'MacEdit-finish-hook)
  595.        (eq major-mode 'MacEdit-mode))
  596.       (error "This command is valid only in buffers created by edit-kbd-macro."))
  597.   (let ((buf (current-buffer))
  598.     (str (buffer-string))
  599.     (func MacEdit-replace-function)
  600.     (arg MacEdit-replace-argument)
  601.     (hook MacEdit-finish-hook))
  602.     (goto-char (point-min))
  603.     (run-hooks 'MacEdit-compile-hook)
  604.     (and (buffer-modified-p)
  605.      func
  606.      (progn
  607.        (message "Compiling keyboard macro...")
  608.        (let ((mac (MacEdit-read-macro
  609.                (and (buffer-name MacEdit-original-buffer)
  610.                 (save-excursion
  611.                   (set-buffer MacEdit-original-buffer)
  612.                   (current-local-map))))))
  613.          (and (buffer-name MacEdit-original-buffer)
  614.           (switch-to-buffer MacEdit-original-buffer))
  615.          (funcall func mac arg))
  616.        (message "Compiling keyboard macro...done")))
  617.     (kill-buffer buf)
  618.     (if hook
  619.     (funcall hook arg)))
  620. )
  621.  
  622. (defun MacEdit-mode ()
  623.   "Keyboard Macro Editing mode.  Press C-c C-c to save and exit.
  624. To abort the edit, just kill this buffer with C-x k RET.
  625.  
  626. The keyboard macro is represented as a series of M-x style command names.
  627. Keystrokes which do not correspond to simple M-x commands are written as
  628. \"type\" commands.  When you press C-c C-c, MacEdit converts each command
  629. back into a suitable keystroke sequence; \"type\" commands are converted
  630. directly back into keystrokes."
  631.   (interactive)
  632.   (error "This mode can be enabled only by edit-kbd-macro or edit-last-kbd-macro.")
  633. )
  634. (put 'MacEdit-mode 'mode-class 'special)
  635.  
  636. (defvar MacEdit-mode-map nil)
  637. (if MacEdit-mode-map
  638.     ()
  639.   (setq MacEdit-mode-map (make-sparse-keymap))
  640.   (define-key MacEdit-mode-map "\C-c\C-c" 'MacEdit-finish-edit)
  641. )
  642.  
  643.  
  644.