1

When using the propertize function alone on an entire string, it works. How to return a string, after changing its different sections separately with add-face-text-property (probably later repeatedly on different sections)?

(let ((heading "Heading [50%] [1/2]"))
  (concat
   "..."
   (when heading
     ;;  (propertize heading 'face 'header-line)
     (set-text-properties 0 (length heading) '(face header-line) heading)
     (add-face-text-property
      (string-match "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)" heading 0)
      (match-end 0)
      `(:foreground ,(face-foreground (org-get-todo-face keyword) nil 1))
      nil
      heading))))

1 Answer 1

4

This is an extremely basic question about Emacs Lisp, and it has nothing to do with strings or add-face-text-property specifically. You really should read through the Emacs Lisp Intro, which is a manual that teaches how to program in Emacs Lisp. It does not assume any prior knowledge of programming.

In Lisp, “returning” a value really means that the expression evaluates to that value. The code you pasted in your question has a lot of irrelevant details in it, and some mistakes. Let’s assume you have this code instead:

(let ((heading "some string"))
  (modify-string heading)
  (modify-string heading))

modify-string here represents any function which has side effects that modify the contents of the string we pass in. Currently this expression evaluates to nil, because the last thing it does is call modify-string, and modify-string returns nil. Essentially, this code starts with a string, modifies it twice, and then throws it away. How can we return the string instead? We need to add another expression to the body that will evaluate to the modified string. What is the simplest expression that evaluates to that string? Yes, the variable itself:

(let ((heading "some string"))
  (modify-string heading)
  (modify-string heading)
  heading)

This is not an uncommon pattern; you’ll see it fairly frequently in the Emacs Lisp code that you read.

2
  • Thanks ! Could this uncommon pattern be changed inside this "when" block using same text property functions or not ? Any keywords?
    – Herman
    Jul 31, 2023 at 13:01
  • OP: If you have another question, post it as another question. Comments are not for extended discussion, including Q&A.
    – Drew
    Jul 31, 2023 at 15:00

Your Answer

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

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