/[emacs]/emacs/lisp/kmacro.el
ViewVC logotype

Diff of /emacs/lisp/kmacro.el

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.7 by kfstorm, Thu Aug 29 13:06:26 2002 UTC revision 1.8 by kfstorm, Sun Sep 8 20:38:04 2002 UTC
# Line 165  macro to be executed before appending to Line 165  macro to be executed before appending to
165    :type 'boolean    :type 'boolean
166    :group 'kmacro)    :group 'kmacro)
167    
168    (defcustom kmacro-step-edit-mini-window-height 0.75
169      "Override `max-mini-window-height' when step edit keyboard macro."
170      :type 'number
171      :group 'kmacro)
172    
173  ;; Keymap  ;; Keymap
174    
# Line 174  macro to be executed before appending to Line 178  macro to be executed before appending to
178      (define-key map "\C-k" 'kmacro-end-or-call-macro-repeat)      (define-key map "\C-k" 'kmacro-end-or-call-macro-repeat)
179      (define-key map "\C-e" 'kmacro-edit-macro-repeat)      (define-key map "\C-e" 'kmacro-edit-macro-repeat)
180      (define-key map "\r"   'kmacro-edit-macro)      (define-key map "\r"   'kmacro-edit-macro)
181        (define-key map " "    'kmacro-step-edit-macro)
182      (define-key map "l"    'kmacro-edit-lossage)      (define-key map "l"    'kmacro-edit-lossage)
183      (define-key map "\C-i" 'kmacro-insert-counter)      (define-key map "\C-i" 'kmacro-insert-counter)
184      (define-key map "\C-a" 'kmacro-add-counter)      (define-key map "\C-a" 'kmacro-add-counter)
# Line 199  macro to be executed before appending to Line 204  macro to be executed before appending to
204  ;;; Provide some binding for startup:  ;;; Provide some binding for startup:
205  ;;;###autoload (global-set-key "\C-x(" 'kmacro-start-macro)  ;;;###autoload (global-set-key "\C-x(" 'kmacro-start-macro)
206  ;;;###autoload (global-set-key "\C-x)" 'kmacro-end-macro)  ;;;###autoload (global-set-key "\C-x)" 'kmacro-end-macro)
207  ;;;###autoload (global-set-key "\C-xe" 'kmacro-end-or-call-macro)  ;;;###autoload (global-set-key "\C-xe" 'kmacro-end-and-call-macro)
208  ;;;###autoload (global-set-key [f3] 'kmacro-start-macro-or-insert-counter)  ;;;###autoload (global-set-key [f3] 'kmacro-start-macro-or-insert-counter)
209  ;;;###autoload (global-set-key [f4] 'kmacro-end-or-call-macro)  ;;;###autoload (global-set-key [f4] 'kmacro-end-or-call-macro)
210  ;;;###autoload (global-set-key "\C-x\C-k" 'kmacro-keymap)  ;;;###autoload (global-set-key "\C-x\C-k" 'kmacro-keymap)
# Line 581  others, use M-x name-last-kbd-macro." Line 586  others, use M-x name-last-kbd-macro."
586      (when (and (or (null arg) (> arg 0))      (when (and (or (null arg) (> arg 0))
587                 (setq repeat-key                 (setq repeat-key
588                       (if (eq kmacro-call-repeat-key t) repeat-key kmacro-call-repeat-key)))                       (if (eq kmacro-call-repeat-key t) repeat-key kmacro-call-repeat-key)))
589        (require 'edmacro)        (setq repeat-key-str (format-kbd-macro (vector repeat-key) nil))
       (setq repeat-key-str (edmacro-format-keys (vector repeat-key) nil))  
590        (while repeat-key        (while repeat-key
591          (message "Repeat macro %swith `%s'..."          (message "Repeat macro %swith `%s'..."
592                   (if (and kmacro-call-repeat-with-arg                   (if (and kmacro-call-repeat-with-arg
# Line 654  With \\[universal-argument], call second Line 658  With \\[universal-argument], call second
658    
659    
660  ;;;###autoload  ;;;###autoload
661    (defun kmacro-end-and-call-macro (arg &optional no-repeat)
662      "Call last keyboard macro, ending it first if currently being defined.
663    With numeric prefix ARG, repeat macro that many times."
664      (interactive "P")
665      (if defining-kbd-macro
666          (kmacro-end-macro nil))
667      (kmacro-call-macro arg no-repeat))
668    
669    
670    ;;;###autoload
671  (defun kmacro-end-call-mouse (event)  (defun kmacro-end-call-mouse (event)
672    "Move point to the position clicked with the mouse and call last kbd macro.    "Move point to the position clicked with the mouse and call last kbd macro.
673  If kbd macro currently being defined end it before activating it."  If kbd macro currently being defined end it before activating it."
# Line 717  If kbd macro currently being defined end Line 731  If kbd macro currently being defined end
731    (edit-kbd-macro "\C-hl"))    (edit-kbd-macro "\C-hl"))
732    
733    
734    ;;; Single-step editing of keyboard macros
735    
736    (defvar kmacro-step-edit-active)         ;; step-editing active
737    (defvar kmacro-step-edit-new-macro)      ;; storage for new macro
738    (defvar kmacro-step-edit-inserting)      ;; inserting into macro
739    (defvar kmacro-step-edit-appending)      ;; append to end of macro
740    (defvar kmacro-step-edit-replace)        ;; replace orig macro when done
741    (defvar kmacro-step-edit-prefix-index)   ;; index of first prefix arg key
742    (defvar kmacro-step-edit-key-index)      ;; index of current key
743    (defvar kmacro-step-edit-action)         ;; automatic action on next pre-command hook
744    (defvar kmacro-step-edit-help)           ;; kmacro step edit help enabled
745    (defvar kmacro-step-edit-num-input-keys) ;; to ignore duplicate pre-command hook
746    
747    (defvar kmacro-step-edit-map (make-sparse-keymap)
748      "Keymap that defines the responses to questions in `kmacro-step-edit-macro'.
749    This keymap is an extension to the `query-replace-map', allowing the
750    following additional answers: `insert-1', `insert', `append-1',
751    `append', `replace-1', `replace', `act-repeat', `skip-rest',
752    `skip-keep'.")
753    
754    ;; query-replace-map answers include: `act', `skip', `act-and-show',
755    ;; `exit', `act-and-exit', `edit', `delete-and-edit', `recenter',
756    ;; `automatic', `backup', `exit-prefix', and `help'.")
757    ;; Also: `quit', `edit-replacement'
758    
759    (set-keymap-parent kmacro-step-edit-map query-replace-map)
760    
761    (define-key kmacro-step-edit-map "\t" 'act-repeat)
762    (define-key kmacro-step-edit-map [tab] 'act-repeat)
763    (define-key kmacro-step-edit-map "\C-k" 'skip-rest)
764    (define-key kmacro-step-edit-map "c" 'automatic)
765    (define-key kmacro-step-edit-map "f" 'skip-keep)
766    (define-key kmacro-step-edit-map "q" 'quit)
767    (define-key kmacro-step-edit-map "d" 'skip)
768    (define-key kmacro-step-edit-map "i" 'insert)
769    (define-key kmacro-step-edit-map "I" 'insert-1)
770    (define-key kmacro-step-edit-map "r" 'replace)
771    (define-key kmacro-step-edit-map "R" 'replace-1)
772    (define-key kmacro-step-edit-map "a" 'append)
773    (define-key kmacro-step-edit-map "A" 'append-1)
774    
775    (defvar kmacro-step-edit-prefix-commands
776      '(universal-argument universal-argument-more universal-argument-minus
777                           digit-argument negative-argument)
778      "Commands which builds up a prefix arg for the current command")
779    
780    (defun kmacro-step-edit-prompt (macro index)
781      ;; Show step-edit prompt
782      (let ((keys (and (not kmacro-step-edit-appending)
783                       index (substring macro index executing-macro-index)))
784            (future (and (not kmacro-step-edit-appending)
785                         (substring macro executing-macro-index)))
786            (message-log-max nil)
787            (curmsg (current-message)))
788    
789        ;; TODO: Scroll macro if max-mini-window-height is too small.
790        (message (concat
791                  (format "Macro: %s%s%s%s%s\n"
792                          (format-kbd-macro kmacro-step-edit-new-macro 1)
793                          (if (and kmacro-step-edit-new-macro (> (length kmacro-step-edit-new-macro) 0)) " " "")
794                          (propertize (if keys (format-kbd-macro keys)
795                                        (if kmacro-step-edit-appending "<APPEND>" "<INSERT>")) 'face 'region)
796                          (if future " " "")
797                          (if future (format-kbd-macro future) ""))
798                  (cond
799                   ((minibufferp)
800                    (format "%s\n%s\n"
801                              (propertize "\
802                             minibuffer                             " 'face 'header-line)
803                              (buffer-substring (point-min) (point-max))))
804                   (curmsg
805                    (format "%s\n%s\n"
806                            (propertize "\
807                             echo area                              " 'face 'header-line)
808                            curmsg))
809                   (t ""))
810                  (if keys
811                      (format "%s\n%s%s %S [yn iIaArR C-k kq!] "
812                              (propertize "\
813    --------------Step Edit Keyboard Macro  [?: help]---------------" 'face 'mode-line)
814                              (if kmacro-step-edit-help "\
815     Step: y/SPC: execute next,  d/n/DEL: skip next,  f: skip but keep
816           TAB: execute while same,  ?: toggle help
817     Edit: i: insert,  a: append,  r: replace, I/A/R: one sequence
818     End:  !/c: execute rest,  C-k: skip rest and save,  q/C-g: quit
819    ----------------------------------------------------------------
820    " "")
821                              (propertize "Next command:" 'face 'bold)
822                              this-command)
823                    (propertize
824                     (format "Type key sequence%s to insert and execute%s: "
825                             (if (numberp kmacro-step-edit-inserting) ""  "s")
826                             (if (numberp kmacro-step-edit-inserting) ""  "[end with C-j]"))
827                     'face 'bold))))))
828    
829    (defun kmacro-step-edit-query ()
830      ;; Pre-command hook function for step-edit in "command" mode
831      (let ((resize-mini-windows t)
832            (max-mini-window-height kmacro-step-edit-mini-window-height)
833            act restore-index next-index)
834    
835        ;; Handle commands which reads additional input using read-char.
836        (cond
837         ((and (eq this-command 'quoted-insert)
838               (not (eq kmacro-step-edit-action t)))
839          ;; Find the actual end of this key sequence.
840          ;; Must be able to backtrack in case we actually execute it.
841          (setq restore-index executing-macro-index)
842          (let (unread-command-events)
843            (quoted-insert 0)
844            (when unread-command-events
845              (setq executing-macro-index (- executing-macro-index (length unread-command-events))
846                    next-index executing-macro-index)))))
847    
848        ;; Query the user; stop macro exection temporarily
849        (let ((macro executing-kbd-macro)
850              (executing-kbd-macro nil)
851              (defining-kbd-macro nil))
852    
853          ;; Any action requested by previous command
854          (cond
855           ((eq kmacro-step-edit-action t)  ;; Reentry for actual command @ end of prefix arg.
856            (cond
857             ((eq this-command 'quoted-insert)
858              (clear-this-command-keys) ;; recent-keys actually
859              (let (unread-command-events)
860                (quoted-insert (prefix-numeric-value current-prefix-arg))
861                (setq kmacro-step-edit-new-macro
862                      (vconcat kmacro-step-edit-new-macro (recent-keys)))
863                (when unread-command-events
864                  (setq kmacro-step-edit-new-macro
865                        (substring kmacro-step-edit-new-macro 0 (- (length unread-command-events)))
866                        executing-macro-index (- executing-macro-index (length unread-command-events)))))
867              (setq current-prefix-arg nil
868                    prefix-arg nil)
869              (setq act 'ignore))
870             (t
871              (setq act 'act)))
872            (setq kmacro-step-edit-action nil))
873           ((eq this-command kmacro-step-edit-action)  ;; TAB -> activate while same command
874            (setq act 'act))
875           (t
876            (setq kmacro-step-edit-action nil)))
877    
878          ;; Handle prefix arg, or query user
879          (cond
880           (act act) ;; set above
881           ((memq this-command kmacro-step-edit-prefix-commands)
882            (unless kmacro-step-edit-prefix-index
883              (setq kmacro-step-edit-prefix-index kmacro-step-edit-key-index))
884            (setq act 'universal-argument))
885           ((eq this-command 'universal-argument-other-key)
886            (setq act 'universal-argument))
887           (t
888            (kmacro-step-edit-prompt macro (or kmacro-step-edit-prefix-index kmacro-step-edit-key-index))
889            (setq act (lookup-key kmacro-step-edit-map
890                                  (vector (with-current-buffer (current-buffer) (read-event))))))))
891    
892        ;; Resume macro execution and perform the action
893        (cond
894         ((eq act 'universal-argument)
895          nil)
896         ((cond
897           ((eq act 'act)
898            t)
899           ((eq act 'act-repeat)
900            (setq kmacro-step-edit-action this-command)
901            t)
902           ((eq act 'quit)
903            (setq kmacro-step-edit-replace nil)
904            (setq kmacro-step-edit-active 'ignore)
905            nil)
906           ((eq act 'skip)
907            (setq kmacro-step-edit-prefix-index nil)
908            nil)
909           ((eq act 'skip-keep)
910            (setq this-command 'ignore)
911            t)
912           ((eq act 'skip-rest)
913            (setq kmacro-step-edit-active 'ignore)
914            nil)
915           ((eq act 'automatic)
916            (setq kmacro-step-edit-active nil)
917            (setq act t)
918            t)
919           ((member act '(insert-1 insert))
920            (setq executing-macro-index (or kmacro-step-edit-prefix-index kmacro-step-edit-key-index))
921            (setq kmacro-step-edit-inserting (if (eq act 'insert-1) 1 t))
922            nil)
923           ((member act '(replace-1 replace))
924            (setq kmacro-step-edit-inserting (if (eq act 'replace-1) 1 t))
925            (setq kmacro-step-edit-prefix-index nil)
926            (if (= executing-macro-index (length executing-kbd-macro))
927                (setq executing-kbd-macro (vconcat executing-kbd-macro [nil])
928                      kmacro-step-edit-appending t))
929            nil)
930           ((member act '(append-1 append))
931            (setq kmacro-step-edit-inserting (if (eq act 'append-1) 1 t))
932            (if (= executing-macro-index (length executing-kbd-macro))
933                (setq executing-kbd-macro (vconcat executing-kbd-macro [nil])
934                      kmacro-step-edit-appending t))
935            t)
936           ((eq act 'help)
937            (setq executing-macro-index (or kmacro-step-edit-prefix-index kmacro-step-edit-key-index))
938            (setq kmacro-step-edit-help (not kmacro-step-edit-help))
939            nil)
940           (t ;; Ignore unknown responses
941            (setq executing-macro-index (or kmacro-step-edit-prefix-index kmacro-step-edit-key-index))
942            nil))
943          (if (> executing-macro-index (or kmacro-step-edit-prefix-index kmacro-step-edit-key-index))
944              (setq kmacro-step-edit-new-macro
945                    (vconcat kmacro-step-edit-new-macro
946                             (substring executing-kbd-macro
947                                        (or kmacro-step-edit-prefix-index kmacro-step-edit-key-index)
948                                        (if (eq act t) nil executing-macro-index)))
949                    kmacro-step-edit-prefix-index nil))
950          (if restore-index
951              (setq executing-macro-index restore-index)))
952         (t
953          (setq this-command 'ignore)))
954        (setq kmacro-step-edit-key-index next-index)))
955    
956    (defun kmacro-step-edit-insert ()
957      ;; Pre-command hook function for step-edit in "insert" mode
958      (let ((resize-mini-windows t)
959            (max-mini-window-height kmacro-step-edit-mini-window-height)
960            (macro executing-kbd-macro)
961            (executing-kbd-macro nil)
962            (defining-kbd-macro nil)
963            cmd keys next-index)
964        (setq executing-macro-index (or kmacro-step-edit-prefix-index kmacro-step-edit-key-index)
965              kmacro-step-edit-prefix-index nil)
966        (kmacro-step-edit-prompt macro nil)
967        ;; Now, we have read a key sequence from the macro, but we don't want
968        ;; to execute it yet.  So push it back and read another sequence.
969        (reset-this-command-lengths)
970        (setq keys (read-key-sequence nil nil nil nil t))
971        (setq cmd (key-binding keys t nil))
972        (if (cond
973             ((null cmd)
974              t)
975             ((eq cmd 'quoted-insert)
976              (clear-this-command-keys) ;; recent-keys actually
977              (quoted-insert (prefix-numeric-value current-prefix-arg))
978              (setq current-prefix-arg nil
979                    prefix-arg nil)
980              (setq keys (vconcat keys (recent-keys)))
981              (when (numberp kmacro-step-edit-inserting)
982                (setq kmacro-step-edit-inserting nil)
983                (when unread-command-events
984                  (setq keys (substring keys 0 (- (length unread-command-events)))
985                        executing-macro-index (- executing-macro-index (length unread-command-events))
986                        next-index executing-macro-index
987                        unread-command-events nil)))
988              (setq cmd 'ignore)
989              nil)
990             ((memq cmd kmacro-step-edit-prefix-commands)
991              (setq universal-argument-num-events 0)
992              (reset-this-command-lengths)
993              nil)
994             ((eq cmd 'universal-argument-other-key)
995              (setq kmacro-step-edit-action t)
996              (setq universal-argument-num-events 0)
997              (reset-this-command-lengths)
998              (if (numberp kmacro-step-edit-inserting)
999                  (setq kmacro-step-edit-inserting nil))
1000              nil)
1001             ((numberp kmacro-step-edit-inserting)
1002              (setq kmacro-step-edit-inserting nil)
1003              nil)
1004             ((equal keys "\C-j")
1005              (setq kmacro-step-edit-inserting nil)
1006              (setq kmacro-step-edit-action nil)
1007              ;; Forget any (partial) prefix arg from next command
1008              (setq kmacro-step-edit-prefix-index nil)
1009              (reset-this-command-lengths)
1010              (setq overriding-terminal-local-map nil)
1011              (setq universal-argument-num-events nil)
1012              (setq next-index kmacro-step-edit-key-index)
1013              t)
1014             (t nil))
1015            (setq this-command 'ignore)
1016          (setq this-command cmd)
1017          (if (memq this-command '(self-insert-command digit-argument))
1018              (setq last-command-char (aref keys (1- (length keys)))))
1019          (if keys
1020              (setq kmacro-step-edit-new-macro (vconcat kmacro-step-edit-new-macro keys))))
1021        (setq kmacro-step-edit-key-index next-index)))
1022    
1023    (defun kmacro-step-edit-pre-command ()
1024      (remove-hook 'post-command-hook 'kmacro-step-edit-post-command)
1025      (when kmacro-step-edit-active
1026        (cond
1027         ((eq kmacro-step-edit-active 'ignore)
1028          (setq this-command 'ignore))
1029         ((/= kmacro-step-edit-num-input-keys num-input-keys)
1030          (if kmacro-step-edit-inserting
1031              (kmacro-step-edit-insert)
1032            (kmacro-step-edit-query))
1033          (setq kmacro-step-edit-num-input-keys num-input-keys)
1034          (if (and kmacro-step-edit-appending (not kmacro-step-edit-inserting))
1035              (setq kmacro-step-edit-appending nil
1036                    kmacro-step-edit-active 'ignore)))))
1037      (when (eq kmacro-step-edit-active t)
1038        (add-hook 'post-command-hook 'kmacro-step-edit-post-command t)))
1039    
1040    (defun kmacro-step-edit-minibuf-setup ()
1041      (remove-hook 'pre-command-hook 'kmacro-step-edit-pre-command t)
1042      (when kmacro-step-edit-active
1043        (add-hook 'pre-command-hook 'kmacro-step-edit-pre-command nil t)))
1044    
1045    (defun kmacro-step-edit-post-command ()
1046      (remove-hook 'pre-command-hook 'kmacro-step-edit-pre-command)
1047      (when kmacro-step-edit-active
1048        (add-hook 'pre-command-hook 'kmacro-step-edit-pre-command nil nil)
1049        (if kmacro-step-edit-key-index
1050            (setq executing-macro-index kmacro-step-edit-key-index)
1051          (setq kmacro-step-edit-key-index executing-macro-index))))  
1052    
1053    
1054    (defun kmacro-step-edit-macro ()
1055      "Step edit and execute last keyboard macro.
1056    
1057    To customize possible responses, change the \"bindings\" in `kmacro-step-edit-map'."
1058      (interactive)
1059      (let ((kmacro-step-edit-active t)
1060            (kmacro-step-edit-new-macro "")
1061            (kmacro-step-edit-inserting nil)
1062            (kmacro-step-edit-appending nil)
1063            (kmacro-step-edit-replace t)
1064            (kmacro-step-edit-prefix-index nil)
1065            (kmacro-step-edit-key-index 0)
1066            (kmacro-step-edit-action nil)
1067            (kmacro-step-edit-help nil)
1068            (kmacro-step-edit-num-input-keys num-input-keys)
1069            (pre-command-hook pre-command-hook)
1070            (post-command-hook post-command-hook)
1071            (minibuffer-setup-hook minibuffer-setup-hook))
1072        (add-hook 'pre-command-hook 'kmacro-step-edit-pre-command nil)
1073        (add-hook 'post-command-hook 'kmacro-step-edit-post-command t)
1074        (add-hook 'minibuffer-setup-hook 'kmacro-step-edit-minibuf-setup t)
1075        (call-last-kbd-macro nil nil)
1076        (if kmacro-step-edit-replace
1077            (setq last-kbd-macro kmacro-step-edit-new-macro))))
1078    
1079  (provide 'kmacro)  (provide 'kmacro)
1080  ;;; kmacro.el ends here  ;;; kmacro.el ends here

Legend:
Removed from v.1.7  
changed lines
  Added in v.1.8

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26