App-Chart

 view release on metacpan or  search on metacpan

emacs/chartprog.el  view on Meta::CPAN


SCALE is how many places to move the decimal point down.  For
example if SCALE is 2 then price 1.23 is returned as 123.  This
is good for working in cents when quotes are in dollars, etc."

  ;; FIXME: Umm, always flonums?
  ;; Prices are returned as flonums of the relevant currency, or if
  ;; the price is an integer then a fixnum.
  ;; If
  ;; prices have fractions of a cent they they might still be flonums.

  ;; In the distant past there was a `decimals' field which was how many
  ;; decimal places on prices.  Now always 0.
  ;;     decimals       integer
  ;; `decimals' is how many decimal places Chart uses for the
  ;; prices internally.  Internally prices are kept as an integer and
  ;; count of decimals.  Using this value for SCALE will ensure an
  ;; integer return.

  (unless (stringp symbol)
    (error "Not a Chart symbol (should be a string)"))
  (if chartprog-latest-record-calls
      (unless (member symbol (aref chartprog-latest-record-calls 0))
        (aset chartprog-latest-record-calls 0
              (cons symbol (aref chartprog-latest-record-calls 0)))))
  (unless field
    (setq field 'last))
  (let ((latest (gethash symbol chartprog-latest-cache)))
    (when (or (not latest)
              (plist-get latest 'dirty))
      ;; ses.el likes to run with inhibit-quit (to force atomic updates),
      ;; but accept-process-output doesn't like that.  with-local-quit here
      ;; will bail out, probably returning nil.
      (with-local-quit
        (setq latest (chartprog-exec-synchronous 'get-latest-record symbol))
        (puthash symbol latest chartprog-latest-cache)))

    (let ((value (plist-get latest field)))
      (when (and value
                 (memq field '(bid offer open high low last change)))
        (let ((factor (- (or scale 0) (plist-get latest 'decimals))))
          (if (/= 0 factor)
              (setq value (* value (expt (if (< factor 0) 10.0 10)
                                         factor))))))
      value)))


;;-----------------------------------------------------------------------------
;; ses.el additions

;;;###autoload
(defun chart-ses-refresh ()
  "Refresh Chart prices in a SES spreadsheet.
`ses-recalculate-all' is run first to find what `chart-latest'
prices are required.  Then quotes for those symbols are
downloaded and `ses-recalculate-all' run a second time to update
the spreadsheet with the new prices.

If the second `ses-recalculate-all' uses prices for further
symbols, perhaps due to tricky conditionals in spreadsheet
formulas, then another downloaded and `ses-recalculate-all' is
done.  This is repeated until all prices used in the recalculate
have been downloaded."

  (interactive)
  (let (fetched)
    (while (let ((record
                  (let ((chartprog-latest-record-calls (vector nil)))
                    (ses-recalculate-all)
                    (aref chartprog-latest-record-calls 0)))
                 want)
             (chartprog-debug-message "record calls %S" record)

             ;; want symbols not already fetched
             (dolist (symbol record)
               (unless (member symbol fetched)
                 (push symbol want)))

             (when want
               (chart-ses-refresh-download want)
               (setq fetched (nconc want fetched))
               t)))))

(defun chart-ses-refresh-download (symbol-list)
  "An internal part of chartprog.el.
Download the symbols (strings) in SYMBOL-LIST for a SES
spreadsheet update."
  (chartprog-with-temp-message (format "Downloading %d quote(s) ..."
                                       (length symbol-list))
    (chartprog-exec 'request-explicit symbol-list)
    (chartprog-latest-cache-remove symbol-list)

    (while symbol-list
      (accept-process-output)

      (dolist (symbol symbol-list)
        (unless (eq (chart-latest symbol 'face) 'chartprog-in-progress)
          (setq symbol-list (remove symbol symbol-list))
          (chartprog-debug-message "ses-refresh got " symbol))))))

;;-----------------------------------------------------------------------------

;; LocalWords: Customizations eg ie watchlist symlist symlists initializing
;; LocalWords: hscrolled hscrolling UTF minibuffer tooltip cl synchronize
;; LocalWords: col init chartprog

(provide 'chartprog)

;;; chartprog.el ends here



( run in 0.445 second using v1.01-cache-2.11-cpan-39bf76dae61 )