Deal with Emacs

simon

Nov. 22, 2018

Created: 2019-10-04 Mon 00:31

From where I talk ?

Who I am ?

  • Mainly a geek guy
  • Research Engineer at Univ. Paris 7 Diderot
    • Post-Doc Católica (Chile)
    • Post-Doc Univ. Liège (hi Berty!)
    • PhD at ONERA Toulouse

simon.tournier@univ-paris-diderot.fr

https://github.com/zimoun

My life

  • 2006-2010: Vim user… and happy!
  • 2011 New Year Resolutions: Switch to Emacs (video)

    \(\Rightarrow\) because the sysadmin showed me crazy stuff

    && he helped me to start.

I am only an enthusiast user, not a guru

My typical use-cases

  • Edit multiple languages: Python, Julia, C/C++, (Fortran), R, Shell, \(\LaTeX\), Haskell, Scheme, etc.
  • Git (pull, commit, merge)
  • Organize my tasks and log them
  • Write docs and few papers, compose "workflow"
(defun ask-at-point (&optional i-know)
  "Feel free to ask everything."
  (interactive "P")
  (if i-know (message "Quick Answer.")
   (browse-url
    (concat "https://www.ddg.gg/?q=emacs+" (thing-at-point 'symbol)))))

Contents

  1. Quick Intro to Emacs
  2. How to configure: use-package
  3. Quick Demo:
    • \(\LaTeX\)
    • Python
  4. Goodies
  5. Org-mode: markdown on steroid
  6. A bit of Emacs Lisp (power to the people)

What Emacs is ?

Definition

Emacs is a User Interface (UI) to manipulate text

Theorem

All what X is able, Emacs does it too.

Proof: Let as an exercise after this presentation.

Corollary

l58yl5wh6fk11.png

(counter) Example

Start Emacs

How you feel when you start

image

image

Weirdness comes from Oldness

Initial release: 1976; 42 years ago (timeline)

  • Macintosh : 1984

    First mass-market computer with GUI and mouse

  • Windows 1.0: 1985

    (claim starting dev. GUI in 1981)

A window is not a window

  • "Frames" are to Emacs what windows are to everything else
  • "Windows" are subdivisions of frames
  • "Buffer" hold the content of a file

    "Open a file" means open a buffer that "visits" that file

SAJFO.jpg

Control C is not Copy

C-x C-f means

press Control and x then Control and f

M-x means press Alt and x

Copy C-w
Paste C-y
Cut M-w
Find C-s
Find-Replace M-%

Ah Bon?

Capture-d%E2%80%99%C3%A9cran-2011-12-21-%C3%A0-09.54.32.png

Keep Calm and Practise

garyplayer.jpg

Some basics

Ressources

sachachua.com/blog

How-to-Learn-Emacs-v2-Large.png

sachachua.com/blog 2

How-to-Learn-Emacs8.png

Refcard

gnuemacsref.png https://www.gnu.org/software/emacs/refcards/pdf/refcard.pdf

A demo speaks better than words

@simon: check your cheatsheet :-)

Summary

"Habit is habit, and not to be flung out of the window by any man, but coaxed downstairs a step at a time." – Mark Twain

Remember

  • Keyboard driven (source of power)
  • Command is first class citizen
  • one command should be binded to one Keymap
describe-key C-h k
describe-function C-h f
describe-mode C-h m
isearch-forward C-s

Remember 2

find-file C-x C-f
set-mark C-SPC
kill-ring-save M-w
kill-region C-w
yank C-y

I DO NOT recommand CUA-mode (windows-like shortcuts)

Remember 3

Muscle one small step after another

500_F_93860366_VpWf1VVnEGzVQwCeHTOsPLrvNHEfGOdb.jpg

How to configure

The configuration is driven by

$HOME/.emacs.d/init.el

Basics

Basics 1

Set variables

;; to see the life in colours
(global-font-lock-mode 1)
;; to remove the icons bar
(tool-bar-mode 0)
;; what?!? who use the latteral bar to scroll ? :-)
(scroll-bar-mode 0)

Set other variables

;; remove the initial starting message
(setq inhibit-startup-message t)

;; delete the *scratch* initial message
(setq initial-scratch-message nil)

@simon: quick demo-it

Basics 2

Conditional set

Rename commands

;; simplify the question-answer process
(defalias 'yes-or-no-p 'y-or-n-p)

;; M-x mode-* instead of the long name
(defalias 'mode-whitespace 'whitespace-mode)
(defalias 'mode-highlight 'global-hl-line-mode)
;; change the default file manager
(defalias 'list-directory 'dired)

Basics 3

Bind

;; set global shortcuts
(global-set-key [?\C-k] 'kill-whole-line)
(global-set-key [?\C-$] 'ispell-region)
;; special binding for specific mode (here dired)
(define-key dired-mode-map (kbd "E") 'dired-toggle-read-only))

Hook

;; delete dirty spaces
(add-hook 'before-save-hook 'delete-trailing-whitespace)

A journey of a thousand miles begins with a single step

a96fdbb368f4b75e4feccbece41721f2.jpg

Packages

"The coolest feature of Emacs is all the available packages." – Doctor Who, the coolest nerd ever

Repos: ELPA and MELPA

Built-in since Emacs 24

(require 'package)
(setq package-enable-at-startup nil)
(add-to-list 'package-archives
             ;; `use-package' is not in ELPA, as many more ;-)
             '("melpa" . "http://melpa.org/packages/"))
(add-to-list 'package-archives
               ;; Add org-plus-contrib
               '("org" . "http://orgmode.org/elpa/"))
(package-initialize)

M-x package-list-packages

use-package

;; boostrap `use-package' by John Wiegley
(unless (package-installed-p 'use-package)
  (package-refresh-contents)
  (package-install 'use-package))
;; load it
(require 'use-package)

Example:

;; change the default file manager
(use-package dired
  :defer t
  :init
  (defalias 'list-directory 'dired)
  :config
  (define-key dired-mode-map (kbd "E") 'dired-toggle-read-only))

use-package 2

;; example of Julia configuration
(use-package ess
  :ensure t
  :defer t
  ;; :init                                  ; example with non-standard loc.
  ;; (setq inferior-julia-program
  ;;    "/Applications/Julia-0.6.app/Contents/Resources/julia/bin/julia")
  :mode (("\\.jl\\'" . ess-julia-mode))
  :defines ess-indent-offset            ; silent warning
  :config
  (require 'ess-julia)
  (require 'ess-utils)
  (setq ess-eval-visibly-p nil)
  (setq ess-use-eldoc'script-only))

Yes, it's awesome!

that-awesome-moment-doctor-who-and-the-ta-r-d-is-when-you-34353243.png

Two live Examples

@simon: do not forget to: sh config-me.sh

@simon: do not forget to turn-on command-log-mode :-)

latex-mode

# apt-get install aspell

python-mode

# apt-get install virtualenv ipython
$ pip install jedi epc pylint --user

More features (demo)

Another killer feature: Org-mode

org-mode is…

"Awesome!" – John Kitchin (video)

  • Markup language (see the backstage of this presentation)
  • Note-taking utility
  • TODO lists maintainer
  • Planner, agenda
  • Tables editor
  • Helper to manage your projects (CEO explains his experience)
  • included by default since Emacs 22 (2006)

Org is vast…

start small!

"Don't try to set up the 'final' task managing system from the start. Because you have no idea yet what your system should look like. […] Start by creating and managing a small TODO list and then develop your own system as the needs arises." – Carsten Dominik (creator of org)

My experience

  • Learn a couple of its capabilities
  • Integrate them into your workflow
  • You’ll find yourself thinking, "Hum?, it would be nice if I could X"
  • Then check the manual, browse the web

… and, in fact, Org can do X

Demo

table and \(\LaTeX\)

  • Write table with Org-mode (easy!)
  • Export to .tex: C-c C-e C-b l l (body only)
  • Use \input{}

(or epxort to buffer C-c C-e C-b l L and copy/paste)

DEMO!

makefile

all: doc.tex table1.tex table2.tex
        pdflatex doc.tex

%.tex: %.org
         emacs -batch -q $< --eval="(org-latex-export-to-latex nil nil t t)"

clean:
        -rm table?.tex
        -rm doc.{aux,log}

Ressources

Computational Document / Reproducible workflow

"Buzzword – What'chu talkin' 'bout, Willis?" – Arnold

Emacs Lisp

Lisp? Really?!?

Lisp is (Lots of ((Irritating, Spurious) (Parentheses)))

lisp_cycles.png

Lisp? tell me more…

First appeared 1958; 60 years ago

"A bad workman blames his tools." – Barack Obama

"Another language is a new life." – Persian proverb

"Learn a language, and you will avoid a war." – Arab proverb

Hacker (Paul Graham) citing Hacker (Eric S. Raymond): More here, here or here.

Finally, the truly serious hacker should consider learning Lisp:

Lisp is worth learning for the profound enlightenment experience you will have when you finally get it; that experience will make you a better programmer for the rest of your days, even if you never actually use Lisp itself a lot.

This is the same argument you tend to hear for learning Latin.

More here, here or here.

True!

giphy-facebook_s.jpg

Old pot, good jam

Emacs Lisp (ELisp) Ressources

Basics

;; work with parenthesis
(show-paren-mode 1)       ; highlight matching parens
(electric-pair-mode 1)    ; open-close parens
  • Start REPL

    M-x ielm

  • Eval in mini-buffer: M-:
  • Buffer with M-x emacs-lisp-mode

    C-x C-e

    end of S-expression

Basics 1

;; Declare variable
(setq x 1)

;; Compute (infix)
(+ x 42)
(* x 4.2)

;; List (linked)
(setq ll (list 1 2 3 4))
;; First element (head)
(car ll)
;; Rest (tail)
(cdr ll)
;; Concatenate lists
(append ll (list 11 22 33 44 55) (list "a" "bb" "ccc"))

Basics 2

Function

(defun hello-world (name)
  "Simply Hello World function.

Do stuff.
And other stuff.
The argument name provides a name to helloing."
  (message "Hello World: %s!" name))

Anonymous function

(lambda (name)
  (message "Hello: %s!" name))

Call/Apply a function: (hello-world "Dude")

Basics 3

(defun ask-at-point (&optional i-know)
  "Feel free to ask everything."
  (interactive "P")
  (if i-know (message "Quick Answer.")
   (browse-url
    (concat "https://www.ddg.gg/?q=emacs+" (thing-at-point 'symbol)))))

All the Lisp power at your fingertips

minion-awesome-quote.jpg

Emacs is just a Lisp machine

  • Few built-in functions coded in C

    C-h f list or car, cdr, etc.

  • And lot of Emacs Lisp ones

    C-h f ibuffer or find-file, dired, org, etc.

Read manual then read code on ELPA and MELPA

e.g., eshell

MIT museum

1836px-MIT_lisp_machine.jpg

Simple functions

Upcase a word

(defun eg/upcase-word ()
  "Change from any word to UPPERCASE."
  (interactive)
  (let ((beg (progn
               (backward-word)
               (point)))
        (end (progn
               (forward-word)
               (point))))
        (upcase-region beg end)))

Next!

(global-set-key (kbd "M-u") 'my/upcase-word)

What about Downcase ?

(defun eg/downcase-word ()
  "Change from any word to DOWNCASE."
  (interactive)
  (let ((beg (progn
               (backward-word)
               (point)))
        (end (progn
               (forward-word)
               (point))))
        (downcase-region beg end)))

Pattern ?

amazing1.png

Refactor

(defun eg/change-case-word (fun)
  "Generic function to change the case of a word.

When the `point' is somewhere in word, first get the `backward-word'
position, second get the `forward-line' position, and last apply FUN
to these both."
  (interactive)
  (let ((beg (progn
               (backward-word)
               (point)))
        (end (progn
               (forward-word)
               (point))))
        (funcall fun beg end)))

Simple, isn't?

(defun eg/capitalize-word ()
  "Remap of `capitalize-word'."
  (interactive)
  (eg/change-case-word 'capitalize-region))

(defun eg/upcase-word ()
  "Remap of `upcase-word'."
  (interactive)
  (eg/change-case-word 'upcase-region))

(defun eg/downcase-word ()
  "Remap of `downcase-word'."
  (interactive)
  (eg/change-case-word 'downcase-region))

;; add quick bindings to the new nice functions
(global-set-key (kbd "M-c") 'eg/capitalize-word)
(global-set-key (kbd "M-u") 'eg/upcase-word)
(global-set-key (kbd "M-l") 'eg/downcase-word)

Count Words

Tutorial from VSCode to extend it

https://code.visualstudio.com/docs/extensions/example-word-count

Come on! It is not simpler than X.

(me: javascript is aweful)

Hum?

M-=

or

M-x count-words-region

Ok, do naive way!

(defun eg/count-words-region (start end)
  "Count words in the selected region.

Worse than `count-words-region'."
  (interactive "r")
  (let ((count-words->how-many 0))
    (goto-char start)
    (while (< (point) end)
      (if (forward-word 1)
          (setq count-words->how-many (1+ count-words->how-many))))

    (message "Region has %d words." count-words->how-many)
    count-words->how-many))

M-x eg/count-words-region

mini-fem solver with ELisp

Let only consider:

  • list and the 4 arithmitic operations on integers and floats
  • functions and recurcivity

\[\cos(x) = \textrm{real}~ \sum_n \frac{ix}{n!}\]

\begin{array}{ccl} u^{\prime\prime} + k^2 u &=& 0 \\ & \textrm{with} & u(0) = 1 \\ & \textrm{and} & u^\prime(1)=iku(1) \end{array}

Demo

see https://github.com/zimoun/elfem1

A bit of elisp history

ELisp is not truly a Lisp?

photo-20161216233158566.jpg

Facts

What Emacs is ?

220px-Unico_Anello.png

Start now!

galactic_explorer.en.png

Alternative Reality

Key of Success

  • Master a tool needs large amount of work

    think all the maths you learn before complete non-trivial proofs

  • Master a tool is not easy; otherwise it is a lie

    think nunchaku practise to be as efficient as Bruce Lee

Why computing tools should be different ?

Be good at something is long

My personal conclusion

Why flamewar editor ?

  • Our daily life is interacting with computers
  • We are all differently wired
  • Cognition is not straightforward

Editor should be freedom, not constraint and pain.

… because Emacs is the best! ;-)

cartoon92.png

Happy end?

Emacs is first at productive procrastination :-)

phd010408s.gif