Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Move emacs cursor like in the normal editor!
(setq my-own/separators-regexp "\\([\-'\"(){};:,.\\/?!@#%&*+=\]\\)\\|\\(\\[\\)\\|\\(\\]\\)\\|\\(\\s-\\)\\|\\(\\s_\\)\\|\\(\\`\\)\\|\\(\\'\\)\\|\\(\\^\\)\\|\\(\n\\)")
(defun my-own/position-normalize-forward(saved-pos my-pos)
"Normalize position after forward move"
(if (>= (- my-pos saved-pos) 2)
(progn
;going to my-pos
(goto-char my-pos)
(if (not (eq my-pos (point-max)))
(progn
;also backward-to-separator to fix position
(my-own/backward-to-separator))))
(progn
;going to my-pos in the usual way
(goto-char my-pos))))
(defun my-own/forward-to-separator()
"Move to the next separator like in the every NORMAL editor"
(interactive "^")
(let ((saved-pos (point))
(my-pos (re-search-forward my-own/separators-regexp)))
(if (eq my-pos 1)
(progn
(goto-char (+ my-pos 1))
(setf my-pos (re-search-forward my-own/separators-regexp))
(my-own/position-normalize-forward saved-pos my-pos))
(my-own/position-normalize-forward saved-pos my-pos))
))
(defun my-own/position-normalize-backward(saved-pos my-pos)
"Normalize position after backward move"
(if (>= (- saved-pos my-pos) 2)
(progn
(goto-char my-pos)
(if (not (eq my-pos 1))
(progn
(my-own/forward-to-separator))))
(progn
(goto-char my-pos))))
;;See how it works at my-own/position-normalize-forward
(defun my-own/backward-to-separator()
"Move to the previous separator like in the every NORMAL editor"
(interactive "^")
(let ((saved-pos (point))
(my-pos (re-search-backward my-own/separators-regexp)))
(if (eq my-pos (point-max))
(progn
(goto-char (- my-pos 1))
(setf my-pos (re-search-backward my-own/separators-regexp))
(my-own/position-normalize-backward saved-pos my-pos))
(my-own/position-normalize-backward saved-pos my-pos))
))
(defun my-own/position-normalize-backspace(saved-pos my-pos)
(print (format "saved-pos: %s my-pos: %s" saved-pos my-pos))
(if (>= (- saved-pos my-pos) 2)
(progn
(print "have to fix")
(goto-char my-pos)
(if (not (eq my-pos 1))
(progn
(print "calling forward-to-separator to normalize position")
(my-own/forward-to-separator)
(let ((new-pos (point)))
(print (format "new-pos: %s" new-pos))
(delete-region new-pos saved-pos)))
(progn
(print "do not need to normalize position because we are at the begining")
(delete-region 1 saved-pos))))
(progn
(print "Usual move")
(goto-char my-pos)
(let ((new-pos (point)))
(print (format "new-pos: %s" new-pos))
(delete-region new-pos saved-pos)))
))
(defun my-own/backspace-to-separator()
"Erase characters to the first separator"
(interactive)
(let ((saved-pos (point))
(my-pos (re-search-backward my-own/separators-regexp)))
(if (eq my-pos (point-max))
(progn
(goto-char (- my-pos 1))
(setf my-pos (re-search-backward my-own/separators-regexp))
(my-own/position-normalize-backspace saved-pos my-pos))
(my-own/position-normalize-backspace saved-pos my-pos))
))
(global-set-key (kbd "C-<right>") 'my-own/forward-to-separator)
(global-set-key (kbd "C-<left>") 'my-own/backward-to-separator)
(global-set-key (kbd "C-<backspace>") 'my-own/backspace-to-separator)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.