Emacs Auto indent/format for AutoHotKey: ahk-mode.el problems, especially with OTB

Discuss features, issues, about Editors for AHK
Sticky
Posts: 10
Joined: 12 Apr 2017, 22:08

Emacs Auto indent/format for AutoHotKey: ahk-mode.el problems, especially with OTB

Post by Sticky » 23 Jun 2022, 16:43

I use emacs to edit my AutoHotKey code. currently using ahk-mode.el.

Although this ahk-mode.el file is quite old, 2016, it appears to be the latest version in melpa.

This version of ahk-mode.el has significant problems, especially with the OTB (One True Brace) style.
short example just for if statement here, more examples for other sorts of statements below the TL;DR line

Code: Select all

	if (x < y) {
		... <--  first-line of if branch is indented correctly
	...  <---   subsequent lines are not indented correctly
	} else {
		...
		...
	}
I therefore fall back to putting { on a line by itself, and use much more vertical space than I like.

I wonder if anybody has already fixed this, and is willing to share?

or if anyone can point me to better a better emacs indentation mode for AutoHotKey?

(C-mode actually does pretty well, since AHK with lots of braces can be written to be very C light. But inevitably it will get confused by some AutoHotKey syntax and then break if you run indent-buffer. it's a pain to have to remember the places where you're not allowed to run indent-buffer.)

(one of my New Year's resolutions - for the past many years - has been to ask first rather than going in trying to write it myself)


TL;DR

Version of ahk-mode.el that I'm using --essentially what appears to be latest stable version in melpa, the emacs package repository.

And more examples of the indentation it produces.

Code: Select all

;;; ahk-mode.el --- Major mode for editing AHK (AutoHotkey and AutoHotkey_L) -*- lexical-binding: t -*-
...
;; Copyright (C) 2015-2016 by Rich Alesi
...
;; URL: https://github.com/ralesi/ahk-mode
;; Version: 1.5.6
...
;; Based on work from
;; xahk-mode - Author:   Xah Lee ( http://xahlee.org/ ) - 2012
;; ahk-mode - Author:   Robert Widhopf-Fenk

Code: Select all


{
	;; BAD:  basically ahk-mode.el is very inconsistent and
	;; frequently broken with the OTB style, with { on same line
	;; as if/function, etc

	;; applying ahk-mode.el to the One  True Brace (OTB) [K&R?]  style
	;;  example on https://www.autohotkey.com/docs/commands/Block.htm
	MyFunction(x, y) {
		...
	}
	if (x < y) {
		...
	...
	} else {
		...
		...
	}
	Loop %RepeatCount% {
		...
	...
	}
	While x < y {
		...
		...
	}
	For k, v in obj {
		...
		...
	}
	Try {
		...
		..
		} Catch e {
			...
			...
			} Finally {
				....
				...
			}
}


{
	;; GOOD - it handles naked {} blocks.
	if(0)
		lineTrue()
	;; And recognizes the next line is  not part of the if then clause
}
{
	;;  this is surprising
	if(0)
		lineTrue()
	else
		lineFalse()

		;;  but it might be considered a feature, giving a
		;;  strong hint that AutoHotKey does not really
		;;  understand traditional if withouut {braces} with
		;;  an else clause
}
{
	;; GOOD - curly {braces}  on line by themselves work
	if(0)
	{
		...
	}

	if(0)
	{
		...
	}
	else if(0)
	{
		...
	}
	else
	{
		...
	}
}
{
	;; GOOD -  {on same line 
	if(0) {
		...  1st  line
	}
	...
	if(0){
		... line #1
	... line #2 <----  is not indented  as you might like
	}

	if(0) {
		...
	}
	else if(0) {
		...
	}
	else {
		...
	}

	if(0) {
		...
	} else if(0) {
		...
	} else {
		...
	}
}

;;  well, at least class  definitions get indented properly

foo()
{
	...
	...
}
foo() {
	...
	...
}

class ClassName
{
	...
	...
}
class ClassName {
	...
	...  ; 
}


{
	;; applying ahk-mode.el to the One  True Brace (OTB) [K&R?]  style
	;;  example on https://www.autohotkey.com/docs/commands/Block.htm
	MyFunction(x, y) {
		...
	}
	if (x < y) {
		...
	...
	} else {
		...
		...
	}
	Loop %RepeatCount% {
		...
	...
	}
	While x < y {
		...
		...
	}
	For k, v in obj {
		...
		...
	}
	Try {
		...
		..
		} Catch e {
			...
			...
			} Finally {
				....
				...
			}
}



User avatar
thqby
Posts: 397
Joined: 16 Apr 2021, 11:18
Contact:

Re: Emacs Auto indent/format for AutoHotKey: ahk-mode.el problems, especially with OTB

Post by thqby » 13 Sep 2022, 02:20

Ahk2 has an extension based on vscode-lsp and nodejs, which can be easily ported to other editors that support lsp, providing code completion, formatting, jump to definition, semantic highlighting and other functions.

Emacs also has a lsp implementation. If your script does not contain v1 commands, you can try it, or use vscode web.

swub
Posts: 19
Joined: 25 Feb 2019, 09:16

Re: Emacs Auto indent/format for AutoHotKey: ahk-mode.el problems, especially with OTB

Post by swub » 16 Jun 2023, 12:00

thqby - does this mean I need to get this from VScode to reuse in emacs? I don't care for VScode and have switch to the alternative VSCodium and think perhaps I could get it from there.

Post Reply

Return to “Editors”