[GAP Forum] Gap Emacs mode

Nikos Apostolakis nikos.ap at gmail.com
Tue Nov 17 12:32:49 GMT 2009


On Tue, Nov 17, 2009 at 10:32:33AM +0200, thus spake aker at gursey.gov.tr:
> Hello,
> 
> I am using Emacs mode for Gap.
> 
> The only way I know to send the file I work to gap-mode is
> 
> C-u M-x gap
> 
> This sends the whole file to gap-mode and executes it.
> 
> I have used Maple via an emacs mode which I have got from
> 
> http://www.mapleprimes.com/blog/joe-riel/emacs-mode-for-maple
> 
> In this Maple mode, it was possible to send a single line to maple mode,
> a region, or a function, or buffer etc. Among the features were
> highlighting...
> 
> Does the emacs mode for gap do these?
> 

No.  I think that a modern gap mode that has these features would be a
great idea.  

> Or, does anyone have his version of these features?
> 


Well, I use the following hack.  I'm not exactly an elisp expert so I'm sure
there is a better way of doing this but you may find the following useful:

-- 8< -------- Begin Elisp code  

(defun nea-send-string-to-gap (string)
  "Send the the string to an already running gap process. The output is
displayed in the process' buffer."
(interactive "sString to send to GAP (Include final \`;\')")
  (set-process-filter (get-process "gap") 'gap-output-filter)
    (save-window-excursion
    (set-buffer "*gap*")
    (goto-char (point-max))
    (process-send-string "gap" (concat string  "\n"))))


;; The name of the temp file should probably be randomly chosen.
;;; Haven't really used this command much.
(defun nea-send-region-to-gap-as-file (begin end)
  "Have GAP read the contents of the region as a file."
  (interactive "r")
  (let ((temp-gap-file "/tmp/tmp.gap"))
    (write-region  begin end temp-gap-file)
    (send-string-to-gap-normally (concat "Read(\"" temp-gap-file "\");\n"))))


(defun nea-send-region-to-gap (begin end &optional arg)
  "Send region to gap."
  (interactive "r\nP")
  (let ((string (buffer-substring begin end)))
    (save-window-excursion
    (set-buffer "*gap*")
    (goto-char (point-max))
    (send-string-to-gap string arg))))


(defun nea-send-line-to-gap (&optional arg)
  "Send current line to GAP."
  (interactive "P")
  (send-region-to-gap (line-beginning-position) (line-end-position) arg))


(defun nea-get-last-gap-output ()
  "Scrap the last output from the gap buffer"
  (interactive)
  (save-excursion
    (set-buffer "*gap*")
    (let ((pt (point))
	  (pmark (progn (goto-char (process-mark (get-buffer-process "*gap*")))
			(forward-line 0)
			(point-marker)))
	  output)
      (goto-char pmark)
      (search-backward-regexp gap-prompt-regexp nil t)
      (forward-line 1)
      (setq output (buffer-substring-no-properties 
		    (point) pmark))
    (goto-char pt)
    output)))



;; The following is an example of how to use nea-get-last-gap-output
(defun nea-ins-gap ()
  "Insert the last output in buffer."
  (interactive)
  (end-of-line)
  (let ((string (nea-get-last-gap-output)))
    (if (string-match "
." (nea-get-last-gap-output))
	(setq string (format "\n#==>\n %s;\n#<==" string))
      (setq string (format " #==> %s" string )))
    (insert string)))


;; I use the following key bindings:

(define-key gap-mode-map "\C-c\C-s" 'nea-send-string-to-gap)
(define-key gap-mode-map "\C-c\C-l" 'nea-send-line-to-gap)
(define-key gap-mode-map "\C-c\C-r" 'nea-send-region-to-gap)
(define-key gap-mode-map "\C-c\C-f" 'nea-send-region-to-gap-as-file)
(define-key gap-mode-map "\C-ci" 'nea-ins-gap)

-- 8< -------- End Elisp code  

> Thanks a lot,
> 
> kursat



More information about the Forum mailing list