Skip to main content
Improve indentation and formatting
Source Link
Basil
  • 12.6k
  • 44
  • 70

Update: You can redefine the setf capability of substringsubstring to add properties from the original string to the new string. This is a lightly adapted version of code from line 616 in cl-lib.elcl-lib.el

(gv-define-expander substring
  (lambda (do place from &optional to)
    (gv-letplace (getter setter) place
      (macroexp-let2* nil ((start from) (end to))
        (funcall do `(substring ,getter ,start ,end)
                 (lambda (v)
                   (funcall setter
                `(apply 'propertize (cl--set-substring
                          ,getter ,start ,end ,v)
                    (text-properties-at 0 ,getter)))))))))


;; example use, returns original string and new string
(let ((s (propertize "testing" 'property1 "this is property1" 'property2 "this is property2")))
  (list s
    (setf (substring s 0 (length s)) "mystring")))


(#("testing" 0 7
   (property2 "this is property2" property1 "this is property1"))
 #("mystring" 0 8
   (property2 "this is property2" property1 "this is property1")))
(gv-define-expander substring
  (lambda (do place from &optional to)
    (gv-letplace (getter setter) place
      (macroexp-let2* nil ((start from) (end to))
        (funcall do `(substring ,getter ,start ,end)
                 (lambda (v)
                   (funcall setter
                            `(apply 'propertize (cl--set-substring
                                                 ,getter ,start ,end ,v)
                                    (text-properties-at 0 ,getter)))))))))

;; example use, returns original string and new string
(let ((s (propertize "testing" 'property1 "this is property1" 'property2 "this is property2")))
  (list s
        (setf (substring s 0 (length s)) "mystring")))


(#("testing" 0 7
   (property2 "this is property2" property1 "this is property1"))
 #("mystring" 0 8
   (property2 "this is property2" property1 "this is property1")))

Update: You can redefine the setf capability of substring to add properties from the original string to the new string. This is a lightly adapted version of code from line 616 in cl-lib.el

(gv-define-expander substring
  (lambda (do place from &optional to)
    (gv-letplace (getter setter) place
      (macroexp-let2* nil ((start from) (end to))
        (funcall do `(substring ,getter ,start ,end)
                 (lambda (v)
                   (funcall setter
                `(apply 'propertize (cl--set-substring
                          ,getter ,start ,end ,v)
                    (text-properties-at 0 ,getter)))))))))


;; example use, returns original string and new string
(let ((s (propertize "testing" 'property1 "this is property1" 'property2 "this is property2")))
  (list s
    (setf (substring s 0 (length s)) "mystring")))


(#("testing" 0 7
   (property2 "this is property2" property1 "this is property1"))
 #("mystring" 0 8
   (property2 "this is property2" property1 "this is property1")))

Update: You can redefine the setf capability of substring to add properties from the original string to the new string. This is a lightly adapted version of code from line 616 in cl-lib.el

(gv-define-expander substring
  (lambda (do place from &optional to)
    (gv-letplace (getter setter) place
      (macroexp-let2* nil ((start from) (end to))
        (funcall do `(substring ,getter ,start ,end)
                 (lambda (v)
                   (funcall setter
                            `(apply 'propertize (cl--set-substring
                                                 ,getter ,start ,end ,v)
                                    (text-properties-at 0 ,getter)))))))))

;; example use, returns original string and new string
(let ((s (propertize "testing" 'property1 "this is property1" 'property2 "this is property2")))
  (list s
        (setf (substring s 0 (length s)) "mystring")))


(#("testing" 0 7
   (property2 "this is property2" property1 "this is property1"))
 #("mystring" 0 8
   (property2 "this is property2" property1 "this is property1")))
added 1357 characters in body
Source Link
John Kitchin
  • 12.1k
  • 1
  • 23
  • 45

I think you can do this:

(setq myvar (apply #'propertize "mystring" (text-properties-at 0 myvar)))

I didn't find any way to use setf or some similar kind of thing to just modify the string contents while preserving the properties.

Update: You can redefine the setf capability of substring to add properties from the original string to the new string. This is a lightly adapted version of code from line 616 in cl-lib.el

(gv-define-expander substring
  (lambda (do place from &optional to)
    (gv-letplace (getter setter) place
      (macroexp-let2* nil ((start from) (end to))
        (funcall do `(substring ,getter ,start ,end)
                 (lambda (v)
                   (funcall setter
                `(apply 'propertize (cl--set-substring
                          ,getter ,start ,end ,v)
                    (text-properties-at 0 ,getter)))))))))


;; example use, returns original string and new string
(let ((s (propertize "testing" 'property1 "this is property1" 'property2 "this is property2")))
  (list s
    (setf (substring s 0 (length s)) "mystring")))


(#("testing" 0 7
   (property2 "this is property2" property1 "this is property1"))
 #("mystring" 0 8
   (property2 "this is property2" property1 "this is property1")))

I couldn't say if this is a good idea, as it only gets the properties from character 0. In your case that seems ok, but in general it might not be. It might or might not make sense though to get all properties from every character in that region to put on the new string.

I think you can do this:

(setq myvar (apply #'propertize "mystring" (text-properties-at 0 myvar)))

I didn't find any way to use setf or some similar kind of thing to just modify the string contents while preserving the properties.

I think you can do this:

(setq myvar (apply #'propertize "mystring" (text-properties-at 0 myvar)))

I didn't find any way to use setf or some similar kind of thing to just modify the string contents while preserving the properties.

Update: You can redefine the setf capability of substring to add properties from the original string to the new string. This is a lightly adapted version of code from line 616 in cl-lib.el

(gv-define-expander substring
  (lambda (do place from &optional to)
    (gv-letplace (getter setter) place
      (macroexp-let2* nil ((start from) (end to))
        (funcall do `(substring ,getter ,start ,end)
                 (lambda (v)
                   (funcall setter
                `(apply 'propertize (cl--set-substring
                          ,getter ,start ,end ,v)
                    (text-properties-at 0 ,getter)))))))))


;; example use, returns original string and new string
(let ((s (propertize "testing" 'property1 "this is property1" 'property2 "this is property2")))
  (list s
    (setf (substring s 0 (length s)) "mystring")))


(#("testing" 0 7
   (property2 "this is property2" property1 "this is property1"))
 #("mystring" 0 8
   (property2 "this is property2" property1 "this is property1")))

I couldn't say if this is a good idea, as it only gets the properties from character 0. In your case that seems ok, but in general it might not be. It might or might not make sense though to get all properties from every character in that region to put on the new string.

Improve formatting
Source Link
Basil
  • 12.6k
  • 44
  • 70

I think you can do this:

(setq myvar (apply 'propertize "mystring" (text-properties-at 0 myvar)))

(setq myvar (apply #'propertize "mystring" (text-properties-at 0 myvar)))

I didn't find any way to use setfsetf or some similar kind of thing to just modify the string partcontents while preserving the properties.

I think you can do this:

(setq myvar (apply 'propertize "mystring" (text-properties-at 0 myvar)))

I didn't find any way to use setf or some similar kind of thing to just modify the string part while preserving the properties.

I think you can do this:

(setq myvar (apply #'propertize "mystring" (text-properties-at 0 myvar)))

I didn't find any way to use setf or some similar kind of thing to just modify the string contents while preserving the properties.

added 32 characters in body
Source Link
John Kitchin
  • 12.1k
  • 1
  • 23
  • 45
Loading
Source Link
John Kitchin
  • 12.1k
  • 1
  • 23
  • 45
Loading