Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use last-command-event and delete unnecessary code #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 19 additions & 27 deletions dmacro.el
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
:type 'key-sequence
:group 'dmacro)



;;; Functions

Expand All @@ -59,28 +60,16 @@
(defalias 'dmacro--user-error
(eval-when-compile (if (fboundp 'user-error) #'user-error #'message)))

(defun dmacro--ensure-keycode (key)
"Return key code or symbol `KEY' by `recent-keys'."
(if (integerp key)
key
(cl-case key
('backspace ?\C-h)
('tab ?\C-i)
('enter ?\C-m)
('return ?\C-m)
('escape ?\C-\[)
('delete ?\C-?)
(t key))))

(defun dmacro-get ()
"Get repeated sequence."
(let ((keys (vconcat dmacro-key dmacro-key))
(rkeys (recent-keys)) arry)
(let* ((lce (vector last-command-event))
(keys (vconcat lce lce))
(rkeys (recent-keys)) arry)
(if (equal keys (cl-subseq rkeys (- (length keys))))
(progn
(setq dmacro--input-subkeys nil)
dmacro--input-keys)
(setq arry (dmacro-search (cl-subseq rkeys 0 (- (length dmacro-key)))))
(setq arry (dmacro-search (cl-subseq rkeys 0 (- (length lce))) lce))
(if (null arry)
(setq dmacro--input-keys nil)
(let ((s1 (car arry))
Expand All @@ -90,29 +79,29 @@
(setq last-kbd-macro dmacro--input-keys)
(if (equal s1 "") dmacro--input-keys s1))))))

(defun dmacro-search (array)
(defun dmacro-search (array key)
"Search `ARRAY'."
(let* ((arry (reverse array))
(sptr 1)
(dptr0 (dmacro-array-search (cl-subseq arry 0 sptr) arry sptr))
(dptr dptr0)
maxptr)
(while (and dptr0
(not (dmacro-array-search dmacro-key (cl-subseq arry sptr dptr0))))
(not (dmacro-array-search key (cl-subseq arry sptr dptr0))))
(when (= dptr0 sptr)
(setq maxptr sptr))
(setq sptr (1+ sptr))
(setq dptr dptr0)
(setq dptr0 (dmacro-array-search (cl-subseq arry 0 sptr) arry sptr)))
(if (null maxptr)
(let ((predict-arry (reverse (cl-subseq arry (1- sptr) dptr))))
(if (dmacro-array-search dmacro-key predict-arry)
(if (dmacro-array-search key predict-arry)
nil
(cons predict-arry (reverse (cl-subseq arry 0 (1- sptr))))))
(cons "" (reverse (cl-subseq arry 0 maxptr))))))

(defun dmacro-array-search (pat arry &optional start)
"Search pattern `PAT' by `ARRY'. `START'."
"Search pattern `PAT' by `ARRY' from `START'."
(let* ((len (length pat))
(max (- (length arry) len))
(p (or start 0))
Expand All @@ -125,19 +114,22 @@

;;; Main

;;;###autoload
(defun dmacro-exec ()
"Repeated detection and execution of key operation."
(interactive)
(let ((keys (dmacro-get)))
(if keys
(execute-kbd-macro keys)
(dmacro--user-error "There is no repetitive operation"))))

;;;###autoload
(define-minor-mode dmacro-mode
"Dynamic Macro"
:group 'dmacro
:lighter " dmac"
:keymap
`((,dmacro-key
. (lambda ()
(interactive)
(let ((keys (dmacro-get)))
(if keys
(execute-kbd-macro keys)
(dmacro--user-error "There is no repetitive operation")))))))
`((,dmacro-key . dmacro-exec)))

;;;###autoload
(define-globalized-minor-mode global-dmacro-mode dmacro-mode
Expand Down
Loading