;;;; -*- Mode: LISP; Syntax: Common-Lisp; Base: 10 -*- ;;;; --------------------------------------------------------------------------- ;;;; File name: general-utilities.lsp ;;;; System: ;;;; Author: Shawn Nicholson ;;;; Created: July 15, 2002 16:38:35 ;;;; Purpose: ;;;; --------------------------------------------------------------------------- ;;;; Modified: Thursday, May 15, 2003 at 16:38:08 by nicholson ;;;; --------------------------------------------------------------------------- (in-package :zgraph-case-display) (defun pretty-print-to-string (form &optional (width 40)) "Pretty-prints a LISP expression to a string. The output is constrained to a width that is the number of characters specified by width." (let ((*print-right-margin* width) (*package* (find-package :data))) (remove-leading-newline (with-output-to-string (s) (pprint form s))))) (defun remove-leading-newline (string) (if (eq (elt string 0) #\newline) (subseq string 1) string)) (defun mentions (expression elem &key (test 'equal)) ;; Returns true if elem is mentioned anywhere in expression ;; Similar to "member" except is recursive (cond ((null expression) nil) ((funcall test expression elem) expression) ((not (listp expression)) nil) (t (or (mentions (first expression) elem :test test) (mentions (rest expression) elem :test test))))) (defun whitespace? (char &key (special-chars nil)) (let ((whitespace-chars (list #\space #\newline #\linefeed #\tab))) (or (member char whitespace-chars) (member char special-chars)))) (defun read-word (s &key (special-terminators nil)) ;; s = string stream ;; special-terminators - if you want a "word" to be terminated by ;; something else (in addition to) normal whitespace chars specify them ;; in a list of characters in this keyword argument ;; Like read only doens't try parsing it, just returns the next word in ;; string form ;; This WILL pull characters from s - so don't expect s to be unchanged ;; Does not read the final terminating character from the stream (with-output-to-string (out) (do ((c (peek-char nil s nil 'done) (peek-char nil s nil 'done))) ((or (eq c 'done) (whitespace? c :special-chars special-terminators)) :done) (write-char (read-char s) out)))) (defun remove-keywords (keywords argslist) "Removes all instances of keywords and their values from argslist" (cond ((null (cdr argslist)) argslist) ((member (first argslist) keywords) (remove-keywords keywords (cddr argslist))) (t (cons (first argslist) (remove-keywords keywords (cdr argslist)))))) (eval-when (:load-toplevel :compile-toplevel :execute) (export '(remove-keywords))) (defun flatten (list) "Removes nesting from a list" (flet ((mapcan-internal (function args) (apply #'append (mapcar function args)))) (cond ((null list) nil) ((atom list) (list list)) (t (mapcan-internal #'flatten list))))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; End of Code