From: John Mastro <john.b.mastro@gmail.com>
To: "help-gnu-emacs@gnu.org" <help-gnu-emacs@gnu.org>
Cc: user <user0012@cocaine.ninja>
Subject: Re: How can I write this function better?
Date: Tue, 14 Mar 2017 10:48:27 -0700 [thread overview]
Message-ID: <CAOj2CQQQNn0n9MtJvGDW6fEcC=9KdsUyEKdHWtSuHkmU79qnJw@mail.gmail.com> (raw)
In-Reply-To: <87zigprm7q.fsf@cocaine.ninja>
user <user0012@cocaine.ninja> wrote:
> 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.
Here was my first thought on how I would personally write it. I wouldn't
say it's better (they achieve the same thing) but perhaps it will give
you an idea or two.
The things I changed were:
- Make the region beginning and end positions arguments to the function
(which default to the region-or-buffer) when called interactively
- Only call `file-name-extension' when the buffer is visiting a file,
otherwise just use ".txt"
- Call `write-region' directly (rather than via `apply')
- Use `call-process' with a destination buffer rather than
`start-process' with a sentinel
- Delete the temporary file at the end unless an optional argument
says not to
Regarding the last point, `start-process' does have an advantage, which
is that the process is asynchronous. However, that may not matter for
this, if the upload will proceed quickly and/or you would wait for it to
finish before moving on anyway. For instance, if the upload takes a
while, then it might be odd to have its output added to your kill-ring
at some point later anyway.
Here's the code (lightly tested):
(defun luser-0x0 (beg end &optional keep-file)
"Upload region from BEG to END https://0x0.st.
When called interactively, BEG and END default to the bounds of
the region if active, or the buffer otherwise. If KEEP-FILE is
non-nil, do not delete the temporary file that was uploaded."
(interactive
(if (use-region-p)
(list (region-beginning) (region-end))
(list (point-min) (point-max))))
(let ((file (make-temp-file "0x0" nil
(if (buffer-file-name)
(file-name-extension (buffer-file-name) t)
".txt"))))
(write-region beg end file)
(with-temp-buffer
(call-process "curl" nil (current-buffer) nil
"-F" (concat "file=@" file) "https://0x0.st")
(kill-new (message "%s" (buffer-string))))
(if keep-file
file
(delete-file file t)
nil)))
Hope that helps
John
next prev parent reply other threads:[~2017-03-14 17:48 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
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
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CAOj2CQQQNn0n9MtJvGDW6fEcC=9KdsUyEKdHWtSuHkmU79qnJw@mail.gmail.com' \
--to=john.b.mastro@gmail.com \
--cc=help-gnu-emacs@gnu.org \
--cc=user0012@cocaine.ninja \
/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.
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).