emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] master 354b635 08/10: Add tiny-helper command


From: Oleh Krehel
Subject: [elpa] master 354b635 08/10: Add tiny-helper command
Date: Wed, 30 Aug 2017 13:46:04 -0400 (EDT)

branch: master
commit 354b6354c84abbae9800565092d79bd7aa5956b0
Author: Kaushal Modi <address@hidden>
Commit: Kaushal Modi <address@hidden>

    Add tiny-helper command
---
 tiny.el | 112 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 112 insertions(+)

diff --git a/tiny.el b/tiny.el
index 6aedbe0..7818a7f 100644
--- a/tiny.el
+++ b/tiny.el
@@ -87,6 +87,20 @@
 ;;     are used in the appropriate place.
 ;;   - otherwise, it's just the result of the expression repeated as
 ;;     many times as necessary.
+;;
+;; Alternatively, if user does not want to type in the "tiny
+;; expressions", they can call the `tiny-helper' command that helps
+;; construct the "tiny expression", and then expands that.
+;;
+;; For example, the below two are equivalent:
+;;
+;;  - Type "m2_9+1*x2"
+;;  - M-x tiny-expand
+;;
+;; OR
+;;
+;;  - M-x tiny-helper
+;;  - 9 RET 2 RET _ RET +1*x2 RET RET (user entry in the interactive prompts)
 
 ;;; Code:
 
@@ -423,5 +437,103 @@ Optional SHIFT argument is the integer amount of days to 
shift."
       (setq time (time-add time (days-to-time shift))))
     (format-time-string formatter time)))
 
+;;;###autoload
+(defun tiny-helper (&optional end-val begin-val sep op fmt)
+  "Helper function for `tiny-expand'.
+
+The arguments to this function construct a “tiny expression”
+\"mBSEO|F\" where
+  E is the end value (END-VAL)     - defaults to 0 internally if nil or \"\",
+                                      or 9 if BEGIN-VAL is nil or \"\" too.
+  B is the begin value (BEGIN-VAL) - defaults to 0 internally if nil or \"\".
+  S is the separator (SEP)         - defaults to \" \" if nil or \"\".
+  O is the elisp operation (OP)    - defaults to \"\" if nil.
+  F is the format (FMT)            - defaults to \"\" if nil.
+
+If `tiny' expansion is possible at point, do it.
+Otherwise activate the helper to generate a valid “tiny
+expression” and expand that.
+
+Usage: Call TINY-HELPER, ↵↵↵↵↵            -> 0 1 2 3 4 5 6 7 8 9
+       Call TINY-HELPER, 9↵2↵_↵+1*x2↵↵    -> 5_7_9_11_13_15_17_19
+       Call TINY-HELPER, 15↵1↵↵-30*2x↵%x↵ -> 1c 1a 18 16 14 12 10 e c a 8 6 4 
2 0"
+  (interactive
+   (when (and (null (barf-if-buffer-read-only)) ;Use the helper only if tiny 
expansion is not
+              (null (tiny-mapconcat))) ;possible at point and if the buffer is 
editable.
+     (let ((prompt (propertize "tiny-helper: " 'face 
'font-lock-function-name-face)))
+       (list (read-string (concat prompt
+                                  "END value "
+                                  "[Hit RET for default=0; "
+                                  "Auto-set to 9 if both begin and end values 
are 0]: "))
+             (read-string (concat prompt
+                                  "BEGIN value "
+                                  "[Hit RET for default=0; "
+                                  "Has to be *smaller* than the end value]: "))
+             (read-string (concat prompt
+                                  "Separator "
+                                  "[Hit RET for default=Space; "
+                                  "eg: \\n; No math operators like - or = 
allowed]: "))
+             (read-string (concat prompt
+                                  "Lisp Operation "
+                                  "[Hit RET for default=\"\" (no Lisp 
operation); "
+                                  "Parentheses are optional; eg: *xx | (+ x 
?A) | *2+3x]: "))
+             (read-string (concat prompt
+                                  "Format "
+                                  "[Hit RET for default=\"\" (%0d); "
+                                  "eg: %x | 0x%x | %c | %s | %(+ x x) | "
+                                  "%014.2f | %03d; Parentheses required here 
for sexps]: "))))))
+  (barf-if-buffer-read-only)  ;Proceed only if the buffer is editable.
+  ;; Use the helper to derive a "tiny expression" if tiny expansion is
+  ;; not possible at point.
+  (when (null (tiny-mapconcat))
+    (let* ((tiny-key-binding (or (substitute-command-keys "\\[tiny-helper]")
+                                 (substitute-command-keys "\\[tiny-expand]")))
+           (end-val (if (null end-val) "" end-val)) ;Initialize to empty 
strings for non-interactive use.
+           (begin-val (if (null begin-val) "" begin-val))
+           (sep (if (null sep) "" sep))
+           (op (if (null op) "" op))
+           (fmt (if (null fmt) "" fmt))
+           (end-val-num (string-to-number end-val)) ;Note that 
(string-to-number "") -> 0
+           (begin-val-num (string-to-number begin-val))
+           tiny-expr)
+      ;; BEGIN-VAL and END-VAL sanity check.
+      (cond
+       ((= end-val-num begin-val-num)
+        (if (zerop end-val-num)
+            ;; If both are zero, set the end value to 9 (arbitrarily chosen).
+            (setq end-val "9")
+          (user-error (format "Begin value (%s) and End value (%s) cannot be 
the same"
+                              begin-val end-val))))
+       ((< end-val-num begin-val-num)
+        (user-error (format "End value (%s) has to be greater than the begin 
value (%s)"
+                            begin-val end-val))))
+      ;; SEP cannot be an empty string if BEGIN-VAL is a non-empty string.
+      ;; It is OK to not specify BEGIN-VAL if it is 0.
+      (when (and (not (string= begin-val ""))
+                 (string= sep ""))
+        (setq sep " "))
+      ;; When non-empty, prefix FMT with the | char for reading clarity.
+      (when (not (string= fmt ""))
+        (setq fmt (concat "|" fmt)))
+      (setq tiny-expr (concat "m" begin-val sep end-val op fmt))
+      (message (format "This %s expansion can also be done by typing %s and 
then %s"
+                       (propertize "tiny"
+                                   'face 'font-lock-function-name-face)
+                       (propertize tiny-expr
+                                   'face 'font-lock-keyword-face)
+                       (if (stringp tiny-key-binding)
+                           (propertize tiny-key-binding
+                                       'face 'font-lock-keyword-face)
+                         (concat
+                          (propertize "M-x tiny-helper"
+                                      'face 'font-lock-keyword-face)
+                          " or "
+                          (propertize "M-x tiny-expand"
+                                      'face 'font-lock-keyword-face)))))
+      (insert tiny-expr)
+      (undo-boundary)))
+  (tiny-expand))
+
+
 (provide 'tiny)
 ;;; tiny.el ends here



reply via email to

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