1 ;;; mlsupport.el --- run-time support for mocklisp code.
3 ;; Copyright (C) 1985 Free Software Foundation, Inc.
6 ;; Keywords: extensions
8 ;; This file is part of GNU Emacs.
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)
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.
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.
26 ;; This package provides equivalents of certain primitives from Gosling
27 ;; Emacs (including the commercial UniPress versions). These have an
28 ;; ml- prefix to distinguish them from native GNU Emacs functions with
29 ;; similar names. The oackage mlconvert.el translates Mocklisp code
30 ;; to use these names.
34 (defmacro ml-defun
(&rest defs
)
35 (list 'ml-defun-1
(list 'quote defs
)))
37 (defun ml-defun-1 (args)
39 (fset (car (car args
)) (cons 'mocklisp
(cdr (car args
))))
40 (setq args
(cdr args
))))
42 (defmacro declare-buffer-specific
(&rest vars
)
43 (cons 'progn
(mapcar (function (lambda (var) (list 'make-variable-buffer-local
(list 'quote var
)))) vars
)))
45 (defun ml-set-default (varname value
)
46 (set-default (intern varname
) value
))
48 ; Lossage: must make various things default missing args to the prefix arg
49 ; Alternatively, must make provide-prefix-argument do something hairy.
51 (defun >> (val count
) (lsh val
(- count
)))
52 (defun novalue () nil
)
54 (defun ml-not (arg) (if (zerop arg
) 1 0))
56 (defun provide-prefix-arg (arg form
)
57 (funcall (car form
) arg
))
59 (defun define-keymap (name)
60 (fset (intern name
) (make-keymap)))
62 ;; Make it work to use ml-use-...-map on "esc" and such.
63 (fset 'esc-map esc-map
)
64 (fset 'ctl-x-map ctl-x-map
)
66 (defun ml-use-local-map (name)
67 (use-local-map (intern (concat name
"-map"))))
69 (defun ml-use-global-map (name)
70 (use-global-map (intern (concat name
"-map"))))
72 (defun local-bind-to-key (name key
)
73 (or (current-local-map)
74 (use-local-map (make-keymap)))
75 (define-key (current-local-map)
78 (concat (char-to-string meta-prefix-char
)
79 (char-to-string (- key
128)))
84 (defun bind-to-key (name key
)
85 (define-key global-map
(if (integerp key
) (char-to-string key
) key
)
88 (defun ml-autoload (name file
)
89 (autoload (intern name
) file
))
91 (defun ml-define-string-macro (name defn
)
92 (fset (intern name
) defn
))
94 (defun push-back-character (char)
95 (setq unread-command-events
(list char
)))
97 (defun to-col (column)
100 (defmacro is-bound
(&rest syms
)
101 (cons 'and
(mapcar (function (lambda (sym) (list 'boundp
(list 'quote sym
)))) syms
)))
103 (defmacro declare-global
(&rest syms
)
104 (cons 'progn
(mapcar (function (lambda (sym) (list 'defvar sym nil
))) syms
)))
106 (defmacro error-occurred
(&rest body
)
107 (list 'condition-case nil
(cons 'progn
(append body
'(nil))) '(error t
)))
109 (defun return-prefix-argument (value)
110 (setq prefix-arg value
))
112 (defun ml-prefix-argument ()
113 (if (null current-prefix-arg
) 1
114 (if (listp current-prefix-arg
) (car current-prefix-arg
)
115 (if (eq current-prefix-arg
'-
) -
1
116 current-prefix-arg
))))
118 (defun ml-print (varname)
119 (interactive "vPrint variable: ")
121 (message "%s => %s" (symbol-name varname
) (symbol-value varname
))
122 (message "%s has no value" (symbol-name varname
))))
124 (defun ml-set (str val
) (set (intern str
) val
))
126 (defun ml-message (&rest args
) (message "%s" (apply 'concat args
)))
128 (defun kill-to-end-of-line ()
129 (ml-prefix-argument-loop
131 (kill-region (point) (1+ (point)))
132 (kill-region (point) (if (search-forward ?
\n nil t
)
133 (1- (point)) (point-max))))))
135 (defun set-auto-fill-hook (arg)
136 (setq auto-fill-function
(intern arg
)))
138 (defun auto-execute (function pattern
)
139 (if (/= (aref pattern
0) ?
*)
140 (error "Only patterns starting with * supported in auto-execute"))
141 (setq auto-mode-alist
(cons (cons (concat "\\." (substring pattern
1)
146 (defun move-to-comment-column ()
147 (indent-to comment-column
))
149 (defun erase-region ()
150 (delete-region (point) (mark)))
152 (defun delete-region-to-buffer (bufname)
153 (copy-to-buffer bufname
(point) (mark))
154 (delete-region (point) (mark)))
156 (defun copy-region-to-buffer (bufname)
157 (copy-to-buffer bufname
(point) (mark)))
159 (defun append-region-to-buffer (bufname)
160 (append-to-buffer bufname
(point) (mark)))
162 (defun prepend-region-to-buffer (bufname)
163 (prepend-to-buffer bufname
(point) (mark)))
165 (defun delete-next-character ()
166 (delete-char (ml-prefix-argument)))
168 (defun delete-next-word ()
169 (delete-region (point) (progn (forward-word (ml-prefix-argument)) (point))))
171 (defun delete-previous-word ()
172 (delete-region (point) (progn (backward-word (ml-prefix-argument)) (point))))
174 (defun delete-previous-character ()
175 (delete-backward-char (ml-prefix-argument)))
177 (defun forward-character ()
178 (forward-char (ml-prefix-argument)))
180 (defun backward-character ()
181 (backward-char (ml-prefix-argument)))
184 (newline (ml-prefix-argument)))
186 (defun ml-next-line ()
187 (next-line (ml-prefix-argument)))
189 (defun ml-previous-line ()
190 (previous-line (ml-prefix-argument)))
192 (defun delete-to-kill-buffer ()
193 (kill-region (point) (mark)))
195 (defun narrow-region ()
196 (narrow-to-region (point) (mark)))
198 (defun ml-newline-and-indent ()
199 (let ((column (current-indentation)))
200 (newline (ml-prefix-argument))
203 (defun newline-and-backup ()
204 (open-line (ml-prefix-argument)))
207 (quoted-insert (ml-prefix-argument)))
209 (defun ml-current-column ()
210 (1+ (current-column)))
212 (defun ml-current-indent ()
213 (1+ (current-indentation)))
215 (defun region-around-match (&optional n
)
216 (set-mark (match-beginning n
))
217 (goto-char (match-end n
)))
219 (defun region-to-string ()
220 (buffer-substring (min (point) (mark)) (max (point) (mark))))
222 (defun use-abbrev-table (name)
223 (let ((symbol (intern (concat name
"-abbrev-table"))))
225 (define-abbrev-table symbol nil
))
226 (symbol-value symbol
)))
228 (defun define-hooked-local-abbrev (name exp hook
)
229 (define-local-abbrev name exp
(intern hook
)))
231 (defun define-hooked-global-abbrev (name exp hook
)
232 (define-global-abbrev name exp
(intern hook
)))
234 (defun case-word-lower ()
235 (ml-casify-word 'downcase-region
))
237 (defun case-word-upper ()
238 (ml-casify-word 'upcase-region
))
240 (defun case-word-capitalize ()
241 (ml-casify-word 'capitalize-region
))
243 (defun ml-casify-word (fun)
248 (progn (forward-word (ml-prefix-argument))
251 (defun case-region-lower ()
252 (downcase-region (point) (mark)))
254 (defun case-region-upper ()
255 (upcase-region (point) (mark)))
257 (defun case-region-capitalize ()
258 (capitalize-region (point) (mark)))
260 (defvar saved-command-line-args nil
)
263 (or saved-command-line-args
264 (setq saved-command-line-args command-line-args
265 command-line-args
()))
266 (length command-line-args
))
269 (or saved-command-line-args
270 (setq saved-command-line-args command-line-args
271 command-line-args
()))
272 (nth i saved-command-line-args
))
274 (defun invisible-argc ()
275 (length (or saved-command-line-args
278 (defun invisible-argv (i)
279 (nth i
(or saved-command-line-args
285 (exit-recursive-edit)
286 (error (kill-emacs))))
288 ;; Lisp function buffer-size returns total including invisible;
289 ;; mocklisp wants just visible.
290 (defun ml-buffer-size ()
291 (- (point-max) (point-min)))
293 (defun previous-command ()
296 (defun beginning-of-window ()
297 (goto-char (window-start)))
299 (defun end-of-window ()
300 (goto-char (window-start))
301 (vertical-motion (- (window-height) 2)))
303 (defun ml-search-forward (string)
304 (search-forward string nil nil
(ml-prefix-argument)))
306 (defun ml-re-search-forward (string)
307 (re-search-forward string nil nil
(ml-prefix-argument)))
309 (defun ml-search-backward (string)
310 (search-backward string nil nil
(ml-prefix-argument)))
312 (defun ml-re-search-backward (string)
313 (re-search-backward string nil nil
(ml-prefix-argument)))
315 (defvar use-users-shell
1
316 "Mocklisp compatibility variable; 1 means use shell from SHELL env var.
317 0 means use /bin/sh.")
319 (defvar use-csh-option-f
1
320 "Mocklisp compatibility variable; 1 means pass -f when calling csh.")
322 (defun filter-region (command)
323 (let ((shell (if (/= use-users-shell
0) shell-file-name
"/bin/sh"))
324 (csh (equal (file-name-nondirectory shell
) "csh")))
325 (call-process-region (point) (mark) shell t t nil
326 (if (and csh use-csh-option-f
) "-cf" "-c")
327 (concat "exec " command
))))
329 (defun execute-monitor-command (command)
330 (let ((shell (if (/= use-users-shell
0) shell-file-name
"/bin/sh"))
331 (csh (equal (file-name-nondirectory shell
) "csh")))
332 (call-process shell nil t t
333 (if (and csh use-csh-option-f
) "-cf" "-c")
334 (concat "exec " command
))))
336 (defun use-syntax-table (name)
337 (set-syntax-table (symbol-value (intern (concat name
"-syntax-table")))))
339 (defun line-to-top-of-window ()
340 (recenter (1- (ml-prefix-argument))))
342 (defun ml-previous-page (&optional arg
)
343 (let ((count (or arg
(ml-prefix-argument))))
346 (setq count
(1- count
)))
349 (setq count
(1+ count
)))))
351 (defun ml-next-page ()
352 (previous-page (- (ml-prefix-argument))))
354 (defun page-next-window (&optional arg
)
355 (let ((count (or arg
(ml-prefix-argument))))
357 (scroll-other-window nil
)
358 (setq count
(1- count
)))
360 (scroll-other-window '-
)
361 (setq count
(1+ count
)))))
363 (defun ml-next-window ()
364 (select-window (next-window)))
366 (defun ml-previous-window ()
367 (select-window (previous-window)))
369 (defun scroll-one-line-up ()
370 (scroll-up (ml-prefix-argument)))
372 (defun scroll-one-line-down ()
373 (scroll-down (ml-prefix-argument)))
375 (defun split-current-window ()
376 (split-window (selected-window)))
378 (defun last-key-struck () last-command-char
)
380 (defun execute-mlisp-line (string)
381 (eval (read string
)))
383 (defun move-dot-to-x-y (x y
)
384 (goto-char (window-start (selected-window)))
385 (vertical-motion (1- y
))
386 (move-to-column (1- x
)))
388 (defun ml-modify-syntax-entry (string)
390 (len (length string
))
391 (datastring (substring string
0 2)))
392 (if (= (aref string
0) ?\-
)
393 (aset datastring
0 ?\
))
394 (if (= (aref string
2) ?\
{)
395 (if (= (aref string
4) ?\
)
396 (aset datastring
0 ?\
<)
397 (error "Two-char comment delimiter: use modify-syntax-entry directly")))
398 (if (= (aref string
3) ?\
})
399 (if (= (aref string
4) ?\
)
400 (aset datastring
0 ?\
>)
401 (error "Two-char comment delimiter: use modify-syntax-entry directly")))
403 (modify-syntax-entry (aref string i
) datastring
)
406 (= (aref string i
) ?\-
))
407 (let ((c (aref string
(1- i
)))
408 (lim (aref string
(1+ i
))))
410 (modify-syntax-entry c datastring
)
412 (setq i
(+ 2 i
)))))))
416 (defun ml-substr (string from to
)
417 (let ((length (length string
)))
418 (if (< from
0) (setq from
(+ from length
)))
419 (if (< to
0) (setq to
(+ to length
)))
420 (substring string from
(+ from to
))))
424 ;;; mlsupport.el ends here