Posts Tagged emacs
I’ve started learning typescript and one of the cool feature of compiler is –watch options that recompile the file when it change it’s content. In Emacs if file is changed the buffer still contain old file and if you try to edit that file you’re ask if you really want to ed it that file because it change content.
It found that it would be nice to have reload when file changes, so I wrote this little bit of Emacs Lisp:
;; http://emacs.stackexchange.com/questions/22647/reload-single-file-in-every-window (defun reload () "Revert buffer, then restore previous position." (interactive) (let ((pt (point))) (revert-buffer t t) (goto-char pt))) ;; two helpers to work with alists (defun add-to-alist (symbol name value) "Add new pair to ALIST" (let* ((list (symbol-value symbol)) (pair (assoc name list))) (if pair (setcdr pair value) (let ((pair (cons name value))) (if list (setf (symbol-value symbol) (cons pair list)) (setf (symbol-value symbol) (cons pair nil))))) (symbol-value symbol))) (defun remove-alist (symbol name) "Remove pair from ALIST" (let* ((list (symbol-value symbol)) (pair (assoc name list))) (if pair (let ((ptr list)) (if (eq (caar ptr) name) (setf (symbol-value symbol) '()) (while ptr (if (eq (caadr ptr) name) (progn (setcdr ptr (cddr ptr)) (setq ptr nil)) (setq ptr (cdr ptr)))) list))))) (require 'filenotify) (setq file-watchers '()) (defun watch () "Function watch file changes and reload the file from buffer" (interactive) (let* ((fname (buffer-file-name)) (pair (assoc fname file-watchers))) (if pair (progn (file-notify-rm-watch (cdr pair)) (remove-alist 'file-watchers descritor))) (let ((descriptor (file-notify-add-watch fname '(change) (lexical-let ((filename fname)) (lambda (event) (with-current-buffer (get-file-buffer filename) (if (not (buffer-modified-p)) (progn (message "update %s" filename) (reload))))))))) (add-to-alist 'file-watchers fname descriptor))))
Then to watch the file You just need to invoke the function using
M-x watch. The function will not reload the buffer if you have modifications. In that case you need to invoke reload function using
This will probably don’t work on windows but I’m not sure, here is documentation for
Right now I use Windows box for development at work and I use Emacs, but I needed to run RStudio to run the shiny application that I was working on. I though it would be nice to run shiny app from Emacs. On windows I was not able to run ansi-term but eshell was working.
Here is a function that run shiny app if the buffer you’re in is directory that contain server.R:
(defun shiny () "run shiny R application in new shell buffer if there is displayed buffer that have shell it will use that window" (interactive) (let* ((R (concat "shiny::runApp('" default-directory "')")) (name "*shiny*") (new-buffer (get-buffer-create name)) (script-proc-buffer (apply 'make-comint-in-buffer "script" new-buffer "R" nil `("-e" ,R))) (window (get-window-with-mode '(comint-mode eshell-mode))) (script-proc (get-buffer-process script-proc-buffer))) (if window (set-window-buffer window new-buffer) (switch-to-buffer-other-window new-buffer)) (set-process-sentinel script-proc 'special-mode-sentinel))) (defun search-window-buffer (fn) "return first window for witch given function return non nil value" (let ((buffers (buffer-list)) (value)) (dolist (buffer buffers value) (let ((window (get-buffer-window buffer))) (if (and window (not value) (funcall fn buffer window)) (setq value window))))))) (defun get-window-with-mode (modes) "return window with given major modes" (search-window-buffer (lambda (buff window) ((let ((mode (with-current-buffer buffer major-mode))) (member mode modes)))))
I’ve needed to use sentinels because If I run simple eshell command it was freezing Emacs and I need to press C-g to cancel the command, it din’t kill R but it was not very good solution.
I’ve also need to update my switch to other major mode buffer, because I was using Eshell that had eshell-mode and my new shiny buffer had comint-mode, I wanted those buffers it to be treated as the same. Above I’ve defined two functions for searching windows because I was using eshell and I wanted to have R from shiny run in the same window. If you’re using ansi-term (on windows it don’t work) you can check it’s major mode and add it to the list of modes in call to get-window-with-mode.
Below is the code for switching to other buffer with same mode (it’s should be common maybe), using
(setq same-mode-alist '((eshell-mode comint-mode) (comint-mode eshell-mode))) ;; SAME BUFFER SWITCH (defun buffer-same-mode (change-buffer-fun) "execute function for next buffer with the same major mode or mode on the list in same-mode-alist variable" (let ((current-mode major-mode) (next-mode nil)) (while (not (or (eq next-mode current-mode) (member next-mode (cdr (assoc current-mode same-mode-alist))))) (funcall change-buffer-fun) (setq next-mode major-mode)))) (defun previous-buffer-same-mode () "switch to previous buffer with same mode" (interactive) (buffer-same-mode #'previous-buffer)) (defun next-buffer-same-mode () "switch to next buffer with same mode" (interactive) (buffer-same-mode #'next-buffer)) (global-set-key [C-M-tab] 'previous-buffer-same-mode) (global-set-key [C-tab] 'next-buffer-same-mode)
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)))
This is by far best feature in Emacs that you can use interactive search inside Minibuffer. If you want to find a function that you don’t know exact name, I do this a lot – who will remember function names in Emacs, you can use:
C-h f C-s
And search for functions. How cool is that? Almost all functions that have completion inside minibuffer work with search like
- C-x b C-s – search for buffer to switch
- C-h <KEY> C-s – search for help
When I switch to Emacs 24, I found that M-x C-s don’t work. So you can’t search for interactive function to execute. But you can get it back. As describe by this Emacs Bug Ticket by putting this to you .emacs file.
(defun read-extended-command () "Read command name to invoke in `execute-extended-command'." (minibuffer-with-setup-hook (lambda () (set (make-local-variable 'minibuffer-default-add-function) (lambda () ;; Get a command name at point in the original buffer ;; to propose it after M-n. (let ((def (with-current-buffer (window-buffer (minibuffer-selected-window)) (and (commandp (function-called-at-point)) (format "%S" (function-called-at-point))))) (all (sort (minibuffer-default-add-completions) (lambda (a b) (string< a b))))) (if def (cons def (delete def all)) all))))) ;; Read a string, completing from and restricting to the set of ;; all defined commands. Don't provide any initial input. ;; Save the command read on the extended-command history list. (completing-read (concat (cond ((eq current-prefix-arg '-) "- ") ((and (consp current-prefix-arg) (eq (car current-prefix-arg) 4)) "C-u ") ((and (consp current-prefix-arg) (integerp (car current-prefix-arg))) (format "%d " (car current-prefix-arg))) ((integerp current-prefix-arg) (format "%d " current-prefix-arg))) ;; This isn't strictly correct if `execute-extended-command' ;; is bound to anything else (e.g. [menu]). ;; It could use (key-description (this-single-command-keys)), ;; but actually a prompt other than "M-x" would be confusing, ;; because "M-x" is a well-known prompt to read a command ;; and it serves as a shorthand for "Extended command: ". "M-x ") obarray 'commandp t nil 'extended-command-history)))
And tell me, why you will ever need Ido-mode
One note about that, when you use one interactive function inside another one you will need to call C-g twice to exit minibuffer
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)))