[comp.lang.apl] GNU emacs code for APL to Keyword translation

mjab@.COM (Michael Berry) (02/07/89)

In a recent posting, Roland Pesch suggested using a standard keyword form
based on the one described in Iverson's "A Dictionary of APL" for posting
APL code to this mailing list.  The code that follows allows users of GNU
Emacs to do that easily.  

If you run the same APL that I do (Sharp APL/UX) you can use this code
unchanged.  If you use a different APL that has the same set of visible
characters in the 8th-bit-on part of @[] av, but in a different order (I
assume that STSC APL for Unix falls in this category), you will have to
change the single variable "visible-over-127" to reflect the @[] av of your
system. 

The variable "visible-over-127" is a list of the @[] av indices of all the
"meta" characters which print out as something interesting in your 8-bit
APL font.  It also contains the code for "@" since the keyword proposal
Roland posted has a keyword for that character.  The translation programs
in this file do *not* translate "@" to "@ " however because find that to be
a problem when translating files (like this one) which are mail messages
and have that character in sensitive parts of net addresses.

If your APL generates a different set of characters than mine does, you
will want to also edit the variables "keywords" and "keywords-unique" which
correspond one-to-one to the elements of "visible-over-127."  I have
included keywords for all the visible characters in @[] av, not just the
real APL characters because I find that these "decorators" have a way of
creeping into character vectors and comments.

INSTALLING THIS CODE

Save this file as "keywords.el" and run M-x byte-compile-file on it.
Add the lisp form (load "keywords") to your .emacs file.

USING THIS CODE

If your current buffer contains APL characters and your want to turn them
into keywords, use M-x convert-keywords-to-apl.

If your current buffer contains keywords (A posting from comp.lang.apl
perhaps!), use M-x convert-apl-to-keywords.

If you want to convert a saved file in place (ie destructively), use 
M-x convert-apl-file-to-keywords or M-x convert-keyword-file-to-apl as
appropriate. 

PS  I have a lot of GNU emacs code to support APL editing including fairly
complete apl- and apl-interaction- modes.  It is far to much code to post
to the net, but I will happily share it with anyone who sends me email
requesting it.

*********** Start of File keywords.el ********************
;;; APL Keywords      mjab@think.com       Tue Feb  7 1989

(defvar visible-over-127 nil "visible quad-av characters in need of translation to keyword form")
(defvar keywords nil "Strings representing APL printing characters not part of normal ascii")
(defvar keywords-unique nil "A form of the APL keywords in which the introducer is never doubled")
(defvar char-to-key-alist nil "This list associates characters with keywords.  The characters are the keys.")
(defvar key-to-char-alist nil "This list associates characters with keywords.  The keywords are the keys.")

(setq visible-over-127
      '(64 160 161 162 164 166 168 169 171 172 173 175 176 177 179 180 185 187 188 189 190 191 193 194 195 196 197 199 200 201 202 204 205 206 207 210 211 213 214 215 216 217 218 219 220 221 223 224 225 226 227 229 230 231 232 233 234 236 238 239 240 244 247 250 251 252 253 254)) 

(setq keywords
      '("@ " "@0~ " "@\" " "@@_ " "@<_ " "@>_ " "@=/ " "@v " "@x " "@,- " "@[-:] " "@/- " "@^~ " "@@! " "@L- " "@Y= " "@v~ " "@[o] " "@[\\] " "@=_ " "@][ " "@@? " "@a " "@@T " "@n " "@L " "@e " "@@D " "@D " "@i " "@o " "@[] " "@I " "@T " "@O " "@r " "@@L " "@Iv " "@u " "@w " "@@c " "@I^ " "@c " "@I- " "@\\- " "@-I " "@-: " "@@I " "@O- " "@@To " "@no " "@e_ " "@@D~ " "@@DI " "@DI " "@i_ " "@\"o " "@['] " "@To " "@\"O " "@O* " "@O\\ " "@OI " "@cI " "@<- " "@D_ " "@-> " "@<> "))

(setq keywords-unique
      '("@ " "@0~ " "@\" " "@._ " "@<_ " "@>_ " "@=/ " "@v " "@x " "@,- " "@[-:] " "@/- " "@^~ " "@.! " "@L- " "@Y= " "@v~ " "@[o] " "@[\\] " "@=_ " "@][ " "@.? " "@a " "@.T " "@n " "@L " "@e " "@.D " "@D " "@i " "@o " "@[] " "@I " "@T " "@O " "@r " "@.L " "@Iv " "@u " "@w " "@.c " "@I^ " "@c " "@I- " "@\\- " "@-I " "@-: " "@.I " "@O- " "@.To " "@no " "@e_ " "@.D~ " "@.DI " "@DI " "@i_ " "@\"o " "@['] " "@To " "@\"O " "@O* " "@O\\ " "@OI " "@cI " "@<- " "@D_ " "@-> " "@<> "))

(setq char-to-key-alist (pairlis visible-over-127 keywords))
(setq keyword-to-char-alist (pairlis keywords-unique visible-over-127))

(defun convert-apl-to-keywords()
  "convert the current buffer to apl keyword form"
  (interactive)
  (save-excursion
    (goto-char 1)
    (while (convert-next-apl-character))))

(defun convert-next-apl-character()
  "convert occurances of next APL character.  Return nil if no APL left."
  (let ((apl)
	(loc)
	(setq case-fold-search nil)
	(setq case-replace nil))
    (skip-chars-forward "[ -~]")
    (setq loc (point))
    (setq apl (buffer-substring loc (1+ loc)))
    (let ((pair (assoc (elt apl 0) char-to-key-alist)))
      (if pair
	  (progn
	    (replace-string apl (cdr pair))
	    (goto-char loc))
	  (progn
	    (if (< 127 (elt apl 0))
		(progn
		  (replace-string apl "@][ ") ;unknown APL chars go to squish-quad
		  (goto-char loc))	;go back to where first occurance was found
		(skip-chars-forward apl)))))) ;not a meta character
    (if (= (point) (point-max))
	nil
	t))


(defun convert-apl-file-to-keywords(file)
  "convert a file in 256-character APL font to Iversonian keywords"
  (interactive "fAPL file name: ")
  (save-excursion
    (find-file file)
    (convert-apl-to-keywords)
    (save-buffer)))

(defun convert-next-keyword()
  "convert occurances of next keyword found to real APL character.  Return nil if no keywords left."
  (let ((pattern "@[^ ]* ")
	(location)
	(case-fold-search nil)		; @"o is not @"O !
	(case-replace nil)
	(apl)
	(key))
    (setq location (list
		     (re-search-forward pattern nil t)
		     (match-beginning 0)
		     (match-end 0)))
    (if (car location)			;pattern was found
	(progn
	  (goto-char (second location))	;Don't miss the one found first!  
	  (setq key (apply 'buffer-substring (cdr location)))
	  (setq apl (cdr (assoc key keyword-to-char-alist)))
	  (if apl
	      (replace-string key (char-to-string apl)))
	  (goto-char (1+ (second location)))	;go back to where first occurance was found
	  t)
	nil)))

(defun convert-keywords-to-apl()
  "convert current buffer from keywords to real APL characters"
  (interactive)
  (save-excursion
    (goto-char 1)
    (replace-string "@@" "@.")
    (goto-char 1)
    (while (convert-next-keyword))))


(defun convert-keyword-file-to-apl(file)
  "convert a file in 256-character APL font to Iversonian keywords"
  (interactive "fAPL file name: ")
  (save-excursion
    (find-file file)
    (convert-apl-to-keywords)
    (save-buffer)))

*********** End of File keywords.el ********************
 		      
	



=============================================
Michael J. A. Berrry

Internet: mjab@think.com
uucp:     {harvard, ihnp4, seismo}!think!mjab
=============================================