1 ;;; mlsupport.el --- run-time support for mocklisp code
3 ;; Copyright (C) 1985, 2001, 2002, 2003, 2004, 2005,
4 ;; 2006, 2007 Free Software Foundation, Inc.
7 ;; Keywords: extensions
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
28 ;; This package provides equivalents of certain primitives from Gosling
29 ;; Emacs (including the commercial UniPress versions). These have an
30 ;; ml- prefix to distinguish them from native GNU Emacs functions with
31 ;; similar names. The package mlconvert.el translates Mocklisp code
32 ;; to use these names.
36 (defmacro ml-defun (&rest defs)
37 (list 'ml-defun-1 (list 'quote defs)))
39 (defun ml-defun-1 (args)
41 (fset (car (car args)) (cons 'mocklisp (cdr (car args))))
42 (setq args (cdr args))))
44 (defmacro declare-buffer-specific (&rest vars)
45 (cons 'progn (mapcar (function (lambda (var) (list 'make-variable-buffer-local (list 'quote var)))) vars)))
47 (defun ml-set-default (varname value)
48 (set-default (intern varname) value))
50 ; Lossage: must make various things default missing args to the prefix arg
51 ; Alternatively, must make provide-prefix-argument do something hairy.
53 (defun >> (val count) (lsh val (- count)))
54 (defun novalue () nil)
56 (defun ml-not (arg) (if (zerop arg) 1 0))
58 (defun provide-prefix-arg (arg form)
59 (funcall (car form) arg))
61 (defun define-keymap (name)
62 (fset (intern name) (make-keymap)))
64 ;; Make it work to use ml-use-...-map on "esc" and such.
65 (fset 'esc-map esc-map)
66 (fset 'ctl-x-map ctl-x-map)
68 (defun ml-use-local-map (name)
69 (use-local-map (intern (concat name "-map"))))
71 (defun ml-use-global-map (name)
72 (use-global-map (intern (concat name "-map"))))
74 (defun local-bind-to-key (name key)
75 (or (current-local-map)
76 (use-local-map (make-keymap)))
77 (define-key (current-local-map)
80 (concat (char-to-string meta-prefix-char)
81 (char-to-string (- key 128)))
86 (defun bind-to-key (name key)
87 (define-key global-map (if (integerp key) (char-to-string key) key)
90 (defun ml-autoload (name file)
91 (autoload (intern name) file))
93 (defun ml-define-string-macro (name defn)
94 (fset (intern name) defn))
96 (defun push-back-character (char)
97 (setq unread-command-events (list char)))
99 (defun to-col (column)
100 (indent-to column 0))
102 (defmacro is-bound (&rest syms)
103 (cons 'and (mapcar (function (lambda (sym) (list 'boundp (list 'quote sym)))) syms)))
105 (defmacro declare-global (&rest syms)
106 (cons 'progn (mapcar (function (lambda (sym) (list 'defvar sym nil))) syms)))
108 (defmacro error-occurred (&rest body)
109 (list 'condition-case nil (cons 'progn (append body '(nil))) '(error t)))
111 (defun return-prefix-argument (value)
112 (setq prefix-arg value))
114 (defun ml-prefix-argument ()
115 (if (null current-prefix-arg) 1
116 (if (listp current-prefix-arg) (car current-prefix-arg)
117 (if (eq current-prefix-arg '-) -1
118 current-prefix-arg))))
120 (defun ml-print (varname)
121 (interactive "vPrint variable: ")
123 (message "%s => %s" (symbol-name varname) (symbol-value varname))
124 (message "%s has no value" (symbol-name varname))))
126 (defun ml-set (str val) (set (intern str) val))
128 (defun ml-message (&rest args) (message "%s" (apply 'concat args)))
130 (defun kill-to-end-of-line ()
131 (ml-prefix-argument-loop
133 (kill-region (point) (1+ (point)))
134 (kill-region (point) (if (search-forward ?\n nil t)
135 (1- (point)) (point-max))))))
137 (defun set-auto-fill-hook (arg)
138 (setq auto-fill-function (intern arg)))
140 (defun auto-execute (function pattern)
141 (if (/= (aref pattern 0) ?*)
142 (error "Only patterns starting with * supported in auto-execute"))
143 (setq auto-mode-alist (cons (cons (concat "\\." (substring pattern 1)
148 (defun move-to-comment-column ()
149 (indent-to comment-column))
151 (defun erase-region ()
152 (delete-region (point) (mark)))
154 (defun delete-region-to-buffer (bufname)
155 (copy-to-buffer bufname (point) (mark))
156 (delete-region (point) (mark)))
158 (defun copy-region-to-buffer (bufname)
159 (copy-to-buffer bufname (point) (mark)))
161 (defun append-region-to-buffer (bufname)
162 (append-to-buffer bufname (point) (mark)))
164 (defun prepend-region-to-buffer (bufname)
165 (prepend-to-buffer bufname (point) (mark)))
167 (defun delete-next-character ()
168 (delete-char (ml-prefix-argument)))
170 (defun delete-next-word ()
171 (delete-region (point) (progn (forward-word (ml-prefix-argument)) (point))))
173 (defun delete-previous-word ()
174 (delete-region (point) (progn (backward-word (ml-prefix-argument)) (point))))
176 (defun delete-previous-character ()
177 (delete-backward-char (ml-prefix-argument)))
179 (defun forward-character ()
180 (forward-char (ml-prefix-argument)))
182 (defun backward-character ()
183 (backward-char (ml-prefix-argument)))
186 (newline (ml-prefix-argument)))
188 (defun ml-next-line ()
189 (next-line (ml-prefix-argument)))
191 (defun ml-previous-line ()
192 (previous-line (ml-prefix-argument)))
194 (defun delete-to-kill-buffer ()
195 (kill-region (point) (mark)))
197 (defun narrow-region ()
198 (narrow-to-region (point) (mark)))
200 (defun ml-newline-and-indent ()
201 (let ((column (current-indentation)))
202 (newline (ml-prefix-argument))
205 (defun newline-and-backup ()
206 (open-line (ml-prefix-argument)))
209 (quoted-insert (ml-prefix-argument)))
211 (defun ml-current-column ()
212 (1+ (current-column)))
214 (defun ml-current-indent ()
215 (1+ (current-indentation)))
217 (defun region-around-match (&optional n)
218 (set-mark (match-beginning n))
219 (goto-char (match-end n)))
221 (defun region-to-string ()
222 (buffer-substring (min (point) (mark)) (max (point) (mark))))
224 (defun use-abbrev-table (name)
225 (let ((symbol (intern (concat name "-abbrev-table"))))
227 (define-abbrev-table symbol nil))
228 (symbol-value symbol)))
230 (defun define-hooked-local-abbrev (name exp hook)
231 (define-abbrev local-abbrev-table name exp (intern hook)))
233 (defun define-hooked-global-abbrev (name exp hook)
234 (define-abbrev global-abbrev-table name exp (intern hook)))
236 (defun case-word-lower ()
237 (ml-casify-word 'downcase-region))
239 (defun case-word-upper ()
240 (ml-casify-word 'upcase-region))
242 (defun case-word-capitalize ()
243 (ml-casify-word 'capitalize-region))
245 (defun ml-casify-word (fun)
250 (progn (forward-word (ml-prefix-argument))
253 (defun case-region-lower ()
254 (downcase-region (point) (mark)))
256 (defun case-region-upper ()
257 (upcase-region (point) (mark)))
259 (defun case-region-capitalize ()
260 (capitalize-region (point) (mark)))
262 (defvar saved-command-line-args nil)
265 (or saved-command-line-args
266 (setq saved-command-line-args command-line-args
267 command-line-args ()))
268 (length command-line-args))
271 (or saved-command-line-args
272 (setq saved-command-line-args command-line-args
273 command-line-args ()))
274 (nth i saved-command-line-args))
276 (defun invisible-argc ()
277 (length (or saved-command-line-args
280 (defun invisible-argv (i)
281 (nth i (or saved-command-line-args
287 (exit-recursive-edit)
288 (error (kill-emacs))))
290 ;; Lisp function buffer-size returns total including invisible;
291 ;; mocklisp wants just visible.
292 (defun ml-buffer-size ()
293 (- (point-max) (point-min)))
295 (defun previous-command ()
298 (defun beginning-of-window ()
299 (goto-char (window-start)))
301 (defun end-of-window ()
302 (goto-char (window-start))
303 (vertical-motion (- (window-height) 2)))
305 (defun ml-search-forward (string)
306 (search-forward string nil nil (ml-prefix-argument)))
308 (defun ml-re-search-forward (string)
309 (re-search-forward string nil nil (ml-prefix-argument)))
311 (defun ml-search-backward (string)
312 (search-backward string nil nil (ml-prefix-argument)))
314 (defun ml-re-search-backward (string)
315 (re-search-backward string nil nil (ml-prefix-argument)))
317 (defvar use-users-shell 1
318 "Mocklisp compatibility variable; 1 means use shell from SHELL env var.
319 0 means use /bin/sh.")
321 (defvar use-csh-option-f 1
322 "Mocklisp compatibility variable; 1 means pass -f when calling csh.")
324 (defun filter-region (command)
325 (let* ((shell (if (/= use-users-shell 0) shell-file-name "/bin/sh"))
326 (csh (equal (file-name-nondirectory shell) "csh")))
327 (call-process-region (point) (mark) shell t t nil
328 (if (and csh use-csh-option-f) "-cf" "-c")
329 (concat "exec " command))))
331 (defun execute-monitor-command (command)
332 (let* ((shell (if (/= use-users-shell 0) shell-file-name "/bin/sh"))
333 (csh (equal (file-name-nondirectory shell) "csh")))
334 (call-process shell nil t t
335 (if (and csh use-csh-option-f) "-cf" "-c")
336 (concat "exec " command))))
338 (defun use-syntax-table (name)
339 (set-syntax-table (symbol-value (intern (concat name "-syntax-table")))))
341 (defun line-to-top-of-window ()
342 (recenter (1- (ml-prefix-argument))))
344 (defun ml-previous-page (&optional arg)
345 (let ((count (or arg (ml-prefix-argument))))
348 (setq count (1- count)))
351 (setq count (1+ count)))))
353 (defun ml-next-page ()
354 (previous-page (- (ml-prefix-argument))))
356 (defun page-next-window (&optional arg)
357 (let ((count (or arg (ml-prefix-argument))))
359 (scroll-other-window nil)
360 (setq count (1- count)))
362 (scroll-other-window '-)
363 (setq count (1+ count)))))
365 (defun ml-next-window ()
366 (select-window (next-window)))
368 (defun ml-previous-window ()
369 (select-window (previous-window)))
371 (defun scroll-one-line-up ()
372 (scroll-up (ml-prefix-argument)))
374 (defun scroll-one-line-down ()
375 (scroll-down (ml-prefix-argument)))
377 (defun split-current-window ()
378 (split-window (selected-window)))
380 (defun last-key-struck () last-command-char)
382 (defun execute-mlisp-line (string)
383 (eval (read string)))
385 (defun move-dot-to-x-y (x y)
386 (goto-char (window-start (selected-window)))
387 (vertical-motion (1- y))
388 (move-to-column (1- x)))
390 (defun ml-modify-syntax-entry (string)
392 (len (length string))
393 (datastring (substring string 0 2)))
394 (if (= (aref string 0) ?\-)
395 (aset datastring 0 ?\ ))
396 (if (= (aref string 2) ?\{)
397 (if (= (aref string 4) ?\ )
398 (aset datastring 0 ?\<)
399 (error "Two-char comment delimiter: use modify-syntax-entry directly")))
400 (if (= (aref string 3) ?\})
401 (if (= (aref string 4) ?\ )
402 (aset datastring 0 ?\>)
403 (error "Two-char comment delimiter: use modify-syntax-entry directly")))
405 (modify-syntax-entry (aref string i) datastring)
408 (= (aref string i) ?\-))
409 (let ((c (aref string (1- i)))
410 (lim (aref string (1+ i))))
412 (modify-syntax-entry c datastring)
414 (setq i (+ 2 i)))))))
418 (defun ml-substr (string from to)
419 (let ((length (length string)))
420 (if (< from 0) (setq from (+ from length)))
421 (if (< to 0) (setq to (+ to length)))
422 (substring string from (+ from to))))
424 (defun ml-concat (&rest args)
425 (let ((newargs nil) this)
427 (setq this (car args))
429 (setq this (number-to-string this)))
430 (setq newargs (cons this newargs)
432 (apply 'concat (nreverse newargs))))
436 ;;; arch-tag: b0ad09bc-8cb2-4be0-8888-2e874839bcbc
437 ;;; mlsupport.el ends here