[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

How can I write this function better?

From: user
Subject: How can I write this function better?
Date: Mon, 13 Mar 2017 13:49:29 -0600
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1 (gnu/linux)

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.


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

This is a function to upload text to the 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'’.

Since we use curl, a file must be created, which is done with 
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 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.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]