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