changeset 666: |
f15e0f021a64 |
parent: |
46e9425cf3c2
|
author: |
Richard Westhaver <ellis@rwest.io> |
date: |
Sun, 22 Sep 2024 22:13:44 -0400 |
permissions: |
-rw-r--r-- |
description: |
more elisp |
1 ;;; std/util.el --- standard utils -*- lexical-binding: t -*- 7 (defun group (source n) 8 "This is Paul Graham's group utility from On Lisp. 10 Group a list of arguments SOURCE by any provided grouping amount 14 (group (quote (foo 2 bar 4)) 2) ;=> ((foo 2) (bar 4)) 15 (group (quote (a b c d e f)) 3) ;=> ((a b c) (d e f)) 17 (when (zerop n) (error "zero length")) 18 (cl-labels ((rec (source acc) 19 (let ((rest (nthcdr n source))) 22 (cl-subseq source 0 n) 25 (cons source acc)))))) 26 (when source (rec source nil)))) 29 "Paul Graham's flatten utility from On Lisp. 31 Given a tree X, return all the leaves of the tree." 32 (cl-labels ((rec (x acc) 34 ((atom x) (cons x acc)) 37 (rec (cdr x) acc)))))) 40 (defun mkstr (&rest args) 41 "Paul Graham's mkstr utility from On Lisp. 43 Coerce ARGS into a single string and return it." 48 ((sequencep a) (setq s (concat s a))) 49 ((numberp a) (setq s(concat s (number-to-string a)))) 50 ((symbolp a) (setq s(concat s (symbol-name a)))))) 53 (defun symb (&rest args) 54 "Paul Graham's symb utility from On Lisp. 56 Concat ARGS and return a newly interned symbol." 57 (intern (apply #'mkstr args))) 60 (defun add-to-load-path (&rest paths) 61 "Add PATHS to `load-path'." 64 ((listp x) (mapc #'add-to-load-path x)) 65 ('_ (cl-pushnew x load-path)))) 68 (defmacro add-packages (&rest pkgs) 69 "add list of packages PKGS to `package-selected-packages'" 70 `(mapc (lambda (x) (add-to-list 'package-selected-packages x)) ',pkgs)) 72 (defun load-keys (&optional custom) 73 (let ((keydefs (or custom (concat user-emacs-directory "keys.el")))) 74 (load keydefs nil t))) 77 (defmacro when-sys= (name body) 78 "(when (string= (system-name) NAME) BODY)" 79 `(when ,(string= (system-name) name) ,body)) 81 (defun darwin-p () (string= system-type "darwin")) 82 (defun linux-p () (string= system-type "gnu/linux")) 84 (defun join-paths (root &rest dirs) 85 "helper function for joining strings to a path." 87 (cl-loop for dir in dirs do 88 (setq result (concat (file-name-as-directory result) dir))) 92 "Return a 3-element list with lines, words and characters in 93 region or whole buffer." 96 (start (if mark-active (region-beginning) (point-min))) 97 (end (if mark-active (region-end) (point-max)))) 100 (while (< (point) end) (if (forward-word 1) (setq n (1+ n))))) 101 (list (count-lines start end) n (- end start)))) 104 (defvar default-line-regexp-alist 105 '((empty . "[\s\t]*$") 106 (indent . "^[\s\t]+") 108 (list . "^\\([\s\t#*+]+\\|[0-9]+[^\s]?[).]+\\)") 109 (heading . "^[=-]+")) 110 "Alist of regexp types used by `default-line-regexp-p'.") 112 (defun default-line-regexp-p (type &optional n) 113 "Test for TYPE on line. 114 TYPE is the car of a cons cell in 115 `default-line-regexp-alist'. It matches a regular 117 With optional N, search in the Nth line from point." 119 (goto-char (pos-bol)) 121 (or (beginning-of-line n) t) 124 (alist-get type default-line-regexp-alist)))))) 127 (defun format-iso-week-number (&optional date) 128 "format DATE as ISO week number with week days starting on 129 Monday. If DATE is nil use current date." 130 (let* ((week (format-time-string "%W" date)) 131 (prefix (if (= (length week) 1) 133 (concat prefix week))) 135 (defun last-day-of-year (&optional date) 136 "Return the last day of the year as time." 137 (encode-time 0 0 0 31 12 (nth 5 (decode-time 138 (or date (current-time)))))) 140 (defun last-day-of-month (&optional date) 141 "Return the last day of month as time." 142 (let* ((now (decode-time (or date (current-time)))) 145 (last-day-of-month (calendar-last-day-of-month month year))) 146 (encode-time 0 0 0 last-day-of-month month year))) 148 (defun last-day-of-week (&optional date) 149 "Return the last day of the week as time." 150 (let* ((now (or date (current-time))) 151 (datetime (decode-time now)) 152 (dow (nth 6 datetime))) 153 (time-add now (days-to-time (- 7 dow))))) 155 (defun first-day-of-week (&optional date) 156 "Return the first day of the week as time." 157 (let* ((now (or date (current-time))) 158 (datetime (decode-time now)) 159 (dow (nth 6 datetime))) 160 (time-subtract now (days-to-time dow)))) 163 (defun hash-table-alist (table) 164 "Returns an association list containing the keys and values of hash table 167 (maphash (lambda (k v) 168 (push (cons k v) alist)) 174 (defun kill-emacs-restart () 176 (server-force-delete) 179 (define-key special-event-map [sigusr1] 'kill-emacs-restart) 181 (defun upgrade-emacs (&optional ask) 183 (package-refresh-contents) 184 (package-install-selected-packages (not ask)) 185 (package-upgrade-all ask))