diff options
author | JSDurand <mmemmew@gmail.com> | 2021-08-21 11:36:18 +0800 |
---|---|---|
committer | JSDurand <mmemmew@gmail.com> | 2021-08-21 11:36:18 +0800 |
commit | 36f8bee3404cdd0960eb74a29b3f61c525bd55a5 (patch) | |
tree | dad648d25148beafc9c697356a43373c81556cde | |
parent | bc2eb293e2006da99040ebee065b2a7485a73cd2 (diff) |
c-conf: re-bind and add include function
* c-conf.el (durand-c-prefix-map): Bind the pragma and the include
functions.
(c-mode-map): Bind the prefix map.
(durand-c-include): Generate the include directives automatically.
-rw-r--r-- | c-conf.el | 45 |
1 files changed, 43 insertions, 2 deletions
@@ -2,7 +2,7 @@ ;; Copyright (C) 2021 李俊緯 -;; Author: 李俊緯 <durand@MacBook-Pro-de-Severe.local> +;; Author: Durand <mmemmew@gmail.com> ;; Keywords: c, convenience, languages ;; This program is free software; you can redistribute it and/or modify @@ -32,8 +32,17 @@ ;; This proves to be too convenient not to bind. (define-key c-mode-map (vector 'S-tab) #'ff-find-other-file) +;;; prefix map +(defvar durand-c-prefix-map '(keymap) + "The keymap of my custom functions. +This will be bound to the key ù.") + +(let ((map durand-c-prefix-map)) + (define-key map (vector ?i) #'durand-c-include) + (define-key map (vector ?p) #'c-generate-pragma)) + ;; generate pragmas automatically. -(define-key c-mode-map (vector ?ù) #'c-generate-pragma) +(define-key c-mode-map (vector ?ù) durand-c-prefix-map) ;;;###autoload (defun c-generate-pragma () @@ -65,5 +74,37 @@ If invoked on a C file, include the corresponding header file." "c\\(p*\\)$" "h\\1" (buffer-name)))) (newline)))) +;;;###autoload +(defun durand-c-include (&optional arg) + "Include a library. +If ARG is non-nil, ask for the type of the include: whether the +angle bracket type or the quote type." + (interactive) + (let* ((library (read-string "Include: ")) + (type (cond + ((null arg) + (cond + ((string-match-p "^\\(std\\|string.h\\)" library) + 'angle) + ('quote))) + (t + (intern + (completing-read "Which type? " + '("angle" "quote") + nil t)))))) + (save-restriction + (widen) + (save-excursion + (goto-char (point-min)) + (re-search-forward "^#include" nil t) + (forward-line 0) + (insert "#include " + (cond ((eq type 'angle) "<") + ((eq type 'quote) "\"")) + library + (cond ((eq type 'angle) ">") + ((eq type 'quote) "\"")) + "\n"))))) + (provide 'c-conf) ;;; c-conf.el ends here |