New Scala 3 syntax in Emacs

Scala 3 has a new “quiet” syntax, which means it uses whitespace and indention to delineate blocks of code, instead of the parentheses. Currently emacs-scala-mode does not support this yet fully, and while it is in the works the quick solution is to turn of automatic indention for scala 3 projects. To achieve this two things are required, it needs to be detected that a scala file is part of a scala 3 project, and second the automatic indention and reformatting needs to be turned off.

A quick, and somewhat hacky, way to detect scala 3 projects is to check the build.sbt file for the version, using projectile to find the root of the project and searching for a scalaVersion to start with 3.

(defun is-scala3-project ()
  "Check if the current project is using scala3.

Loads the build.sbt file for the project and serach for the scalaVersion."
  (projectile-with-default-dir (projectile-project-root)
    (when (file-exists-p "build.sbt")
      (with-temp-buffer
        (insert-file-contents "build.sbt")
        (search-forward "scalaVersion := \"3" nil t)))))

Now using emacs advice-add the function adding the hooks to indent and format can be modified to no longer no longer insert the hooks for automatic formatting

(defun with-disable-for-scala3 (orig-scala-mode-map:add-self-insert-hooks &rest arguments)
    "When using scala3 skip adding indention hooks."
    (unless (is-scala3-project)
      (apply orig-scala-mode-map:add-self-insert-hooks arguments)))

(advice-add #'scala-mode-map:add-self-insert-hooks :around #'with-disable-for-scala3)

To still have some indention but not use the scala syntax specific one, the indent-line-function needs to be replaced with one purely using the previous line for reference ignoring syntax.

(defun disable-scala-indent ()
  "In scala 3 indent line does not work as expected due to whitespace grammar."
  (when (is-scala3-project)
    (setq indent-line-function 'indent-relative-maybe)))

(add-hook 'scala-mode-hook #'disable-scala-indent)

While it is more convenient to have indention supported, while the issue in emacs-scala-mode is being worked on this provides a quick fix to still have scala 3 code formatted. Also with metals and lsp supporting scala 3, the buffer can be reformatted in case formatting is an issue.

Attaching Metals LSP debugger to existing process in Emacs

Metals supports the DAP Protocol which allows to debug Scala code in Emacs. By default all the setup is done to use code lenses to start an instance of the program to debug it, set breakpoints inspect variables, all the good stuff to expect from a debugger. It is not as obvious how to debug a remote Scala process however as this requires a bit of setup.

Setting up the Scala process

Scala, as running on the JVM, allows to be run with the well know flags to be debugged namely

-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005

When using sbt this can be simplified to running as

sbt -jvm-debug 5005

which launches an sbt shell exposing a debug port 5005 on localhost. It is good practice to always use localhost and if truly remote to use SSH port forwarding, as the debugger has no authentication or any security in place.

Attaching the debugger

In dap-mode in Emacs uses debug-templates to determine what to run, in the case of scala and metals we need to provide the connection information as well as the lsp-project to use as the source for the code to set breakpoints and in general browse the code. The project name to use can normally be found in the build.sbt file this means for a project like the one associated with the FP Tower Foundations Course the setup looks like

(dap-register-debug-template
  "Scala Attach Foundations (localhost:5005)"
  '(:type "scala"
    :request "attach"
    :name "Scala Attach Foundations (localhost:5005)"
    :hostName "localhost"
    :port 5005
    :targets [(:uri "file:///Users/pfehre/source/foundations?id=foundation")]))

important is the targets uri in this case as the default template for scala attach sets the project name to root resulting in the invalid target file:///Users/pfehre/source/foundations?id=root. With this setup and evaluated the commad dap-debug will now contain a Scala Attach Foundations (localhost:5005) target to use to attach to the process.

As this is project specific setup it makes sense to add it to the .dir-locals.el file so it automatically gets registered when visiting the project folder.

((nil . ((eval . (dap-register-debug-template
                  "Scala Attach Foundations (localhost:5005)"
                  '(:type "scala"
                    :request "attach"
                    :name "Scala Attach Foundations (localhost:5005)"
                    :hostName "localhost"
                    :port 5005
                    :targets [(:uri "file:///Users/pfehre/source/foundations?id=foundation")]))))))

would make this work.

Opening Sourcegraph from Emacs

Sourcegraph is super useful when browsing through code and dependencies, there is currently no plugin for emacs, but it is pretty easy to configure git-link to do the trick as @sqs pointed out to me on Twitter.

This is all the configuration to make git-link work as expected, and it should be pretty generic.

Bloop integration in Emacs

Some quick and hacky integration of Scala bloop with Emacs, using ammonite as the console and projectile for compilation and testing.

;;; bloop --- bloop minor mode
;; Author: Philipp Fehre <philipp@fehre.co.uk>
;; Keywords: scala, bloop, tools, convenience
;;; Commentary:
;; Helpers to integrate better with bloop, inspired by emacs-bloop
;; https://github.com/tues/emacs-bloop/blob/master/bloop.el
;; C-c M-j jack-in a bloop project running a new Ammonite REPL buffer
;; C-c M-z switch to an active Ammonite REPL
;; C-c b c Compile a bloop project backed by projectile-compile-project
;; C-c b t Test a bloop project backed by projectile-test-project
;; C-c b r Run a bloop project
;; Changelog:
;; - 25/8/2020 - Added run command mapping bloop-run (C-c b r)
;; - 1/5/2020 - initial working version
;;; Code:
(require 'projectile)
(require 'scala-mode)
(require 'ammonite-term-repl)
(require 's)
(defgroup bloop nil
"Bloop integration for emacs"
:group 'tools
:group 'convenience
:link '(url-link :tag "Gist" "https://gist.github.com/sideshowcoder/a9b2ceaca38cdf0ea95f29bf0130b171"))
(defcustom bloop-program-name "bloop"
"Program used to run bloop commands, default to whatever is in the path."
:type 'string
:group 'bloop)
(defcustom bloop-reporter "scalac"
"Either bloop or scalac.
The main difference is that bloop shows errors in reverse order.
Emacs generally assumes the first error in the output is the most
relevant so the scalac reporter will most likely be preferred.
This is used for test and compile."
:type 'string
:group 'bloop)
(defun bloop--command (&rest args)
"Build a bloop command for ARGS."
(s-join " " (cons bloop-program-name args)))
(defun bloop--available-projects ()
"Get a list of currently available projects from bloop."
(projectile-with-default-dir (projectile-project-root)
(let ((projects-string (shell-command-to-string (bloop--command "projects"))))
(split-string projects-string))))
(defun bloop-switch-to-ammonite ()
"Switch to the running Ammonite REPL."
(interactive)
(if-let ((ammonite-buffer (get-buffer ammonite-term-repl-buffer-name)))
(switch-to-buffer ammonite-buffer)
(message "Ammonite is not running try C-c M-j to start an Ammonite REPL for bloop.")))
(defun bloop-run-ammonite (project)
"Run Ammonite for a bloop PROJECT."
(interactive (list (completing-read "Run Ammonite REPL for project: " (bloop--available-projects))))
(projectile-with-default-dir (projectile-project-root)
(let ((ammonite-term-repl-program bloop-program-name)
(ammonite-term-repl-program-args (list "console" project)))
(run-ammonite))))
(defun bloop-compile (project)
"Compile a bloop PROJECT."
(interactive (list (completing-read "Compile bloop project: " (bloop--available-projects))))
(let ((command (bloop--command "compile" project)))
(projectile--run-project-cmd command projectile-compilation-cmd-map
:show-prompt 't
:prompt-prefix "Compile command: "
:save-buffers t)))
(defun bloop-test (project)
"Test a bloop PROJECT."
(interactive (list (completing-read "Test bloop project: " (bloop--available-projects))))
(let ((test-command (bloop--command "test" "--reporter" bloop-reporter project)))
(projectile--run-project-cmd test-command projectile-test-cmd-map
:show-prompt 't
:prompt-prefix "Test command: "
:save-buffers t)))
(defun bloop-run (project)
"Run a bloop PROJECT."
(interactive (list (completing-read "Run bloop project: " (bloop--available-projects))))
(let ((run-command (bloop--command "run" project)))
(projectile--run-project-cmd run-command projectile-run-cmd-map
:show-prompt 't
:prompt-prefix "Run command: "
:save-buffers t)))
;;;###autoload
(define-minor-mode bloop-mode
"Bloop integration for emacs."
:lighter " bloop"
:keymap (let ((map (make-sparse-keymap)))
(define-key map (kbd "C-c M-j") #'bloop-run-ammonite)
(define-key map (kbd "C-c M-z") #'bloop-switch-to-ammonite)
(define-key map (kbd "C-c b c") #'bloop-compile)
(define-key map (kbd "C-c b t") #'bloop-test)
(define-key map (kbd "C-c b r") #'bloop-run)
map))
;;;###autoload
(add-hook 'scala-mode-hook 'bloop-mode)
(provide 'bloop)
;;; bloop.el ends here
view raw bloop.el hosted with ❤ by GitHub

Emacs Markdown list DWIM

Markdown has multiple types of lists, and in my mind checking items from those list means different things depending on the type.

- [ ] this item is open
- [x] this item is close
- this item is also open
- ~~this item is also close~~

For checklists I want the box to be checked when I toggle the item for, others I want the item to be crossed out via strike-through. All of those actions already exist in Emacs Markdown-Mode, but they are different depending on the list, I want Emacs to do what I mean no matter the list so I added this

(defun coder--markdown-toogle-list-item-dwim ()
    "Toogle the current list item depending on the type do the right thing.

1. When it is not a markdown list, ignore
2. When the list is a checklist indicated by [ ] check the checkbox
3. When the list is a normal list, strike-through the current item
4. When the item already has strike-through applied, un-strike it"
    (interactive)
    (save-match-data
      (save-excursion
        (when-let ((bounds (markdown-cur-list-item-bounds)))
          ;; move to the beginning of the item after the list marker
          (goto-char (cl-first bounds))
          (forward-char (cl-fourth bounds))

          (cond ((looking-at "\\[[\s-xX]\\]") (markdown-toggle-gfm-checkbox))
                ((thing-at-point-looking-at markdown-regex-strike-through) (markdown-insert-strike-through))
                (t (progn
                     (set-mark (point))
                     (end-of-line)
                     (activate-mark)
                     ;; remove trailing whitespace from line first, this
                     ;; otherwise breaks strikethrough rendering
                     (delete-trailing-whitespace)
                     (markdown-insert-strike-through))))))))

Happy markdown editing!

Lauching Emacsclient via Spotlight

When installing Emacs via

$ brew cask install emacs

it automatically installs emacsclient, but this needs to be launched via the terminal. When running the Emacs.app from Applications it will launch a new instance of emacs every time. Most of the time when I want to quickly edit something I tend to prefer to launch emacsclient to create a new window or frame in the existing running instance. Since I launch almost all my applications via Spotlight the simple way to achieve this is to create a script with the .command and use this instead.

$ cat ec.command 
#!/bin/bash

EMACSCLIENT=/usr/local/bin/emacsclient
$EMACSCLIENT -n -c -a '' $*

With this in place configure the script to be run via the terminal of your choice, in my case iTerm, by right clicking on the file and Get Info.

ec.command GetInfo

Thats it now Spotlight will execute the script when typing ec and hitting Enter on the top hit, assuming it is indexed.

Learning some emacs

I have been a very faithful Vim user for the last couple years, but recently I ran into more and more things which made me want to know more OS like shortcuts. On Mac OS that means that using Emacs is pretty close, most of the basic text navigation just works in most other text windows, and Emacs is a powerful editor in itself as well. The advantage is that I can more or less have my whole setup inside emacs, it can act as my terminal, my editor, as well as my interface to other servers via SSH and the emacs built in trampmode. When editing some ruby I realized that I’ve been relying on CTags quite a lot, and I need to have a quick way to generate and update them in emacs as well, which let me to write my first few lines in ELisp in years.

(defun create-tags ()
  "Create tags file for current project defined by the textmate-project-root"
  (interactive)
  (let ((root (textmate-project-root))
        (tags-file (concat (textmate-project-root) "TAGS")))
    (shell-command (concat "ctags -e -R -f " tags-file " " root))
    (visit-tags-table tags-file)))

This small function allows me to run a simple

M-x create-tags

to update the CTags for the current project, aka the closest folder with containing .git directory, a Makefile, Rakefile etc.. So far the experience has been really nice, but I’m still catching up speed wise to my Vim days.

Detect long paths incompatible with OneDrive for Mac


Screen Shot 2015-09-01 at 21.35.28

As a user of OneDrive on MacOS I ran into an issue that OneDrive would hang at the processing files or scanning files stage. After some searching I found that this can be related to having paths with more than 255 characters in the name but how to find those? Well some small bash magic did the trick for me.

cd ~/OneDrive && find `pwd` -name '*' | grep '^.{255}'

This will list all the paths longer than 255 characters for the OneDrive directory located in home.

Canned v0.3.3

It has been some time since I wrote about canned and a lot has happend since then. With the current release canned now supports all features on Windows as well as Unix, which has been limited before due to the Windows limitation of certain characters in the filename, which caused complex get parameters to create filenames that are not valid on Windows.
With the new version canned now fully supports multiple response bodies in a single file, allowing you to match on headers, parameters, and body of the request, as well es return different status codes for each of them. This allows now to have a single file bundling up all the post responses for a certain resource, emulating the rejection of certain parameters, or headers while returning the correct status code for the operation.

For more details see the changelog thanks to everybody who contributed to the recent releases, reporting and fixing bugs as well as adding features like proper SOAP XML support to emulate ever more complex APIs.

Moving from Tumblr to WordPress

Recently I moved this blog from Tumblr to WordPress, as I wanted more control over the environment. My setup now is a hosted WordPress instance on heroku, with really nothing special, using the current PHP stack with Apache2 as my server, and ClearDB to provide me a MySQL instance to store data in. Since this basic setup will provide rather dreadful performance, especially on a single free Dyno, I use Amazons CloudFront as my CDN. All this gets me to a quite fast experience. The setup was quick and painless, but migration of the content proved harder than expected. All there is to do to make this work on heroku is using environment variables to get the database setup in PHP.

https://gist.github.com/sideshowcoder/251bfe82128958901c20.js

Importing Tumblr into WordPress

WordPress offers an importer for Tumblr, sadly this importer seems to strip HTML from Tumblr posts which meant that all my carefully embedded content was not imported. Looking around there are multiple ways to solve this, the quickest being Tumblr2Wordpress which allows to create a WordPress compatible XML file from all the Tumblr posts, with the content being HTML. Importing is as simple as hitting import on the new WordPress blog.  

One thing which constantly annoys me on the more “casual” web is that people break links all the time, and since I didn’t want to be one of those people I wanted to make sure that all links to my content still work after the migration. Also since I use Disqus for my commenting, and Disqus uses the URL to determine the correct comment stream (afaik) I wanted to make sure this works correctly. Tumblr uses a somewhat weird way to reference posts its URL format is something like: http://tumblr.com/posts/SOME_ID/THE_SLUGIFIED_POST_TITLE what is funny about this is that the last part of the URL does not matter at all, it is just the ID Tumblr cares about, and this ID is also carried over by the import and stored in the post_name attribute for the post. So to faking the first part of the Tumblr URL in WordPress is easy, just make the permalink structure: /post/%postname%. Now if everybody would just link to the ID this would work great, but sadly most of the time the title is going to be included, even though nobody cares about it. The solution to this is simple, just don’t make WordPress care about it either, but display it if needed.https://gist.github.com/sideshowcoder/6c57a8fa85c28e9a837b.js

Added to function.php of the theme that is in use, this little snippet will provide a new tag for the permalinks to attach and strip of the slugified title to the to the permalinks, depending on if the imported post used the Tumblr way of URL or if it is a new WordPress post, just don’t name your posts 1234… /post/%postname%/%tumblrslug% will now give you the structure you need.

Easy management with Rake

For the longest time I have been a ruby guy and I just like to use rake to automate almost everything and do the needed command remembering for me. So to make sure I can keep managing my blog I ended up creating a Rakefile with some function to manage my remote instance, and I use dotenv to manage my local variables in the same way as heroku does remotely, through environment variables. For anybody interested, this is what this looks like

And that’s it, the blog is migrated, all the links should still work and I can easily manage the needed tasks, like backup, getting into the database, running a local server all through Rake.