format-time-stringのような関数を実装する最も簡単な方法は何ですか


12

この format-time-string 関数は文字列を受け取り、その文字列内の一連の特別な構成(先頭にが%付いた文字)を特定のテキストに置き換えます。

そのような機能を自分の関数に実装したいと思います。

  • 文字と記号の間に次のような連想リストがあります'((?n . name) (?r . reputation))
  • 関数はのような文字列を取る必要があります"My name is %n, and my rep is %r"
  • それは交換する必要がある%n%rの変数の値を持つnamereputation、その結果を返します。
  • %%同様に処理する必要がありますformat-time-string(それをに置き換えてください%)。

この機能を実装する最も簡単な方法は何ですか?
これを容易にするライブラリまたは関数はありますか?%%正しく処理することが重要です。

回答:


19

使用されるデフォルトの機能があるformat-specformat-spec-make

(let* ((name "Malabarba")
       (reputation "good")
       (fs (format-spec-make ?n name ?r reputation)))
  (format-spec "My name is %n, with a %r reputation.  %% is kept." fs))

1
Emacsにそのようなものが存在することを期待していませんでした。特に良いのは、無効な形式コードが含まれている場合の初歩的なエラー処理があることです。
wasamasa 2015年

7

elispには文字列を処理するためのかなり原始的な機能があるため、フォーマット文字列をバッファに入れ、それを反復するのが良いでしょう:

(defvar malabarba-alist '((?n . "Malabarba") (?r . 7488) (?% . "%")))

(defun malabarba-format (string)
  (with-temp-buffer
    (insert string)
    (goto-char 1)
    (while (search-forward "%" nil t)
      (let ((s (cdr (assoc (char-after) malabarba-alist))))
        (cond
          (s (delete-char -1) (delete-char 1) (insert (format "%s" (eval s))))
          (t (unless (eobp) (forward-char))))))
    (buffer-string)))

微妙な微妙さ— バッファの最後に戻る%ため、文字列の最後に孤独がある場合、これは壊れません。char-afternil


はい、@ AlanShutkoでも使用されている、文字列を1回処理するこのアプローチは、より理にかなっています。回答を削除しました。
2015年

3

これが、偽のカレンダーのformat-time-stringに使用するコードです。

(defun mystcal-format-time (format-string time)
  "Format time
%Y is the year.
%m is the numeric month.
%B is the full name of the month.
%d is the day of the month, zero-padded, %e is blank-padded.
%u is the numeric day of week from 1 (Monday) to 7, %w from 0 (Sunday) to 6.
%A is the locale's full name of the day of week.
%H is the hour on a 24-hour clock, %I is on a 12-hour clock, %k is like %H
 only blank-padded, %l is like %I blank-padded.
%p is the locale's equivalent of either AM or PM.
%M is the minute.
%S is the second."
  (let* (output
         (start 0)
         (decoded-time (if (listp time)
                           time
                         (mystcal-decode-time time)))
         (turn (nth 0 decoded-time))
         (minute (nth 1 decoded-time))
         (hour (nth 2 decoded-time))
         (day (nth 3 decoded-time))
         (month (mystcal-month decoded-time))
         (year (nth 5 decoded-time))
         (weekday (mystcal-weekday-of-day day))
         (hour12 (mod hour 12)))
    (save-match-data
      (while (string-match "%" format-string start)
        (let ((index (match-beginning 0)))
          ;; First copy non-format text
          (setq output (concat output (substring format-string start 
                                                 index)))
          ;; Process format codes here
          (let (fmted)
            (setq output (concat output 
                                 (case (aref format-string (1+ index))
                                   (?Y (number-to-string year))
                                   (?m (number-to-string month))
                                   (?B (mystcal-month-name month))
                                   (?d (format "%02d" day))
                                   (?e (format "%2d" day))
                                   (?u (number-to-string (if (zerop weekday)
                                                             7
                                                           weekday)))
                                   (?w (number-to-string weekday))
                                   (?A (mystcal-weekday-name 
                                        (mystcal-weekday-of-day day)))
                                   (?H (format "%02d" hour))
                                   (?k (format "%2d" hour))
                                   (?I (format "%02d" (if (zerop hour12) 12 hour12)))
                                   (?l (format "%2d" (if (zerop hour12) 12 hour12)))
                                   (?p (if (< hour 12)
                                           "AM" "PM"))
                                   (?M (format "%02d" minute))
                                   (?S "00")))))
          (setq start (+ 2 index)))))
    (setq output (concat output (substring format-string start)))
    output))
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.