; Copyright 1996-2004 by Salvador E. Tropea [SET],
; see copyrigh file for details
;
;;;
;;; Here are 3 macros converted from TEML to sLisp
;;;
;******************************************************************
;
; MACRO:   MakeFuncText: Sample macro to comment a function
;
; USE:     Put the cursor immediately after a function name which is
;          at the left of the screen, then press Alt-T. Do not include
;          the return type or parameters before using this macro.
;
;******************************************************************

(defmacro 'Make Function Text'
 (eval
  ; add white space
  (InsertText '\n\n')
  ; go before beginning of intended function name
  (SendCommands cmcLineUp cmcLineUp cmcLineStart
  ; mark function name
                cmcSelectOn cmcWordRight cmcSelectOff)
  ; copy for prototyping
  (SendCommands cmcLineStart cmcLineDown cmcCopyBlock cmcLineUp cmcLineStart)
  ; add "Function" to comment area
  (InsertText '\nFunction: ')
  ; put in comment lines before and after
  (SendCommands cmcLineEnd cmcLineUp cmcLineStart)
  (InsertText '/*****************************************************')
  (SendCommands cmcLineStart cmcLineDown cmcLineEnd)
  (InsertText '\n\n')
  (SendCommands cmcLineStart)
  (InsertText 'Description:\n')
  (SendCommands cmcLineStart)
  (InsertText '*****************************************************/\n')
  ; go back to end of name
  (SendCommands cmcLineDown cmcLineEnd)
  )
)

;*******************************************************************
;
; MACRO:       MakeStub
;
; DESCRIPTION: Creates a stub, based on a user-entered function name.
;   It assumes the cursor is positioned immediately after the name,
;   and the name is at the left of the screen.
;
;*******************************************************************

(defmacro 'Make Stub'
 (eval
  ; go before beginning of intended function name
  (SendCommands cmcLineStart)
  ; void return type
  (InsertText "void ")
  (SendCommands cmcLineEnd)
  ; void parameter
  (InsertText "( void )\n{\n")
  (InsertText "printf(\"This is ")
  (SendCommands cmcLineUp cmcLineUp cmcLineStart cmcWordRight)
  (SendCommands cmcSelectOn cmcWordRight cmcCharLeft cmcCharLeft cmcSelectOff)
  (SendCommands cmcLineDown cmcLineDown cmcLineEnd)
  (InsertText " ")
  (SendCommands cmcCopyBlock cmcHideSelect cmcLineEnd)
  (InsertText "\\n\");")
  (InsertText "\n}")
 )
)

;*******************************************************************
;
; MACRO:       MainCIO
;
; DESCRIPTION: Inserts outline of main program for C that uses STDIO.H
;
;*******************************************************************/

(defmacro 'Main C STDIO'
 (eval
  (InsertText "#include <stdio.h>\n\n")
  (InsertText "int main(void)\n{\n\n  return 0;\n")
  (SendCommands cmcLineStart)
  (InsertText "}")
  (SendCommands cmcLineUp cmcLineUp cmcCharRight)
 )
)

;*******************************************************************
;
; MACRO:   Test RunProgam
;
; DESCRIPTION: Example of how to call an external program, in this
; example the editor will call ls and put the directory list in the
; Message Window.
;
;*******************************************************************/

(defmacro 'Test RunProgram'
 (eval
  (InsertText
   (RunProgramRedir
    (AskString "The output of the program will be inserted" "External program")
   )
  )
 )
)

;*******************************************************************
;
; MACRO:   Test RunProgam Filter
;
; DESCRIPTION: Example of how to use an external filter program, it
; takes the current selection, sends it to an external program (using
; the shell input redirection) and collects the output of the
; program. Finally the selected text is replaced by the output of
; the filter.
;
;*******************************************************************/

(defmacro 'Test RunProgram Filter'
 (eval
  (setv "input" (GetSelection))
  (if (length input)
   ; Call the filter
   (eval
    (setv "program" (AskString "The output of the filter will replace the selection" "External filter"))
    (if (length program)
     (eval
      (setv "output" (RunProgramRedir program input))
      (if (length output)
       (eval
        (SendCommands cmcCut)
        (InsertText output 1)
       )
       ; Inform we didn't get text
       (MessageBox "No output from the filter")
      )
     )
     ; Ask the user to enter something
     (MessageBox "Please enter the filter to use")
    )
   )
   ; Ask the user to select something firt
   (MessageBox "Please select a text first")
  )
 )
)

;*******************************************************************
;
; MACRO:   Word Count
;
; DESCRIPTION: Example to show how to execute an external tool and
; parse the output using regex.
;
;*******************************************************************/

(defmacro 'Word Count'
 (eval
  (setv "input" (GetSelection))
  (if (length input)
   (eval
    (setv "output" (RunProgramRedir "wc" input))
    (if (length output)
     (eval
      (if (prex output "\\s*(\\d+)\\s*(\\d+)\\s*(\\d+)")
       (ShowInStatusLine (+ "Lines: " _1 " Words: " _2 " Characters: " _3))
      )
     )
     ; Inform we didn't get text
     (MessageBox "No output from wc")
    )
   )
   ; Ask the user to select something firt
   (MessageBox "Please select a text first")
  )
 )
)


;*******************************************************************
;
; These macro were created by Rolf Campbell (Endlisnis)
; email: s257m@unb.ca Endlisnis@hotmail.com
; and uses an external program provided by him.
;
; MACRO:   Prototype
;
; DESCRIPTION: Look ups the function under the cursor to report the
; prototype, even when partially typed.
;
; http://www.geocities.com/CollegePark/Quad/1974/LEMUR02.ZIP
; As Lemur is currently unavailable I commented these macros.
;
;*******************************************************************/

; (defmacro 'List Members'
;  (eval
;   (RunProgram (+ "lemur " (WhichEditor) " -s " (WordUnderCursor 256 1)))
;   (SendCommands cmcGoEndOfWord)
;  )
; )
; 
; (defmacro 'Prototype'
;  (eval
;   (ShowInStatusLine
;    (RunProgramRedir
;     (+ "lemur " (WhichEditor) " " (WordUnderCursor 256 1))
;    )
;   )
;  )
; )
; 
; (defmacro 'Prototype Complete'
;  (eval
;   (setv "proto"
;    (RunProgramRedir
;     (+ "lemur " (WhichEditor) " -c " (WordUnderCursor 256 1))
;    )
;   )
;   (SendCommands cmcGoEndOfWord)
;   (InsertText (+ proto "(") 1)
;   (ShowInStatusLine (+ "Added: " proto))
;  )
; )

;*******************************************************************
;
;    These examples were contributed by Thiago F.G. Albuquerque
;  <tfga@terra.com.br>.
;    I added them commented to save memory, you can uncoment them
;  using the "Indent uncommenting" editor command found in Tool&Ops|
;  Un/Indent block menu.
;
;*******************************************************************

(defmacro 'DuplicateLineOrSelection'
 (if (SelectionExists)
  (InsertText (GetSelection))
  (progn
   (SendCommands cmcLineStart cmcSelectOn cmcLineEnd cmcSelectOff cmcCopy cmcLineEnd)
   (InsertText "\n")
   (SendCommands cmcPaste)
  )
 )
)

(defmacro 'EvalSelection_Print'
 (InsertText (+ "\n" (EvalString (GetSelection))))
)

(defmacro 'EvalSelection_StatusLine'
 (ShowInStatusLine (EvalString (GetSelection)))
)

; (defmacro 'EvalSelection_MessageWindow'
;  (eval
;   (ShowInMessageWindow "Return value:" 1)
;   (ShowInMessageWindow (EvalString (GetSelection)))
;  )
; )
;
;
; This example shows how to choose a compiler according to the file type.
;
; (defmacro 'Compile'
;  (progn
;   (setv "name" (WhichEditor edfWENameNoExt)) ; name without extension
;   (setv "ext"  (WhichEditor edfWEExtension)) ; extension
; 
;   (cond (== (GetSyntaxLang) 'Perl') (RunProgram (+ "perl -cw " name ext) 0 'Perl') ; Perl
;         1                           (RunProgram (+ "make obj/" name ".o"))  ; C, C++
;   )
;  )
; )
; 
; (defmacro 'Make'
;  (eval
;   (RunProgram "make")
;  )
; )
;
; If you are editing a file named "stuff.cpp",
; this macro takes you to "stuff.h" and vice-versa.
; (OpenFile) checks first if the file is already open,
; in which case it just switches to the right window.
;
; (defmacro 'Switch-cpp-h'
;  (eval
;   (setv "name" (WhichEditor edfWENameNoExt)) ; name without extension
;   (setv "ext"  (WhichEditor edfWEExtension)) ; extension
;   (cond (not (strcmp ext ".cpp"))  (OpenFile (+ name ".h"))
;         (not (strcmp ext ".h"))    (OpenFile (+ name ".cpp"))
;   )
;  )
; )
; 
; This is an implementation of pseudo-macros in sLisp.
; Assign to Ctrl-Space.
;
; (defmacro 'pmacro'
;  (progn
;   (SendCommands cmcSelectOn cmcCharLeft cmcCharLeft cmcSelectOff)
;   (setv "str" (GetSelection))
;   (SendCommands cmcClear)
;   (cond (not (strcmp str "#I"))    (progn  (InsertText '#include ".h"')
;                                            (SendCommands cmcCharLeft cmcCharLeft cmcCharLeft)
;                                    )
; 
;         (not (strcmp str "#i"))    (progn  (InsertText '#include <.h>')
;                                            (SendCommands cmcCharLeft cmcCharLeft cmcCharLeft)
;                                    )
; 
;         (not (strcmp str "#d"))    (InsertText '#define ')
;
;         (not (strcmp str "pr"))    (progn  (InsertText 'printf("");')
;                                            (SendCommands cmcCharLeft cmcCharLeft cmcCharLeft)
;                                    )
; 
;         (not (strcmp str "re"))    (progn  (InsertText 'return ;')
;                                            (SendCommands cmcCharLeft)
;                                    )
; 
;         (not (strcmp str "{}"))    (progn  (InsertText "{}")
;                                            (SendCommands cmcCharLeft cmcNewLine cmcNewLine cmcLineUp)
;                                            (InsertText "    ")
;                                    )
; 
;         (not (strcmp str "//"))    (progn (InsertText "///////////////////////////////////////////////////////////////////////////")
;                                           (SendCommands cmcNewLine)
;                                    )
; 
;         (not (strcmp str "/*"))    (progn (InsertText "/****************************************************************************")
;                                           (SendCommands cmcNewLine cmcNewLine)
;                                           (InsertText "*****************************************************************************/")
;                                           (SendCommands cmcNewLine cmcLineUp cmcLineUp)
;                                    )
; 
;         (not (strcmp str "in"))    (progn (InsertText "#ifndef \n")
;                                           (InsertText "#define \n")
;                                           (InsertText "\n")
;                                           (InsertText "#endif")
;                                           (SendCommands cmcLineUp cmcLineUp cmcLineUp cmcLineEnd)
;                                    )
;
;         (not (strcmp str ".h"))    (progn
;                                           (setv "filename" (+ (WhichEditor edfWENameNoExt) "_h_____"))
;                                           (InsertText (+ "#ifndef " filename "\n"))
;                                           (InsertText (+ "#define " filename "\n"))
;                                           (InsertText "\n")
;                                           (InsertText "#endif")
;                                    )
; 
;         (not (strcmp str "fo"))    (progn (InsertText "for (int i=0; i<; i++)")
;                                           (SendCommands cmcNewLine)
;                                           (InsertText "{}")
;                                           (SendCommands cmcCharLeft cmcNewLine cmcNewLine cmcLineUp)
;                                           (InsertText "    ")
;                                    )
; 
;         (not (strcmp str "ma"))    (progn (InsertText "int main(int argc, char *argv[])\n")
;                                           (InsertText "{\n")
;                                           (InsertText "\n")
;                                           (InsertText "    return 0;\n")
;                                           (InsertText "}")
;                                           (SendCommands cmcLineUp cmcLineUp
;                                                         cmcCharRight cmcCharRight cmcCharRight)
;                                    )
; 
;         (not (strcmp str "cl"))    (progn (InsertText "class\n")
;                                           (InsertText "{\n")
;                                           (InsertText " public:\n")
;                                           (InsertText " // Data\n")
;                                           (InsertText " \n")
;                                           (InsertText " // Construction\n")
;                                           (InsertText " \n")
;                                           (InsertText " // Methods\n")
;                                           (InsertText " \n")
;                                           (InsertText " // Operators\n")
;                                           (InsertText "};")
;                                           (SendCommands cmcLineUp cmcLineUp cmcLineUp cmcLineUp cmcLineUp cmcLineUp cmcLineUp cmcLineUp cmcLineUp cmcLineUp
;                                                         cmcLineEnd cmcCharRight)
;                                    )
; 
;         1                          (InsertText str)
;   )
;  )
; )
;
; I assigned this to TAB and then I assigned cmcUnIndentBlk to Shift-Tab.
;
; (defmacro 'IndentLineOrBlock'
;  (if (SelectionExists)
;   (SendCommands cmcIndentBlk)
;   (InsertText "    ")
;  )
; )
;
; (defmacro 'Test_Loop'
;  (eval
;   (setv "i" 0)
;   (loop
;    (InsertText (tostr i))
;    (if (== i 5) (exitloop))
;    (setv "i" (++ i))
;   )
;  )
; )
;
;-------------------------------------------------------------------------------------------
;
; More macros from Thiago, they are short and funny so I let them uncommented.
;
;-------------------------------------------------------------------------------------------
; These 2 macros make cmcIndentBlk and cmcUnIndentBlk behave like
; cmcCommentIndent and cmcCommentUnIndent, i.e.: if there's no selection,
; indent/unindent the current line.

(defmacro 'IndentLineOrSelection'
  (if (SelectionExists)     (SendCommands cmcIndentBlk)
                            (SendCommands cmcPushCursorPos
                             cmcLineStart cmcSelectOn cmcLineEnd cmcSelectOff
                             cmcIndentBlk
                             cmcPopCursorPos )
  )
)

(defmacro 'UnIndentLineOrSelection'
  (if (SelectionExists)     (SendCommands cmcUnIndentBlk)
                            (SendCommands cmcPushCursorPos
                             cmcLineStart cmcSelectOn cmcLineEnd cmcSelectOff
                             cmcUnIndentBlk
                             cmcPopCursorPos )
  )
)

;-------------------------------------------------------------------------------------------
; ^Ins and no selection -> copy current word

(defmacro 'CopySelectionOrCurWord'
  (if (SelectionExists)     (SendCommands cmcCopy)
                            (SendCommands cmcMarkWord cmcCopy)
  )
)

;-------------------------------------------------------------------------------------------
; Select the entire text

(defmacro 'SelectAll'
    (SendCommands cmcTextStart cmcSelectOn cmcTextEnd cmcSelectOff)
)

;-------------------------------------------------------------------------------------------
; New (and simpler) version of CopyLine: uses cmcMarkLine now

(defmacro 'CopyLine'
          (SendCommands cmcMarkLine
                        cmcCopy
          )
)

; CommentIndent uses the current syntax highlight to get the comment string
; (e.g.: "//" for C++, "#" for Perl, etc.). If the current file has no syntax
; highlight, it does nothing. In that case, you have to call ArbitraryIndent
; and provide the comment string yourself. This macro makes this automatic.

(defmacro 'CommentIndent'
 (progn
    (if (not (SelectionExists))    (SendCommands cmcMarkLine))

    (if (== (GetSyntaxLang) '' )    (SendCommands cmcArbitraryIndent)
                                    (SendCommands cmcCommentIndent)
    )
 )
) 
