Initial Commit
[packages] / xemacs-packages / ilisp / ilisp-low.el
1 ;;; -*- Mode: Emacs-Lisp -*-
2
3 ;;; ilisp-low.el --
4 ;;; ILISP low level interface functions Lisp <-> Emacs.
5 ;;;
6 ;;; This file is part of ILISP.
7 ;;; Please refer to the file COPYING for copyrights and licensing
8 ;;; information.
9 ;;; Please refer to the file ACKNOWLEGDEMENTS for an (incomplete) list
10 ;;; of present and past contributors.
11 ;;;
12 ;;; $Id: ilisp-low.el,v 1.3 2001-07-02 09:40:47 youngs Exp $
13
14 ;;;%Lisp mode extensions
15 ;;;%%Sexps
16 (defun lisp-previous-sexp (&optional prefix)
17   "Return the previous sexp.  If PREFIX is T, then prefix like ' or #'
18 are allowed."
19   (save-excursion
20     (condition-case ()
21         (progn
22           (if (and (memq major-mode ilisp-modes)
23                    (= (point)
24                       (process-mark (get-buffer-process (current-buffer)))))
25               nil
26               (if (not
27                    (or (eobp) (memq (char-after (point)) '(? ?\) ?\n ?\t))))
28                   (forward-sexp))
29               (skip-chars-backward " \t\n")
30               (let ((point (point)))
31                 (backward-sexp)
32                 (skip-chars-backward "^ \t\n(\",")
33                 (if (not prefix) (skip-chars-forward "#'"))
34                 (buffer-substring (point) point))))
35       (error nil))))
36
37 ;;;
38 (defun lisp-def-name (&optional namep)
39   "Return the name of a definition assuming that you are at the start
40 of the sexp.  If the form starts with DEF, the form start and the next
41 symbol will be returned.  Optional NAMEP will return only the name without the defining symbol."
42   (let ((case-fold-search t))
43     (if (looking-at
44          ;; (( \( (def*) (( \( (setf)) | \(?)) | \(?) (symbol)
45          ;; 12    3    3 45    6    65      42      1 7      7
46          ;;0011\(22 def*        22         32 43\(54 setf54         43   \(?32 11      00 60           60
47          "\\(\\((\\(def[^ \t\n]*\\)[ \t\n]+\\(\\((\\(setf\\)[ \t\n]+\\)\\|(*\\)\\)\\|(?\\)\\([^ \t\n)]*\\)")
48         (let ((symbol (buffer-substring (match-beginning 7) (match-end 7))))
49           (if (match-end 6)
50               (concat (if (not namep) 
51                           (concat 
52                            (buffer-substring (match-beginning 3) (match-end 3))
53                            " "))
54                       "("
55                       (buffer-substring (match-beginning 6) (match-end 6))
56                       " " symbol ")")
57               (if (match-end 3)
58                   (concat (if (not namep)
59                               (concat 
60                                (buffer-substring (match-beginning 3) 
61                                                  (match-end 3))
62                                " "))
63                           symbol)
64                   symbol))))))
65
66
67 ;;;
68 (defun lisp-minus-prefix ()
69   "Set current-prefix-arg to its absolute value if numeric and return
70 T if it is a negative."
71   (if current-prefix-arg
72       (if (symbolp current-prefix-arg)
73           (progn (setq current-prefix-arg nil) t)
74           (if (< (setq current-prefix-arg
75                        (prefix-numeric-value current-prefix-arg))
76                  0)
77               (progn 
78                 (setq current-prefix-arg (- current-prefix-arg)) t)))))
79
80
81
82 ;;;%%Defuns
83 (defun lisp-defun-region-and-name ()
84   "Return the region of the current defun and the name starting it."
85   (save-excursion
86     (let ((end (lisp-defun-end))
87           (begin (lisp-defun-begin)))
88       (list begin end (lisp-def-name)))))
89   
90 ;;;
91 (defun lisp-region-name (start end)
92   "Return a name for the region from START to END."
93   (save-excursion
94     (goto-char start)
95     (if (re-search-forward "^[ \t]*[^;\n]" end t)
96         (forward-char -1))
97     (setq start (point))
98     (goto-char end)
99     (re-search-backward "^[ \t]*[^;\n]" start 'move)
100     (end-of-line)
101     (skip-chars-backward " \t")
102     (setq end (min (point) end))
103     (goto-char start)
104     (let ((from
105            (if (= (char-after (point)) ?\()
106                (lisp-def-name)
107                (buffer-substring (point) 
108                                  (progn (forward-sexp) (point))))))
109       (goto-char end)
110       (if (= (char-after (1- (point))) ?\))
111           (progn
112             (backward-sexp)
113             (if (= (point) start)
114                 from
115                 (concat "from " from " to " (lisp-def-name))))
116           (concat "from " from " to " 
117                   (buffer-substring (save-excursion
118                                       (backward-sexp)
119                                       (point)) 
120                                     (1- (point))))))))