Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Elisp Snippets
Table of Contents
1. Elisp Snippets
1.1. Overview
1.2. Strings
1.2.1. Concatenate strings
1.2.2. Join Strings by Separator
1.2.3. Split String
1.2.4. Split String with quotes
1.2.5. Replace Strings
1.2.6. Regex
1.4. S-expressions
1.4.1. Parse s-expressions
1.4.2. Evaluate s-expressions
1.6. Elisp
1.6.1. Load an Elisp file
1.6.2. Load all elisp files of a directory
1.6.3. Add directory to load path
1.6.4. Switch and Create the Scratch Buffer
1.6.5. Eval String in Clipboard
1.6.6. Extract Function Documentation
1.6.7. Describe function
1.11. Compilation
1.11.1. Enhanced Compilation Command
1.11.2. Compile a single c++ file related to current buffer and run it.
1.11.3. Compile and run a single c++ file at point in dired mode
1.12. File
1 of 45 15/5/19, 1:07 pm
Elisp Snippets https://caiorss.github.io/Emacs-Elisp-Programm...
1.14. Directory
1.14.1. Open directory
1.14.2. Create directory
1.14.3. List directory
1.20. Packages
1.20.1. Test if package is installed
1.20.2. Install a package if it is not installed
1.20.3. Install a single-file package from URL
1.22. Persistence
1.22.1. Save and Reload Current Session
1.22.2. Save and Restore Current Window Configuration
2 of 45 15/5/19, 1:07 pm
Elisp Snippets https://caiorss.github.io/Emacs-Elisp-Programm...
Top
1 Elisp Snippets
1.1 Overview
This section provides Elisp snippets and examples about Emacs API to help developers find what they need quickly.
1.2 Strings
> (apply #'concat '("hello" " world " " elisp "))
"hello world elisp "
("/bin/app" "-x" "-y" "-z" "/home/user/some name with space/etc" "-k" "cmd")
1.2.6 Regex
3 of 45 15/5/19, 1:07 pm
Elisp Snippets https://caiorss.github.io/Emacs-Elisp-Programm...
From: jidaikobo-shibata/join-multi-lines-to-one.el
(defun join-multi-lines-to-one ()
"Join multi lines."
(interactive)
(let ((beg (region-beginning))
(end (region-end))
strings)
(goto-char beg)
(back-to-indentation)
(setq beg (point))
(goto-char end)
(goto-char (- (point) 1))
(end-of-line)
(setq end (point))
(setq strings (buffer-substring-no-properties beg end))
(setq strings (replace-regexp-in-string "\n\\|^>+ *\\|^[\t ]+" " " strings))
(setq strings (replace-regexp-in-string " +" " " strings))
(delete-region beg end)
(insert strings)
(goto-char beg)))
1.4 S-expressions
(defun clipboard/get ()
"Return the content of clipboard as string"
(interactive)
(with-temp-buffer
(clipboard-yank)
(buffer-substring-no-properties (point-min) (point-max))))
4 of 45 15/5/19, 1:07 pm
Elisp Snippets https://caiorss.github.io/Emacs-Elisp-Programm...
(defun buffer/copy-file-name ()
(interactive)
(clipboard/set (buffer-file-name)))
(defun buffer/copy-path ()
(interactive)
(clipboard/set (file-name-directory (buffer-file-name)))
(message "Copied file path to clipboard"))
> (clipboard/get)
"/home/arch/projects/emacs/"
(defun buffer/copy-content ()
"Copy buffer content to clibpoard
Usage: M-x buffer/copy-content"
(interactive)
(clipboard/set
(buffer-substring-no-properties
(point-min)
(point-max)
)))
1.6 Elisp
(load-file "~/.emacs.d/tools.el")
(load-file "~/.emacs.d/tools.elc")
"
(mapc #'load (directory-files path t "\\.el$")))
It adds a directory containing Emacs packages (<package name>.el) to the load path. The user can load packages by adding the code (require
'<package>) to the file init.el.
This function switches and crates the scratch buffer if it doesn't exist or was deleted. Usage: M-x scratch.
(defun scratch ()
"
Switches to scratch buffer and creates
it if it doesn't exist.
5 of 45 15/5/19, 1:07 pm
Elisp Snippets https://caiorss.github.io/Emacs-Elisp-Programm...
Suggestion:
Add (defalias 's #'scratch) to the init file.
You can switch to the scratch buffer with > M-x s
"
(interactive)
(switch-to-buffer buf)
(lisp-interaction-mode)
))
It only works on Linux and requires Xclip to be installed, but with a few changes can be tweaked to work in another Os.
;;
;; Copy the following line in this comment block
;;
;; (message "Loading my copypaste file...")
ELISP> (eval-xclip)
"Loading my copypaste file..."
ELISP>
ELISP>
ELISP> (defun sample-function (a b c)
"Function Docstring"
(+ a (* 5 b) (* 3 c)))
;; Extract Documentation
;;
ELISP> (documentation 'sample-function)
"Function Docstring"
;; Extract Code
;;
ELISP> (symbol-function 'sample-function)
(lambda
(a b c)
"Function Docstring"
(+ a
(* 5 b)
(* 3 c)))
;; Extract Arguments
ELISP> (help-function-arglist 'sample-function)
(a b c)
Final Code
(fun2org 'sample-function)
"** sample-function (a b c)\nFunction Docstring\n\n#+BEGIN_SRC emacs-lisp\n(lambda (a b c) \"Function Docstring\" (+ a (* 5 b) (* 3 c)))\n#+END_SRC\n"
ELISP>
ELISP> (defun fun2org (function-symbol)
(let ((args (help-function-arglist function-symbol))
(doc (documentation function-symbol))
(code (symbol-function function-symbol)))
(print (format "** %s %s
%s
#+BEGIN_SRC emacs-lisp
%S
"** sample-function (a b c)
Function Docstring
6 of 45 15/5/19, 1:07 pm
Elisp Snippets https://caiorss.github.io/Emacs-Elisp-Programm...
(describe-function 'find-file-noselect)
(require 'cl)
(require 'cl)
ELISP> (cl-reduce (lambda (acc x) (and acc x)) '(t nil t t t f) :initial-value t)
nil
ELISP> (cl-reduce (lambda (acc x) (and acc x)) '(t t t t t f) :initial-value t)
f
ELISP> (cl-reduce (lambda (acc x) (and acc x)) '(t t t t t t) :initial-value t)
t
(save-buffer)
(kill-buffer "Elisp_Snippets.org")
Returns the content of a buffer referencend by its name or the buffer object.
7 of 45 15/5/19, 1:07 pm
Elisp Snippets https://caiorss.github.io/Emacs-Elisp-Programm...
(defun get-selection ()
"Get the text selected in current buffer as string"
(interactive)
(buffer-substring-no-properties (region-beginning) (region-end)))
This function creates a Python scratch buffer like elisp *scratch* which allows the user to play with Python without creating any file. It is
possible to run
It can be easily changed to work with other languages like ruby by replacing python-mode to ruby-mode.
(defun python/scratch ()
(interactive)
(let (
;; Creates a new buffer object.
(buf (get-buffer-create "*python-scratch*"))
)
;; Executes functions that would change the current buffer at
;; buffer buf
(with-current-buffer buf
;;; Set the new buffer to scratch mode
(python-mode)
;;; Pop to scratch buffer
(pop-to-buffer buf)
)))
(defun sh/scratch ()
(interactive)
(let (
;; Creates a new buffer object.
(buf (get-buffer-create "*sh-scratch*"))
)
;; Executes functions that would change the current buffer at
;; buffer buf
(with-current-buffer buf
;;; Set the new buffer to scratch mode
(sh-mode)
;;; Pop to scratch buffer
(pop-to-buffer buf)
)))
Output:
((cursor-type . bar)
(buffer-display-time 22711 20278 987330 737000)
(buffer-display-count . 4)
(buffer-invisibility-spec
(org-babel-hide-hash . t)
(org-babel-hide-hash . t)
(org-babel-hide-hash . t)
(org-babel-hide-hash . t)
(org-babel-hide-result . t)
(org-hide-block . t)
(org-cwidth) (org-link) (outline . t) t)
(buffer-file-truename . "~/Documents/projects/emacs/Elisp_Snippets.org") (point-before-scroll)
(buffer-auto-save-file-format . t)
(buffer-file-format) (buffer-file-coding-system . utf-8-unix)
(enable-multibyte-characters . t)
(mark-active)
(overwrite-mode) ...)
(buffer-local-variables <buffer-name>)
8 of 45 15/5/19, 1:07 pm
Elisp Snippets https://caiorss.github.io/Emacs-Elisp-Programm...
Append current buffer content to file. If the file doesn't exist, it will be created. Otherwise the buffer content will be appended to the end of the
file.
(with-current-buffer "my-buffer-name"
(append-to-file (point-min) (point-max) "/tmp/afile.txt"))
(revert-buffer t t t)
1. Prompt functions
Function Description
2. Read string
> (insert (concat "\n" (read-string " prompt > " ))) ;; M-x eval-last-sexp
user enter a message in the minibuffer
Reads a file name from the user and gives auto completion. Enter tab to autocomplete the file name.
9 of 45 15/5/19, 1:07 pm
Elisp Snippets https://caiorss.github.io/Emacs-Elisp-Programm...
Opens a prompt that asks for the path in the minibuffer with completion and inserts the path at the current point.
(defun insert-path ()
"
Opens a prompt that asks for the path
in the minibuffer with completion
and inserts the path at the current
point.
"
(interactive)
(insert (read-file-name "file > ")))
1.9.2 Output
1. Message
2. Print
3. Princ
4. Message box
Display a message, in a dialog box if possible. If a dialog box is not available, use the echo area.
10 of 45 15/5/19, 1:07 pm
Elisp Snippets https://caiorss.github.io/Emacs-Elisp-Programm...
5. Tooltip
Show a tooltip
shell-command
async-shell-command
start-process
call-process
shell-command-to-string
shell-command-on-region
getenv
setenv
1.10.2 Add application path to the PATH variable - Solving command not found
1. Overview
In order to Emacs find where applications such as ruby shell, scala compiler and tools are installed it is necessary to the PATH environment variable contain
the path to the directories which contains those applications.
Copy this command below to the scratch buffer and enter M-x eval-print-last-last-sexp.
(getenv "PATH")
"/home/archbox/opt/scala/bin:/usr/bin:/bin: ..."
11 of 45 15/5/19, 1:07 pm
Elisp Snippets https://caiorss.github.io/Emacs-Elisp-Programm...
(defun show-path-variable ()
(interactive)
;; Create a new buffer to display results to user
(switch-to-buffer (get-buffer-create "*path-variable*"))
;; Clear buffer
(delete-region (point-min) (point-max))
;; save cursor position
(save-excursion
(mapc (lambda (p) (insert (concat p "\n")))
(split-string (getenv "PATH")
;; Get path separator (;) for Windows
;; and (:) for Unix-like systems.
(if (equal system-type 'windows/nt)
";"
":"
)))))
By running this command M-x show-path-varible it will print all paths in the $PATH variable in a new buffer a easy way to read:
/home/archbox/opt/scala-2.11.8/bin
/home/archbox/opt/jdk1.8.0_74/bin/
/usr/local/sbin
/usr/local/bin
/usr/bin
/usr/lib/jvm/default/bin
/usr/bin/site_perl
/usr/bin/vendor_perl
/usr/bin/core_perl
/home/archbox/opt/java/bin
/home/archbox/opt/scala-2.11.8/bin/
It will add the directory: ~/opt/scala/bin to the $PATH variable. Before run the code below when the user runs M-x async-shell-command scala it will shows
an error message like this:
Note: The character ':' is the separator for Unix-like OS (Linux/ BSD and OSX) and ';' is the separator for Windows.
After it is done the Scala REPL will look like the picture below:
12 of 45 15/5/19, 1:07 pm
Elisp Snippets https://caiorss.github.io/Emacs-Elisp-Programm...
Usage example:
Alternative way:
(require 'cl)
(cl-flet
((add-to-path-var
(&rest pathlist)
(let* ((path-sep (if (equal system-type 'windows/nt)
";" ;; Windows separator
":" ;; Unix separator
)))
(setenv "PATH"
(concat (apply #'concat
(mapcar
(lambda (path)
(concat (expand-file-name path)
path-sep))
pathlist))
(getenv "PATH"))))))
;; Add paths to be added to PATH variable here,
(add-to-path-var
"~/opt/scala/bin"
"~/opt/jdk1.8.0_20/bin/"
))
(with-selected-frame (make-frame)
(shell-command "lspci"))
> (insert (format "\nKernel version %s " (shell-command-to-string "uname -r"))) ;; M-x eval-last-sexp
(defun insert-debug-info ()
(interactive)
13 of 45 15/5/19, 1:07 pm
Elisp Snippets https://caiorss.github.io/Emacs-Elisp-Programm...
The function shell-command-to-lines runs a shell command and returns the output lines. This function is useful to create shell command wrappers over Unix
shell commands like find.
ELISP> (mapc (lambda (p) (princ p) (princ "\n")) (shell-command-to-lines "ls /var/log"))
btmp
faillog
httpd
journal
lastlog
old
pacman.log
samba
speech-dispatcher
wtmp
Xorg.0.log
Xorg.0.log.old
ELISP> (mapc (lambda (p) (princ p) (princ "\n")) (shell-command-to-lines "find ~/.local/share/ -name \"*.desktop\""))
/home/arch/.local/share/xfce4/helpers/custom-WebBrowser.desktop
/home/arch/.local/share/applications/userapp-mono-IAJQMY.desktop
/home/arch/.local/share/applications/userapp-sh-9VFBMY.desktop
/home/arch/.local/share/applications/userapp-em-FLD8LY.desktop
/home/arch/.local/share/applications/userapp-mpv-FLQ9LY.desktop
/home/arch/.local/share/applications/userapp-Firefox-SOBHMY.desktop
ELISP> (mapc (lambda (p) (princ p) (princ "\n")) (search-files "~/.local" "*.desktop"))
/home/arch/.local/share/xfce4/helpers/custom-WebBrowser.desktop
/home/arch/.local/share/applications/userapp-mono-IAJQMY.desktop
/home/arch/.local/share/applications/userapp-sh-9VFBMY.desktop
/home/arch/.local/share/applications/userapp-em-FLD8LY.desktop
/home/arch/.local/share/applications/userapp-mpv-FLQ9LY.desktop
/home/arch/.local/share/applications/userapp-Firefox-SOBHMY.desktop
1.10.4 Pipe a region to external command terminal shell command pipe interface
Pipes the buffer content to external command and print the output in the buffer *Shell Command Output* . The command $ wc -l counts the number of line
of the current file.
Pipes the buffer content to external command $wc -l and get the output as a string.
14 of 45 15/5/19, 1:07 pm
Elisp Snippets https://caiorss.github.io/Emacs-Elisp-Programm...
pmin
pmax
command
"*shell-output*")
(let ((output
(with-current-buffer "*shell-output*"
(buffer-substring-no-properties
(point-min)
(point-max)))))
(kill-buffer "*shell-output*")
output
))
The command below will pipe the buffer *scratch* to the command =$ sed 's/foo.*/bar/g'= which replaces all values of foo for bar.
(with-current-buffer "*scratch*"
(shell-command-on-region
(point-min)
(point-max) "sed 's/foo.*/bar/g'" "*shell-output*" t )) ;; M-x eval-last-sexp
Usage: M-x shell-command-on-buffer Enter: sed 's/defun/defn/g' eplaces all defun words by defn.
15 of 45 15/5/19, 1:07 pm
Elisp Snippets https://caiorss.github.io/Emacs-Elisp-Programm...
Enter M-x shell-command-on-buffer and then $ ruby -ne 'printf("-%6s%s", $., $_)'
16 of 45 15/5/19, 1:07 pm
Elisp Snippets https://caiorss.github.io/Emacs-Elisp-Programm...
Emacs regexp islimited and doesn't have lookahead like perl or ruby regex. This command can extend the functionality of Emacs regex using ruby. This
function applies a ruby regex on the buffer.
1. Ping a host
(start-process NAME BUFFER PROGRAM &rest PROGRAM-ARGS
17 of 45 15/5/19, 1:07 pm
Elisp Snippets https://caiorss.github.io/Emacs-Elisp-Programm...
2. Tracerote a host
18 of 45 15/5/19, 1:07 pm
Elisp Snippets https://caiorss.github.io/Emacs-Elisp-Programm...
Some Linux ncurses-based applications like Htop, Top, Nmtui (Network Manager's terminal UI) doesn't work well running in Emacs terminal
emulator M-x term or M-x ansi-term . The solution is run the command from Emacs and send to an external terminal emulator.
This command uses xfce4-terminal but it can be changed to any other terminal emulator.
(defun run-terminal ()
"Launch application in a terminal emulator."
(interactive)
(start-process
"termx"
nil
;; Change this for your terminal.
"xfce4-terminal" "-e" (read-shell-command "Shell: ")))
To launch htop:
(shell-command-in-terminal "htop")
To launch nmtui:
(shell-command-in-terminal "nmtui")
(defun htop ()
"Launch htop task manager."
(interactive)
(shell-command-in-terminal "htop"))
This command asks the user a shell command providing tab completion and launches an asynchronous process (it doesn't freeze Emacs).
Unlike asynchronous-shell-command this command doesn't create a new buffer. It is useful to launch desktop GUI applications from Emacs like
Firefox, terminal emulator, file mangers and so on.
Note: In order to this command find the app the directory of the app must be in the $PATH variable.
19 of 45 15/5/19, 1:07 pm
Elisp Snippets https://caiorss.github.io/Emacs-Elisp-Programm...
(defun shell-launch ()
"Launch a process without creating a buffer. It is useful to launch apps from Emacs."
(interactive)
(let* ((cmd-raw (read-shell-command "Launch command: "))
(cmd-args (split-string-and-unquote cmd-raw))
(cmd (car cmd-args)) ;; program that will run
(args (cdr cmd-args))) ;; command arguments
(apply #'start-process `(,cmd
nil
,cmd ,@args
))))
This comamnd can be bound to a keybiding with the code bellow that bidns the key combination SUPER (Windows Key) + 8.
M-x shell/buffer-repl This command deletes all other windows and splits the current frame into two windows one containing a M-x shell (bash) repl and the other
a shell-script scratch buffer (sh-mode).
M-x shell/buffer-repl-frame Like the shell/buffer-repl, but shell and the scratch buffer are displayed in new frame.
(defun shell/buffer-repl ()
"Create "
(interactive)
(delete-other-windows)
(defun shell/buffer-repl-frame ()
(interactive)
(with-selected-frame (make-frame)
(shell/buffer-repl)))
This functions are useful to send regions, lines and regions to M-x shell repl from a shell-buffer or shell scratch buffer to the repl.
(defun shell/send-region ()
"Send selected text to M-x shell repl."
(interactive)
(comint-send-region (get-buffer-process "*shell*" )
(region-beginning) (region-end))
(comint-send-string (get-buffer-process "*shell*" ) "\n"))
(defun shell/insert-region ()
"Insert selected text into M-x shell repl and run it."
(interactive)
(let ((text (buffer-substring-no-properties (region-beginning)
(region-end)
)))
(with-current-buffer "*shell*"
(insert text)
(comint-send-input)
)))
(defun shell/send-line ()
"Send current line to M-x shell repl."
(interactive)
(comint-send-region (get-buffer-process "*shell*")
(line-beginning-position)
(line-end-position)
)
(comint-send-string (get-buffer-process "*shell*" ) "\n"))
(defun shell/insert-line ()
"Insert current line to M-x shell repl."
(interactive)
(let ((line (buffer-substring-no-properties
(line-beginning-position)
(line-end-position)
)))
(with-current-buffer "*shell*"
(insert line)
(comint-send-input)
)))
(defun shell/execute-line ()
"Execute current line as shell command."
(interactive)
(shell-command
(buffer-substring (line-beginning-position)
(line-end-position))))
(defun shell/execute-region ()
"Execute current region as shell command."
(interactive)
20 of 45 15/5/19, 1:07 pm
Elisp Snippets https://caiorss.github.io/Emacs-Elisp-Programm...
(shell-command
(buffer-substring (region-beginning)
(region-end))))
(defun shell/insert-buffer ()
"Insert all buffer content into M-x shell repl and run it."
(interactive)
(let ((text (buffer-substring-no-properties (point-min)
(point-max)
)))
(with-current-buffer "*shell*"
(insert text)
(comint-send-input)
)))
Example:
1. User runs M-x shell/buffer-repl-frame. It will open a frame like in the picture bellow.
1. User select the region with some code and runs M-x shell/insert-region. It will send the current region to the shell repl and display the output, in this case the
content of root directory.
21 of 45 15/5/19, 1:07 pm
Elisp Snippets https://caiorss.github.io/Emacs-Elisp-Programm...
This command is similar to emacs M-x compile, however it expands '%f' to the buffer file name, '%n' to buffer base name and '%d' to buffer
directory.
Example: if the current buffer is associated to the file /home/arch/scripts/gui1.scala and the user types the command M-x compile2 followed by
the command below.
The advantage of this command is that user doesn't need to type the file name to be compiled.
(defun compile2 ()
"Enhanced compilation command.
- %f - is expanded to current buffer file name.
- %d - is expanded to current buffer current directory.
- %n - is expanded to current buffer base name.
1.11.2 Compile a single c++ file related to current buffer and run it.
Usage: When the user run the command M-x cpp/compile-run-buffer at the current buffer (file code1.cpp) it will compile and run this file
running the command $ g++ code1.cpp -o code1.bin && ./code1.bin .
(defun cpp/compile-run-buffer ()
"Compile and run current c++ buffer (single-file C++ code).
Usage: By running M-x cpp/compile-buffer-run at current buffer (file code1.cpp)
will run $ g++ code1.cpp -o code1.bin && ./code1.bin"
(interactive)
22 of 45 15/5/19, 1:07 pm
Elisp Snippets https://caiorss.github.io/Emacs-Elisp-Programm...
1.11.3 Compile and run a single c++ file at point in dired mode
Usage: In dired mode mode, place the cursor at the file to be compiled and run M-x cpp/compile-run-dired.
(defun cpp/compile-run-dired ()
"Compile single c++ file at point in dired mode.
Usage: Position the cursor at the file to be compiled and
run M-x cpp/compile-dired."
(interactive)
(let* ((file-name (file-name-nondirectory (dired-file-name-at-point)))
;; Change file name extension to .bin
(output (concat (file-name-base file-name) ".bin")))
(save-buffer)
(compile (format "g++ %s -o %s && ./%s"
file-name
output
output
))))
1.12 File
23 of 45 15/5/19, 1:07 pm
Elisp Snippets https://caiorss.github.io/Emacs-Elisp-Programm...
The Emacs API doesn't provide a straightforward way to read file directly to a string. The only way to perform this taks is using a temporary
buffer.
order hosts,bind
multi on
# End of file
(find-file-other-window "/etc/fstab")
(find-file-other-frame "/etc/fstab")
Function: find-file-nonselect
Emacs Documentation: Read file FILENAME into a buffer and return the buffer.If a buffer exists visiting FILENAME, return that one, but verify
that the file has not changed since visited or saved. The buffer is not selected, just returned to the caller.
> b1
#<buffer .bashrc>
;; Example:
;; ELISP> (open-as-root "/etc/host.conf")
;;
;;
;; Open an already opened buffer as root
;;
;; M-x open-buffer-as-root
;;
(defun open-buffer-as-root ()
(interactive)
(let
(
;; Get the current buffer file name
(filename (buffer-file-name (current-buffer)))
;; Get the current file name
(bufname (buffer-name (current-buffer)))
)
(progn
(kill-buffer bufname) ;; Kill current buffer
(open-as-root filename)))) ;; Open File as root
24 of 45 15/5/19, 1:07 pm
Elisp Snippets https://caiorss.github.io/Emacs-Elisp-Programm...
ELISP> (buffer-file-name)
"/home/archbox/.emacs.d/init.el"
ELISP>
Example: It will replace the extension of file name init.el to init.py. Note: it will not modify the file.
ELISP>
(let ((file "/home/archbox/.emacs.d/init.el"))
(concat (file-name-directory file)
(file-name-base file)
".py"
))
"/home/archbox/.emacs.d/init.py"
ELISP>
or
(dired "/var/log")
(dired-other-window "/var/log")
25 of 45 15/5/19, 1:07 pm
Elisp Snippets https://caiorss.github.io/Emacs-Elisp-Programm...
(dired-other-frame "/var/log")
.
..
Xorg.0.log
Xorg.0.log.old
btmp
faillog
httpd
journal
lastlog
old
pacman.log
samba
speech-dispatcher
wtmp
/var/log/.
/var/log/..
/var/log/Xorg.0.log
/var/log/Xorg.0.log.old
/var/log/btmp
...
26 of 45 15/5/19, 1:07 pm
Elisp Snippets https://caiorss.github.io/Emacs-Elisp-Programm...
1.15.1 Overview
Source: hiroina/.emacs
(defun dired-copy-path ()
"In dired, copy file path to kill-buffer.
At 2nd time it copy current directory to kill-buffer."
(interactive)
(let (path)
(setq path (dired-file-name-at-point))
(if (string= path (current-kill 0 1)) (setq path (dired-current-directory)))
(message path)
(kill-new path)))
Usage:
1. Copy file from clibpoard with Emacs or from system's default file manager.
2. M-x dired-paste-file-from-clipboard
(defun dired-paste-file-from-clipboard ()
"Paste a file from clpboard to current directory."
(interactive)
(flet ((clipboard-get ()
(with-temp-buffer
(clipboard-yank)
(buffer-substring-no-properties
(point-min)
(point-max)))))
(copy-file (clipboard-get) ".")
(revert-buffer)))
(defun dired-touch ()
"Creates empty file at current directory."
(interactive)
(append-to-file "" nil (read-string "New file: "))
(if (equal major-mode 'dired-mode)
(revert-buffer)))
(setq lexical-binding t)
1.15.7 Open a file with default system application dired file open system
27 of 45 15/5/19, 1:07 pm
Elisp Snippets https://caiorss.github.io/Emacs-Elisp-Programm...
;;; Linux
(gnu/linux (let ((process-connection-type nil))
(start-process
"proc"
nil
;; Command
"xdg-open" (expand-file-name filename))))
;; Windows 7, 8, 10 - Kernel NT
(windows-nt (start-process
"proc"
nil
;; Command
"cmd" "/C" "start" "" (expand-file-name filename)
))))
Function: dired-xdg
(defun dired-xdg ()
"Open file at point with systems' default app.
Usage: In the dired mode select a file and type M-x dired-xdg
to execute the file with the system default app.
"
(interactive)
(with-current-buffer
(xdg-open (dired-get-filename))))
(defun dired-close-all ()
"Close all dired buffers."
(interactive)
(mapc (lambda (buf)
(if (equal (buffer-local-value 'major-mode buf)
'dired-mode)
(kill-buffer buf)))
(buffer-list)))
The function M-x dired-bookmarks opens a helm menu where the user can select its favorites directories. It provides quick directory navigation.
(defvar dired-bookmarks-path-list
'("~"
"~/Desktop"
"~/Downloads"
"~/Documents"
"~/Documents/projects"
"~/Documents/wiki"
"/tmp"
;"/mnt/transfer"
"/var/log"
"/etc"
"/etc/init.d"
"~/.config"
"~/.local"
"~/.local/share"
"~/.emacs.d"
))
(defun dired-bookmarks ()
"Open a directory by selecting it in Helm menu."
(interactive)
(helm
:prompt "Bookmarks: "
:sources `((
(name . "Bookmark: ")
(candidates . dired-bookmarks-path-list)
(action . dired)
))))
Usage:
28 of 45 15/5/19, 1:07 pm
Elisp Snippets https://caiorss.github.io/Emacs-Elisp-Programm...
1. M-x dired/filter-extension
2. Enter the extensions to filter separated by space like: 'html js ccp'
3. It will only show files matching this extensions. To view all files type g.
(defun dired/filter-extension ()
"Filter dired buffer by file extensions.
Usage:
1. M-x dired/filter-extension
2. Enter the file extensions separated by space like: exe dll
It will only show files ending with extensions *.exe and *.dll.
To show all files of dired buffer again type 'g'."
(interactive)
(dired-mark-files-regexp
(mapconcat 'identity
(mapcar (lambda (ext) (format "\\.%s$" ext))
(split-string (read-string "Extension: ")))
"\\\|"
))
(dired-toggle-marks)
(dired-do-kill-lines))
This command only displays files matching the given regex (Emacs' regex).
Example: By entering M-x dired/filter '\.pdf$' without quotes it will only show file names ending with .pdf. To display all files again type 'g'.
(defun dired/filter ()
"Show only files which name matches a regex."
(interactive)
(dired-mark-files-regexp (read-regexp "Regex: "))
(dired-toggle-marks)
(dired-do-kill-lines))
(defun dired-exec-explorer ()
"In dired, execute Explorer"
(interactive)
(let (path)
(setq path (dired-file-name-at-point))
(setq path (replace-regexp-in-string "~" "c:/home" path))
(setq path (replace-regexp-in-string "/" "\\\\" path))
(message path)
;(kill-new path)
(start-process "explorer" nil "explorer" (concat "/select," path))))
1.16.1 Overview
References:
Unix time stamp (Number of seconds or microseconds since January first of 1970):
ELISP> (current-time)
(22889 50074 486018 948000)
29 of 45 15/5/19, 1:07 pm
Elisp Snippets https://caiorss.github.io/Emacs-Elisp-Programm...
ELISP> (current-time-string)
"Sat Jul 15 04:26:27 2017"
ELISP>
ELISP> (float-time)
1500103652.678849
ELISP>
ELISP> (current-time-zone)
(-10800 "-03")
ELISP>
ELISP> user-init-file
"/home/arch/.emacs.d/init.el"
ELISP> user-emacs-directory
"~/.emacs.d/"
The variable window-system is the name of window system through which the selected frame is displayed.
(defun test-window-system ()
(interactive)
(if window-system
(message "Running in Window System / GUI")
(message "Running in terminal ")
))
;; In GUI
;;-----------------------------
> (test-window-system) ;; M-x eval-print-last-sexp
"Running in Window System / GUI"
;; In Terminal
;;------------------------------
> (test-window-system) ;; M-x eval-print-last-sexp
"Running in terminal "
(defun open-yandex ()
"Open the web site http://www.yandex.com."
(interactive)
(browse-url "http://www.yandex.com"))
30 of 45 15/5/19, 1:07 pm
Elisp Snippets https://caiorss.github.io/Emacs-Elisp-Programm...
;; Or
;;; Or
;;; Or
1.18.3 Search Web sites with Emacs web search url browser
1. Search google
(require 'url-util)
(defun search-google ()
(interactive)
"Search www.google.ca."
(browse-url
(format "http://www.google.ca?gws_rd=ssl#q=%s"
(url-encode-url
(read-string "Google: ")))))
Usage:
M-x search-hackernews-with-google
M-x search-reddit-with-google
M-x search-stackoverflow-with-gooogle
(require 'url-util)
(defun search-hackernews-with-google ()
(interactive)
(search-google-url "https://news.ycombinator.com" (read-string "Hnews: ")))
(defun search-reddit-with-google ()
(interactive)
(search-google-url "https://www.reddit.com" (read-string "Reddit: ")))
(defun search-stackoverflow-with-google ()
(interactive)
(search-google-url "http://stackoverflow.com" (read-string "S.O Search: ")))
3. Search github
(require 'url-util)
(defun search-github ()
(interactive)
"Search www.google.ca"
(browse-url
(format "https://github.com/search?q=%s"
(url-encode-url
(read-string "Github Search: ")))))
31 of 45 15/5/19, 1:07 pm
Elisp Snippets https://caiorss.github.io/Emacs-Elisp-Programm...
4. Search gisthub
(require 'url-util)
(defun search-gisthub ()
"Search gisthub : http://gist.github.com.
Usage: M-x search-gisthub"
(interactive)
(browse-url
(format "https://gist.github.com/search?p=50&q=%s&ref=searchresults"
(url-encode-url
(read-string "Gisthub Search: ")))))
(defun open-emacs-manual ()
"Open Emacs online Manual.
Usage: M-x open-emacs-manual.
It opens the web site: https://www.gnu.org/software/emacs/manual"
(interactive)
(browse-url "https://www.gnu.org/software/emacs/manual/"))
(defun search/dictionary ()
"Search Merrian Webster Dictionary·"
(interactive)
(browse-url
(format "https://www.merriam-webster.com/dictionary/%s"
(url-encode-url
(read-string "Dictionary: ")))))
Emacs Provides a built-in lightweight web browser eww browser that can be used to quick access web sites in terminal or Emacs GUI.
(eww-browse-url "http://m.reddit.com/r/emacs")
(eww-open-file "/some/html/file.html")
Key Bindings
http://m.reddit.com
http://m.reddit.com/r/emacs
Google Search
Google News:
32 of 45 15/5/19, 1:07 pm
Elisp Snippets https://caiorss.github.io/Emacs-Elisp-Programm...
http://wiki.archlinux.org/index.php/Table_of_contents
Wikipedia
http://en.m.wikipedia.org
Hackernews
http://www.news.ycombinator.com
http://www.httpbin.org/get
Example:
Output:
HTTP/1.1 200 OK
Server: nginx
Date: Fri, 23 Sep 2016 23:44:21 GMT
Content-Type: application/json
Content-Length: 304
Connection: keep-alive
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
{
"args": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip",
"Extension": "Security/Digest Security/SSL",
"Host": "www.httpbin.org",
"Mime-Version": "1.0",
"User-Agent": "URL/Emacs"
},
"origin": "179.176.152.199",
"url": "http://www.httpbin.org/get"
}
This function downloads a file from the url copied to clipboard and then asks the user to which file the data will be downloaded suggesting the
file name extracted from the url.
This function updates the current buffer (revert buffer) if executed in a dired buffer (directory).
(defun download-url-to-file ()
"Download a file from a URL in the clibpoard.
This command asks the user for the url suggesting the url
stored in the clipboard and then asks the user for the file name
to be downloaded suggesting the file name extracted from the url.
"
(interactive)
(let*
;; Get the URL suggestion from clibpoard
((text (with-temp-buffer
(clipboard-yank)
(buffer-substring-no-properties (point-min)
33 of 45 15/5/19, 1:07 pm
Elisp Snippets https://caiorss.github.io/Emacs-Elisp-Programm...
(point-max))))
;; Ask the user to confirm the url
(url (read-string "Url: " text)))
(url-copy-file
url
;; Get file name.
(read-file-name "File name: "
(car (last (split-string url "/")))))
1.19.4 Download a file and display it on a buffer http web buffer clipboard
This command reads an url from the user and download the file content and displays it on a buffer without write a file to disk.
Example:
(defun download-code-view ()
"Download a source code from url and displays it on a buffer."
(interactive)
(let* (
;; Get url from clipboard
(clipboard (with-temp-buffer
(clipboard-yank)
(buffer-substring-no-properties (point-min)
(point-max))))
;; Read url and suggesting clipboard
(url (read-string "Url: " clipboard))
(buf (url-retrieve-synchronously url))
;; Tries to extract the file name from url
(filename (car (last (split-string url "/"))))
(bufname (concat "*" filename "*"))
)
(switch-to-buffer buf)
;; Remove http header
(goto-char (point-min))
(re-search-forward "^$")
(delete-region (point) (point-min))
(kill-whole-line)
;; Remove \r characters or ^M
(replace-string (char-to-string 13) "")
;; Rename buffer to a better name
(with-current-buffer buf
(rename-buffer bufname))))
Source
34 of 45 15/5/19, 1:07 pm
Elisp Snippets https://caiorss.github.io/Emacs-Elisp-Programm...
(point) (point-max)))
(kill-buffer (current-buffer))
response-string))
ELISP> (princ (url-http-post "http://httpbin.org/post" '(("use" . "dummy") ("pass" . "something else") ("code" . "pxyz0011213"))))
{
"args": {},
"data": "",
"files": {},
"form": {
"code": "pxyz0011213",
"pass": "something else",
"use": "dummy"
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip",
"Content-Length": "48",
"Content-Type": "application/x-www-form-urlencoded",
"Extension": "Security/Digest Security/SSL",
"Host": "httpbin.org",
"Mime-Version": "1.0",
"User-Agent": "URL/Emacs"
},
"json": null,
"origin": "167.103.159.147",
"url": "http://httpbin.org/post"
}
This command install a single-file elisp package from a given URL to source file.
Usage:
1. Copy the url of the package file to be installed like http://mumble.net/~campbell/emacs/paredit-beta.el from EmacsWiki:Par Edit.
2. Type M-x package-install-url It will ask to confirm the URL. Then type return and the package will be installed.
To find where the package was intalled type M-x find-library <library-name>.
(defun package-install-url ()
"Install a single-file package *.el file from Url."
(interactive)
(let* (
;; Suggest the URL stored in the clipboard
(clipboard (with-temp-buffer
(clipboard-yank)
(buffer-substring-no-properties (point-min) (point-max))))
;; Ask the user to confirm the url
(url (read-string "Url: " clipboard))
)
35 of 45 15/5/19, 1:07 pm
Elisp Snippets https://caiorss.github.io/Emacs-Elisp-Programm...
This command switches between buffer with files that are in current directory.
(defun buffer/switch-in-directory ()
"Switch between buffers in same directory."
(interactive)
(helm
:prompt "Buffer switch: "
:sources `((
(name . "Dir: ")
(candidates . ,(mapcar
(lambda (b) (cons (buffer-name b) b))
;; filter buffers not in this directory (code bellow)
(remove-if-not (lambda (b)
(buffer/with-file-in-directory-p
(or (file-name-directory (buffer-file-name))
default-directory
)
b
))
(buffer-list)
)))
(action . switch-to-buffer)
))))
(require 'helm)
(defun helm-recent-files ()
(interactive)
(helm
:prompt "File: "
:sources `((
(name . "File: ")
(candidates . ,recentf-list)
(action . find-file)
))))
(require 'helm)
36 of 45 15/5/19, 1:07 pm
Elisp Snippets https://caiorss.github.io/Emacs-Elisp-Programm...
(defun helm-recent-dirs ()
(interactive)
(helm
:prompt "Dir: "
:sources `((
(name . "Dir: ")
(candidates . (lambda ()
(unique
(map #'file-name-directory recentf-list))))
(action . dired)))))
This piece of code gets all executables in the $PATH variable and searche for one that matches the user input and then launches it. It is useful
to launch applications without block Emacs.
(defun get-executables ()
"Returns a list of all files available in the directories of the $PATH variable."
(apply #'append
(mapcar (lambda (p) (directory-files p t))
(remove-if-not #'file-exists-p
(split-string (getenv "PATH") ":")))))
(defun helm-launcher ()
"Launches apps available in $PATH without blocking Emacs.
Usage M-x heml-launcher"
(interactive)
(let ((data (mapcar (lambda (p)
(cons (file-name-nondirectory p)
p
))
(get-executables)
)))
(helm
:prompt "Shell: "
:sources `((
(name . "Shell: ")
(candidates . ,data)
(action . run-async)
)))))
37 of 45 15/5/19, 1:07 pm
Elisp Snippets https://caiorss.github.io/Emacs-Elisp-Programm...
(defun switch-file ()
"Switch between buffers that are associated with files.
Usage: M-x switch file."
(interactive)
(let ((data
(mapcar (lambda (b)
(cons (buffer-file-name b) b ))
(remove-if-not #'buffer-file-name (buffer-list)))))
(helm
:prompt "Buffer: "
:sources `((
(name . "File Buffers")
(candidates . ,data)
(action . switch-to-buffer)
)))))
(defun helm-switch-mode ()
"Switch between all major programming modes available in Emacs.
Usage: M-x helm-switch-mode."
(interactive)
(cl-flet ((unique (xs)
(let
((result nil))
(dolist (x xs)
(if (not (member x result))
(push x result)))
(reverse result))))
(helm
:prompt "Mode: "
:sources `((
(name . "Emacs Major Modes")
(candidates . ,(unique (mapcar #'symbol-name
(remove-if-not #'symbolp
(mapcar #'cdr auto-mode-alist)))))
(action . ,(lambda (m) (funcall (intern-soft m)))))))))
(setq helm-url-default-url-list
'(
("google" . "http://www.google.ca")
("yandex" . "http://www.yandex.com")
("reddit" . "http://www.reddit.com")
("/r/haskell". "http://www.reddit.com/r/haskell")
("/r/emacs" . "http://www.reddit.com/r/emacs")
("/r/csharp" . "http://www.reddit.com/r/csharp")
))
(defun helm-web ()
(interactive)
(helm
:prompt "Web Site: "
:sources `((
(name . "Bookmarks")
(candidates . helm-url-default-url-list)
(action . (lambda (c) (browse-url (cdr c))) )
))))
This command extracts the build tasks from a Makefile and shows it as a helm menu where the user can select the task to be executed. It will
be run as a asynchronous process.
Version1:
(defun helm-make ()
(interactive)
(let ((tasks
(re-seq "^\\w+"
(with-temp-buffer
(insert-file "Makefile")
(buffer-substring-no-properties
(point-min)
(point-max))))))
(helm
:prompt "Makefile task: "
38 of 45 15/5/19, 1:07 pm
Elisp Snippets https://caiorss.github.io/Emacs-Elisp-Programm...
:sources `((
(name . "Task")
(candidates . ,tasks)
(action . (lambda (task)
(start-process "make"
"*make*"
"make" task
))))))
(switch-to-buffer "*make*")))
(defun helm-make2 ()
(interactive)
(flet ((re-seq2 (regexp string) (save-match-data
(let ((pos 0)
matches)
(while (string-match regexp string pos)
(push (match-string 0 string) matches)
(setq pos (match-end 0)))
matches))))
(let ((tasks (re-seq2 "^\\w+"
(with-temp-buffer
(insert-file "Makefile")
(buffer-substring-no-properties
(point-min)
(point-max)
)))))
(helm
:prompt "Makefile task: "
:sources `((
(name . "Task")
(candidates . ,tasks)
(action . (lambda (task)
(start-process "make"
"*make*"
"make" task
))))))
(switch-to-buffer "*make*"))))
Example: Assuming that the current directory has the Makefile with content. The command M-x helm-make will show the tasks all, html,
browse and clean.
all: html
html:
mkdir -p dist
emacs --batch -q -l build.el --kill
mv dist/README.html dist/index.html
cp -r -v images dist/images
cp -r *.org dist/
browse:
firefox dist/index.html
clean:
rm -rf dist/*.html dist/*.html~
1.22 Persistence
Interactive Developement
ELISP>
ELISP> (format "(setq last-session-files '%S)" (remove-if 'null (mapcar 'buffer-file-name (buffer-list))))
"(setq last-session-files '(\"/home/tux/.emacs.d/init.el\" \"/sudo:root@localhost:/etc/host.conf\"))"
ELISP> (setq code (format "(setq last-session-files '%S)" (remove-if 'null (mapcar 'buffer-file-name (buffer-list)))))
"(setq last-session-files '(\"/home/tux/.emacs.d/init.el\" \"/sudo:root@localhost:/etc/host.conf\"))"
ELISP>
39 of 45 15/5/19, 1:07 pm
Elisp Snippets https://caiorss.github.io/Emacs-Elisp-Programm...
nil
ELISP>
ELISP>
Joining Everything
File: sessions.el
(defun make-session-code ()
(interactive)
(format "(setq last-session-files '%S)" (remove-if 'null (mapcar 'buffer-file-name (buffer-list)))))
(defun save-session ()
"Save Current Session"
(interactive)
(when (file-exists-p session-file) (delete-file session-file))
(write-file session-file (make-session-code)))
(defun load-session ()
(interactive)
(load-file session-file)
(mapcar 'find-file last-session-files)
)
(message "Reloaded")
Press <F7> to save the curren window configuration and <F8> to restore. The functions can also be executed with A-x save-view or A-x
restore-view.
(defun save-view ()
"Save current window configuration"
(interactive)
(setq winconf (current-window-configuration))
(message "View Saved - Press to restore"))
(defun restore-view ()
"Restore saved window configuration"
(interactive)
(set-window-configuration winconf)
(message "View loaded"))
1.23.1 Save the scratch buffer and reload every Emacs startup
Source: scratch.el
40 of 45 15/5/19, 1:07 pm
Elisp Snippets https://caiorss.github.io/Emacs-Elisp-Programm...
;; window-setup-hook
;; @see info 38.1.1 Summary: Sequence of Actions at Startup
(add-hook 'kill-buffer-hook; *scratch*
(lambda ()
(if (equal (buffer-name) "*scratch*") (scratch-save))))
(add-hook 'after-save-hook
(lambda ()
(unless (get-buffer "*scratch*") (scratch-resume))))
(defun scratch-save ()
(let ((buf (get-buffer "*scratch*")))
(when buf
(set-buffer buf)
(write-file scratch-buffer-file)
(ignore-errors (kill-buffer "scratch.el")))))
(defun scratch-resume ()
"*scratch* "
(interactive)
(set-buffer (get-buffer-create "*scratch*"))
(funcall initial-major-mode)
(insert-file-contents scratch-buffer-file nil nil nil t)
The function occur is useful for code navigation and scan code statements, this function is invoked by M-x occur.
Examples:
The function python/scan-functions will scan for all lines that starts with def statement in a Python code and show the matching lines in the
right side.
(defun python/scan-functions ()
(interactive)
(split-window-horizontally)
(occur "def"))
Example:
It can also be useful to Scan code tags like: @FIXME, @TODO, @README, @NOTE. This fuction can be excuted by typing: M-x scan-code-tags
Example:
(defun scan-code-tags ()
"Scan code tags: @TODO: , @FIXME:, @BUG:, @NOTE:."
(interactive)
(split-window-horizontally)
(occur "@FIXME:\\|@TODO:\\|@BUG:\\|@NOTE:"))
41 of 45 15/5/19, 1:07 pm
Elisp Snippets https://caiorss.github.io/Emacs-Elisp-Programm...
1.23.3 Quick edit and reload Emacs Configuration File. elisp customization
It is useful to quick edit and reload ~/emacs.d/init.el without restart emacs. Those functions can be put in the init.el file.
Source: http://www.emacswiki.org/emacs-en/download/misc-cmds.el
(defun refresh ()
"Revert buffer without confirmation."
(interactive)
(revert-buffer t t))
Usage:
Using Eval
Copy and paste the code below to the scratch buffer and enter M-x eval-buffer. This code can also be put in ~/emacs.d/init.el, in the config file.
42 of 45 15/5/19, 1:07 pm
Elisp Snippets https://caiorss.github.io/Emacs-Elisp-Programm...
(defun make-menu-rows ()
(mapcar
(lambda (x)
(format "[\"%s\" (load-theme '%s)]" (symbol-name x) (symbol-name x) ))
(custom-available-themes)))
(defun make-color-menu-code ()
(format
"
(easy-menu-define djcb-menu global-map \"Color Themes\"
'(\"Color Themes\"
%s
)
)
"
(mapconcat 'identity (make-menu-rows) "\n")))
;;
;; "Eval is evil". It must be avoided, because, it is hard to refactor,
;; hard to debug and vulnerable to code injection on Web Apps, so a better
;; way to write it is to use Elisp macros.
;;
;; @DONE: Change eval-string to an Elisp macro.
;;
(eval-string (make-color-menu-code))
Using Macros
Developement:
ELISP> (custom-available-themes)
(cyberpunk adwaita deeper-blue dichromacy leuven light-blue manoj-dark misterioso tango-dark tango tsdh-dark tsdh-light wheatgrass whiteboard wombat)
ELISP> (mapcar
(lambda (sym) `[,(symbol-name sym) (load-theme (quote ,sym))])
(custom-available-themes)
)
(["adwaita"
(load-theme 'adwaita)]
["deeper-blue"
(load-theme 'deeper-blue)]
["dichromacy"
(load-theme 'dichromacy)]
["leuven"
(load-theme 'leuven)]
["light-blue"
(load-theme 'light-blue)]
["manoj-dark"
(load-theme 'manoj-dark)]
["misterioso"
(load-theme 'misterioso)]
["tango-dark"
(load-theme 'tango-dark)]
...
;;; Now execute the generated code, the menu will pop up.
;;;
Final code
43 of 45 15/5/19, 1:07 pm
Elisp Snippets https://caiorss.github.io/Emacs-Elisp-Programm...
(defun make-menu-rows ()
(mapcar
(lambda (sym) `[,(symbol-name sym) (load-theme (quote ,sym))])
(custom-available-themes)))
(defmacro make-color-menu ()
`(easy-menu-define djcb-menu global-map "Color Themes"
'("Color Themes"
,@(make-menu-rows))))
(make-color-menu)
(progn
(defun make-menu-rows ()
(mapcar
(lambda (sym) `[,(symbol-name sym) (load-theme (quote ,sym))])
(custom-available-themes)))
(defmacro make-color-menu ()
`(easy-menu-define djcb-menu global-map "Color Themes"
'("Color Themes"
,@(make-menu-rows))))
(make-color-menu))
(defun ielm/clear ()
"Clear IELM buffer."
(interactive)
(with-current-buffer "*ielm*"
(let ((inhibit-read-only t))
(erase-buffer))))
(defun ielm/send-region ()
(interactive)
(let ((text (buffer-substring-no-properties (region-beginning)
(region-end))))
(with-current-buffer "*ielm*"
(insert text)
(ielm-send-input))))
(defun ielm-select-buffer-other-window ()
"Select other window' buffer as IELM's working buffer."
(interactive)
(let ((buf (save-window-excursion
(other-window 1)
(current-buffer))))
(ielm-change-working-buffer buf)
(ielm-print-working-buffer)))
44 of 45 15/5/19, 1:07 pm
Elisp Snippets https://caiorss.github.io/Emacs-Elisp-Programm...
2.5 Start IELM in other window setting the working buffer to current buffer
This command starts IELM in other window setting the working buffer to current buffer. It allows the user to manipulate the current buffer.
(defun ielm-this-buffer ()
"Start IELM in other window with current buffer set to current windows' buffer."
(interactive)
(let ((buf (current-buffer)))
(ielm-other-window)
(ielm-change-working-buffer buf)
(ielm-print-working-buffer)))
(defun ielm-other-frame ()
"Launch IELM in other frame."
(interactive)
(with-selected-frame (make-frame)
(ielm)))
https://www.emacswiki.org/emacs/EmacsClient
https://www.emacswiki.org/emacs/EmacsAsDaemon
http://emacs-fu.blogspot.com.br/2009/02/emacs-daemon.html
https://wiki.archlinux.org/index.php/Emacs
4 Org-mode
Moved to: org-mode.html
5.1 Gists
garaud/pipe-to-emacs.py - Pipe to Emacs: Insert a result from an UNIX command into a new Emacs buffer
TikhonJelvis/epage - "A little pager script I wrote that calls out to emacsclient."
https://www.emacswiki.org/emacs/download/install-elisp.el
45 of 45 15/5/19, 1:07 pm