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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:03:36+00:00 2026-06-09T08:03:36+00:00

I’m trying to write a function that will (1) search a given file for

  • 0

I’m trying to write a function that will (1) search a given file for a given string, and (2) if the file does not contain the string, add the string to the file. So far I have this:

(setq nocite-file "~/Dropbox/docs/school/thesis/_nocites.tex")

(defun add-nocite-prompt (key)
  "Prompts for a BibTex key.  If that key does not already exist in the file
nocite-file, add-nocite-prompt appends a \nocite{} instruction to that file."
  (interactive "sBibTex Key: ")
;; check for definition of nocite-file, else prompt
  (unless (boundp 'nocite-file)
    (setq nocite-file (read-from-minibuffer "Define nocite-file: ")))
  (setq nocite-string (concat "\\nocite{" key "}\n"))
  (with-current-buffer (find-file-noselect nocite-file)
    (goto-char (point-min))
    (unless (search-forward nocite-string)
      (lambda ()
    (goto-char (point-max))
    (insert nocite-string)))))

When I run it, however, emacs tells me Search failed: "\\nocite{test-input}
"
Which is fine, but it doesn’t do any of the things I want it to do when the search fails. I can’t tell what’s wrong with my unless statement.

Ideally, the function would append the string to a file in the background and save without having to manually save and kill the buffer, but I haven’t tackled that part of it yet. The plan is to bind this to a keystroke so I can enter the BibTex key without interrupting workflow.

  • 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-09T08:03:38+00:00Added an answer on June 9, 2026 at 8:03 am

    There are two things to fix in your code.

    First, take a look at the documentation for search-forward it tells you to use the 3rd argument to make sure an error is not thrown.

    Second, lambda does not do what you want. Lambda defines a new function, but what you are trying to do is to evaluate a function that executes two functions in a row. You whould be using progn for that.

    Here is the modified code with the added functionality of saving automatically the file.

    (defun add-nocite-prompt (key)
      "Prompts for a BibTex key.  If that key does not already exist in the file
    nocite-file, add-nocite-prompt appends a \nocite{} instruction to that file."
      (interactive "sBibTex Key: ")
    ;; check for definition of nocite-file, else prompt
      (unless (boundp 'nocite-file)
        (setq nocite-file (read-from-minibuffer "Define nocite-file: ")))
      (setq nocite-string (concat "\\nocite{" key "}\n"))
      (with-current-buffer (find-file-noselect nocite-file)
        (goto-char (point-min))
        (unless (search-forward nocite-string nil t)
          (progn
        (goto-char (point-max))
        (insert nocite-string)
        (save-buffer)))))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a function that will clean a strings' special characters. I do NOT
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is

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.