(listify-key-sequence-1, event-modifiers): Don't presume internal bit layout
[bpt/emacs.git] / lisp / emulation / mlsupport.el
CommitLineData
6594deb0
ER
1;;; mlsupport.el --- run-time support for mocklisp code.
2
58142744
ER
3;; Copyright (C) 1985 Free Software Foundation, Inc.
4
e5167999 5;; Maintainer: FSF
fd7fa35a 6;; Keywords: extensions
e5167999 7
0d20f9a0
JB
8;; This file is part of GNU Emacs.
9
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
e5167999 12;; the Free Software Foundation; either version 2, or (at your option)
0d20f9a0
JB
13;; any later version.
14
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.
19
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.
23
edbd2f74
ER
24;;; Commentary:
25
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.
31
e5167999 32;;; Code:
0d20f9a0 33
0d20f9a0
JB
34(defmacro ml-defun (&rest defs)
35 (list 'ml-defun-1 (list 'quote defs)))
36
37(defun ml-defun-1 (args)
38 (while args
39 (fset (car (car args)) (cons 'mocklisp (cdr (car args))))
40 (setq args (cdr args))))
41
42(defmacro declare-buffer-specific (&rest vars)
43 (cons 'progn (mapcar (function (lambda (var) (list 'make-variable-buffer-local (list 'quote var)))) vars)))
44
45(defun ml-set-default (varname value)
46 (set-default (intern varname) value))
47
48; Lossage: must make various things default missing args to the prefix arg
49; Alternatively, must make provide-prefix-argument do something hairy.
50
51(defun >> (val count) (lsh val (- count)))
52(defun novalue () nil)
53
54(defun ml-not (arg) (if (zerop arg) 1 0))
55
56(defun provide-prefix-arg (arg form)
57 (funcall (car form) arg))
58
59(defun define-keymap (name)
60 (fset (intern name) (make-keymap)))
61
b34429f8
RS
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)
65
0d20f9a0
JB
66(defun ml-use-local-map (name)
67 (use-local-map (intern (concat name "-map"))))
68
69(defun ml-use-global-map (name)
70 (use-global-map (intern (concat name "-map"))))
71
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)
76 (if (integerp key)
77 (if (>= key 128)
78 (concat (char-to-string meta-prefix-char)
79 (char-to-string (- key 128)))
80 (char-to-string key))
81 key)
82 (intern name)))
83
84(defun bind-to-key (name key)
85 (define-key global-map (if (integerp key) (char-to-string key) key)
86 (intern name)))
87
88(defun ml-autoload (name file)
89 (autoload (intern name) file))
90
91(defun ml-define-string-macro (name defn)
92 (fset (intern name) defn))
93
94(defun push-back-character (char)
72766144 95 (setq unread-command-events (list char)))
0d20f9a0
JB
96
97(defun to-col (column)
98 (indent-to column 0))
99
100(defmacro is-bound (&rest syms)
101 (cons 'and (mapcar (function (lambda (sym) (list 'boundp (list 'quote sym)))) syms)))
102
103(defmacro declare-global (&rest syms)
104 (cons 'progn (mapcar (function (lambda (sym) (list 'defvar sym nil))) syms)))
105
106(defmacro error-occurred (&rest body)
107 (list 'condition-case nil (cons 'progn (append body '(nil))) '(error t)))
108
109(defun return-prefix-argument (value)
110 (setq prefix-arg value))
111
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))))
117
118(defun ml-print (varname)
119 (interactive "vPrint variable: ")
120 (if (boundp varname)
121 (message "%s => %s" (symbol-name varname) (symbol-value varname))
122 (message "%s has no value" (symbol-name varname))))
123
124(defun ml-set (str val) (set (intern str) val))
125
126(defun ml-message (&rest args) (message "%s" (apply 'concat args)))
127
128(defun kill-to-end-of-line ()
129 (ml-prefix-argument-loop
130 (if (eolp)
131 (kill-region (point) (1+ (point)))
132 (kill-region (point) (if (search-forward ?\n nil t)
133 (1- (point)) (point-max))))))
134
135(defun set-auto-fill-hook (arg)
136 (setq auto-fill-function (intern arg)))
137
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)
ac6ff3ed 142 "\\'")
0d20f9a0
JB
143 function)
144 auto-mode-alist)))
145
146(defun move-to-comment-column ()
147 (indent-to comment-column))
148
149(defun erase-region ()
150 (delete-region (point) (mark)))
151
152(defun delete-region-to-buffer (bufname)
153 (copy-to-buffer bufname (point) (mark))
154 (delete-region (point) (mark)))
155
156(defun copy-region-to-buffer (bufname)
157 (copy-to-buffer bufname (point) (mark)))
158
159(defun append-region-to-buffer (bufname)
160 (append-to-buffer bufname (point) (mark)))
161
162(defun prepend-region-to-buffer (bufname)
163 (prepend-to-buffer bufname (point) (mark)))
164
165(defun delete-next-character ()
166 (delete-char (ml-prefix-argument)))
167
168(defun delete-next-word ()
169 (delete-region (point) (progn (forward-word (ml-prefix-argument)) (point))))
170
171(defun delete-previous-word ()
172 (delete-region (point) (progn (backward-word (ml-prefix-argument)) (point))))
173
174(defun delete-previous-character ()
175 (delete-backward-char (ml-prefix-argument)))
176
177(defun forward-character ()
178 (forward-char (ml-prefix-argument)))
179
180(defun backward-character ()
181 (backward-char (ml-prefix-argument)))
182
183(defun ml-newline ()
184 (newline (ml-prefix-argument)))
185
186(defun ml-next-line ()
187 (next-line (ml-prefix-argument)))
188
189(defun ml-previous-line ()
190 (previous-line (ml-prefix-argument)))
191
192(defun delete-to-kill-buffer ()
193 (kill-region (point) (mark)))
194
195(defun narrow-region ()
196 (narrow-to-region (point) (mark)))
197
198(defun ml-newline-and-indent ()
199 (let ((column (current-indentation)))
200 (newline (ml-prefix-argument))
201 (indent-to column)))
202
203(defun newline-and-backup ()
204 (open-line (ml-prefix-argument)))
205
206(defun quote-char ()
207 (quoted-insert (ml-prefix-argument)))
208
209(defun ml-current-column ()
210 (1+ (current-column)))
211
212(defun ml-current-indent ()
213 (1+ (current-indentation)))
214
215(defun region-around-match (&optional n)
216 (set-mark (match-beginning n))
217 (goto-char (match-end n)))
218
219(defun region-to-string ()
220 (buffer-substring (min (point) (mark)) (max (point) (mark))))
221
222(defun use-abbrev-table (name)
223 (let ((symbol (intern (concat name "-abbrev-table"))))
224 (or (boundp symbol)
225 (define-abbrev-table symbol nil))
226 (symbol-value symbol)))
227
228(defun define-hooked-local-abbrev (name exp hook)
229 (define-local-abbrev name exp (intern hook)))
230
231(defun define-hooked-global-abbrev (name exp hook)
232 (define-global-abbrev name exp (intern hook)))
233
234(defun case-word-lower ()
235 (ml-casify-word 'downcase-region))
236
237(defun case-word-upper ()
238 (ml-casify-word 'upcase-region))
239
240(defun case-word-capitalize ()
241 (ml-casify-word 'capitalize-region))
242
243(defun ml-casify-word (fun)
244 (save-excursion
245 (forward-char 1)
246 (forward-word -1)
247 (funcall fun (point)
248 (progn (forward-word (ml-prefix-argument))
249 (point)))))
250
251(defun case-region-lower ()
252 (downcase-region (point) (mark)))
253
254(defun case-region-upper ()
255 (upcase-region (point) (mark)))
256
257(defun case-region-capitalize ()
258 (capitalize-region (point) (mark)))
259\f
260(defvar saved-command-line-args nil)
261
262(defun argc ()
263 (or saved-command-line-args
264 (setq saved-command-line-args command-line-args
265 command-line-args ()))
266 (length command-line-args))
267
268(defun argv (i)
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))
273
274(defun invisible-argc ()
275 (length (or saved-command-line-args
276 command-line-args)))
277
278(defun invisible-argv (i)
279 (nth i (or saved-command-line-args
280 command-line-args)))
281
282(defun exit-emacs ()
283 (interactive)
284 (condition-case ()
285 (exit-recursive-edit)
286 (error (kill-emacs))))
287\f
288;; Lisp function buffer-size returns total including invisible;
289;; mocklisp wants just visible.
290(defun ml-buffer-size ()
291 (- (point-max) (point-min)))
292
293(defun previous-command ()
294 last-command)
295
296(defun beginning-of-window ()
297 (goto-char (window-start)))
298
299(defun end-of-window ()
300 (goto-char (window-start))
301 (vertical-motion (- (window-height) 2)))
302\f
303(defun ml-search-forward (string)
304 (search-forward string nil nil (ml-prefix-argument)))
305
306(defun ml-re-search-forward (string)
307 (re-search-forward string nil nil (ml-prefix-argument)))
308
309(defun ml-search-backward (string)
310 (search-backward string nil nil (ml-prefix-argument)))
311
312(defun ml-re-search-backward (string)
313 (re-search-backward string nil nil (ml-prefix-argument)))
314
315(defvar use-users-shell 1
316 "Mocklisp compatibility variable; 1 means use shell from SHELL env var.
3170 means use /bin/sh.")
318
319(defvar use-csh-option-f 1
320 "Mocklisp compatibility variable; 1 means pass -f when calling csh.")
321
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))))
328
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))))
335\f
336(defun use-syntax-table (name)
337 (set-syntax-table (symbol-value (intern (concat name "-syntax-table")))))
338
339(defun line-to-top-of-window ()
340 (recenter (1- (ml-prefix-argument))))
341
342(defun ml-previous-page (&optional arg)
343 (let ((count (or arg (ml-prefix-argument))))
344 (while (> count 0)
345 (scroll-down nil)
346 (setq count (1- count)))
347 (while (< count 0)
348 (scroll-up nil)
349 (setq count (1+ count)))))
350
351(defun ml-next-page ()
352 (previous-page (- (ml-prefix-argument))))
353
354(defun page-next-window (&optional arg)
355 (let ((count (or arg (ml-prefix-argument))))
356 (while (> count 0)
357 (scroll-other-window nil)
358 (setq count (1- count)))
359 (while (< count 0)
360 (scroll-other-window '-)
361 (setq count (1+ count)))))
362
363(defun ml-next-window ()
364 (select-window (next-window)))
365
366(defun ml-previous-window ()
367 (select-window (previous-window)))
368
369(defun scroll-one-line-up ()
370 (scroll-up (ml-prefix-argument)))
371
372(defun scroll-one-line-down ()
373 (scroll-down (ml-prefix-argument)))
374
375(defun split-current-window ()
376 (split-window (selected-window)))
377
378(defun last-key-struck () last-command-char)
379
380(defun execute-mlisp-line (string)
381 (eval (read string)))
382
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)))
387
388(defun ml-modify-syntax-entry (string)
389 (let ((i 5)
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")))
402 (while (< i len)
403 (modify-syntax-entry (aref string i) datastring)
404 (setq i (1+ i))
405 (if (and (< i len)
406 (= (aref string i) ?\-))
407 (let ((c (aref string (1- i)))
408 (lim (aref string (1+ i))))
409 (while (<= c lim)
410 (modify-syntax-entry c datastring)
411 (setq c (1+ c)))
412 (setq i (+ 2 i)))))))
413\f
414
415
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))))
49116ac0
JB
421
422(provide 'mlsupport)
423
6594deb0 424;;; mlsupport.el ends here