From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 9363 invoked from network); 29 Jan 2003 16:03:56 -0000 Received: from sunsite.dk (130.225.247.90) by ns1.primenet.com.au with SMTP; 29 Jan 2003 16:03:56 -0000 Received: (qmail 5008 invoked by alias); 29 Jan 2003 16:03:27 -0000 Mailing-List: contact zsh-users-help@sunsite.dk; run by ezmlm Precedence: bulk X-No-Archive: yes X-Seq: 5841 Received: (qmail 5001 invoked from network); 29 Jan 2003 16:03:26 -0000 Received: from localhost (HELO sunsite.dk) (127.0.0.1) by localhost with SMTP; 29 Jan 2003 16:03:26 -0000 X-MessageWall-Score: 0 (sunsite.dk) Received: from [143.90.131.180] by sunsite.dk (MessageWall 1.0.8) with SMTP; 29 Jan 2003 16:3:25 -0000 Received: from rabi ([211.131.155.69]) by t-mta2.odn.ne.jp with SMTP id <20030129160317047.BHPS.24063.t-mta2.odn.ne.jp@mta2.odn.ne.jp> for ; Thu, 30 Jan 2003 01:03:17 +0900 From: "Clifford Caoile" To: Subject: a calculator for zsh-4.x Date: Thu, 30 Jan 2003 01:03:02 +0900 Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset="iso-2022-jp" Content-Transfer-Encoding: 7bit X-Priority: 3 (Normal) X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook IMO, Build 9.0.2416 (9.0.2911.0) Importance: Normal X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000 Dear zsh-users: In my excitement over zsh-4.x, I bring to you some sample calculator functions to enhance your zsh experience. However, I haven't searched this mailing list for similar subjects at all, and I am ignorant of this mailing list past posts. Perhaps I'm duplicating suggestions here, but I hope this is helpful. I don't know about you, but I reach for zsh when I want to calculate addition in hexadecimal (like whenever I have read a linux ksymoops message). With the following .zshrc snippet, my shell becomes a convenient-to-type calculator: Code Snippet: # -----------------------------------------------------------------begin # calculator - paste me into your .zshrc # ---------------------------------------------------------------------- # Here are some quick calculators that output in integer # hexadecimal, decimal, and binary. zcalc () { print $(( ans = ${1:-ans} )) } zcalch () { print $(( [#16] ans = ${1:-ans} )) } zcalcd () { print $(( [#10] ans = ${1:-ans} )) } zcalco () { print $(( [#8] ans = ${1:-ans} )) } zcalcb () { print $(( [#2] ans = ${1:-ans} )) } # A key binding that will allow you to quickly get into zcalc bindkey -s '\C-xd' "zcalc \'" # this last one lets you calculate the ascii value of a single character zcalcasc () { print $(( [#16] ans = ##${1:-ans} )) } # -------------------------------------------------------------------end Usage example: %> zcalc '23 / 3' # integer math 7 %> zcalc '23.0 / 3' # forced floating point math 7.666666666666667 %> zcalc '23 % 3' # programmers love to mod 2 %> zcalc 'ans + 2' # your last answer is still there 4 %> zcalc '0xCD & 0x23' # uh, why are you bit masking? 1 %> zcalc '0xCD | 0x23' # or you like "or"? 239 %> zcalcb ; zcalco ; zcalcd ; zcalch # I want it in a readable format please. 2#11101111 8#357 239 16#EF %> zcalch '2#11101111 & 0211' # mix the input bases 16#C3 Recommended convenience feature: Try the bindkey Control-X d. It's a quick jump into zsh-based calculator. You only need to worry about adding the last quotation mark. (Ugh) Call for advice: Actually, I'm most interested in enhancing the bindkey definition. Placing just one quotation mark on the command line seems to be sloppy. I wish I was able to set the position of the cursor at any arbitrary point of the inserted string, just like in emacs. Anybody have any suggestions? I guess this is just the whole reason for posting... Instead of putting this in my .zshrc file, is it better to make it a zsh module? HTH, HAND ,-~-. < ^ ; ~, Clifford Escobar CAOILE (aka "Piyo-kun") (_ _, J~~> _.___...:.__..__.: __.:_. .:_.__::_..:._::._... _____ p(^_^)q