0

I want a function that can take a utf hexcode from the user, and use it to change the glasses-separator in glasses-mode. When I use completing-read using a list of hexcodes things for well and the separator uses the corresponding symbol associated with the hex code.

I also want the ability for the user to pass whatever hexcode he wants to use (not just being restricted to the list). I use read-string for the user input. The problem is that if a user enters \u21E8, the result is that for that CamelCase gets modified to the string Camel\u21E8Case rather than using the symbol associated with \u21E8 which is an arrow.

(defvar camelsens-actm nil)

(defun camelsens (hexcode)
  "Splits CamelCase phrases using separator."

(interactive
 (list
  (let* ( (optn '("list" "code"))
          (option (completing-read "Option: " optn nil t ""))
          (cseq '("\u27A4" "\u25BA" "\u2192" "disable")) )
    (pcase option
      ("list" (completing-read "Code: " cseq nil t "disable"))
      ("code" (read-string "Code: "))) )))

(pcase hexcode
  ("disable"
     (glasses-mode 0)
     (setq camelsens-actm nil))
  (_
     (unless camelsens-actm
       (glasses-mode 1)
       (setq camelsens-actm t))
     (setq glasses-separator hexcode)
     (glasses-set-overlay-properties))))
5
  • AFAICT, the main problem is that your function is not "clean", in the sense that sometimes the code variable is supposed to have a string value and sometimes it is supposed to be a single character (those are different types in Emacs Lisp and need conversion). But you seem wedded to writing functions in a particular style that makes it difficult (if not impossible) to know what it is supposed to be in any particular invocation, so you end up having to reason about each case. If you cannot describe what your function does in a sentence or two (with no exceptions), you should rethink it. Commented Oct 5, 2022 at 11:37
  • So instead of presenting a function that people have to figure out and then asking: "how do I fix this problem in it?", it is better to describe what you want to accomplish (in a couple of sentences, or in a prose paragraph if necessary, although it should be a short paragraph), and then study the responses carefully. Commented Oct 5, 2022 at 11:41
  • In particular, if the name of the function does not describe, more or less completely, what the function does, that is a problem. Commented Oct 5, 2022 at 11:51
  • Voting to close the question now. From the plethora of comments, to both the question and answers, it seems clear that the question isn't clear, and that there are multiple questions in it and it's evolving. This doesn't help readers. Please post specific, single questions, and don't try to turn this into a discussion site due to the possibility of helpful comments. Commented Oct 6, 2022 at 16:45
  • You post the same questions (and each is rarely a single question - more like an ongoing series of questions, often amounting to "please debug my code" - here, there, and everywhere. Friendly advice: it would help both you and others to try to focus your questions more. If you really want interactive discussion then try a site such as Reddit. HTH. Commented Oct 6, 2022 at 16:49

2 Answers 2

1

Try this:

(defvar camelsens-actm nil)

(defun toggle-glasses-mode-and-or-set-separator ()
  "Ask the user some questions and use the answers to specify whether to disable `glasses-mode' or to make sure that it is enabled, change the separator and redisplay the buffer."
  (interactive)
  (let* ((option (completing-read "Option: " '("list" "code") nil t ""))
         (disable-or-separator-string
          (pcase option
            ("list" (completing-read "Code: "
                                     '("\u27A4" "\u25BA" "\u2192" "disable")
                                     nil t "disable"))
            ("code" (string (read-char-by-name "Code: "))))))
    (pcase disable-or-separator-string
      ("disable"
       (glasses-mode 0)
       (setq camelsens-actm nil))
      (_
       (unless camelsens-actm
         (glasses-mode 1)
         (setq camelsens-actm t))
       (setq glasses-separator disable-or-separator-string)
       (glasses-set-overlay-properties)))))

(global-set-key (kbd "C-c z") #'toggle-glasses-mode-and-or-set-separator)

The name of the function tries to describe what the function does: the fact that it's a crazy name tells you something about the function. It's nevertheless easier to understand, primarily because of the variable disable-or-separator-string: that too is a crazy name for a crazy variable, but at least it has the advantage of always being a string (and if it isn't then you know where to look for the error).

5
  • Quite crazy. completing-read with \u does work but not with read-char-by-name which needs #x. Am unsure whether there is an inconsistency with display-fill-column-indicator-character which also takes unicode. Commented Oct 6, 2022 at 3:26
  • Now glasses-mode behaves as it should. Commented Oct 6, 2022 at 3:43
  • Yet, I do not see it too crazy to let a user decide which symbol to use for glasses-separator. What improvements can be done for this to be simplified? Changes to glasses.el perhaps? Commented Oct 6, 2022 at 3:46
  • That's not the crazy part: you can do that by customizing glasses-separator and you can use C-x 8 RET (bound to insert-char) there to insert arbitrary unicode characters. Why is that not good enough? The crazy part is wrapping simple things in complicated functions with complicated interfaces that need excruciating amounts of debugging to fix when they are broken (and because they are complicated, they are broken more often than not). Commented Oct 6, 2022 at 3:59
  • It would be an improvement if insert-char could also allow use of \u2192 rather than only #x2192. Commented Oct 6, 2022 at 10:25
0

You include lots of stuff in your question that seems extraneous, so it's not too clear what you're asking.

Is string-to-char what you're looking for?

(string-to-char "\u2192") ; => character \u2192

Evaluating that with C-x C-e prints this. The #x2192 shows the number value in hex. Elisp characters are simply numbers.

8594 (#o20622, #x2192)


If that's really what you're asking then your question has nothing to do with "interactive selection" of anything, and certainly nothing to do with glasses-mode.

If so, consider getting rid of everything that's irrelevant and titling your question something like "How do I convert a string that names a character to that character?" And the answer to that simple question is to use function string-to-char.

If that isn't really what you're asking then I have no idea what is.

9
  • The problem is about the call to (setq glasses-separator hexcode). I am doing (read-string "Code: "). When I input \u2192, the call to (setq glasses-separator hexcode) just uses the string \u2192 as separator, rather than its unicode character. But passing the code from completing-read does not introduce a problem. Commented Oct 5, 2022 at 10:08
  • So it is a string-to-char conversion problem, but it is mixed up in the convoluted interactive spec, which makes it difficult to figure out what is what in each particular case. You should step back and decide what the specification of your function should be: if you are not able to describe what your function is supposed to do in a couple of sentences, then rethink the spec first and then write the function to conform. Commented Oct 5, 2022 at 11:45
  • @NickD I have added more detail. The interactive spec is important because it is the way that a user can supply the utf8 hexcode for glasses-separator. Commented Oct 5, 2022 at 20:31
  • I have put ("code" (string-to-char (read-string "Code: "))) but the symbol still does not get inserted. Commented Oct 5, 2022 at 23:57
  • Use read-char-by-name instead: see the last paragraph in its description C-h f read-char-by-name. Commented Oct 6, 2022 at 0:05

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.