CMake/Editors/Emacs: Difference between revisions

From KitwarePublic
Jump to navigationJump to search
(Split "CMake/Editors/Emacs" page out from "CMake Editors Support")
 
(Replace content with link to new CMake community wiki)
 
(19 intermediate revisions by 4 users not shown)
Line 1: Line 1:
* '''Emacs''' [http://www.cmake.org/CMakeDocs/cmake-mode.el combined syntax highlighting and indentation mode].  The file in the repository used to not function properly when running fill-paragraph (M-q).  There was a [[CMake Emacs mode patch for comment formatting|patched version]] to fix this, but it is largely unnecessary now that the one in the repository works properly now.  To enable it, add the following to your ''.emacs'' file:
{{CMake/Template/Moved}}


<pre>
This page has moved [https://gitlab.kitware.com/cmake/community/wikis/doc/editors/Emacs here].
; Add cmake listfile names to the mode list.
(setq auto-mode-alist
  (append
  '(("CMakeLists\\.txt\\'" . cmake-mode))
  '(("\\.cmake\\'" . cmake-mode))
  auto-mode-alist))
 
(autoload 'cmake-mode "~/CMake/Docs/cmake-mode.el" t)
</pre>
 
I've been long irritated with having to deal with multiple buffers all name CMakeLists.txt.  Emacs by default will call them CMakeLists.txt, CMakeLists.txt<2>, CMakeLists.txt<3>, etc..  This is really hard to switch back and forth when the buffer names are difficult to associate with location.
 
I've found a couple of solutions to this problem.
 
<ol><li>Use uniquify emacs package.  This gives several options to automatically rename buffers based on their location on disk.
 
<pre>
;; uniquify.el is a helper routine to help give buffer names a better unique name.
(when (load "uniquify" 'NOERROR)
  (require 'uniquify)
  (setq uniquify-buffer-name-style 'forward)
  ;(setq uniquify-buffer-name-style 'post-forward)
  )
</pre>
</li>
 
<li>Rename the buffer as part of the cmake-mode
 
<pre>
(defun cmake-rename-buffer ()
  "Renames a CMakeLists.txt buffer to cmake-<directory name>."
  (interactive)
  ;(print (concat "buffer-filename = " (buffer-file-name)))
  ;(print (concat "buffer-name    = " (buffer-name)))
  (when (and (buffer-file-name) (string-match "CMakeLists.txt" (buffer-name)))
      ;(setq file-name (file-name-nondirectory (buffer-file-name)))
      (setq parent-dir (file-name-nondirectory (directory-file-name (file-name-directory (buffer-file-name)))))
      ;(print (concat "parent-dir = " parent-dir))
      (setq new-buffer-name (concat "cmake-" parent-dir))
      ;(print (concat "new-buffer-name= " new-buffer-name))
      (rename-buffer new-buffer-name t)
      )
  )
 
(add-hook 'cmake-mode-hook (function cmake-rename-buffer))
</pre>
</li></ol>
 
I actually prefer renaming my buffers with my cmake-rename-buffer function, because the buffer names start with a lower case letter. :)

Latest revision as of 15:40, 30 April 2018


The CMake community Wiki has moved to the Kitware GitLab Instance.

This page has moved here.