comparison lisp/emulation/mlsupport.el @ 35:63b375f17a65

Initial revision
author Jim Blandy <jimb@redhat.com>
date Tue, 31 Oct 1989 15:59:53 +0000
parents
children 4cd7543be581
comparison
equal deleted inserted replaced
34:edf8af31003b 35:63b375f17a65
1 ;; Run-time support for mocklisp code.
2 ;; Copyright (C) 1985 Free Software Foundation, Inc.
3
4 ;; This file is part of GNU Emacs.
5
6 ;; GNU Emacs is free software; you can redistribute it and/or modify
7 ;; it under the terms of the GNU General Public License as published by
8 ;; the Free Software Foundation; either version 1, or (at your option)
9 ;; any later version.
10
11 ;; GNU Emacs is distributed in the hope that it will be useful,
12 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;; GNU General Public License for more details.
15
16 ;; You should have received a copy of the GNU General Public License
17 ;; along with GNU Emacs; see the file COPYING. If not, write to
18 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
19
20
21 (provide 'mlsupport)
22
23 (defmacro ml-defun (&rest defs)
24 (list 'ml-defun-1 (list 'quote defs)))
25
26 (defun ml-defun-1 (args)
27 (while args
28 (fset (car (car args)) (cons 'mocklisp (cdr (car args))))
29 (setq args (cdr args))))
30
31 (defmacro declare-buffer-specific (&rest vars)
32 (cons 'progn (mapcar (function (lambda (var) (list 'make-variable-buffer-local (list 'quote var)))) vars)))
33
34 (defun ml-set-default (varname value)
35 (set-default (intern varname) value))
36
37 ; Lossage: must make various things default missing args to the prefix arg
38 ; Alternatively, must make provide-prefix-argument do something hairy.
39
40 (defun >> (val count) (lsh val (- count)))
41 (defun novalue () nil)
42
43 (defun ml-not (arg) (if (zerop arg) 1 0))
44
45 (defun provide-prefix-arg (arg form)
46 (funcall (car form) arg))
47
48 (defun define-keymap (name)
49 (fset (intern name) (make-keymap)))
50
51 (defun ml-use-local-map (name)
52 (use-local-map (intern (concat name "-map"))))
53
54 (defun ml-use-global-map (name)
55 (use-global-map (intern (concat name "-map"))))
56
57 (defun local-bind-to-key (name key)
58 (or (current-local-map)
59 (use-local-map (make-keymap)))
60 (define-key (current-local-map)
61 (if (integerp key)
62 (if (>= key 128)
63 (concat (char-to-string meta-prefix-char)
64 (char-to-string (- key 128)))
65 (char-to-string key))
66 key)
67 (intern name)))
68
69 (defun bind-to-key (name key)
70 (define-key global-map (if (integerp key) (char-to-string key) key)
71 (intern name)))
72
73 (defun ml-autoload (name file)
74 (autoload (intern name) file))
75
76 (defun ml-define-string-macro (name defn)
77 (fset (intern name) defn))
78
79 (defun push-back-character (char)
80 (setq unread-command-char char))
81
82 (defun to-col (column)
83 (indent-to column 0))
84
85 (defmacro is-bound (&rest syms)
86 (cons 'and (mapcar (function (lambda (sym) (list 'boundp (list 'quote sym)))) syms)))
87
88 (defmacro declare-global (&rest syms)
89 (cons 'progn (mapcar (function (lambda (sym) (list 'defvar sym nil))) syms)))
90
91 (defmacro error-occurred (&rest body)
92 (list 'condition-case nil (cons 'progn (append body '(nil))) '(error t)))
93
94 (defun return-prefix-argument (value)
95 (setq prefix-arg value))
96
97 (defun ml-prefix-argument ()
98 (if (null current-prefix-arg) 1
99 (if (listp current-prefix-arg) (car current-prefix-arg)
100 (if (eq current-prefix-arg '-) -1
101 current-prefix-arg))))
102
103 (defun ml-print (varname)
104 (interactive "vPrint variable: ")
105 (if (boundp varname)
106 (message "%s => %s" (symbol-name varname) (symbol-value varname))
107 (message "%s has no value" (symbol-name varname))))
108
109 (defun ml-set (str val) (set (intern str) val))
110
111 (defun ml-message (&rest args) (message "%s" (apply 'concat args)))
112
113 (defun kill-to-end-of-line ()
114 (ml-prefix-argument-loop
115 (if (eolp)
116 (kill-region (point) (1+ (point)))
117 (kill-region (point) (if (search-forward ?\n nil t)
118 (1- (point)) (point-max))))))
119
120 (defun set-auto-fill-hook (arg)
121 (setq auto-fill-function (intern arg)))
122
123 (defun auto-execute (function pattern)
124 (if (/= (aref pattern 0) ?*)
125 (error "Only patterns starting with * supported in auto-execute"))
126 (setq auto-mode-alist (cons (cons (concat "\\." (substring pattern 1)
127 "$")
128 function)
129 auto-mode-alist)))
130
131 (defun move-to-comment-column ()
132 (indent-to comment-column))
133
134 (defun erase-region ()
135 (delete-region (point) (mark)))
136
137 (defun delete-region-to-buffer (bufname)
138 (copy-to-buffer bufname (point) (mark))
139 (delete-region (point) (mark)))
140
141 (defun copy-region-to-buffer (bufname)
142 (copy-to-buffer bufname (point) (mark)))
143
144 (defun append-region-to-buffer (bufname)
145 (append-to-buffer bufname (point) (mark)))
146
147 (defun prepend-region-to-buffer (bufname)
148 (prepend-to-buffer bufname (point) (mark)))
149
150 (defun delete-next-character ()
151 (delete-char (ml-prefix-argument)))
152
153 (defun delete-next-word ()
154 (delete-region (point) (progn (forward-word (ml-prefix-argument)) (point))))
155
156 (defun delete-previous-word ()
157 (delete-region (point) (progn (backward-word (ml-prefix-argument)) (point))))
158
159 (defun delete-previous-character ()
160 (delete-backward-char (ml-prefix-argument)))
161
162 (defun forward-character ()
163 (forward-char (ml-prefix-argument)))
164
165 (defun backward-character ()
166 (backward-char (ml-prefix-argument)))
167
168 (defun ml-newline ()
169 (newline (ml-prefix-argument)))
170
171 (defun ml-next-line ()
172 (next-line (ml-prefix-argument)))
173
174 (defun ml-previous-line ()
175 (previous-line (ml-prefix-argument)))
176
177 (defun delete-to-kill-buffer ()
178 (kill-region (point) (mark)))
179
180 (defun narrow-region ()
181 (narrow-to-region (point) (mark)))
182
183 (defun ml-newline-and-indent ()
184 (let ((column (current-indentation)))
185 (newline (ml-prefix-argument))
186 (indent-to column)))
187
188 (defun newline-and-backup ()
189 (open-line (ml-prefix-argument)))
190
191 (defun quote-char ()
192 (quoted-insert (ml-prefix-argument)))
193
194 (defun ml-current-column ()
195 (1+ (current-column)))
196
197 (defun ml-current-indent ()
198 (1+ (current-indentation)))
199
200 (defun region-around-match (&optional n)
201 (set-mark (match-beginning n))
202 (goto-char (match-end n)))
203
204 (defun region-to-string ()
205 (buffer-substring (min (point) (mark)) (max (point) (mark))))
206
207 (defun use-abbrev-table (name)
208 (let ((symbol (intern (concat name "-abbrev-table"))))
209 (or (boundp symbol)
210 (define-abbrev-table symbol nil))
211 (symbol-value symbol)))
212
213 (defun define-hooked-local-abbrev (name exp hook)
214 (define-local-abbrev name exp (intern hook)))
215
216 (defun define-hooked-global-abbrev (name exp hook)
217 (define-global-abbrev name exp (intern hook)))
218
219 (defun case-word-lower ()
220 (ml-casify-word 'downcase-region))
221
222 (defun case-word-upper ()
223 (ml-casify-word 'upcase-region))
224
225 (defun case-word-capitalize ()
226 (ml-casify-word 'capitalize-region))
227
228 (defun ml-casify-word (fun)
229 (save-excursion
230 (forward-char 1)
231 (forward-word -1)
232 (funcall fun (point)
233 (progn (forward-word (ml-prefix-argument))
234 (point)))))
235
236 (defun case-region-lower ()
237 (downcase-region (point) (mark)))
238
239 (defun case-region-upper ()
240 (upcase-region (point) (mark)))
241
242 (defun case-region-capitalize ()
243 (capitalize-region (point) (mark)))
244
245 (defvar saved-command-line-args nil)
246
247 (defun argc ()
248 (or saved-command-line-args
249 (setq saved-command-line-args command-line-args
250 command-line-args ()))
251 (length command-line-args))
252
253 (defun argv (i)
254 (or saved-command-line-args
255 (setq saved-command-line-args command-line-args
256 command-line-args ()))
257 (nth i saved-command-line-args))
258
259 (defun invisible-argc ()
260 (length (or saved-command-line-args
261 command-line-args)))
262
263 (defun invisible-argv (i)
264 (nth i (or saved-command-line-args
265 command-line-args)))
266
267 (defun exit-emacs ()
268 (interactive)
269 (condition-case ()
270 (exit-recursive-edit)
271 (error (kill-emacs))))
272
273 ;; Lisp function buffer-size returns total including invisible;
274 ;; mocklisp wants just visible.
275 (defun ml-buffer-size ()
276 (- (point-max) (point-min)))
277
278 (defun previous-command ()
279 last-command)
280
281 (defun beginning-of-window ()
282 (goto-char (window-start)))
283
284 (defun end-of-window ()
285 (goto-char (window-start))
286 (vertical-motion (- (window-height) 2)))
287
288 (defun ml-search-forward (string)
289 (search-forward string nil nil (ml-prefix-argument)))
290
291 (defun ml-re-search-forward (string)
292 (re-search-forward string nil nil (ml-prefix-argument)))
293
294 (defun ml-search-backward (string)
295 (search-backward string nil nil (ml-prefix-argument)))
296
297 (defun ml-re-search-backward (string)
298 (re-search-backward string nil nil (ml-prefix-argument)))
299
300 (defvar use-users-shell 1
301 "Mocklisp compatibility variable; 1 means use shell from SHELL env var.
302 0 means use /bin/sh.")
303
304 (defvar use-csh-option-f 1
305 "Mocklisp compatibility variable; 1 means pass -f when calling csh.")
306
307 (defun filter-region (command)
308 (let ((shell (if (/= use-users-shell 0) shell-file-name "/bin/sh"))
309 (csh (equal (file-name-nondirectory shell) "csh")))
310 (call-process-region (point) (mark) shell t t nil
311 (if (and csh use-csh-option-f) "-cf" "-c")
312 (concat "exec " command))))
313
314 (defun execute-monitor-command (command)
315 (let ((shell (if (/= use-users-shell 0) shell-file-name "/bin/sh"))
316 (csh (equal (file-name-nondirectory shell) "csh")))
317 (call-process shell nil t t
318 (if (and csh use-csh-option-f) "-cf" "-c")
319 (concat "exec " command))))
320
321 (defun use-syntax-table (name)
322 (set-syntax-table (symbol-value (intern (concat name "-syntax-table")))))
323
324 (defun line-to-top-of-window ()
325 (recenter (1- (ml-prefix-argument))))
326
327 (defun ml-previous-page (&optional arg)
328 (let ((count (or arg (ml-prefix-argument))))
329 (while (> count 0)
330 (scroll-down nil)
331 (setq count (1- count)))
332 (while (< count 0)
333 (scroll-up nil)
334 (setq count (1+ count)))))
335
336 (defun ml-next-page ()
337 (previous-page (- (ml-prefix-argument))))
338
339 (defun page-next-window (&optional arg)
340 (let ((count (or arg (ml-prefix-argument))))
341 (while (> count 0)
342 (scroll-other-window nil)
343 (setq count (1- count)))
344 (while (< count 0)
345 (scroll-other-window '-)
346 (setq count (1+ count)))))
347
348 (defun ml-next-window ()
349 (select-window (next-window)))
350
351 (defun ml-previous-window ()
352 (select-window (previous-window)))
353
354 (defun scroll-one-line-up ()
355 (scroll-up (ml-prefix-argument)))
356
357 (defun scroll-one-line-down ()
358 (scroll-down (ml-prefix-argument)))
359
360 (defun split-current-window ()
361 (split-window (selected-window)))
362
363 (defun last-key-struck () last-command-char)
364
365 (defun execute-mlisp-line (string)
366 (eval (read string)))
367
368 (defun move-dot-to-x-y (x y)
369 (goto-char (window-start (selected-window)))
370 (vertical-motion (1- y))
371 (move-to-column (1- x)))
372
373 (defun ml-modify-syntax-entry (string)
374 (let ((i 5)
375 (len (length string))
376 (datastring (substring string 0 2)))
377 (if (= (aref string 0) ?\-)
378 (aset datastring 0 ?\ ))
379 (if (= (aref string 2) ?\{)
380 (if (= (aref string 4) ?\ )
381 (aset datastring 0 ?\<)
382 (error "Two-char comment delimiter: use modify-syntax-entry directly")))
383 (if (= (aref string 3) ?\})
384 (if (= (aref string 4) ?\ )
385 (aset datastring 0 ?\>)
386 (error "Two-char comment delimiter: use modify-syntax-entry directly")))
387 (while (< i len)
388 (modify-syntax-entry (aref string i) datastring)
389 (setq i (1+ i))
390 (if (and (< i len)
391 (= (aref string i) ?\-))
392 (let ((c (aref string (1- i)))
393 (lim (aref string (1+ i))))
394 (while (<= c lim)
395 (modify-syntax-entry c datastring)
396 (setq c (1+ c)))
397 (setq i (+ 2 i)))))))
398
399
400
401 (defun ml-substr (string from to)
402 (let ((length (length string)))
403 (if (< from 0) (setq from (+ from length)))
404 (if (< to 0) (setq to (+ to length)))
405 (substring string from (+ from to))))