Skip to content

vdelachaux/4DPop-Macros

Repository files navigation

Static Badge Static Badge
release license

4DPop Macros adds macros available to the method editor. Using macro-commands saves a lot of time during method entry.

When the component is loaded, the macros are automatically installed and available in the method editor, they can be invoked:

  • By clicking on the Macros button in the method editor toolbar.
  • By double-clicking on their name in one of the footer lists of the method editor.
  • For some of them (see the "Menu" column below) in the "Method" menu or the sub-menu of the "Insert macro" item of the editor's contextual menu.
  • For some, by a keyboard shortcut indicated in the "Shortcut" column below.
  • For some, in the predictive input window. The triggering text is indicated in the "Input" column below.

For more information on the use and operation of macros, you can refer to the Creating and using macros of the 4D documentation.

That's why they have an associated shortcut 😉

Name Goal Menu Input Shortcut*
Declarations… Help with the declaration of parameters and local variables ✔︎ ⌘ $
Compiler directive… Help to enclose selection with //%W directive ✔︎ ⌘ ⌥ ⇧ c
Beautifier… A code formatter ✔︎ ⌘ §
Copy and replace Swaps the selection with the contents of the clipboard ✔︎ ⌘ ⌥ c
Special paste… Paste the text contained in the clipboard by applying transformations ✔︎ _p ⌘ ⌥ v
Comments Intelligent comment/uncomment ✔︎ ⌘ j
Duplicate and comment The selected text is saved as a comment immediately before the selection. ✔︎ ⌘ ⌥ d
Replay last macro Replays the last macro used for the method being edited ✔︎ ⌘ +

*Shortcuts may be displayed incorrectly in the macro menu because they are interpreted. e.g. ⌘ ⌥ c will be displayed ⌘ ©

This tool operate on the selected text of the frontmost method (or on the whole method if there is no selection*). It extract the parameters and local variables, then tries to discover their type and displays the list of elements found, allowing you to change their type. Once validated, the declarations are placed at the top of the method or selection according to your choice. The declaration tool is able to parse the C_xxx directives and replace it using the var keyword.

*Not yet for a class method, so it's best to work with the selection for now.

  • ① The list displays first the parameters, then the local variables.

    • For each element, an icon represents the type deduced, and the number of uses in the analyzed code is displayed on the right.
    • The elements for which the type could not be determined are displayed in red.
    • Items that were found in a statement instruction, but are not used, are displayed in orange. They will not be included in the created directives.
    • Arrays are underlined.
  • ② The line of code in which the first occurrence of the element was found is displayed under the list

    • The same information is available via the tooltip.

  • ③ To the right are the available types.

    • The shortcut for each type is option + the underlined letter of the label
    • The class is displayed when relevant.
  • ④ The Filter menu allows you to display only certain items.

  • ⑤ If you validate, the directives are pasted at the top of the method or selection, before the first line which is not a comment. Untyped or unused variables are ignored, declarations that have been made throughout the method, as well as all previous declarative blocks are deleted.
    • If you close the window with the close box, the method is not changed.

Arrays:

📍Since declaration and sizing use the same command, you can force the position of the command by using hexadecimal notation (or a variable) for the size, i.e.:

ARRAY LONGINT($array; 0x0000)
ARRAY TEXT($array; $size)

📍Two-dimensional arrays are not yet supported


⚠️ In binary mode, the tool is different and is not more maintained. Please refer to the old documentation.

👀 How the tool determine the item type?

  • If a variable or a parameter is already declared (with var, C_xxx or #DECLARE). It is obvious ;-)
  • If no declaration line was found, the tools try to deduce the type by analyzing the code.
    • First by detecting simple patterns: for example, $x:=10 gives the type Integer to $x, $o.key:=10gives the type Object to $o.
    • Then, using the 4D command syntax: $x:=Count parameters gives the type Integer to $x, Is picture file ($pathname) gives the type Text to $pathname.
    • And finally, if the type has still not been found using the nomenclature that you can define in the preferences.
  • You can always force a type by selecting the one you want using the radio buttons on the right.

This macro is often useful even if the explanation of its operation seems complex. The usage scenario is as follows:

You type some code referring to a calculated expression and then you realize that you need this expression again later on and decide to put the expression in an intermediate variable. In this case you type $maVariable:=MyExpression and then replace MyExpression with $maVariable.

With the macro:

  • First type $maVariable and copy this text.
  • Then, select MyExpression and call the macro: The text "MyExpression" is placed in the clipboard and replaced by the text "$maVariable".
  • Then, you position the cursor after the first occurence of $myVariable and type := then paste the content of the clipboard to obtain $myVariable:=MyExpression.

This tool formats the selected text, or the whole method if there is no selection, by applying the rules you have defined in the preferences.

The tool displays a dialog allowing to choose what will be pasted in the method editor. The "Result" area allows you to preview the text as it will be pasted.

The available transformations are :

  1. String: The text is pasted between quotation marks, the special characters (\r, \n, \t and ") are escaped .
  2. Comments: Each line is preceded by a comment mark, the text is not modified.
  3. HTML: The html code contained in the clipboard is assigned to a local variable. The characters are escaped.
  4. Regex Pattern: The text is pasted between quotation marks. The special characters of the pattern are escaped to be used as such taking into account that the backslash must be escaped in 4D.
  5. Pathname: Pastes the path name of a file or folder copied to disk. You can choose the POSIX format instead of the system and choose to use a path relative to the project folder if relevant.
  6. Insert in text: Allows you to insert an expression into a string by surrounding the clipboard content with "+ and +". The text thus created replaces the text selected in the string or is inserted at the cursor position.
  7. JSON code: Transforms a JSON text into code to create a 4D object
  8. Tokenized: Paste code with tokens

When the compiler displays warnings to draw your attention to instructions that could lead to runtime errors, you can, after analysis, selectively disable some of them during compilation.

To do this, select the line(s) of code in the method editor that cause this warning and select the "Compiler directive…" action from the macro menu. The tool asks for the number displayed in brackets (e.g. 538.3) and when you validate, surrounds the selected code with the pattern //%W-538.3 ... //%W+538.3.

If the selected text is already commented, it is uncommented. Otherwise, the macro analyzes the selected text to choose the best commenting method:

  • single-line comment:
// CLOSE DOCUMENT($Doc_shortcuts)
  • block comment:
/*
	APPEND TO ARRAY($tTxt_attributeName; "editable")
	APPEND TO ARRAY($tTxt_attributeValue; "false")
*/
  • intra-line
ON ERR CALL(/*"noERROR"*/"")

I use it when I want to preserve a piece of code before editing it, but an image is better than text ;-)

As the name suggests, the selected code is copied with the tokens.

Name Goal Menu Input Shortcut*
Macro test Call the project method 4DPop_TEST_Macros of your database. Be sure to share the method. ✔︎ ⌘ 0
Paste a color Displays the system color palette and pastes the value of the selected color. ✔︎ _c
Find with Google Launch a Google search, in the default browser, with the selected test ✔︎
New method… Creates a new method containing the selected text and replaces the selection with the method name. ✔︎
Remove blank lines As the name suggests ✔︎ ⌘ *
Comment block Surround the selection with /* and */ ✔︎

🚧

🚧