* [emacs-lisp newbie] print-something() -> clipboard?
@ 2012-05-18 19:46 Tom Roche
2012-05-19 7:19 ` Andreas Röhler
` (2 more replies)
0 siblings, 3 replies; 6+ messages in thread
From: Tom Roche @ 2012-05-18 19:46 UTC (permalink / raw)
To: help-gnu-emacs
From my init.el I load a file that contains function definitions which
all have the form
> (defun print-path-and-filename ()
> "Print the current buffer's file's path at point."
> (interactive "*") ; abort if buffer is read-only
> (if buffer-file-name (insert buffer-file-name) nil))
> (global-set-key "\C-pp" 'print-path-and-filename)
...
> (defun print-buffer-name ()
> "Print (at point) the current buffer's name."
> (interactive "*") ; abort if buffer is read-only
> (insert (buffer-name (window-buffer (minibuffer-selected-window)))))
> (global-set-key "\C-pb" 'print-buffer-name)
>
> (provide 'tlr-print)
Most of the functions are buffer-independent (printing, e.g., current
time, emacs version) but some (like the above) are not. For the
latter, I'd like to have the option to "print" the datum to the
clipboard. How can I (easily :-) do that?
Examples esp appreciated. Feel free to point to doc.
TIA, Tom Roche <Tom_Roche@pobox.com>
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [emacs-lisp newbie] print-something() -> clipboard?
2012-05-18 19:46 [emacs-lisp newbie] print-something() -> clipboard? Tom Roche
@ 2012-05-19 7:19 ` Andreas Röhler
2012-05-20 18:57 ` Tom Roche
2012-05-20 20:27 ` Tom Roche
2 siblings, 0 replies; 6+ messages in thread
From: Andreas Röhler @ 2012-05-19 7:19 UTC (permalink / raw)
To: help-gnu-emacs
Am 18.05.2012 21:46, schrieb Tom Roche:
>
>> From my init.el I load a file that contains function definitions which
> all have the form
>
>> (defun print-path-and-filename ()
>> "Print the current buffer's file's path at point."
>> (interactive "*") ; abort if buffer is read-only
>> (if buffer-file-name (insert buffer-file-name) nil))
>> (global-set-key "\C-pp" 'print-path-and-filename)
> ...
>> (defun print-buffer-name ()
>> "Print (at point) the current buffer's name."
>> (interactive "*") ; abort if buffer is read-only
>> (insert (buffer-name (window-buffer (minibuffer-selected-window)))))
>> (global-set-key "\C-pb" 'print-buffer-name)
>>
>> (provide 'tlr-print)
>
> Most of the functions are buffer-independent (printing, e.g., current
> time, emacs version) but some (like the above) are not. For the
> latter, I'd like to have the option to "print" the datum to the
> clipboard. How can I (easily :-) do that?
>
> Examples esp appreciated. Feel free to point to doc.
>
> TIA, Tom Roche<Tom_Roche@pobox.com>
>
>
Hi,
the answer might not be a one-liner, as the X-clipboard does not come with Emacs.
This example works at my machine:
(defun yank-date-clipboard ()
"Insert into the kill-ring, at X also into the clipboard."
(interactive)
(kill-new (format-time-string "%Y%m%d")))
HTH,
Andreas
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [emacs-lisp newbie] print-something() -> clipboard?
2012-05-18 19:46 [emacs-lisp newbie] print-something() -> clipboard? Tom Roche
2012-05-19 7:19 ` Andreas Röhler
@ 2012-05-20 18:57 ` Tom Roche
2012-05-20 19:20 ` Drew Adams
2012-05-20 20:27 ` Tom Roche
2 siblings, 1 reply; 6+ messages in thread
From: Tom Roche @ 2012-05-20 18:57 UTC (permalink / raw)
To: help-gnu-emacs
Tom Roche 18.05.2012 21:46
>> (defun print-buffer-name ()
>> "Print (at point) the current buffer's name."
>> (interactive "*") ; abort if buffer is read-only
>> (insert (buffer-name (window-buffer (minibuffer-selected-window)))))
>> (global-set-key "\C-pb" 'print-buffer-name)
...
>> I'd like to have the option to "print" the datum to the clipboard.
>> How can I (easily :-) do that?
Andreas Röhler Sat, 19 May 2012 09:19:36 +0200
> (defun yank-date-clipboard ()
> "Insert into the kill-ring, at X also into the clipboard."
> (interactive)
> (kill-new (format-time-string "%Y%m%d")))
Thanks! So now I'm wondering, how can I toggle between
insert-to-buffer and insert-to-clipboard semantics? I now have
(defun print-buffer-name ()
"Print (at point) the current buffer's name."
(interactive "*") ; abort if buffer is read-only
(let ((bn (buffer-name (window-buffer (minibuffer-selected-window)))))
(insert bn)))
(global-set-key "\C-pb" 'print-buffer-name)
(defun print-buffer-name-clipboard ()
"Print to clipboard the current buffer's name."
(interactive "*") ; abort if buffer is read-only
(let ((bn (buffer-name (window-buffer (minibuffer-selected-window)))))
(kill-new bn)))
(global-set-key "\C-pc" 'print-buffer-name-clipboard)
but I don't want to define a separate keychord in each case. I want
something like (expressed procedurally--unfortunately I currently think
in '{}', not '()')
if I type [C-p b]
(insert bn)
else if I type [C-u C-p b]
(kill-new bn)
Can that be done with one {function, keychord} definition?
Or must I have 2, like above? If the latter, how to define
[C-u C-p b]?
your assistance is appreciated, Tom Roche <Tom_Roche@pobox.com>
^ permalink raw reply [flat|nested] 6+ messages in thread
* RE: [emacs-lisp newbie] print-something() -> clipboard?
2012-05-20 18:57 ` Tom Roche
@ 2012-05-20 19:20 ` Drew Adams
0 siblings, 0 replies; 6+ messages in thread
From: Drew Adams @ 2012-05-20 19:20 UTC (permalink / raw)
To: help-gnu-emacs, 'Tom Roche'
> if I type [C-p b]
> (insert bn)
> else if I type [C-u C-p b]
> (kill-new bn)
>
> Can that be done with one {function, keychord} definition?
> Or must I have 2, like above? If the latter, how to define
> [C-u C-p b]?
Bind `C-p b' to `foo':
(defun foo (&optional arg)
"..."
(interactive "P")
(let ((bn ...))
(if arg
(insert bn)
(kill-new bn))))
See the Elisp manual, node `Prefix Command Arguments'.
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [emacs-lisp newbie] print-something() -> clipboard?
2012-05-18 19:46 [emacs-lisp newbie] print-something() -> clipboard? Tom Roche
2012-05-19 7:19 ` Andreas Röhler
2012-05-20 18:57 ` Tom Roche
@ 2012-05-20 20:27 ` Tom Roche
2012-05-21 13:24 ` Drew Adams
2 siblings, 1 reply; 6+ messages in thread
From: Tom Roche @ 2012-05-20 20:27 UTC (permalink / raw)
To: help-gnu-emacs
Tom Roche 18.05.2012 21:46
>>> (defun print-buffer-name ()
>>> "Print (at point) the current buffer's name."
>>> (interactive "*") ; abort if buffer is read-only
>>> (insert (buffer-name (window-buffer (minibuffer-selected-window)))))
>>> (global-set-key "\C-pb" 'print-buffer-name)
...
>>> I'd like to have the option to "print" the datum to the clipboard.
>>> How can I (easily :-) do that?
Andreas Röhler Sat, 19 May 2012 09:19:36 +0200
>> (defun yank-date-clipboard ()
>> "Insert into the kill-ring, at X also into the clipboard."
>> (interactive)
>> (kill-new (format-time-string "%Y%m%d")))
Drew Adams Sun, 20 May 2012 12:20:09 -0700
> (defun foo (&optional arg)
> "..."
> (interactive "P")
> (let ((bn ...))
> (if arg
> (insert bn)
> (kill-new bn))))
Thanks! I probably should `let`, but this works:
(defun print-buffer-name (&optional arg)
"Print (at point) the current buffer's name, or to clipboard with prefix."
(interactive "P") ; sets first arg to the raw command prefix
(setq bn (buffer-name (window-buffer (minibuffer-selected-window))))
(if arg
(kill-new bn)
(insert bn)))
(global-set-key "\C-pb" 'print-buffer-name)
thanks again, Tom Roche <Tom_Roche@pobox.com>
^ permalink raw reply [flat|nested] 6+ messages in thread
* RE: [emacs-lisp newbie] print-something() -> clipboard?
2012-05-20 20:27 ` Tom Roche
@ 2012-05-21 13:24 ` Drew Adams
0 siblings, 0 replies; 6+ messages in thread
From: Drew Adams @ 2012-05-21 13:24 UTC (permalink / raw)
To: help-gnu-emacs, 'Tom Roche'
> > (defun foo (&optional arg)
> > "..."
> > (interactive "P")
> > (let ((bn ...))
> > (if arg
> > (insert bn)
> > (kill-new bn))))
>
> Thanks! I probably should `let`,
Yes, you should.
> but this works:
>
> (defun print-buffer-name (&optional arg)
> "..."
> (interactive "P")
> (setq bn ...)
> (if arg
> (kill-new bn)
> (insert bn)))
`bn' is a free variable here - by default a global, dynamically scoped variable.
If you or some code that you use defines another variable named `bn' then your
code will suffer from variable capture: You could end up changing the other
variable.
There is NO reason to use `setq' here, and NO reason not to use `let'.
When you suffer from variable capture you don't necessarily realize it, and
debugging the problem (with perhaps farflung symptoms) can be a headache. A
self-imposed headache in this case, for no benefit.
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2012-05-21 13:24 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-05-18 19:46 [emacs-lisp newbie] print-something() -> clipboard? Tom Roche
2012-05-19 7:19 ` Andreas Röhler
2012-05-20 18:57 ` Tom Roche
2012-05-20 19:20 ` Drew Adams
2012-05-20 20:27 ` Tom Roche
2012-05-21 13:24 ` Drew Adams
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).