Posts Tagged elisp
I recently switched from jasmine/istanbul to jest framework for my project jQuery Terminal and I’ve got coverage drop from 80-81% to about 69% with some update of the code and commented out code that I was working on.
So I thought it would be cool to highlight the lines that where covered and not covered by tests and since I use emacs as my editor I thought that I write a function that will do that.
Here it is:
(defun root-git-repo () (interactive) (replace-regexp-in-string "\n" "" (shell-command-to-string "git rev-parse --show-toplevel"))) (defun line-pos-at-line (line) (interactive) (save-excursion (goto-line line) (line-beginning-position))) (defun coverage-mark-buffer () (interactive) (let* ((dir (root-git-repo)) (json-object-type 'hash-table) (json-array-type 'list) (json-key-type 'string) (json (json-read-file (concat dir "/coverage/coverage-final.json"))) (filename (buffer-file-name (current-buffer))) (coverage (gethash filename json)) (statments (gethash "statementMap" coverage))) (save-excursion (maphash (lambda (key value) (let* ((statment (gethash key statments)) (start (gethash "start" statment)) (end (gethash "end" statment)) (start-line-pos (line-pos-at-line (gethash "line" start))) (start-pos (+ start-line-pos (gethash "column" start))) (end-line-pos (line-pos-at-line (gethash "line" start))) (end-pos (+ end-line-pos (gethash "column" end))) (color (if (= value 0) "dark red" "dark green")) (face `((t (:background ,color))))) (hlt-highlight-region start-pos end-pos face))) (gethash "s" coverage)))))
The function is using hlt-highlight-region from highlight.el by Drew Adams.
The function don’t check if coverage file exists. It assume that you’re opening from from git repo and that coverage file is in coverage directory (default for jest) in git root.
If you want to clear the buffer you can use this function:
(defun coverage-clear-buffer () (interactive) (save-excursion (end-of-buffer) (hlt-unhighlight-region 0 (point))))
Clear is much faster.
If you’re iterested in code coverage in Emacs you can take a look at jest-coverage minor mode that I’ve created based on this solution.
In GNU Emacs if you execute shell command for instance using exec function the whole Editor freeze until shell command is finished. So I’ve written asynchronous version of exec:
(defun async-exec-command (command &rest success) (interactive "MExecute command: ") (let* ((buffer-name (generate-new-buffer-name "**shell**")) (buffer (get-buffer-create buffer-name)) (process (apply #'start-process (append (list buffer-name buffer) (split-string command " "))))) (lexical-let ((buffer buffer) (success (car success)) (command command)) (set-process-sentinel process (if success (lambda (process str) (save-excursion (set-buffer buffer) (let ((content (buffer-string))) (kill-buffer buffer) (if (or (string= str "finished\n") (string-match "exited abnormally" str)) (funcall success content) (message content))))) (lambda (proces str) (kill-buffer buffer))))) (concat "execute: " command)))
I needed something to speed up my work when I have few windows, I was looking at switch-window.el, but it kind of silly that the content of the window disappear. So I’ve written this simple 2 functions which act similar to my other sollution Faster buffer bookmarking in Emacs. But you mark window to which you want to jump.
It set Keyboard C-c C-0 for mark and C-C C-<1-9> to jump.
(setq window-alist '()) (defun mark-window (number) "Give current window a number to select it later using `switch-to-number-window'" (interactive "nNumber Your Window: ") (let ((pair (assoc number window-alist)) (window (get-buffer-window))) (if pair (setf (cdr pair) window) (add-to-list 'window-alist (cons number window))))) (defun switch-to-number-window (number) "Jump to the window marked with a `mark-window' function" (interactive "nNumber Your Window: ") (let ((pair (assoc number window-alist))) (if pair (select-window (cdr pair)) (message "Invalid Number")))) ;; create 9 keyboard shortcuts using closure (forced using lexical-let) (dolist (i (map #'1+ (range 9))) (global-set-key (read-kbd-macro (concat "C-c C-" (number-to-string i))) (lexical-let ((i i)) (lambda () (interactive) (switch-to-number-window i))))) (global-set-key (kbd "C-c C-0") 'mark-window)
It use range helper function (same as in python)
(defun range (n &optional list) "function return list of numbers from 1 to n" (if (eq n 0) list (let ((n (- n 1))) (range n (cons n list)))))
EDIT: I just found windmove commands which are much better.
I need to monitor few IRC channels and I use Emacs so I write simple elisp function that I append to
erc-insert-pre-hook and it notify me when there is some activity on those channels. I made this mainly because I what to know if someone visit
#openclipart channel (because people where visiting ask question and leave after few minutes, there is no much activity on this channel)
(setq inactivity-buffer-alist '(("#openclipart" (inactivity . 900)) ("#hackerrank" (inactivity . 900)) ("#aiki" (inactivity . 900)))) (defun channel-activity (string &rest ignore) "notification when there is activity on a erc channel after inactivity" (let* ((buffer (buffer-name)) (buffer-alist-pair (assoc buffer inactivity-buffer-alist)) (buffer-alist (cdr buffer-alist-pair)) (current-time (current-time))) (if (not (null buffer-alist)) (let ((last-time-pair (assoc 'last-time buffer-alist)) (inactivity (cdr (assoc 'inactivity buffer-alist)))) (if (not (and (string-match "^\\*\\*\\*" string) (string-match "[freenode-info]" string))) (progn (if (or (null last-time-pair) (> (float-time (time-subtract current-time (cdr last-time-pair))) inactivity)) (async-exec-command "mpg123 -q /home/kuba/Pobrane/beep-6.mp3")) (if (null last-time-pair) (setf (cdr buffer-alist-pair) (append buffer-alist (list (cons 'last-time current-time)))) (setf (cdr last-time-pair) current-time)))))))) (add-hook 'erc-insert-pre-hook 'channel-activity)
You can add your channels to
inactivity-buffer-alist along with time of inactivity (in miliseconds)
The function I use for notification is play sound using
(async-exec-command "mpg123 -q /home/kuba/Pobrane/beep-6.mp3") – normal shell command was stoping execution of Emacs for few seconds
The code for this function is as follow
(defun async-exec-command (command &rest success) (interactive) (let* ((buffer-name (generate-new-buffer-name "**shell**")) (buffer (get-buffer-create buffer-name)) (process (apply #'start-process (append (list buffer-name buffer) (split-string command " "))))) (lexical-let ((buffer buffer) (success (car success)) (command command)) (set-process-sentinel process (if success (lambda (process str) (if (string= str "finished\n") (save-excursion (set-buffer buffer) (let ((content (buffer-string))) (kill-buffer buffer) (funcall success content))))) (lambda (proces str) (kill-buffer buffer))))) (concat "execute: " command)))