home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.006 / xemacs-1 / lib / xemacs-19.13 / lisp / modes / rexx-mode.el < prev    next >
Encoding:
Text File  |  1995-04-17  |  21.6 KB  |  580 lines

  1. ;;; rexx-mode.el --- major mode for editing REXX program files
  2. ;; Keywords: languages
  3.  
  4. ;; Copyright (C) 1993 by Anders Lindgren.
  5.  
  6. ;; This file is part of XEmacs.
  7.  
  8. ;; XEmacs is free software; you can redistribute it and/or modify it
  9. ;; under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation; either version 2, or (at your option)
  11. ;; any later version.
  12.  
  13. ;; XEmacs is distributed in the hope that it will be useful, but
  14. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16. ;; General Public License for more details.
  17.  
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with XEmacs; see the file COPYING.  If not, write to the Free
  20. ;; Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  
  22. ;;; AUTHOR
  23. ;;;    Anders Lindgren, d91ali@csd.uu.se
  24. ;;;
  25. ;;;         Abbrevationtable due to:
  26. ;;;    Johan Bergkvist, nv91-jbe@nada.kth.se
  27. ;;;
  28. ;;; USAGE
  29. ;;;    This file contains code for a GNU Emacs major mode for
  30. ;;;    editing REXX program files.
  31. ;;;
  32. ;;;     Type C-h m in Emacs for information on how to configurate
  33. ;;;    the rexx-mode, or see rexx-mode.doc.
  34. ;;;
  35. ;;;    Put the following lines into your .emacs and rexx-mode
  36. ;;;    will be automatically loaded when editing a REXX program.
  37. ;;;    If rexx-mode shall be used for files with other extensions
  38. ;;;    you can create more (cons ...) lines with these extensions.
  39. ;;;
  40. ;;;    (autoload 'rexx-mode "rexx-mode" "REXX mode" nil t)
  41. ;;;    (setq auto-mode-alist
  42. ;;;          (append
  43. ;;;           (list (cons "\\.rexx$"  'rexx-mode)
  44. ;;;             (cons "\\.elx$"   'rexx-mode)
  45. ;;;             (cons "\\.ncomm$" 'rexx-mode)
  46. ;;;             (cons "\\.cpr$"   'rexx-mode)
  47. ;;;                    )
  48. ;;;           auto-mode-alist))
  49. ;;;
  50. ;;; HISTORY
  51. ;;;    93-01-07 V0.1 ALi    Works for the first time.
  52. ;;;    92-01-11 V0.2 ALi    rexx-calc-indent totally rewritten.
  53. ;;;     93-03-08 V0.3 JB        rexx-indent-and-newline-and-indent added.
  54. ;;;                             Abbrev-table containing 173 entries created.
  55. ;;;                             rexx-check-expansion added.
  56. ;;;                             rexx-mode enables use of abbrev-table.
  57. ;;;    93-03-15 V0.4 ALi    abbrev-mode removed, better to call
  58. ;;;                 (abbrev-mode 1) from the hook.
  59. ;;;                case-fold-search set to t to recognize capital
  60. ;;;                 letters in keywords.
  61. ;;;                Old (setq case-fold-search nil) removed which
  62. ;;;                 prevented the recognition of END.
  63. ;;;                rexx-indent-and-newline-and-indent renamed to
  64. ;;;                 rexx-indent-newline-indent.
  65. ;;;                rexx-i-n-i now only expands abbrevs when
  66. ;;;                 buffer is in abbrev-mode.
  67. ;;;                New rexx-newline-and-indent added.
  68. ;;;    93-03-20      ALi     A serious bug in the routine for checking
  69. ;;;                strings and comments found and fixed.
  70. ;;;               V1.0 Relesed!    
  71. ;;;
  72.  
  73.  
  74. (provide 'rexx-mode)
  75.  
  76. (defconst rexx-indent 8
  77.   "*This variable contains the indentation in rexx-mode.")
  78.  
  79. (defconst rexx-end-indent 0
  80.   "*This variable indicates the relative position of the \"end\" in REXX mode.")
  81.  
  82. (defconst rexx-cont-indent 8
  83.   "*This variable indicates how far a continued line shall be intended.")
  84.  
  85. (defconst rexx-comment-col 32
  86.   "*This variable gives the desired comment column 
  87. for comments to the right of text.")
  88.  
  89. (defconst rexx-tab-always-indent t
  90.   "*Non-nil means TAB in REXX mode should always reindent the current line,
  91. regardless of where in the line point is when the TAB command is used.")
  92.  
  93. (defconst rexx-special-regexp 
  94.   ".*\\(,\\|then\\|else\\)[ \t]*\\(/\\*.*\\*/\\)?[ \t]*$"
  95.   "*Regular expression for parsing lines which shall be followed by
  96. a extra indention")
  97.  
  98. (defvar rexx-mode-map nil
  99.   "Keymap for rexx-mode.")
  100. (if rexx-mode-map
  101.     nil
  102.   (setq rexx-mode-map (make-sparse-keymap))
  103.   (define-key rexx-mode-map "\t"   'rexx-indent-command)
  104.   (define-key rexx-mode-map "\C-m"  'rexx-indent-and-newline)
  105.   (define-key rexx-mode-map 'backspace 'backward-delete-char-untabify)
  106.   (define-key rexx-mode-map "\C-c\C-p" 'rexx-find-matching-do)
  107.   (define-key rexx-mode-map "\C-c\C-c" 'rexx-debug)
  108.   )
  109.  
  110. (defvar rexx-mode-syntax-table nil
  111.   "Syntax table in use in REXX-mode buffers.")
  112.  
  113. (if rexx-mode-syntax-table
  114.     ()
  115.   (setq rexx-mode-syntax-table (make-syntax-table))
  116.   (modify-syntax-entry ?\\ "\\" rexx-mode-syntax-table)
  117.   (modify-syntax-entry ?/ ". 14" rexx-mode-syntax-table)
  118.   (modify-syntax-entry ?* ". 23" rexx-mode-syntax-table)
  119.   (modify-syntax-entry ?+ "." rexx-mode-syntax-table)
  120.   (modify-syntax-entry ?- "." rexx-mode-syntax-table)
  121.   (modify-syntax-entry ?= "." rexx-mode-syntax-table)
  122.   (modify-syntax-entry ?% "." rexx-mode-syntax-table)
  123.   (modify-syntax-entry ?< "." rexx-mode-syntax-table)
  124.   (modify-syntax-entry ?> "." rexx-mode-syntax-table)
  125.   (modify-syntax-entry ?& "." rexx-mode-syntax-table)
  126.   (modify-syntax-entry ?| "." rexx-mode-syntax-table)
  127.   (modify-syntax-entry ?. "_" rexx-mode-syntax-table)
  128.   (modify-syntax-entry ?\' "\"" rexx-mode-syntax-table))
  129.  
  130. (defvar rexx-mode-abbrev-table nil
  131.   "*Abbrev table in use in rexx-mode buffers.")
  132.  
  133. (if rexx-mode-abbrev-table
  134.     nil
  135.   (define-abbrev-table 'rexx-mode-abbrev-table '(
  136.      ("address" "ADDRESS" rexx-check-expansion 0)
  137.      ("arg" "ARG" rexx-check-expansion 0)
  138.      ("break" "BREAK" rexx-check-expansion 0)
  139.      ("call" "CALL" rexx-check-expansion 0)
  140.      ("do" "DO" rexx-check-expansion 0)
  141.      ("drop" "DROP" rexx-check-expansion 0)
  142.      ("echo" "ECHO" rexx-check-expansion 0)
  143.      ("else" "ELSE" rexx-check-expansion 0)
  144.      ("end" "END" rexx-check-expansion 0)
  145.      ("exit" "EXIT" rexx-check-expansion 0)
  146.      ("if" "IF" rexx-check-expansion 0)
  147.      ("interpret" "INTERPRET" rexx-check-expansion 0)
  148.      ("iterate" "ITERATE" rexx-check-expansion 0)
  149.      ("leave" "LEAVE" rexx-check-expansion 0)
  150.      ("nop" "NOP" rexx-check-expansion 0)
  151.      ("numeric" "NUMERIC" rexx-check-expansion 0)
  152.      ("options" "OPTIONS" rexx-check-expansion 0)
  153.      ("otherwise" "OTHERWISE" rexx-check-expansion 0)
  154.      ("parse" "PARSE" rexx-check-expansion 0)
  155.      ("procedure" "PROCEDURE" rexx-check-expansion 0)
  156.      ("pull" "PULL" rexx-check-expansion 0)
  157.      ("push" "PUSH" rexx-check-expansion 0)
  158.      ("queue" "QUEUE" rexx-check-expansion 0)
  159.      ("return" "RETURN" rexx-check-expansion 0)
  160.      ("say" "SAY" rexx-check-expansion 0)
  161.      ("select" "SELECT" rexx-check-expansion 0)
  162.      ("shell" "SHELL" rexx-check-expansion 0)
  163.      ("signal" "SIGNAL" rexx-check-expansion 0)
  164.      ("then" "THEN" rexx-check-expansion 0)
  165.      ("trace" "TRACE" rexx-check-expansion 0)
  166.      ("upper" "UPPER" rexx-check-expansion 0)
  167.      ("when" "WHEN" rexx-check-expansion 0)
  168.      ("value" "VALUE" rexx-check-expansion 0)
  169.      ("to" "TO" rexx-check-expansion 0)
  170.      ("by" "BY" rexx-check-expansion 0)
  171.      ("for" "FOR" rexx-check-expansion 0)
  172.      ("forever" "FOREVER" rexx-check-expansion 0)
  173.      ("while" "WHILE" rexx-check-expansion 0)
  174.      ("until" "UNTIL" rexx-check-expansion 0)
  175.      ("form" "FORM" rexx-check-expansion 0)
  176.      ("digits" "DIGITS" rexx-check-expansion 0)
  177.      ("fuzz" "FUZZ" rexx-check-expansion 0)
  178.      ("scientific" "SCIENTIFIC" rexx-check-expansion 0)
  179.      ("engineering" "ENGINEERING" rexx-check-expansion 0)
  180.      ("failat" "FAILAT" rexx-check-expansion 0)
  181.      ("prompt" "PROMPT" rexx-check-expansion 0)
  182.      ("results" "RESULTS" rexx-check-expansion 0)
  183.      ("upper" "UPPER" rexx-check-expansion 0)
  184.      ("external" "EXTERNAL" rexx-check-expansion 0)
  185.      ("source" "SOURCE" rexx-check-expansion 0)
  186.      ("with" "WITH" rexx-check-expansion 0)
  187.      ("command" "COMMAND" rexx-check-expansion 0)
  188.      ("function" "FUNCTION" rexx-check-expansion 0)
  189.      ("var" "VAR" rexx-check-expansion 0)
  190.      ("version" "VERSION" rexx-check-expansion 0)
  191.      ("expose" "EXPOSE" rexx-check-expansion 0)
  192.      ("on" "ON" rexx-check-expansion 0)
  193.      ("off" "OFF" rexx-check-expansion 0)
  194.      ("abbrev" "ABBREV" rexx-check-expansion 0)
  195.      ("abs" "ABS" rexx-check-expansion 0)
  196.      ("addlib" "ADDLIB" rexx-check-expansion 0)
  197.      ("b2c" "B2C" rexx-check-expansion 0)
  198.      ("bitand" "BITAND" rexx-check-expansion 0)
  199.      ("bitchg" "BITCHG" rexx-check-expansion 0)
  200.      ("bitclr" "BITCLR" rexx-check-expansion 0)
  201.      ("bitcomp" "BITCOMP" rexx-check-expansion 0)
  202.      ("bitor" "BITOR" rexx-check-expansion 0)
  203.      ("bittst" "BITTST" rexx-check-expansion 0)
  204.      ("bitset" "BITSET" rexx-check-expansion 0)
  205.      ("c2b" "C2B" rexx-check-expansion 0)
  206.      ("c2d" "C2D" rexx-check-expansion 0)
  207.      ("c2x" "C2X" rexx-check-expansion 0)
  208.      ("center" "CENTER" rexx-check-expansion 0)
  209.      ("centre" "CENTRE" rexx-check-expansion 0)
  210.      ("close" "CLOSE" rexx-check-expansion 0)
  211.      ("compress" "COMPRESS" rexx-check-expansion 0)
  212.      ("compare" "COMPARE" rexx-check-expansion 0)
  213.      ("copies" "COPIES" rexx-check-expansion 0)
  214.      ("d2c" "D2C" rexx-check-expansion 0)
  215.      ("datatype" "DATATYPE" rexx-check-expansion 0)
  216.      ("delstr" "DELSTR" rexx-check-expansion 0)
  217.      ("delword" "DELWORD" rexx-check-expansion 0)
  218.      ("eof" "EOF" rexx-check-expansion 0)
  219.      ("errortext" "ERRORTEXT" rexx-check-expansion 0)
  220.      ("exists" "EXISTS" rexx-check-expansion 0)
  221.      ("export" "EXPORT" rexx-check-expansion 0)
  222.      ("freespace" "FREESPACE" rexx-check-expansion 0)
  223.      ("getclip" "GETCLIP" rexx-check-expansion 0)
  224.      ("getspace" "GETSPACE" rexx-check-expansion 0)
  225.      ("hash" "HASH" rexx-check-expansion 0)
  226.      ("import" "IMPORT" rexx-check-expansion 0)
  227.      ("index" "INDEX" rexx-check-expansion 0)
  228.      ("insert" "INSERT" rexx-check-expansion 0)
  229.      ("lastpos" "LASTPOS" rexx-check-expansion 0)
  230.      ("left" "LEFT" rexx-check-expansion 0)
  231.      ("length" "LENGTH" rexx-check-expansion 0)
  232.      ("max" "MAX" rexx-check-expansion 0)
  233.      ("min" "MIN" rexx-check-expansion 0)
  234.      ("open" "OPEN" rexx-check-expansion 0)
  235.      ("overlay" "OVERLAY" rexx-check-expansion 0)
  236.      ("pos" "POS" rexx-check-expansion 0)
  237.      ("pragma" "PRAGMA" rexx-check-expansion 0)
  238.      ("random" "RANDOM" rexx-check-expansion 0)
  239.      ("randu" "RANDU" rexx-check-expansion 0)
  240.      ("readch" "READCH" rexx-check-expansion 0)
  241.      ("readln" "READLN" rexx-check-expansion 0)
  242.      ("remlib" "REMLIB" rexx-check-expansion 0)
  243.      ("reverse" "REVERSE" rexx-check-expansion 0)
  244.      ("right" "RIGHT" rexx-check-expansion 0)
  245.      ("seek" "SEEK" rexx-check-expansion 0)
  246.      ("setclip" "SETCLIP" rexx-check-expansion 0)
  247.      ("show" "SHOW" rexx-check-expansion 0)
  248.      ("sign" "SIGN" rexx-check-expansion 0)
  249.      ("space" "SPACE" rexx-check-expansion 0)
  250.      ("storage" "STORAGE" rexx-check-expansion 0)
  251.      ("strip" "STRIP" rexx-check-expansion 0)
  252.      ("substr" "SUBSTR" rexx-check-expansion 0)
  253.      ("subword" "SUBWORD" rexx-check-expansion 0)
  254.      ("symbol" "SYMBOL" rexx-check-expansion 0)
  255.      ("time" "TIME" rexx-check-expansion 0)
  256.      ("trace" "TRACE" rexx-check-expansion 0)
  257.      ("translate" "TRANSLATE" rexx-check-expansion 0)
  258.      ("trim" "TRIM" rexx-check-expansion 0)
  259.      ("verify" "VERIFY" rexx-check-expansion 0)
  260.      ("word" "WORD" rexx-check-expansion 0)
  261.      ("wordindex" "WORDINDEX" rexx-check-expansion 0)
  262.      ("wordlength" "WORDLENGTH" rexx-check-expansion 0)
  263.      ("words" "WORDS" rexx-check-expansion 0)
  264.      ("writech" "WRITECH" rexx-check-expansion 0)
  265.      ("writeln" "WRITELN" rexx-check-expansion 0)
  266.      ("x2c" "X2C" rexx-check-expansion 0)
  267.      ("xrange" "XRANGE" rexx-check-expansion 0)
  268.      ("allocmem" "ALLOCMEM" rexx-check-expansion 0)
  269.      ("baddr" "BADDR" rexx-check-expansion 0)
  270.      ("bitxor" "BITXOR" rexx-check-expansion 0)
  271.      ("break_c" "BREAK_C" rexx-check-expansion 0)
  272.      ("break_d" "BREAK_D" rexx-check-expansion 0)
  273.      ("break_e" "BREAK_E" rexx-check-expansion 0)
  274.      ("break_f" "BREAK_F" rexx-check-expansion 0)
  275.      ("cache" "CACHE" rexx-check-expansion 0)
  276.      ("closeport" "CLOSEPORT" rexx-check-expansion 0)
  277.      ("d2x" "D2X" rexx-check-expansion 0)
  278.      ("date" "DATA" rexx-check-expansion 0)
  279.      ("delay" "DELAY" rexx-check-expansion 0)
  280.      ("delete" "DELETE" rexx-check-expansion 0)
  281.      ("error" "ERROR" rexx-check-expansion 0)
  282.      ("failure" "FAILURE" rexx-check-expansion 0)
  283.      ("find" "FIND" rexx-check-expansion 0)
  284.      ("forbid" "FORBID" rexx-check-expansion 0)
  285.      ("freemem" "FREEMEM" rexx-check-expansion 0)
  286.      ("getarg" "GETARG" rexx-check-expansion 0)
  287.      ("getpkt" "GETPKT" rexx-check-expansion 0)
  288.      ("halt" "HALT" rexx-check-expansion 0)
  289.      ("ioerr" "IOERR" rexx-check-expansion 0)
  290.      ("lines" "LINES" rexx-check-expansion 0)
  291.      ("makedir" "MAKEDIR" rexx-check-expansion 0)
  292.      ("next" "NEXT" rexx-check-expansion 0)
  293.      ("novalue" "NOVALUE" rexx-check-expansion 0)
  294.      ("null" "NULL" rexx-check-expansion 0)
  295.      ("offset" "OFFSET" rexx-check-expansion 0)
  296.      ("openport" "OPENPORT" rexx-check-expansion 0)
  297.      ("permit" "PERMIT" rexx-check-expansion 0)
  298.      ("rename" "RENAME" rexx-check-expansion 0)
  299.      ("reply" "REPLY" rexx-check-expansion 0)
  300.      ("showdir" "SHOWDIR" rexx-check-expansion 0)
  301.      ("showlist" "SHOWLIST" rexx-check-expansion 0)
  302.      ("sourceline" "SOURCELINE" rexx-check-expansion 0)
  303.      ("statef" "STATEF" rexx-check-expansion 0)
  304.      ("syntax" "SYNTAX" rexx-check-expansion 0)
  305.      ("trunc" "TRUNC" rexx-check-expansion 0)
  306.      ("typepkt" "TYPEPKT" rexx-check-expansion 0)
  307.      ("waitpkt" "WAITPKT" rexx-check-expansion 0)
  308.      ("x2d" "X2D" rexx-check-expansion 0))))
  309.  
  310. ;;;###autoload
  311. (defun rexx-mode ()
  312. "Major mode for editing REXX code.
  313. \\{rexx-mode-map}
  314.  
  315. Variables controlling indentation style:
  316.  rexx-indent
  317.     The basic indentation for do-blocks.
  318.  rexx-end-indent
  319.     The relative offset of the \"end\" statement. 0 places it in the
  320.     same column as the statements of the block. Setting it to the same
  321.     value as rexx-indent places the \"end\" under the do-line.
  322.  rexx-cont-indent
  323.     The indention for lines following \"then\", \"else\" and \",\"
  324.     (continued) lines.
  325.  rexx-tab-always-indent
  326.     Non-nil means TAB in REXX mode should always reindent the current 
  327.     line, regardless of where in the line the point is when the TAB
  328.     command is used.
  329.  
  330. If you have set rexx-end-indent to a nonzero value, you probably want to
  331. remap RETURN to rexx-indent-newline-indent. It makes sure that lines
  332. indents correctly when you press RETURN.
  333.  
  334. An extensive abbrevation table consisting of all the keywords of REXX are
  335. supplied. Expanded keywords are converted into upper case making it
  336. easier to distinguish them. To use this feature the buffer must be in
  337. abbrev-mode. (See example below.)
  338.  
  339. Turning on REXX mode calls the value of the variable rexx-mode-hook with
  340. no args, if that value is non-nil.
  341.  
  342. For example:
  343. (setq rexx-mode-hook '(lambda ()
  344.             (setq rexx-indent 4)
  345.             (setq rexx-end-indent 4)
  346.             (setq rexx-cont-indent 4)
  347.             (local-set-key \"\\C-m\" 'rexx-indent-newline-indent)
  348.             (abbrev-mode 1)
  349.             ))
  350.  
  351. will make the END aligned with the DO/SELECT. It will indent blocks and
  352. IF-statenents four steps and make sure that the END jumps into the
  353. correct position when RETURN is pressed. Finaly it will use the abbrev
  354. table to convert all REXX keywords into upper case."
  355.   (interactive)
  356.   (kill-all-local-variables)
  357.   (use-local-map rexx-mode-map)
  358.   (set-syntax-table rexx-mode-syntax-table)
  359.   (setq major-mode 'rexx-mode)
  360.   (setq mode-name "REXX")
  361.   (setq local-abbrev-table rexx-mode-abbrev-table)
  362.   (make-local-variable 'case-fold-search)
  363.   (setq case-fold-search t)
  364.   (make-local-variable 'paragraph-start)
  365.   (setq paragraph-start (concat "^$\\|" page-delimiter))
  366.   (make-local-variable 'paragraph-separate)
  367.   (setq paragraph-separate paragraph-start)
  368.   (make-local-variable 'paragraph-ignore-fill-prefix)
  369.   (setq paragraph-ignore-fill-prefix t)
  370.   (make-local-variable 'indent-line-function)
  371.   (setq indent-line-function 'rexx-indent-command)
  372.   (make-local-variable 'parse-sexp-ignore-comments)
  373.   (setq parse-sexp-ignore-comments t)
  374.   (make-local-variable 'comment-start)
  375.   (setq comment-start "/* ")
  376.   (make-local-variable 'comment-end)
  377.   (setq comment-end " */")
  378.   (make-local-variable 'comment-column)
  379.   (setq comment-column 32)
  380.   (make-local-variable 'comment-start-skip)
  381.   (setq comment-start-skip "/\\*+ *")
  382.   (make-local-variable 'comment-indent-hook)
  383.   (setq comment-indent-hook 'rexx-comment-indent)
  384.   (run-hooks 'rexx-mode-hook))
  385.  
  386.  
  387. (defun rexx-indent-command (&optional whole-exp)
  388.   "Indent the current line as REXX code."
  389.   (interactive "P")
  390.   (if whole-exp
  391.       (let ((shift-amt (rexx-indent-line))
  392.         beg
  393.         end)
  394.     (save-excursion
  395.       (if rexx-tab-always-indent
  396.           (beginning-of-line))
  397.       (setq beg (point))
  398.       (forward-sexp 1)
  399.       (setq end (point))
  400.       (goto-char beg)
  401.       (forward-line 1)
  402.       (setq beg (point)))
  403.     (if (> end beg)
  404.         (indent-code-rigidly beg end shift-amt)))
  405.     (if (and (not rexx-tab-always-indent)
  406.          (save-excursion
  407.           (skip-chars-backward " \t")
  408.           (not (bolp))))
  409.     (insert-tab)
  410.       (rexx-indent-line))))
  411.  
  412. (defun rexx-indent-line ()
  413.   "Indent the current line as REXX code.
  414. Return the amount the indentation changed by."
  415.   (let ((indent (rexx-calc-indent))
  416.     beg
  417.     shift-amt
  418.         (pos (- (point-max) (point))))
  419.     (beginning-of-line)
  420.     (setq beg (point))
  421.     (cond ((eq indent nil) (setq indent (current-indentation)))
  422.       ((eq indent t) (setq indent (rexx-calculate-indent-within-comment)))
  423.       ((looking-at "[ \t]*#") (setq indent 0))
  424.       (t (skip-chars-forward " \t")
  425.          (if (listp indent) (setq indent (car indent)))
  426.           ;; /* Sprekspecifik kod! */
  427.          (if (looking-at "end") (setq indent (- indent rexx-end-indent)))))
  428.     (skip-chars-forward " \t")
  429.     (setq shift-amt (- indent (current-column)))
  430.     (if (zerop shift-amt)
  431.     (if (> (- (point-max) pos) (point))
  432.         (goto-char (- (point-max) pos)))
  433.       (delete-region beg (point))
  434.       (indent-to indent)
  435.       (if (> (- (point-max) pos) (point))
  436.       (goto-char (- (point-max) pos))))
  437.     shift-amt))
  438.  
  439. (defun rexx-calc-indent ()
  440.   "Return the appropriate indentation for this line as an int."
  441.   (save-excursion
  442.     (beginning-of-line)
  443.     (let ((block (rexx-find-environment))
  444.       beg
  445.       state
  446.       indent)
  447.       (save-excursion (setq state (rexx-inside-comment-or-string)))
  448.       (cond ((or (nth 3 state) (nth 4 state))
  449.          (nth 4 state))    ;; Inside a comment or string
  450.         (t    
  451.          ;; Find line to indent current line after.
  452.          (rexx-backup-to-noncomment 1)
  453.          (beginning-of-line)
  454.          (setq beg (rexx-find-environment))
  455.          (while (> beg block)
  456.            (goto-char beg)
  457.            (beginning-of-line)
  458.            (setq beg (rexx-find-environment)))
  459.  
  460.          (if (> (point) block)         
  461.          ;; Check to see if we shall make a special indention
  462.          (if (looking-at rexx-special-regexp)
  463.              (+ (current-indentation) rexx-cont-indent)
  464.            ;; If not, find the basic indention by stepping
  465.            ;; by all special indented lines.
  466.            (progn
  467.              (setq indent (current-indentation))
  468.              (rexx-backup-to-noncomment 1)
  469.              (beginning-of-line)
  470.              (while (looking-at rexx-special-regexp)
  471.                (setq indent (current-indentation))
  472.                (rexx-backup-to-noncomment 1)
  473.                (beginning-of-line))
  474.              indent))
  475.            (if (= 1 block)
  476.            0
  477.          ;; Indent after the do-line.
  478.          (progn
  479.            (goto-char block)
  480.            (+ (current-indentation) rexx-indent)))))))))
  481.  
  482. (defun rexx-backup-to-noncomment (lim)
  483.   "Backup the point to the previous noncomment REXX line."
  484.   (let (stop)
  485.     (while (not stop)
  486.       (skip-chars-backward " \t\n\f" lim)
  487.       (if (and (>= (point) (+ 2 lim))
  488.            (save-excursion
  489.          (forward-char -2)
  490.          (looking-at "\\*/")))
  491.       (search-backward "/*" lim 'move)
  492.     (setq stop t)))
  493.     (>= (point) lim)))
  494.  
  495. (defun rexx-find-environment ()
  496.   "Return the position of the corresponding \"do\" or \"select\".
  497. If none found, return the beginning of buffer."
  498.   (save-excursion
  499.     (let ((do-level 1)
  500.       (cont t)
  501.       state)
  502.       (while (and cont (not (zerop do-level)))
  503.     (setq cont (re-search-backward "\\b\\(do\\|select\\|end\\)\\b" 1 t))
  504.     (save-excursion (setq state (rexx-inside-comment-or-string)))
  505.     (setq do-level (+ do-level
  506.               (cond ((or (nth 3 state) (nth 4 state)) 0)
  507.                 ((looking-at "do") -1)
  508.                 ((looking-at "select") -1)
  509.                 ((looking-at "end") +1)
  510.                 (t 0)))))
  511.  
  512.       (if cont (point) 1))))
  513.  
  514. (defun rexx-calculate-indent-within-comment ()
  515.   "Return the indentation amount for line, assuming that
  516. the current line is to be regarded as part of a block comment."
  517.   (let (end star-start)
  518.     (save-excursion
  519.       (beginning-of-line)
  520.       (skip-chars-forward " \t")
  521.       (setq star-start (= (following-char) ?\*))
  522.       (skip-chars-backward " \t\n")
  523.       (setq end (point))
  524.       (beginning-of-line)
  525.       (skip-chars-forward " \t")
  526.       (and (re-search-forward "/\\*[ \t]*" end t)
  527.        star-start
  528.        (goto-char (1+ (match-beginning 0))))
  529.       (current-column))))
  530.  
  531. (defun rexx-comment-indent ()
  532.   (if (looking-at "^/\\*")
  533.       0                ;Existing comment at bol stays there.
  534.     (save-excursion
  535.       (skip-chars-backward " \t")
  536.       (max (1+ (current-column))    ;Else indent at comment column
  537.        comment-column))))    ; except leave at least one space.
  538.  
  539. (defun rexx-find-matching-do ()
  540.   "Set mark, look for the \"do\" or \"select\" for the present block."
  541.   (interactive)
  542.   (set-mark-command nil)
  543.   (beginning-of-line)
  544.   (goto-char (rexx-find-environment)))
  545.  
  546. (defun rexx-check-expansion ()
  547.   "If abbrev was made within a comment or a string, de-abbrev!"
  548.   (let ((state (rexx-inside-comment-or-string)))
  549.     (if (or (nth 3 state) (nth 4 state))
  550.     (unexpand-abbrev))))
  551.  
  552. (defun rexx-inside-comment-or-string ()
  553.   "Check if the point is inside a comment or a string.
  554. It returns the state from parse-partial-sexp for the search that
  555. terminated on the points position"
  556.   (let ((origpoint (point))
  557.     state)
  558.     (save-excursion 
  559.       (goto-char 1)
  560.       (while (> origpoint (point))
  561.     (setq state (parse-partial-sexp (point) origpoint 0))))
  562.     state))
  563.  
  564. (defun rexx-indent-and-newline ()
  565.   "New newline-and-indent which expands abbrevs before running
  566. a regular newline-and-indent."
  567.   (interactive)
  568.   (if abbrev-mode 
  569.       (expand-abbrev))
  570.   (newline-and-indent))
  571.  
  572. (defun rexx-indent-newline-indent ()
  573.   "New newline-and-indent which expands abbrevs and indent the line
  574. before running a regular newline-and-indent."
  575.   (interactive)
  576.   (rexx-indent-command)
  577.   (if abbrev-mode 
  578.       (expand-abbrev))
  579.   (newline-and-indent))
  580.