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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:04:07+00:00 2026-05-11T19:04:07+00:00

I would like emacs to mark files that are generated as read-only when they’re

  • 0

I would like emacs to mark files that are generated as read-only when they’re opened. The part of the puzzle that I’m missing is how to check if a file “exists”. I currently have the following:

;;
;; get file extension
;;
(defun get-ext (file-name)
  (car (cdr (split-string file-name "\\."))))

;; 
;; get the base name of the file
;;
(defun base-name (file-name)
  (car (split-string file-name "\\.")))

;;
;; if an 'lzz' file exists for this header, mark it as read only
;;
(defun mark-read-only ()
  (if (string= (get-ext (cur-file)) "h")
      (if ( ??file-exists??? (concat (base-name (cur-file)) ".lzz") )
          (toggle-read-only))))

What can I use for “???file-exists???”?

Once I find this, I’ll add “mark-read-only” to the appropriate hook (which I think is the find-file-hook).

BACKGROUND

We use lzz as a code generator to simplify our C/C++ development process. Briefly, lzz takes a single input file (which looks very like C/C++) and generates header and source files as appropriate.

By default, lzz includes #line directives so that the debugger points to the original source and not the generated source, however, to reduce compilation dependencies we normally disable these directives in header files. The result is that when debugging templates or inline functions, the debugger normally points to the generated header file and not the original source file.

This is not a big deal, however, recently I’ve found that when debugging I’ll make a quick modification to the displayed file and then I’ll rebuild. Of course this normally means that the change I made disappears because the file I edited is generated and so the changes are “blown away” during the library rebuild.

SOLUTION

Thanks to everyone for their help and comments. A special thanks to cobbal for pointing out the correct function to use.

Here’s the resulting code (with updates based on the other comments here too):

(defun cur-file ()
  "Return the filename (without directory) of the current buffer"
  (file-name-nondirectory (buffer-file-name (current-buffer)))
  )

(defun mark-generated-as-read-only ()
  "Mark generated source files as read only.
Mark generated files (lzz or gz) read only to avoid accidental updates."
  (if
      (or (string= (file-name-extension (cur-file)) "h")
          (string= (file-name-extension (cur-file)) "cpp"))
      (cond
       (
        (file-exists-p (concat (file-name-sans-extension (cur-file)) ".lzz"))
        (toggle-read-only))
       (
        (file-exists-p (concat (file-name-sans-extension (cur-file)) ".gz") )
        (toggle-read-only))
       )
    )
  )
  • 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-11T19:04:07+00:00Added an answer on May 11, 2026 at 7:04 pm

    try file-exists-p

    "Return t if file filename exists (whether or not you can read it.)".

    Note that it’s not spesific to files and works for directories too.

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

Sidebar

Related Questions

I would like to use Emacs to edit some VB6 files but Emacs does
I would like to have an .emacs setting so that tabs are always formed
there are portions of my .emacs file that I would like to behave differently
I would like to create only a syntax highlighting minor mode for Emacs. I
I would like to assign the Alt-Backspace shortcut to pop-global-mark in Emacs (NOX). I
I would like to know Emacs' equivalent of Vim's :n, which opens several files
I'm using emacs with cdlatex-mode to edit LaTeX files. I would like to know
I would like to insert dates in an emacs buffer like I do in
I am new to emacs and I would like to use emacs-rails for editing
I would like to to try Emacs, and want to give it the best

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.