Emacs: improved printf skeleton

master
Pierre Neidhardt 2014-02-11 01:56:20 +01:00
parent 1cb803c4dc
commit 26903ae6c0
2 changed files with 22 additions and 9 deletions

View File

@ -361,3 +361,11 @@ end of line after an ' = ' separtor."
(setq matches (1+ matches))) (setq matches (1+ matches)))
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))

View File

@ -129,8 +129,7 @@ restored."
;;============================================================================== ;;==============================================================================
;; TODO: elements: (setq skeleton-further-elements '((q "\""))) ;; TODO: elements: (setq skeleton-further-elements '((q "\"")))
;; TODO: print: simpler version? ;; TODO: insert % variables progressively
;; TODO: same number of prompt than of %
(add-hook (add-hook
'c++-mode-hook 'c++-mode-hook
(lambda () (lambda ()
@ -138,13 +137,19 @@ restored."
(define-skeleton snip-print (define-skeleton snip-print
"fprintf/printf snippet "fprintf/printf snippet
If no file descriptor is provided, switch do printf. If no file descriptor is provided, switch do printf. The format
string is properly parsed (%% are not taken into account)."
The format string is properly parsed (%% are not taken into account)." nil nil
'(setq v1 (skeleton-read "File desc: " "stdout")) '(setq v1 (skeleton-read "File desc: " "stdout"))
(if (string= v1 "") "printf (" (concat "fprintf (" v1 ", ")) (if (string= v1 "") "printf (" (concat "fprintf (" v1 ", "))
"\"" (setq v1 (skeleton-read "Format string: " "%s\\n")) "\"" "\"" (setq v1 (skeleton-read "Format string: " "%s\\n")) "\""
(if (not (string-match "\\([^%]\\|^\\)\\(%%\\)*%[^%]" v1)) ");" "" ) | '(setq v2 (count-percents v1))
(nil ("Value: " ", " str) ");") '(setq v1 0)
) '(setq str "")
)) '(while (< v1 v2)
(setq v1 (1+ v1))
(setq str (concat str ", " (skeleton-read "Value: "))))
str
");")
))