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 8934363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:46:04+00:00 2026-06-15T09:46:04+00:00

I have written a small function to reverse the items in the marked region

  • 0

I have written a small function to reverse the items in the marked region using the comma as item separator. The function code is:

(defun reverse-list (beg end)
  "Reverses a list in-place, where comma ',' is the list item separator."
  (interactive "r")
  (if (region-active-p)
    (let ((region-list (reverse (split-string (region-as-string) ","))))
      (kill-region beg end)
      (loop for s in region-list do (progn
                                      (insert (chomp s))
                                      (insert ", ")))
      (delete-char -2))
    (message "Error: No region selected!")))  

where chomp strips leading/trailing whitespace from a string and region-as-string yields the region as a string.

The function is very useful, however it would be great to be able to select the separator on the fly. The behaviour I’m looking for is:

  • if called without universal argument use the comma as item separator
  • if called with universal argument (C-u) ask the user to input a (possibly multi-char) separator string

I tried to achieve this, but haven’t been successful in doing so. It would be great if you could provide help!

Thanks in advance,
elemakil

  • 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-15T09:46:05+00:00Added an answer on June 15, 2026 at 9:46 am

    You can use the interactive code P to read the "raw" prefix argument. This is nil if there was no prefix argument, and non-nil if there was a prefix argument, so you can test this and decide whether to set the separator to "," or to use read-string to prompt the user to enter it.

    Also, I’d make the following comments on your code:

    1. Your function only works if called interactively, so that beg and end are really the beginning and end of the region. If called non-interactively, these might not match (or there might not even be a region) and in that case your function will go wrong (because it deletes the buffer between beg and end but inserts the reversal of the region). So you need to call (buffer-substring beg end), not (region-as-string).

    2. [Edited, see comments] It’s not a good idea to raise an error in the case where the region is inactive. In Emacs, the region continues to exist even when it’s inactive (that is, no longer highlighted). Or the user might have turned off transient-mark-mode and so never have an active region at all. In both cases, the user might still want to run your command.

      At most, you might change the behavior of the command when the region is active (see for example, comment-dwim).

    3. You delete the region by calling kill-region, which copies the removed text to the kill-ring. Is this really what you want to do? It might surprise the user. It’s better to call delete-region unless you actually mean to save the deleted text.

    4. You don’t ensure that point is in the right place before you start calling insert. In interactive use, you’ll get away with it, but for non-interactive use point could be anywhere, so you ought to move it explicitly. And also use save-excursion, of course.

    5. It seems very inelegant to insert an extra ", " and then have to delete it afterwards. Better not to insert it in the first place.

    Here’s some revised code that fixes all of the above:

    (defun reverse-list (beg end read-separator)
      "Reverse the region in-place, treating it as a list of items
    separated by commas. With a prefix argument, prompt for the
    separator."
      (interactive "r\nP")
      (save-excursion
        (let* ((separator (if read-separator (read-string "Separator: ") ","))
               (region-list (nreverse (split-string (buffer-substring beg end) separator)))
               (separator (concat separator " ")))
          (goto-char beg)
          (delete-region beg end)
          (loop for s in region-list
                for sep = "" then separator
                do (insert sep) (insert (chomp s))))))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a small animate function using three.js for 3D rendering(full test application
I have written this small function to retrieve the tail of a list: let
I have written a small function, which uses ElementTree to parse xml file,but it
Hi I am learning JQuery and I have written a small function where I
What I have written a small piece of code to find and remove if
I have written a small function, which uses ElementTree and xpath to extract the
Why it is not allowed to overload = using friend function? I have written
I have written a small function in C# which isn't my main launguage so
I am still new to Javascript and JQuery. I have written a small function
I have written some code in which a small part of the code takes

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.