0

I am using this function interactively, where the string from completing-read is passed to intern to convert it into a symbol suitable for describe-face.

Currently, I want to handle the non-interactive case, where a face is passed directly to the function. The strategy is to add a condition alternative for a face or face name, that I can call describe-face with that face as a single argument.

Perhaps using stringp for face names and facep for faces.

Is this ok, can it be improved?

(defun laxy-descface (facenm)
  "Describe the typeface properties of face name FACENM."
 
  (interactive
   (list
    (let* ( (cseq '("default" "mode-line" "mode-line-inactive"
            "list" "text-property")) )
      (completing-read "Face_property: " cseq nil t "text-property"))))

  (cond
   ((stringp facenm)
 
    (pcase facenm
      ("text-property"
       (describe-face (get-text-property (point) 'face)))
      ("default"
       (describe-face (intern facenm)))
      ("mode-line"
       (describe-face (intern facenm)))
      ("mode-line-inactive"
       (describe-face (intern facenm)))
      ("list"
       (list-faces-display))) )

   ((facep facenm)

    (describe-face facenm))))
Dilna
  • 1,173
  • 3
  • 10
  • Who is stopping you? – NickD Aug 11 '22 at 03:03
  • What `pcase` alternative can I use that handles the non-interactive case, calling the face being passed without use of `intern`? – Dilna Aug 11 '22 at 11:51
  • I'm not sure that clarifies the question, but in any case: *add your clarifications to the question - do not ask new question or add clarifications in a comment*. The question needs to be clear on its own. – NickD Aug 11 '22 at 13:10
  • I am not sure why you insist on making it a symbol. Why don't you try out the comments [here](https://emacs.stackexchange.com/questions/72979/pass-face-to-describe-face-when-using-function-interactively)? The first comment says your code was correct, then in a subsequent comment @NickD, explained that `describe-face` takes a string also. We are happy to answer your questions, but you should at least try out and try to understand our answers... – dalanicolai Aug 11 '22 at 19:59
  • Your code in [that](https://emacs.stackexchange.com/questions/72979/pass-face-to-describe-face-when-using-function-interactively) question will work perfectly fine when called non-interactively. Just remove the quote from the `'face` at the end of your example, as was mentioned in the first comment there. But why don't you call `describe-face` non-interactively directly? – dalanicolai Aug 11 '22 at 20:02
  • Although `describe-face` takes a string also, it is not recommended by the emacs maintainers. That's why it is not documented. – Dilna Aug 11 '22 at 20:03
  • Okay, I did not know that, it isn't mentioned in the docstring. It would also have been nice if you mentioned that before, instead of just ignoring the comment. Well, so then indeed use an `intern`. And I would advise you to lookup how to use `or` in `pcase`. – dalanicolai Aug 11 '22 at 20:27
  • I only found out later, after trying it out and getting it to work. I am not aware why they do not recommend people use that functionality. – Dilna Aug 11 '22 at 20:36
  • I see now, you maybe tried to explain it [here](https://emacs.stackexchange.com/questions/72979/pass-face-to-describe-face-when-using-function-interactively). However, you mentioned `face` which I thought had to be a symbol or string. But, you still had to remove the quote in that answer and replace it by an intern. Anyway, also the code in the current question here is fine, just lookup how to use `or` in `pcase` (if you would like to clean it up a little). And you do not need an asterisk after the `let` here (but you could leave it of course). – dalanicolai Aug 11 '22 at 20:46
  • How are you suggesting using `or` with `pcase` exactly? – Dilna Aug 11 '22 at 22:00
  • Using `or` in `pcase` is advanced enough that I cannot understand it on my own. How can it help me? – Dilna Aug 11 '22 at 23:38

1 Answers1

0

Except for some (subjective) style changes, your code looks fine, and there is not much to be improved. However, your code contains three times (describe-face (intern facenm)). Often, there are ways to reduce repetition of code (but only do them when it improves the code). Here, you can use the pcase pattern (or pattern1 pattern2...)

(defun laxy-describe-face (facename)
  "Describe the typeface properties of face name FACENAME"
  (interactive
   (let ((options '("default"
                    "mode-line"
                    "mode-line-inactive"
                    "list"
                    "text-property")))
     (list (completing-read "Select face or action: " options nil t "text-property"))))
  (cond ((stringp facename)
         (pcase facename
           ("text-property"
            (describe-face (get-text-property (point) 'face)))
           ((or "default" "mode-line" "mode-line-inactive")
            (describe-face (intern facename)))
           ("list"
            (list-faces-display))) )
        ((facep facename)
         (describe-face facename))))
dalanicolai
  • 6,108
  • 7
  • 23