Emacs is one such example of a text editor that, due to its versatility and customizable nature, has garnered quite the name for itself among the developer community. As one of the oldest text editors out there, Emacs is known for its stability and consistency.
What makes Emacs special is that it is not just a text editor, but rather, a fully-fledged machine. Emacs can be set up as a shell, an email client, an organizer, and so much more. The complexity of Emacs arises from the Lisp interpreter at its core, which in turn allows users to customize it with even more functionalities using the Lisp language.
This article covers how to use Lisp through various methods to configure and customize Emacs.
Basics of Lisp
Before seeing how Lisp can be used to configure Emacs, it is first important to look at some of the basics of the Lisp language to get a better understanding of this feature.
In Lisp, programs are made up of symbolic expressions, shortened to s-exps. These expressions can consist of either variables only, or include other functions, as well.
A function is notated by wrapping the text inside a parenthesis. For example, for calling the addition function, the following syntax is used:
The above statement says “add 2 to 2.” Nested s-exps will look something like this:
You can also store values inside of a variable using the setq command:
Functions can be defined using the defun keyword. For example, a function that calculates the square of a number is written as follows:
(* x x))
(square 2)
You can evaluate functions defined by using the keys Ctrl + x followed by Ctrl + e. This will produce an output inside the mini-buffer. For example, the square function will have the following output:
Note: you must evaluate both the defun segment and the square segment.
That covers the basics. The following sections will show how to configure Emacs using Lisp.
Initialization File
When Emacs starts, the first processed file is the initialization file, or init file, which contains commands written in Lisp that allow users to configure Emacs. To open the initialization file, press Ctrl + x, followed by Ctrl + f, and then enter ~/.emacs. Inside this expression, you can insert additional code to customize Emacs.
1) Adding Support for Packages
Lisp can be used to add support in Emacs for packages of different sources. Melpa is one of the sources from which users can install these extensions. To add Melpa to Emacs, add the following lines to the init file:
(add-to-list 'package-archives
'("melpa" . "https://ift.tt/1DNRPtt") t)
(package-initialize)
(package-refresh-contents)
This code adds the Melpa archive to the list of package repositories, gives permission to Emacs to use these packages, initializes these packages, and refreshes the content for the changes to take place. If you open your package list by hitting Alt + x and enter package-list-packages, you can see the installed packages in the Melpa archive.
2) Changing Theme of Emacs
Initially, when you load Emacs, you will get a screen welcoming you to Emacs and giving you various options, such as the Emacs Tutorial.
However, this page does not look that great. Lisp allows you to change the theme of Emacs and the startup page according to your preferences. Users can load various types of themes, change font sizes, and even add line bullets.
For example, say you want to replace the startup page to the scratch buffer, load the Material theme, and add line bullets. This can be done by adding the following lines to the init file:
(load-theme 'material t)
(global-linum-mode t)
As defined above, the setq syntax makes the inhibit-startup-message true, which removes the initial starting page. The load-theme loads the material theme. The global-linum-mode syntax is simply a function that is set to be true and is executed to produce line numbers. This is what Emacs should look like after inputting the above commands:
3) Bind Shortcuts to Key Bindings
Lisp can also be used to bind commands or shortcuts to keys. This allows users to customize Emacs according to their preferences, as well as run custom functions made by the user with just a click.
Suppose you have defined a function that simply produces a backslash character and you want to assign this to the keys Ctrl + x followed by Ctrl + o. This can be done by adding the following lines to the initialization file:
(interactive)
(insert "\"))
(global-set-key (kbd "C-x C-o")
'insert_backslash)
Here, the function insert_backslash is defined, made interactive (this allows the function to be called interactively meaning with the key binding), and the output is a backslash. Then, you can bind the function with the keys given above using the global-set-key keyword.
Why Use Lisp?
Lisp is an integral part of Emacs, as it allows Emacs to have more power and functionality. Lisp provides an opportunity for users to customize Emacs and turn it into something that matches their interests and needs. Lisp is what makes Emacs truly powerful and unique compared to other text editors.
from Linux Hint https://ift.tt/31ZTSQ8
0 Comments