source: trunk/j/src/org/armedbear/lisp/j.lisp @ 4201

Last change on this file since 4201 was 4201, checked in by piso, 19 years ago

(push :j *features*)

File size: 4.3 KB
Line 
1;;; j.lisp
2
3(in-package "J")
4
5(push :j *features*)
6
7(export '(set-global-property
8          reset-display
9          log.debug
10          add-hook
11          invoke-hook
12          update-display
13          update-location-bar
14          location-bar-cancel-input
15          restore-focus
16          status
17          defcommand
18          open-file-hook
19          after-save-hook
20          key-pressed-hook
21          variable-value
22          with-editor
23          with-other-editor
24          with-single-undo))
25
26(defun set-global-property (&rest args)
27  (let ((count (length args)) key value)
28    (cond ((oddp count)
29           (error "odd number of arguments to SET-GLOBAL-PROPERTY"))
30          ((= count 2)
31           (%set-global-property (string (first args)) (second args)))
32          (t
33           (do ((args args (cddr args)))
34               ((null args) (values))
35             (%set-global-property (string (first args)) (second args)))))))
36
37(defun reset-display ()
38  (jstatic "resetDisplay" "org.armedbear.j.Editor"))
39
40(defmacro log.debug (&rest args)
41  (when args
42    `(let ((method (jmethod "org.armedbear.j.Log" "debug" "java.lang.String")))
43       (jstatic method nil (format nil ,@args)))))
44
45(defun add-hook (hook function)
46  (when (symbolp hook)
47    (unless (boundp hook) (set hook nil))
48    (let ((hook-functions (symbol-value hook)))
49      (unless (memq function hook-functions)
50        (setq hook-functions (cons function hook-functions))
51        (set hook hook-functions)))))
52
53(defun invoke-hook (hook &rest args)
54  (when (symbolp hook)
55    (unless (boundp hook) (set hook nil))
56    (let ((hooks (symbol-value hook)))
57      (dolist (function hooks)
58        (apply function args)))))
59
60(defun update-display (&optional ed)
61  (let ((method (jmethod "org.armedbear.j.Editor" "updateDisplay"))
62        (ed (or ed (current-editor))))
63    (jcall method ed)))
64
65(defun update-location-bar (&optional ed)
66  (let ((method (jmethod "org.armedbear.j.Editor" "updateLocation"))
67        (ed (or ed (current-editor))))
68    (jcall method ed)))
69
70(defun location-bar-cancel-input ()
71  (jstatic "cancelInput" "org.armedbear.j.LocationBar"))
72
73(defun status (string &optional ed)
74  (let ((method (jmethod "org.armedbear.j.Editor" "status" "java.lang.String"))
75        (ed (or ed (current-editor))))
76    (jcall method ed string)))
77
78;; Internal.
79(defun execute-command (command &optional ed)
80  (let ((method (jmethod "org.armedbear.j.Editor"
81                         "executeCommand" "java.lang.String"))
82        (ed (or ed (current-editor))))
83    (jcall method ed command)
84    (update-display ed)))
85
86(defmacro defcommand (name &optional (command nil))
87  (unless command
88    (setq command (remove #\- (string `,name))))
89  `(setf (symbol-function ',name)
90         (lambda () (execute-command ,command))))
91
92(defvar open-file-hook nil)
93
94(defvar after-save-hook nil)
95
96(defvar key-pressed-hook nil)
97
98(defun variable-value (name &optional (kind :current) where)
99  (%variable-value name kind where))
100
101(defun set-variable-value (name kind &rest rest)
102  (let (where new-value)
103    (case (length rest)
104      (1
105       (setq where nil
106             new-value (car rest)))
107      (2
108       (setq where (car rest)
109             new-value (cadr rest)))
110      (t
111       (error 'program-error "SET-VARIABLE-VALUE: wrong number of arguments")))
112    (%set-variable-value name kind where new-value)))
113
114(defsetf variable-value set-variable-value)
115
116(defsetf current-editor %set-current-editor)
117
118(defmacro with-editor (editor &rest forms)
119  (let ((old-editor (gensym)))
120  `(let ((,old-editor (current-editor)))
121     (unwind-protect
122      (progn
123        (setf (current-editor) ,editor)
124        ,@forms)
125      (update-display ,editor)
126      (setf (current-editor) ,old-editor)))))
127
128(defmacro with-other-editor (&rest forms)
129  (let ((old-editor (gensym))
130        (other-editor (gensym)))
131    `(let ((,old-editor (current-editor))
132           (,other-editor (other-editor)))
133       (unless ,other-editor
134         (error "there is no other editor"))
135       (unwind-protect
136        (progn
137          (setf (current-editor) ,other-editor)
138          ,@forms)
139        (update-display ,other-editor)
140        (setf (current-editor) ,old-editor)))))
141
142(defmacro with-single-undo (&rest forms)
143  (let ((info (gensym)))
144    `(let ((,info (begin-compound-edit)))
145       (unwind-protect
146        (progn ,@forms)
147        (end-compound-edit ,info)))))
148
149(in-package "COMMON-LISP-USER")
150(use-package "J")
Note: See TracBrowser for help on using the repository browser.