1 ;;; mlsupport.el --- run-time support for mocklisp code.
4 ;; Last-Modified: 16 Mar 1992
6 ;; Copyright (C) 1985 Free Software Foundation, Inc.
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 (defmacro ml-defun
(&rest defs
)
27 (list 'ml-defun-1
(list 'quote defs
)))
29 (defun ml-defun-1 (args)
31 (fset (car (car args
)) (cons 'mocklisp
(cdr (car args
))))
32 (setq args
(cdr args
))))
34 (defmacro declare-buffer-specific
(&rest vars
)
35 (cons 'progn
(mapcar (function (lambda (var) (list 'make-variable-buffer-local
(list 'quote var
)))) vars
)))
37 (defun ml-set-default (varname value
)
38 (set-default (intern varname
) value
))
40 ; Lossage: must make various things default missing args to the prefix arg
41 ; Alternatively, must make provide-prefix-argument do something hairy.
43 (defun >> (val count
) (lsh val
(- count
)))
44 (defun novalue () nil
)
46 (defun ml-not (arg) (if (zerop arg
) 1 0))
48 (defun provide-prefix-arg (arg form
)
49 (funcall (car form
) arg
))
51 (defun define-keymap (name)
52 (fset (intern name
) (make-keymap)))
54 (defun ml-use-local-map (name)
55 (use-local-map (intern (concat name
"-map"))))
57 (defun ml-use-global-map (name)
58 (use-global-map (intern (concat name
"-map"))))
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)
66 (concat (char-to-string meta-prefix-char
)
67 (char-to-string (- key
128)))
72 (defun bind-to-key (name key
)
73 (define-key global-map
(if (integerp key
) (char-to-string key
) key
)
76 (defun ml-autoload (name file
)
77 (autoload (intern name
) file
))
79 (defun ml-define-string-macro (name defn
)
80 (fset (intern name
) defn
))
82 (defun push-back-character (char)
83 (setq unread-command-char char
))
85 (defun to-col (column)
88 (defmacro is-bound
(&rest syms
)
89 (cons 'and
(mapcar (function (lambda (sym) (list 'boundp
(list 'quote sym
)))) syms
)))
91 (defmacro declare-global
(&rest syms
)
92 (cons 'progn
(mapcar (function (lambda (sym) (list 'defvar sym nil
))) syms
)))
94 (defmacro error-occurred
(&rest body
)
95 (list 'condition-case nil
(cons 'progn
(append body
'(nil))) '(error t
)))
97 (defun return-prefix-argument (value)
98 (setq prefix-arg value
))
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
))))
106 (defun ml-print (varname)
107 (interactive "vPrint variable: ")
109 (message "%s => %s" (symbol-name varname
) (symbol-value varname
))
110 (message "%s has no value" (symbol-name varname
))))
112 (defun ml-set (str val
) (set (intern str
) val
))
114 (defun ml-message (&rest args
) (message "%s" (apply 'concat args
)))
116 (defun kill-to-end-of-line ()
117 (ml-prefix-argument-loop
119 (kill-region (point) (1+ (point)))
120 (kill-region (point) (if (search-forward ?
\n nil t
)
121 (1- (point)) (point-max))))))
123 (defun set-auto-fill-hook (arg)
124 (setq auto-fill-function
(intern arg
)))
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)
134 (defun move-to-comment-column ()
135 (indent-to comment-column
))
137 (defun erase-region ()
138 (delete-region (point) (mark)))
140 (defun delete-region-to-buffer (bufname)
141 (copy-to-buffer bufname
(point) (mark))
142 (delete-region (point) (mark)))
144 (defun copy-region-to-buffer (bufname)
145 (copy-to-buffer bufname
(point) (mark)))
147 (defun append-region-to-buffer (bufname)
148 (append-to-buffer bufname
(point) (mark)))
150 (defun prepend-region-to-buffer (bufname)
151 (prepend-to-buffer bufname
(point) (mark)))
153 (defun delete-next-character ()
154 (delete-char (ml-prefix-argument)))
156 (defun delete-next-word ()
157 (delete-region (point) (progn (forward-word (ml-prefix-argument)) (point))))
159 (defun delete-previous-word ()
160 (delete-region (point) (progn (backward-word (ml-prefix-argument)) (point))))
162 (defun delete-previous-character ()
163 (delete-backward-char (ml-prefix-argument)))
165 (defun forward-character ()
166 (forward-char (ml-prefix-argument)))
168 (defun backward-character ()
169 (backward-char (ml-prefix-argument)))
172 (newline (ml-prefix-argument)))
174 (defun ml-next-line ()
175 (next-line (ml-prefix-argument)))
177 (defun ml-previous-line ()
178 (previous-line (ml-prefix-argument)))
180 (defun delete-to-kill-buffer ()
181 (kill-region (point) (mark)))
183 (defun narrow-region ()
184 (narrow-to-region (point) (mark)))
186 (defun ml-newline-and-indent ()
187 (let ((column (current-indentation)))
188 (newline (ml-prefix-argument))
191 (defun newline-and-backup ()
192 (open-line (ml-prefix-argument)))
195 (quoted-insert (ml-prefix-argument)))
197 (defun ml-current-column ()
198 (1+ (current-column)))
200 (defun ml-current-indent ()
201 (1+ (current-indentation)))
203 (defun region-around-match (&optional n
)
204 (set-mark (match-beginning n
))
205 (goto-char (match-end n
)))
207 (defun region-to-string ()
208 (buffer-substring (min (point) (mark)) (max (point) (mark))))
210 (defun use-abbrev-table (name)
211 (let ((symbol (intern (concat name
"-abbrev-table"))))
213 (define-abbrev-table symbol nil
))
214 (symbol-value symbol
)))
216 (defun define-hooked-local-abbrev (name exp hook
)
217 (define-local-abbrev name exp
(intern hook
)))
219 (defun define-hooked-global-abbrev (name exp hook
)
220 (define-global-abbrev name exp
(intern hook
)))
222 (defun case-word-lower ()
223 (ml-casify-word 'downcase-region
))
225 (defun case-word-upper ()
226 (ml-casify-word 'upcase-region
))
228 (defun case-word-capitalize ()
229 (ml-casify-word 'capitalize-region
))
231 (defun ml-casify-word (fun)
236 (progn (forward-word (ml-prefix-argument))
239 (defun case-region-lower ()
240 (downcase-region (point) (mark)))
242 (defun case-region-upper ()
243 (upcase-region (point) (mark)))
245 (defun case-region-capitalize ()
246 (capitalize-region (point) (mark)))
248 (defvar saved-command-line-args nil
)
251 (or saved-command-line-args
252 (setq saved-command-line-args command-line-args
253 command-line-args
()))
254 (length command-line-args
))
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
))
262 (defun invisible-argc ()
263 (length (or saved-command-line-args
266 (defun invisible-argv (i)
267 (nth i
(or saved-command-line-args
273 (exit-recursive-edit)
274 (error (kill-emacs))))
276 ;; Lisp function buffer-size returns total including invisible;
277 ;; mocklisp wants just visible.
278 (defun ml-buffer-size ()
279 (- (point-max) (point-min)))
281 (defun previous-command ()
284 (defun beginning-of-window ()
285 (goto-char (window-start)))
287 (defun end-of-window ()
288 (goto-char (window-start))
289 (vertical-motion (- (window-height) 2)))
291 (defun ml-search-forward (string)
292 (search-forward string nil nil
(ml-prefix-argument)))
294 (defun ml-re-search-forward (string)
295 (re-search-forward string nil nil
(ml-prefix-argument)))
297 (defun ml-search-backward (string)
298 (search-backward string nil nil
(ml-prefix-argument)))
300 (defun ml-re-search-backward (string)
301 (re-search-backward string nil nil
(ml-prefix-argument)))
303 (defvar use-users-shell
1
304 "Mocklisp compatibility variable; 1 means use shell from SHELL env var.
305 0 means use /bin/sh.")
307 (defvar use-csh-option-f
1
308 "Mocklisp compatibility variable; 1 means pass -f when calling csh.")
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
))))
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
))))
324 (defun use-syntax-table (name)
325 (set-syntax-table (symbol-value (intern (concat name
"-syntax-table")))))
327 (defun line-to-top-of-window ()
328 (recenter (1- (ml-prefix-argument))))
330 (defun ml-previous-page (&optional arg
)
331 (let ((count (or arg
(ml-prefix-argument))))
334 (setq count
(1- count
)))
337 (setq count
(1+ count
)))))
339 (defun ml-next-page ()
340 (previous-page (- (ml-prefix-argument))))
342 (defun page-next-window (&optional arg
)
343 (let ((count (or arg
(ml-prefix-argument))))
345 (scroll-other-window nil
)
346 (setq count
(1- count
)))
348 (scroll-other-window '-
)
349 (setq count
(1+ count
)))))
351 (defun ml-next-window ()
352 (select-window (next-window)))
354 (defun ml-previous-window ()
355 (select-window (previous-window)))
357 (defun scroll-one-line-up ()
358 (scroll-up (ml-prefix-argument)))
360 (defun scroll-one-line-down ()
361 (scroll-down (ml-prefix-argument)))
363 (defun split-current-window ()
364 (split-window (selected-window)))
366 (defun last-key-struck () last-command-char
)
368 (defun execute-mlisp-line (string)
369 (eval (read string
)))
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
)))
376 (defun ml-modify-syntax-entry (string)
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")))
391 (modify-syntax-entry (aref string i
) datastring
)
394 (= (aref string i
) ?\-
))
395 (let ((c (aref string
(1- i
)))
396 (lim (aref string
(1+ i
))))
398 (modify-syntax-entry c datastring
)
400 (setq i
(+ 2 i
)))))))
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
))))
412 ;;; mlsupport.el ends here