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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:44:59+00:00 2026-06-03T02:44:59+00:00

I’m trying to write an Emacs major mode for working with biological sequence data

  • 0

I’m trying to write an Emacs major mode for working with biological sequence data (i.e. DNA and peptides), and I want to implement syntax highlighting where different letters are colored differently. Since the mode needs to be able to differentiate DNA sequences and amino acid sequences and color them differently, I am putting each sequence in the files on a single line with a single-character prefix (+ or #) that should indicate how the following line should be highlighted.

So, for example, if the file contained a line that read:

+AAGATCCCAGATT

The “A”s should all be in one color that is different from the rest of the line.

I have tried the following as a test:

(setq dna-keyword
 '(("^\+\\([GCT\-]*\\(A\\)\\)*" (2 font-lock-function-name-face))
  )
)

(define-derived-mode bioseq-mode fundamental-mode
  (setq font-lock-defaults '(dna-keyword))
  (setq mode-name "bioseq mode")
)

But that only matches the last A instead of all of them.

My first thought was to try to match the whole line with one regexp and then use another regexp to match just the A’s within that line, but I have no idea if that’s possible in the context of font-lock-mode or how it would be accomplished. Any ideas on how to do something like that, or how to accomplish this in a different way?

  • 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-03T02:45:01+00:00Added an answer on June 3, 2026 at 2:45 am

    Indeed, Emacs provides just what you need to do this with the “anchored match” feature of font-lock mode. The syntax is a bit hairy, but it allows you to specify additional “matchers” (basically a regexp, subexpression identifier and face name) which (by default) will be applied following the position where the main “matcher” regexp finished up to the end of the line. There are more complicated ways of customizing exactly what range of text they apply to, but that’s the general idea.

    Here’s a simple example which also shows how you could define your own faces for the purpose:

    (defface bioseq-mode-a
      '((((min-colors 8)) :foreground "red"))
      "Face for As in bioseq-mode")
    
    (defface bioseq-mode-g
      '((((min-colors 8)) :foreground "blue"))
      "Face for Gs in bioseq-mode")
    
    (setq dna-keyword
          '(("^\\+" ("A" nil nil (0 'bioseq-mode-a)))
            ("^\\+" ("G" nil nil (0 'bioseq-mode-g)))))
    

    You can also specify two or more anchored matchers for one main matcher (the main matcher here being the regexp "^\\+"). To make this work, each anchored matcher after the first needs to explicitly return to the beginning of the line before beginning its search; otherwise it would only begin highlighting after the last occurrence of the previous anchored matcher. This is accomplished by putting (beginning-of-line) in the PRE-MATCH-FORM slot (element 2 of the list; see below).

    (setq dna-keyword
          '(("^\\+"
             ("A" nil nil (0 'bioseq-mode-a))
             ("G" (beginning-of-line) nil (0 'bioseq-mode-g)))))
    

    I think it’s mostly a matter of taste which you prefer; the second way might be slightly clearer code if you have many different anchored matchers for a single line, but I doubt there’s a significant performance difference.

    Here’s the relevant bit of the documentation for font-lock-defaults:

    HIGHLIGHT should be either MATCH-HIGHLIGHT or MATCH-ANCHORED.

    [….]

    MATCH-ANCHORED should be of the form:

    (MATCHER PRE-MATCH-FORM POST-MATCH-FORM MATCH-HIGHLIGHT …)

    where MATCHER is a regexp to search for or the function name to call to make
    the search, as for MATCH-HIGHLIGHT above, but with one exception; see below.
    PRE-MATCH-FORM and POST-MATCH-FORM are evaluated before the first, and after
    the last, instance MATCH-ANCHORED’s MATCHER is used. Therefore they can be
    used to initialize before, and cleanup after, MATCHER is used. Typically,
    PRE-MATCH-FORM is used to move to some position relative to the original
    MATCHER, before starting with MATCH-ANCHORED’s MATCHER. POST-MATCH-FORM might
    be used to move back, before resuming with MATCH-ANCHORED’s parent’s MATCHER.

    The above-mentioned exception is as follows. The limit of the MATCHER search
    defaults to the end of the line after PRE-MATCH-FORM is evaluated.
    However, if PRE-MATCH-FORM returns a position greater than the position after
    PRE-MATCH-FORM is evaluated, that position is used as the limit of the search.
    It is generally a bad idea to return a position greater than the end of the
    line, i.e., cause the MATCHER search to span lines.

    I always find that I have to read the font-lock documentation about three times before it starts to make sense to me 😉

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

Sidebar

Related Questions

I want to construct a data frame in an Rcpp function, but when I
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

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.