The NEXT thing to try!!

Icon

Random rants and tips about NETCF, WinCE, Emacs, Powershell etc.

Using Autoinserting(emacs) for actionscript

This is my post second post in this series. After spending some time in lisp, I have managed auto-insert to work for actionscript.

Emacs has got Autoinserting which allows putting some predefined code in a file when you create it first time. This is a familiar concept for people who use IDEs like FlexBuilder for coding.

In flex builder when we create a class first time, it asks for the classname and other information like base class, interfaces and dump some code for the first time. Autoinserting is the answer to achieve such a functionality in emacs.

It works as follows:

  1. When you create a new file with .as extension, it prompts for the base class name
  2. Pressing ENTER will imply no base class otherwise it will insert the required code (import statement and extends string in the template)
  3. After that it asks for package name, again pressing ENTER will mean default (i.e top level package) otherwise package name given will be taken.
  4. It repeatedly keeps asking for the interfaces that you plan to implement, unless you press ENTER

Here is the required code you need to add to your .emacs file.

P.S :=> Don’t forget to set the flex-home variable

(auto-insert-mode t)
(setq auto-insert-alist
      (append '(((actionscript-mode .  "Actionscript Mode") . insert-as-template))
              auto-insert-alist))

(defvar flex-home "C:/flex/sdk/framework/src")

(defun get-dot-seperated (file-name)
  (interactive)
  (setq file-name (substring file-name 1 )) ;; Get rid of the first /
  (setq parts (split-string file-name "/")) ;; split string into parts based on /
  ;;Start creating . seperated import string
  (setq file-dot-path "")
  (while parts
    (setq token (car parts))
    (setq file-dot-path (concat   file-dot-path "." token))
    (setq parts (cdr parts)))
  (substring file-dot-path 1))

(defun insert-as-template ()
  (interactive)
  (let ((baseclass (read-file-name "[]base class?" flex-home))
        (package (read-string "[]Package?" ""))
        ;;(title (substring (file-name-nondirectory buffer-file-name) 0 -3))
        (title (file-name-sans-extension (file-name-nondirectory buffer-file-name)))
        (tab "t"))

    (insert (concat
             "package " package "n"
             "{n"))

    (setq hasBaseClass (not (or (= (length baseclass) 0)
                   (equal buffer-file-name baseclass))))

    (when hasBaseClass
      (setq foo (length flex-home))
      (setq base-class-name (substring baseclass foo ( - (length baseclass) 3))) ;;get /mx/controls/Datagrid from full path
      (insert (concat "import " (get-dot-seperated base-class-name) ";n")))

    (setq point-for-imports (point))

    (insert (concat "public class " title))

    (if hasBaseClass
        (insert (concat " extends " (file-name-sans-extension (file-name-nondirectory baseclass)))))

    (setq point-after-class (point))

    (insert (concat
             "n"
            "{n"
            "n"
            tab "//--------------------------------------------------------------------------n"
            tab "//n"
            tab "//  Constructorn"
            tab "//n"
            tab "//--------------------------------------------------------------------------n"
            "n"
            tab "/**n"
            tab "*  Constructor.n"
            tab "*/n"
            tab "public function " title "()n"
            tab "{n"
            tab tab "//Start from heren" tab tab))

   ;;bookmark this point, so that we can place the cursor here when finished
    (setq point-beginning (point))
    (insert (concat
            "n"
            tab "}n"
            "}n"
            "}n"
            )))

  (setq imports-for-interfaces "")
  (setq implements-string "")
  (setq base-class-path "")
  (setq prompt t)
  (while prompt
    (let ((interface (read-file-name "[]implements?" flex-home)))

      (when (not (or (= (length interface) 0)
                     (equal buffer-file-name interface)))

        (setq foo (length flex-home))
        (setq interface-name (substring interface foo ( - (length interface) 3))) ;;get /mx/controls/Datagrid from full path
        (setq imports-for-interfaces (concat imports-for-interfaces "import " (get-dot-seperated interface-name) ";n"))
        (setq implements-string (concat implements-string (file-name-sans-extension (file-name-nondirectory interface-name)) ",")))

      (if (or (= (length interface) 0)
              (equal buffer-file-name interface))
          (setq prompt nil))))

  (when (> (length implements-string) 0)
    (setq implements-string (substring implements-string 0 (1- (length implements-string))))
    (goto-char point-after-class)
    (insert (concat "nt" "implements " implements-string))
    (setq point-beginning (+ point-beginning (length implements-string))))

  (when (> (length imports-for-interfaces) 0)
    (goto-char point-for-imports)
    (insert (concat imports-for-interfaces "n"))
    (setq point-beginning (+ point-beginning (length imports-for-interfaces))))

  (goto-char point-beginning))

As a result I get this as my start file:

package src
{
import mx.controls.Tree;
import mx.controls.treeClasses.ITreeDataDescriptor;
import mx.collections.IViewCursor;

public class MyTree extends Tree
	implements ITreeDataDescriptor,IViewCursor
{

	//--------------------------------------------------------------------------
	//
	//  Constructor
	//
	//--------------------------------------------------------------------------

	/**
	*  Constructor.
	*/
	public function MyTree()
	{
		//Start from here

	}
}
}
Advertisements

Filed under: actionscript, emacs

Using Imenu (emacs) for actionscript

If you use emacs as your editor while programming in actionscript, then this tweak could be really useful for you.

Emacs has got Imenu facility which allows finding major definitions in a file/buffer. In the context of actionscript this includes class definitions, private/protected/public variables, getters/setters and ofcourse functions. Thus, using Imenu you can bind your keys so that it jump on the definition of a function when those keys are pressed. When you change the contents of the file, you can press

*Rescan*

to make it re-read the buffer and generate a new indexing of the file contents. You can also set

'imenu-auto-rescan'

to a non-nil value so that it automatically scans your file, everytime you make any changes. Here is screenshot of the Imenu:

actionscript-mode is already available for emacs and thus by using the mode you can get all sort of code-coloring. After adding actionscript-mode to your mode-list, you need to add this code to your .emacs to get the desired functionality.


(add-hook 'actionscript-mode-hook 'imenu-add-menubar-index)
(add-hook 'actionscript-mode-hook 'actionscript-init-imenu)
(defun actionscript-init-imenu ()
  (interactive)
  (setq imenu-generic-expression as-imenu-as-generic-expression)
  (global-set-key [mouse-3] 'imenu))(defvar as-imenu-as-generic-expression
  ` (
    ("variables"
     ,(concat
       "^"
        "[ t]*(public|protected|mx_internal|private)"
        "[ t]+var"
        "[ t]+([a-zA-Z_][a-zA-Z0-9_]*)"     ; match function name
        "[ t]*:([a-zA-Z_][a-zA-Z0-9_]*)"        ), 2)

    ;; Getters
    ("Getters"
     ,(concat
       "^"
       "[ t]*(override[ tn]+)?"
        "[ t]*(public|protected|mx_internal|private)"
        "[ t]+function"
        "[ t]+"
        "(get[ t]+([a-zA-Z_][a-zA-Z0-9_]*)[ t]*)()"
       ) 3)

    ;;setters
    ("setters"
     ,(concat
       "^"
       "[ t]*(override[ tn]+)?"
        "[ t]*(public|protected|mx_internal|private)"
        "[ t]+function"
        "[ t]+"
        "(set[ t]+([a-zA-Z_][a-zA-Z0-9_]*)[ t]*)"
        "("
        "([a-zA-Z_][a-zA-Z0-9_]*):([a-zA-Z_][a-zA-Z0-9_]*)[ tn]*"
        ")"
       ) 3)

;; Class definitions
    (nil
     ,(concat
         "^"
         "((public|protected|mx_internal|private)[ t]+)?"
         "(class|interface)[ t]+"
         "("                                ; the string we want to get
         "[a-zA-Z0-9_]+"                      ; class name
         ")"
         "[ tn]*"
         "(extends [ tn]*[a-zA-Z0-9_]+)?"
         "[ tn]*";;[:{]"
         "(implements [ tn]*([a-zA-Z0-9_]+[,][ tn]*)*[a-zA-Z0-9_])?"
         "[ tn]*";;[:{]"
         ) 4)

;; General function name regexp
    (nil
     ,(concat
       "^"
       "[ t]*(override[ tn]+)?"
        "[ t]*(public|protected|mx_internal|private)"
       "([ t]+static)?"
        "[ t]+function"
        "[ t]+([a-zA-Z_][a-zA-Z0-9_]*)"     ; match function name
        "[ t]*("
        "[^)]*)"
       ) 4)
    )
  "Imenu generic expression for C++ mode.  See `imenu-generic-expression'.")

Attached is my .emacs file

Filed under: actionscript, emacs

actionscript-mode for emacs!!!

I have been working on actionscript for more than 8 months. And had’t heard about any mode available for actionscript so far. But recently I found one. Emacs is my weakness and without emacs I really feel weak.

But thankfully now someone has written an actionscript-mode for emacs also. The workaround was to use javascript-mode or java-mode. But it somehow doesn’t give you that good feeling. Though I have yet to discover all the good offerings this mode has. But in case you are interested you can find the mode here.

And to add to it this mode supports AS3. So what are you waiting for?

Filed under: actionscript, emacs

About the author

avatar
25, Male, living in Hyderabad Mobile Developer, working for Microsoft IDC

View Nisheet Jain's profile on LinkedIn

RSS Mobile Dev Blog

  • An error has occurred; the feed is probably down. Try again later.