| 1 |
;;; bg-build-util.el --- bg-build-util.el -*- lexical-binding: t; -*- |
| 2 |
;; Copyright (C) 2007 Vesa Karvonen |
| 3 |
;; |
| 4 |
;; MLton is released under a HPND-style license. |
| 5 |
;; See the file MLton-LICENSE for details. |
| 6 |
|
| 7 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 8 |
;; Utils |
| 9 |
|
| 10 |
(defun bg-build-cons-once (entry list) |
| 11 |
(cons entry (remove* entry list :test (function equal)))) |
| 12 |
|
| 13 |
(defun bg-build-flatmap (fn list) |
| 14 |
(apply (function append) (mapcar fn list))) |
| 15 |
|
| 16 |
(defun bg-build-remove-from-assoc (alist key) |
| 17 |
(remove* |
| 18 |
nil alist |
| 19 |
:test (function |
| 20 |
(lambda (_ key-value) |
| 21 |
(equal key (car key-value)))))) |
| 22 |
|
| 23 |
(defun bg-build-replace-in-assoc (alist key value) |
| 24 |
(cons (cons key value) |
| 25 |
(bg-build-remove-from-assoc alist key))) |
| 26 |
|
| 27 |
(defun bg-build-assoc-cdr (key alist) |
| 28 |
"Same as (cdr (assoc key alist)) except that doesn't attempt to call cdr |
| 29 |
on nil." |
| 30 |
(let ((key-value (assoc key alist))) |
| 31 |
(when key-value |
| 32 |
(cdr key-value)))) |
| 33 |
|
| 34 |
(defun bg-build-const (value) |
| 35 |
"Returns a function that returns the given value." |
| 36 |
(let ((value value)) |
| 37 |
(lambda (&rest _) |
| 38 |
value))) |
| 39 |
|
| 40 |
(defun bg-build-kill-current-buffer () |
| 41 |
"Kills the current buffer." |
| 42 |
(interactive) |
| 43 |
(kill-buffer (current-buffer))) |
| 44 |
|
| 45 |
(defun bg-build-make-hash-table () |
| 46 |
"Makes a hash table with `equal' semantics." |
| 47 |
(make-hash-table :test 'equal :size 1)) |
| 48 |
|
| 49 |
(defun bg-build-point-at-current-line () |
| 50 |
"Returns point at the beginning of the current line." |
| 51 |
(save-excursion |
| 52 |
(beginning-of-line) |
| 53 |
(point))) |
| 54 |
|
| 55 |
(defun bg-build-current-line () |
| 56 |
"Returns the current line number counting from 1." |
| 57 |
(+ 1 (count-lines 1 (bg-build-point-at-current-line)))) |
| 58 |
|
| 59 |
(defun bg-build-time-to-double (time) |
| 60 |
"Converts a time to a double." |
| 61 |
(+ (* (car time) 65536.0) |
| 62 |
(cadr time) |
| 63 |
(if (cddr time) (* (caddr time) 1e-06) 0))) |
| 64 |
|
| 65 |
(defun bg-build-attr-newer? (attr1 attr2) |
| 66 |
"Returns non-nil iff the modification time of `attr1' is later than the |
| 67 |
modification time of `attr2'. Note that this also returns nil when either |
| 68 |
one of the modification times is nil." |
| 69 |
(and attr1 attr2 |
| 70 |
(> (bg-build-time-to-double (nth 5 attr1)) |
| 71 |
(bg-build-time-to-double (nth 5 attr2))))) |
| 72 |
|
| 73 |
(defun bg-build-pos-to-point (pos) |
| 74 |
"Returns the value of point in the current buffer at the position given |
| 75 |
as a (line . col) pair." |
| 76 |
(save-excursion |
| 77 |
(goto-line (car pos)) |
| 78 |
(+ (point) (cdr pos)))) |
| 79 |
|
| 80 |
(defun bg-build-point-to-pos (point) |
| 81 |
"Returns the position as a (line . col) pair corresponding to the |
| 82 |
specified point in the current buffer." |
| 83 |
(save-excursion |
| 84 |
(goto-char point) |
| 85 |
(beginning-of-line) |
| 86 |
(let ((line (+ (count-lines 1 (point)) 1)) |
| 87 |
(col (- point (point)))) |
| 88 |
(cons line col)))) |
| 89 |
|
| 90 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 91 |
|
| 92 |
(provide 'bg-build-util) |