From 1cb803c4dc51cb8842cb8d06179fc2bec0feec3b Mon Sep 17 00:00:00 2001 From: Pierre Neidhardt Date: Tue, 11 Feb 2014 00:45:27 +0100 Subject: [PATCH] Emacs: printf skeleton --- .emacs.d/functions.el | 8 ++++++++ .emacs.d/mode-cc.el | 25 +++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/.emacs.d/functions.el b/.emacs.d/functions.el index 1af167ab..95df0a37 100644 --- a/.emacs.d/functions.el +++ b/.emacs.d/functions.el @@ -353,3 +353,11 @@ end of line after an ' = ' separtor." (insert " = " (calc-eval (buffer-substring-no-properties (line-beginning-position) (line-end-position))))) +(defun count-occurences (regex string) + "Return number of times regex occurs in string." + (let ((start 0) (matches 0)) + (while (string-match regex string start) + (setq start (match-end 0)) + (setq matches (1+ matches))) + matches)) + diff --git a/.emacs.d/mode-cc.el b/.emacs.d/mode-cc.el index 94c4b0b4..24120d9d 100644 --- a/.emacs.d/mode-cc.el +++ b/.emacs.d/mode-cc.el @@ -123,3 +123,28 @@ restored." (add-to-list 'semantic-lex-c-preprocessor-symbol-file (concat qt4-base-dir "/Qt/qconfig.h")) (add-to-list 'semantic-lex-c-preprocessor-symbol-file (concat qt4-base-dir "/Qt/qconfig-large.h")) (add-to-list 'semantic-lex-c-preprocessor-symbol-file (concat qt4-base-dir "/Qt/qglobal.h"))))) + +;;============================================================================== +;; Skel +;;============================================================================== +;; TODO: elements: (setq skeleton-further-elements '((q "\""))) + +;; TODO: print: simpler version? +;; TODO: same number of prompt than of % +(add-hook + 'c++-mode-hook + (lambda () + + (define-skeleton snip-print + "fprintf/printf snippet + +If no file descriptor is provided, switch do printf. + +The format string is properly parsed (%% are not taken into account)." nil + '(setq v1 (skeleton-read "File desc: " "stdout")) + (if (string= v1 "") "printf (" (concat "fprintf (" v1 ", ")) + "\"" (setq v1 (skeleton-read "Format string: " "%s\\n")) "\"" + (if (not (string-match "\\([^%]\\|^\\)\\(%%\\)*%[^%]" v1)) ");" "" ) | + (nil ("Value: " ", " str) ");") + ) +))