being productive with emacsweb.psung.name/emacs/2007/emacs-slides-2.pdf · similar to lisp and...

50
Being Productive With Emacs Part 2 Phil Sung [email protected] http://stuff.mit.edu/iap/emacs Special thanks to Piaw Na and Arthur Gleckler

Upload: others

Post on 15-Nov-2020

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Being Productive With Emacsweb.psung.name/emacs/2007/emacs-slides-2.pdf · Similar to lisp and scheme Use *scratch* buffer as a temporary work space – or activate lispinteractionmode

Being Productive With EmacsPart 2

Phil Sungsipb­iap­[email protected]

http://stuff.mit.edu/iap/emacsSpecial thanks to Piaw Na and Arthur Gleckler

Page 2: Being Productive With Emacsweb.psung.name/emacs/2007/emacs-slides-2.pdf · Similar to lisp and scheme Use *scratch* buffer as a temporary work space – or activate lispinteractionmode

Previously...● Emacs as an editor

– Useful features– Motifs in emacs– Learning more

Page 3: Being Productive With Emacsweb.psung.name/emacs/2007/emacs-slides-2.pdf · Similar to lisp and scheme Use *scratch* buffer as a temporary work space – or activate lispinteractionmode

Previously...● Acquiring Emacs

– Already installed on Athena (v.21)– Ubuntu: emacs­snapshot­gtk package (v.22)– Gentoo: emacs­cvs package (v.22)– Windows: EmacsW32 installer

Page 4: Being Productive With Emacsweb.psung.name/emacs/2007/emacs-slides-2.pdf · Similar to lisp and scheme Use *scratch* buffer as a temporary work space – or activate lispinteractionmode

Previously...● Learning more about emacs

– Look up an existing function or key● C­h f, C­h k

– Apropos (search for commands)● C­h a

– Help about help facilities● C­h C­h

Page 5: Being Productive With Emacsweb.psung.name/emacs/2007/emacs-slides-2.pdf · Similar to lisp and scheme Use *scratch* buffer as a temporary work space – or activate lispinteractionmode

Previously...● Learning more about Emacs

– Emacs tutorial● C­h t

– Emacs manual● M­x info, select emacs

Page 6: Being Productive With Emacsweb.psung.name/emacs/2007/emacs-slides-2.pdf · Similar to lisp and scheme Use *scratch* buffer as a temporary work space – or activate lispinteractionmode

Previously...● If you're stuck...

– Cancel: C­g– Undo: C­/ or C­_

Page 7: Being Productive With Emacsweb.psung.name/emacs/2007/emacs-slides-2.pdf · Similar to lisp and scheme Use *scratch* buffer as a temporary work space – or activate lispinteractionmode

Today● Customizing Emacs● Elisp basics● Defining a new command

Page 8: Being Productive With Emacsweb.psung.name/emacs/2007/emacs-slides-2.pdf · Similar to lisp and scheme Use *scratch* buffer as a temporary work space – or activate lispinteractionmode

Customizing Emacs● Almost every aspect of the editor can be 

customized– More fine­grained control than major/minor modes– In general, use M­x eval­expression– Show trailing whitespace on lines(setq show­trailing­whitespace t)

– Show column numbers in mode line(column­number­mode t)

Page 9: Being Productive With Emacsweb.psung.name/emacs/2007/emacs-slides-2.pdf · Similar to lisp and scheme Use *scratch* buffer as a temporary work space – or activate lispinteractionmode

Customizing Emacs● Customization buffer: M­x customize

– Browse, point and click for customization options– No elisp necessary, but capabilities are limited

Page 10: Being Productive With Emacsweb.psung.name/emacs/2007/emacs-slides-2.pdf · Similar to lisp and scheme Use *scratch* buffer as a temporary work space – or activate lispinteractionmode

Review: running elisp code● Evaluate elisp with M­x eval­expression

– Example function call: (+ 2 4 6)● Get or set variables:

– sentence­end­double­space– (setq inhibit­startup­message t)

● Sometimes these correspond to commands:– (forward­char) is same as C­f orM­x forward­char

Page 11: Being Productive With Emacsweb.psung.name/emacs/2007/emacs-slides-2.pdf · Similar to lisp and scheme Use *scratch* buffer as a temporary work space – or activate lispinteractionmode

Writing elisp code● Use the *scratch* buffer for playing with 

elisp– C­x C­e to evaluate– Move to ~/.emacs for permanent changes

Page 12: Being Productive With Emacsweb.psung.name/emacs/2007/emacs-slides-2.pdf · Similar to lisp and scheme Use *scratch* buffer as a temporary work space – or activate lispinteractionmode

Your .emacs file● C­x C­f ~/.emacs

● Elisp code here is loaded when Emacs starts– Run any valid lisp code here– Set variables, keybindings to your liking– Define your own commands

● M­x customize works by adding code to your .emacs

Page 13: Being Productive With Emacsweb.psung.name/emacs/2007/emacs-slides-2.pdf · Similar to lisp and scheme Use *scratch* buffer as a temporary work space – or activate lispinteractionmode

Why bother with elisp?● Macros record and play back key sequences

– Start recording macro: C­x (– Stop recording macro: C­x )– Execute last macro: C­x e

● Great for automating tedious tasks– C­x e e e ...– C­u 100 C­x e

Page 14: Being Productive With Emacsweb.psung.name/emacs/2007/emacs-slides-2.pdf · Similar to lisp and scheme Use *scratch* buffer as a temporary work space – or activate lispinteractionmode

Macro example6.00 12 programming

6.001 15 sicp

6.002 15 circuits

6.003 15 linear­systems

6.004 15 digital

6.011 12 signal­proc

6.00 programming

6.001 sicp

6.002 circuits

6.003 linear­systems

6.004 digital

6.011 signal­proc

M-f M-f M-d C-n C-a repeatedlyLet's remove this

column

Page 15: Being Productive With Emacsweb.psung.name/emacs/2007/emacs-slides-2.pdf · Similar to lisp and scheme Use *scratch* buffer as a temporary work space – or activate lispinteractionmode

Why elisp?● Macros only repeat canned key sequences● Sometimes you need:

– Calculations– Control flow– User interaction– Additional features– Maintainability

Page 16: Being Productive With Emacsweb.psung.name/emacs/2007/emacs-slides-2.pdf · Similar to lisp and scheme Use *scratch* buffer as a temporary work space – or activate lispinteractionmode

Elisp is...● an implementation language● a customization language● an extension language

Page 17: Being Productive With Emacsweb.psung.name/emacs/2007/emacs-slides-2.pdf · Similar to lisp and scheme Use *scratch* buffer as a temporary work space – or activate lispinteractionmode

Elisp for implementation● Example: M­x calc

– C­h f to see where calc is defined– RET on filename in help buffer to view source code

Page 18: Being Productive With Emacsweb.psung.name/emacs/2007/emacs-slides-2.pdf · Similar to lisp and scheme Use *scratch* buffer as a temporary work space – or activate lispinteractionmode

Elisp for customization● Set variables and options● Persistent customizations can go in .emacs● Compare to M­x customize

Page 19: Being Productive With Emacsweb.psung.name/emacs/2007/emacs-slides-2.pdf · Similar to lisp and scheme Use *scratch* buffer as a temporary work space – or activate lispinteractionmode

Elisp for extensions● Alter behavior of existing commands● Define your own commands, functions● Define new modes

Page 20: Being Productive With Emacsweb.psung.name/emacs/2007/emacs-slides-2.pdf · Similar to lisp and scheme Use *scratch* buffer as a temporary work space – or activate lispinteractionmode

Why elisp?● It's the implementation language● Dynamic environment

– No need to recompile/restart emacs– Easily override or modify existing behaviors

● Simple one­liners are sufficient to do a lot!

Page 21: Being Productive With Emacsweb.psung.name/emacs/2007/emacs-slides-2.pdf · Similar to lisp and scheme Use *scratch* buffer as a temporary work space – or activate lispinteractionmode

Getting started● Similar to lisp and scheme● Use *scratch* buffer as a temporary work space

– or activate lisp­interaction­mode anywhere

– C­x C­e after an expression to evaluate it– or use M­x eval­expression (M­:)

● Example: setting a variable– (setq undo­limit 100000)

Page 22: Being Productive With Emacsweb.psung.name/emacs/2007/emacs-slides-2.pdf · Similar to lisp and scheme Use *scratch* buffer as a temporary work space – or activate lispinteractionmode

Getting started● Evaluating an expression can mean

– Performing some computation/action– Displaying the value of a variable– Defining a function for later use

Page 23: Being Productive With Emacsweb.psung.name/emacs/2007/emacs-slides-2.pdf · Similar to lisp and scheme Use *scratch* buffer as a temporary work space – or activate lispinteractionmode

Basic elisp● These are expressions (“atoms”)

– 15– “Error message”– best­value

● These are also (“compound”) expressions– (+ 1 2)– (setq include­all­files t)

Page 24: Being Productive With Emacsweb.psung.name/emacs/2007/emacs-slides-2.pdf · Similar to lisp and scheme Use *scratch* buffer as a temporary work space – or activate lispinteractionmode

Setting variables● Set variable by evaluating(setq undo­limit 100000)

– i.e. do M­: (setq ...) [RET]● Read variable by evaluating undo­limit

– i.e. do M­: undo­limit [RET]● Find out more about any variable with C­h v

Page 25: Being Productive With Emacsweb.psung.name/emacs/2007/emacs-slides-2.pdf · Similar to lisp and scheme Use *scratch* buffer as a temporary work space – or activate lispinteractionmode

Common customizations● Configuration options● Set your own keybindings

Page 26: Being Productive With Emacsweb.psung.name/emacs/2007/emacs-slides-2.pdf · Similar to lisp and scheme Use *scratch* buffer as a temporary work space – or activate lispinteractionmode

Configuration options● Some customizations are done by setting 

variables– (setq undo­limit 100000)

– (setq enable­recursive­minibuffers t)

– (setq fill­column 80)

Page 27: Being Productive With Emacsweb.psung.name/emacs/2007/emacs-slides-2.pdf · Similar to lisp and scheme Use *scratch* buffer as a temporary work space – or activate lispinteractionmode

Configuration options● Other options are exposed as their own 

functions– (menu­bar­mode nil) (Hide menu bar)– (icomplete­mode)

(Show completions continuously)– (server­start) (Start emacs server)

Page 28: Being Productive With Emacsweb.psung.name/emacs/2007/emacs-slides-2.pdf · Similar to lisp and scheme Use *scratch* buffer as a temporary work space – or activate lispinteractionmode

More about variables● Many variables are boolean

– Usually a distinction is only made between nil and non­nil values (e.g. t)

● Look in function documentation to see which variables can alter the function's behavior

● C­h v to get documentation for a variable

Page 29: Being Productive With Emacsweb.psung.name/emacs/2007/emacs-slides-2.pdf · Similar to lisp and scheme Use *scratch* buffer as a temporary work space – or activate lispinteractionmode

Key bindings● We've seen two ways to invoke commands

– C­x n w (key invocation)– M­x widen (M­x invocation, or invoking by name)

● Emacs binds each key to a command in a keymap– A keymap can be specific to a mode or feature– Bindings may be changed at any time

Page 30: Being Productive With Emacsweb.psung.name/emacs/2007/emacs-slides-2.pdf · Similar to lisp and scheme Use *scratch* buffer as a temporary work space – or activate lispinteractionmode

Customizing key bindings● (global­set­key  [f2]  'split­window­horizontally)

● (global­set­key "\C­o" 'find­file)● (global­set­key "\C­x\C­\\"                'next­line)

binds to C-x C-\

Page 31: Being Productive With Emacsweb.psung.name/emacs/2007/emacs-slides-2.pdf · Similar to lisp and scheme Use *scratch* buffer as a temporary work space – or activate lispinteractionmode

Customizing key bindings● A binding can be set to apply only in a particular mode

– (define­key text­mode­map            "\C­cp"            'backward­paragraph)

binds to C-c p

Page 32: Being Productive With Emacsweb.psung.name/emacs/2007/emacs-slides-2.pdf · Similar to lisp and scheme Use *scratch* buffer as a temporary work space – or activate lispinteractionmode

Keybindings● What keys can you assign?

– Reserved for users:● C­c [letter]

– Reserved for major and minor modes:● C­c C­[anything]● C­c [punctuation]● C­c [digit]

Page 33: Being Productive With Emacsweb.psung.name/emacs/2007/emacs-slides-2.pdf · Similar to lisp and scheme Use *scratch* buffer as a temporary work space – or activate lispinteractionmode

Calling commands● Any command you use can be invoked 

programmatically by elisp– Often, M­x my­function is accessible as(my­function)

– For key commands, look up the full name first● Use commands as building blocks for more 

complex behaviors

Page 34: Being Productive With Emacsweb.psung.name/emacs/2007/emacs-slides-2.pdf · Similar to lisp and scheme Use *scratch* buffer as a temporary work space – or activate lispinteractionmode

Hooks● Specify a custom command to run whenever a 

particular event occurs, e.g.– when a particular mode is entered– when any file is loaded or saved– when a file is committed to CVS

Page 35: Being Productive With Emacsweb.psung.name/emacs/2007/emacs-slides-2.pdf · Similar to lisp and scheme Use *scratch* buffer as a temporary work space – or activate lispinteractionmode

Hooks● (add­hook  'vc­checkin­hook  '(lambda ()    (send­email­to­group)))

Page 36: Being Productive With Emacsweb.psung.name/emacs/2007/emacs-slides-2.pdf · Similar to lisp and scheme Use *scratch* buffer as a temporary work space – or activate lispinteractionmode

Hooks● (add­hook 'java­mode­hook  '(lambda () (setq indent­tabs­mode t)              (setq tab­width 4)              (set­fill­column 80)))

Page 37: Being Productive With Emacsweb.psung.name/emacs/2007/emacs-slides-2.pdf · Similar to lisp and scheme Use *scratch* buffer as a temporary work space – or activate lispinteractionmode

Hooks● General template

– (add­hook 'name­of­hook'(lambda () (do­this)            (do­that)            (do­the­other­thing)))

Page 38: Being Productive With Emacsweb.psung.name/emacs/2007/emacs-slides-2.pdf · Similar to lisp and scheme Use *scratch* buffer as a temporary work space – or activate lispinteractionmode

Hooks● To find available hooks:

– Every major mode has a hook– M­x apropos­variable and search for 

"hook"

Page 39: Being Productive With Emacsweb.psung.name/emacs/2007/emacs-slides-2.pdf · Similar to lisp and scheme Use *scratch* buffer as a temporary work space – or activate lispinteractionmode

Defining your own functions● (defun function­name (arg1 arg2 ...)  “Description of function”  (do­this)  (do­that)  (do­the­other­thing))

● Invoke with:(function­name one two ...)

Page 40: Being Productive With Emacsweb.psung.name/emacs/2007/emacs-slides-2.pdf · Similar to lisp and scheme Use *scratch* buffer as a temporary work space – or activate lispinteractionmode

Strategy for making functions● Find key commands that would have desired 

result● Replace key commands with elisp function calls

Page 41: Being Productive With Emacsweb.psung.name/emacs/2007/emacs-slides-2.pdf · Similar to lisp and scheme Use *scratch* buffer as a temporary work space – or activate lispinteractionmode

A simple function● (defun capitalize­backwards ()  "Capitalize last letter of a word."  (backward­word)  (forward­word)  (backward­char)  (capitalize­word 1))

Page 42: Being Productive With Emacsweb.psung.name/emacs/2007/emacs-slides-2.pdf · Similar to lisp and scheme Use *scratch* buffer as a temporary work space – or activate lispinteractionmode

Not every function is a command● Functions need arguments:

– (defun square (x) (* x x))(square 5) ==> 25

● Commands don't say what arguments to substitute– M­x square ==> ??

● Interactive specification needed to say what arguments to fill in

Page 43: Being Productive With Emacsweb.psung.name/emacs/2007/emacs-slides-2.pdf · Similar to lisp and scheme Use *scratch* buffer as a temporary work space – or activate lispinteractionmode

A simple command● (defun capitalize­backwards ()  "Capitalize last letter of a word."  (interactive)  (backward­word)  (forward­word)  (backward­char)  (capitalize­word 1))

Page 44: Being Productive With Emacsweb.psung.name/emacs/2007/emacs-slides-2.pdf · Similar to lisp and scheme Use *scratch* buffer as a temporary work space – or activate lispinteractionmode

Problem● This command moves the cursor

– This can be distracting if the user isn't expecting it

Page 45: Being Productive With Emacsweb.psung.name/emacs/2007/emacs-slides-2.pdf · Similar to lisp and scheme Use *scratch* buffer as a temporary work space – or activate lispinteractionmode

Restoring the cursor● (defun capitalize­backwards ()  "Capitalize last letter of a word."  (interactive)  (save­excursion    (backward­word)    (forward­word)    (backward­char)    (capitalize­word 1)))

Page 46: Being Productive With Emacsweb.psung.name/emacs/2007/emacs-slides-2.pdf · Similar to lisp and scheme Use *scratch* buffer as a temporary work space – or activate lispinteractionmode

Useful functions● (point)● (point­max)● (current­buffer)● (message “This is the answer: %s”         answer)

Page 47: Being Productive With Emacsweb.psung.name/emacs/2007/emacs-slides-2.pdf · Similar to lisp and scheme Use *scratch* buffer as a temporary work space – or activate lispinteractionmode

Local variables● (let ((a new­value)      (b another­value)      ...)    (do­something)    (do­something­else))

Page 48: Being Productive With Emacsweb.psung.name/emacs/2007/emacs-slides-2.pdf · Similar to lisp and scheme Use *scratch* buffer as a temporary work space – or activate lispinteractionmode

Example: counting word length● (defun word­length ()  "Prints the length of a word."  (interactive)  (save­excursion    (backward­word)    (let ((a (point)))      (forward­word)      (let ((b (point)))        (message "Word is %d letters"                 (­ b a))))))

Page 49: Being Productive With Emacsweb.psung.name/emacs/2007/emacs-slides-2.pdf · Similar to lisp and scheme Use *scratch* buffer as a temporary work space – or activate lispinteractionmode

Getting help with elisp● Manuals

– M­x info, then select elisp or eintr● Learning by example

– Function documentation (C­h f or C­h k) always gives a link to the function's source code

Page 50: Being Productive With Emacsweb.psung.name/emacs/2007/emacs-slides-2.pdf · Similar to lisp and scheme Use *scratch* buffer as a temporary work space – or activate lispinteractionmode

Next week...● Control flow● User interaction● Commands for manipulating text● Other extension methods