From 26903ae6c025f9935c6017eeb9e93406247f73a5 Mon Sep 17 00:00:00 2001 From: Pierre Neidhardt Date: Tue, 11 Feb 2014 01:56:20 +0100 Subject: [PATCH] Emacs: improved printf skeleton --- .emacs.d/functions.el | 8 ++++++++ .emacs.d/mode-cc.el | 23 ++++++++++++++--------- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/.emacs.d/functions.el b/.emacs.d/functions.el index 95df0a37..9b225b38 100644 --- a/.emacs.d/functions.el +++ b/.emacs.d/functions.el @@ -361,3 +361,11 @@ end of line after an ' = ' separtor." (setq matches (1+ matches))) matches)) +(defun count-percents (string) + "Return number of times % occurs in string, %% exluded." + (let ((start 0) (matches 0)) + (while (string-match "%." string start) + (unless (string= (match-string 0 string) "%%") + (setq matches (1+ matches))) + (setq start (match-end 0))) + matches)) diff --git a/.emacs.d/mode-cc.el b/.emacs.d/mode-cc.el index 24120d9d..e92c5e6f 100644 --- a/.emacs.d/mode-cc.el +++ b/.emacs.d/mode-cc.el @@ -129,8 +129,7 @@ restored." ;;============================================================================== ;; TODO: elements: (setq skeleton-further-elements '((q "\""))) -;; TODO: print: simpler version? -;; TODO: same number of prompt than of % +;; TODO: insert % variables progressively (add-hook 'c++-mode-hook (lambda () @@ -138,13 +137,19 @@ restored." (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 +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) ");") - ) -)) + '(setq v2 (count-percents v1)) + '(setq v1 0) + '(setq str "") + '(while (< v1 v2) + (setq v1 (1+ v1)) + (setq str (concat str ", " (skeleton-read "Value: ")))) + str + ");") + + ))