Shanidar

Titular notícies
Nombre de resultats 8 per a emacs

21/05/2019 - LSP for Python and Scala
If you have have known me for any length of time you'll know I write mostly Python and Scala lately (Rust is getting into the mix slowly). And you should know, I am a heavy emacs user. I have been using emacs for close to 15 years, for the past 3 my emacs of choice has been spacemacs. I used to have a very long, customised and complex .emacs and with spacemacs I get a mostly-batteries-included package. That's nice after a while, and I also have gotten really proficient at using evil.


One problem of using emacs is the integration with some languages. If you write Scala, with IntelliJ you get super-fancy completion, refactoring, code analysis, jump-to-definition... Many goodies. In emacs, the best-in-class system used to be ensime. It worked, but it was not really supported for spacemacs (since I'm an old emacs user I could play around that), but the main issue was that my old MacBook was short on memory for running ensime and a lot more. So, I wrote most of my Scala code in hardcode mode. No completion, documentation or jump to definition.

This is why I learnt how to set up GNU global, jump to definition is just too handy. Luckily, the people at Scala Center not only are smart, but also try to improve developer experience, and had been working in a language server for Scala for a while, called metals. I got it working recently, and it's great. You get documentation on hover, error messages, jump to definition. Oh, I forgot to mention, the language server protocol is an invention from Microsoft to standardise how editors handle language completions and all that. They probably introduced it for Visual Studio Code (I actually use it from time to time, it has some remote pair programming capabilities I'll talk someday), but now it's extending across all editors.

After using LSP in emacs for Scala for a while I decided to set it up for Python as well, in preparation for our next PyBCN podcast, about tools we use. I was pretty happy with the completions I was getting, but semantic completions from a language server are usually better. So far, lsp with python is ok. Oh, you'll see screenshots at the end!

You'll need to install the language server. I usually have a high level Python environment with all my tools, for things I am just starting to work on:

pyenv virtualenv 3.7.1 tools
pyenv activate 3.7.1/envs/tools
pip install "python-language-server[all]" bpython mypy flake8

After this, some configuration is needed in emacs. Here you can find parts of my configuration, commented. These sit in my dotspacemacs/user-config
;; First come the configurations for Scala language server 
;; thingies. sbt is the Scala build system.

(use-package scala-mode
:mode "\\.s\\(cala\\|bt\\)$")

(use-package sbt-mode
:commands sbt-start sbt-command
:config
(substitute-key-definition
'minibuffer-complete-word
'self-insert-command
minibuffer-local-completion-map))

;; This is the main mode for LSP
(use-package lsp-mode
:init (setq lsp-prefer-flymake nil)
:ensure t)

;; This makes imenu-lsp-minor-mode available. This minor mode
;; will show a table of contents of methods, classes, variables.
;; You can configure it to be on the left by using `configure`

(add-hook 'lsp-after-open-hook 'lsp-enable-imenu)

;; lsp-ui enables the fancy showing of documentation, error
;; messages and type hints
(use-package lsp-ui
:ensure t
:config
(setq lsp-ui-sideline-ignore-duplicate t)
(add-hook 'lsp-mode-hook 'lsp-ui-mode))

;; company is the best autocompletion system for emacs (probably)
;; and this uses the language server to provide semantic completions

(use-package company-lsp
:commands company-lsp
:config
(push 'company-lsp company-backends))

;; I use pyenv to handle my virtual environments, so when I enable
;; pyenv in a Python buffer, it will trigger lsp. Otherwise, it
;; will use the old systems (I think based on jedi)

(add-hook 'pyenv-mode-hook 'lsp)

;; Flycheck checks your code and helps show alerts from the linter
(use-package flycheck
:init (global-flycheck-mode))

;; Show flake8 errors in lsp-ui
(defun lsp-set-cfg ()
(let ((lsp-cfg `(:pyls (:configurationSources ("flake8")))))
(lsp--set-configuration lsp-cfg)))

;; Activate that after lsp has started
(add-hook 'lsp-after-initialize-hook 'lsp-set-cfg)

;; I like proper fonts for documentation, in this case I use the
;; Inter font. High legibility, small size

(add-hook 'lsp-ui-doc-frame-hook
(lambda (frame _w)
(set-face-attribute 'default frame

:font "Inter"

:height 140)))

;; Configure lsp-scala after Scala file has been opened
(use-package lsp-scala
:after scala-mode
:demand t
:hook (scala-mode . lsp))

You can see how it looks now.

Traditional completion (non-LSP)
LSP-powered completion. Way more information!

Fancy inlined documentation
Sadly, inlined documentation doesn't look as good as it should: compare with Scala with metals and lsp-scala:

LSP mode in Scala with metals

If you've gotten this far, I share my most interesting weekly readings in  tag here . You can also get these as a weekly newsletter by subscribing here.


09/05/2019 - gtags (GNU global) in emacs for Scala
Photo by thom masat on Unsplash
As you may know, I’m a heavy emacs user, and a frequent Scala developer. Scala tooling for emacs was restricted to mostly ensime until recently. Although ensime is an excellent piece of software, it made my old Macbook suffer a lot (it only had 8gb of RAM). So, most of the time I just went hardcore mode developer, and worked with no autocompletion, no jump to definition, no-nothing. A pervasive use of ripgrep and good memory were sometimes enough, but I was envious of many things I could see in my colleagues using IntelliJ. Of course, switching editors was not an option.

I looked up what solutions were available, and the only option that seemed good enough was using the classic ctags/etags/gtags I hadn’t used since my C days.

Having a tags implementation can bring an almost-decent jump-to-definition essentially for free: gtags (and the others) work by pre-analysing the data and generating symbol maps which are usually stored as files, the overhead in speed and memory is minimal.

Installing it can get somewhat unintuitive (specially on Mac), since you need very specific settings for it to work with emacs and especially, Scala code.

Start by installing GNU global:
brew install global --with-pygments --with-ctags --with-exuberant-ctags
The key parts is having pygments.
Aside from this you will need to export the following environment variables:
GTAGSCONF=/usr/local/share/gtags/gtags.conf
GTAGSLABEL=pygments
Finally, you need to install (or activate) ggtags-mode. If you use spacemacs, you only need to activate the gtags layer.
All this has become moot as soon as metals has reached a usable state: now you can have a really fast language server for Scala, written in Scala with low memory overhead.


11/09/2016 - More emacs configuration tweaks (multiple-cursor on click, minimap, code folding, ensime eval overlays)
At Affectv we use a wide range of editors: Sublime, Atom, Emacs, Pycharm, IntelliJ... Actually only two people use the same editor! As such, from time to time I see things in other people's editors that I would like to have as well. So, yesterday I decided to improve on some configuration settings on Spacemacs.

Click for multiple-cursors

I saw this on Jordi's Sublime, and it is much more comfortable than using more-like-this or similar helper functions, even if I need to use the trackpad to do so. After all, a multi-cursor edit (proper edit, not as a substitute for a macro) is rare enough that I can tolerate leaving the home row. Easy enough to configure thanks to Magnar Sveen.

(global-unset-key (kbd "M-<down-mouse-1>"))
(global-set-key (kbd "M-<mouse-1>") 'mc/add-cursor-on-click)


Minimap

Also from Sublime, I had this on my old emacs setup. As simple as adding minimap to the list of additional packages and configuring its property group. See animation below.

dotspacemacs-additional-packages '(helm-dash key-chord pig-mode mmm-mode minimap origami ansible)

Folding

I have always loved how clean vim's folding works, and how Sublime has this nice folding. Then I found origami-mode and my emacs-life was complete. I tweaked a little the folding functions so that minimap was updated on fold (for some reason it is not, I guess minimap is tied to the "modified" hook or similar). I bound z and Z (and A-z which maps to æ in Colemak) to the basic fold operations.

(eval-after-load 'origami
'(progn
(defun rb-show-only (buffer point)
(interactive (list (current-buffer) (point)))
(progn (origami-show-only-node buffer point)
(minimap-new-minimap)))

(defun rb-toggle-rec (buffer point)
(interactive (list (current-buffer) (point)))
(progn (origami-recursively-toggle-node buffer point)
(minimap-new-minimap)))

(define-key evil-normal-state-map "æ" 'rb-show-only)
(define-key evil-normal-state-map "Z" 'origami-toggle-node)
(define-key evil-visual-state-map "Z" 'origami-toggle-node)
(define-key evil-insert-state-map "C-Z" 'origami-toggle-node)
(define-key evil-normal-state-map "z" 'rb-toggle-rec)
(define-key evil-visual-state-map "z" 'rb-toggle-rec)
(define-key evil-insert-state-map "C-z" 'rb-toggle-rec)
)))


For some reason just advising the functions with after didn't work, this is not great but does work. I left the Z bindings as they are, since I have not used them yet, and will probably delete them if I keep not using them.


Execution overlays in Ensime (Scala)

I saw this for Cider in the emacs church meeting from August, and heard @fommil (I think it was him) mention that it was coming to ensime. And indeed it was. And it's easy enough to use C-c C-v C-r (thing of it as extended command, eval, region to remember), given an open inferior Scala interpreter. Symbol prettify does not apply to overlays, so you need to customise the arrow used therein.





07/08/2016 - Moving from Emacs to Spacemacs
A couple of days ago I attended (first time I managed in almost 6 months) the London chapter of the Emacs Church (also known as the local meetup for emacs lovers). In this event we were shown how to use emacs effectively for Clojure development (using Cider) and I saw in real life Spacemacs.

In case you don't know, Spacemacs is a "distribution" of Emacs prepared (is open source, of course) to be easy to setup, and somehow specially prepared for former Vim users to move to Emacs. For instance, on startup asks if you want to be in Emacs mode, evil mode or hybrid (Emacs mode keys in vim insert mode) by default.

As you may remember, I've been using Emacs with evil for around 3 years already, and have been pretty happy with it. I'm not so happy about the state of my .emacs file: currently it is 2652 lines long (of course around 400 or 500 of those are generated automatically by custom). Too big and unwieldy. And I'm too lazy to move all the nuts and bolts to something more lightweight and sane.

I thought that trying out Spacemacs could be the perfect excuse to clean the mess off my .emacs file, since I could carefully move piece by piece whatever I needed as I needed it (like I usually do when upgrading computers).

For a start, the beginning was a good experience. Since by default it includes most of the fancy stuff I use normally (helm being the biggest, fanciest helper I need, I only needed to tweak helm-files and switch-buffer, and add recentf) and I no longer use mu4e on a day-to-day basis, I could easily switch to it.

It's actually really easy to try Spacemacs alongside your normal emacs: just download it and from the spacemacs folder run something like
HOME=~/fromsource/spacemacs /Applications/Emacs.app/Contents/MacOS/Emacs
or the equivalent in your environment. Caveat: multi-term/ansi-term won't work as expected (best solution is to actually move to using Spacemacs as default). Aside from that I have had no other big issues, and recently moved to Spacemacs to be the main Emacs and if needed I can run my old configuration with the "HOME trick".

One thing I have not figured out how to exactly do "the Spacemacs way" but I needed no matter what is having my set of normal Emacs keybindings in evil insert and normal modes. Hybrid mode covers insert more or less nicely, but some commands I need them as they are because not only are they part of my muscle memory, but I also happen to like them and use them everywhere (OS X input fields, terminal windows). I tried to set this up in many places in the .spacemacs file (the user-config section, using the -init or -after macros of package initialisation...) And finally this made it work, so, in case you need to modify evil insert or normal key maps in Spacemacs:

(eval-after-load 'evil 
'(progn
(define-key evil-normal-state-map (kbd "SPC") 'ace-jump-mode)
(define-key evil-insert-state-map (kbd "C-a") 'move-beginning-of-line)
(define-key evil-insert-state-map (kbd "C-e") 'move-end-of-line)
(define-key evil-insert-state-map (kbd "C-k") 'kill-line)
(define-key evil-insert-state-map (kbd "C-w") 'kill-region)
(define-key evil-visual-state-map (kbd "C-a") 'move-beginning-of-line)
(define-key evil-visual-state-map (kbd "C-e") 'move-end-of-line)
(define-key evil-normal-state-map (kbd "C-a") 'move-beginning-of-line)
(define-key evil-normal-state-map (kbd "C-e") 'move-end-of-line)
(define-key evil-normal-state-map (kbd "C-k") 'kill-line)
(define-key evil-normal-state-map (kbd "C-y") 'yank)
(define-key evil-insert-state-map (kbd "C-y") 'yank)
(define-key evil-insert-state-map (kbd "C-t") 'transpose-chars)
(define-key evil-normal-state-map (kbd "C-w") 'kill-region)
(define-key evil-visual-state-map (kbd "C-w") 'kill-region)
(define-key evil-visual-state-map (kbd "SPC") 'ace-jump-mode)))
Something I found surprising though is the load time: Spacemacs does not load packages on startup and my .emacs setup did, and they roughly start up at the same speed. Somehow I expected a faster startup time.

Worth also downloading the latest emacs port for homebrew, which fixes some annoyances with powerline colours as well as being quite more up to date than Carbon Emacs.

Now the only big thing left I have is reconfiguring multiple-cursors and my definitions of more-like-this and all that to be a happy Spacemacs user.


02/11/2015 - SyncTeX and pdf-view-mode for emacs
Or, destiny is cruel
Back in the days of yore, when I was switching between my Windows machine and a Linux machine, I remember having SyncTeX active in my Windows machine. It was a wonderful experience: SyncTeX lets you click anywhere on a generated file from LaTeX and gets back to your editor, to the place generating where you clicked. This was extremely useful, specially later on when you need to adjust many formulas to fit and you need a bit of back-and-forth-ing.

Then I got a Mac, and since Preview is so handy I slowly forgot about SyncTeX. Time went on, and I merrily kept on editing LaTeX files as usual. I even managed to deliver my PhD dissertation a couple weeks ago, the formal speech will be in a month or two (come at your own risk). AucTeX’s preview saved most of the days, so I slowly even forgot SyncTeX existed. Shame on me indeed.

The other day I got an annotated PDF from one of my advisors, and I just couldn’t open the annotations. I tried all programs I had for Mac, and no luck: annotations weren’t showing, just saw the icons. Surveying for some command-line tool to extract annotations (just in case) I found pdf-tools, a replacement for DocView based on Poppler. It had the awesome ability of actually displaying annotations, with it it was pretty clear the annotations were broken in that PDF. I got a new set of PDFs from my advisor with the annotations in place, though. While waiting for it to arrive…

I saw SyncTeX was an option of pdf-tools. I had been using that, hadn’t I? So, I activated SyncTeX in AucTeX (it is TeX-source-correlate-method, see here) and indeed: I could have two frames, one with the actual LaTeX sources and the other with a PDF, and go from one to the other. Even hyperreferences in PDF work! See (well, click on the full-screen mode to see it larger or you won't see anything)!



Getting pdf-tools to work wasn’t incredibly tricky (given the hoops you need for some packages, sometimes). Just

brew install pdf-tools

and after reading

brew info pdf-tools

I was told to run

  emacs -Q --batch --eval "(package-install-file \"/usr/local/Cellar/pdf-tools/0.60/pdf-tools-0.60.tar\")"

and this does the trick (well, change emacs for your actual emacs, which likely is /Applications/Emacs.app/Contents/MacOS/Emacs) You’ll also need to add to your .emacs file (or temporarily in your *scratch* buffer)


(setenv "PKG_CONFIG_PATH" (concat "/usr/local/Cellar/zlib/1.2.8/lib/pkgconfig" ":" "/usr/local/lib/pkgconfig:/opt/X11/lib/pkgconfig"))


(getenv "PKG_CONFIG_PATH")


and run

(pdf-tools-install)

as advised in the package’s README. And that's it, open a PDF and activate pdf-view-mode to check everything is in place. Well worth it!


30/12/2010 - Emacs 30 Day Challenge: Get the latest gnus to improve speed



The gnus logo,
from gnus homepage
Ever since my first post of the emacs 30 Day Challenge, I was warned about gnus slowness. A lot of comments in reddit pointed to this, and suggested using wanderlust or installing a local IMAP server (like dovecot or notmuch) to speed up the IMAP back-end (search, retrieval and processing), while also allowing for offline reading.

The main drawback was keeping it nicely cross-platform. As you may remember, I split my time between two computers: my MacBook and my Acer netbook. Any solution regarding to gnus had to be completely cross-platform in the most seamless way I could. And having a local IMAP server didn't look like it (although notmuch can be installed through macports).

When I posted my first update on gnus in HackerNews, a kind reader pointed me to the latest development snapshot, as having far, far better IMAP and nnir (the gnus IMAP search system) support. I went to the gnus homepage, and downloaded the latest CVS snapshot. It didn't work any better, it was in fact worse: nnir search didn't even work!

The problem? The latest development snapshot is in git, not in CVS! By the way, I have found very hard to know which is the latest version of gnus. I had Gnus 5.13 (if I'm not mistaken) before installing the git version, which is NoGnus 0.11 All gnus alpha versions are labeled NoGnus, to distinguish them from the final versions.

What do you need to install the latest gnus? First, a recent copy of git. At least 1.7.0.4 as suggested by the gnus git page. Don't ignore this: I had an outdated git in my MacBook and without upgrading it I could not get the package.

Once you have a current git system, fire a terminal window and go to where you want to have your new gnus. I did the following steps in ~/emacs/ Download the repository with
git clone http://git.gnus.org/gnus.git
and then you are ready to go with
cd gnus
./configure
Now make would finish the charm and get you a ready to use gnus... But sadly, it didn't work in my MacBook (had no problems in the netbook)! The problem?
Cannot open doc string file "/Applications/Emacs.app/Contents/Resources/etc/DOC-22.1.1"
make[1]: *** [gnus-load.el] Error 255
make: *** [lick] Error 2
I did a ugly hack to solve this,
cd /Applications/Emacs.app/Contents/Resources/etc
cp DOC-22.3.1 DOC-22.1.1
cd -
make
YMMV here when copying the correct DOC-emacs-ver file. Just check which one do you have. Then I changed where my emacs looked for gnus by adding
(add-to-list 'load-path "~/emacs/test-git-gnus/gnus/lisp/")
(require 'gnus-load)
to my .emacs file. When I checked with M-x gnus, it was blindingly fast. Starts up in a whim, and updates in a moment. But when I tried G G to do a mail search I got
nnir search Symbol's value as variable is void: gnus-registry-install
D'uh! What's this? I still don't know... But after a small google search I found a viable solution:
(setq gnus-registry-install t)
Now you can check that G G searches amazingly fast through your Gmail inbox.
(setq nnmail-crosspost t)
On the other hand, to prevent mails you processed through Gmail's website (or another mail reader) to be again in your gnus inbox (and then being unable to delete them or mark them as read as the server responds with a "non existent post"), add this to your .gnus.el:
(setq gnus-agent nil)


I hope you enjoyed this second installment in the world of gnus. I hope you give gnus another try, if the problem you had was its speed. The development git gnus will surprise you!

If you enjoyed this post, please share it with your emacs (or emacs-wannabe) friends through Hacker News, Reddit/emacs, Twitter or even Facebook. Thank you for reading this far!

The "Related posts" method I use involves Javascript, thus it doesn't work in the RSS feed. To view related posts, please refer to the original article. Thanks!

21/12/2010 - The emacs 30 Day Challenge: A glimpse of BBDB, The Insidious Big Brother Database and gnus



Keep your contacts
under control
Bbdb stands for The Insidious Big Brother Database, and is a very powerful contact book for emacs. It holds names, emails, aliases and other information and has good integration with gnus, wanderlust and vm (view mail). In my case, I have only checked gnus integration which is indeed pretty good.

This is the third installment in the set of posts for the emacs 30 Day Challenge: doing everything (as far as possible) from within emacs.

First you'll need o get the package from SourceForge here. Uncompress in something like ~/emacs/bbdb. The following lines can be add either to your usual .emacs file (if you want to use it 'always') or to your .gnus.el if you will use bbdb only when you use gnus. You can activate it with
(add-to-list 'load-path
"~/emacs/bbdb/lisp")
(require 'bbdb)
And activate gnus integration with
(bbdb-initialize 'gnus 'message)
(bbdb-insinuate-message)
(add-hook 'gnus-startup-hook 'bbdb-insinuate-gnus)
You are ready to start filling your database! The first step I took here was moving my bbdb file (the main contact database) to Dropbox with
(setq bbdb-file "~/Dropbox/bbdb")
This means that my database keeps automagically in sync from my netbook and my MacBook. Now that everything looks fine, let's check gnus integration. M-x gnus, open some email from your inbox and press : (colon). Voilà! You should have a newly created buffer with an automatic bbdb entry for the mail sender. You can edit the newly created record with e or add a new field with C-o. Also, delete the current field with d, and the current record if you are in the first field. For more information, press C-h m.

Once you have something in your database, you can search through it with M-x bbdb (or via anything.el integration, but this package is a topic for another post), and send an email to a contact by getting the cursor over their record and pressing m. This uses the default emacs mail sender, and you probably prefer to send this mail via gnus (I do, to take advantage of multiple SMTP accounts). To use it add the following to your .gnus.el:
(setq bbdb-send-mail-style 'gnus)
But most times, I start writing an email and then decide to whom I want to send it. And bbdb together with gnus has a good method of doing this. You only need to type the first few characters of the name (forename or surname) or the first few characters of the (primary) net address and press tab to fill to the first result, and keep pressing tab to cycle to all possible completions. To get this, add the following
(setq bbdb-complete-name-full-completion 't)
(setq bbdb-completion-type 'primary-or-name)
(setq bbdb-complete-name-allow-cycling 't)
Finally, some other configuration stuff I find useful
(setq
bbdb-offer-save 1
bbdb-use-pop-up t
bbdb-electric-p t
bbdb-popup-target-lines 1
)
where 1 means save without asking (one yes-and-no question you can skip, usually), and the other options make the default bbdb 'record found' screen very small and disposable by pressing space (i.e. electric).

You may find answer to some bbdb questions in the emacswiki.

If you liked this, please share it with your emacs friends through Reddit, Hackernews or whatever you enjoy.
The "Related posts" method I use involves Javascript, thus it doesn't work in the RSS feed. To view related posts, please refer to the original article. Thanks!

17/09/2009 - AucTeX again
For a while I have been either not LaTeXing a lot, or not using AucTeX frequently. Strange indeed! But in July, while in Göttingen I wrote part of some lecture notes of one of the courses. And again, used auctex-preview in emacs to find errors in what I was writing.I can't even tell how wonderful it is, to share the source with the images with the formulas. An image is worth more than a googol