Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

print only text discarding text properties

Tags:

emacs

elisp

I have the following function to print the line where point is to the *scratch* buffer,

(defun print-line ()
  (print (thing-at-point 'line) (get-buffer "*scratch*")))

but it prints even the fontified info like this

#(" OFFICE
" 0 2 (fontified t org ...

How to discard the printing of the fontified info.

like image 809
Talespin_Kit Avatar asked Dec 04 '11 02:12

Talespin_Kit


3 Answers

To expand on Daimrod's mention of buffer-substring-no-properties...

M-x apropos RET no-properties RET

buffer-substring-no-properties
  Function: Return the characters of part of the buffer, without the
            text properties.
field-string-no-properties
  Function: Return the contents of the field around POS, without text
            properties.
insert-buffer-substring-no-properties
  Function: Insert before point a substring of BUFFER, without text
            properties.
match-string-no-properties
  Function: Return string of text matched by last search, without text
            properties.
minibuffer-contents-no-properties
  Function: Return the user input in a minibuffer as a string, without
            text-properties.
substring-no-properties
  Function: Return a substring of STRING, without text properties.

You can read about text properties in the manual:

M-: (info "(elisp) Text Properties") RET

like image 110
phils Avatar answered Nov 13 '22 07:11

phils


I needed something similar for eredis when manipulating strings from an org-table. You can use `set-text-properties' to get rid of them when displaying the string.

(defun strip-text-properties(txt)
  (set-text-properties 0 (length txt) nil txt)
      txt)

(defun print-line ()
 (print (strip-text-properties 
         (thing-at-point 'line))
    (get-buffer "*scratch*")))
like image 32
justinhj Avatar answered Nov 13 '22 08:11

justinhj


I've tried some things but it's weird, I don't really understand how text properties work.

For example:

(type-of (thing-at-point 'line)) => string

As you've said if one tries to print it, the properties are printed as well, but if one tries to insert it:

(insert (format "%s" (thing-at-point 'line)))

Only the string is printed, not the properties.

So it seems to me that those properties are just bound to the string but you can manipulate the string as usual:

(lenght (thing-at-point 'line))
(substring (thing-at-point 'line) 0 2)

However, if all you want is the line, and the line only you can use buffer-substring-no-properties:

(defun print-line ()     
  (print (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
like image 1
Daimrod Avatar answered Nov 13 '22 08:11

Daimrod