a0169575c38e7126e0004013438e519a7187781f
[lice.git] / global.lisp
bloba0169575c38e7126e0004013438e519a7187781f
1 (in-package :lice)
3 (defconstant +debug-v+ 1)
4 (defconstant +debug-vv+ 2)
5 (defconstant +debug-vvv+ 3)
7 (defparameter *debug-level* +debug-v+)
8 ;;(defparameter *debug-level* +debug-vvv+)
10 (defun dformat (lvl &rest fmt-args)
11 (when (>= *debug-level* lvl)
12 (with-open-file (f #p"/tmp/debug" :direction :output :if-exists :append
13 :if-does-not-exist :create)
14 (apply 'format f fmt-args))))
16 (defmacro verbose-body (&body body)
17 "Print each sexpr in BODY and its return value."
18 (let ((ret (gensym "RET")))
19 (loop for i in body
20 collect `(let ((,ret ,i))
21 (format t "~s => ~s~%" ,i ,ret)
22 ,ret))))
24 (defun last1 (l)
25 "Return the last elt of the list."
26 (car (last l)))
28 (defun nconc1 (list item)
29 "destructively append ITEM to the end of L"
30 (nconc list (list item)))
32 ;; (defun grow-vector (vector amt initial-element)
33 ;; "grow the vector's size by AMT elements"
34 ;; (adjust-array vector (+ (length vector) amt)
35 ;; :initial-element initial-element
36 ;; :fill-pointer t))
38 ;; (defun vector-append (vector1 vector2)
39 ;; "append vector2 to the end of vector1."
40 ;; (let ((len (length vector1)))
41 ;; (grow-vector vector1 (length vector2) (elt vector2 0))
42 ;; (replace vector1 vector2 :start1 (1+ len) :start2 1)))
44 (define-condition lice-condition ()
45 () (:documentation "The base condition for all lice related errors."))
47 ;; (defun fmt (fmt &rest args)
48 ;; "A movitz hack function. FORMAT basically doesn't work, so i use this to get around it."
49 ;; (let ((s (make-array 100 :fill-pointer 0 :element-type 'character)))
50 ;; (apply #'format s fmt args)
51 ;; s))
53 #+movitz
54 (defun read-from-string (string)
55 "Read the string and return an sexpr. This is a MOVITZ hack
56 because it doesn't have read-from-string."
57 (muerte::simple-read-from-string string))
59 ;;; Lisp function we like to have
61 (defun concat (&rest strings)
62 "Concatenate all the arguments and make the result a string.
63 The result is a string whose elements are the elements of all the arguments.
64 Each argument must be a string."
65 (apply 'concatenate 'string strings))
67 (defmacro while (test &body body)
68 "If TEST yields non-nil, eval BODY... and repeat.
69 The order of execution is thus TEST, BODY, TEST, BODY and so on
70 until TEST returns nil."
71 (if body
72 `(loop while ,test do ,@body)
73 `(loop while ,test)))
75 (defmacro check-number-coerce-marker (marker-var)
76 "Verify that MARKER-VAR is a number or if it's a marker then
77 set the var to the marker's position."
78 `(progn
79 (check-type ,marker-var (or number marker))
80 (when (typep ,marker-var 'marker)
81 (setf ,marker-var (marker-position ,marker-var)))))
83 (defun cdr-safe (object)
84 "Return the cdr of OBJECT if it is a cons cell, or else nil."
85 (when (consp object)
86 (cdr object)))
88 (defvar *quit-code* 7
89 "The terminal char code for the interrupt key.")
91 (defvar *inhibit-quit* nil
92 ;; XXX: this is not correct docs
93 "Non-nil inhibits C-g quitting from happening immediately.
94 Note that `quit-flag' will still be set by typing C-g,
95 so a quit will be signaled as soon as `inhibit-quit' is nil.
96 To prevent this happening, set `quit-flag' to nil
97 before making `inhibit-quit' nil.")
99 (defvar *waiting-for-input* nil
100 "T when we're waiting for .. input")
102 (defvar *quit-flag* nil
103 "Set to T when the user hit the quit key")
105 ;; XXX: get rid of this function and all callers
106 (defun assq (prop list)
107 "Return non-nil if key is `eq' to the car of an element of list.
108 The value is actually the first element of list whose car is key.
109 Elements of list that are not conses are ignored."
110 (assoc prop (remove-if 'listp list)))
112 (defmacro depricate (symbol refer-to)
113 "A macro to mark a symbol as depricated. This is done with
114 function in emacs whose purpose is better done another
115 way. For example, a set- function replaced by a setf function. "
116 `(setf (get (quote ,symbol) :depricated) (quote ,refer-to)))
118 (defun read-string-with-escapes (stream close)
119 "read in a string and handle \\f \\n \\r \\t \\v escape characters."
120 (with-output-to-string (out)
121 (do ((char (read-char stream nil :eof) (read-char stream nil :eof)))
122 ((or (eq char :eof) (char= char close))
123 (if (eq char :eof)
124 (error 'end-of-file :stream stream)))
125 (when (char= char #\\)
126 (setq char (read-char stream nil :eof))
127 (case char
128 (:eof (error 'end-of-file :stream stream))
129 (#\f (setq char #\Page))
130 (#\n (setq char #\Newline))
131 (#\r (setq char #\Return))
132 (#\t (setq char #\Tab))
133 (#\v (setq char #\Vt))))
134 (write-char char out))))
136 ;; LiCE handles a few escape codes like GNU Emacs
137 (set-macro-character #\" #'read-string-with-escapes)
139 (defun run-hooks (&rest hooks)
140 "Run each hook in HOOKS.
141 Each argument should be a symbol, a hook variable.
142 These symbols are processed in the order specified.
143 If a hook symbol has a non-nil value, that value may be a function
144 or a list of functions to be called to run the hook.
145 If the value is a function, it is called with no arguments.
146 If it is a list, the elements are called, in order, with no arguments.
148 Major modes should not use this function directly to run their mode
149 hook; they should use `run-mode-hooks' instead.
151 Do not use `make-local-variable' to make a hook variable buffer-local.
152 Instead, use `add-hook' and specify t for the LOCAL argument."
153 (mapc (lambda (h)
154 (when (symbolp h)
155 (setf h (symbol-value h)))
156 (mapc 'funcall h))
157 hooks))
159 (defun add-hook (hook function &optional append local)
160 "Add to the value of HOOK the function function.
161 FUNCTION is not added if already present.
162 FUNCTION is added (if necessary) at the beginning of the hook list
163 unless the optional argument append is non-nil, in which case
164 function is added at the end.
166 The optional fourth argument, LOCAL, if non-nil, says to modify
167 the hook's buffer-local value rather than its default value.
168 This makes the hook buffer-local if needed, and it makes t a member
169 of the buffer-local value. That acts as a flag to run the hook
170 functions in the default value as well as in the local value.
172 HOOK should be a symbol, and FUNCTION may be any valid function. If
173 HOOK is void, it is first set to nil. If HOOK's value is a single
174 function, it is changed to a list of functions."
175 (declare (ignore append local))
176 (pushnew function (symbol-value hook)))
178 (defun remove-hook (hook function &optional local)
179 "Remove from the value of HOOK the function FUNCTION.
180 HOOK should be a symbol, and FUNCTION may be any valid function. If
181 FUNCTION isn't the value of HOOK, or, if FUNCTION doesn't appear in the
182 list of hooks to run in HOOK, then nothing is done. See `add-hook'.
184 The optional third argument, LOCAL, if non-nil, says to modify
185 the hook's buffer-local value rather than its default value."
186 (declare (ignore local))
187 (setf (symbol-value hook) (remove function (symbol-value hook))))
189 (depricate substring subseq)
190 (defun substring (string from &optional (to (length string)))
191 "Return a substring of string, starting at index from and ending before to.
192 to may be nil or omitted; then the substring runs to the end of string.
193 from and to start at 0. If either is negative, it counts from the end.
195 This function allows vectors as well as strings."
196 (when (< from 0)
197 (setf from (max 0 (+ (length string) from))))
198 (when (< to 0)
199 (setf to (max 0 (+ (length string) to))))
200 (subseq string from to))
202 (depricate memq member)
203 (defun memq (elt list)
204 "Return non-nil if ELT is an element of LIST.
205 Comparison done with `eq'. The value is actually the tail of LIST
206 whose car is ELT."
207 (member elt list :test 'eq))
209 (defun int-to-string (n)
210 "Return the decimal representation of number as a string.
211 Uses a minus sign if negative.
212 number may be an integer or a floating point number."
213 (check-type n number)
214 (prin1-to-string n))
216 (defun string-to-char (string)
217 "Convert arg string to a character, the first character of that string.
218 A multibyte character is handled correctly."
219 (char string 0))
221 (provide :lice-0.1/global)