From 36f8bee3404cdd0960eb74a29b3f61c525bd55a5 Mon Sep 17 00:00:00 2001 From: JSDurand Date: Sat, 21 Aug 2021 11:36:18 +0800 Subject: 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. --- c-conf.el | 45 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/c-conf.el b/c-conf.el index e98ddbd..b6cad71 100644 --- a/c-conf.el +++ b/c-conf.el @@ -2,7 +2,7 @@ ;; Copyright (C) 2021 李俊緯 -;; Author: 李俊緯 +;; Author: Durand ;; 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 -- cgit v1.2.3-18-g5258