From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: John Mastro Newsgroups: gmane.emacs.help Subject: Re: How can I write this function better? Date: Tue, 14 Mar 2017 10:48:27 -0700 Message-ID: References: <87zigprm7q.fsf@cocaine.ninja> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: blaine.gmane.org 1489513766 28929 195.159.176.226 (14 Mar 2017 17:49:26 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Tue, 14 Mar 2017 17:49:26 +0000 (UTC) Cc: user To: "help-gnu-emacs@gnu.org" Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Tue Mar 14 18:49:20 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 1cnqZR-0006LR-4t for geh-help-gnu-emacs@m.gmane.org; Tue, 14 Mar 2017 18:49:17 +0100 Original-Received: from localhost ([::1]:32773 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cnqZT-0002Mu-TW for geh-help-gnu-emacs@m.gmane.org; Tue, 14 Mar 2017 13:49:19 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:38532) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cnqZ0-0002Mm-Vw for help-gnu-emacs@gnu.org; Tue, 14 Mar 2017 13:48:52 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cnqYz-0002iP-Ix for help-gnu-emacs@gnu.org; Tue, 14 Mar 2017 13:48:51 -0400 Original-Received: from mail-qk0-x22a.google.com ([2607:f8b0:400d:c09::22a]:35996) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1cnqYz-0002iA-Ep for help-gnu-emacs@gnu.org; Tue, 14 Mar 2017 13:48:49 -0400 Original-Received: by mail-qk0-x22a.google.com with SMTP id 1so253419496qkl.3 for ; Tue, 14 Mar 2017 10:48:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=m/M/P4SBwlhgYL3Y/eKL67pI6KA8Qi4xU9yT+b90F4U=; b=nswq3m3ur+YoNGk/Rz3dFpL+UhCkEm1f8G8qTpMFosoF4k3SgyFe6FQEIskuEiPf/q 64QBMtfJY5hDyuessu3YgC4czSGueKr7HPV5QDTySP3A7Zgr6Dk5n3d5gEhDmvaLKACy DQgf7x+feEcvc1LK9JIQTYni0IGxz2U9IT9vPzZgiuxyBTXL0d7S7Ogkic7vhz6ZkQMr dejlOXKUzAry6LNym7H3w0Ok3SBEUe5hgKIzqwlI71DDNodq3/4mkR5Drp46opzB3zPy RXjRupYJxz+9/xza6hVEQPtxAnJMWzOPRel+C7puz93nzAvtENJWjQSoEaslQb2ItJLY vFTw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=m/M/P4SBwlhgYL3Y/eKL67pI6KA8Qi4xU9yT+b90F4U=; b=W6xGCt4zvd/iKNwc8hfOkZeLfH01/LEUUNw93bdj4E+Nguw2So1cvPTEavdHaQWl6K MlovRIwCL4AVe/tet6zJSXR7kqVbJljvcyR2pLK9mbBqxD4MayHZxfz3GJBY2NcizkDy XsV0XRD2vXVY1T8YR+Zdpqc2XLYCZW5hxFwX2hqfmO+mi//of5eoUJ8n1nyqy4xo2iBP LPlxn+tH3NXAw2iDXtCzuBKWyfjZIZ4Fz68LzMZCvIWWkMMRgCKph2G5pDyhdwz2d7kW fOew5a1RYt82/WuGO978oj5og+UNleowk7lDhB/9LjX3YxfRI3vjLqLZrFwn7eoPdilp ZHBQ== X-Gm-Message-State: AMke39nfJlo1uJHDnziv5oYg6DyUTnfuz849k1Ae+F401ww1Are8E4SHTHaqQAaOMh+xwge0Pi4sX2Gzo+JFag== X-Received: by 10.55.123.5 with SMTP id w5mr42177581qkc.76.1489513728206; Tue, 14 Mar 2017 10:48:48 -0700 (PDT) Original-Received: by 10.237.49.162 with HTTP; Tue, 14 Mar 2017 10:48:27 -0700 (PDT) In-Reply-To: <87zigprm7q.fsf@cocaine.ninja> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400d:c09::22a 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:112570 Archived-At: user 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