Skip to content

Instantly share code, notes, and snippets.

@josephwilk josephwilk/vst.el
Last active Feb 21, 2018

What would you like to do?
Render readonly bars for floats.
(defconst MAX-LENGTH 100)
(defun vst-update-all ()
(goto-char (point-min))
(let ((inhibit-read-only t))
(remove-text-properties (point) (point-max) '(read-only t)))
(while (re-search-forward "sop2_cc .+:\s*\\([0-9]*.[0-9]+\\)\n" nil t)
(let ((full (round (* MAX-LENGTH (string-to-number (match-string 1))))))
(let ((empty (- MAX-LENGTH full)))
(goto-char (match-beginning 0))
(let ((start-pnt (point))
(pad (if (>= empty 0)
(make-string empty ?╌)
(insert (concat "#╟"
(make-string (+ 1 full) ?▓)
(put-text-property start-pnt (point) 'read-only t)))))))
(defun vst-update (new-number old-number)
(if (not (= new-number old-number))
(let ((full (round (* MAX-LENGTH number))))
(let ((empty (- MAX-LENGTH full)))
(forward-line 1)
(forward-char (+ full 2))
(let ((inhibit-read-only t)
(start-pnt (point)))
(delete-char 1)
(if (> new-number old-number)
(insert "")
(insert "")
(put-text-property start-pnt (point) 'read-only t))))))))
(require 'thingatpt)
(put 'float 'end-op (lambda () (re-search-forward "[0-9-]*\.[0-9-]*" nil t)))
(put 'float 'beginning-op (lambda () (if (re-search-backward "[^0-9-\.]" nil t) (forward-char))))
(defun change-number-at-point (func)
(let* ((bounds (bounds-of-thing-at-point 'float))
(number (buffer-substring (car bounds) (cdr bounds)))
(point (point)))
(goto-char (car bounds))
(delete-char (length number))
(insert (format "%.2f" (funcall func (string-to-number number))))
(goto-char point)))
(defun inc-float-at-point ()
(change-number-at-point (lambda (number)
(let ((new-number (min 1.00 (+ number 0.01))))
(vst-update new-number number)
(defun dec-float-at-point ()
(change-number-at-point (lambda (number)
(let ((new-number (max 0.00 (- number 0.01))))
(vst-update new-number number)
(global-set-key [(meta up)] 'inc-float-at-point)
(global-set-key [(meta down)] 'dec-float-at-point)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.