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

  • Home
  • SEARCH
  • 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 6998057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:21:46+00:00 2026-05-27T20:21:46+00:00

I am working on splitting code into smaller files and refactoring it a bit.

  • 0

I am working on splitting code into smaller files and refactoring it a bit. Consider the following code below as the section I want to extract:

(require 'package)
(add-to-list 'package-archives
             '("marmalade" . "http://marmalade-repo.org/packages/") t)
(package-initialize)
(when (not package-archive-contents)
  (package-refresh-contents))

(defvar my-packages '(org magit)
  "A list of packages to ensure are installed at launch.")

(dolist (p my-packages)
  (when (not (package-installed-p p))
    (package-install p)))
  • I want to take the section above and replace it with something like (require `file-name)
  • Then take the text replaced and place that in a new file in the current directory named file-name.el
  • And then add a line to the top of the file (provides `file-name)

It would be great if I could hit a keychord and then type a name and have this happen. If there is an easy way to do this then I would love to hear possible solutions.

Edit:
I’m starting a bounty because I think this applies to more types of code than Lisp and I would like to have something a little more general that I can expand upon.

I have considered yasnippet but I don’t think it’s powerful enough to perform the task at hand. Basically the ideal workflow would be marking the lines to be extracted, replacing that with an appropriate require or include directive and sending the text off to it’s own file. Ideally one command and something that is aware of the type of file being edited or at least the major mode so the behavior can be customized, again yasnippet is good at performing different tasks when editing in different major modes however I would have no idea how to make that work or evaluate the possibility of making it work.

Let me know if you need any more information.

  • 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-05-27T20:21:46+00:00Added an answer on May 27, 2026 at 8:21 pm

    Slightly tested:

    (defun extract-to-package (name start end)
      (interactive (list (read-string "Package name to create: ")
                         (region-beginning) (region-end)))
      (let ((snip (buffer-substring start end)))
        (delete-region start end)
        (insert (format "(require '%s)\n" name))
        (with-current-buffer (find-file-noselect (concat name ".el"))
          (insert snip)
          (insert (format "(provide '%s)\n" name))
          (save-buffer))))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone please help me out? My code for splitting the strings is working
I'm splitting a string into an array, then I want to remove the white
I am working on splitting out an existing, working application that I currently have
... which I didn't feel like splitting into several question posts, since I guess,
tl;dr; Even without my explanation one can look at the code below and the
I have the following controller code that returns a Json list object to my
Recently my co-worker showed me a block of code that was not working correctly:
I'm working on a clojure program that includes these lines of code: (defn makeStruct
I am trying to separate a large table into multiple discrete types. I'm following
Hi everyone I am working on a game and I have run into a

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.