5

Is there a way to have describe-variable show characters instead of numbers where it makes sense?

Here's for example output of describe-variable word-separating-categories:

word-separating-categories is a variable defined in `C source code'. Its value is ((72 . 75))

Documentation: List of pair (cons) of categories to determine word boundary. See the documentation of the variable `word-combining-categories'.

It would help if instead ((72 . 75)) it would show something meaningful.

1
  • You could probably pipe the value of the variable through char-to-string.
    – Dan
    Apr 3, 2016 at 17:58

2 Answers 2

2

In Emacs a character is an integer. There is no way for describe-variable to know that the value of this variable is a list that contains conses with car and cdr that you want to see rendered as characters.

But you can use something like this:

(mapcar (lambda (cs)
          (cons (format "%c" (car cs)) (format "%c" (cdr cs))))
        word-separating-categories)
5
  • But it is certainly technically possible to provide a way for describe-variable to know such things. For example, by adding a suitable property to the symbol containing relevant information. I think a case could be made that this would be a useful enhancement to emacs. Apr 3, 2016 at 21:22
  • @HaraldHanche-Olsen I initially was going to suggest that the OP use M-x report-emacs-bug to request an enhancement of describe-variable. But I don't think it is very feasible. Certainly the usual approach of using "character" before the name in the docstring would not work, as the characters are within a larger structure. And what property value would you provide, for your suggestion? The value is not a character; it is a list of conses of characters. You would need an elaborate type-defining mechanism similar to what Customize :type offers, and that is certainly overengineering here.
    – Drew
    Apr 3, 2016 at 21:26
  • One possibility for the property could be a function to compute a string describing the value. For robustness, the function could return nil or throw an error in case of unexpected data, leaving describe-variable to use the standard display as a fallback. The cost of adding this functionality to describe-variable would be negligible – the cost of adding such functions to relevant variables, less trivial. But one doesn't have to do them all from day one. Apr 3, 2016 at 21:38
  • @HaraldHanche-Olsen: Go for it, if you think it is worthwhile - M-x report-emacs-bug. Include a patch...
    – Drew
    Apr 3, 2016 at 21:40
  • Hmm. Looking at the source code, I realize that the function called should probably be expected to prettyprint the result if it is large. That complicates things quite considerably. Apr 3, 2016 at 21:58
0

print1-to-string is used during making display string of the variable's value, so maybe you can try to hijack that, use, e.g.

(defun display-in-char-p (var val)
  (or (eq 'word-separating-categories var)
      (numberp val)
      (and (listp val) (ignore-errors (seq-every-p #'numberp val)))))

(defun describe-variable-hack (orig-fun &rest args)
  (when (display-in-char-p (car args) (symbol-value (car args)))
    (advice-add 'prin1-to-string :filter-return
                (defun prin1-to-string-hack (rtv)
                  (replace-regexp-in-string
                   "[0-9]+"
                   (lambda (match) (string (string-to-number match)))
                   rtv))))
  (unwind-protect (apply orig-fun args)
    (advice-remove 'prin1-to-string #'prin1-to-string-hack)))

(advice-add 'describe-variable :around #'describe-variable-hack)

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.