[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

ctrl-lang.el [LONG]

From: Evans Winner
Subject: ctrl-lang.el [LONG]
Date: 19 Jan 2007 21:50:22 -0800
User-agent: G2/1.0

FWIW, this is a generic major mode for font-lock and doc lookup for IBM
AS/400 (aka iSeries) Control Language files.  Just the ticket for me
and the three other people on the planet who would want such a thing.

;;; ctrl-lang.el --- generic AS/400 Control Language mode

;; Version: 0.1
;; Copyright (C) 2006 thorne
;; Time-stamp: <2006-08-13 07:19:50 ewinner>
;; Author: thorne <address@hidden>
;; Created: 2006.8.3
;; Keywords: files languages
;; Favorite Color: Beer, as in Emacs.

;; This file is not part of GNU Emacs.
;; This program is free software; you can redistribute it
;; and/or modify it under the terms of the GNU General
;; Public License as published by the Free Software
;; Foundation; either version 2 of the License, or (at your
;; option) any later version.
;; This program is distributed in the hope that it will be
;; useful, but WITHOUT ANY WARRANTY; without even the
;; implied warranty of MERCHANTABILITY or FITNESS FOR A
;; PARTICULAR PURPOSE.  See the GNU General Public License
;; for more details.
;; You should have received a copy of the GNU General Public
;; License along with this program; if not, write to the
;; Free Software Foundation, Inc., 59 Temple Place, Suite
;; 330, Boston, MA 02111-1307 USA

;;; Commentary:

;; PURPOSE: For editing IBM AS/400 (aka iSeries) CL program
;; source code (CL not to be confused with Common Lisp in
;; this context).  Mostly just syntax highlighting, mostly.
;; Not much fun, really, and a bit fruity.
;; USE: Load or autoload this file, then visit a CL source
;; file and then do `M-x ctrl-lang-mode'.
;; MISC: I am not a programmer really, and this is as much
;; about trying to learn some Emacs Lisp as anything else.
;; So, (standard disclamer...) do me the favor of letting me
;; know if there is stuff that is seriously dumb about it or
;; needs fixing, etc.  I would appreciate it.
;; REQUIRES: This package uses the generic mode package,
;; `generic.el'.

;;; Change Log:
;;  Version 0.1
;;  * initial release

;;; Bugs:
;; * `?%' needs a space after it or else the following CL
;;   command won't get font-locked.  Dunno why.
;; * Can't embed strings in comments.  I can't find a work-
;;   around.

;;; Code:

;;  ctrl-lang-doc-base-address --  If IBM ever changes their
;;                                 URL for CL docs, or if
;;                                 you want to use the docs
;;                                 for a more release of
;;                                 OS/400 change this.
;;  site-commands              --  Add your own custom Cl
;;                                 commands here.
;; Other than that, see below.

;; This hack uses browse-url and the ibm site.
;; For the docs, concatenates the base URL with the first 8
;; letters of the command name (then `.htm').
;; For the syntax diagram, it's URL - command name (all of
;; it) then `syn.htm'.  Don't ask me why.
(defconst ctrl-lang-doc-base-address
"Base web address to use for automatic command doc lookup.")

;; To be closer to normal CL coding standards:
(defvar ctrl-lang-tab-width 11
  "*`tab-with' to use in `ctrl-lang-mode'.")
(defvar ctrl-lang-fill-column 68
  "*`fill-column' value to use in `ctrl-lang-mode'.")

;; Site-specific CL commands:
(setq site-commands
       "FOOBARBAZ" "FROM3XB"))

;; Setup
(require 'generic)
(require 'browse-url)  ; For the doc lookup feature.

;; This is for the giant list of commands.  I don't know how
;; to do this without clobbering people's default setting.
(setq max-specpdl-size 2000)

;; Helper functions and misc.

;; Called by default with the mode hook.  Set the values in
;; the User-changable variables section above.  I think it's
;; buffer local...
(defun ctrl-lang-env-setup ()
"Set `fill-column' and `tab-width' for `ctrl-lang-mode'.
Called by `ctrl-lang-mode-hook'."
(setq tab-width ctrl-lang-tab-width)
(setq fill-column ctrl-lang-fill-column))

(defun ctrl-lang-add-mode-line ()
"Add text: `/* -*- Mode: ctrl-lang -*- */' to top of buffer.
Allows Emacs to open the file in ctrl-lang mode
automatically, since we can't use `auto-mode-alist'
in the context of files without a distinctive file
extension--as is the case with CL source files."
        (goto-char (point-min))
        (insert "/* -*- Mode: ctrl-lang -*- */\n")))
(global-set-key "\C-c\C-lm" 'ctrl-lang-add-mode-line)

(defun ctrl-lang-get-doc ()
"Browse IBM's online documentation for the selected command.

Uses function `point' and function `mark', so the whole CL
command and nothing but the whole CL command must be
selected when this function is run."
 (concat ctrl-lang-doc-base-address
         (downcase (buffer-substring (mark)
                           (if (< (- (point) (mark)) 8)
                             (+ (mark) 8))))
(global-set-key "\C-c\C-ld" 'ctrl-lang-get-doc)

(defun ctrl-lang-get-syntax ()
"Browse IBM's online syntax diagram for selected command.

Uses function `point' and function `mark', so the whole CL
command and nothing but the whole CL command must be
selected when this function is run."
 (concat ctrl-lang-doc-base-address
         (downcase (buffer-substring (mark) (point)))
(global-set-key "\C-c\C-ls" 'ctrl-lang-get-syntax)

;;; Data:

;; These are the giant lists of CL commands.  (Stupid?)
;; We are doing two lists.  One for most everything and
;; one for just flow control and some other keyword-like
;; CL commands, like PGM and IF.  The first we do in
;; `font-lock-function-name-face', the second we do in
;; `font-lock-keyword-face'.

;; This one is open to additions...
(setq keyword-list

;; The giant list... Commented items have been moved to the
;; keyword list, but are left in in case.  I am leaving it
;; in not-compact, but easy-to-maintain one-to-a-line
;; format.  Sorry.
;; This comes from IBM's listing for V5R2
(setq command-list

(define-generic-mode 'ctrl-lang-mode
  nil                   ; Keywords
  (list                 ; Everything else.  Order matters.
;; Strings
   '("'[^']*'" . 'font-lock-string-face)
;; Comments
   '("/\\*.*\\*/" .      ; I think this works.
;; Labels

     . 'font-lock-constant-face)
;; Line continuations
   '("[-+]$" . 'font-lock-warning-face)
;; Command shortcuts
   '("||" . 'font-lock-function-name-face)
   '("|<" . 'font-lock-function-name-face)
   '("|>" . 'font-lock-function-name-face)
;; For params that look like (*)
   '("(\\(\\*\\))" 1 font-lock-type-face)
;; Data thingie
   '("^//[ \\t]" . 'font-lock-builtin-face)
;; The list of keywords
   (generic-make-keywords-list command-list
;; The giant list of CL commands
   (generic-make-keywords-list keyword-list
;; Site-specific CL commands
   (generic-make-keywords-list site-commands
;; Builtin variable recasters... or whatever you call them.
   '("%BINARY" . 'font-lock-builtin-face)
   '("%BIN" . 'font-lock-builtin-face)
   '("%SST" . 'font-lock-builtin-face)
   '("%SUBSTRING" . 'font-lock-builtin-face)
   '("%SWITCH" . 'font-lock-builtin-face)
;; Prompt operators
   '("[ \t]\\?[<*&%?]?" . 'font-lock-warning-face)
;; Math operators
   '("[ \t][-*/+=][ \t]" . 'font-lock-builtin-face)
;; Variables

     . 'font-lock-variable-name-face)
;; Params
   '("\\*[A-Z][A-Z]*" . 'font-lock-type-face))
  nil                   ; File associations.  Can't use.
                        ; Use mode line string instead.
  (list (function
         (lambda ()     ; I think this makes font-lock
                        ; case insensitive
           (make-local-variable 'font-lock-defaults)
           (setq font-lock-defaults
                  'generic-font-lock-defaults nil t)))))
"Generic mode for AS/400 Control Language files.
Syntax highlighting (almost) only.

`ctrl-lang-add-mode-line' -- Add a magic mode line for the
                             mode to the start of the buffer
                             \(embedded in a CL comment.\)
`ctrl-lang-get-doc'       -- Use browse-url to get IBM's
                             online documentation page for
                             the selected CL command.
`ctrl-lang-get-syntax'    -- Use browse-url to get IBM's
                             online syntax diagram page for
                             the selected CL command.

(add-hook 'ctrl-lang-mode-hook 'auto-fill-mode)
(add-hook 'ctrl-lang-mode-hook 'ctrl-lang-env-setup)

(provide 'ctrl-lang)

;;; ctrl-lang.el ends here

reply via email to

[Prev in Thread] Current Thread [Next in Thread]