1

How do you pass arguments to a function using lambda? I have looked through:
https://www.gnu.org/software/emacs/manual/html_node/elisp/Argument-List.html#Argument-List
and
https://www.emacswiki.org/emacs/download/highlight-chars.el
The only examples I found in the 2nd link were for tabs and whitespaces which use functions requiring no arguments.

Here's what I have tried so far:
1.

  (require 'highlight-chars)
  (defun my:add-semi-colon-highlighting ()
    (add-hook 'font-lock-mode-hook
              (lambda () (hc-highlight-chars ";" "highlight"))))

  (add-hook 'c-mode-common-hook 'my:add-semi-colon-highlighting)

2.

  (require 'highlight-chars)
  (defun my:add-semi-colon-highlighting ()
    (add-hook 'font-lock-mode-hook
              (lambda () (hc-highlight-chars (";","highlight")))))

  (add-hook 'c-mode-common-hook 'my:add-semi-colon-highlighting)

3.

  (require 'highlight-chars)
  (defun my:add-semi-colon-highlighting ()
    (add-hook 'font-lock-mode-hook
              (lambda () (hc-highlight-chars ";" highlight))))

  (add-hook 'c-mode-common-hook 'my:add-semi-colon-highlighting)

Not sure if this should go into another question or not, but if wanted to customize the face for ";" (E.g red foreground and current background), how would I do that in the my:add-semi-colon-highlighting function?

1 Answer 1

0

#2 is not Lisp - no comma.

#1 and #3 are almost right. And admittedly the doc for this function is a bit complex on this matter.

So the answer to your general question about passing multiple arguments is to do as you did (except for #2, with the comma).

The answer to your specific question about hc-highlight-chars is to use this code: (hc-highlight-chars '(";") 'highlight).

Interactively, as the doc says, you enter first the characters and then the name of a face. And a name is indeed a string, such as "highlight".

But at the end of the doc string you see a description of the arguments for non-interactive use, and there it says that argument FACE is a face. I've now clarified that further by adding (e.g., a symbol) after it. A face is typically a symbol.

For argument CHARS things are more complex. For that the doc says:

CHARS is a possible value for `hc-other-chars', that is, a list of
entries such as described above.

And you really need to consult the doc above, which explains that the spec of which chars to highlight is a list, each of whose elements can take multiple forms, one of which is a string of characters. (";") is a list of one element that is a string of characters.

If you look more into the code you'll see that hc-highlight-chars already uses add-hook to add to font-lock-mode-hook. It does this, for the current case:

(add-hook 'font-lock-mode-hook
          (lambda () (hc-highlight-other-chars '("ab") nil 'highlight))  
          'APPEND)

It can help perhaps to start by using hc-highlight-chars interactively, to get the hang of what it does.

The doc (the file Commentary and other doc strings) tells you more about hc-highlight-other-chars, other workhorse functions, and user option hc-other-chars. You might need to read it twice to understand it all well, but I think the necessary info is there. Let me know if you find that's not the case.

As for your question about customizing a face: Do you mean how to customize face highlight, which you used in your example? If so, the answer is M-x customize-face highlight.

Or did you mean create a new face to use, which has attributes that you choose? For that, you use defface -- see the Elisp manual, node Defining Faces.

Or did you perhaps mean provide a face specification on the fly, without changing any existing defined face or defining a new one (defface)? You can do that this way, for example:

`(hc-highlight-chars '(";") '(:foreground "red" :background "green"))`

(This is why the doc describing the FACE parameter to the function says that it is a face, without saying that it must be a symbol. A face can also be just such a property list (plist) of face attributes and their values.)

3
  • Thank you for the informative answer. I tried to replace my hc-highlight-chars expression with the one you provided but now I receive "Error in post-command hook (global-font-lock-mode-check-buffers: (error "Variable binding depth exceeds max-specpdl-size")
    – Jonathan
    Jul 31, 2016 at 1:56
  • Show your code - we cannot guess what you did (though it sounds like you added it to post-command-hook). You do not need to put it on a hook. Just add it to your init file. As I said, hc-highlight-chars already adds what is needed to font-lock-mode-hook.
    – Drew
    Jul 31, 2016 at 2:45
  • Oh, I thought you meant for me to only replace the expression inside the hook and not the hook itself. Removed the hook and works perfectly. Thanks again.
    – Jonathan
    Jul 31, 2016 at 3:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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