Emacs

Table of Contents

Emacs

Code snippets

General

  • Disable Meta behavior in Emacs for right Option key

    Rif.: apple.stackexchange.com

    (when (eq system-type 'darwin)
      (setq mac-right-option-modifier 'none))
    
  • Generic function to get JSON data from a URI
    (require 'json)
    (defun get-json-data (url)
      (with-current-buffer
          (url-retrieve-synchronously url)
        (goto-char (+ 1 url-http-end-of-headers))
        (json-read-object)))
    
  • Org 2 Webjump list

    Convert a list of bookmarks (expressed in a org document) in a data structure suitable for webjump.

    (require 'cl)
    (defun get-webjump-sites ()
      "converts a org document in a data structure suitable for webjump"
      (let ((regex "\\[\\[\\(.*\\)\\]\\[\\(.*\\)\\]\\]"))
        (with-current-buffer (get-file-buffer "~/Dropbox/stefanorodighiero.net/links.org")
          (loop for i in (org-map-entries 'org-entry-properties nil 'file)
                for item-string = (cdr (assoc "ITEM" i))
                if (string-match regex item-string)
                collect `(,(match-string 2 item-string) . ,(match-string 1 item-string))))))
    

Elfeed

Download the image under the cursor

(defun shr-download-image ()
  "Downloads the image under point"
  (interactive)
  (let ((url (get-text-property (point) 'image-url)))
    (if (not url)
        (message "No image under point!")
      (url-copy-file url (expand-file-name (url-file-nondirectory url)
                                           "~/Pictures/elfeed/")))))

(define-key elfeed-show-mode-map "D" #'shr-download-image)

Helm

An example of a custom-made Helm menu based on current buffer contents

(defun helm-subroutines ()
  (interactive)
  (helm :sources '(((name . "Perl subroutines")
                    (volatile)
                    (headline "^sub ")))))

Switch theme

Disables all enabled themes before activating the new one:

(defun switch-theme (theme)
  (interactive
   (list
    (intern (completing-read "Switch to custom theme: "
                             (mapcar 'symbol-name
                                     (custom-available-themes))))))
  (dolist (curr custom-enabled-themes) (disable-theme curr))
  (load-theme theme))

Increase text size

Increase text size for all the existing buffers matching a certain major-mode.

(defun increase-text-scale-in-buffer-group (major-mode-name)
  "Increment text scale for all buffers matching a give major mode name"
  (interactive
   (list (intern (completing-read
                  "Select major mode from the list: "
                  (remove-duplicates (mapcar (lambda (b)
                                               (buffer-local-value
                                                'major-mode (get-buffer b)))
                                             (buffer-list)))))))
  (cl-loop for buffer in (buffer-list)
           when (string= (buffer-local-value
                          'major-mode (get-buffer buffer))
                         major-mode-name)
           do (with-current-buffer buffer
                (text-scale-increase 1))))

Create thumbnail

(defun create-thumbnail (filename)
  (let* ((dir (file-name-directory filename))
         (basename (file-name-base filename))
         (extension (downcase (file-name-extension filename)))
         (thumb-filename (concat dir basename "-thumb." extension)))
    (call-process "convert" nil nil nil
                  "-resize" "25%"
                  filename
                  thumb-filename)
    thumb-filename))

Clone indirect buffer in another frame

Rif.: Superuser

(global-set-key [?\C-x ?5 ?c]
  '(lambda(newname display-flag)
    "Like `clone-indirect-buffer-other-window' but display in another frame."
    (interactive
      (progn
        (if (get major-mode 'no-clone-indirect)
          (error "Cannot indirectly clone a buffer in %s mode" mode-name))
        (list (if current-prefix-arg
          (read-buffer "Name of indirect buffer: " (current-buffer))) t)))
    (save-window-excursion
      (let ((newbuf (clone-indirect-buffer newname display-flag)))
      (switch-to-buffer-other-frame newbuf)))))

Dired

Rif.: Emacswiki

(setq dired-dwim-target t) 

org-babel: create a SQLite DB based on a table

In a sqlite src block:

#+begin_src sqlite :db /tmp/test.db :var orgtable=log :colnames yes
drop table if exists sleep;
create table sleep (day text, up text, in_bed text);
.mode csv sleep
.import $orgtable sleep
#+end_src

Keys and commands

General

Key sequence Command Elisp function
C-x r t Rectangle: prefix  
C-c C-x C-v Toggle inline image mode  
M-g M-g Goto line…  
C-u C-x C-e Eval and insert output at point  
M-0 C-k Kill and yank backwards  
C-s C-w Appends the rest of the word under the cursor to the search pattern  
C-fx C-m f Set the file coding-system of the current buffer  

Dired

Key sequence Command Elisp function
+ Create directory  
m/u Mark/Unmark for deletion  
Q Search&replace on marked files  
C-x C-q Edit mode  
C-c C-c (in edit mode) Apply changes  
C-c C-k (in edit mode) Cancel  
# Flag all auto-save files (files whose names start and end with ‘#’) for deletion  
~ Flag all backup files (files whose names end with ‘~’) for deletion  
% & Flag for deletion all files with certain kinds of names which suggest you could easily create those files again.  

Org-mode

Key sequence Command Elisp function
C-c c Capture  
C-c C-w Refile (org-refile)
C-c C-t Toggle state  
C-c C-d Add deadline  
C-c / Sparse tree (org-sparse-tree)
C-c C-x b Show the current subtree in an indirect buffer (org-tree-to-indirect-buffer)

Markdown mode

Key sequence Command Elisp function
C-c C-s b Insert blockquote (markdown-insert-blockquote)
C-c C-c p Preview  

eww

Key sequence Command Elisp function
H Show browsing history  
l   (eww-back-url)
w Copy current page URL (eww-copy-page-url)

Helm

Key sequence Command Elisp function
C-x c C-x C-f   (helm-find-files)
C-Space   Mark buffers in helm-mini
M-S-d   Delete marked buffers in helm-mini

SLIME

Key bindings

Key sequence Command Elisp function
C-M-i Complete symbol  
C-M-x Evaluate the current toplevel form slime-eval-defun
<Leader> !p Change package  

<Leader> is comma (,).

Snippets

This comes from #lisp IRC channel: "when you are in a slime repl window, slime-process will return that instance process", so

(switch-to-buffer (process-buffer
                   (slime-process)))

Date: 2014-04-26 Sat 00:00

Author: Stefano Rodighiero

Created: 2020-10-11 Sun 10:09

Validate