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