1

Is there a way to add text properties to empty strings? As far as I can tell, calling put-text-property on an empty string silently does nothing. I seem to recall reading somewhere that the empty string is (at least potentially) a singleton, so perhaps this is why it doesn't work.

This may seem like a perverse thing to want to do since a text property that doesn't apply to any text is pretty useless. I'm working on a company-mode backend that – for reasons which aren't really important to this question – needs to remember where the completion prefix starts. This is easy to calculate, and after doing so I stash the offset as a text property on the prefix.

However, a prefix can, in some very important cases, be blank. Since I can't add properties to an empty prefix, I'm stuck checking whether the property is blank in lots of places. Hence, my initial question.

If it's not possible to add text properties to blank strings, is there some other way I could transparently associate extra data with the empty prefix string? I know some lisps allow you to tack meta data onto objects, so perhaps there's something like this in elisp. Any ideas would be greatly appreciated.

  • 1
    Can you use the zero width white space unicode character to create that 'blank' string? You can insert it by doing C-x 8 RET 200b RET. – Kaushal Modi Mar 28 '15 at 13:54
1
  1. Nope. When you add text properties to a string you really add them to the characters in the string. There are no characters in an empty string - nothing to add the properties to.

  2. I know of no way to associate anything with an empty string. However, you can associate data with a cons cell (which contains an empty string, for example). Or you can associate data with a non-empty string (which has an empty string as its prefix or suffix or anywhere else inside it, for example). And you can associate data with a symbol, and that symbol can have an associated string (either its print name or as a separate property).

IOW, try encapsulating your string within a cons cell, a larger string, or a symbol, or a structure/object,...

Or else keep track of the data in a separate structure that stores info about several such strings (e.g., the bookmarks model).

  • Using some ancillary data structure might be possible, but I have a feeling it won't pan out. The prefix string is threaded through the various stages of company-mode processing, and changing it to some other structure may break company's internal processing. Thanks for the pointers, though! – abingham Mar 27 '15 at 23:27
  • Consider attaching the string you really want, which might be empty, to a carrier string that is just a suffix or prefix, which is nonempty and which holds the properties for the part you are interested in. IOW, instead of string S, use string S2, where S is a prefix (or suffix) of S2. – Drew Mar 28 '15 at 0:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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