Search code examples
stringpropertiesemacs

Getting 1st element of string type in emacs lisp


I am trying to handle some data my little emacs helper program retrieves.

This is what the data structure look like:

#("[[id:b6f64bd1-3bf8-4a15-ad50-46bc5d5d1c94][Subsub1]]" 43 50 (fontified nil line-prefix #("**" 0 2 (face org-indent)) wrap-prefix #("***** " 0 2 (face org-indent) 2 6 (face org-indent)) face org-level-1 org-category "dummy")))

I am interested in getting the first element, i.e. the string "[[...]]".

Letting some functions investigate the data, does not really reveal the trick:

;; initiailize helper var
(setq dummy  #("[[id:b6f64bd1-3bf8-4a15-ad50-46bc5d5d1c94][Subsub1]]" 43 50 (fontified nil line-prefix #("**" 0 2 (face org-indent)) wrap-prefix #("***** " 0 2 (face org-indent) 2 6 (face org-indent)) face org-level-1 org-category "dummy")))

;; check type
(type-of dummy) ;; => string (for some reason unknown)

;; check length
 (length dummy) ;; => 52 (#o64, #x34, ?4)

;; get substring
(seq-subseq dummy 45 52) ;; => #("bsub1]]" 0 5 (fontified nil line-prefix #("**" 0 2 (face org-indent)) wrap-prefix #("***** " 0 2 (face org-indent) 2 6 (face org-indent)) face org-level-1 org-category "dummy"))

(seq-subseq dummy 50 52) ;; => "]]"

All this does not really make sense to me. From documentation, I'd assume that the construct `#(..." determines some vector.

But (seq-elt dummy 0) would not return the string I am looking for.

Can someone please provide some hint about this string datatype or even some direction about how to retrieve the first element of the data structure.


Solution

  • Unlike Common Lisp, Emacs Lisp doesn't use #(...) for vectors, it uses [...].

    In Emacs Lisp, there is a #( syntax for strings:

    #("characters" property-data...)
    

    It does look as if the data you are retrieving might be of this form. There is a manual section on working with strings with text properties.

    I see there is a propertize function which converts a regular string to one with properties.

    Use the substring-no-properties function to extract plain string data from it, without properties.