Skip to content

Instantly share code, notes, and snippets.

@josephwilk
Last active February 21, 2018 11:36
Show Gist options
  • Save josephwilk/a882017e49dd64b6d84f8078eebdf2a2 to your computer and use it in GitHub Desktop.
Save josephwilk/a882017e49dd64b6d84f8078eebdf2a2 to your computer and use it in GitHub Desktop.
Render readonly bars for floats.
(defconst MAX-LENGTH 100)
(defun vst-update-all ()
(interactive)
(save-excursion
(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))
(next-line)
(beginning-of-line)
(let ((start-pnt (point))
(pad (if (>= empty 0)
(make-string empty ?╌)
"")))
(kill-line)
(insert (concat "#╟"
(make-string (+ 1 full) ?▓)
pad
"╢"))
(put-text-property start-pnt (point) 'read-only t)))))))
(defun vst-update (new-number old-number)
(interactive)
(if (not (= new-number old-number))
(save-excursion
(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 ()
(interactive)
(change-number-at-point (lambda (number)
(let ((new-number (min 1.00 (+ number 0.01))))
(vst-update new-number number)
new-number))))
(defun dec-float-at-point ()
(interactive)
(change-number-at-point (lambda (number)
(let ((new-number (max 0.00 (- number 0.01))))
(vst-update new-number number)
new-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