4

I have a yasnippet that is intended to create a footnote in org-mode format and automatically insert the contents of the clipboard.

# -*- mode: snippet -*-
# name: footnote
# key: fn 
# --
[fn:: `(clipboard-yank)`]

This worked correctly until this week. Desired output is this:

[fn:: foo]

But now when I do it, it truncates the contents and adds a Y, such that when the clipboard contains foo, entering fn <TAB> results in this output:

[fn:: ooY]

UPDATE/CLARIFICATION

My yasnippet for org-mode footnotes actually uses not clipboard-yank but my own custom function pasteboard-paste-no-spaces.

# -*- mode: snippet -*-
# name: footnote
# key: fn 
# --
[fn:: `(pasteboard-paste-no-spaces)`]

For simplicity, I mentioned clipboard-yank since I tested it and it has the same problem. But since I use a custom function, switching to current-kill won't work for me.

Here's pasteboard-paste-no-spaces:

(defun pasteboard-paste-no-spaces ()
  "Paste from OS X system pasteboard via `pbpaste' to point."
  (interactive)
  (let ((start (point))
    (end (if mark-active
         (mark)
           (point))))
    (shell-command-on-region start end
                 "pbpaste | perl -p -e 's/\r$//' | tr '\r' '\n'"
                 nil t)
    (save-excursion
      )))

UPDATE #2

Thanks for all the help. I'm having the same problem with tiny-expand. I tried using the (with-temp-buffer) approach you suggested to create a new function yas/tiny-expand, but it doesn't work:

(defun yas/tiny-expand (&rest args)
 "Return the string that `tiny-expand' would insert."
 (with-temp-buffer
 (apply #'tiny-expand args) 
 (buffer-string))) 

Here is the relevant yasnippet where I tried to use (with-temp-buffer): https://gist.github.com/5a799834ddf948ce65a7def95a834401

Here's the original version: https://gist.github.com/1c947de20a1b7790d04d69ac39884f22

incandescentman
  • 4,111
  • 16
  • 53

1 Answers1

5

You should use current-kill instead of clipboard-yank because the latter works by side-effect.

# -*- mode: snippet -*-
# name: footnote
# key: fn 
# --
[fn:: `(current-kill 0)`]

I'm actually not using clipboard-yank but my own custom function pasteboard-paste-no-spaces

You would need to modify your custom function to return a string value instead of inserting it directly, something like:

(defun pasteboard-without-spaces ()
  "Return content of OS X system pasteboard via `pbpaste'."
  (shell-command-to-string "pbpaste | perl -p -e 's/\r$//' | tr '\r' '\n'"))

I seem to be getting the same error with [...] org-insert-time-stamp

That function is more problematic since it weaves together inserting the string with generating it. You can run it in a temp buffer to make it behave better:

(defun yas/org-get-time-stamp (&rest args)
  "Return the string that `org-insert-time-stamp' would insert."
  (with-temp-buffer
    (apply #'org-insert-time-stamp args)
    (buffer-string)))

You can apply this trick to any function that inserts content directly to the buffer.


I'm having the same problem with tiny-expand.

This functions also wants to read and delete other text in the buffer, to make it safe the text should be passed as a string instead:

(defun yas/tiny-expand (str)
  (with-temp-buffer
    (insert str)
    (goto-char (point-max)) ; tiny-expand works on text preceding point
    (tiny-expand)
    (buffer-string)))
# -*- mode: snippet; require-final-newline: nil -*-
# name: jdw
# key: jdw
# --

* week of `(yas/org-get-time-stamp (current-time))` [0%] 
*** WEEKLIES - week of `(yas/org-get-time-stamp (current-time))` 
***** TODO review weeklies
***** TODO $0

*** DAILIES - week of `(yas/org-get-time-stamp (current-time))` 
`(yas/tiny-expand "m0\\n9|\\n***** COMMITTED <%(date \"today\" x)> [0%%]\\n******* TODO wake up 8:30am\\n SCHEDULED: <%(date \"today\" x) 8:30am>\\n******* TODO blue light \\n SCHEDULED: <%(date \"today\" x) 8:31am>\\n******* TODO meditate \\n SCHEDULED: <%(date \"today\" x) 8:45am>\\n******* TODO write Wise Mind form or LIFE form\\n SCHEDULED: <%(date \"today\" x) 9:00am> \\n******* TODO disable Internet\\n SCHEDULED: <%(date \"today\" x) 9:15am> \\n******* TODO morning pages at a café or at The Writers' Room \\n SCHEDULED: <%(date \"today\" x) 9:45am>\\n******* TODO go straight to The Writers' Room\\n SCHEDULED: <%(date \"today\" x) 10:00am>\\n******* TODO review weeklies and calibrate with dailies \\n******* TODO submit dailies to Erika \\n DEADLINE: <%(date \"today\" x) 2:00pm> \\n******* TODO work on book 3 hours \\n******* TODO two lines of poetry \\n DEADLINE: <%(date \"today\" x) 8:00pm> \\n******* TODO lights out by 12:30am\\n SCHEDULED: <%(date \"today\" x) 11:59pm> \\n\\n")`

Note that the string param needs to be escaped for the Lisp reader (an easy way to do this quickly is to use paredit-doublequote).


If you don't feel like changing all your snippets right now, the latest release, 0.10.0 (available from MELPA stable and GNU ELPA), will work with your original snippet. I plan to temporarily (i.e. at least until release 0.11.0) back out the change that breaks this and just issue a warning for snippets that do insertion by side-effect. See also yasnippet issue #710.

npostavs
  • 9,033
  • 1
  • 21
  • 53
  • I'm actually not using `clipboard-yank` but my own custom function `pasteboard-paste-no-spaces`. For simplicity, I avoided trying to explain a complicated custom function in the question since this also happens with `clipboard-yank`. But switching to `current-kill` won't work for me. I added details in the question. – incandescentman Jun 20 '16 at 00:16
  • 1
    @incandescentman: updated for your custom paste function. – npostavs Jun 20 '16 at 00:22
  • I seem to be getting the same error with this snippet: `(let ((x (org-insert-time-stamp (current-time)))))`. How would I fix this one? https://gist.github.com/2e49d06c84cbcb92c7e6bc0a11759b3b – incandescentman Jun 20 '16 at 15:22
  • 2
    @incandescentman wrap in `with-temp-buffer`, see edit. – npostavs Jun 20 '16 at 16:58
  • That works! Thank you so much for all the help. I'm now having the same problem with `tiny-expand`. See addition above. – incandescentman Jun 20 '16 at 17:39
  • @incandescentman updated for `tiny-expand` – npostavs Jun 21 '16 at 20:04