Application emacs

From campisano.org
Jump to navigation Jump to search

Emacs

Install

  • from Debian-like O.S.:
# for terminal version
apt-get install emacs25-nox
# or for X version
apt-get install emacs25

shared/opt install schema v1.2

su -
#### as root
# cleanup
apt-get autoremove --purge emacs\* emacsen-common+
# dependencies for terminal version
apt-get install curl tar gzip gcc make libtinfo-dev zlib1g-dev libssl1.0-dev
# or dependencies for X version
apt-get install curl tar gzip gcc make libtinfo-dev zlib1g-dev libssl1.0-dev libtool texinfo build-essential xorg-dev libgtk-3-dev libjpeg-dev libncurses5-dev libdbus-1-dev libgif-dev libtiff-dev libm17n-dev libpng-dev librsvg2-dev libotf-dev libgnutls28-dev libxml2-dev libxpm-dev
# binary
DESTINATION="/home/shared/opt/software"                 # Edit when needed
VERSION="26.3"                                          # Edit when needed
URL="ftp://ftp.gnu.org/pub/gnu/emacs/emacs-${VERSION}.tar.gz"
umask 0027
mkdir -p "${DESTINATION}/tmp"
cd "${DESTINATION}/tmp"
curl -C - -kLO "${URL}"
tar -xzf "emacs-${VERSION}.tar.gz"
cd "emacs-${VERSION}"
# configure for terminal version
CFLAGS="-g0 -O3 -march=native -s" ./configure --prefix=${DESTINATION}/emacs-${VERSION} -with-x-toolkit=no --without-x --without-all --with-zlib --with-gnutls --without-gnutls --with-modules --with-threads --with-compress-install --with-file-notification=yes
# or configure for X version
CFLAGS="-g0 -O3 -march=native -s" ./configure --prefix=${DESTINATION}/emacs-${VERSION} -with-x-toolkit=gtk3 --with-x --with-zlib --with-gnutls --with-modules --with-threads --with-compress-install --with-file-notification=yes
make
make install
cd "${DESTINATION}"
chown -R root:users "emacs-${VERSION}"
find "emacs-${VERSION}" -type d -exec chmod a-s,u+rwx,g+rx,g-w,o-rwx {} \;
find "emacs-${VERSION}" -type f -exec chmod a-s,u+rw,g+r,g-w,o-rwx {} \;
rm -rf tmp emacs
ln -s "emacs-${VERSION}" emacs
exit
#### as final user
SOFTWARE_PATH="/home/shared/opt/software"               # Edit when needed
"${SOFTWARE_PATH}/emacs/bin/emacs"

Configure

Emacs as default git difftool

git config --global diff.tool emacs
git config --global difftool.prompt false
# the result of the following command is: cmd = emacs -nw --eval \"(progn (setq vc-handled-backends ()) (ediff-files \\\"$LOCAL\\\" \\\"$REMOTE\\\"))\"
git config --global difftool.emacs.cmd 'emacs -nw --eval "(progn (setq vc-handled-backends ()) (ediff-files \"$LOCAL\" \"$REMOTE\"))"'

Emacs as default git mergetool

git config --global merge.tool emacs
git config --global mergetool.prompt false
# the result of the following command is: cmd =  emacs -nw --eval \"(progn (setq vc-handled-backends ()) (ediff-merge-files-with-ancestor \\\"$LOCAL\\\" \\\"$REMOTE\\\" \\\"$BASE\\\" nil \\\"$MERGED\\\"))\"
git config --global mergetool.emacs.cmd 'emacs -nw --eval "(progn (setq vc-handled-backends ()) (ediff-merge-files-with-ancestor \"$LOCAL\" \"$REMOTE\" \"$BASE\" nil \"$MERGED\"))"'

Improve GDB print of STL containers

  • Install libstdc++6
sudo apt-get install libstdc++6
  • libstdc++6 provide a version of python prettyprinter like function

to be used with gdb, however if you got errors like

"Python Exception <type 'exceptions.ValueError'> Cannot find type"

you may want to install a more recent version:

# download
svn co svn://gcc.gnu.org/svn/gcc/trunk/libstdc++-v3/python ~/gdb/python
# configure
cat >> ~/.gdbinit << EOF
python
import os
import sys
sys.path.insert(0, os.environ['HOME'] + '/gdb/python')
from libstdcxx.v6.printers import register_libstdcxx_printers
register_libstdcxx_printers (None)
end
EOF
  • Configure GDB print behaviour
cat >> ~/.gdbinit << EOF

#
# C++ related beautifiers (optional)
#
set print pretty on
set print object on
set print static-members on
set print vtbl on
set print demangle on
set demangle-style gnu-v3
set print sevenbit-strings off
EOF

C++ navigation and completation with cquery

su -
#### as root
# dependencies
apt-get install g++ cmake git
# binary
DESTINATION="/home/shared/opt/software"                 # Edit when needed
VERSION="70c755b2e390d3edfb594a84a7531beb26b2bc07"      # Edit when needed
mkdir -p "${DESTINATION}/tmp"
cd "${DESTINATION}/tmp"
git clone https://github.com/cquery-project/cquery.git .
git checkout "${VERSION}"
git submodule update --init
mkdir build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="/home/shared/opt/software/cquery-${VERSION}" -DCMAKE_EXPORT_COMPILE_COMMANDS=YES
cmake --build .
cmake --build . --target install
cd "${DESTINATION}"
rm -rf tmp cquery
ln -s "cquery-${VERSION}" cquery
chown -R root:users "cquery-${VERSION}"
find "cquery-${VERSION}" -type d -exec chmod u+rwx,g+rx,g-w,o-rwx {} \;
find "cquery-${VERSION}" -type f -exec chmod u+rw,g+r,g-w,o-rwx {} \;
exit

(use-package lsp-mode
  :ensure t
  :config
  (add-hook 'prog-mode-hook #'lsp)
  )

(use-package lsp-ui
  :ensure t
  :config
  (add-hook 'lsp-mode-hook #'lsp-ui-mode)
  )

(use-package company
  :ensure t
  :config
  (global-company-mode))
  
(use-package company-lsp
  :ensure t
  :config
  (setq company-lsp-enable-recompletion t)
  (add-to-list 'company-backends 'company-lsp)
  )

Java navigation and completation with Eclipse JDT Language Server

# dependencies
su -c "apt-get install curl tar gzip" # NOTE: jdk and maven are required too
# binary
DESTINATION="/home/shared/opt/software"                 # Edit when needed
VERSION="d30acc4c7cf961a2234b19b5b9e8dfe3d7402987"      # Edit when needed
URL="https://github.com/eclipse/eclipse.jdt.ls/archive/d30acc4c7cf961a2234b19b5b9e8dfe3d7402987.zip"
umask 0027
mkdir tmp
cd tmp
curl -C - -kLO "${URL}"
unzip "${VERSION}.zip"
cd "eclipse.jdt.ls-${VERSION}"
./mvnw clean verify
cd ../..
su -c "chown -R root:users tmp;\
 find tmp -type d -exec chmod a-s,u+rwx,g+rx,g-w,o-rwx {} \;;\
 find tmp -type f -exec chmod a-s,u+rw,g+r,g-w,o-rwx {} \;;\
 mv tmp/eclipse.jdt.ls-${VERSION} ${DESTINATION};\
 rm -f ${DESTINATION}/eclipse.jdt.ls;\
 ln -s eclipse.jdt.ls-${VERSION} ${DESTINATION}/eclipse.jdt.ls;\
 chmod 2777 ${DESTINATION}/eclipse.jdt.ls/org.eclipse.jdt.ls.product/target/repository/config_linux"
  • configure
(use-package lsp-java
  :ensure t
  :after lsp
  :config
  (setq-default lsp-java-server-install-dir "/home/shared/opt/software/eclipse.jdt.ls/org.eclipse.jdt.ls.product/target/repository/")

  (require 'dap-java)

  ;; (require 'lsp-java-boot)
  ;; ;; to enable the lenses
  ;; (add-hook 'lsp-mode-hook #'lsp-lens-mode)
  ;; (add-hook 'java-mode-hook #'lsp-java-boot-lens-mode)

  (add-hook 'java-mode-hook #'lsp)
  (add-hook 'java-mode-hook 'flycheck-mode)
  (add-hook 'java-mode-hook 'company-mode)
  )

Emacs commands

Modifier keys

Defaults:

  • C stands for the <Control> modifier, key [Ctrl]
  • M stands for the <Meta> modifier, key [Alt] usually
  • S stands for the <Shift> modifier, ket [Shift]

Basic Emacs commands

Files

Stop current command            C-g
Open file                       C-x C-f
Save file                       C-x C-s
Save file as new                C-x C-w
Close file                      C-x k

Editing

Select all                      C-x h
Auto-indent selected code       TAB
Copy                            M-w
Cut                             C-w
Paste (Yank)                    C-y
Undo                            C-/
Redo (unexistent: undo of undo) C-g C-/
Search                          C-s
Replace                         M-%
Replace with newlines           M-% <string to replace> RET C-q C-j RET  # where C-q C-j is the newline character
Go to line (emacs >= 23.2)      M-g g
  • rectangle
Put a marker                                        C-SPACE
# OR
Visual rectangle selection                          C-x SPACE
# then
Delete text rectangle (from marker)                 C-x r d
Cut text rectangle (from marker)                    C-x r k
Copy text rectangle (from marker)                   C-x r M-w
Paste (Yank) text rectangle (from first line)       C-x r y
Insert text in each line of rectangle (from marker) C-x r t <string> RET

Navigation

Buffer navigation (i.e. open file)

Previous buffer                 C-x Left
Next buffer                     C-x Right
List buffers                    C-x C-b

Window navigation

It is not default. You can enable default keybinds (see (windmove-default-keybindings)) or you can explicit define yours like follows:

;; set keybindings to move focus to near windows
(global-set-key (kbd "<M-up>") 'windmove-up)
(global-set-key (kbd "<M-down>") 'windmove-down)
(global-set-key (kbd "<M-right>") 'windmove-right)
(global-set-key (kbd "<M-left>") 'windmove-left)

;; set keybindings to manage splitted windows size
(global-set-key (kbd "<M-S-up>") 'shrink-window)
(global-set-key (kbd "<M-S-down>") 'enlarge-window)
(global-set-key (kbd "<M-S-left>") 'shrink-window-horizontally)
(global-set-key (kbd "<M-S-right>") 'enlarge-window-horizontally)

Source navigation

When available (when some package (like cquery above) allows emacs to understand the source code), you can go:

Find definitions                M-.
Find references                 M-?

Windows

Split window horizontally       C-x 2
Split window vertically         C-x 3
Focus to next window            C-x o
Enlarge window                  C-x ^
Close window                    C-x 0

Projectile commands

Alternate header source         C-c p a
Search file in project          C-c p f

Magit

Open Magit interface            C-x g
 quit                           q
 git add                        s
 git commit message             c c
  git completing commit         C-c C-c
 diffing with ediff             e

Debugging

Start debugger                  M-x gdb (select the binary file)
Set a breakpoint                C-x C-a C-b
Delete a breakpoint             C-x C-a C-d
Step into next statement        C-x C-a C-s
Step out until previous stack   C-x C-a C-f
Step next current stack stat.   C-x C-a C-n
Continue                        C-x C-a C-r
Go to inner stack               C-x C-a >
Go to parent stack              C-x C-a <

References