mlsupport.el 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. ;; Run-time support for mocklisp code.
  2. ;; Copyright (C) 1985 Free Software Foundation, Inc.
  3. ;; This file is part of GNU Emacs.
  4. ;; GNU Emacs is distributed in the hope that it will be useful,
  5. ;; but WITHOUT ANY WARRANTY. No author or distributor
  6. ;; accepts responsibility to anyone for the consequences of using it
  7. ;; or for whether it serves any particular purpose or works at all,
  8. ;; unless he says so in writing. Refer to the GNU Emacs General Public
  9. ;; License for full details.
  10. ;; Everyone is granted permission to copy, modify and redistribute
  11. ;; GNU Emacs, but only under the conditions described in the
  12. ;; GNU Emacs General Public License. A copy of this license is
  13. ;; supposed to have been given to you along with GNU Emacs so you
  14. ;; can know your rights and responsibilities. It should be in a
  15. ;; file named COPYING. Among other things, the copyright notice
  16. ;; and this notice must be preserved on all copies.
  17. (provide 'mlsupport)
  18. (defmacro ml-defun (&rest defs)
  19. (list 'ml-defun-1 (list 'quote defs)))
  20. (defun ml-defun-1 (args)
  21. (while args
  22. (fset (car (car args)) (cons 'mocklisp (cdr (car args))))
  23. (setq args (cdr args))))
  24. (defmacro declare-buffer-specific (&rest vars)
  25. (cons 'progn (mapcar (function (lambda (var) (list 'make-variable-buffer-local (list 'quote var)))) vars)))
  26. (defmacro setq-default (var val)
  27. (list 'set-default (list 'quote var) val))
  28. (defun ml-set-default (varname value)
  29. (set-default (intern varname) value))
  30. ; Lossage: must make various things default missing args to the prefix arg
  31. ; Alternatively, must make provide-prefix-argument do something hairy.
  32. (defun >> (val count) (lsh val (- count)))
  33. (defun novalue () nil)
  34. (defun ml-not (arg) (if (zerop arg) 1 0))
  35. (defun provide-prefix-arg (arg form)
  36. (funcall (car form) arg))
  37. (defun define-keymap (name)
  38. (fset (intern name) (make-keymap)))
  39. (defun ml-use-local-map (name)
  40. (use-local-map (intern (concat name "-map"))))
  41. (defun ml-use-global-map (name)
  42. (use-global-map (intern (concat name "-map"))))
  43. (defun local-bind-to-key (name key)
  44. (or (current-local-map)
  45. (use-local-map (make-keymap)))
  46. (define-key (current-local-map)
  47. (if (integerp key)
  48. (if (>= key 128)
  49. (concat (char-to-string meta-prefix-char)
  50. (char-to-string (- key 128)))
  51. (char-to-string key))
  52. key)
  53. (intern name)))
  54. (defun bind-to-key (name key)
  55. (define-key global-map (if (integerp key) (char-to-string key) key)
  56. (intern name)))
  57. (defun ml-autoload (name file)
  58. (autoload (intern name) file))
  59. (defun ml-define-string-macro (name defn)
  60. (fset (intern name) defn))
  61. (defun push-back-character (char)
  62. (setq unread-command-char char))
  63. (defun to-col (column)
  64. (indent-to column 0))
  65. (defmacro is-bound (&rest syms)
  66. (cons 'and (mapcar (function (lambda (sym) (list 'boundp (list 'quote sym)))) syms)))
  67. (defmacro declare-global (&rest syms)
  68. (cons 'progn (mapcar (function (lambda (sym) (list 'defvar sym nil))) syms)))
  69. (defmacro error-occurred (&rest body)
  70. (list 'condition-case nil (cons 'progn (append body '(nil))) '(error t)))
  71. (defun return-prefix-argument (value)
  72. (setq prefix-arg value))
  73. (defun ml-prefix-argument ()
  74. (if (null current-prefix-arg) 1
  75. (if (listp current-prefix-arg) (car current-prefix-arg)
  76. (if (eq current-prefix-arg '-) -1
  77. current-prefix-arg))))
  78. (defun ml-print (varname)
  79. (interactive "vPrint variable: ")
  80. (if (boundp varname)
  81. (message "%s => %s" (symbol-name varname) (symbol-value varname))
  82. (message "%s has no value" (symbol-name varname))))
  83. (defun ml-set (str val) (set (intern str) val))
  84. (defun ml-message (&rest args) (message "%s" (apply 'concat args)))
  85. (defun kill-to-end-of-line ()
  86. (ml-prefix-argument-loop
  87. (if (eolp)
  88. (kill-region (point) (1+ (point)))
  89. (kill-region (point) (if (search-forward ?\n nil t)
  90. (1- (point)) (point-max))))))
  91. (defun set-auto-fill-hook (arg)
  92. (setq auto-fill-hook (intern arg)))
  93. (defun auto-execute (function pattern)
  94. (if (/= (aref pattern 0) ?*)
  95. (error "Only patterns starting with * supported in auto-execute"))
  96. (setq auto-mode-alist (cons (cons (concat "\\." (substring pattern 1)
  97. "$")
  98. function)
  99. auto-mode-alist)))
  100. (defun move-to-comment-column ()
  101. (indent-to comment-column))
  102. (defun erase-region ()
  103. (delete-region (point) (mark)))
  104. (defun delete-region-to-buffer (bufname)
  105. (copy-to-buffer bufname (point) (mark))
  106. (delete-region (point) (mark)))
  107. (defun copy-region-to-buffer (bufname)
  108. (copy-to-buffer bufname (point) (mark)))
  109. (defun append-region-to-buffer (bufname)
  110. (append-to-buffer bufname (point) (mark)))
  111. (defun prepend-region-to-buffer (bufname)
  112. (prepend-to-buffer bufname (point) (mark)))
  113. (defun delete-next-character ()
  114. (delete-char (ml-prefix-argument)))
  115. (defun delete-next-word ()
  116. (delete-region (point) (progn (forward-word (ml-prefix-argument)) (point))))
  117. (defun delete-previous-word ()
  118. (delete-region (point) (progn (backward-word (ml-prefix-argument)) (point))))
  119. (defun delete-previous-character ()
  120. (delete-backward-char (ml-prefix-argument)))
  121. (defun forward-character ()
  122. (forward-char (ml-prefix-argument)))
  123. (defun backward-character ()
  124. (backward-char (ml-prefix-argument)))
  125. (defun ml-newline ()
  126. (newline (ml-prefix-argument)))
  127. (defun ml-next-line ()
  128. (next-line (ml-prefix-argument)))
  129. (defun ml-previous-line ()
  130. (previous-line (ml-prefix-argument)))
  131. (defun delete-to-kill-buffer ()
  132. (kill-region (point) (mark)))
  133. (defun narrow-region ()
  134. (narrow-to-region (point) (mark)))
  135. (defun ml-newline-and-indent ()
  136. (let ((column (current-indentation)))
  137. (newline (ml-prefix-argument))
  138. (indent-to column)))
  139. (defun newline-and-backup ()
  140. (open-line (ml-prefix-argument)))
  141. (defun quote-char ()
  142. (quoted-insert (ml-prefix-argument)))
  143. (defun ml-current-column ()
  144. (1+ (current-column)))
  145. (defun ml-current-indent ()
  146. (1+ (current-indentation)))
  147. (defun region-around-match (&optional n)
  148. (set-mark (match-beginning n))
  149. (goto-char (match-end n)))
  150. (defun region-to-string ()
  151. (buffer-substring (min (point) (mark)) (max (point) (mark))))
  152. (defun use-abbrev-table (name)
  153. (let ((symbol (intern (concat name "-abbrev-table"))))
  154. (or (boundp symbol)
  155. (define-abbrev-table symbol nil))
  156. (symbol-value symbol)))
  157. (defun define-hooked-local-abbrev (name exp hook)
  158. (define-local-abbrev name exp (intern hook)))
  159. (defun define-hooked-global-abbrev (name exp hook)
  160. (define-global-abbrev name exp (intern hook)))
  161. (defun case-word-lower ()
  162. (ml-casify-word 'downcase-region))
  163. (defun case-word-upper ()
  164. (ml-casify-word 'upcase-region))
  165. (defun case-word-capitalize ()
  166. (ml-casify-word 'capitalize-region))
  167. (defun ml-casify-word (fun)
  168. (save-excursion
  169. (forward-char 1)
  170. (forward-word -1)
  171. (funcall fun (point)
  172. (progn (forward-word (ml-prefix-argument))
  173. (point)))))
  174. (defun case-region-lower ()
  175. (downcase-region (point) (mark)))
  176. (defun case-region-upper ()
  177. (upcase-region (point) (mark)))
  178. (defun case-region-capitalize ()
  179. (capitalize-region (point) (mark)))
  180. (defvar saved-command-line-args nil)
  181. (defun argc ()
  182. (or saved-command-line-args
  183. (setq saved-command-line-args command-line-args
  184. command-line-args ()))
  185. (length command-line-args))
  186. (defun argv (i)
  187. (or saved-command-line-args
  188. (setq saved-command-line-args command-line-args
  189. command-line-args ()))
  190. (nth i saved-command-line-args))
  191. (defun invisible-argc ()
  192. (length (or saved-command-line-args
  193. command-line-args)))
  194. (defun invisible-argv (i)
  195. (nth i (or saved-command-line-args
  196. command-line-args)))
  197. (defun exit-emacs ()
  198. (interactive)
  199. (condition-case ()
  200. (exit-recursive-edit)
  201. (error (kill-emacs))))
  202. ;; Lisp function buffer-size returns total including invisible;
  203. ;; mocklisp wants just visible.
  204. (defun ml-buffer-size ()
  205. (- (point-max) (point-min)))
  206. (defun previous-command ()
  207. last-command)
  208. (defun beginning-of-window ()
  209. (goto-char (window-start)))
  210. (defun end-of-window ()
  211. (goto-char (window-start))
  212. (vertical-motion (- (window-height) 2)))
  213. (defun ml-search-forward (string)
  214. (search-forward string nil nil (ml-prefix-argument)))
  215. (defun ml-re-search-forward (string)
  216. (re-search-forward string nil nil (ml-prefix-argument)))
  217. (defun ml-search-backward (string)
  218. (search-backward string nil nil (ml-prefix-argument)))
  219. (defun ml-re-search-backward (string)
  220. (re-search-backward string nil nil (ml-prefix-argument)))
  221. (defvar use-users-shell 1
  222. "Mocklisp compatibility variable; 1 means use shell from SHELL env var.
  223. 0 means use /bin/sh.")
  224. (defvar use-csh-option-f 1
  225. "Mocklisp compatibility variable; 1 means pass -f when calling csh.")
  226. (defun filter-region (command)
  227. (let ((shell (if (/= use-users-shell 0) shell-file-name "/bin/sh"))
  228. (csh (equal (file-name-nondirectory shell) "csh")))
  229. (call-process-region (point) (mark) shell t t nil
  230. (if (and csh use-csh-option-f) "-cf" "-c")
  231. (concat "exec " command))))
  232. (defun execute-monitor-command (command)
  233. (let ((shell (if (/= use-users-shell 0) shell-file-name "/bin/sh"))
  234. (csh (equal (file-name-nondirectory shell) "csh")))
  235. (call-process shell nil t t
  236. (if (and csh use-csh-option-f) "-cf" "-c")
  237. (concat "exec " command))))
  238. (defun use-syntax-table (name)
  239. (set-syntax-table (symbol-value (intern (concat name "-syntax-table")))))
  240. (defun line-to-top-of-window ()
  241. (recenter (1- (ml-prefix-argument))))
  242. (defun ml-previous-page (&optional arg)
  243. (let ((count (or arg (ml-prefix-argument))))
  244. (while (> count 0)
  245. (scroll-down nil)
  246. (setq count (1- count)))
  247. (while (< count 0)
  248. (scroll-up nil)
  249. (setq count (1+ count)))))
  250. (defun ml-next-page ()
  251. (previous-page (- (ml-prefix-argument))))
  252. (defun page-next-window (&optional arg)
  253. (let ((count (or arg (ml-prefix-argument))))
  254. (while (> count 0)
  255. (scroll-other-window nil)
  256. (setq count (1- count)))
  257. (while (< count 0)
  258. (scroll-other-window '-)
  259. (setq count (1+ count)))))
  260. (defun ml-next-window ()
  261. (select-window (next-window)))
  262. (defun ml-previous-window ()
  263. (select-window (previous-window)))
  264. (defun scroll-one-line-up ()
  265. (scroll-up (ml-prefix-argument)))
  266. (defun scroll-one-line-down ()
  267. (scroll-down (ml-prefix-argument)))
  268. (defun split-current-window ()
  269. (split-window (selected-window)))
  270. (defun last-key-struck () last-command-char)
  271. (defun execute-mlisp-line (string)
  272. (eval (read string)))
  273. (defun move-dot-to-x-y (x y)
  274. (goto-char (window-start (selected-window)))
  275. (vertical-motion (1- y))
  276. (move-to-column (1- x)))
  277. (defun ml-modify-syntax-entry (string)
  278. (let ((i 5)
  279. (len (length string))
  280. (datastring (substring string 0 2)))
  281. (if (= (aref string 0) ?\-)
  282. (aset datastring 0 ?\ ))
  283. (if (= (aref string 2) ?\{)
  284. (if (= (aref string 4) ?\ )
  285. (aset datastring 0 ?\<)
  286. (error "Two-char comment delimiter: use modify-syntax-entry directly")))
  287. (if (= (aref string 3) ?\})
  288. (if (= (aref string 4) ?\ )
  289. (aset datastring 0 ?\>)
  290. (error "Two-char comment delimiter: use modify-syntax-entry directly")))
  291. (while (< i len)
  292. (modify-syntax-entry (aref string i) datastring)
  293. (setq i (1+ i))
  294. (if (and (< i len)
  295. (= (aref string i) ?\-))
  296. (let ((c (aref string (1- i)))
  297. (lim (aref string (1+ i))))
  298. (while (<= c lim)
  299. (modify-syntax-entry c datastring)
  300. (setq c (1+ c)))
  301. (setq i (+ 2 i)))))))
  302. (defun ml-substr (string from to)
  303. (let ((length (length string)))
  304. (if (< from 0) (setq from (+ from length)))
  305. (if (< to 0) (setq to (+ to length)))
  306. (substring string from (+ from to))))