my-emacs-d/lisp/gpolonkai/nav-utils.el

89 lines
2.8 KiB
EmacsLisp

;;; gpolonkai/nav-utils.el --- Navigation utilities
;;;
;;; SPDX-License-Identifier: GPL-3.0-or-later
;;; Copyright © 2025 Gergely Polonkai
;;; This library is free software; you can redistribute it and/or
;;; modify it under the terms of the GNU Lesser General Public
;;; License as published by the Free Software Foundation; either
;;; version 3 of the License, or (at your option) any later version.
;;;
;;; This library is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;;; Lesser General Public License for more details.
;;;
;;; You should have received a copy of the GNU Lesser General Public
;;; License along with this library; if not, write to the
;;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;;; Boston, MA 02111-1307, USA.
;;;
;;; Commentary:
;;;
;;; Utility functions stolen from the Internet or written by me
;;;
;;; Code:
;; Inspired by Bozhidar Batsov's solution:
;; http://emacsredux.com/blog/2013/05/22/smarter-navigation-to-the-beginning-of-a-line/
(defun gpolonkai/move-to-beginning-of-line ()
"Move to different beginnings of the line.
These are, in order:
- beginning of the visual line if `visual-line-mode' is active,
- the first non-whitespace (indentation),
- the actual beginning of the line.
This function will jump between the first character and the
indentation if used multiple times."
(interactive)
(let ((last-pos (point)))
(when visual-line-mode
(beginning-of-visual-line))
(when (= (point) last-pos)
(back-to-indentation))
(when (= (point) last-pos)
(beginning-of-line))
(when (and (eq major-mode 'org-mode)
(= (point) last-pos))
(org-beginning-of-line))
(when (= (point) last-pos)
(back-to-indentation))))
(defun gpolonkai/move-to-end-of-line ()
"Move to the end of the line.
If `visual-line-mode' is active, jump to the end of the visual
line first. Then jump to the actual end of the line."
(interactive)
(let ((last-pos (point)))
(when visual-line-mode
(end-of-visual-line))
(when (= (point) last-pos)
(end-of-line))
(when (and (eq major-mode 'org-mode)
(= (point) last-pos))
(org-end-of-line))))
(defun gpolonkai/goto-next-char (chr)
"Move toe the next occurence of CHR on the same line."
(interactive "c")
(when (search-forward (char-to-string chr) (pos-eol) t)
(backward-char)))
(defun gpolonkai/beginning-of-next-word ()
"Move to the beginning of the next word."
(interactive)
(let ((current-point (point)))
(forward-word 1)
(backward-word 1)
(when (<= (point) current-point)
(forward-word 2)
(backward-word 1))))
(provide 'gpolonkai/nav-utils)
;;; nav-utils.el ends here