Move utility functions from configuration.org to lisp/gpolonkai/utilities.el

This commit is contained in:
Gergely Polonkai 2025-05-29 19:11:37 +02:00
parent c29882eacf
commit 29483f463a
No known key found for this signature in database
GPG Key ID: 38F402C8471DDE93
3 changed files with 104 additions and 99 deletions

View File

@ -2,105 +2,6 @@
This is a collection of functions and commands i wrote or stole from all around the internet.
** Utilities
*** Make a backup filename under ~user-emacs-cache-directory~
Taken from [[http://ergoemacs.org/emacs/emacs_set_backup_into_a_directory.html][Xahs site]].
#+begin_src emacs-lisp
(defun xah/backup-file-name (fpath)
"Return a new file path for FPATH under `user-emacs-cache-directory'"
(let* ((backup-root-dir (expand-file-name "backup" user-emacs-cache-directory))
(file-path (replace-regexp-in-string "[A-Za-z]:" "" fpath))
(backup-file-path (replace-regexp-in-string "//" "/" (concat backup-root-dir file-path "~"))))
(make-directory (file-name-directory backup-file-path) (file-name-directory backup-file-path))
backup-file-path))
#+end_src
*** Check if were running under Termux
We need to do things differently, if so.
Theres probably a better way, though, other than checking the path of our home directory.
#+begin_src emacs-lisp
(defun gpolonkai/termux-p ()
"Check if Emacs is running under Termux."
(string-match-p
(regexp-quote "/com.termux/")
(expand-file-name "~")))
#+end_src
*** Find the first number on line
#+begin_src emacs-lisp
(defun gpolonkai/find-number-on-line ()
"Find the first number on the current line."
(save-excursion
(without-restriction
(goto-char (pos-bol))
(when (re-search-forward "[0-9]+" (pos-eol) t)
(number-at-point)))))
#+end_src
*** Round number at point to the given decimals
#+begin_src emacs-lisp
(defun gpolonkai/round-number-at-point-to-decimals (decimal-count)
(interactive "NDecimal count: ")
(let ((mult (expt 10 decimal-count)))
(replace-match (number-to-string
(/
(fround
(*
mult
(number-at-point)))
mult)))))
#+end_src
*** Leave ~isearch~ at the other end of the matched string
From [[http://endlessparentheses.com/leave-the-cursor-at-start-of-match-after-isearch.html][Endless Parentheses]].
#+begin_src emacs-lisp
(defun ep/isearch-exit-other-end ()
"Exit isearch, at the opposite end of the string."
(interactive)
(isearch-exit)
(goto-char isearch-other-end))
#+end_src
*** Mark the current match after leaving ~isearch~
From [[http://emacs.stackexchange.com/a/31321/507][Emacs SE]].
#+begin_src emacs-lisp
(defun e-se/isearch-exit-mark-match ()
"Exit isearch and mark the current match."
(interactive)
(isearch-exit)
(push-mark isearch-other-end)
(activate-mark))
#+end_src
*** Copy the prototype of the current C function
#+begin_src emacs-lisp
(defun gpolonkai/copy-func-prototype ()
"Copy the current function's prototype to the kill ring."
(interactive)
(save-excursion
(beginning-of-defun)
(let ((protocopy-begin (point)))
(forward-list)
(let ((protocopy-end (point)))
(kill-ring-save protocopy-begin protocopy-end)))))
#+end_src
** Window manipulation
*** Bury a window

View File

@ -56,6 +56,9 @@
(use-package bind-key)
;; Custom functions and commands
(load "gpolonkai/utilities")
;; I started moving my configuration to this Org file. Its easier to document this way.
(org-babel-load-file (expand-file-name "configuration.org" user-emacs-directory))

101
lisp/gpolonkai/utilities.el Normal file
View File

@ -0,0 +1,101 @@
;;; gpolonkai/utilities.el --- 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:
;; Make a backup filename under `user-emacs-cache-directory'
;;
;; Taken from http://ergoemacs.org/emacs/emacs_set_backup_into_a_directory.html
(defun xah/backup-file-name (fpath)
"Return a new file path for FPATH under `user-emacs-cache-directory'."
(let* ((backup-root-dir (expand-file-name "backup" user-emacs-cache-directory))
(file-path (replace-regexp-in-string "[A-Za-z]:" "" fpath))
(backup-file-path (replace-regexp-in-string "//" "/" (concat backup-root-dir file-path "~"))))
(make-directory (file-name-directory backup-file-path) (file-name-directory backup-file-path))
backup-file-path))
;; Check if were running under Termux
;;
;; We need to do things differently, if so. Theres probably a better way, though, other than checking the path of our
;; home directory.
(defun gpolonkai/termux-p ()
"Check if Emacs is running under Termux."
(string-match-p
(regexp-quote "/com.termux/")
(expand-file-name "~")))
(defun gpolonkai/find-number-on-line ()
"Find the first number on the current line."
(save-excursion
(without-restriction
(goto-char (pos-bol))
(when (re-search-forward "[0-9]+" (pos-eol) t)
(number-at-point)))))
(defun gpolonkai/round-number-at-point-to-decimals (decimal-count)
"Round number at point to the given decimals.
The number of decimals in the result is specified by DECIMAL-COUNT."
(interactive "NDecimal count: ")
(let ((mult (expt 10 decimal-count)))
(replace-match (number-to-string
(/
(fround
(*
mult
(number-at-point)))
mult)))))
;; From http://endlessparentheses.com/leave-the-cursor-at-start-of-match-after-isearch.html
(defun ep/isearch-exit-other-end ()
"Exit `isearch' at the opposite end of the string."
(interactive)
(isearch-exit)
(goto-char isearch-other-end))
;; From http://emacs.stackexchange.com/a/31321/507
(defun e-se/isearch-exit-mark-match ()
"Exit isearch and mark the current match."
(interactive)
(isearch-exit)
(push-mark isearch-other-end)
(activate-mark))
(defun gpolonkai/copy-func-prototype ()
"Copy the current function's prototype to the kill ring."
(interactive)
(save-excursion
(beginning-of-defun)
(let ((protocopy-begin (point)))
(forward-list)
(let ((protocopy-end (point)))
(kill-ring-save protocopy-begin protocopy-end)))))
(provide 'gpolonkai/utilities)
;;; utilities.el ends here