my-emacs-d/lisp/gpolonkai/utilities.el

102 lines
3.4 KiB
EmacsLisp
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

;;; 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