2011-12-04 12 views
12

Ho la seguente funzione per stampare la riga dove punto è al graffio * tampone *,stampare solo testo rigetti proprietà del testo

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

ma stampa anche informazioni fontified come questo

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

Come scartare la stampa delle informazioni criticate.

risposta

8

Avevo bisogno di qualcosa di simile per eredis quando si manipolano le stringhe da un tavolo dell'organizzazione. È possibile utilizzare `set-text-properties' per sbarazzarsi di loro durante la visualizzazione della stringa.

(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*"))) 
1

Ho provato alcune cose ma è strano, non capisco come funzionano le proprietà del testo.

Ad esempio:

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

Come hai detto, se si cerca di stamparlo, le proprietà vengono stampati così, ma se si cerca di inserirla:

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

Solo il la stringa viene stampata, non le proprietà.

Quindi mi sembra che tali proprietà sono solo legati alla stringa, ma è possibile manipolare la stringa come al solito:

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

Tuttavia, se tutto quello che volete è la linea, e solo la linea che si può utilizzare buffer-substring-no-properties:

(defun print-line()  
    (print (buffer-substring-no-properties (point-at-bol) (point-at-eol)))) 
+1

Anche la stringa del buffer stampa i messaggi sensibili, le proprietà di sottostringa-buffer-no-proprietà senza messaggi sensibili. –

+0

@Talespin_Kit: Oh, hai perfettamente ragione. – Daimrod

16

Ad ampliare menzione di Daimrod di buffer-substring-no-properties ...

MxaproposRETno-propertiesRET

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. 

Si può leggere su proprietà del testo nel manuale:

M-: (info "(elisp) Proprietà del testo") RET

+1

Woa Non ho notato 'substring-no-properties', ma con esso si può semplicemente fare' (defun print-line() (let ((line (thing-at-point 'line))) (sottostringa-no -proprietà linea 0 (lunghezza linea)))) '. – Daimrod

+0

@Daimrod Gli argomenti di 'substring-no-properties' sono facoltativi. – ceving

Problemi correlati