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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:19:03+00:00 2026-06-02T16:19:03+00:00

I’m writing a derived mode, based on comint-mode. The mode is an interface to

  • 0

I’m writing a derived mode, based on comint-mode. The mode is an interface to a command line program (GRASS gis), and the comint mode completion works for the programs. I’m trying to add on support for completing the arguments to the program, via completion-at-point-functions. A toy example is:

(setq my-commands 
      '(("ls"
         ("my-completion-1")
         ("my-completion-2"))
        ("mv"
         ("my-completion-3")
         ("my-completion-4"))))


(defun my-completion-at-point ()
  (interactive)
  (let ((pt (point)) ;; collect point
        start end)

    (save-excursion ;; collect the program name
      (comint-bol)
      (re-search-forward "\\(\\S +\\)\\s ?"))
    (if (and (>= pt (match-beginning 1))
             (<= pt (match-end 1)))
        () ;; if we're still entering the command, pass completion on to
      ;; comint-completion-at-point by returning nil

      (let ((command (match-string-no-properties 1)))
        (when (member* command my-commands :test 'string= :key 'car)
          ;; If the command is one of my-commands, use the associated completions 
          (goto-char pt)
          (re-search-backward "\\S *")
          (setq start (point))
          (re-search-forward "\\S *")
          (setq end (point))
          (list start end (cdr (assoc command my-commands)) :exclusive 'no))))))

(push 'my-completion-at-point completion-at-point-functions)

This almost works. I get normal completion of program names. However, if I have entered ls at the command line, hitting tab inserts my-completion- and doesn’t offer the two options. Hitting tab again inserts my-completion- a second time, so that I now have ls my-completion-mycompletion-.

My actual code includes a few lines to check for multi-line commands, but makes no changes to the completion code. With this version of the code, I hitting tab on a line that starts with one of the program names in my-commands I am presented with a list of the possible arguments to complete the command with, but nothing is inserted in the buffer, and the list does not get narrowed by typing the first few letters of an argument.

I’ve been over the manual, but I can’t figure out the correct way to write a completion-at-point function. Any ideas what I’m missing?

I have looked briefly at pcomplete, but the didn’t really understand the ‘documentation’, and didn’t make any progress.

  • 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-02T16:19:04+00:00Added an answer on June 2, 2026 at 4:19 pm

    The problem seems to be with the way you’re finding start and end to return the boundaries of the argument at point. I didn’t spend long enough debugging it to be sure of the details, but I think if you call the function interactively you’ll see that it returns the same value for start and end, and this means that the completion UI doesn’t know to use the argument at point to select from the completion table you’ve passed it.

    Changing the last part of your function to the following seems to be one fix:

    (when (member* command my-commands :test 'string= :key 'car)
      ;; If the command is one of my-commands, use the associated completions 
      (goto-char pt)
      (let ((start
             (save-excursion
               (skip-syntax-backward "^ ")
               (point))))
    
        (list start pt (cdr (assoc command my-commands)) :exclusive 'no)))))))
    

    This gives the expected results when added as an element of completion-at-point-functions.

    Here I’ve used skip-syntax-backward instead of regexp search, which I think is slightly more idiomatic Elisp for this kind of thing. It just says to move point backwards across anything that is not in syntax class “whitespace”. The skip-syntax functions return the distance moved rather than the value of point, so we have to add a call to point at the end of the save-excursion.

    If you do use regexp searches in a function like this, it’s usually a good idea to pass t for the fourth argument, noerror, so that it doesn’t pass on errors to the user if it fails to match. This does mean that you have to check for yourself whether the return value is nil, though.

    Finally, instead of push to add the completion function you might want to use add-hook as follows:

    (add-hook 'completion-at-point-functions 'my-completion-at-point nil t)
    

    This does two useful things: it checks whether your function is already in the hook before adding it, and (by passing t for the fourth argument, local) it only adds the function to the buffer-local value of the completion-at-point hook. This is almost certainly what you want, since you don’t want to use these completions in every other Emacs buffer when you press the TAB key.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
I am writing an app with both english and french support. The app requests
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text

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.