2

I have a Java properties file that, when opened with Emacs, shows up in a color coded form, as below:

enter image description here

I also have a simple text file.

When I yank something from the properties file into the text file, the font colors are yanked along with the content.

enter image description here

How can I strip a selection of such text in the text file of any colors and any other unwanted decorations?

  • As an idea, you could advice kill-ring-save or yank to apply remove-text-properties in the object. – Muihlinn Aug 25 at 19:26
1

Assuming that your current work flow is doing M-w (i.e. kill-ring-save) and then yanking it with C-y, you can accomplish what you want with a function modeled after kill-ring-save, which saves a substring of the buffer to the kill ring but without any properties. The modified function works by temporarily setting the variable filter-buffer-substring-function to a function that filters the buffer substring and gets rid of properties:

(defun ndk/buffer-substring-no-properties-filter (beg end delete)
  (buffer-substring-no-properties beg end))

(defun ndk/kill-ring-save-no-properties (beg end &optional region)
  (interactive (list (mark) (point)
                     (prefix-numeric-value current-prefix-arg)))
  (let ((filter-buffer-substring-function #'ndk/buffer-substring-no-properties-filter))
    (if region
        (kill-ring-save beg end region)
      (kill-ring-sage beg end))))

You can bind the function to a key like this:

(global-set-key (kbd "C-c w") #'ndk/kill-ring-save-no-properties)

I am assuming that C-c w is not used either in the global map or in any of the local maps that might be active in the buffer where the strings you wish to copy are (the Java properties file in your example). If it is, you might have to hunt around for a suitable alternative, but C-c w should be free: the sequence C-c <letter> where <letter> is an upper or lower case letter is reserved for users.

You can add the definitions of the two function and the key binding to your emacs initialization file.

With these settings, the work flow is:

  • select a region in the original buffer
  • do C-c w (instead of M-w)
  • switch to the other buffer
  • yank with C-y

See the doc string for the variable filter-buffer-substring-function for some of the gory details.

EDIT: I tried doing something similar on the yank side, rather than the kill-ring-save side, as @Drew indicated in his answer, but it did not work for me and I'm not sure why it did not work. Here's the function I tried:

(defun ndk/yank-no-properties ()
  (interactive)
  (let ((yank-excluded-properties t))
    (yank)))

From a very cursory reading of the yank code (i.e. this may be completely wrong), it looks as if the yank-handler property is an essential ingredient but I don't know how to deal with it.

| improve this answer | |
  • This worked wonderfully, and is a great long-term solution for people who have to deal with this issue a lot. Thank you for taking the time to write it out in such detail, and in such easy-to-follow instructions. – Calculemus Aug 28 at 11:04
1
  1. If you're using Lisp: don't remove the properties after yanking. Save for yanking with the properties already removed.

    Instead of using buffer-substring use buffer-substring-no-properties. C-h f says:

    buffer-substring-no-properties is a built-in function in ‘C source code’.

    (buffer-substring-no-properties START END)

    Return the characters of part of the buffer, without the text properties. The two arguments START and END are character positions; they can be in either order.

  2. But if you do want the properties in the text saved on the kill ring for some reason, but you don't want them when you yank, use option yank-excluded-properties or yank-handled-properties to handle that. C-h v says:

    yank-excluded-properties is a variable defined in simple.el.

    Its value is ...

    Documentation:

    Text properties to discard when yanking.

    The value should be a list of text properties to discard or t, which means to discard all text properties.

    See also yank-handled-properties.

    You can customize this variable.

| improve this answer | |
  • Thank you for your answer. Unfortunately I think I need more directions. I'm not a proficient Emacs user, and I rarely evaluate Lisp expressions within it. I used M-x eval-expression and called (buffer substring-no properties (region-beginning) (region-end)) but all it did was flash the yanked string in the mini-buffer. I wasn't able to do anything with the string. I also tried M-x eval-expression and called yank-excluded-properties but all that resulted in was more mini-buffer flashing. What am I doing wrong? – Calculemus Aug 25 at 21:25
  • Just customize the yank option yank-excluded-properties: M-x customize-option. – Drew Aug 25 at 21:44

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.