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