home *** CD-ROM | disk | FTP | other *** search
- ;;;; lisp.jl -- Some Lispy functions
- ;;; Copyright (C) 1993, 1994 John Harper <jsh@ukc.ac.uk>
-
- ;;; This file is part of Jade.
-
- ;;; Jade is free software; you can redistribute it and/or modify it
- ;;; under the terms of the GNU General Public License as published by
- ;;; the Free Software Foundation; either version 2, or (at your option)
- ;;; any later version.
-
- ;;; Jade 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 General Public License for more details.
-
- ;;; You should have received a copy of the GNU General Public License
- ;;; along with Jade; see the file COPYING. If not, write to
- ;;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
-
- (defmacro defvar (symbol init-value &optional doc-string)
- "(defvar NAME DEFAULT-VALUE [DOC-STRING]) <MACRO>
- Define a variable called NAME whose standard initial-value is DEFAULT-
- VALUE. If NAME is already bound to a value it is left as it is."
- (list 'progn
- (list
- 'unless
- (list 'boundp (list 'quote symbol))
- (list 'setq symbol init-value))
- (list 'when
- doc-string
- (list 'put (list 'quote symbol) ''variable-documentation doc-string))
- (list 'quote symbol)))
-
- (defmacro defconst (symbol init-value &optional doc-string)
- "(defconst NAME DEFAULT-VALUE [DOC-STRING]) <MACRO>
- Define a constant NAME whose standard value is DEFAULT-VALUE.
- If NAME is already bound to a value it is left untouched."
- (list 'progn
- (list
- 'unless
- (list 'boundp (list 'quote symbol))
- (list 'setq symbol init-value))
- (list 'set-const-variable (list 'quote symbol))
- (list 'when
- (list 'stringp doc-string)
- (list 'put (list 'quote symbol) ''variable-documentation doc-string))
- (list 'quote symbol)))
-
- (setq features ())
- (defun require (feature)
- (unless (member feature features)
- (load (format-string "%s" feature))))
- (defun provide (feature)
- (unless (member feature features)
- (setq features (cons feature features))))
- (defun featurep (feature)
- (member feature features))
-
- (defun add-hook (hook-sym new-func &optional at-end)
- "(add-hook HOOK-SYMBOL FUNCTION-NAME [AT-END])
- Arrange it so that FUNCTION-NAME is added to the hook-list stored in
- symbol, HOOK-SYMBOL. It will added at the head of the list unless AT-END
- is non-nil in which case it is added at the end."
- (unless (boundp hook-sym)
- (set hook-sym nil))
- (if at-end
- (set hook-sym (nconc (symbol-value hook-sym) (cons new-func nil)))
- (set hook-sym (cons new-func (symbol-value hook-sym)))))
-
- (defun remove-hook (hook-sym old-func)
- "(remove-hook HOOK-SYMBOL FUNCTION-NAME)
- Remove FUNCTION-NAME from the hook HOOK-SYMBOL."
- (set hook-sym (delete old-func (symbol-value hook-sym))))
-
- (defun documentation (symbol &optional use-var-doc &aux doc)
- "(documentation SYMBOL [IS-VARIABLE])
- Returns the documentation-string for SYMBOL. If IS-VARIABLE is t the
- documentation for the variable stored in SYMBOL is returned, else
- the function doc is provided."
- (when (symbolp symbol)
- (if use-var-doc
- (setq doc (get symbol 'variable-documentation))
- (setq symbol (symbol-function symbol))
- (cond
- ((subr-p symbol)
- (setq doc (subr-documentation symbol)))
- ((or (eq 'macro (car symbol)) (eq 'special (car symbol)))
- (setq doc (nth 3 symbol)))
- (t
- (setq doc (nth 2 symbol)))))
- (when (numberp doc)
- (setq doc (get-doc-string doc)))
- (when (stringp doc)
- doc)))
-
- (defun document-var (symbol doc-string)
- "(document-var SYMBOL DOC-STRING)
- Sets the `variable-documentation' property of SYMBOL to DOC-STRING."
- (put symbol 'variable-documentation doc-string)
- symbol)
-
- (defun format-string (&rest args)
- (car (apply 'format (cons "") args)))
-
- (defun read-from-string (string &optional start)
- "(read-from-string STRING [START])
- Reads an object from STRING, starting at character number START (default
- is 0)."
- (read (cons (if (numberp start) start 0) string)))
-
- (defun autoload (symbol file)
- "(autoload SYMBOL FILE)
- Tell the evaluator that the function value of SYMBOL will be initialised
- when the FILE is loaded."
- (fset symbol (list 'autoload file)))
-
- (defmacro setcar (&rest args)
- (cons 'rplaca args))
- (defmacro setcdr (&rest args)
- (cons 'rplacd args))
- (defmacro eql (&rest args)
- (cons 'eq args))
- (defmacro string= (&rest args)
- (cons 'equal args))
- (fset 'string-equal-p (symbol-function 'string=))
- (defmacro string< (&rest args)
- (cons '< args))
- (fset 'string-less-p (symbol-function 'string<))
-
- (defun set-variable ()
- "(set-variable)
- Prompts for a variable and a value to set it to."
- (let
- ((var (prompt-for-variable "Variable name: ")))
- (when var
- (let
- ((val (lisp-prompt "Value: ")))
- (when (and val (setq val (read-from-string val)))
- (set var val))))))
-
- (defun show-variable ()
- "(show-variable)
- Prompts for a variable and returns its value."
- (let
- ((var (prompt-for-variable "Variable name: ")))
- (when var
- (symbol-value var))))
-
- (defun error (&rest args)
- (signal 'error (list (apply 'format-string args))))
-
- ;;; Command interface to Lisp functions
-
- (defvar command-arg-list nil
- "List of arguments to next `command-prompt' function")
-
- (defun add-command-arg (&optional arg)
- "(add-command-arg [ARG])
- Add ARG (will prompt if not given) to the list of arguments to be given to
- the next command invoked by `command-prompt'."
- (unless arg
- (setq arg (eval (read-from-string (prompt2 'prompt-complete-symbol
- "Enter next argument:")))))
- (when arg
- (setq command-arg-list (nconc command-arg-list (cons arg nil)))))
-
- (defun clear-command-args ()
- "(clear-command-args)
- Discard any arguments to be used by `command-prompt'."
- (setq command-arg-list nil))
-
- (defun command-prompt (&optional title)
- "(command-prompt [TITLE])
- Prompt for the name of a function then invoke it with the contents of
- `command-arg-list' as arguments."
- (let
- ((res (apply (prompt-for-function (if title title "Enter command name:"))
- command-arg-list)))
- (setq command-arg-list nil)
- (prin1 res t)
- res))
-