home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / p / plbin.zip / pl / lisp / qui_cmds.el < prev    next >
Lisp/Scheme  |  1992-05-26  |  16KB  |  562 lines

  1. ; /ports/emacs/GNU/el3.1 @(#)qui_cmds.el    1.3 1/8/91 
  2. ; /ports/home/sitaram/Gnu/qui_emacs @(#)qui_cmds.el    1.8 11/13/90 
  3. ;;;              QUI - GNU Emacs Interface
  4. ;;;              Support Functions
  5. ;;;
  6. ;;;          Consolidated by Sitaram Muralidhar
  7. ;;;
  8. ;;;             sitaram@quintus.com
  9. ;;;            Quintus Computer Systems, Inc.
  10. ;;;                 12 Nov 1990
  11. ;;;
  12. ;;;    This file defines functions that support the QUI - GNU Emacs
  13. ;;;                  interface.
  14.  
  15. (defvar qui-zap-file (make-temp-name "/tmp/qui")
  16.   "Temporary file name used for code being consulted or compiled in Qui.")
  17. (defvar  " ")
  18.  
  19. (defun qui-compile ()
  20.   (interactive)
  21.   (cond
  22.     ((string-equal (buffer-name) "*qui-emacs*")
  23.      (error "Cannot load from the qui scratch buffer")))
  24.   (qui-load "compile")
  25. )
  26.  
  27. (defun qui-load  (pl-load-type)
  28.   (let (pl-char )
  29.     (cond 
  30.      ((string-equal (buffer-file-name) "")
  31.       (error "Cannot load from this buffer before it is written to a file"))
  32. ;;  Need to check this, currently just sending things blindly to emacs
  33.      (t
  34.       (sleep-for 1)
  35.       (message 
  36.        (concat 
  37.         pl-load-type
  38.         " Prolog... enter p for procedure, r for region or b for buffer "))
  39.       (send-load-to-qui pl-load-type (read-char))
  40.       )
  41.      )
  42.     )
  43.   )
  44.  
  45. (defun send-load-to-qui  (sltp-load-type sltp-char)
  46.   (let (word1 word2
  47.           (file-name (expand-file-name (buffer-file-name))))
  48.     (cond 
  49.      ((string-equal sltp-load-type "compile")
  50.       (setq word1 "Compiling"))
  51.      ((string-equal sltp-load-type "consult")
  52.       (setq word1 "Consulting"))
  53.      )
  54.     (cond 
  55.      ((= sltp-char ?p)
  56.       (save-excursion
  57.         (message "Please Wait, finding predicate boundaries...")
  58.         (sit-for 0)
  59.     (save-excursion
  60.       (find-pred)
  61.       (write-region (point) (mark) qui-zap-file)
  62.       )
  63.         )
  64.       (setq word2 "procedure")
  65. ;;; loadpred <size> filename <size> tmpfile
  66.       (send-qui 
  67.     (concat LOADPRED  (padded-length file-name) 
  68.         file-name  (padded-length qui-zap-file)  qui-zap-file)))
  69.      ((= sltp-char ?r)
  70.       (write-region (point) (mark) qui-zap-file)
  71.       (setq word2 "region")
  72. ;;; loadregi <size> filename <size> tmpfile
  73.       (send-qui 
  74.     (concat LOADREGI  (padded-length file-name) 
  75.         file-name  (padded-length qui-zap-file)  qui-zap-file)))
  76.      ((= sltp-char ?b)
  77.       (save-excursion
  78.         (mark-whole-buffer)
  79.         (write-region (point) (mark) qui-zap-file)
  80.       )
  81.       (setq word2 "buffer")
  82. ;;; loadfile <size> filename
  83.       (send-qui 
  84.     (concat LOADFILE (padded-length file-name) file-name)))
  85.      (t (error "Bad option"))
  86.     )
  87.     (message (concat word1 " " word2 "..."))
  88.     (sit-for 0)
  89.     (&clear-message)
  90.   )
  91. )
  92.  
  93. ; ---------------------------------------------------------------------
  94. ;                  Find Definition
  95. ; Most of this stuff is the same as the prolog-emacs interface, minor
  96. ; modifications have been made to accomodate the manner in which Emacs
  97. ; talks to QUI.
  98. ; ---------------------------------------------------------------------
  99.  
  100. (defvar   *qui-functor*               0)
  101. (defvar   *qui-arity*               0)
  102. (defvar   *qui-env*)
  103. (defvar   *qui-print-name*           "")
  104. (defvar   *qui-already-saw-last-file* t)
  105. (defvar   *called-from-@find*       nil)
  106. (defconst *QuiNoArity*               -1)
  107.  
  108.  
  109. (defun find-qui-definition  ()
  110.   (interactive)
  111.   (let (token-type token)
  112.     (@fd-clear)
  113. ;;; Currently blindly sending to qui
  114.     (save-excursion 
  115.       (if (not (re-search-backward "[][?\001- \"%(#),{|}?\177]" nil t))
  116.           (beginning-of-buffer)
  117.     (forward-char)
  118.         )
  119.       (condition-case nil 
  120.           (let (token1 token1-type)
  121.             (setq token-type (next-token))
  122.             (setq token (region-to-string))
  123.             (cond 
  124.              ((string-equal token-type "atom")
  125.               (setq *qui-functor* token)
  126.               (save-excursion 
  127.                 (setq token1-type (next-token))
  128.                 (setq token1 (region-to-string))
  129.                 (if (string-equal token1 "/")
  130.                     (progn 
  131.                       (setq token1-type (next-token))
  132.                       (setq token1 (region-to-string))
  133.                       (if (string-equal token1-type "integer")
  134.                           (setq *qui-arity* (string-to-int token1))
  135.                         (setq *qui-arity* -1)
  136.                         )
  137.                       )
  138.                   (setq *qui-arity* -1)
  139.                   )
  140.                 )
  141.               (if (= *qui-arity* -1)
  142.                   (progn 
  143.                     (setq *qui-arity* 0)
  144.                     (error-occurred
  145.                      (next-token)
  146.                      (if (string-equal (region-to-string) "-->")
  147.                          (setq *qui-arity* (+ *qui-arity* 2)
  148.                                )
  149.                        )
  150.                      )
  151.                     )
  152.                 ))
  153.              ((string-equal token-type "functor")
  154.               (setq *qui-functor* token)
  155.               (condition-case nil
  156.                   (let ()
  157.                     (setq *qui-arity* (head-arity))
  158.                     (error-occurred
  159.                      (next-token)
  160.                      (if (string-equal (region-to-string) "-->")
  161.                          (setq *qui-arity* (+ *qui-arity* 2)
  162.                                )
  163.                        )
  164.                      )
  165.                     )
  166.                 (error (setq *qui-arity* *QuiNoArity*)))
  167.               )
  168.              (t 
  169.               (setq *qui-functor* "")
  170.               (setq *qui-arity* -1)
  171.               )
  172.              )
  173.             )
  174.         (error                          ; Handler for error
  175.          (setq *qui-functor* "")
  176.          (setq *qui-arity* -1)
  177.          )
  178.         )
  179.       )
  180.     (query-user)
  181.     (let ((mess
  182.            (concat "Please Wait, looking for predicate: "
  183.                    *qui-functor*
  184.                    (if (= *qui-arity* *QuiNoArity*)
  185.                        ""
  186.                      (concat "/" (int-to-string *qui-arity*))
  187.                      )
  188.                    "..."
  189.                    )
  190.            ))
  191.       (message mess)
  192.       (sit-for 0)
  193.       )
  194.     (get-predicate-files)
  195.     )
  196.   )
  197.  
  198. ; Send     "finddef <size> functor <size> arity <size> module" to QUI
  199. ; No Module information available, "_NoModule_" sent to QUI.
  200.  
  201. (defun get-predicate-files ()
  202.   (send-qui (concat FINDDEF  (padded-length *qui-functor*) 
  203.               *qui-functor*  
  204.               (if (= *qui-arity* *QuiNoArity*)
  205.                 (padded-length "-1") 
  206.                 (padded-length (int-to-string *qui-arity*))
  207.               )
  208.               (if (= *qui-arity* *QuiNoArity*)
  209.               *QuiNoArity*
  210.               *qui-arity*
  211.               )
  212.               (padded-length NOMODULE)  NOMODULE
  213.           )
  214.      )
  215. )
  216.   
  217. (defun parse-*qui-functor*-and-*qui-arity*  (&optional string)
  218.   (let ((buf (get-buffer-create "*temp*"))
  219.         token-type token)
  220.     (if (not string)
  221.         (setq string (read-string "Name/Arity: ")))
  222.     (save-excursion 
  223.       (set-buffer buf)
  224.       (widen)
  225.       (erase-buffer)
  226.       (insert-string string)
  227.       (beginning-of-buffer)
  228.       (setq token-type (next-token))
  229.       (setq token (region-to-string))
  230.       (if (string-equal token-type "atom")
  231.           (progn 
  232.             (setq *qui-functor* token)
  233.             (setq token-type (next-token))
  234.             (setq token (region-to-string))
  235.             (cond 
  236.              ((string-equal token-type "eof")
  237.               (setq *qui-arity* *QuiNoArity*))
  238.               ((not (string-equal token "/"))
  239.                (error 
  240.                 (concat "Name and arity must be separated by a '/': " token)))
  241.               (t
  242.                (setq token-type (next-token))
  243.                (setq token (region-to-string))
  244.                (if (string-equal token-type "integer")
  245.                    (progn
  246.                      (setq *qui-arity* (string-to-int token))
  247.                      (setq token-type (next-token))
  248.                      (setq token (region-to-string))
  249.                      (if (not (string-equal token-type "eof"))
  250.                          (error "Extra tokens after arity will be ignored")
  251.                        )
  252.                      )
  253.                  (error "Arity must be an integer: " token)
  254.                  )
  255.                )
  256.               )
  257.             )
  258.         (error (concat "Functor must be an atom: " token))
  259.         )
  260.       )
  261.     ))
  262.  
  263.  
  264. (defun query-user  ()
  265.   (let (user-response)
  266.     (setq user-response
  267.       (read-from-minibuffer "Find (Name/Arity): "
  268.                 (if (string-equal *qui-functor* "")
  269.                     ""
  270.                   (concat 
  271.                    *qui-functor*
  272.                    (if (= *qui-arity* *QuiNoArity*)
  273.                        ""
  274.                      (concat "/" (int-to-string *qui-arity*))
  275.                      )
  276.                    )
  277.                   )
  278.                 )
  279.           )
  280.     (if (not (string-equal user-response ""))
  281.     (parse-*qui-functor*-and-*qui-arity* user-response)
  282.       )
  283.     ))
  284.  
  285.  
  286. (defun find-more-qui-definition  ()
  287.   (interactive)
  288.   (if *qui-already-saw-last-file*
  289.       (conditional-message "find-definition ""ESC ."" must be used first")
  290.     (if (fd-buffer-empty)
  291.         (progn 
  292.           (setq *qui-already-saw-last-file* t)
  293.           (conditional-message 
  294.            (concat *qui-print-name* " has no more source files")))
  295.       (let ((fmd-file-name (fd-get-filename)) fmd-message)
  296.         (if (string-equal fmd-file-name "user")
  297.             (setq fmd-message (concat *qui-print-name*
  298.                                       " was defined in pseudo-file 'user'"))
  299.           (progn
  300.             (condition-case nil
  301.                 (let () 
  302.                   (find-file-other-window fmd-file-name)
  303.                   (setq fmd-message
  304.                         (locate-definition *qui-functor* *qui-arity* *qui-print-name*))
  305.                   (if (string-equal *qui-env* "debug")
  306.                       (pop-to-buffer "*prolog*" nil))
  307.                   )
  308.               (error
  309.                (setq fmd-message
  310.                      (concat *qui-print-name*
  311.                              " was defined in "
  312.                              fmd-file-name 
  313.                              ", but the file no longer exists")))
  314.               )
  315.             )
  316.           )
  317.         (if (fd-buffer-empty)
  318.             (if (string-equal fmd-message "")
  319.                 (setq fmd-message " ")
  320.               )
  321.           (if (string-equal fmd-message "")
  322.               (setq fmd-message "Type ""ESC ,"" for more")
  323.             (setq fmd-message
  324.                   (concat fmd-message ", type ""ESC ,"" for more"))
  325.             )
  326.           )
  327.         (conditional-message fmd-message)
  328.         )
  329.       )
  330.     )
  331.   (setq *called-from-@find* nil)
  332.   )
  333.  
  334. (defun conditional-message (message)
  335.   (if *called-from-@find*
  336.       (&qp-message message)
  337.     (progn 
  338.       (message message)
  339.         (sit-for 0)
  340.     )
  341.     )
  342. )
  343.  
  344.  
  345. (defun @fd-clear ()
  346.   (let ((buf (get-buffer-create "*qui-find-def*")))
  347.     (save-excursion
  348.       (set-buffer buf)
  349.       (widen)
  350.       (erase-buffer)
  351.       )
  352.     )
  353.   )
  354.  
  355. (defun @fd-in (file)
  356.   (save-excursion
  357.     (set-buffer "*qui-find-def*")
  358.     (end-of-buffer)
  359.     (insert-string (concat file "\n"))
  360.     )
  361.   )
  362.  
  363. (defun fd-get-filename ()
  364.   (let (ans)
  365.     (save-excursion
  366.       (set-buffer "*qui-find-def*")
  367.       (beginning-of-buffer)
  368.     (set-mark (point))
  369.     (search-forward " ")
  370.     (backward-char)
  371.     (setq *qui-functor* (region-to-string))
  372.     (forward-char)
  373.     (delete-region (point) (mark))
  374.     (set-mark (point))
  375.     (search-forward " ")
  376.     (backward-char)
  377.     (setq *qui-arity* (string-to-int (region-to-string)))
  378.     (forward-char)
  379.     (delete-region (point) (mark))
  380.     (set-mark (point))
  381.     (end-of-line)
  382.     (setq ans (region-to-string))
  383.     (forward-char)
  384.     (delete-region (point) (mark))
  385.     ans
  386.     )
  387. ))
  388.  
  389. (defun fd-buffer-empty ()
  390.   (save-excursion
  391.     (set-buffer "*qui-find-def*")
  392.     (= (buffer-size) 0)
  393.     )
  394.   )
  395.  
  396. (defun locate-definition (&optional functor arity print-name)
  397.   (if (not functor) (setq  functor (read-string "Functor: ")))
  398.   (if (not arity) (setq  arity (read-string "Arity: ")))
  399.   (if (not print-name) (setq print-name (read-string "Print Name: ")))
  400.   (let ((continue t)
  401.         (found-arity 0) (saved-point (point)) return)
  402.     (goto-char (point-min))
  403.     (while continue
  404.       (if (not (re-search-forward (concat "^" functor) nil t))
  405.           (progn 
  406.         (goto-char saved-point)              
  407.         (setq return
  408.                   (concat "Cannot find a definition for " 
  409.                           print-name 
  410.                           " in this file"))
  411.         (setq continue nil)
  412.             )
  413.         (if (not (within-comment))
  414.             (let  (valid-arity (saved-dot (point)))
  415.               (cond 
  416.                ((looking-at "[A-Za-z0-9_]")
  417.                 (setq valid-arity nil))
  418.                ((= (following-char) ?\( )
  419.                 (setq valid-arity
  420.                       (condition-case nil
  421.                           (progn (setq found-arity (all-arity saved-dot)) t)
  422.                         (error nil))
  423.                       ))
  424.                (t
  425.                 (setq found-arity 0)
  426.                 (setq found-arity
  427.                       (+ found-arity
  428.                          (arity-overhead-for-grammar-rule saved-dot)))
  429.                 (setq valid-arity t)
  430.                 )
  431.                )
  432.               (if valid-arity
  433.                   (if (or (= arity found-arity) (= arity *QuiNoArity*))
  434.                       (progn 
  435.             (goto-char saved-dot)
  436.             (beginning-of-line)
  437.             (setq return "")
  438.             (setq continue nil)
  439.                         )
  440.             (goto-char saved-dot)
  441.                     )
  442.         (goto-char saved-dot)
  443.                 )
  444.               )
  445.           )
  446.         )
  447.       )
  448.     (if (string-equal return "") (push-mark saved-point))
  449.     return)
  450.   )
  451.  
  452. ;---------------------------------------------------------------------------
  453. ; Qui sends a list of Name-Arity-filename triples to emacs by calling
  454. ; founddef with the Functor, Arity, Module and Filename. On receiving
  455. ; an "enddef ", the "find" begins through the triples in *qui-find-def*.
  456. ;---------------------------------------------------------------------------
  457.  
  458. ;;;
  459. ;;; Built-in definition
  460. ;;;
  461.  
  462. (defun builtin (functor arity module)
  463.   (cond ((= (string-to-int arity) *QuiNoArity*)
  464.      (setq *qui-print-name* functor))
  465.     (t (setq *qui-print-name* (concat functor "/" arity)))
  466.   )
  467.   (&qp-message (concat *qui-print-name* " is a built-in predicate"))
  468. )
  469.  
  470. ;;;
  471. ;;; No definition for predicate
  472. ;;;
  473.  
  474. (defun nondef (functor arity module)
  475.   (cond ((= (string-to-int arity) *QuiNoArity*)
  476.      (setq *qui-print-name* functor))
  477.     (t (setq *qui-print-name* (concat functor "/" arity)))
  478.   )
  479.   (&qp-message 
  480.     (concat *qui-print-name* " has no file(s) associated with it"))
  481. )
  482.  
  483. ;;;
  484. ;;; Undefined predicate
  485. ;;; arg3 - functor, arg2 - arity, arg1 - module
  486.  
  487. (defun undef (functor arity module)
  488.   (cond ((= (string-to-int arity) *QuiNoArity*)
  489.      (setq *qui-print-name* functor))
  490.     (t (setq *qui-print-name* (concat functor "/" arity)))
  491.   )
  492.   (&qp-message (concat *qui-print-name* " is undefined"))
  493. )
  494.  
  495. ;;;
  496. ;;; Look for first definition of predicate, signaled by enddef
  497. ;;;
  498.  
  499. (defun enddef ()
  500.   (setq *qui-already-saw-last-file* nil)
  501.   (setq *called-from-@find* 1)
  502.   (cond ((= *qui-arity* *QuiNoArity*)
  503.      (setq *qui-print-name* *qui-functor*))
  504.     (t (setq *qui-print-name* (concat *qui-functor* "/" *qui-arity*))))
  505.   (setq *qui-env* "")
  506.   (find-more-qui-definition)
  507. )
  508.  
  509. ;;;
  510. ;;; edit-file - find-file file and goto-char pos
  511. ;;; arg2 is filename arg1 is pos
  512. ;;;
  513.  
  514. (defun edit-file (file pos)
  515.   (find-file file)
  516.   (cond ((= pos 0)
  517.      (goto-char (point-min)))
  518.     (t (goto-char (1+ pos))))
  519. )
  520.  
  521. ;;;
  522. ;;; fill find defns buffer - create one if necessary and write triples
  523. ;;; to it, this is repeatedly called by QUI to fill in definitions.
  524. ;;; arg4 is functor, arg3 - arity, arg2 - module, arg1 - filename
  525.  
  526. (defun fill-defns (functor arity module filename)
  527.   (get-buffer-create "*qui-find-def*")
  528.   (let ((triple (concat functor space arity space filename)))
  529.     (@fd-in triple)
  530.   )
  531. )
  532.  
  533. ;;;
  534. ;;; Qui to quit
  535. ;;;
  536.  
  537. (defun qui-quit ()
  538.   (cond ((get-buffer "*qui-find-def*")
  539.      (kill-buffer "*qui-find-def*")))
  540.   (cond ((get-buffer "*temp*")
  541.      (kill-buffer "*temp*")))
  542.   (message "Qui quitting, terminating qui-emacs interface ")
  543. )
  544.  
  545. ;;;
  546. ;;; Cantload
  547. ;;;
  548.  
  549. (defun cantload ()
  550.   (message "Cannot load into prolog now")
  551.   (sit-for 0)
  552. )
  553.  
  554. ;;;
  555. ;;; Cantccp
  556. ;;;
  557.  
  558. (defun cantccp ()
  559.   (message "Cannot find definition now")
  560.   (sit-for 0)
  561. )
  562.