Mercurial > hg > xemacs-beta
comparison lisp/emulators/mlsupport.el @ 0:376386a54a3c r19-14
Import from CVS: tag r19-14
author | cvs |
---|---|
date | Mon, 13 Aug 2007 08:45:50 +0200 |
parents | |
children | b82b59fe008d |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:376386a54a3c |
---|---|
1 ;;; mlsupport.el --- run-time support for mocklisp code. | |
2 | |
3 ;; Copyright (C) 1985 Free Software Foundation, Inc. | |
4 | |
5 ;; Maintainer: FSF | |
6 ;; Keywords: extensions | |
7 | |
8 ;; This file is part of XEmacs. | |
9 | |
10 ;; XEmacs is free software; you can redistribute it and/or modify it | |
11 ;; under the terms of the GNU General Public License as published by | |
12 ;; the Free Software Foundation; either version 2, or (at your option) | |
13 ;; any later version. | |
14 | |
15 ;; XEmacs is distributed in the hope that it will be useful, but | |
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
18 ;; General Public License for more details. | |
19 | |
20 ;; You should have received a copy of the GNU General Public License | |
21 ;; along with XEmacs; see the file COPYING. If not, write to the Free | |
22 ;; Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
23 | |
24 ;;; Synched up with: FSF 19.30. | |
25 | |
26 ;;; Commentary: | |
27 | |
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 oackage mlconvert.el translates Mocklisp code | |
32 ;; to use these names. | |
33 | |
34 ;;; Code: | |
35 | |
36 (or (fboundp 'ml-prefix-argument-loop) | |
37 (error "emacs was not compiled with #define MOCKLISP_SUPPORT in config.h")) | |
38 | |
39 (defmacro ml-defun (&rest defs) | |
40 (list 'ml-defun-1 (list 'quote defs))) | |
41 | |
42 (defun ml-defun-1 (args) | |
43 (while args | |
44 (fset (car (car args)) (cons 'mocklisp (cdr (car args)))) | |
45 (setq args (cdr args)))) | |
46 | |
47 (defmacro declare-buffer-specific (&rest vars) | |
48 (cons 'progn | |
49 (mapcar (function (lambda (var) | |
50 (list 'make-variable-buffer-local (list 'quote var)))) | |
51 vars))) | |
52 | |
53 (defun ml-set-default (varname value) | |
54 (set-default (intern varname) value)) | |
55 | |
56 ; Lossage: must make various things default missing args to the prefix arg | |
57 ; Alternatively, must make provide-prefix-argument do something hairy. | |
58 | |
59 (defun >> (val count) | |
60 (lsh val (- count))) | |
61 (defun novalue () | |
62 nil) | |
63 | |
64 (defun ml-not (arg) | |
65 (if (zerop arg) 1 0)) | |
66 | |
67 (defun provide-prefix-arg (arg form) | |
68 (funcall (car form) arg)) | |
69 | |
70 (defun define-keymap (name) | |
71 (fset (intern name) (make-keymap))) | |
72 | |
73 (defun ml-use-local-map (name) | |
74 (use-local-map (intern (concat name "-map")))) | |
75 | |
76 (defun ml-use-global-map (name) | |
77 (use-global-map (intern (concat name "-map")))) | |
78 | |
79 (defun local-bind-to-key (name key) | |
80 (or (current-local-map) | |
81 (use-local-map (make-keymap))) | |
82 (define-key (current-local-map) | |
83 (if (integerp key) | |
84 (if (>= key 128) | |
85 (concat (char-to-string meta-prefix-char) | |
86 (char-to-string (- key 128))) | |
87 (char-to-string key)) | |
88 key) | |
89 (intern name))) | |
90 | |
91 (defun bind-to-key (name key) | |
92 (define-key global-map (if (integerp key) (char-to-string key) key) | |
93 (intern name))) | |
94 | |
95 (defun ml-autoload (name file) | |
96 (autoload (intern name) file)) | |
97 | |
98 (defun ml-define-string-macro (name defn) | |
99 (fset (intern name) defn)) | |
100 | |
101 (defun push-back-character (char) | |
102 (setq unread-command-event (character-to-event char))) | |
103 | |
104 (defun to-col (column) | |
105 (indent-to column 0)) | |
106 | |
107 (defmacro is-bound (&rest syms) | |
108 (cons 'and (mapcar (function (lambda (sym) (list 'boundp (list 'quote sym)))) syms))) | |
109 | |
110 (defmacro declare-global (&rest syms) | |
111 (cons 'progn (mapcar (function (lambda (sym) (list 'defvar sym nil))) syms))) | |
112 | |
113 (defmacro error-occurred (&rest body) | |
114 (list 'condition-case nil (cons 'progn (append body '(nil))) '(error t))) | |
115 | |
116 (defun return-prefix-argument (value) | |
117 (setq prefix-arg value)) | |
118 | |
119 (defun ml-prefix-argument () | |
120 (cond ((null current-prefix-arg) | |
121 1) | |
122 ((listp current-prefix-arg) | |
123 (car current-prefix-arg)) | |
124 ((eq current-prefix-arg '-) | |
125 -1) | |
126 (t | |
127 current-prefix-arg))) | |
128 | |
129 (defun ml-print (varname) | |
130 (interactive "vPrint variable: ") | |
131 (if (boundp varname) | |
132 (message "%s => %s" (symbol-name varname) (symbol-value varname)) | |
133 (message "%s has no value" (symbol-name varname)))) | |
134 | |
135 (defun ml-set (str val) (set (intern str) val)) | |
136 | |
137 (defun ml-message (&rest args) (message "%s" (apply 'concat args))) | |
138 | |
139 (defun kill-to-end-of-line () | |
140 (ml-prefix-argument-loop | |
141 (if (eolp) | |
142 (kill-region (point) (1+ (point))) | |
143 (kill-region (point) (if (search-forward ?\n nil t) | |
144 (1- (point)) (point-max)))))) | |
145 | |
146 (defun set-auto-fill-hook (arg) | |
147 (setq auto-fill-function (intern arg))) | |
148 | |
149 (defun auto-execute (function pattern) | |
150 (if (/= (aref pattern 0) ?*) | |
151 (error "Only patterns starting with * supported in auto-execute")) | |
152 (setq auto-mode-alist (cons (cons (concat "\\." (substring pattern 1) | |
153 "\\'") | |
154 function) | |
155 auto-mode-alist))) | |
156 | |
157 (defun move-to-comment-column () | |
158 (indent-to comment-column)) | |
159 | |
160 (defun erase-region () | |
161 (delete-region (point) (mark))) | |
162 | |
163 (defun delete-region-to-buffer (bufname) | |
164 (copy-to-buffer bufname (point) (mark)) | |
165 (delete-region (point) (mark))) | |
166 | |
167 (defun copy-region-to-buffer (bufname) | |
168 (copy-to-buffer bufname (point) (mark))) | |
169 | |
170 (defun append-region-to-buffer (bufname) | |
171 (append-to-buffer bufname (point) (mark))) | |
172 | |
173 (defun prepend-region-to-buffer (bufname) | |
174 (prepend-to-buffer bufname (point) (mark))) | |
175 | |
176 (defun delete-next-character () | |
177 (delete-char (ml-prefix-argument))) | |
178 | |
179 (defun delete-next-word () | |
180 (delete-region (point) | |
181 (progn (forward-word (ml-prefix-argument)) (point)))) | |
182 | |
183 (defun delete-previous-word () | |
184 (delete-region (point) | |
185 (progn (backward-word (ml-prefix-argument)) (point)))) | |
186 | |
187 (defun delete-previous-character () | |
188 (delete-backward-char (ml-prefix-argument))) | |
189 | |
190 (defun forward-character () | |
191 (forward-char (ml-prefix-argument))) | |
192 | |
193 (defun backward-character () | |
194 (backward-char (ml-prefix-argument))) | |
195 | |
196 (defun ml-newline () | |
197 (newline (ml-prefix-argument))) | |
198 | |
199 (defun ml-next-line () | |
200 (next-line (ml-prefix-argument))) | |
201 | |
202 (defun ml-previous-line () | |
203 (previous-line (ml-prefix-argument))) | |
204 | |
205 (defun delete-to-kill-buffer () | |
206 (kill-region (point) (mark))) | |
207 | |
208 (defun narrow-region () | |
209 (narrow-to-region (point) (mark))) | |
210 | |
211 (defun ml-newline-and-indent () | |
212 (let ((column (current-indentation))) | |
213 (newline (ml-prefix-argument)) | |
214 (indent-to column))) | |
215 | |
216 (defun newline-and-backup () | |
217 (open-line (ml-prefix-argument))) | |
218 | |
219 (defun quote-char () | |
220 (quoted-insert (ml-prefix-argument))) | |
221 | |
222 (defun ml-current-column () | |
223 (1+ (current-column))) | |
224 | |
225 (defun ml-current-indent () | |
226 (1+ (current-indentation))) | |
227 | |
228 (defun region-around-match (&optional n) | |
229 (set-mark (match-beginning n)) | |
230 (goto-char (match-end n))) | |
231 | |
232 (defun region-to-string () | |
233 (buffer-substring (min (point) (mark)) (max (point) (mark)))) | |
234 | |
235 (defun use-abbrev-table (name) | |
236 (let ((symbol (intern (concat name "-abbrev-table")))) | |
237 (or (boundp symbol) | |
238 (define-abbrev-table symbol nil)) | |
239 (symbol-value symbol))) | |
240 | |
241 (defun define-hooked-local-abbrev (name exp hook) | |
242 (define-abbrev (or local-abbrev-table | |
243 (error "Major mode has no abbrev table")) | |
244 (downcase name) | |
245 exp (intern hook))) | |
246 | |
247 (defun define-hooked-global-abbrev (name exp hook) | |
248 (define-abbrev global-abbrev-table (downcase name) | |
249 exp (intern hook))) | |
250 | |
251 (defun case-word-lower () | |
252 (ml-casify-word 'downcase-region)) | |
253 | |
254 (defun case-word-upper () | |
255 (ml-casify-word 'upcase-region)) | |
256 | |
257 (defun case-word-capitalize () | |
258 (ml-casify-word 'capitalize-region)) | |
259 | |
260 (defun ml-casify-word (fun) | |
261 (save-excursion | |
262 (forward-char 1) | |
263 (forward-word -1) | |
264 (funcall fun (point) | |
265 (progn (forward-word (ml-prefix-argument)) | |
266 (point))))) | |
267 | |
268 (defun case-region-lower () | |
269 (downcase-region (point) (mark))) | |
270 | |
271 (defun case-region-upper () | |
272 (upcase-region (point) (mark))) | |
273 | |
274 (defun case-region-capitalize () | |
275 (capitalize-region (point) (mark))) | |
276 | |
277 (defvar saved-command-line-args nil) | |
278 | |
279 (defun argc () | |
280 (or saved-command-line-args | |
281 (setq saved-command-line-args command-line-args | |
282 command-line-args ())) | |
283 (length command-line-args)) | |
284 | |
285 (defun argv (i) | |
286 (or saved-command-line-args | |
287 (setq saved-command-line-args command-line-args | |
288 command-line-args ())) | |
289 (nth i saved-command-line-args)) | |
290 | |
291 (defun invisible-argc () | |
292 (length (or saved-command-line-args | |
293 command-line-args))) | |
294 | |
295 (defun invisible-argv (i) | |
296 (nth i (or saved-command-line-args | |
297 command-line-args))) | |
298 | |
299 (defun exit-emacs () | |
300 (interactive) | |
301 (condition-case () | |
302 (exit-recursive-edit) | |
303 (error (kill-emacs)))) | |
304 | |
305 ;; Lisp function buffer-size returns total including invisible; | |
306 ;; mocklisp wants just visible. | |
307 (defun ml-buffer-size () | |
308 (- (point-max) (point-min))) | |
309 | |
310 (defun previous-command () | |
311 last-command) | |
312 | |
313 (defun beginning-of-window () | |
314 (goto-char (window-start))) | |
315 | |
316 (defun end-of-window () | |
317 (goto-char (window-start)) | |
318 (vertical-motion (- (window-height) 2))) | |
319 | |
320 (defun ml-search-forward (string) | |
321 (search-forward string nil nil (ml-prefix-argument))) | |
322 | |
323 (defun ml-re-search-forward (string) | |
324 (re-search-forward string nil nil (ml-prefix-argument))) | |
325 | |
326 (defun ml-search-backward (string) | |
327 (search-backward string nil nil (ml-prefix-argument))) | |
328 | |
329 (defun ml-re-search-backward (string) | |
330 (re-search-backward string nil nil (ml-prefix-argument))) | |
331 | |
332 (defvar use-users-shell 1 | |
333 "Mocklisp compatibility variable; 1 means use shell from SHELL env var. | |
334 0 means use /bin/sh.") | |
335 | |
336 (defvar use-csh-option-f 1 | |
337 "Mocklisp compatibility variable; 1 means pass -f when calling csh.") | |
338 | |
339 (defun filter-region (command) | |
340 (let* ((shell (if (/= use-users-shell 0) shell-file-name "/bin/sh")) | |
341 (csh (equal (file-name-nondirectory shell) "csh"))) | |
342 (call-process-region (point) (mark) shell t t nil | |
343 (if (and csh use-csh-option-f) "-cf" "-c") | |
344 (concat "exec " command)))) | |
345 | |
346 (defun execute-monitor-command (command) | |
347 (let* ((shell (if (/= use-users-shell 0) shell-file-name "/bin/sh")) | |
348 (csh (equal (file-name-nondirectory shell) "csh"))) | |
349 (call-process shell nil t t | |
350 (if (and csh use-csh-option-f) "-cf" "-c") | |
351 (concat "exec " command)))) | |
352 | |
353 (defun use-syntax-table (name) | |
354 (set-syntax-table (symbol-value (intern (concat name "-syntax-table"))))) | |
355 | |
356 (defun line-to-top-of-window () | |
357 (recenter (1- (ml-prefix-argument)))) | |
358 | |
359 (defun ml-previous-page (&optional arg) | |
360 (let ((count (or arg (ml-prefix-argument)))) | |
361 (while (> count 0) | |
362 (scroll-down nil) | |
363 (setq count (1- count))) | |
364 (while (< count 0) | |
365 (scroll-up nil) | |
366 (setq count (1+ count))))) | |
367 | |
368 (defun ml-next-page () | |
369 (ml-previous-page (- (ml-prefix-argument)))) | |
370 | |
371 (defun page-next-window (&optional arg) | |
372 (let ((count (or arg (ml-prefix-argument)))) | |
373 (while (> count 0) | |
374 (scroll-other-window nil) | |
375 (setq count (1- count))) | |
376 (while (< count 0) | |
377 (scroll-other-window '-) | |
378 (setq count (1+ count))))) | |
379 | |
380 (defun ml-next-window () | |
381 (select-window (next-window))) | |
382 | |
383 (defun ml-previous-window () | |
384 (select-window (previous-window))) | |
385 | |
386 (defun scroll-one-line-up () | |
387 (scroll-up (ml-prefix-argument))) | |
388 | |
389 (defun scroll-one-line-down () | |
390 (scroll-down (ml-prefix-argument))) | |
391 | |
392 (defun split-current-window () | |
393 (split-window (selected-window))) | |
394 | |
395 (defun last-key-struck () last-command-char) | |
396 | |
397 (defun execute-mlisp-line (string) | |
398 (eval (read string))) | |
399 | |
400 (defun move-dot-to-x-y (x y) | |
401 (goto-char (window-start (selected-window))) | |
402 (vertical-motion (1- y)) | |
403 (move-to-column (1- x))) | |
404 | |
405 (defun ml-modify-syntax-entry (string) | |
406 (let ((i 5) | |
407 (len (length string)) | |
408 (datastring (substring string 0 2))) | |
409 (if (= (aref string 0) ?\-) | |
410 (aset datastring 0 ?\ )) | |
411 (if (= (aref string 2) ?\{) | |
412 (if (= (aref string 4) ?\ ) | |
413 (aset datastring 0 ?\<) | |
414 (error "Two-char comment delimiter: use modify-syntax-entry directly"))) | |
415 (if (= (aref string 3) ?\}) | |
416 (if (= (aref string 4) ?\ ) | |
417 (aset datastring 0 ?\>) | |
418 (error "Two-char comment delimiter: use modify-syntax-entry directly"))) | |
419 (while (< i len) | |
420 (modify-syntax-entry (aref string i) datastring) | |
421 (setq i (1+ i)) | |
422 (if (and (< i len) | |
423 (= (aref string i) ?\-)) | |
424 (let ((c (aref string (1- i))) | |
425 (lim (aref string (1+ i)))) | |
426 (while (<= c lim) | |
427 (modify-syntax-entry c datastring) | |
428 (setq c (1+ c))) | |
429 (setq i (+ 2 i))))))) | |
430 | |
431 | |
432 | |
433 (defun ml-substr (string from to) | |
434 (let ((length (length string))) | |
435 (if (< from 0) (setq from (+ from length))) | |
436 (if (< to 0) (setq to (+ to length))) | |
437 (substring string from (+ from to)))) | |
438 | |
439 | |
440 (defun ml-nargs () | |
441 "Number of arguments to currently executing mocklisp function." | |
442 (if (eq mocklisp-arguments 'interactive) | |
443 0 | |
444 (length mocklisp-arguments))) | |
445 | |
446 (defun ml-arg (n prompt) | |
447 "Argument number N to currently executing mocklisp function." | |
448 (if (eq mocklisp-arguments 'interactive) | |
449 (read-from-minibuffer prompt) | |
450 ;; Mocklisp likes to be origin 1 | |
451 (elt mocklisp-arguments (1- n)))) | |
452 | |
453 (defun ml-interactive () | |
454 "True if currently executing mocklisp function was called interactively." | |
455 (eq mocklisp-arguments 'interactive)) | |
456 | |
457 ;; Now in subr.el, because too many loser call it. | |
458 ;(defun insert-string (&rest args) | |
459 ; "Mocklisp-compatibility insert function. | |
460 ;Like the function `insert' except that any argument that is a number | |
461 ;is converted into a string by expressing it in decimal. (Yuck!!)" | |
462 ; (while args | |
463 ; (let ((arg (car args))) | |
464 ; (if (integerp arg) | |
465 ; (insert (number-to-string arg)) | |
466 ; (insert arg)) | |
467 ; (setq args (cdr args))))) | |
468 | |
469 (defun ml-concat (&rest args) | |
470 (let ((newargs nil) this) | |
471 (while args | |
472 (setq this (car args)) | |
473 (if (numberp this) | |
474 (setq this (number-to-string this))) | |
475 (setq newargs (cons this newargs) | |
476 args (cdr args))) | |
477 (apply 'concat (nreverse newargs)))) | |
478 | |
479 | |
480 ;;; Lose, please. None of these should be used in non-automatically-converted | |
481 ;;; code, so issue compilation warnings in case someone is using them because | |
482 ;;; they don't know any better. | |
483 | |
484 (make-obsolete 'read-no-blanks-input 'read-string) | |
485 (make-obsolete 'declare-buffer-specific 'make-variable-buffer-local) | |
486 (make-obsolete '>> 'lsh) | |
487 (make-obsolete 'novalue 'ignore) | |
488 (make-obsolete 'define-keymap "use (fset 'symbol (make-keymap))") | |
489 (make-obsolete 'local-bind-to-key 'local-set-key) | |
490 (make-obsolete 'bind-to-key 'global-set-key) | |
491 (make-obsolete 'to-col 'indent-to) | |
492 (make-obsolete 'is-bound 'boundp) | |
493 (make-obsolete 'declare-global 'defvar) | |
494 (make-obsolete 'error-occurred 'condition-case) | |
495 (make-obsolete 'set-auto-fill-hook "set variable auto-fill-function instead") | |
496 (make-obsolete 'auto-execute "set variable auto-mode-alist instead") | |
497 (make-obsolete 'move-to-comment-column "use (indent-to comment-column)") | |
498 (make-obsolete 'erase-region 'delete-region) | |
499 (make-obsolete 'delete-region-to-buffer "use copy-to-buffer and delete-region") | |
500 (make-obsolete 'copy-region-to-buffer 'copy-to-buffer) | |
501 (make-obsolete 'append-region-to-buffer 'append-to-buffer) | |
502 (make-obsolete 'prepend-region-to-buffer 'prepend-to-buffer) | |
503 (make-obsolete 'delete-next-character 'delete-char) | |
504 (make-obsolete 'forward-character 'forward-char) | |
505 (make-obsolete 'backward-character 'backward-char) | |
506 (make-obsolete 'delete-to-kill-buffer 'kill-region) | |
507 (make-obsolete 'narrow-region 'narrow-to-region) | |
508 (make-obsolete 'newline-and-backup 'open-line) | |
509 (make-obsolete 'quote-char 'quoted-insert) | |
510 (make-obsolete 'region-to-string 'buffer-substring) | |
511 (make-obsolete 'define-hooked-local-abbrev 'define-abbrev) | |
512 (make-obsolete 'define-hooked-global-abbrev 'define-abbrev) | |
513 (make-obsolete 'filter-region 'call-process-region) | |
514 (make-obsolete 'execute-monitor-command 'call-process) | |
515 (make-obsolete 'use-syntax-table 'set-syntax-table) | |
516 (make-obsolete 'line-to-top-of-window 'recenter) | |
517 (make-obsolete 'insert-string 'insert) | |
518 | |
519 (provide 'mlsupport) | |
520 | |
521 ;;; mlsupport.el ends here |