Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8389473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:44:13+00:00 2026-06-09T18:44:13+00:00

I conjured up the following code while working on some Vim emulation feature: If

  • 0

I conjured up the following code while working on some Vim emulation feature:

If you press ; followed by Return then the cursor will jump to the end of the line and insert a semicolon.

(global-set-key (kbd ";") 'insert-or-append)

(defun insert-or-append ()
  "If the user enters <return>, then jump to end of line and append a semicolon,
   otherwise insert user input at the position of the cursor"
  (interactive)
  (let ((char-read (read-char-exclusive))
        (trigger ";"))
    (if (eql ?\r char-read)
        (progn
          (end-of-line)
          (insert trigger))
      (insert (this-command-keys)))))

This function works alright, but everything is hardcoded. I would prefer to make it more generic. Ideally, I’d like to specify a kbd-macro (for example (kbd "<return>")) as argument and compare it to the result of (read-char). However, kbd returns a symbol and (read-char) returns a character code. I’ve been going over the Emacs documentation, but was unable to find a conversion.

Is there a way to compare the two? Or is there an easier approach?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T18:44:14+00:00Added an answer on June 9, 2026 at 6:44 pm

    What about this:

    (global-set-key (kbd "RET") 'electric-inline-comment)
    
    (defun electric-inline-comment ()
      (interactive "*")
      (if (and (eq last-command 'self-insert-command)
               (looking-back ";"))
          (progn
            (delete-region (match-beginning 0) (match-end 0))
            (end-of-line)
            (insert ";"))
        (newline)))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Some time ago we took over the responsibility of a legacy code base. One
I am going through some exam questions for the 70-536 exam and an actual
I am very new to this site and to programming. I started doing some
I am working on a legacy framework. Lets say 'A' is the base-class and
Can anyone conjure from this code why the ItemsSource line would be getting a
If I could do magic I would conjure up a C# code analysis tool;
Is there a way to save a search pattern to a register and then
For some reason, black boxes are displaying in random spaces in Chrome (latest build).
I used Aquamacs so far, and I need to install and run Clojure using
This page introduces a lot of clojure libraries. And this page also comments to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.