Skip to content

Instantly share code, notes, and snippets.

@jidaikobo-shibata
Last active August 10, 2024 23:36
Show Gist options
  • Save jidaikobo-shibata/467f4302c002049bfb95511bd21cdbe7 to your computer and use it in GitHub Desktop.
Save jidaikobo-shibata/467f4302c002049bfb95511bd21cdbe7 to your computer and use it in GitHub Desktop.
Emacs(Elisp): forward/backward-wordだと、移動距離が微妙に大きい。単語境界も微妙だった。ので、ちょっと変質的にカーソル移動をカスタマイズ。
;;; ------------------------------------------------------------
;;; 自分好みのカーソル移動 (2)
;; gist-description: Emacs(Elisp): forward/backward-wordだと、移動距離が微妙に大きい。単語境界も微妙だった。ので、カーソル移動をちょっとカスタマイズ。
;; gist-id: 467f4302c002049bfb95511bd21cdbe7
;; gist-name: skip-chars-(forward|backward)-dwim.el
;; gist-private: nil
(defun my-skip-chars (forward)
"Skip characters based on the direction FORWARD."
(let ((start (point))
(char-sets '("a-zA-Z0-9"
" "
"()"
"<>"
"ぁ-んー"
"ァ-ヶー"
"亜-黑ー")))
(dolist (char-set char-sets)
(when (eq start (point))
(if forward
(skip-chars-forward char-set)
(skip-chars-backward char-set))))
;; Move one character if still at the start point
(when (eq start (point))
(if forward
(forward-char 1)
(backward-char 1)))))
(defun skip-chars-forward-dwim ()
"Skip characters forward in a DWIM manner."
(interactive "^")
(my-skip-chars t))
(defun skip-chars-backward-dwim ()
"Skip characters backward in a DWIM manner."
(interactive "^")
(my-skip-chars nil))
(global-set-key (kbd "<M-left>") 'skip-chars-backward-dwim)
(global-set-key (kbd "<M-right>") 'skip-chars-forward-dwim)
;;; ------------------------------------------------------------
;;; 次/前のwordbreakへ
;; gist-description: Emacs(Elisp): forward/backward-wordだと、移動距離が微妙に大きいので、単語境界でひっかかるように。
;; gist-id: 467f4302c002049bfb95511bd21cdbe7
;; gist-name: move-to-next(previous)-word-break.el
;; gist-private: nil
;; thx http://d.hatena.ne.jp/h1mesuke/20070803/p1
(defun move-to-next-word-break (&optional arg)
"Move point forward ARG word breaks (backward if ARG is negative)."
(interactive "^P")
(setq arg (if arg (prefix-numeric-value arg) 1))
(if (< arg 0)
(move-to-previous-word-break (- arg))
(while (and (> arg 0)
(< (point) (point-max))
(progn (forward-char)
(re-search-forward "\\b" nil t)))
(setq arg (1- arg)))))
(defun move-to-previous-word-break (&optional arg)
"Move point backward ARG word breaks (forward if ARG is negative)."
(interactive "^P")
(setq arg (if arg (prefix-numeric-value arg) 1))
(if (< arg 0)
(move-to-next-word-break (- arg))
(while (and (> arg 0)
(> (point) (point-min))
(progn (backward-char)
(re-search-backward "\\b" nil t)))
(setq arg (1- arg)))))
(global-set-key (kbd "<M-left>") 'move-to-previous-word-break)
(global-set-key (kbd "<M-right>") 'move-to-next-word-break)
;;; ------------------------------------------------------------
;;; 自分好みのカーソル移動
;; gist-description: Emacs(Elisp): forward/backward-wordだと、移動距離が微妙に大きい。単語境界も微妙だった。ので、ちょっと変質的にカーソル移動をカスタマイズ。
;; gist-id: 467f4302c002049bfb95511bd21cdbe7
;; gist-name: skip-chars-(forward|backward)-dwim.el
;; gist-private: nil
(defun skip-chars-forward-dwim ()
"Skip chars forward dwim."
(interactive "^")
(let ((start (point)))
(if (eq last-command this-command)
(skip-chars-forward "a-zA-Z0-9_-")
(skip-chars-forward "a-zA-Z0-9_"))
(when (eq start (point))
(skip-syntax-forward " "))
(when (eq start (point))
(skip-syntax-forward "()"))
(when (eq start (point))
(skip-syntax-forward "<>"))
(when (eq start (point))
(skip-chars-forward "-"))
(when (eq start (point))
(skip-chars-forward "ぁ-んー"))
(when (eq start (point))
(skip-chars-forward "ァ-ヶー"))
(when (eq start (point))
(skip-chars-forward "亜-黑ー"))
(when (eq start (point))
(goto-char (+ (point) 1)))))
(defun skip-chars-backward-dwim ()
"Skip chars backward dwim."
(interactive "^")
(let ((start (point)))
(if (eq last-command this-command)
(skip-chars-backward "a-zA-Z0-9_-")
(skip-chars-backward "a-zA-Z0-9_"))
(when (eq start (point))
(skip-syntax-backward " "))
(when (eq start (point))
(skip-syntax-backward "()"))
(when (eq start (point))
(skip-syntax-backward "<>"))
(when (eq start (point))
(skip-chars-backward "-"))
(when (eq start (point))
(skip-chars-backward "ぁ-んー"))
(when (eq start (point))
(skip-chars-backward "ァ-ヶー"))
(when (eq start (point))
(skip-chars-backward "亜-黑ー"))
(when (eq start (point))
(goto-char (- (point) 1)))))
(global-set-key (kbd "<M-left>") 'skip-chars-backward-dwim)
(global-set-key (kbd "<M-right>") 'skip-chars-forward-dwim)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment