【问题标题】:AutoHotKey Source Code Line BreakAutoHotKey 源代码换行符
【发布时间】:2015-01-25 06:06:35
【问题描述】:

有没有办法在 AutoHotKey 源代码中进行换行?我的代码超过 80 个字符,我想将它们整齐地分开。我知道我们可以用其他语言来做到这一点,例如下面的 VBA:

http://www.excelforum.com/excel-programming-vba-macros/564301-how-do-i-break-vba-code-into-two-or-more-lines.html

If Day(Date) > 10 _
And Hour(Time) > 20 Then _
MsgBox "It is after the tenth " & _
"and it is evening"

AutoHotKey 中是否有源代码换行符?我使用的是旧版 AutoHotKey,版本 1.0.47.06

【问题讨论】:

    标签: autohotkey


    【解决方案1】:

    文档中有一个Splitting a Long Line into a Series of Shorter Ones 部分:

    长线可以分成一组较小的线 提高可读性和可维护性。这并没有减少 脚本的执行速度,因为这些行在内存中合并 脚本启动的那一刻。

    方法#1:以“and”、“or”、||、&&、逗号或a开头的行 period 会自动与它正上方的行合并(在 v1.0.46+,所有其他表达式运算符也是如此,除了 ++ 和 --)。在以下示例中,第二行附加到第一行,因为它以逗号开头:

    FileAppend, This is the text to append.`n   ; A comment is allowed here.
        , %A_ProgramFiles%\SomeApplication\LogFile.txt  ; Comment.
    

    同样,以下行将合并为一行 因为最后两个以“and”或“or”开头:

    if (Color = "Red" or Color = "Green"  or Color = "Blue"   ; Comment.
        or Color = "Black" or Color = "Gray" or Color = "White")   ; Comment.
        and ProductIsAvailableInColor(Product, Color)   ; Comment. 
    

    三元运算符也是不错的选择:

    ProductIsAvailable := (Color = "Red")
        ? false  ; We don't have any red products, so don't bother calling the function.
        : ProductIsAvailableInColor(Product, Color)
    

    虽然上面示例中使用的缩进是可选的,但它可能会改进 通过指示哪些行属于它们上方的行来清晰。还有,它 不必为以 单词“AND”和“OR”;该程序会自动执行此操作。最后, 空行或 cmets 可以添加在任何一个之间或末尾 以上示例中的行。

    方法#2:这个方法应该用于合并大量行 或者当线条不适合方法 #1 时。这种方法虽然 对于自动替换热字串特别有用,它也可以用于 使用任何命令或表达式。例如:

    ; EXAMPLE #1:
    Var = 
    (
    Line 1 of the text.
    Line 2 of the text. By default, a line feed (`n) is present between lines. 
    )
    
    ; EXAMPLE #2: 
    FileAppend,  ; The comma is required in this case. 
    (
    A line of text. 
    By default, the hard carriage return (Enter) between the previous line and this one will be written to the file as a linefeed (`n).
         By default, the tab to the left of this line will also be written to the file (the same is true for spaces).
    By default, variable references such as %Var% are resolved to the variable's contents. 
    ), C:\My File.txt
    

    在上面的示例中,一系列线的边界为 顶部和底部由一对括号。这被称为 续部分。请注意,底线包含 FileAppend 在右括号之后的最后一个参数。这 练习是可选的;它是在这样的情况下完成的,以便逗号 将被视为参数分隔符而不是文字逗号。

    请阅读文档链接了解更多详情。

    所以你的例子可以改写如下:

    If Day(Date) > 10 
    And Hour(Time) > 20 Then
        MsgBox
        (
        It is after the tenth 
        and it is evening
        )
    

    【讨论】:

      【解决方案2】:

      我不知道这样做的一般方法,但您似乎可以用运算符换行并开始折行的其余部分(例如下一个实线)。只要第二行(以及第三、第四等,如适用)以(可选的空格加)运算符开头,AHK 就会将整个内容视为一行。

      例如:

      hello := "Hello, "
             . "world!"
      MsgBox %hello%
      

      这里第二行逻辑开头的连接运算符. 的存在使得AHK 将两行视为一个。

      (我还尝试将运算符和第一行的结尾留给第二行,然后用双引号字符串开始;这不起作用。)

      【讨论】:

        猜你喜欢
        • 2018-06-24
        • 2016-07-20
        • 2011-09-24
        • 2015-02-06
        • 2011-07-12
        • 1970-01-01
        • 1970-01-01
        • 2019-06-03
        • 1970-01-01
        相关资源
        最近更新 更多