home *** CD-ROM | disk | FTP | other *** search
/ Education Sampler 1992 [NeXTSTEP] / Education_1992_Sampler.iso / NeXT / GnuSource / emacs-15.0.3 / lisp / mlsupport.el < prev    next >
Lisp/Scheme  |  1990-07-19  |  12KB  |  410 lines

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