cmake-font-lock - Advanced, type aware, highlight support for CMake

Overview

cmake-font-lock - Advanced, type aware, highlight support for CMake

Author: Anders Lindgren
Version: 0.1.12
URL: https://github.com/Lindydancer/cmake-font-lock

Advanced syntax coloring support for CMake scripts.

The major feature of this package is to highlighting function arguments according to their use. For example:

Example CMake script

CMake, as a programming language, has a very simple syntax. Unfortunately, this makes it hard to read CMake scripts. CMake supports function calls, but they may not be nested. In addition, the functions do not support return values like normal programming languages, instead a function is capable of setting variables in the scope of the parent. In other words, a parameter could be anything like the name of a variable, a keyword, a property, or even a plain string.

By highlighting each argument, CMake scripts becomes much easier to read, and also to write.

This package is aware of all built-in CMake functions, as provided by CMake 3.22.1. In addition, it allows you to add function signatures for your own functions.

The following is colored

  • Function arguments are colored according to it's use, An argument can be colored as a keyword, a variable, a property, or a target. This package provides information on all built-in CMake functions. Information on user-defined functions can be added.
  • All function names are colored, however, special functions like if, while, function, and include are colored using a different color.
  • The constants true, false, yes, no, y, n, on, and off.
  • The constructs ${...}, $ENV{...}, and $ .
  • In preprocessor definitions like -DNAME, NAME is colored.
  • Comments and quoted strings.

Background

This package is designed to be used together with a major mode for editing CMake files. Once such package is cmake-mode.el distributed by Kitware. However this package is not dependent upon or associated with any specific CMake major mode. (Note that the Kitware package contains rudimentary syntax coloring support, this package replaces that part of the major mode.)

Installation

Install this package with Emacs' package manager.

When installed, this package is automatically activated when using CMake mode, or any other mode in the list cmake-font-lock-modes. Set this variable to nil to disable automatic initialization.

Customizing

In order to perform syntax coloring correctly, this package has to be aware of the keywords and types of the CMake functions used. To add information about non-standard CMake function, the following functions can be used:

cmake-font-lock-add-keywords -- Add keyword information

Adding the list of keywords to a function is a simple way to get basic coloring correct. For most functions, this is sufficient. For example:

    (cmake-font-lock-add-keywords
       "my-func" '("FILE" "RESULT" "OTHER"))

cmake-font-lock-set-signature -- Set full function type

Set the signature (the type of the arguments) for a function. For example:

    (cmake-font-lock-set-signature
       "my-func" '(:var nil :prop) '(("FILE" :file) ("RESULT" :var)))

Custom types

The signatures of CMake functions provided by this package use a number of types (see cmake-font-lock-function-signatures for details). However, when adding new signatures, it's possible to use additional types. In that case, the variable cmake-font-lock-argument-kind-face-alist must be modified to map the CMake type to a concrete Emacs face. For example:

(cmake-font-lock-set-signature "my_warn" (:warning))
(add-to-list '(:warning . font-lock-warning-face)
             cmake-font-lock-argument-kind-face-alist)

Problems

  • In CMake, function taking expressions, like if and while, treat any string as the name of a variable, if one exists. This mode fontifies all such as variables, whether or not they actually refer to variable. You can quote the arguments to fontify them as strings (even though that will not prevent CMake from interpreting them as variables).

  • Normally, keywords are written explicitly when calling a function. However, it is legal to assigning them to a variable, which is expanded at the time the function is called. In this case, remaining arguments will not be colored correctly. For example:

      set(def DEFINITION var2)
      get_directory_property(var1 ${def} my_property)
    

Converted from cmake-font-lock.el by el2markdown.

Comments
  • font-lock-activate isue with hooks

    font-lock-activate isue with hooks

    Hi:

    I am trying to use this package with the hooks, but it is not working.

    After some debugging I just found that the problem is that font-lock-mode is nil in that moment because it is set later; but you can use the global variable global-font-lock-mode. which is already set in that moment:

    Something like:

    (when (and (or font-lock-mode  global-font-lock-mode)
               (fboundp 'font-lock-refresh-defaults))
          (font-lock-refresh-defaults)))
    

    In cmake-font-lock-activate could fix this issue.

    opened by Ergus 8
  • Performance issue with large CMakeLists.txt files

    Performance issue with large CMakeLists.txt files

    Using current master version and a large CMakeLists.txt file, adding a new line starting with set( in the beginning of the file will make emacs use a lot of cpu on each key stroke until the set(...) block is closed.

    This make this mode unusable in large projects as each new character typed takes a noticeable time to display (~100-300ms).

    It's probably due to a whole buffer look ahead to find the closing ), but it's too costly.

    It's easy to reproduce with CMake's own CMakeLists.txt: add a set(bla on the first line, and continue typing new characters.

    opened by Niluge-KiWi 6
  • Updates for syntax up to CMake 3.14.0

    Updates for syntax up to CMake 3.14.0

    I based this on the head of repo MarkaPola/cmake-font-lock then added extra updates for CMake 3.14.0.

    It would be nice to have a version with these changes available in ELPA.

    Thanks!

    opened by madscientist 5
  • andersl-cmake-font-lock-activate: Symbol's function definition is void: font-lock-refresh-defaults

    andersl-cmake-font-lock-activate: Symbol's function definition is void: font-lock-refresh-defaults

    On Emacs 23 symbol's function definition is void: font-lock-refresh-defaults

    A quickfix:

    --- a/andersl-cmake-font-lock.el
    +++ b/andersl-cmake-font-lock.el
    @@ -782,7 +782,8 @@ To activate this every time a CMake file is opened, use the following:
       (if (not font-lock-mode)
           (andersl-cmake-font-lock-setup)
         (andersl-cmake-font-lock-setup)
    -    (font-lock-refresh-defaults)))
    +    (when (fboundp 'font-lock-refresh-defaults)
    +      (font-lock-refresh-defaults))))
    
    
     (defun andersl-cmake-font-lock-add-keywords (name keywords)
    
    opened by Niluge-KiWi 3
  • Using with font-lock-add-keywords breaks

    Using with font-lock-add-keywords breaks

    Hi,

    Thank you for the really awesome package! It is really great :)

    I normally add some custom highlighting of keywords in prog-mode, eg FIXME, TODO. However, when I do that cmake-font-lock won't activate from the cmake-mode-hook, only if I call it manually. My current workaround solution is to not enable the keyword highlighting in cmake-mode:

    ;; Highlight some keywords in prog-mode
    (add-hook 'prog-mode-hook
              (lambda ()
                ;; Highlighting in cmake-mode this way interferes with
                ;; cmake-font-lock, which is something I don't yet understand.
                (when (not (derived-mode-p 'cmake-mode))
                  (font-lock-add-keywords
                   nil
                   '(("\\<\\(FIXME\\|TODO\\|BUG\\|DONE\\)" 1 font-lock-warning-face t)
                     )
                   )
                  )
                )
              )
    

    So the steps I take are:

    1. Start Emacs opening a CMakeLists.txt file
    2. Using (message) I deteremined first the prog-mode-hook above is called, then the cmake-mode-hook is called
    3. No cmake-font-lock
    4. Run M-x cmake-font-lock-activate enables cmake-font-lock

    Here is how I set up cmake-mode:

    (use-package cmake-mode
      :ensure t
      :mode ("CMakeLists.txt" ".cmake")
      :hook (cmake-mode . (lambda ()
                            (add-to-list 'company-backends 'company-cmake)))
      :config
      (use-package cmake-font-lock
        :ensure t
        :defer t
        :commands (cmake-font-lock-activate)
        :hook (cmake-mode . (lambda ()
                              (message "\nActivating font lock?\n")
                              (cmake-font-lock-activate)))
        )
      )
    

    Do you know why this is happening? It would be useful to note on the README that the above workaround is necessary to get cmake-font-lock to activate properly. :)

    Thank you!

    opened by nilsdeppe 2
  • Conflict with package rainbow-delimiters

    Conflict with package rainbow-delimiters

    Hi, When I use cmake-font-lock along with rainbow-delimiters, cmake-font-lock does not have any visible effect on the buffer. When rainbow-delimiters is disabled I recover correct coloring.

    Here is a minimal setup with use-package

    (use-package rainbow-delimiters
      :ensure t
      :hook (prog-mode . rainbow-delimiters-mode))
    
    (use-package cmake-mode
      :ensure t)
    
    (use-package cmake-font-lock
      :ensure t
      :hook (cmake-mode . cmake-font-lock-activate))
    
    1. Would it be easy to fix ?
    2. Have you even considered pushing your changes to the kitware cmake-mode.el ? This is really enjoyable to have this coloring.

    Thanks

    opened by tpadioleau 14
  • Possible integration with new project

    Possible integration with new project

    Hi,
    I'm currently working on a new project, which I'm calling extended-cmake-mode to integrate cmake-mode and everything good about this project, and build on them to create a true IDE experience. I was looking to include this project as a dependency, but it looks like the project isn't on MELPA. I was wondering if you were willing to either team up with me and bring the features from here to extended-cmake-mode, or submit a recipe to MELPA.

    Thanks

    opened by jdemilledt 2
  • function-signatures versus function-keywords ... overlap?

    function-signatures versus function-keywords ... overlap?

    I'm no expert on Emacs font-lock implementations, but it feels to me like there's some overlap between cmake-font-lock-function-keywords-alist and cmake-font-lock-function-signatures.

    I feel like function-signatures is (or would be if it was complete) a superset of the information in keywords-alist, in that if all the function-signatures were completed then all the keywords would be specified by the function signatures. Is there a reason to keep two separate settings?

    This mainly came up for me because while adding keywords for my recent pull request I found myself adding the PERMISSIONS keywords for the install() command, which has syntax like: install(... PERMISSIONS OWNER_READ OWNER_WRITE ...) I was thinking that while PERMISSIONS is a keyword, the options such as OWNER_READ etc. might be better font-locked as a different type of thing. Maybe a property font or something like that.

    So I looked further and found function-signatures where I could define the entire syntax of the function... but then I got confused about what the distinction is between these two and when one should use one versus the other, and why there are even two of them when it seems like if all functions were fully specified by function-signatures then why do we need function-keywords?

    So I thought I'd ask :smile:

    opened by madscientist 3
  • font-lock-refresh-defaults is only called once

    font-lock-refresh-defaults is only called once

    Hi, I'm not sure if this is expected behavior from font-lock or there's something wrong with my setup. The issue I noticed is that, font-lock-mode is only t when I open a file in cmake-mode for the first time. For subsequent files opened in cmake-mode, font-lock-mode is nil, so, font-lock-refresh-defaults is not called. Font locking still works normally,font-lock-mode is only nil during the execution of cmake-mode-hook, it is t when I manually check after loading has finished.

    Since all font-lock-refresh-defaults does is killing the local variable font-lock-set-defaults, I've modified the function cmake-font-lock-activate as follow and everything now works as expected.

    (defun cmake-font-lock-activate ()
      "Activate advanced CMake colorization.
    
    To activate this every time a CMake file is opened, use the following:
    
        (add-hook 'cmake-mode-hook 'cmake-font-lock-activate)"
      (interactive)
      (cmake-font-lock-setup)
      (when (boundp 'font-lock-set-defaults)
        (kill-local-variable 'font-lock-set-defaults))
      (when (and (boundp 'font-lock-mode) font-lock-mode)
        (font-lock-mode -1)
        (font-lock-mode 1)))
    
    opened by haqle 3
  • Variable expansion kills font lock on keywords

    Variable expansion kills font lock on keywords

    For example, in

    install(DIRECTORY ./
      DESTINATION ${INSTALL_MODULES_DIR}
      COMPONENT Development
      FILES_MATCHING PATTERN *.cmake)
    

    only DIRECTORY and DESTINATION are properly font locked. Others, COMPONENT, FILES_MATCHING, and PATTERN, are not font locked. However, if I enclose variable expansion into quotes, i.e.

    install(DIRECTORY ./
      DESTINATION "${INSTALL_MODULES_DIR}"
      COMPONENT Development
      FILES_MATCHING PATTERN *.cmake)
    

    all keywords are font locked properly. Although, I didn't study the implementation in details, I tend to think that if this is not a bug but at least an unintended side effect. What blocks the current implementation from mitigating this problem on variable expansions? Instead of " as anchors, one has ${ and }. I've read the second problem in README but it makes little sense to me in this case.

    opened by Alexander-Shukaev 1
Owner
Anders Lindgren
Anders Lindgren
📦 CMake's missing package manager. A small CMake script for setup-free, cross-platform, reproducible dependency management.

Setup-free CMake dependency management CPM.cmake is a CMake script that adds dependency management capabilities to CMake. It's built as a thin wrapper

CPM.cmake 1.6k Jan 9, 2023
CMake checks cache helper modules – for fast CI CMake builds!

cmake-checks-cache Cross platform CMake projects do platform introspection by the means of "Check" macros. Have a look at CMake's How To Write Platfor

Cristian Adam 65 Dec 6, 2022
CMake scripts for painless usage of SuiteSparse+METIS from Visual Studio and the rest of Windows/Linux/OSX IDEs supported by CMake

CMake scripts for painless usage of Tim Davis' SuiteSparse (CHOLMOD,UMFPACK,AMD,LDL,SPQR,...) and METIS from Visual Studio and the rest of Windows/Lin

Jose Luis Blanco-Claraco 395 Dec 24, 2022
cmake-avr - a cmake toolchain for AVR projects

cmake-avr - a cmake toolchain for AVR projects Testing the example provided The toolchain was created and tested within the following environment: Lin

Matthias Kleemann 163 Dec 5, 2022
Make CMake less painful when trying to write Modern Flexible CMake

Izzy's eXtension Modules IXM is a CMake library for writing Modern flexible CMake. This means: Reducing the amount of CMake written Selecting reasonab

IXM 107 Sep 1, 2022
CMake module to enable code coverage easily and generate coverage reports with CMake targets.

CMake-codecov CMake module to enable code coverage easily and generate coverage reports with CMake targets. Include into your project To use Findcodec

HPC 82 Nov 30, 2022
unmaintained - CMake module to activate certain C++ standard, feature checks and appropriate automated workarounds - basically an improved version of cmake-compile-features

Compatibility This library provides an advanced target_compile_features() and write_compiler_detection_header(). The problem with those is that they a

Jonathan Müller 74 Dec 26, 2022
[CMake] [BSD-2] CMake module to find ICU

FindICU.cmake A CMake module to find International Components for Unicode (ICU) Library Note that CMake, since its version 3.7.0, includes a FindICU m

julp 29 Nov 2, 2022
CMake project for BL602 RISC-V processor

bl602_cmake_base CMake project for BL602 RISC-V processor How to build NOTE : This project uses a pre-compiled version of the Buffalo SDK (bl_iot_sdk)

null 9 Jan 6, 2022
A CMake toolchain file for iOS, macOS, watchOS & tvOS C/C++/Obj-C++ development

A CMake toolchain file for iOS, macOS, watchOS & tvOS C/C++/Obj-C++ development

Alexander Widerberg 1.4k Jan 4, 2023
curl cmake module libcurl build with msvc x86

curl-msvc Infomation curl cmake module libcurl build with MSVC10.0 arch (x86 | i386) source from https://github.com/curl/curl tags: curl-7_79_1 Usage

Jason Payne 0 May 16, 2022
NeoWorld is a resampler using the CMake build system

NeoWorld is a resampler using the CMake build system. It's designed for utsu, OpenUTAU, and UTAU.

null 5 Dec 23, 2022
A CMake addon that avoids you writing boilerplate code for resource management.

SHader INJ(I)ector SHINJI (originally SHader INJector) is a CMake addon that avoids you writing boilerplate code for resource management and exposes s

Lorenzo Rutayisire 6 Dec 14, 2022
A CMake starter template using CPM

Cmake Starter About A lightweight Cmake project meant for a binary application (not a shared library), tests with catch2 are configured, CPM is the pa

Matt Williams 1 Jul 14, 2022
Non-intrusive CMake dependency management

cmodule Non-intrusive CMake dependency management. Normally CMake's find_package() looks for packages installed on host system (and compiled for host

scapix.com 14 Sep 29, 2022
Simple library for embedding static resources into C++ binaries using CMake

libromfs libromfs is an easy way to bundle resources directly into any C++ application and access them through a simple interface. The main advantage

WerWolv 28 Nov 30, 2022
Project to enable using CMake from a Maven build.

CMake-Maven-Project Introduction A Maven project for the CMake build system. It can be used by including it as a plugin within your Maven project's po

null 60 Nov 14, 2022
Autotools-style configure script wrapper around CMake

configure-cmake configure-cmake is an autotools-style configure script for CMake-based projects. People building software on Linux or BSD generally ex

Evan Nemerson 82 Dec 14, 2022
Check for coding style issues in CMake files

cmakelint parses CMake files and reports style issues. cmakelint requires Python. Installation sudo pip install cmakelint Usage Syntax: cmakelint [--c

Richard Quirk 99 Dec 10, 2022