From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: user Newsgroups: gmane.emacs.help Subject: How can I write this function better? Date: Mon, 13 Mar 2017 13:49:29 -0600 Message-ID: <87zigprm7q.fsf@cocaine.ninja> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Trace: blaine.gmane.org 1489437742 4031 195.159.176.226 (13 Mar 2017 20:42:22 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Mon, 13 Mar 2017 20:42:22 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1 (gnu/linux) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Mon Mar 13 21:42:17 2017 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cnWnC-00085B-2j for geh-help-gnu-emacs@m.gmane.org; Mon, 13 Mar 2017 21:42:10 +0100 Original-Received: from localhost ([::1]:54319 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cnWnI-0006ry-6g for geh-help-gnu-emacs@m.gmane.org; Mon, 13 Mar 2017 16:42:16 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:41341) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cnVyO-0008RY-P7 for help-gnu-emacs@gnu.org; Mon, 13 Mar 2017 15:49:42 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cnVyL-0007RC-Ir for help-gnu-emacs@gnu.org; Mon, 13 Mar 2017 15:49:40 -0400 Original-Received: from cock.li ([185.100.85.212]:37664) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1cnVyL-0007PB-94 for help-gnu-emacs@gnu.org; Mon, 13 Mar 2017 15:49:37 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=cocaine.ninja; s=mail; t=1489434573; bh=DDCk7t1Hpz9T8lWNNlMp8QZil1ivc3YVMcG9hBzonFY=; h=From:To:Subject:Date:From; b=Suis893O+7dO+WWDkeiCc6KO0eY8BDDlqPuf6YFgo/vvFdv38hKKwfn3uJPgvG5fo pzFsP9EllQLORjIwdDILvLjiFHsJ3KrOdnucx0qLSAgI73LFtepqwnqXR4xJjtwYQn dfSvd3sveCQdJNnqLJ6jycwqr67aMRBMk7X0y8i3WdY+yWq+BFqsXOLkkFuouIpbE3 m0p+46CIumaB/I2TNdLoPyOKognjnZli0zQiGJKJpzlGrvmNXrsT4q9iHVdNy68laJ 2FhrQ+/I2HxWFiEx9NOXNACDzkIFrk4RFeqxmrb989TsMBOHKSnZpOm5CXJzP3AGn7 eD+DZvC+C3ppA== X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 185.100.85.212 X-Mailman-Approved-At: Mon, 13 Mar 2017 16:41:50 -0400 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.org gmane.emacs.help:112560 Archived-At: 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 a= t, 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)=20 (let ((temporary-file (make-temp-file "0x0" nil (file-name-extension (or (buffer-file-name) (concat (buffer-name) ".txt"))=20 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=3D@%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: =E2=80=98curl -F'file=3D@foo.text' h= ttps://0x0.st=E2=80=99. Since we use curl, a file must be created, which is done with (make-temp-fi= le...). The third argument to (make-temp-file ...) is a suffix for the file, our us= age of it will now be explained. Uploading files without extensions to 0x0.st causes the service to append t= he .bin suffix which could cause people to be nervous about downloading the file. For this reason we generate an appropriate extension when possible, or =E2=80=98.txt=E2=80=99 when buffers aren't files. I'm explicit (verbose) wi= th the creation of =E2=80=98.txt=E2=80=99 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 sequentia= l). 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 funct= ion 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