unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* How can I write this function better?
@ 2017-03-13 19:49 user
  2017-03-14 12:44 ` Marcin Borkowski
  2017-03-14 17:48 ` John Mastro
  0 siblings, 2 replies; 5+ messages in thread
From: user @ 2017-03-13 19:49 UTC (permalink / raw)
  To: help-gnu-emacs

Hello help-gnu-emacs, I'm looking for pointers with my luser-0x0 function. I'm
sure there are better ways I could write this. I'm not asking to have you
rewrite it for me, that'd be rude of me, but rather what I should to look at,
such as documentation or learning new constructs etc. I'm _very_ new to
programming and Lisp (learning Lisp and Forth as my first languages; quite
fun). An explanation, which I first wrote for myself, is below the code.

Thanks.

(defun luser-0x0 ()
  "Upload region or file to https://0x0.st"
  (interactive) 
  (let ((temporary-file (make-temp-file "0x0" nil
					(file-name-extension (or (buffer-file-name)
								 (concat (buffer-name) ".txt")) 
							     t)))
	(buffer (list (point-min) (point-max)))
	(region (list (region-beginning) (region-end))))
    (apply 'write-region (append (if (region-active-p) region
				   buffer)
				 (list temporary-file)))
    (set-process-filter (start-process "0x0" nil "curl"
				       "-F" (format "file=@%s" temporary-file)
				       "https://0x0.st")
			(lambda (process output)
			  (kill-new (message output))))))


This is a function to upload text to the https://0x0.st service. I use 0x0
because it's raw text, which means people can download it without opening or
switching to a browser.

Text is uploaded via the curl command: ‘curl -F'file=@foo.text' https://0x0.st’.

Since we use curl, a file must be created, which is done with (make-temp-file...).
The third argument to (make-temp-file ...) is a suffix for the file, our usage
of it will now be explained.

Uploading files without extensions to 0x0.st causes the service to append the
.bin suffix which could cause people to be nervous about downloading the
file. For this reason we generate an appropriate extension when possible, or
‘.txt’ when buffers aren't files. I'm explicit (verbose) with the creation of
‘.txt’ for the sake of clarity.

I want this function to be dwim-like[1]. When uploading text, you're either
selecting a specific piece or everything in the current buffer. When writing
our text to our temporary file, we'll use write-region even when there is no
region, since the usage of write-file forces us to visit the written file.

The usage of apply on a list of arguments is for simplicity, and once again,
clarity. The intent is nicely obvious with the variable names.

To use curl, we must start a process. If we separated (set-process-filter ...)
from (start-process ...), say creating a function, the process filter would
never run because curl would already be finished (our function is sequential).

With the process started inside (set-process-filter ...) we're able to apply
the filter when the process starts and before it finishes. Our filter function
is the easiest part of the code! It displays the output in the echo area and
kills it.

[1] https://en.wikipedia.org/wiki/DWIM



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2017-03-16  8:30 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-03-13 19:49 How can I write this function better? user
2017-03-14 12:44 ` Marcin Borkowski
2017-03-14 17:48 ` John Mastro
2017-03-15  9:55   ` Patrick
2017-03-16  8:30     ` Thien-Thi Nguyen

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).