Skip to content

Instantly share code, notes, and snippets.

@reachtarunhere
Created March 20, 2023 17:20
Show Gist options
  • Save reachtarunhere/369e951ef355b68a2500149a66a06c88 to your computer and use it in GitHub Desktop.
Save reachtarunhere/369e951ef355b68a2500149a66a06c88 to your computer and use it in GitHub Desktop.
emacs chat gist

------- user:Can you teach me to write a simple emacs extension?

------- assistant:

Sure, I can give you an overview of how to write a simple Emacs extension.

  1. Identify the Functionality You Want to Implement The first step is to identify the specific functionality you want to implement in your extension. For example, you might want to create a function that inserts a timestamp into the current buffer.

  2. Define a Function Once you've identified the functionality, you can define a function in Elisp that implements it. Here's an example of a function that inserts a current timestamp into a buffer:

(defun insert-timestamp ()
  "Inserts current timestamp into buffer"
  (interactive)
  (insert (format-time-string "[%Y-%m-%d %H:%M:%S]")))

This function can be called using M-x insert-timestamp or bound to a specific key combination.

  1. Testing and Debugging Once you've defined your function, you can test it to make sure it's working correctly. If there are any issues, you can use Emacs' built-in debugging tools to identify and fix them.

  2. Packaging and Distribution If you want to share your extension with others, you can package it as an Emacs Lisp Package (ELPA) and distribute it through the MELPA package repository or other channels. This will allow other Emacs users to easily install and use your extension.

That's a high-level overview of how to write a simple Emacs extension! Good luck with your coding!

------- user:What does the interactive function do?

------- assistant:

The interactive function is a special form in Elisp that tells Emacs that the following function is intended to be used interactively (i.e., for user input).

The interactive function takes no arguments and is typically used as the first line of code in a function that requires user input. When the function is called interactively (e.g., using M-x function-name), Emacs will prompt the user for any required input and pass it as arguments to the function.

For example, the following function uses interactive to prompt the user for a file name and then opens the specified file in a new buffer:

(defun open-file (filename)
  "Opens specified file in a new buffer"
  (interactive "fEnter file name: ")
  (find-file filename))

When the user calls this function using M-x open-file, Emacs will prompt them for a file name, and then open that file in a new buffer.

In summary, the interactive function is used to define the user interface for a function and to specify how input should be obtained from the user.

------- user:Interesting how does open-file work in the first place when no arguments are supplied when we call it?

------- assistant:

Ah, good question!

When you call open-file with M-x open-file, Emacs will execute the interactive form to prompt the user for input. If the user enters a file name, that name is passed as an argument to the open-file function.

However, if the function is called with an argument (either through a keybinding or programatically), that argument is passed directly to the function and the interactive form is not executed. In this case, the user is not prompted for input, and the function uses the provided argument instead.

For example, if you have a file named example.txt and you want to open it using open-file, you could call the function programmatically with (open-file "~/example.txt") and it would open that file in a new buffer without prompting the user for input.

So whether the interactive form is executed or not depends on how the function is called.

------- user:I don't understand how the first line of the function containing interactive got executed without an error?

------- assistant:

The first line of the insert-timestamp function contains the interactive special form, which is used to define the interactive behavior of the function.

When you call the function using M-x insert-timestamp, Emacs evaluates the function in the context of an interactive command.

In this context, Emacs understands that the function is meant to be executed interactively and uses the interactive form to obtain any required input from the user.

So the interactive form is not executed when the function is evaluated during normal execution, but instead only when it is called as an interactive command.

When you call the function programmatically with (insert-timestamp), it will throw an error because there is no user input provided.

------- user:Thanks for the explanation. Can you go into more details about the interactive special form and how it is implemented

------- assistant:

Certainly, here's more information about how the interactive special form works in Elisp.

The interactive special form is used to define the interactive behavior of a function. It specifies how input is obtained from the user and how that input is delivered to the function.

The interactive form can take various arguments to customize its behavior. Here are some of the most common arguments:

  • "" (empty string): This tells Emacs that the function takes no arguments and should be called without any input from the user.
  • "p" (prefix): This tells Emacs to pass the current prefix argument as the first argument to the function. The prefix argument is typically obtained by pressing the C-u key before invoking a command interactively.
  • "r" (region): This tells Emacs to pass the current buffer region as the first argument to the function.
  • "f" (file): This tells Emacs to prompt the user for a file name and pass it as the first argument to the function.

Here's an example that demonstrates how to use the interactive form to prompt the user for a string and use it as an argument:

(defun my-function (input-string)
  "This function takes a string argument"
  (interactive "sEnter a string: ")
  (message "You entered: %s" input-string))

When you call my-function interactively using M-x my-function, Emacs will prompt you to enter a string. Whatever you enter will be passed as an argument to the function and displayed using message.

In terms of implementation, Emacs evaluates the interactive form before executing the function. When it encounters an interactive form, it generates a prompt string based on the arguments passed to the form. This prompt is displayed to the user, and their response is passed as arguments to the function.

I hope this helps clarify how the interactive special form works!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment