help-gnu-emacs
[Top][All Lists]
Advanced

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

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 "address@hidden" 
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'address@hidden' 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 via email to

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