all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: user <user0012@cocaine.ninja>
To: help-gnu-emacs@gnu.org
Subject: How can I write this function better?
Date: Mon, 13 Mar 2017 13:49:29 -0600	[thread overview]
Message-ID: <87zigprm7q.fsf@cocaine.ninja> (raw)

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



             reply	other threads:[~2017-03-13 19:49 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-03-13 19:49 user [this message]
2017-03-14 12:44 ` How can I write this function better? Marcin Borkowski
2017-03-14 17:48 ` John Mastro
2017-03-15  9:55   ` Patrick
2017-03-16  8:30     ` Thien-Thi Nguyen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87zigprm7q.fsf@cocaine.ninja \
    --to=user0012@cocaine.ninja \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.