1

I'm trying to modify how asm-mode does its automatic indentation. I thought I would use the following:

(defun yasm-calculate-indentation (oldfun)
  "Add the formatting for a few other keywords I like."
  (cond
   ((looking-at-p "section") 0)         ; section
   ((looking-at-p "global") 0)          ; global
   ((looking-at-p "extern") 0)          ; extern
   (:default (oldfun))))

(add-hook 'asm-mode-hook
          (lambda ()
            (advice-add :around #'asm-calculate-indentation
                        #'yasm-calculate-indentation)))

However I'm getting the error: Unknown add-function location ‘asm-calculate-indentation’

Leaving me rather confused.

Am I adding the hook in the wrong location to be able to access that name?

I tried adding the advice from the minibuffer when editing an assembly file but it gave me the same error.

This is my first time trying to use advice so maybe that's where I'm going wrong... How can I get this to work?

  • Drew's answer correctly identifies the source of the error message, but there are some additional problems with this code. (a) oldfun is not the name of a function, but rather a local variable whose value is a function, so it must be called as (funcall oldfun). (b) There is no need to call advice-add from a hook function. You can advise symbols before they are defined as functions; this is called forward advice. Furthermore, the hook will run every time asm-mode is enabled, whereas the advice needs only be registered once. – Basil Apr 4 at 17:59
  • @basil: Please consider putting such info in an answer. Comments can be deleted at any time. – Drew Apr 4 at 21:47
  • @Drew Done, please review. – Basil Apr 4 at 22:40
  • @Basil: Looks good to me. Thx. – Drew Apr 5 at 0:55
  • thanks to you both for the help! works great – GreenLlama Apr 5 at 12:46
2

Drew's answer correctly identifies the source of the error message, but there are some additional problems with the code.

(defun yasm-calculate-indentation (oldfun)
  "Add the formatting for a few other keywords I like."
  (cond
   ((looking-at-p "section") 0)         ; section
   ((looking-at-p "global") 0)          ; global
   ((looking-at-p "extern") 0)          ; extern
   (:default (oldfun))))

First, oldfun is not the name of a function, but rather a local variable whose value is a function, so it must be called as (funcall oldfun) instead of (oldfun).

(add-hook 'asm-mode-hook
          (lambda ()
            (advice-add :around #'asm-calculate-indentation
                        #'yasm-calculate-indentation)))

Second, there is no need to call advice-add from a hook function. You can advise symbols before they are defined as functions; this is called forward advice.

Furthermore, asm-mode-hook will run every time asm-mode is enabled, whereas the advice needs only be registered once.

Putting all of this together:

(define-advice asm-calculate-indentation (:before-until () my-keywords)
  "Determine indentation for a few other keywords I like.
If this advice returns non-nil, that value is used and
`asm-calculate-indentation' is not called; otherwise the value
returned by `asm-calculate-indentation' is used."
  (and (looking-at-p (rx (or "section" "global" "extern")))
       0))

The convenience macro define-advice was added in Emacs 25; in Emacs 24 you can emulate it as follows:

(defun yasm-calculate-indentation ()
  "Determine indentation for a few other keywords I like.
Intended as :before-until advice for `asm-calculate-indentation'."
  (and (looking-at-p (rx (or "section" "global" "extern")))
       0))

(advice-add 'asm-calculate-indentation :before-until
            #'yasm-calculate-indentation)
| improve this answer | |
2
(advice-add :around #'asm-calculate-indentation #'yasm-calculate-indentation)

should be

(advice-add #'asm-calculate-indentation :around #'yasm-calculate-indentation)

C-h f advice-add says:

advice-add is a compiled Lisp function in `nadvice.el'.

(advice-add SYMBOL WHERE FUNCTION &optional PROPS)

Like `add-function' but for the function named SYMBOL.
...

C-h f add-function says:

add-function is a Lisp macro in `nadvice.el'.

(add-function WHERE PLACE FUNCTION &optional PROPS)

...

The error message tells you that asm-calculate-indentation is not a known add-function location:

error: Unknown add-function location ‘asm-calculate-indentation’

The known locations are listed in C-h f add-function: :around etc.

| improve this answer | |
  • wow that is a frustrating change in argument order... thanks! – GreenLlama Apr 4 at 21:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.