Home | History | Annotate | Line # | Download | only in misc
po-mode.el revision 1.1.1.1
      1 ;;; po-mode.el -- major mode for GNU gettext PO files
      2 
      3 ;; Copyright (C) 1995-1999, 2000-2002, 2005-2006 Free Software Foundation, Inc.
      4 
      5 ;; Authors: Franois Pinard <pinard (at) iro.umontreal.ca>
      6 ;;          Greg McGary <gkm (at) magilla.cichlid.com>
      7 ;; Keywords: i18n gettext
      8 ;; Created: 1995
      9 
     10 ;; This file is part of GNU gettext.
     11 
     12 ;; GNU gettext is free software; you can redistribute it and/or modify
     13 ;; it under the terms of the GNU General Public License as published by
     14 ;; the Free Software Foundation; either version 2, or (at your option)
     15 ;; any later version.
     16 
     17 ;; GNU gettext is distributed in the hope that it will be useful,
     18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
     19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     20 ;; GNU General Public License for more details.
     21 
     22 ;; You should have received a copy of the GNU General Public License
     23 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
     24 ;; Free Software Foundation, 51 Franklin Street, Fifth Floor,
     25 ;; Boston, MA 02110-1301, USA.
     26 
     27 ;;; Commentary:
     28 
     29 ;; This package provides the tools meant to help editing PO files,
     30 ;; as documented in the GNU gettext user's manual.  See this manual
     31 ;; for user documentation, which is not repeated here.
     32 
     33 ;; To install, merely put this file somewhere GNU Emacs will find it,
     34 ;; then add the following lines to your .emacs file:
     35 ;;
     36 ;;   (autoload 'po-mode "po-mode"
     37 ;;             "Major mode for translators to edit PO files" t)
     38 ;;   (setq auto-mode-alist (cons '("\\.po\\'\\|\\.po\\." . po-mode)
     39 ;;				  auto-mode-alist))
     40 ;;
     41 ;; To use the right coding system automatically under Emacs 20 or newer,
     42 ;; also add:
     43 ;;
     44 ;;   (autoload 'po-find-file-coding-system "po-compat")
     45 ;;   (modify-coding-system-alist 'file "\\.po\\'\\|\\.po\\."
     46 ;;				  'po-find-file-coding-system)
     47 ;;
     48 ;; You may also adjust some variables, below, by defining them in your
     49 ;; '.emacs' file, either directly or through command 'M-x customize'.
     50 
     51 ;;; Code:
     52 
     53 (defconst po-mode-version-string "2.02" "\
     55 Version number of this version of po-mode.el.")
     56 
     57 ;;; Emacs portability matters - part I.
     58 ;;; Here is the minimum for customization to work.  See part II.
     59 
     60 ;; Identify which Emacs variety is being used.
     61 ;; This file supports:
     62 ;;   - XEmacs (version 19 and above) -> po-XEMACS = t,
     63 ;;   - GNU Emacs (version 20 and above) -> po-EMACS20 = t,
     64 ;;   - GNU Emacs (version 19) -> no flag.
     65 (eval-and-compile
     66   (cond ((string-match "XEmacs\\|Lucid" emacs-version)
     67 	 (setq po-EMACS20 nil po-XEMACS t))
     68 	((and (string-lessp "19" emacs-version) (featurep 'faces))
     69 	 (setq po-EMACS20 t po-XEMACS nil))
     70 	(t (setq po-EMACS20 nil po-XEMACS nil))))
     71 
     72 ;; Experiment with Emacs LISP message internationalisation.
     73 (eval-and-compile
     74   (or (fboundp 'set-translation-domain)
     75       (defsubst set-translation-domain (string) nil))
     76   (or (fboundp 'translate-string)
     77       (defsubst translate-string (string) string)))
     78 (defsubst _ (string) (translate-string string))
     79 (defsubst N_ (string) string)
     80 
     81 ;; Handle missing 'customs' package.
     82 (eval-and-compile
     83   (condition-case ()
     84       (require 'custom)
     85     (error nil))
     86   (if (and (featurep 'custom) (fboundp 'custom-declare-variable))
     87       nil
     88     (defmacro defgroup (&rest args)
     89       nil)
     90     (defmacro defcustom (var value doc &rest args)
     91       (` (defvar (, var) (, value) (, doc))))))
     92 
     93 ;;; Customisation.
     95 
     96 (defgroup po nil
     97   "Major mode for editing PO files"
     98   :group 'i18n)
     99 
    100 (defcustom po-auto-edit-with-msgid nil
    101   "*Automatically use msgid when editing untranslated entries."
    102   :type 'boolean
    103   :group 'po)
    104 
    105 (defcustom po-auto-fuzzy-on-edit nil
    106   "*Automatically mark entries fuzzy when being edited."
    107   :type 'boolean
    108   :group 'po)
    109 
    110 (defcustom po-auto-select-on-unfuzzy nil
    111   "*Automatically select some new entry while making an entry not fuzzy."
    112   :type 'boolean
    113   :group 'po)
    114 
    115 (defcustom po-auto-replace-revision-date t
    116   "*Automatically revise date in headers.  Value is nil, t, or ask."
    117   :type '(choice (const nil)
    118 		 (const t)
    119 		 (const ask))
    120   :group 'po)
    121 
    122 (defcustom po-default-file-header "\
    123 # SOME DESCRIPTIVE TITLE.
    124 # Copyright (C) YEAR Free Software Foundation, Inc.
    125 # FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
    126 #
    127 #, fuzzy
    128 msgid \"\"
    129 msgstr \"\"
    130 \"Project-Id-Version: PACKAGE VERSION\\n\"
    131 \"PO-Revision-Date: YEAR-MO-DA HO:MI +ZONE\\n\"
    132 \"Last-Translator: FULL NAME <EMAIL@ADDRESS>\\n\"
    133 \"Language-Team: LANGUAGE <LL (at) li.org>\\n\"
    134 \"MIME-Version: 1.0\\n\"
    135 \"Content-Type: text/plain; charset=CHARSET\\n\"
    136 \"Content-Transfer-Encoding: 8bit\\n\"
    137 "
    138   "*Default PO file header."
    139   :type 'string
    140   :group 'po)
    141 
    142 (defcustom po-translation-project-address
    143   "translation (at) iro.umontreal.ca"
    144   "*Electronic mail address of the Translation Project.
    145 Typing \\[po-send-mail] (normally bound to `M') the user will send the PO file
    146 to this email address."
    147   :type 'string
    148   :group 'po)
    149 
    150 (defcustom po-translation-project-mail-label "TP-Robot"
    151   "*Subject label when sending the PO file to `po-translation-project-address'.
    152 Don't change it when you send PO files to \"translation (at) iro.umontreal.ca\", the
    153 Translation Project Robot at http://www.iro.umontreal.ca/contrib/po/HTML/.  If
    154 the label is different, your submission will be consiedered as a regular mail
    155 and not stored at the TP site and also not forwarded to the package maintainer."
    156   :type 'string
    157   :group 'po)
    158 
    159 (defcustom po-highlighting (or po-EMACS20 po-XEMACS)
    160   "*Highlight text whenever appropriate, when non-nil.
    161 However, on older Emacses, a yet unexplained highlighting bug causes files
    162 to get mangled."
    163   :type 'boolean
    164   :group 'po)
    165 
    166 (defcustom po-highlight-face 'highlight
    167   "*The face used for PO mode highlighting.  For Emacses with overlays.
    168 Possible values are 'highlight', 'modeline', 'secondary-selection',
    169 'region', and 'underline'.
    170 This variable can be set by the user to whatever face they desire.
    171 It's most convenient if the cursor color and highlight color are
    172 slightly different."
    173   :type 'face
    174   :group 'po)
    175 
    176 (defcustom po-team-name-to-code
    177   ;; All possible languages, a complete ISO 639 list and a little more.
    178   '(("LANGUAGE" . "LL")
    179     ("(Afan) Oromo" . "om")
    180     ("Abkhazian" . "ab")
    181     ("Afar" . "aa")
    182     ("Afrikaans" . "af")
    183     ("Albanian" . "sq")
    184     ("Amharic" . "am")
    185     ("Arabic" . "ar")
    186     ("Argentinian" . "es_AR")
    187     ("Armenian" . "hy")
    188     ("Assamese" . "as")
    189     ("Avestan" . "ae")
    190     ("Aymara" . "ay")
    191     ("Azerbaijani" . "az")
    192     ("Bashkir" . "ba")
    193     ("Basque" . "eu")
    194     ("Belarusian" . "be")
    195     ("Bengali" . "bn")
    196     ("Bihari" . "bh")
    197     ("Bislama" . "bi")
    198     ("Bosnian" . "bs")
    199     ("Brazilian Portuguese" . "pt_BR")
    200     ("Breton" . "br")
    201     ("Bulgarian" . "bg")
    202     ("Burmese" . "my")
    203     ("Catalan" . "ca")
    204     ("Chamorro" . "ch")
    205     ("Chechen" . "ce")
    206     ("Chinese" . "zh")
    207     ("Chinese (simplified)" . "zh_CN")
    208     ("Chinese (traditional)" . "zh_TW")
    209     ("Church Slavic" . "cu")
    210     ("Chuvash" . "cv")
    211     ("Cornish" . "kw")
    212     ("Corsican" . "co")
    213     ("Croatian" . "hr")
    214     ("Czech" . "cs")
    215     ("Danish" . "da")
    216     ("Dutch" . "nl")
    217     ("Dzongkha" . "dz")
    218     ("English" . "en")
    219     ("Esperanto" . "eo")
    220     ("Estonian" . "et")
    221     ("Faroese" . "fo")
    222     ("Fijian" . "fj")
    223     ("Finnish" . "fi")
    224     ("French" . "fr")
    225     ("Frisian" . "fy")
    226     ("Galician" . "gl")
    227     ("Georgian" . "ka")
    228     ("German" . "de")
    229     ("Greek" . "el")
    230     ("Guarani" . "gn")
    231     ("Gujarati" . "gu")
    232     ("Hausa" . "ha")
    233     ("Hebrew" . "he")
    234     ("Herero" . "hz")
    235     ("Hindi" . "hi")
    236     ("Hiri Motu" . "ho")
    237     ("Hungarian" . "hu")
    238     ("Hyam" . "jab")
    239     ("Icelandic" . "is")
    240     ("Ido" . "io")
    241     ("Indonesian" . "id")
    242     ("Interlingua" . "ia")
    243     ("Interlingue" . "ie")
    244     ("Inuktitut" . "iu")
    245     ("Inupiak" . "ik")
    246     ("Irish" . "ga")
    247     ("Italian" . "it")
    248     ("Japanese" . "ja")
    249     ("Javanese" . "jv")
    250     ("Jju" . "kaj")
    251     ("Kagoma" . "kdm")
    252     ("Kalaallisut" . "kl")
    253     ("Kannada" . "kn")
    254     ("Kashmiri" . "ks")
    255     ("Kazakh" . "kk")
    256     ("Khmer" . "km")
    257     ("Kikuyu" . "ki")
    258     ("Kinyarwanda" . "rw")
    259     ("Kirghiz" . "ky")
    260     ("Kirundi" . "rn")
    261     ("Komi" . "kv")
    262     ("Konkani" . "kok")
    263     ("Korean" . "ko")
    264     ("Kuanyama" . "kj")
    265     ("Kurdish" . "ku")
    266     ("Laotian" . "lo")
    267     ("Latin" . "la")
    268     ("Latvian" . "lv")
    269     ("Letzeburgesch" . "lb")
    270     ("Lingala" . "ln")
    271     ("Lithuanian" . "lt")
    272     ("Low Saxon" . "nds")
    273     ("Macedonian" . "mk")
    274     ("Maithili" . "mai")
    275     ("Malagasy" . "mg")
    276     ("Malay" . "ms")
    277     ("Malayalam" . "ml")
    278     ("Maltese" . "mt")
    279     ("Manipuri" . "mni")
    280     ("Manx" . "gv")
    281     ("Maori" . "mi")
    282     ("Marathi" . "mr")
    283     ("Marshall" . "mh")
    284     ("Mayan" . "myn")
    285     ("Moldavian" . "mo")
    286     ("Mongolian" . "mn")
    287     ("Nahuatl" . "nah")
    288     ("Nauru" . "na")
    289     ("Navajo" . "nv")
    290     ("Ndonga" . "ng")
    291     ("Nepali" . "ne")
    292     ("North Ndebele" . "nd")
    293     ("Northern Sami" . "se")
    294     ("Northern Sotho" . "nso")
    295     ("Norwegian Bokmal" . "nb")
    296     ("Norwegian Nynorsk" . "nn")
    297     ("Norwegian" . "no")
    298     ("Nyanja" . "ny")
    299     ("Occitan" . "oc")
    300     ("Old English" . "ang")
    301     ("Oriya" . "or")
    302     ("Ossetian" . "os")
    303     ("Pez" . "pbb")
    304     ("Pali" . "pi")
    305     ("Pashto" . "ps")
    306     ("Persian" . "fa")
    307     ("Polish" . "pl")
    308     ("Portuguese" . "pt")
    309     ("Punjabi" . "pa")
    310     ("Quechua" . "qu")
    311     ("Rhaeto-Roman" . "rm")
    312     ("Romanian" . "ro")
    313     ("Russian" . "ru")
    314     ("Samoan" . "sm")
    315     ("Sango" . "sg")
    316     ("Sanskrit" . "sa")
    317     ("Sardinian" . "sc")
    318     ("Scots" . "gd")
    319     ("Serbian" . "sr")
    320     ("Sesotho" . "st")
    321     ("Setswana" . "tn")
    322     ("Shona" . "sn")
    323     ("Sindhi" . "sd")
    324     ("Sinhalese" . "si")
    325     ("Siswati" . "ss")
    326     ("Slovak" . "sk")
    327     ("Slovenian" . "sl")
    328     ("Somali" . "so")
    329     ("Sorbian" . "wen")
    330     ("South Ndebele" . "nr")
    331     ("Spanish" . "es")
    332     ("Sundanese" . "su")
    333     ("Swahili" . "sw")
    334     ("Swedish" . "sv")
    335     ("Tagalog" . "tl")
    336     ("Tahitian" . "ty")
    337     ("Tajik" . "tg")
    338     ("Tamil" . "ta")
    339     ("Tatar" . "tt")
    340     ("Telugu" . "te")
    341     ("Tetum" . "tet")
    342     ("Thai" . "th")
    343     ("Tibetan" . "bo")
    344     ("Tigrinya" . "ti")
    345     ("Tonga" . "to")
    346     ("Tsonga" . "ts")
    347     ("Turkish" . "tr")
    348     ("Turkmen" . "tk")
    349     ("Twi" . "tw")
    350     ("Tyap" . "kcg")
    351     ("Uighur" . "ug")
    352     ("Ukrainian" . "uk")
    353     ("Urdu" . "ur")
    354     ("Uzbek" . "uz")
    355     ("Vietnamese" . "vi")
    356     ("Volapuk" . "vo")
    357     ("Walloon" . "wa")
    358     ("Welsh" . "cy")
    359     ("Wolof" . "wo")
    360     ("Xhosa" . "xh")
    361     ("Yiddish" . "yi")
    362     ("Yoruba" . "yo")
    363     ("Zapotec" . "zap")
    364     ("Zhuang" . "za")
    365     ("Zulu" . "zu")
    366     )
    367   "*Association list giving team codes from team names.
    368 This is used for generating a submission file name for the 'M' command.
    369 If a string instead of an alist, it is a team code to use unconditionnally."
    370   :type 'sexp
    371   :group 'po)
    372 
    373 (defcustom po-gzip-uuencode-command "gzip -9 | uuencode -m"
    374   "*The filter to use for preparing a mail invoice of the PO file.
    375 Normally \"gzip -9 | uuencode -m\", remove the -9 for lesser compression,
    376 or remove the -m if you are not using the GNU version of 'uuencode'."
    377   :type 'string
    378   :group 'po)
    379 
    380 (defvar po-subedit-mode-syntax-table
    381   (copy-syntax-table text-mode-syntax-table)
    382   "Syntax table used while in PO mode.")
    383 
    384 ;;; Emacs portability matters - part II.
    386 
    387 ;;; Many portability matters are addressed in this page.  The few remaining
    388 ;;; cases, elsewhere, all involve  'eval-and-compile', 'boundp' or 'fboundp'.
    389 
    390 ;; Protect string comparisons from text properties if possible.
    391 (eval-and-compile
    392   (fset 'po-buffer-substring
    393 	(symbol-function (if (fboundp 'buffer-substring-no-properties)
    394 			     'buffer-substring-no-properties
    395 			   'buffer-substring)))
    396 
    397   (if (fboundp 'match-string-no-properties)
    398       (fset 'po-match-string (symbol-function 'match-string-no-properties))
    399     (defun po-match-string (number)
    400       "Return string of text matched by last search."
    401       (po-buffer-substring (match-beginning number) (match-end number)))))
    402 
    403 ;; Handle missing 'with-temp-buffer' function.
    404 (eval-and-compile
    405   (if (fboundp 'with-temp-buffer)
    406       (fset 'po-with-temp-buffer (symbol-function 'with-temp-buffer))
    407 
    408     (defmacro po-with-temp-buffer (&rest forms)
    409       "Create a temporary buffer, and evaluate FORMS there like 'progn'."
    410       (let ((curr-buffer (make-symbol "curr-buffer"))
    411 	    (temp-buffer (make-symbol "temp-buffer")))
    412 	`(let ((,curr-buffer (current-buffer))
    413 	       (,temp-buffer (get-buffer-create
    414 			      (generate-new-buffer-name " *po-temp*"))))
    415 	   (unwind-protect
    416 	       (progn
    417 		 (set-buffer ,temp-buffer)
    418 		 ,@forms)
    419 	     (set-buffer ,curr-buffer)
    420 	     (and (buffer-name ,temp-buffer)
    421 		  (kill-buffer ,temp-buffer))))))))
    422 
    423 ;; Handle missing 'kill-new' function.
    424 (eval-and-compile
    425   (if (fboundp 'kill-new)
    426       (fset 'po-kill-new (symbol-function 'kill-new))
    427 
    428     (defun po-kill-new (string)
    429       "Push STRING onto the kill ring, for Emacs 18 where kill-new is missing."
    430       (po-with-temp-buffer
    431 	(insert string)
    432 	(kill-region (point-min) (point-max))))))
    433 
    434 ;; Handle missing 'read-event' function.
    435 (eval-and-compile
    436   (fset 'po-read-event
    437 	(cond ((fboundp 'read-event)
    438 	       ;; GNU Emacs.
    439 	       'read-event)
    440 	      ((fboundp 'next-command-event)
    441 	       ;; XEmacs.
    442 	       'next-command-event)
    443 	      (t
    444 	       ;; Older Emacses.
    445 	       'read-char))))
    446 
    447 ;; Handle missing 'force-mode-line-update' function.
    448 (eval-and-compile
    449   (if (fboundp 'force-mode-line-update)
    450       (fset 'po-force-mode-line-update
    451 	    (symbol-function 'force-mode-line-update))
    452 
    453     (defun po-force-mode-line-update ()
    454       "Force the mode-line of the current buffer to be redisplayed."
    455       (set-buffer-modified-p (buffer-modified-p)))))
    456 
    457 ;; Handle portable highlighting.  Code has been adapted (OK... stolen! :-)
    458 ;; from 'ispell.el'.
    459 (eval-and-compile
    460   (cond
    461    (po-EMACS20
    462 
    463     (defun po-create-overlay ()
    464       "Create and return a deleted overlay structure.
    465 The variable 'po-highlight-face' selects the face to use for highlighting."
    466       (let ((overlay (make-overlay (point) (point))))
    467 	(overlay-put overlay 'face po-highlight-face)
    468 	;; The fun thing is that a deleted overlay retains its face, and is
    469 	;; movable.
    470 	(delete-overlay overlay)
    471 	overlay))
    472 
    473     (defun po-highlight (overlay start end &optional buffer)
    474       "Use OVERLAY to highlight the string from START to END.
    475 If limits are not relative to the current buffer, use optional BUFFER."
    476       (move-overlay overlay start end (or buffer (current-buffer))))
    477 
    478     (defun po-rehighlight (overlay)
    479       "Ensure OVERLAY is highlighted."
    480       ;; There is nothing to do, as GNU Emacs allows multiple highlights.
    481       nil)
    482 
    483     (defun po-dehighlight (overlay)
    484       "Display normally the last string which OVERLAY highlighted.
    485 The current buffer should be in PO mode, when this function is called."
    486       (delete-overlay overlay)))
    487 
    488    (po-XEMACS
    489 
    490     (defun po-create-overlay ()
    491       "Create and return a deleted overlay structure."
    492       ;; The same as for GNU Emacs above, except the created extent is
    493       ;; already detached, so there's no need to "delete" it
    494       ;; explicitly.
    495       (let ((extent (make-extent nil nil)))
    496 	(set-extent-face extent po-highlight-face)
    497 	extent))
    498 
    499     (defun po-highlight (extent start end &optional buffer)
    500       "Use EXTENT to highlight the string from START to END.
    501 If limits are not relative to the current buffer, use optional BUFFER."
    502       (set-extent-endpoints extent start end (or buffer (current-buffer))))
    503 
    504     (defun po-rehighlight (extent)
    505       "Ensure EXTENT is highlighted."
    506       ;; Nothing to do here.
    507       nil)
    508 
    509     (defun po-dehighlight (extent)
    510       "Display normally the last string which EXTENT highlighted."
    511       (detach-extent extent)))
    512 
    513    (t
    514 
    515     (defun po-create-overlay ()
    516       "Create and return a deleted overlay structure."
    517       (cons (make-marker) (make-marker)))
    518 
    519     (defun po-highlight (overlay start end &optional buffer)
    520       "Use OVERLAY to highlight the string from START to END.
    521 If limits are not relative to the current buffer, use optional BUFFER.
    522 No doubt that highlighting, when Emacs does not allow it, is a kludge."
    523       (save-excursion
    524 	(and buffer (set-buffer buffer))
    525 	(let ((modified (buffer-modified-p))
    526 	      (buffer-read-only nil)
    527 	      (inhibit-quit t)
    528 	      (buffer-undo-list t)
    529 	      (text (buffer-substring start end)))
    530 	  (goto-char start)
    531 	  (delete-region start end)
    532 	  (insert-char ?  (- end start))
    533 	  (sit-for 0)
    534 	  (setq inverse-video (not inverse-video))
    535 	  (delete-region start end)
    536 	  (insert text)
    537 	  (sit-for 0)
    538 	  (setq inverse-video (not inverse-video))
    539 	  (set-buffer-modified-p modified)))
    540       (set-marker (car overlay) start (or buffer (current-buffer)))
    541       (set-marker (cdr overlay) end (or buffer (current-buffer))))
    542 
    543     (defun po-rehighlight (overlay)
    544       "Ensure OVERLAY is highlighted."
    545       (let ((buffer (marker-buffer (car overlay)))
    546 	    (start (marker-position (car overlay)))
    547 	    (end (marker-position (cdr overlay))))
    548 	(and buffer
    549 	     (buffer-name buffer)
    550 	     (po-highlight overlay start end buffer))))
    551 
    552     (defun po-dehighlight (overlay)
    553       "Display normally the last string which OVERLAY highlighted."
    554       (let ((buffer (marker-buffer (car overlay)))
    555 	    (start (marker-position (car overlay)))
    556 	    (end (marker-position (cdr overlay))))
    557 	(if buffer
    558 	    (save-excursion
    559 	      (set-buffer buffer)
    560 	      (let ((modified (buffer-modified-p))
    561 		    (buffer-read-only nil)
    562 		    (inhibit-quit t)
    563 		    (buffer-undo-list t))
    564 		(let ((text (buffer-substring start end)))
    565 		  (goto-char start)
    566 		  (delete-region start end)
    567 		  (insert-char ?  (- end start))
    568 		  (sit-for 0)
    569 		  (delete-region start end)
    570 		  (insert text)
    571 		  (sit-for 0)
    572 		  (set-buffer-modified-p modified)))))
    573 	(setcar overlay (make-marker))
    574 	(setcdr overlay (make-marker))))
    575 
    576     )))
    577 
    578 ;;; Buffer local variables.
    580 
    581 ;; The following block of declarations has the main purpose of avoiding
    582 ;; byte compiler warnings.  It also introduces some documentation for
    583 ;; each of these variables, all meant to be local to PO mode buffers.
    584 
    585 ;; Flag telling that MODE-LINE-STRING should be displayed.  See 'Window'
    586 ;; page below.  Exceptionally, this variable is local to *all* buffers.
    587 (defvar po-mode-flag)
    588 
    589 ;; PO buffers are kept read-only to prevent random modifications.  READ-ONLY
    590 ;; holds the value of the read-only flag before PO mode was entered.
    591 (defvar po-read-only)
    592 
    593 ;; The current entry extends from START-OF-ENTRY to END-OF-ENTRY, it
    594 ;; includes preceding whitespace and excludes following whitespace.  The
    595 ;; start of keyword lines are START-OF-MSGID and START-OF-MSGSTR.
    596 ;; ENTRY-TYPE classifies the entry.
    597 (defvar po-start-of-entry)
    598 (defvar po-start-of-msgid)
    599 (defvar po-start-of-msgstr)
    600 (defvar po-end-of-entry)
    601 (defvar po-entry-type)
    602 
    603 ;; A few counters are usefully shown in the Emacs mode line.
    604 (defvar po-translated-counter)
    605 (defvar po-fuzzy-counter)
    606 (defvar po-untranslated-counter)
    607 (defvar po-obsolete-counter)
    608 (defvar po-mode-line-string)
    609 
    610 ;; PO mode keeps track of fields being edited, for one given field should
    611 ;; have one editing buffer at most, and for exiting a PO buffer properly
    612 ;; should offer to close all pending edits.  Variable EDITED-FIELDS holds an
    613 ;; an list of "slots" of the form: (ENTRY-MARKER EDIT-BUFFER OVERLAY-INFO).
    614 ;; To allow simultaneous edition of the comment and the msgstr of an entry,
    615 ;; ENTRY-MARKER points to the msgid line if a comment is being edited, or to
    616 ;; the msgstr line if the msgstr is being edited.  EDIT-BUFFER is the
    617 ;; temporary Emacs buffer used to edit the string.  OVERLAY-INFO, when not
    618 ;; nil, holds an overlay (or if overlays are not supported, a cons of two
    619 ;; markers) for this msgid string which became highlighted for the edit.
    620 (defvar po-edited-fields)
    621 
    622 ;; We maintain a set of movable pointers for returning to entries.
    623 (defvar po-marker-stack)
    624 
    625 ;; SEARCH path contains a list of directories where files may be found,
    626 ;; in a format suitable for read completion.  Each directory includes
    627 ;; its trailing slash.  PO mode starts with "./" and "../".
    628 (defvar po-search-path)
    629 
    630 ;; The following variables are meaningful only when REFERENCE-CHECK
    631 ;; is identical to START-OF-ENTRY, else they should be recomputed.
    632 ;; REFERENCE-ALIST contains all known references for the current
    633 ;; entry, each list element is (PROMPT FILE LINE), where PROMPT may
    634 ;; be used for completing read, FILE is a string and LINE is a number.
    635 ;; REFERENCE-CURSOR is a cycling cursor into REFERENCE-ALIST.
    636 (defvar po-reference-alist)
    637 (defvar po-reference-cursor)
    638 (defvar po-reference-check)
    639 
    640 ;; The following variables are for marking translatable strings in program
    641 ;; sources.  KEYWORDS is the list of keywords for marking translatable
    642 ;; strings, kept in a format suitable for reading with completion.
    643 ;; STRING-CONTENTS holds the value of the most recent string found in sources,
    644 ;; and when it is not nil, then STRING-BUFFER, STRING-START and STRING-END
    645 ;; describe where it is.  MARKING-OVERLAY, if not 'nil', holds the overlay
    646 ;; which highlight the last found string; for older Emacses, it holds the cons
    647 ;; of two markers around the highlighted region.
    648 (defvar po-keywords)
    649 (defvar po-string-contents)
    650 (defvar po-string-buffer)
    651 (defvar po-string-start)
    652 (defvar po-string-end)
    653 (defvar po-marking-overlay)
    654 
    655 ;;; PO mode variables and constants (usually not to customize).
    657 
    658 ;; The textdomain should really be "gettext", only trying it for now.
    659 ;; All this requires more thinking, we cannot just do this like that.
    660 (set-translation-domain "po-mode")
    661 
    662 (defun po-mode-version ()
    663   "Show Emacs PO mode version."
    664   (interactive)
    665   (message (_"Emacs PO mode, version %s") po-mode-version-string))
    666 
    667 (defconst po-help-display-string
    668   (_"\
    669 PO Mode Summary           Next Previous            Miscellaneous
    670 *: Later, /: Docum        n    p    Any type       .     Redisplay
    671                           t    T    Translated     /v    Version info
    672 Moving around             f    F    Fuzzy          ?, h  This help
    673 <    First if any         o    O    Obsolete       =     Current index
    674 >    Last if any          u    U    Untranslated   0     Other window
    675 /SPC Auto select                                   V     Validate
    676                         Msgstr Comments            M     Mail officially
    677 Modifying entries         RET  #    Call editor    _     Undo
    678 TAB   Remove fuzzy mark   k    K    Kill to        E     Edit out full
    679 DEL   Fuzzy or fade out   w    W    Copy to        Q     Forceful quit
    680 LFD   Init with msgid     y    Y    Yank from      q     Confirm and quit
    681 
    682 gettext Keyword Marking                            Position Stack
    683 ,    Find next string     Compendiums              m  Mark and push current
    684 M-,  Mark translatable    *c    To compendium      r  Pop and return
    685 M-.  Change mark, mark    *M-C  Select, save       x  Exchange current/top
    686 
    687 Program Sources           Auxiliary Files          Lexicography
    688 s    Cycle reference      a    Cycle file          *l    Lookup translation
    689 M-s  Select reference     C-c C-a  Select file     *M-l  Add/edit translation
    690 S    Consider path        A    Consider PO file    *L    Consider lexicon
    691 M-S  Ignore path          M-A  Ignore PO file      *M-L  Ignore lexicon
    692 ")
    693   "Help page for PO mode.")
    694 
    695 (defconst po-mode-menu-layout
    696   `("PO"
    697     ("Moving around"
    698      ["Auto select" po-auto-select-entry
    699       ,@(if (featurep 'xemacs) '(t)
    700 	  '(:help "Jump to next interesting entry"))]
    701      "---"
    702      "Forward"
    703      ["Any next" po-next-entry
    704       ,@(if (featurep 'xemacs) '(t)
    705 	  '(:help "Jump to next entry"))]
    706      ["Next translated" po-next-translated-entry
    707       ,@(if (featurep 'xemacs) '(t)
    708 	  '(:help "Jump to next translated entry"))]
    709      ["Next fuzzy" po-next-fuzzy-entry
    710       ,@(if (featurep 'xemacs) '(t)
    711 	  '(:help "Jump to next fuzzy entry"))]
    712      ["Next obsolete" po-next-obsolete-entry
    713       ,@(if (featurep 'xemacs) '(t)
    714 	  '(:help "Jump to next obsolete entry"))]
    715      ["Next untranslated" po-next-untranslated-entry
    716       ,@(if (featurep 'xemacs) '(t)
    717 	  '(:help "Jump to next untranslated entry"))]
    718      ["Last file entry" po-last-entry
    719       ,@(if (featurep 'xemacs) '(t)
    720 	  '(:help "Jump to last entry"))]
    721      "---"
    722      "Backward"
    723      ["Any previous" po-previous-entry
    724       ,@(if (featurep 'xemacs) '(t)
    725 	  '(:help "Jump to previous entry"))]
    726      ["Previous translated" po-previous-translated-entry
    727       ,@(if (featurep 'xemacs) '(t)
    728 	  '(:help "Jump to previous translated entry"))]
    729      ["Previous fuzzy" po-previous-fuzzy-entry
    730       ,@(if (featurep 'xemacs) '(t)
    731 	  '(:help "Jump to previous fuzzy entry"))]
    732      ["Previous obsolete" po-previous-obsolete-entry
    733       ,@(if (featurep 'xemacs) '(t)
    734 	  '(:help "Jump to previous obsolete entry"))]
    735      ["Previous untranslated" po-previous-untranslated-entry
    736       ,@(if (featurep 'xemacs) '(t)
    737 	  '(:help "Jump to previous untranslated entry"))]
    738      ["First file entry" po-first-entry
    739       ,@(if (featurep 'xemacs) '(t)
    740 	  '(:help "Jump to first entry"))]
    741      "---"
    742      "Position stack"
    743      ["Mark and push current" po-push-location
    744       ,@(if (featurep 'xemacs) '(t)
    745 	  '(:help "Remember current location"))]
    746      ["Pop and return" po-pop-location
    747       ,@(if (featurep 'xemacs) '(t)
    748 	  '(:help "Jump to last remembered location and forget about it"))]
    749      ["Exchange current/top" po-exchange-location
    750       ,@(if (featurep 'xemacs) '(t)
    751 	  '(:help "Jump to last remembered location and remember current location"))]
    752      "---"
    753      ["Redisplay" po-current-entry
    754       ,@(if (featurep 'xemacs) '(t)
    755 	  '(:help "Make current entry properly visible"))]
    756      ["Current index" po-statistics
    757       ,@(if (featurep 'xemacs) '(t)
    758 	  '(:help "Statistical info on current translation file"))])
    759     ("Modifying entries"
    760      ["Undo" po-undo
    761       ,@(if (featurep 'xemacs) '(t)
    762 	  '(:help "Revoke last changed entry"))]
    763      "---"
    764      "Msgstr"
    765      ["Edit msgstr" po-edit-msgstr
    766       ,@(if (featurep 'xemacs) '(t)
    767 	  '(:help "Edit current translation"))]
    768      ["Ediff and merge msgstr" po-edit-msgstr-and-ediff
    769       ,@(if (featurep 'xemacs) '(t)
    770 	  '(:help "Call `ediff' on current translation for merging"))]
    771      ["Cut msgstr" po-kill-msgstr
    772       ,@(if (featurep 'xemacs) '(t)
    773 	  '(:help "Cut (kill) current translation"))]
    774      ["Copy msgstr" po-kill-ring-save-msgstr
    775       ,@(if (featurep 'xemacs) '(t)
    776 	  '(:help "Copy current translation"))]
    777      ["Paste msgstr" po-yank-msgstr
    778       ,@(if (featurep 'xemacs) '(t)
    779 	  '(:help "Paste (yank) text most recently cut/copied translation"))]
    780      "---"
    781      "Comments"
    782      ["Edit comment" po-edit-comment
    783       ,@(if (featurep 'xemacs) '(t)
    784 	  '(:help "Edit current comment"))]
    785      ["Ediff and merge comment" po-edit-comment-and-ediff
    786       ,@(if (featurep 'xemacs) '(t)
    787 	  '(:help "Call `ediff' on current comment for merging"))]
    788      ["Cut comment" po-kill-comment
    789       ,@(if (featurep 'xemacs) '(t)
    790 	  '(:help "Cut (kill) current comment"))]
    791      ["Copy comment" po-kill-ring-save-comment
    792       ,@(if (featurep 'xemacs) '(t)
    793 	  '(:help "Copy current translation"))]
    794      ["Paste comment" po-yank-comment
    795       ,@(if (featurep 'xemacs) '(t)
    796 	  '(:help "Paste (yank) text most recently cut/copied"))]
    797      "---"
    798      ["Remove fuzzy mark" po-unfuzzy
    799       ,@(if (featurep 'xemacs) '(t)
    800 	  '(:help "Remove \"#, fuzzy\""))]
    801      ["Fuzzy or fade out" po-fade-out-entry
    802       ,@(if (featurep 'xemacs) '(t)
    803 	  '(:help "Set current entry fuzzy, or if already fuzzy delete it"))]
    804      ["Init with msgid" po-msgid-to-msgstr
    805       ,@(if (featurep 'xemacs) '(t)
    806 	  '(:help "\
    807 Initialize or replace current translation with the original message"))])
    808     ("Other files"
    809      ["Other window" po-other-window
    810       ,@(if (featurep 'xemacs) '(t)
    811 	  '(:help "Select other window; if necessay split current frame"))]
    812      "---"
    813      "Program sources"
    814      ["Cycle reference" po-cycle-source-reference t]
    815      ["Select reference" po-select-source-reference t]
    816      ["Consider path" po-consider-source-path t]
    817      ["Ignore path" po-ignore-source-path t]
    818      "---"
    819      "Compendiums"
    820      ["To compendium" po-save-entry nil]
    821      ["Select, save" po-select-and-save-entry nil]
    822      "---"
    823      "Auxiliary files"
    824      ["Cycle file" po-cycle-auxiliary t]
    825      ["Select file" po-select-auxiliary t]
    826      ["Consider file" po-consider-as-auxiliary t]
    827      ["Ignore file" po-ignore-as-auxiliary t]
    828      "---"
    829      "Lexicography"
    830      ["Lookup translation" po-lookup-lexicons nil]
    831      ["Add/edit translation" po-edit-lexicon-entry nil]
    832      ["Consider lexicon" po-consider-lexicon-file nil]
    833      ["Ignore lexicon" po-ignore-lexicon-file nil])
    834     "---"
    835     "Source marking"
    836     ["Find first string" (po-tags-search '(nil)) t]
    837     ["Prefer keyword" (po-select-mark-and-mark '(nil)) t]
    838     ["Find next string" po-tags-search t]
    839     ["Mark preferred" po-mark-translatable t]
    840     ["Mark with keyword" po-select-mark-and-mark t]
    841     "---"
    842     ["Version info" po-mode-version
    843      ,@(if (featurep 'xemacs) '(t)
    844 	  '(:help "Display version number of PO mode"))]
    845     ["Help page" po-help
    846      ,@(if (featurep 'xemacs) '(t)
    847 	  '(:help "Show the PO mode help screen"))]
    848     ["Validate" po-validate
    849      ,@(if (featurep 'xemacs) '(t)
    850 	  '(:help "Check validity of current translation file using `msgfmt'"))]
    851     ["Mail officially" po-send-mail
    852      ,@(if (featurep 'xemacs) '(t)
    853 	  '(:help "Send current translation file to the Translation Robot by mail"))]
    854     ["Edit out full" po-edit-out-full
    855      ,@(if (featurep 'xemacs) '(t)
    856 	  '(:help "Leave PO mode to edit translation file using fundamental mode"))]
    857     "---"
    858     ["Forceful quit" po-quit
    859      ,@(if (featurep 'xemacs) '(t)
    860 	  '(:help "Close (kill) current translation file without saving"))]
    861     ["Soft quit" po-confirm-and-quit
    862      ,@(if (featurep 'xemacs) '(t)
    863 	  '(:help "Save current translation file, than close (kill) it"))])
    864   "Menu layout for PO mode.")
    865 
    866 (defconst po-subedit-mode-menu-layout
    867   `("PO-Edit"
    868     ["Ediff and merge translation variants" po-subedit-ediff
    869       ,@(if (featurep 'xemacs) '(t)
    870 	  '(:help "Call `ediff' for merging variants"))]
    871     ["Cycle through auxiliary files" po-subedit-cycle-auxiliary t]
    872     "---"
    873     ["Abort edit" po-subedit-abort
    874      ,@(if (featurep 'xemacs) '(t)
    875 	  '(:help "Don't change the translation"))]
    876     ["Exit edit" po-subedit-exit
    877      ,@(if (featurep 'xemacs) '(t)
    878 	 '(:help "Use this text as the translation and close current edit buffer"))])
    879   "Menu layout for PO subedit mode.")
    880 
    881 (defconst po-subedit-message
    882   (_"Type 'C-c C-c' once done, or 'C-c C-k' to abort edit")
    883   "Message to post in the minibuffer when an edit buffer is displayed.")
    884 
    885 (defvar po-auxiliary-list nil
    886   "List of auxiliary PO files, in completing read format.")
    887 
    888 (defvar po-auxiliary-cursor nil
    889   "Cursor into the 'po-auxiliary-list'.")
    890 
    891 (defvar po-compose-mail-function
    892   (let ((functions '(compose-mail-other-window
    893 		     message-mail-other-window
    894 		     compose-mail
    895 		     message-mail))
    896 	result)
    897     (while (and (not result) functions)
    898       (if (fboundp (car functions))
    899 	  (setq result (car functions))
    900 	(setq functions (cdr functions))))
    901     (cond (result)
    902 	  ((fboundp 'mail-other-window)
    903 	   (function (lambda (to subject)
    904 		       (mail-other-window nil to subject))))
    905 	  ((fboundp 'mail)
    906 	   (function (lambda (to subject)
    907 		       (mail nil to subject))))
    908 	  (t (function (lambda (to subject)
    909 			 (error (_"I do not know how to mail to '%s'") to))))))
    910   "Function to start composing an electronic message.")
    911 
    912 (defvar po-any-msgid-regexp
    913   "^\\(#~[ \t]*\\)?msgid.*\n\\(\\(#~[ \t]*\\)?\".*\n\\)*"
    914   "Regexp matching a whole msgid field, whether obsolete or not.")
    915 
    916 (defvar po-any-msgstr-regexp
    917   ;; "^\\(#~[ \t]*\\)?msgstr.*\n\\(\\(#~[ \t]*\\)?\".*\n\\)*"
    918   "^\\(#~[ \t]*\\)?msgstr\\(\\[[0-9]\\]\\)?.*\n\\(\\(#~[ \t]*\\)?\".*\n\\)*"
    919   "Regexp matching a whole msgstr or msgstr[] field, whether obsolete or not.")
    920 
    921 (defvar po-msgstr-idx-keyword-regexp
    922   "^\\(#~[ \t]*\\)?msgstr\\[[0-9]\\]"
    923   "Regexp matching an indexed msgstr keyword, whether obsolete or not.")
    924 
    925 (defvar po-msgfmt-program "msgfmt"
    926   "Path to msgfmt program from GNU gettext package.")
    927 
    928 ;; Font lock based highlighting code.
    929 (defconst po-font-lock-keywords
    930   '(
    931     ;; ("^\\(msgid \\|msgstr \\)?\"\\|\"$" . font-lock-keyword-face)
    932     ;; (regexp-opt
    933     ;;  '("msgid " "msgid_plural " "msgstr " "msgstr[0] " "msgstr[1] "))
    934     ("\
    935 ^\\(\\(msg\\(id\\(_plural\\)?\\|str\\(\\[[0-9]\\]\\)?\\)?\\) \\)?\"\\|\"$"
    936      . font-lock-keyword-face)
    937     ("\\\\.\\|%\\*?[-.0-9ul]*[a-zA-Z]" . font-lock-variable-name-face)
    938     ("^# .*\\|^#[:,]?" . font-lock-comment-face)
    939     ("^#:\\(.*\\)" 1 font-lock-reference-face)
    940     ;; The following line does not work, and I wonder why.
    941     ;;("^#,\\(.*\\)" 1 font-function-name-reference-face)
    942     )
    943   "Additional expressions to highlight in PO mode.")
    944 
    945 ;; Old activator for 'font lock'.  Is it still useful?  I don't think so.
    946 ;;(if (boundp 'font-lock-keywords)
    947 ;;    (put 'po-mode 'font-lock-keywords 'po-font-lock-keywords))
    948 
    949 ;; 'hilit19' based highlighting code has been disabled, as most probably
    950 ;; nobody really needs it (it also generates ugly byte-compiler warnings).
    951 ;;
    952 ;;(if (fboundp 'hilit-set-mode-patterns)
    953 ;;    (hilit-set-mode-patterns 'po-mode
    954 ;;			     '(("^# .*\\|^#$" nil comment)
    955 ;;			       ("^#[.,:].*" nil include)
    956 ;;			       ("^\\(msgid\\|msgstr\\) *\"" nil keyword)
    957 ;;			       ("^\"\\|\"$" nil keyword))))
    958 
    959 ;;; Mode activation.
    961 
    962 ;; Emacs 21.2 comes with po-find-file-coding-system. We give preference
    963 ;; to the version shipped with Emacs.
    964 (if (not (fboundp 'po-find-file-coding-system))
    965   (require 'po-compat))
    966 
    967 (defvar po-mode-abbrev-table nil
    968   "Abbrev table used while in PO mode.")
    969 (define-abbrev-table 'po-mode-abbrev-table ())
    970 
    971 (defvar po-mode-map
    972   ;; Use (make-keymap) because (make-sparse-keymap) does not work on Demacs.
    973   (let ((po-mode-map (make-keymap)))
    974     (suppress-keymap po-mode-map)
    975     (define-key po-mode-map "\C-i" 'po-unfuzzy)
    976     (define-key po-mode-map "\C-j" 'po-msgid-to-msgstr)
    977     (define-key po-mode-map "\C-m" 'po-edit-msgstr)
    978     (define-key po-mode-map " " 'po-auto-select-entry)
    979     (define-key po-mode-map "?" 'po-help)
    980     (define-key po-mode-map "#" 'po-edit-comment)
    981     (define-key po-mode-map "," 'po-tags-search)
    982     (define-key po-mode-map "." 'po-current-entry)
    983     (define-key po-mode-map "<" 'po-first-entry)
    984     (define-key po-mode-map "=" 'po-statistics)
    985     (define-key po-mode-map ">" 'po-last-entry)
    986     (define-key po-mode-map "a" 'po-cycle-auxiliary)
    987 ;;;;  (define-key po-mode-map "c" 'po-save-entry)
    988     (define-key po-mode-map "f" 'po-next-fuzzy-entry)
    989     (define-key po-mode-map "h" 'po-help)
    990     (define-key po-mode-map "k" 'po-kill-msgstr)
    991 ;;;;  (define-key po-mode-map "l" 'po-lookup-lexicons)
    992     (define-key po-mode-map "m" 'po-push-location)
    993     (define-key po-mode-map "n" 'po-next-entry)
    994     (define-key po-mode-map "o" 'po-next-obsolete-entry)
    995     (define-key po-mode-map "p" 'po-previous-entry)
    996     (define-key po-mode-map "q" 'po-confirm-and-quit)
    997     (define-key po-mode-map "r" 'po-pop-location)
    998     (define-key po-mode-map "s" 'po-cycle-source-reference)
    999     (define-key po-mode-map "t" 'po-next-translated-entry)
   1000     (define-key po-mode-map "u" 'po-next-untranslated-entry)
   1001     (define-key po-mode-map "v" 'po-mode-version)
   1002     (define-key po-mode-map "w" 'po-kill-ring-save-msgstr)
   1003     (define-key po-mode-map "x" 'po-exchange-location)
   1004     (define-key po-mode-map "y" 'po-yank-msgstr)
   1005     (define-key po-mode-map "A" 'po-consider-as-auxiliary)
   1006     (define-key po-mode-map "E" 'po-edit-out-full)
   1007     (define-key po-mode-map "F" 'po-previous-fuzzy-entry)
   1008     (define-key po-mode-map "K" 'po-kill-comment)
   1009 ;;;;  (define-key po-mode-map "L" 'po-consider-lexicon-file)
   1010     (define-key po-mode-map "M" 'po-send-mail)
   1011     (define-key po-mode-map "O" 'po-previous-obsolete-entry)
   1012     (define-key po-mode-map "T" 'po-previous-translated-entry)
   1013     (define-key po-mode-map "U" 'po-previous-untranslated-entry)
   1014     (define-key po-mode-map "Q" 'po-quit)
   1015     (define-key po-mode-map "S" 'po-consider-source-path)
   1016     (define-key po-mode-map "V" 'po-validate)
   1017     (define-key po-mode-map "W" 'po-kill-ring-save-comment)
   1018     (define-key po-mode-map "Y" 'po-yank-comment)
   1019     (define-key po-mode-map "_" 'po-undo)
   1020     (define-key po-mode-map "0" 'po-other-window)
   1021     (define-key po-mode-map "\177" 'po-fade-out-entry)
   1022     (define-key po-mode-map "\C-c\C-a" 'po-select-auxiliary)
   1023     (define-key po-mode-map "\C-c\C-e" 'po-edit-msgstr-and-ediff)
   1024     (define-key po-mode-map [?\C-c?\C-#] 'po-edit-comment-and-ediff)
   1025     (define-key po-mode-map "\C-c\C-C" 'po-edit-comment-and-ediff)
   1026     (define-key po-mode-map "\M-," 'po-mark-translatable)
   1027     (define-key po-mode-map "\M-." 'po-select-mark-and-mark)
   1028 ;;;;  (define-key po-mode-map "\M-c" 'po-select-and-save-entry)
   1029 ;;;;  (define-key po-mode-map "\M-l" 'po-edit-lexicon-entry)
   1030     (define-key po-mode-map "\M-s" 'po-select-source-reference)
   1031     (define-key po-mode-map "\M-A" 'po-ignore-as-auxiliary)
   1032 ;;;;  (define-key po-mode-map "\M-L" 'po-ignore-lexicon-file)
   1033     (define-key po-mode-map "\M-S" 'po-ignore-source-path)
   1034     po-mode-map)
   1035   "Keymap for PO mode.")
   1036 
   1037 (defun po-mode ()
   1038   "Major mode for translators when they edit PO files.
   1039 
   1040 Special commands:
   1041 \\{po-mode-map}
   1042 Turning on PO mode calls the value of the variable 'po-mode-hook',
   1043 if that value is non-nil.  Behaviour may be adjusted through some variables,
   1044 all reachable through 'M-x customize', in group 'Emacs.Editing.I18n.Po'."
   1045   (interactive)
   1046   (kill-all-local-variables)
   1047   (setq major-mode 'po-mode
   1048 	mode-name "PO")
   1049   (use-local-map po-mode-map)
   1050   (if (fboundp 'easy-menu-define)
   1051       (progn
   1052 	(easy-menu-define po-mode-menu po-mode-map "" po-mode-menu-layout)
   1053 	(and po-XEMACS (easy-menu-add po-mode-menu))))
   1054   (set (make-local-variable 'font-lock-defaults) '(po-font-lock-keywords t))
   1055 
   1056   (set (make-local-variable 'po-read-only) buffer-read-only)
   1057   (setq buffer-read-only t)
   1058 
   1059   (make-local-variable 'po-start-of-entry)
   1060   (make-local-variable 'po-start-of-msgid)
   1061   (make-local-variable 'po-start-of-msgstr)
   1062   (make-local-variable 'po-end-of-entry)
   1063   (make-local-variable 'po-entry-type)
   1064 
   1065   (make-local-variable 'po-translated-counter)
   1066   (make-local-variable 'po-fuzzy-counter)
   1067   (make-local-variable 'po-untranslated-counter)
   1068   (make-local-variable 'po-obsolete-counter)
   1069   (make-local-variable 'po-mode-line-string)
   1070 
   1071   (setq po-mode-flag t)
   1072 
   1073   (po-check-file-header)
   1074   (po-compute-counters nil)
   1075 
   1076   (set (make-local-variable 'po-edited-fields) nil)
   1077   (set (make-local-variable 'po-marker-stack) nil)
   1078   (set (make-local-variable 'po-search-path) '(("./") ("../")))
   1079 
   1080   (set (make-local-variable 'po-reference-alist) nil)
   1081   (set (make-local-variable 'po-reference-cursor) nil)
   1082   (set (make-local-variable 'po-reference-check) 0)
   1083 
   1084   (set (make-local-variable 'po-keywords)
   1085        '(("gettext") ("gettext_noop") ("_") ("N_")))
   1086   (set (make-local-variable 'po-string-contents) nil)
   1087   (set (make-local-variable 'po-string-buffer) nil)
   1088   (set (make-local-variable 'po-string-start) nil)
   1089   (set (make-local-variable 'po-string-end) nil)
   1090   (set (make-local-variable 'po-marking-overlay) (po-create-overlay))
   1091 
   1092   (add-hook 'write-contents-hooks 'po-replace-revision-date)
   1093 
   1094   (run-hooks 'po-mode-hook)
   1095   (message (_"You may type 'h' or '?' for a short PO mode reminder.")))
   1096 
   1097 (defvar po-subedit-mode-map
   1098   ;; Use (make-keymap) because (make-sparse-keymap) does not work on Demacs.
   1099   (let ((po-subedit-mode-map (make-keymap)))
   1100     (define-key po-subedit-mode-map "\C-c\C-a" 'po-subedit-cycle-auxiliary)
   1101     (define-key po-subedit-mode-map "\C-c\C-c" 'po-subedit-exit)
   1102     (define-key po-subedit-mode-map "\C-c\C-e" 'po-subedit-ediff)
   1103     (define-key po-subedit-mode-map "\C-c\C-k" 'po-subedit-abort)
   1104     po-subedit-mode-map)
   1105   "Keymap while editing a PO mode entry (or the full PO file).")
   1106 
   1107 ;;; Window management.
   1109 
   1110 (make-variable-buffer-local 'po-mode-flag)
   1111 
   1112 (defvar po-mode-line-entry '(po-mode-flag ("  " po-mode-line-string))
   1113   "Mode line format entry displaying MODE-LINE-STRING.")
   1114 
   1115 ;; Insert MODE-LINE-ENTRY in mode line, but on first load only.
   1116 (or (member po-mode-line-entry mode-line-format)
   1117     ;; mode-line-format usually contains global-mode-string, but some
   1118     ;; people customize this variable. As a last resort, append at the end.
   1119     (let ((prev-entry (or (member 'global-mode-string mode-line-format)
   1120                           (member "   " mode-line-format)
   1121                           (last mode-line-format))))
   1122       (setcdr prev-entry (cons po-mode-line-entry (cdr prev-entry)))))
   1123 
   1124 (defun po-update-mode-line-string ()
   1125   "Compute a new statistics string to display in mode line."
   1126   (setq po-mode-line-string
   1127 	(concat (format "%dt" po-translated-counter)
   1128 		(if (> po-fuzzy-counter 0)
   1129 		    (format "+%df" po-fuzzy-counter))
   1130 		(if (> po-untranslated-counter 0)
   1131 		    (format "+%du" po-untranslated-counter))
   1132 		(if (> po-obsolete-counter 0)
   1133 		    (format "+%do" po-obsolete-counter))))
   1134   (po-force-mode-line-update))
   1135 
   1136 (defun po-type-counter ()
   1137   "Return the symbol name of the counter appropriate for the current entry."
   1138   (cond ((eq po-entry-type 'obsolete) 'po-obsolete-counter)
   1139 	((eq po-entry-type 'fuzzy) 'po-fuzzy-counter)
   1140 	((eq po-entry-type 'translated) 'po-translated-counter)
   1141 	((eq po-entry-type 'untranslated) 'po-untranslated-counter)
   1142 	(t (error (_"Unknown entry type")))))
   1143 
   1144 (defun po-decrease-type-counter ()
   1145   "Decrease the counter corresponding to the nature of the current entry."
   1146   (let ((counter (po-type-counter)))
   1147     (set counter (1- (eval counter)))))
   1148 
   1149 (defun po-increase-type-counter ()
   1150   "Increase the counter corresponding to the nature of the current entry.
   1151 Then, update the mode line counters."
   1152   (let ((counter (po-type-counter)))
   1153     (set counter (1+ (eval counter))))
   1154   (po-update-mode-line-string))
   1155 
   1156 ;; Avoid byte compiler warnings.
   1157 (defvar po-fuzzy-regexp)
   1158 (defvar po-untranslated-regexp)
   1159 
   1160 (defun po-compute-counters (flag)
   1161   "Prepare counters for mode line display.  If FLAG, also echo entry position."
   1162   (and flag (po-find-span-of-entry))
   1163   (setq po-translated-counter 0
   1164 	po-fuzzy-counter 0
   1165 	po-untranslated-counter 0
   1166 	po-obsolete-counter 0)
   1167   (let ((position 0) (total 0) current here)
   1168     ;; FIXME 'here' looks obsolete / 2001-08-23 03:54:26 CEST -ke-
   1169     (save-excursion
   1170       (po-find-span-of-entry)
   1171       (setq current po-start-of-msgstr)
   1172       (goto-char (point-min))
   1173       ;; While counting, skip the header entry, for consistency with msgfmt.
   1174       (po-find-span-of-entry)
   1175       (if (string-equal (po-get-msgid nil) "")
   1176 	  (goto-char po-end-of-entry))
   1177       (if (re-search-forward "^msgid" (point-max) t)
   1178 	  (progn
   1179 	    ;; Start counting
   1180 	    (while (re-search-forward po-any-msgstr-regexp nil t)
   1181 	      (and (= (% total 20) 0)
   1182 		   (if flag
   1183 		       (message (_"Position %d/%d") position total)
   1184 		     (message (_"Position %d") total)))
   1185 	      (setq here (point))
   1186 	      (goto-char (match-beginning 0))
   1187 	      (setq total (1+ total))
   1188 	      (and flag (eq (point) current) (setq position total))
   1189 	      (cond ((eq (following-char) ?#)
   1190 		     (setq po-obsolete-counter (1+ po-obsolete-counter)))
   1191 		    ((looking-at po-untranslated-regexp)
   1192 		     (setq po-untranslated-counter (1+ po-untranslated-counter)))
   1193 		    (t (setq po-translated-counter (1+ po-translated-counter))))
   1194 	      (goto-char here))
   1195 
   1196 	    ;; Make another pass just for the fuzzy entries, kind of kludgey.
   1197 	    ;; FIXME: Counts will be wrong if untranslated entries are fuzzy, yet
   1198 	    ;; this should not normally happen.
   1199 	    (goto-char (point-min))
   1200 	    (while (re-search-forward po-fuzzy-regexp nil t)
   1201 	      (setq po-fuzzy-counter (1+ po-fuzzy-counter)))
   1202 	    (setq po-translated-counter (- po-translated-counter po-fuzzy-counter)))
   1203 	'()))
   1204 
   1205     ;; Push the results out.
   1206     (if flag
   1207 	(message (_"\
   1208 Position %d/%d; %d translated, %d fuzzy, %d untranslated, %d obsolete")
   1209 		 position total po-translated-counter po-fuzzy-counter
   1210 		 po-untranslated-counter po-obsolete-counter)
   1211       (message "")))
   1212   (po-update-mode-line-string))
   1213 
   1214 (defun po-redisplay ()
   1215   "Redisplay the current entry."
   1216   ;; FIXME: Should try to fit the whole entry on the window.  If this is not
   1217   ;; possible, should try to fit the comment and the msgid.  Otherwise,
   1218   ;; should try to fit the msgid.  Else, the first line of the msgid should
   1219   ;; be at the top of the window.
   1220   (goto-char po-start-of-msgid))
   1221 
   1222 (defun po-other-window ()
   1223   "Get the cursor into another window, out of PO mode."
   1224   (interactive)
   1225   (if (one-window-p t)
   1226       (progn
   1227 	(split-window)
   1228 	(switch-to-buffer (other-buffer)))
   1229     (other-window 1)))
   1230 
   1231 ;;; Processing the PO file header entry.
   1233 
   1234 (defun po-check-file-header ()
   1235   "Create a missing PO mode file header, or replace an oldish one."
   1236   (save-excursion
   1237     (let ((buffer-read-only po-read-only)
   1238 	  insert-flag end-of-header)
   1239       (goto-char (point-min))
   1240       (if (re-search-forward po-any-msgstr-regexp nil t)
   1241 	  (progn
   1242 	    ;; There is at least one entry.
   1243 	    (goto-char (match-beginning 0))
   1244 	    (previous-line 1)
   1245 	    (setq end-of-header (match-end 0))
   1246 	    (if (looking-at "msgid \"\"\n")
   1247 		;; There is indeed a PO file header.
   1248 		(if (re-search-forward "\n\"PO-Revision-Date: "
   1249 				       end-of-header t)
   1250 		    nil
   1251 		  ;; This is an oldish header.  Replace it all.
   1252 		  (goto-char end-of-header)
   1253 		  (while (> (point) (point-min))
   1254 		    (previous-line 1)
   1255 		    (insert "#~ ")
   1256 		    (beginning-of-line))
   1257 		  (beginning-of-line)
   1258 		  (setq insert-flag t))
   1259 	      ;; The first entry is not a PO file header, insert one.
   1260 	      (setq insert-flag t)))
   1261 	;; Not a single entry found.
   1262 	(setq insert-flag t))
   1263       (goto-char (point-min))
   1264       (if insert-flag
   1265 	  (progn
   1266 	    (insert po-default-file-header)
   1267 	    (if (not (eobp))
   1268 		(insert "\n")))))))
   1269 
   1270 (defun po-replace-revision-date ()
   1271   "Replace the revision date by current time in the PO file header."
   1272   (if (fboundp 'format-time-string)
   1273       (if (or (eq po-auto-replace-revision-date t)
   1274 	      (and (eq po-auto-replace-revision-date 'ask)
   1275 		   (y-or-n-p (_"May I set PO-Revision-Date? "))))
   1276 	  (save-excursion
   1277 	    (goto-char (point-min))
   1278 	    (if (re-search-forward "^\"PO-Revision-Date:.*" nil t)
   1279 		(let* ((buffer-read-only po-read-only)
   1280 		       (time (current-time))
   1281 		       (seconds (or (car (current-time-zone time)) 0))
   1282 		       (minutes (/ (abs seconds) 60))
   1283 		       (zone (format "%c%02d%02d"
   1284 				     (if (< seconds 0) ?- ?+)
   1285 				     (/ minutes 60)
   1286 				     (% minutes 60))))
   1287 		  (replace-match
   1288 		       (concat "\"PO-Revision-Date: "
   1289 			       (format-time-string "%Y-%m-%d %H:%M" time)
   1290 			       zone "\\n\"")
   1291 		       t t))))
   1292 	(message ""))
   1293     (message (_"PO-Revision-Date should be adjusted..."))))
   1294 
   1295 ;;; Handling span of entry, entry type and entry attributes.
   1297 
   1298 (defun po-find-span-of-entry ()
   1299   "Find the extent of the PO file entry where the cursor is.
   1300 Set variables PO-START-OF-ENTRY, PO-START-OF-MSGID, PO-START-OF-MSGSTR,
   1301 PO-END-OF-ENTRY and PO-ENTRY-TYPE to meaningful values.  Decreasing priority
   1302 of type interpretation is: obsolete, fuzzy, untranslated or translated."
   1303   (let ((here (point)))
   1304     (if (re-search-backward po-any-msgstr-regexp nil t)
   1305 	(progn
   1306 	  ;; After a backward match, (match-end 0) will not extend
   1307 	  ;; beyond point, in case point was *inside* the regexp.  We
   1308 	  ;; need a dependable (match-end 0), so we redo the match in
   1309 	  ;; the forward direction.
   1310 	  (re-search-forward po-any-msgstr-regexp)
   1311 	  (if (<= (match-end 0) here)
   1312 	      (progn
   1313 		;; We most probably found the msgstr of the previous
   1314 		;; entry.  The current entry then starts just after
   1315 		;; its end, save this information just in case.
   1316 		(setq po-start-of-entry (match-end 0))
   1317 		;; However, it is also possible that we are located in
   1318 		;; the crumb after the last entry in the file.  If
   1319 		;; yes, we know the middle and end of last PO entry.
   1320 		(setq po-start-of-msgstr (match-beginning 0)
   1321 		      po-end-of-entry (match-end 0))
   1322 		(if (re-search-forward po-any-msgstr-regexp nil t)
   1323 		    (progn
   1324 		      ;; We definitely were not in the crumb.
   1325 		      (setq po-start-of-msgstr (match-beginning 0)
   1326 			    po-end-of-entry (match-end 0)))
   1327 		  ;; We were in the crumb.  The start of the last PO
   1328 		  ;; file entry is the end of the previous msgstr if
   1329 		  ;; any, or else, the beginning of the file.
   1330 		  (goto-char po-start-of-msgstr)
   1331 		  (setq po-start-of-entry
   1332 			(if (re-search-backward po-any-msgstr-regexp nil t)
   1333 			    (match-end 0)
   1334 			  (point-min)))))
   1335 	    ;; The cursor was inside msgstr of the current entry.
   1336 	    (setq po-start-of-msgstr (match-beginning 0)
   1337 		  po-end-of-entry (match-end 0))
   1338 	    ;; The start of this entry is the end of the previous
   1339 	    ;; msgstr if any, or else, the beginning of the file.
   1340 	    (goto-char po-start-of-msgstr)
   1341 	    (setq po-start-of-entry
   1342 		  (if (re-search-backward po-any-msgstr-regexp nil t)
   1343 		      (match-end 0)
   1344 		    (point-min)))))
   1345       ;; The cursor was before msgstr in the first entry in the file.
   1346       (setq po-start-of-entry (point-min))
   1347       (goto-char po-start-of-entry)
   1348       ;; There is at least the PO file header, so this should match.
   1349       (re-search-forward po-any-msgstr-regexp)
   1350       (setq po-start-of-msgstr (match-beginning 0)
   1351 	    po-end-of-entry (match-end 0)))
   1352     ;; Find start of msgid.
   1353     (goto-char po-start-of-entry)
   1354     (re-search-forward po-any-msgid-regexp)
   1355     (setq po-start-of-msgid (match-beginning 0))
   1356     ;; Classify the entry.
   1357     (setq po-entry-type
   1358 	  (if (eq (following-char) ?#)
   1359 	      'obsolete
   1360 	    (goto-char po-start-of-entry)
   1361 	    (if (re-search-forward po-fuzzy-regexp po-start-of-msgid t)
   1362 		'fuzzy
   1363 	      (goto-char po-start-of-msgstr)
   1364 	      (if (looking-at po-untranslated-regexp)
   1365 		  'untranslated
   1366 		'translated))))
   1367     ;; Put the cursor back where it was.
   1368     (goto-char here)))
   1369 
   1370 (defun po-add-attribute (name)
   1371   "Add attribute NAME to the current entry, unless it is already there."
   1372   (save-excursion
   1373     (let ((buffer-read-only po-read-only))
   1374       (goto-char po-start-of-entry)
   1375       (if (re-search-forward "\n#[,!] .*" po-start-of-msgid t)
   1376 	  (save-restriction
   1377 	    (narrow-to-region (match-beginning 0) (match-end 0))
   1378 	    (goto-char (point-min))
   1379 	    (if (re-search-forward (concat "\\b" name "\\b") nil t)
   1380 		nil
   1381 	      (goto-char (point-max))
   1382 	      (insert ", " name)))
   1383 	(skip-chars-forward "\n")
   1384 	(while (eq (following-char) ?#)
   1385 	  (next-line 1))
   1386 	(insert "#, " name "\n")))))
   1387 
   1388 (defun po-delete-attribute (name)
   1389   "Delete attribute NAME from the current entry, if any."
   1390   (save-excursion
   1391     (let ((buffer-read-only po-read-only))
   1392       (goto-char po-start-of-entry)
   1393       (if (re-search-forward "\n#[,!] .*" po-start-of-msgid t)
   1394 	  (save-restriction
   1395 	    (narrow-to-region (match-beginning 0) (match-end 0))
   1396 	    (goto-char (point-min))
   1397 	    (if (re-search-forward
   1398 		 (concat "\\(\n#[,!] " name "$\\|, " name "$\\| " name ",\\)")
   1399 		 nil t)
   1400 		(replace-match "" t t)))))))
   1401 
   1402 ;;; Entry positionning.
   1404 
   1405 (defun po-say-location-depth ()
   1406   "Tell how many entries in the entry location stack."
   1407   (let ((depth (length po-marker-stack)))
   1408     (cond ((= depth 0) (message (_"Empty location stack")))
   1409 	  ((= depth 1) (message (_"One entry in location stack")))
   1410 	  (t (message (_"%d entries in location stack") depth)))))
   1411 
   1412 (defun po-push-location ()
   1413   "Stack the location of the current entry, for later return."
   1414   (interactive)
   1415   (po-find-span-of-entry)
   1416   (save-excursion
   1417     (goto-char po-start-of-msgid)
   1418     (setq po-marker-stack (cons (point-marker) po-marker-stack)))
   1419   (po-say-location-depth))
   1420 
   1421 (defun po-pop-location ()
   1422   "Unstack a saved location, and return to the corresponding entry."
   1423   (interactive)
   1424   (if po-marker-stack
   1425       (progn
   1426 	(goto-char (car po-marker-stack))
   1427 	(setq po-marker-stack (cdr po-marker-stack))
   1428 	(po-current-entry)
   1429 	(po-say-location-depth))
   1430     (error (_"The entry location stack is empty"))))
   1431 
   1432 (defun po-exchange-location ()
   1433   "Exchange the location of the current entry with the top of stack."
   1434   (interactive)
   1435   (if po-marker-stack
   1436       (progn
   1437 	(po-find-span-of-entry)
   1438 	(goto-char po-start-of-msgid)
   1439 	(let ((location (point-marker)))
   1440 	  (goto-char (car po-marker-stack))
   1441 	  (setq po-marker-stack (cons location (cdr po-marker-stack))))
   1442 	(po-current-entry)
   1443 	(po-say-location-depth))
   1444     (error (_"The entry location stack is empty"))))
   1445 
   1446 (defun po-current-entry ()
   1447   "Display the current entry."
   1448   (interactive)
   1449   (po-find-span-of-entry)
   1450   (po-redisplay))
   1451 
   1452 (defun po-first-entry-with-regexp (regexp)
   1453   "Display the first entry in the file which msgstr matches REGEXP."
   1454   (let ((here (point)))
   1455     (goto-char (point-min))
   1456     (if (re-search-forward regexp nil t)
   1457 	(progn
   1458 	  (goto-char (match-beginning 0))
   1459 	  (po-current-entry))
   1460       (goto-char here)
   1461       (error (_"There is no such entry")))))
   1462 
   1463 (defun po-last-entry-with-regexp (regexp)
   1464   "Display the last entry in the file which msgstr matches REGEXP."
   1465   (let ((here (point)))
   1466     (goto-char (point-max))
   1467     (if (re-search-backward regexp nil t)
   1468 	(po-current-entry)
   1469       (goto-char here)
   1470       (error (_"There is no such entry")))))
   1471 
   1472 (defun po-next-entry-with-regexp (regexp wrap)
   1473   "Display the entry following the current entry which msgstr matches REGEXP.
   1474 If WRAP is not nil, the search may wrap around the buffer."
   1475   (po-find-span-of-entry)
   1476   (let ((here (point)))
   1477     (goto-char po-end-of-entry)
   1478     (if (re-search-forward regexp nil t)
   1479 	(progn
   1480 	  (goto-char (match-beginning 0))
   1481 	  (po-current-entry))
   1482       (if (and wrap
   1483 	       (progn
   1484 		 (goto-char (point-min))
   1485 		 (re-search-forward regexp po-start-of-entry t)))
   1486 	  (progn
   1487 	    (goto-char (match-beginning 0))
   1488 	    (po-current-entry)
   1489 	    (message (_"Wrapping around the buffer")))
   1490 	(goto-char here)
   1491 	(error (_"There is no such entry"))))))
   1492 
   1493 (defun po-previous-entry-with-regexp (regexp wrap)
   1494   "Redisplay the entry preceding the current entry which msgstr matches REGEXP.
   1495 If WRAP is not nil, the search may wrap around the buffer."
   1496   (po-find-span-of-entry)
   1497   (let ((here (point)))
   1498     (goto-char po-start-of-entry)
   1499     (if (re-search-backward regexp nil t)
   1500 	(po-current-entry)
   1501       (if (and wrap
   1502 	       (progn
   1503 		 (goto-char (point-max))
   1504 		 (re-search-backward regexp po-end-of-entry t)))
   1505 	  (progn
   1506 	    (po-current-entry)
   1507 	    (message (_"Wrapping around the buffer")))
   1508 	(goto-char here)
   1509 	(error (_"There is no such entry"))))))
   1510 
   1511 ;; Any entries.
   1512 
   1513 (defun po-first-entry ()
   1514   "Display the first entry."
   1515   (interactive)
   1516   (po-first-entry-with-regexp po-any-msgstr-regexp))
   1517 
   1518 (defun po-last-entry ()
   1519   "Display the last entry."
   1520   (interactive)
   1521   (po-last-entry-with-regexp po-any-msgstr-regexp))
   1522 
   1523 (defun po-next-entry ()
   1524   "Display the entry following the current entry."
   1525   (interactive)
   1526   (po-next-entry-with-regexp po-any-msgstr-regexp nil))
   1527 
   1528 (defun po-previous-entry ()
   1529   "Display the entry preceding the current entry."
   1530   (interactive)
   1531   (po-previous-entry-with-regexp po-any-msgstr-regexp nil))
   1532 
   1533 ;; Untranslated entries.
   1534 
   1535 (defvar po-after-entry-regexp
   1536   "\\(\\'\\|\\(#[ \t]*\\)?$\\)"
   1537   "Regexp which should be true after a full msgstr string matched.")
   1538 
   1539 (defvar po-untranslated-regexp
   1540   (concat "^msgstr[ \t]*\"\"\n" po-after-entry-regexp)
   1541   "Regexp matching a whole msgstr field, but only if active and empty.")
   1542 
   1543 (defun po-next-untranslated-entry ()
   1544   "Find the next untranslated entry, wrapping around if necessary."
   1545   (interactive)
   1546   (po-next-entry-with-regexp po-untranslated-regexp t))
   1547 
   1548 (defun po-previous-untranslated-entry ()
   1549   "Find the previous untranslated entry, wrapping around if necessary."
   1550   (interactive)
   1551   (po-previous-entry-with-regexp po-untranslated-regexp t))
   1552 
   1553 (defun po-msgid-to-msgstr ()
   1554   "Use another window to edit msgstr reinitialized with msgid."
   1555   (interactive)
   1556   (po-find-span-of-entry)
   1557   (if (or (eq po-entry-type 'untranslated)
   1558 	  (eq po-entry-type 'obsolete)
   1559 	  (y-or-n-p (_"Really loose previous translation? ")))
   1560       (po-set-msgstr (po-get-msgid nil)))
   1561   (message ""))
   1562 
   1563 ;; Obsolete entries.
   1564 
   1565 (defvar po-obsolete-msgstr-regexp
   1566   "^#~[ \t]*msgstr.*\n\\(#~[ \t]*\".*\n\\)*"
   1567   "Regexp matching a whole msgstr field of an obsolete entry.")
   1568 
   1569 (defun po-next-obsolete-entry ()
   1570   "Find the next obsolete entry, wrapping around if necessary."
   1571   (interactive)
   1572   (po-next-entry-with-regexp po-obsolete-msgstr-regexp t))
   1573 
   1574 (defun po-previous-obsolete-entry ()
   1575   "Find the previous obsolete entry, wrapping around if necessary."
   1576   (interactive)
   1577   (po-previous-entry-with-regexp po-obsolete-msgstr-regexp t))
   1578 
   1579 ;; Fuzzy entries.
   1580 
   1581 (defvar po-fuzzy-regexp "^#[,!] .*fuzzy"
   1582   "Regexp matching the string inserted by msgmerge for translations
   1583 which does not match exactly.")
   1584 
   1585 (defun po-next-fuzzy-entry ()
   1586   "Find the next fuzzy entry, wrapping around if necessary."
   1587   (interactive)
   1588   (po-next-entry-with-regexp po-fuzzy-regexp t))
   1589 
   1590 (defun po-previous-fuzzy-entry ()
   1591   "Find the next fuzzy entry, wrapping around if necessary."
   1592   (interactive)
   1593   (po-previous-entry-with-regexp po-fuzzy-regexp t))
   1594 
   1595 (defun po-unfuzzy ()
   1596   "Remove the fuzzy attribute for the current entry."
   1597   (interactive)
   1598   (po-find-span-of-entry)
   1599   (cond ((eq po-entry-type 'fuzzy)
   1600 	 (po-decrease-type-counter)
   1601 	 (po-delete-attribute "fuzzy")
   1602 	 (po-current-entry)
   1603 	 (po-increase-type-counter)))
   1604   (if po-auto-select-on-unfuzzy
   1605       (po-auto-select-entry))
   1606   (po-update-mode-line-string))
   1607 
   1608 ;; Translated entries.
   1609 
   1610 (defun po-next-translated-entry ()
   1611   "Find the next translated entry, wrapping around if necessary."
   1612   (interactive)
   1613   (if (= po-translated-counter 0)
   1614       (error (_"There is no such entry"))
   1615     (po-next-entry-with-regexp po-any-msgstr-regexp t)
   1616     (po-find-span-of-entry)
   1617     (while (not (eq po-entry-type 'translated))
   1618       (po-next-entry-with-regexp po-any-msgstr-regexp t)
   1619       (po-find-span-of-entry))))
   1620 
   1621 (defun po-previous-translated-entry ()
   1622   "Find the previous translated entry, wrapping around if necessary."
   1623   (interactive)
   1624   (if (= po-translated-counter 0)
   1625       (error (_"There is no such entry"))
   1626     (po-previous-entry-with-regexp po-any-msgstr-regexp t)
   1627     (po-find-span-of-entry)
   1628     (while (not (eq po-entry-type 'translated))
   1629       (po-previous-entry-with-regexp po-untranslated-regexp t)
   1630       (po-find-span-of-entry))))
   1631 
   1632 ;; Auto-selection feature.
   1633 
   1634 (defun po-auto-select-entry ()
   1635   "Select the next entry having the same type as the current one.
   1636 If none, wrap from the beginning of the buffer with another type,
   1637 going from untranslated to fuzzy, and from fuzzy to obsolete.
   1638 Plain translated entries are always disregarded unless there are
   1639 no entries of the other types."
   1640   (interactive)
   1641   (po-find-span-of-entry)
   1642   (goto-char po-end-of-entry)
   1643   (if (and (= po-untranslated-counter 0)
   1644 	   (= po-fuzzy-counter 0)
   1645 	   (= po-obsolete-counter 0))
   1646       ;; All entries are plain translated.  Next entry will do, or
   1647       ;; wrap around if there is none.
   1648       (if (re-search-forward po-any-msgstr-regexp nil t)
   1649 	  (goto-char (match-beginning 0))
   1650 	(goto-char (point-min)))
   1651     ;; If over a translated entry, look for an untranslated one first.
   1652     ;; Else, look for an entry of the same type first.
   1653     (let ((goal (if (eq po-entry-type 'translated)
   1654 		    'untranslated
   1655 		  po-entry-type)))
   1656       (while goal
   1657 	;; Find an untranslated entry, or wrap up for a fuzzy entry.
   1658 	(if (eq goal 'untranslated)
   1659 	    (if (and (> po-untranslated-counter 0)
   1660 		     (re-search-forward po-untranslated-regexp nil t))
   1661 		(progn
   1662 		  (goto-char (match-beginning 0))
   1663 		  (setq goal nil))
   1664 	      (goto-char (point-min))
   1665 	      (setq goal 'fuzzy)))
   1666 	;; Find a fuzzy entry, or wrap up for an obsolete entry.
   1667 	(if (eq goal 'fuzzy)
   1668 	    (if (and (> po-fuzzy-counter 0)
   1669 		     (re-search-forward po-fuzzy-regexp nil t))
   1670 		(progn
   1671 		  (goto-char (match-beginning 0))
   1672 		  (setq goal nil))
   1673 	      (goto-char (point-min))
   1674 	      (setq goal 'obsolete)))
   1675 	;; Find an obsolete entry, or wrap up for an untranslated entry.
   1676 	(if (eq goal 'obsolete)
   1677 	    (if (and (> po-obsolete-counter 0)
   1678 		     (re-search-forward po-obsolete-msgstr-regexp nil t))
   1679 		(progn
   1680 		  (goto-char (match-beginning 0))
   1681 		  (setq goal nil))
   1682 	      (goto-char (point-min))
   1683 	      (setq goal 'untranslated))))))
   1684   ;; Display this entry nicely.
   1685   (po-current-entry))
   1686 
   1687 ;;; Killing and yanking fields.
   1689 
   1690 (defun po-extract-unquoted (buffer start end)
   1691   "Extract and return the unquoted string in BUFFER going from START to END.
   1692 Crumb preceding or following the quoted string is ignored."
   1693   (save-excursion
   1694     (goto-char start)
   1695     (search-forward "\"")
   1696     (setq start (point))
   1697     (goto-char end)
   1698     (search-backward "\"")
   1699     (setq end (point)))
   1700   (po-extract-part-unquoted buffer start end))
   1701 
   1702 (defun po-extract-part-unquoted (buffer start end)
   1703   "Extract and return the unquoted string in BUFFER going from START to END.
   1704 Surrounding quotes are already excluded by the position of START and END."
   1705   (po-with-temp-buffer
   1706    (insert-buffer-substring buffer start end)
   1707    ;; Glue concatenated strings.
   1708    (goto-char (point-min))
   1709    (while (re-search-forward "\"[ \t]*\\\\?\n\\(#~\\)?[ \t]*\"" nil t)
   1710      (replace-match "" t t))
   1711    ;; Remove escaped newlines.
   1712    (goto-char (point-min))
   1713    (while (re-search-forward "\\\\[ \t]*\n" nil t)
   1714      (replace-match "" t t))
   1715    ;; Unquote individual characters.
   1716    (goto-char (point-min))
   1717    (while (re-search-forward "\\\\[\"abfnt\\0-7]" nil t)
   1718      (cond ((eq (preceding-char) ?\") (replace-match "\"" t t))
   1719 	   ((eq (preceding-char) ?a) (replace-match "\a" t t))
   1720 	   ((eq (preceding-char) ?b) (replace-match "\b" t t))
   1721 	   ((eq (preceding-char) ?f) (replace-match "\f" t t))
   1722 	   ((eq (preceding-char) ?n) (replace-match "\n" t t))
   1723 	   ((eq (preceding-char) ?t) (replace-match "\t" t t))
   1724 	   ((eq (preceding-char) ?\\) (replace-match "\\" t t))
   1725 	   (t (let ((value (- (preceding-char) ?0)))
   1726 		(replace-match "" t t)
   1727 		(while (looking-at "[0-7]")
   1728 		  (setq value (+ (* 8 value) (- (following-char) ?0)))
   1729 		  (replace-match "" t t))
   1730 		(insert value)))))
   1731    (buffer-string)))
   1732 
   1733 (defun po-eval-requoted (form prefix obsolete)
   1734   "Eval FORM, which inserts a string, and return the string fully requoted.
   1735 If PREFIX, precede the result with its contents.  If OBSOLETE, comment all
   1736 generated lines in the returned string.  Evaluating FORM should insert the
   1737 wanted string in the buffer which is current at the time of evaluation.
   1738 If FORM is itself a string, then this string is used for insertion."
   1739   (po-with-temp-buffer
   1740     (if (stringp form)
   1741 	(insert form)
   1742       (push-mark)
   1743       (eval form))
   1744     (goto-char (point-min))
   1745     (let ((multi-line (re-search-forward "[^\n]\n+[^\n]" nil t)))
   1746       (goto-char (point-min))
   1747       (while (re-search-forward "[\"\a\b\f\n\r\t\\]" nil t)
   1748 	(cond ((eq (preceding-char) ?\") (replace-match "\\\"" t t))
   1749 	      ((eq (preceding-char) ?\a) (replace-match "\\a" t t))
   1750 	      ((eq (preceding-char) ?\b) (replace-match "\\b" t t))
   1751 	      ((eq (preceding-char) ?\f) (replace-match "\\f" t t))
   1752 	      ((eq (preceding-char) ?\n)
   1753 	       (replace-match (if (or (not multi-line) (eobp))
   1754 				  "\\n"
   1755 				"\\n\"\n\"")
   1756 			      t t))
   1757 	      ((eq (preceding-char) ?\r) (replace-match "\\r" t t))
   1758 	      ((eq (preceding-char) ?\t) (replace-match "\\t" t t))
   1759 	      ((eq (preceding-char) ?\\) (replace-match "\\\\" t t))))
   1760       (goto-char (point-min))
   1761       (if prefix (insert prefix " "))
   1762       (insert (if multi-line "\"\"\n\"" "\""))
   1763       (goto-char (point-max))
   1764       (insert "\"")
   1765       (if prefix (insert "\n"))
   1766       (if obsolete
   1767 	  (progn
   1768 	    (goto-char (point-min))
   1769 	    (while (not (eobp))
   1770 	      (or (eq (following-char) ?\n) (insert "#~ "))
   1771 	      (search-forward "\n"))))
   1772       (buffer-string))))
   1773 
   1774 (defun po-get-msgid (kill)
   1775   "Extract and return the unquoted msgid string.
   1776 If KILL, then add the unquoted string to the kill ring."
   1777   (let ((string (po-extract-unquoted (current-buffer)
   1778 				     po-start-of-msgid po-start-of-msgstr)))
   1779     (if kill (po-kill-new string))
   1780     string))
   1781 
   1782 (defun po-get-msgstr (kill)
   1783   "Extract and return the unquoted msgstr string.
   1784 If KILL, then add the unquoted string to the kill ring."
   1785   (let ((string (po-extract-unquoted (current-buffer)
   1786 				     po-start-of-msgstr po-end-of-entry)))
   1787     (if kill (po-kill-new string))
   1788     string))
   1789 
   1790 (defun po-set-msgid (form)
   1791   "Replace the current msgid, using FORM to get a string.
   1792 Evaluating FORM should insert the wanted string in the current buffer.  If
   1793 FORM is itself a string, then this string is used for insertion.  The string
   1794 is properly requoted before the replacement occurs.
   1795 
   1796 Returns 'nil' if the buffer has not been modified, for if the new msgid
   1797 described by FORM is merely identical to the msgid already in place."
   1798   (let ((string (po-eval-requoted form "msgid" (eq po-entry-type 'obsolete))))
   1799     (save-excursion
   1800       (goto-char po-start-of-entry)
   1801       (re-search-forward po-any-msgid-regexp po-start-of-msgstr)
   1802       (and (not (string-equal (po-match-string 0) string))
   1803 	   (let ((buffer-read-only po-read-only))
   1804 	     (replace-match string t t)
   1805 	     (goto-char po-start-of-msgid)
   1806 	     (po-find-span-of-entry)
   1807 	     t)))))
   1808 
   1809 (defun po-set-msgstr (form)
   1810   "Replace the current msgstr or msgstr[], using FORM to get a string.
   1811 Evaluating FORM should insert the wanted string in the current buffer.  If
   1812 FORM is itself a string, then this string is used for insertion.  The string
   1813 is properly requoted before the replacement occurs.
   1814 
   1815 Returns 'nil' if the buffer has not been modified, for if the new msgstr
   1816 described by FORM is merely identical to the msgstr already in place."
   1817   (let ((string (po-eval-requoted form "msgstr" (eq po-entry-type 'obsolete)))
   1818         (msgstr-idx nil))
   1819     (save-excursion
   1820       (goto-char po-start-of-entry)
   1821       (save-excursion                   ; check for an indexed msgstr
   1822         (if (re-search-forward po-msgstr-idx-keyword-regexp
   1823 			       po-end-of-entry t)
   1824 	    (setq msgstr-idx (buffer-substring-no-properties
   1825 			      (match-beginning 0) (match-end 0)))))
   1826       (re-search-forward po-any-msgstr-regexp po-end-of-entry)
   1827       (and (not (string-equal (po-match-string 0) string))
   1828 	   (let ((buffer-read-only po-read-only))
   1829 	     (po-decrease-type-counter)
   1830 	     (replace-match string t t)
   1831              (goto-char (match-beginning 0))
   1832              (if (eq msgstr-idx nil) ; hack: replace msgstr with msgstr[d]
   1833 		 nil
   1834 	       (insert msgstr-idx)
   1835 	       (looking-at "\\(#~[ \t]*\\)?msgstr")
   1836 	       (replace-match ""))
   1837 	     (goto-char po-start-of-msgid)
   1838 	     (po-find-span-of-entry)
   1839 	     (po-increase-type-counter)
   1840 	     t)))))
   1841 
   1842 (defun po-kill-ring-save-msgstr ()
   1843   "Push the msgstr string from current entry on the kill ring."
   1844   (interactive)
   1845   (po-find-span-of-entry)
   1846   (po-get-msgstr t))
   1847 
   1848 (defun po-kill-msgstr ()
   1849   "Empty the msgstr string from current entry, pushing it on the kill ring."
   1850   (interactive)
   1851   (po-kill-ring-save-msgstr)
   1852   (po-set-msgstr ""))
   1853 
   1854 (defun po-yank-msgstr ()
   1855   "Replace the current msgstr string by the top of the kill ring."
   1856   (interactive)
   1857   (po-find-span-of-entry)
   1858   (po-set-msgstr (if (eq last-command 'yank) '(yank-pop 1) '(yank)))
   1859   (setq this-command 'yank))
   1860 
   1861 (defun po-fade-out-entry ()
   1862   "Mark an active entry as fuzzy; obsolete a fuzzy or untranslated entry;
   1863 or completely delete an obsolete entry, saving its msgstr on the kill ring."
   1864   (interactive)
   1865   (po-find-span-of-entry)
   1866 
   1867   (cond ((eq po-entry-type 'translated)
   1868 	 (po-decrease-type-counter)
   1869 	 (po-add-attribute "fuzzy")
   1870 	 (po-current-entry)
   1871 	 (po-increase-type-counter))
   1872 
   1873 	((or (eq po-entry-type 'fuzzy)
   1874 	     (eq po-entry-type 'untranslated))
   1875 	 (if (y-or-n-p (_"Should I really obsolete this entry? "))
   1876 	     (progn
   1877 	       (po-decrease-type-counter)
   1878 	       (save-excursion
   1879 		 (save-restriction
   1880 		   (narrow-to-region po-start-of-entry po-end-of-entry)
   1881 		   (let ((buffer-read-only po-read-only))
   1882 		     (goto-char (point-min))
   1883 		     (skip-chars-forward "\n")
   1884 		     (while (not (eobp))
   1885 		       (insert "#~ ")
   1886 		       (search-forward "\n")))))
   1887 	       (po-current-entry)
   1888 	       (po-increase-type-counter)))
   1889 	 (message ""))
   1890 
   1891 	((and (eq po-entry-type 'obsolete)
   1892 	      (po-check-for-pending-edit po-start-of-msgid)
   1893 	      (po-check-for-pending-edit po-start-of-msgstr))
   1894 	 (po-decrease-type-counter)
   1895 	 (po-update-mode-line-string)
   1896 	 (po-get-msgstr t)
   1897 	 (let ((buffer-read-only po-read-only))
   1898 	   (delete-region po-start-of-entry po-end-of-entry))
   1899 	 (goto-char po-start-of-entry)
   1900 	 (if (re-search-forward po-any-msgstr-regexp nil t)
   1901 	     (goto-char (match-beginning 0))
   1902 	   (re-search-backward po-any-msgstr-regexp nil t))
   1903 	 (po-current-entry)
   1904 	 (message ""))))
   1905 
   1906 ;;; Killing and yanking comments.
   1908 
   1909 (defvar po-active-comment-regexp
   1910   "^\\(#\n\\|# .*\n\\)+"
   1911   "Regexp matching the whole editable comment part of an active entry.")
   1912 
   1913 (defvar po-obsolete-comment-regexp
   1914   "^\\(#~ #\n\\|#~ # .*\n\\)+"
   1915   "Regexp matching the whole editable comment part of an obsolete entry.")
   1916 
   1917 (defun po-get-comment (kill-flag)
   1918   "Extract and return the editable comment string, uncommented.
   1919 If KILL-FLAG, then add the unquoted comment to the kill ring."
   1920   (let ((buffer (current-buffer))
   1921 	(obsolete (eq po-entry-type 'obsolete)))
   1922     (save-excursion
   1923       (goto-char po-start-of-entry)
   1924       (if (re-search-forward (if obsolete po-obsolete-comment-regexp
   1925 			         po-active-comment-regexp)
   1926 			     po-end-of-entry t)
   1927 	  (po-with-temp-buffer
   1928 	    (insert-buffer-substring buffer (match-beginning 0) (match-end 0))
   1929 	    (goto-char (point-min))
   1930 	    (while (not (eobp))
   1931 	      (if (looking-at (if obsolete "#~ # ?" "# ?"))
   1932 		  (replace-match "" t t))
   1933 	      (forward-line 1))
   1934 	    (and kill-flag (copy-region-as-kill (point-min) (point-max)))
   1935 	    (buffer-string))
   1936 	""))))
   1937 
   1938 (defun po-set-comment (form)
   1939   "Using FORM to get a string, replace the current editable comment.
   1940 Evaluating FORM should insert the wanted string in the current buffer.
   1941 If FORM is itself a string, then this string is used for insertion.
   1942 The string is properly recommented before the replacement occurs."
   1943   (let ((obsolete (eq po-entry-type 'obsolete))
   1944 	string)
   1945     (po-with-temp-buffer
   1946       (if (stringp form)
   1947 	  (insert form)
   1948 	(push-mark)
   1949 	(eval form))
   1950       (if (not (or (bobp) (= (preceding-char) ?\n)))
   1951 	  (insert "\n"))
   1952       (goto-char (point-min))
   1953       (while (not (eobp))
   1954 	(insert (if (= (following-char) ?\n)
   1955 		    (if obsolete "#~ #" "#")
   1956 		  (if obsolete "#~ # " "# ")))
   1957 	(search-forward "\n"))
   1958       (setq string (buffer-string)))
   1959     (goto-char po-start-of-entry)
   1960     (if (re-search-forward
   1961 	 (if obsolete po-obsolete-comment-regexp po-active-comment-regexp)
   1962 	 po-end-of-entry t)
   1963 	(if (not (string-equal (po-match-string 0) string))
   1964 	    (let ((buffer-read-only po-read-only))
   1965 	      (replace-match string t t)))
   1966       (skip-chars-forward " \t\n")
   1967       (let ((buffer-read-only po-read-only))
   1968 	(insert string))))
   1969   (po-current-entry))
   1970 
   1971 (defun po-kill-ring-save-comment ()
   1972   "Push the msgstr string from current entry on the kill ring."
   1973   (interactive)
   1974   (po-find-span-of-entry)
   1975   (po-get-comment t))
   1976 
   1977 (defun po-kill-comment ()
   1978   "Empty the msgstr string from current entry, pushing it on the kill ring."
   1979   (interactive)
   1980   (po-kill-ring-save-comment)
   1981   (po-set-comment "")
   1982   (po-redisplay))
   1983 
   1984 (defun po-yank-comment ()
   1985   "Replace the current comment string by the top of the kill ring."
   1986   (interactive)
   1987   (po-find-span-of-entry)
   1988   (po-set-comment (if (eq last-command 'yank) '(yank-pop 1) '(yank)))
   1989   (setq this-command 'yank)
   1990   (po-redisplay))
   1991 
   1992 ;;; Editing management and submode.
   1994 
   1995 ;; In a string edit buffer, BACK-POINTER points to one of the slots of the
   1996 ;; list EDITED-FIELDS kept in the PO buffer.  See its description elsewhere.
   1997 ;; Reminder: slots have the form (ENTRY-MARKER EDIT-BUFFER OVERLAY-INFO).
   1998 
   1999 (defvar po-subedit-back-pointer)
   2000 
   2001 (defun po-clean-out-killed-edits ()
   2002   "From EDITED-FIELDS, clean out any edit having a killed edit buffer."
   2003   (let ((cursor po-edited-fields))
   2004     (while cursor
   2005       (let ((slot (car cursor)))
   2006 	(setq cursor (cdr cursor))
   2007 	(if (buffer-name (nth 1 slot))
   2008 	    nil
   2009 	  (let ((overlay (nth 2 slot)))
   2010 	    (and overlay (po-dehighlight overlay)))
   2011 	  (setq po-edited-fields (delete slot po-edited-fields)))))))
   2012 
   2013 (defun po-check-all-pending-edits ()
   2014   "Resume any pending edit.  Return nil if some remains."
   2015   (po-clean-out-killed-edits)
   2016   (or (null po-edited-fields)
   2017       (let ((slot (car po-edited-fields)))
   2018 	(goto-char (nth 0 slot))
   2019 	(pop-to-buffer (nth 1 slot))
   2020 	(let ((overlay (nth 2 slot)))
   2021 	  (and overlay (po-rehighlight overlay)))
   2022 	(message po-subedit-message)
   2023 	nil)))
   2024 
   2025 (defun po-check-for-pending-edit (position)
   2026   "Resume any pending edit at POSITION.  Return nil if such edit exists."
   2027   (po-clean-out-killed-edits)
   2028   (let ((marker (make-marker)))
   2029     (set-marker marker position)
   2030     (let ((slot (assoc marker po-edited-fields)))
   2031       (if slot
   2032 	  (progn
   2033 	    (goto-char marker)
   2034 	    (pop-to-buffer (nth 1 slot))
   2035 	    (let ((overlay (nth 2 slot)))
   2036 	      (and overlay (po-rehighlight overlay)))
   2037 	    (message po-subedit-message)))
   2038       (not slot))))
   2039 
   2040 (defun po-edit-out-full ()
   2041   "Get out of PO mode, leaving PO file buffer in fundamental mode."
   2042   (interactive)
   2043   (if (and (po-check-all-pending-edits)
   2044 	   (yes-or-no-p (_"Should I let you edit the whole PO file? ")))
   2045       (progn
   2046 	(setq buffer-read-only po-read-only)
   2047 	(fundamental-mode)
   2048 	(message (_"Type 'M-x po-mode RET' once done")))))
   2049 
   2050 (defun po-ediff-quit ()
   2051   "Quit ediff and exit `recursive-edit'."
   2052   (interactive)
   2053   (ediff-quit t)
   2054   (exit-recursive-edit))
   2055 
   2056 (add-hook 'ediff-keymap-setup-hook
   2057 	  '(lambda ()
   2058 	     (define-key ediff-mode-map "Q" 'po-ediff-quit)))
   2059 
   2060 (defun po-ediff-buffers-exit-recursive (b1 b2 oldbuf end)
   2061   "Ediff buffer B1 and B2, pop back to OLDBUF and replace the old variants.
   2062 This function will delete the first two variants in OLDBUF, call
   2063 `ediff-buffers' to compare both strings and replace the two variants in
   2064 OLDBUF with the contents of B2.
   2065 Once done kill B1 and B2.
   2066 
   2067 For more info cf. `po-subedit-ediff'."
   2068   (ediff-buffers b1 b2)
   2069   (recursive-edit)
   2070   (pop-to-buffer oldbuf)
   2071   (delete-region (point-min) end)
   2072   (insert-buffer b2)
   2073   (mapc 'kill-buffer `(,b1 ,b2))
   2074   (display-buffer entry-buffer t))
   2075 
   2076 (defun po-subedit-ediff ()
   2077   "Edit the subedit buffer using `ediff'.
   2078 `po-subedit-ediff' calls `po-ediff-buffers-exit-recursive' to edit translation
   2079 variants side by side if they are actually different; if variants are equal just
   2080 delete the first one.
   2081 
   2082 `msgcat' is able to produce those variants; every variant is marked with:
   2083 
   2084 #-#-#-#-#  file name reference  #-#-#-#-#
   2085 
   2086 Put changes in second buffer.
   2087 
   2088 When done with the `ediff' session press \\[exit-recursive-edit] exit to
   2089 `recursive-edit', or call \\[po-ediff-quit] (`Q') in the ediff control panel."
   2090   (interactive)
   2091   (let* ((marker-regex "^#-#-#-#-#  \\(.*\\)  #-#-#-#-#\n")
   2092 	 (buf1 " *po-msgstr-1") ; default if first marker is missing
   2093 	 buf2 start-1 end-1 start-2 end-2
   2094 	 (back-pointer po-subedit-back-pointer)
   2095 	 (entry-marker (nth 0 back-pointer))
   2096 	 (entry-buffer (marker-buffer entry-marker)))
   2097     (goto-char (point-min))
   2098     (if (looking-at marker-regex)
   2099 	(and (setq buf1 (match-string-no-properties 1))
   2100 	     (forward-line 1)))
   2101     (setq start-1 (point))
   2102     (if (not (re-search-forward marker-regex (point-max) t))
   2103 	(error "Only 1 msgstr found")
   2104       (setq buf2 (match-string-no-properties 1)
   2105 	    end-1 (match-beginning 0))
   2106       (let ((oldbuf (current-buffer)))
   2107 	(save-current-buffer
   2108 	  (set-buffer (get-buffer-create
   2109 		       (generate-new-buffer-name buf1)))
   2110 	  (setq buffer-read-only nil)
   2111 	  (erase-buffer)
   2112 	  (insert-buffer-substring oldbuf start-1 end-1)
   2113 	  (setq buffer-read-only t))
   2114 
   2115 	(setq start-2 (point))
   2116 	(save-excursion
   2117 	  ;; check for a third variant; if found ignore it
   2118 	  (if (re-search-forward marker-regex (point-max) t)
   2119 	      (setq end-2 (match-beginning 0))
   2120 	    (setq end-2 (goto-char (1- (point-max))))))
   2121 	(save-current-buffer
   2122 	  (set-buffer (get-buffer-create
   2123 		       (generate-new-buffer-name buf2)))
   2124 	  (erase-buffer)
   2125 	  (insert-buffer-substring oldbuf start-2 end-2))
   2126 
   2127 	(if (not (string-equal (buffer-substring-no-properties start-1 end-1)
   2128 			       (buffer-substring-no-properties start-2 end-2)))
   2129 	    (po-ediff-buffers-exit-recursive buf1 buf2 oldbuf end-2)
   2130 	  (message "Variants are equal; delete %s" buf1)
   2131 	  (forward-line -1)
   2132 	  (delete-region (point-min) (point)))))))
   2133 
   2134 (defun po-subedit-abort ()
   2135   "Exit the subedit buffer, merely discarding its contents."
   2136   (interactive)
   2137   (let* ((edit-buffer (current-buffer))
   2138 	 (back-pointer po-subedit-back-pointer)
   2139 	 (entry-marker (nth 0 back-pointer))
   2140 	 (overlay-info (nth 2 back-pointer))
   2141 	 (entry-buffer (marker-buffer entry-marker)))
   2142     (if (null entry-buffer)
   2143 	(error (_"Corresponding PO buffer does not exist anymore"))
   2144       (or (one-window-p) (delete-window))
   2145       (switch-to-buffer entry-buffer)
   2146       (goto-char entry-marker)
   2147       (and overlay-info (po-dehighlight overlay-info))
   2148       (kill-buffer edit-buffer)
   2149       (setq po-edited-fields (delete back-pointer po-edited-fields)))))
   2150 
   2151 (defun po-subedit-exit ()
   2152   "Exit the subedit buffer, replacing the string in the PO buffer."
   2153   (interactive)
   2154   (goto-char (point-max))
   2155   (skip-chars-backward " \t\n")
   2156   (if (eq (preceding-char) ?<)
   2157       (delete-region (1- (point)) (point-max)))
   2158   (run-hooks 'po-subedit-exit-hook)
   2159   (let ((string (buffer-string)))
   2160     (po-subedit-abort)
   2161     (po-find-span-of-entry)
   2162     (cond ((= (point) po-start-of-msgid)
   2163 	   (po-set-comment string)
   2164 	   (po-redisplay))
   2165 	  ((= (point) po-start-of-msgstr)
   2166 	   (let ((replaced (po-set-msgstr string)))
   2167 	     (if (and replaced
   2168 		      po-auto-fuzzy-on-edit
   2169 		      (eq po-entry-type 'translated))
   2170 		 (progn
   2171 		   (po-decrease-type-counter)
   2172 		   (po-add-attribute "fuzzy")
   2173 		   (po-current-entry)
   2174 		   (po-increase-type-counter)))))
   2175 	  (t (debug)))))
   2176 
   2177 (defun po-edit-string (string type expand-tabs)
   2178   "Prepare a pop up buffer for editing STRING, which is of a given TYPE.
   2179 TYPE may be 'comment or 'msgstr.  If EXPAND-TABS, expand tabs to spaces.
   2180 Run functions on po-subedit-mode-hook."
   2181   (let ((marker (make-marker)))
   2182     (set-marker marker (cond ((eq type 'comment) po-start-of-msgid)
   2183 			     ((eq type 'msgstr) po-start-of-msgstr)))
   2184     (if (po-check-for-pending-edit marker)
   2185 	(let ((edit-buffer (generate-new-buffer
   2186 			    (concat "*" (buffer-name) "*")))
   2187 	      (edit-coding buffer-file-coding-system)
   2188 	      (buffer (current-buffer))
   2189 	      overlay slot)
   2190 	  (if (and (eq type 'msgstr) po-highlighting)
   2191 	      ;; ;; Try showing all of msgid in the upper window while editing.
   2192 	      ;; (goto-char (1- po-start-of-msgstr))
   2193 	      ;; (recenter -1)
   2194 	      (save-excursion
   2195 		(goto-char po-start-of-entry)
   2196 		(re-search-forward po-any-msgid-regexp nil t)
   2197 		(let ((end (1- (match-end 0))))
   2198 		  (goto-char (match-beginning 0))
   2199 		  (re-search-forward "msgid +" nil t)
   2200 		  (setq overlay (po-create-overlay))
   2201 		  (po-highlight overlay (point) end buffer))))
   2202 	  (setq slot (list marker edit-buffer overlay)
   2203 		po-edited-fields (cons slot po-edited-fields))
   2204 	  (pop-to-buffer edit-buffer)
   2205 	  (set (make-local-variable 'po-subedit-back-pointer) slot)
   2206 	  (set (make-local-variable 'indent-line-function)
   2207 	       'indent-relative)
   2208 	  (setq buffer-file-coding-system edit-coding)
   2209 	  (setq local-abbrev-table po-mode-abbrev-table)
   2210 	  (erase-buffer)
   2211 	  (insert string "<")
   2212 	  (goto-char (point-min))
   2213 	  (and expand-tabs (setq indent-tabs-mode nil))
   2214 	  (use-local-map po-subedit-mode-map)
   2215 	  (if (fboundp 'easy-menu-define)
   2216 	      (progn
   2217 		(easy-menu-define po-subedit-mode-menu po-subedit-mode-map ""
   2218 		  po-subedit-mode-menu-layout)
   2219 		(and po-XEMACS (easy-menu-add po-subedit-mode-menu))))
   2220 	  (set-syntax-table po-subedit-mode-syntax-table)
   2221 	  (run-hooks 'po-subedit-mode-hook)
   2222 	  (message po-subedit-message)))))
   2223 
   2224 (defun po-edit-comment ()
   2225   "Use another window to edit the current translator comment."
   2226   (interactive)
   2227   (po-find-span-of-entry)
   2228   (po-edit-string (po-get-comment nil) 'comment nil))
   2229 
   2230 (defun po-edit-comment-and-ediff ()
   2231   "Use `ediff' to edit the current translator comment.
   2232 This function calls `po-edit-msgstr' and `po-subedit-ediff'; for more info
   2233 read `po-subedit-ediff' documentation."
   2234   (interactive)
   2235   (po-edit-comment)
   2236   (po-subedit-ediff))
   2237 
   2238 (defun po-edit-msgstr ()
   2239   "Use another window to edit the current msgstr."
   2240   (interactive)
   2241   (po-find-span-of-entry)
   2242   (po-edit-string (if (and po-auto-edit-with-msgid
   2243 			   (eq po-entry-type 'untranslated))
   2244 		      (po-get-msgid nil)
   2245 		    (po-get-msgstr nil))
   2246 		  'msgstr
   2247 		  t))
   2248 
   2249 (defun po-edit-msgstr-and-ediff ()
   2250   "Use `ediff' to edit the current msgstr.
   2251 This function calls `po-edit-msgstr' and `po-subedit-ediff'; for more info
   2252 read `po-subedit-ediff' documentation."
   2253   (interactive)
   2254   (po-edit-msgstr)
   2255   (po-subedit-ediff))
   2256 
   2257 ;;; String normalization and searching.
   2259 
   2260 (defun po-normalize-old-style (explain)
   2261   "Normalize old gettext style fields using K&R C multiline string syntax.
   2262 To minibuffer messages sent while normalizing, add the EXPLAIN string."
   2263   (let ((here (point-marker))
   2264 	(counter 0)
   2265 	(buffer-read-only po-read-only))
   2266     (goto-char (point-min))
   2267     (message (_"Normalizing %d, %s") counter explain)
   2268     (while (re-search-forward
   2269 	    "\\(^#?[ \t]*msg\\(id\\|str\\)[ \t]*\"\\|[^\" \t][ \t]*\\)\\\\\n"
   2270 	    nil t)
   2271       (if (= (% counter 10) 0)
   2272 	  (message (_"Normalizing %d, %s") counter explain))
   2273       (replace-match "\\1\"\n\"" t nil)
   2274       (setq counter (1+ counter)))
   2275     (goto-char here)
   2276     (message (_"Normalizing %d...done") counter)))
   2277 
   2278 (defun po-normalize-field (field explain)
   2279   "Normalize FIELD of all entries.  FIELD is either the symbol msgid or msgstr.
   2280 To minibuffer messages sent while normalizing, add the EXPLAIN string."
   2281   (let ((here (point-marker))
   2282 	(counter 0))
   2283     (goto-char (point-min))
   2284     (while (re-search-forward po-any-msgstr-regexp nil t)
   2285       (if (= (% counter 10) 0)
   2286 	  (message (_"Normalizing %d, %s") counter explain))
   2287       (goto-char (match-beginning 0))
   2288       (po-find-span-of-entry)
   2289       (cond ((eq field 'msgid) (po-set-msgid (po-get-msgid nil)))
   2290 	    ((eq field 'msgstr) (po-set-msgstr (po-get-msgstr nil))))
   2291       (goto-char po-end-of-entry)
   2292       (setq counter (1+ counter)))
   2293     (goto-char here)
   2294     (message (_"Normalizing %d...done") counter)))
   2295 
   2296 ;; Normalize, but the British way! :-)
   2297 (defsubst po-normalise () (po-normalize))
   2298 
   2299 (defun po-normalize ()
   2300   "Normalize all entries in the PO file."
   2301   (interactive)
   2302   (po-normalize-old-style (_"pass 1/3"))
   2303   (po-normalize-field t (_"pass 2/3"))
   2304   (po-normalize-field nil (_"pass 3/3"))
   2305   ;; The last PO file entry has just been processed.
   2306   (if (not (= po-end-of-entry (point-max)))
   2307       (let ((buffer-read-only po-read-only))
   2308 	(kill-region po-end-of-entry (point-max))))
   2309   ;; A bizarre format might have fooled the counters, so recompute
   2310   ;; them to make sure their value is dependable.
   2311   (po-compute-counters nil))
   2312 
   2313 ;;; Multiple PO files.
   2315 
   2316 (defun po-show-auxiliary-list ()
   2317   "Echo the current auxiliary list in the message area."
   2318   (if po-auxiliary-list
   2319       (let ((cursor po-auxiliary-cursor)
   2320 	    string)
   2321 	(while cursor
   2322 	  (setq string (concat string (if string " ") (car (car cursor)))
   2323 		cursor (cdr cursor)))
   2324 	(setq cursor po-auxiliary-list)
   2325 	(while (not (eq cursor po-auxiliary-cursor))
   2326 	  (setq string (concat string (if string " ") (car (car cursor)))
   2327 		cursor (cdr cursor)))
   2328 	(message string))
   2329     (message (_"No auxiliary files."))))
   2330 
   2331 (defun po-consider-as-auxiliary ()
   2332   "Add the current PO file to the list of auxiliary files."
   2333   (interactive)
   2334   (if (member (list buffer-file-name) po-auxiliary-list)
   2335       nil
   2336     (setq po-auxiliary-list
   2337 	  (nconc po-auxiliary-list (list (list buffer-file-name))))
   2338     (or po-auxiliary-cursor
   2339 	(setq po-auxiliary-cursor po-auxiliary-list)))
   2340   (po-show-auxiliary-list))
   2341 
   2342 (defun po-ignore-as-auxiliary ()
   2343   "Delete the current PO file from the list of auxiliary files."
   2344   (interactive)
   2345   (setq po-auxiliary-list (delete (list buffer-file-name) po-auxiliary-list)
   2346 	po-auxiliary-cursor po-auxiliary-list)
   2347   (po-show-auxiliary-list))
   2348 
   2349 (defun po-seek-equivalent-translation (name string)
   2350   "Search a PO file NAME for a 'msgid' STRING having a non-empty 'msgstr'.
   2351 STRING is the full quoted msgid field, including the 'msgid' keyword.  When
   2352 found, display the file over the current window, with the 'msgstr' field
   2353 possibly highlighted, the cursor at start of msgid, then return 't'.
   2354 Otherwise, move nothing, and just return 'nil'."
   2355   (let ((current (current-buffer))
   2356 	(buffer (find-file-noselect name)))
   2357     (set-buffer buffer)
   2358     (let ((start (point))
   2359 	  found)
   2360       (goto-char (point-min))
   2361       (while (and (not found) (search-forward string nil t))
   2362 	;; Screen out longer 'msgid's.
   2363 	(if (looking-at "^msgstr ")
   2364 	    (progn
   2365 	      (po-find-span-of-entry)
   2366 	      ;; Ignore an untranslated entry.
   2367 	      (or (string-equal
   2368 		   (buffer-substring po-start-of-msgstr po-end-of-entry)
   2369 		   "msgstr \"\"\n")
   2370 		  (setq found t)))))
   2371       (if found
   2372 	  (progn
   2373 	    (switch-to-buffer buffer)
   2374 	    (po-find-span-of-entry)
   2375 	    (if po-highlighting
   2376 		(progn
   2377 		  (goto-char po-start-of-entry)
   2378 		  (re-search-forward po-any-msgstr-regexp nil t)
   2379 		  (let ((end (1- (match-end 0))))
   2380 		    (goto-char (match-beginning 0))
   2381 		    (re-search-forward "msgstr +" nil t)
   2382 		    ;; Just "borrow" the marking overlay.
   2383 		    (po-highlight po-marking-overlay (point) end))))
   2384 	    (goto-char po-start-of-msgid))
   2385 	(goto-char start)
   2386 	(po-find-span-of-entry)
   2387 	(set-buffer current))
   2388       found)))
   2389 
   2390 (defun po-cycle-auxiliary ()
   2391   "Select the next auxiliary file having an entry with same 'msgid'."
   2392   (interactive)
   2393   (po-find-span-of-entry)
   2394   (if po-auxiliary-list
   2395       (let ((string (buffer-substring po-start-of-msgid po-start-of-msgstr))
   2396 	    (cursor po-auxiliary-cursor)
   2397 	    found name)
   2398 	(while (and (not found) cursor)
   2399 	  (setq name (car (car cursor)))
   2400 	  (if (and (not (string-equal buffer-file-name name))
   2401 		   (po-seek-equivalent-translation name string))
   2402 	      (setq found t
   2403 		    po-auxiliary-cursor cursor))
   2404 	  (setq cursor (cdr cursor)))
   2405 	(setq cursor po-auxiliary-list)
   2406 	(while (and (not found) cursor)
   2407 	  (setq name (car (car cursor)))
   2408 	  (if (and (not (string-equal buffer-file-name name))
   2409 		   (po-seek-equivalent-translation name string))
   2410 	      (setq found t
   2411 		    po-auxiliary-cursor cursor))
   2412 	  (setq cursor (cdr cursor)))
   2413 	(or found (message (_"No other translation found")))
   2414         found)))
   2415 
   2416 (defun po-subedit-cycle-auxiliary ()
   2417   "Cycle auxiliary file, but from the translation edit buffer."
   2418   (interactive)
   2419   (let* ((entry-marker (nth 0 po-subedit-back-pointer))
   2420 	 (entry-buffer (marker-buffer entry-marker))
   2421 	 (buffer (current-buffer)))
   2422     (pop-to-buffer entry-buffer)
   2423     (po-cycle-auxiliary)
   2424     (pop-to-buffer buffer)))
   2425 
   2426 (defun po-select-auxiliary ()
   2427   "Select one of the available auxiliary files and locate an equivalent entry.
   2428 If an entry having the same 'msgid' cannot be found, merely select the file
   2429 without moving its cursor."
   2430   (interactive)
   2431   (po-find-span-of-entry)
   2432   (if po-auxiliary-list
   2433       (let ((string (buffer-substring po-start-of-msgid po-start-of-msgstr))
   2434 	    (name (car (assoc (completing-read (_"Which auxiliary file? ")
   2435 					       po-auxiliary-list nil t)
   2436 			      po-auxiliary-list))))
   2437 	(po-consider-as-auxiliary)
   2438 	(or (po-seek-equivalent-translation name string)
   2439 	    (find-file name)))))
   2440 
   2441 ;;; Original program sources as context.
   2443 
   2444 (defun po-show-source-path ()
   2445   "Echo the current source search path in the message area."
   2446   (if po-search-path
   2447       (let ((cursor po-search-path)
   2448 	    string)
   2449 	(while cursor
   2450 	  (setq string (concat string (if string " ") (car (car cursor)))
   2451 		cursor (cdr cursor)))
   2452 	(message string))
   2453     (message (_"Empty source path."))))
   2454 
   2455 (defun po-consider-source-path (directory)
   2456   "Add a given DIRECTORY, requested interactively, to the source search path."
   2457   (interactive "DDirectory for search path: ")
   2458   (setq po-search-path (cons (list (if (string-match "/$" directory)
   2459 					 directory
   2460 				       (concat directory "/")))
   2461 			     po-search-path))
   2462   (setq po-reference-check 0)
   2463   (po-show-source-path))
   2464 
   2465 (defun po-ignore-source-path ()
   2466   "Delete a directory, selected with completion, from the source search path."
   2467   (interactive)
   2468   (setq po-search-path
   2469 	(delete (list (completing-read (_"Directory to remove? ")
   2470 				       po-search-path nil t))
   2471 		po-search-path))
   2472   (setq po-reference-check 0)
   2473   (po-show-source-path))
   2474 
   2475 (defun po-ensure-source-references ()
   2476   "Extract all references into a list, with paths resolved, if necessary."
   2477   (po-find-span-of-entry)
   2478   (if (= po-start-of-entry po-reference-check)
   2479       nil
   2480     (setq po-reference-alist nil)
   2481     (save-excursion
   2482       (goto-char po-start-of-entry)
   2483       (if (re-search-forward "^#:" po-start-of-msgid t)
   2484 	  (let (current name line path file)
   2485 	    (while (looking-at "\\(\n#:\\)? *\\([^: ]*\\):\\([0-9]+\\)")
   2486 	      (goto-char (match-end 0))
   2487 	      (setq name (po-match-string 2)
   2488 		    line (po-match-string 3)
   2489 		    path po-search-path)
   2490 	      (if (string-equal name "")
   2491 		  nil
   2492 		(while (and (not (file-exists-p
   2493 				  (setq file (concat (car (car path)) name))))
   2494 			    path)
   2495 		  (setq path (cdr path)))
   2496 		(setq current (and path file)))
   2497 	      (if current
   2498 		  (setq po-reference-alist
   2499 			(cons (list (concat current ":" line)
   2500 				    current
   2501 				    (string-to-number line))
   2502 			      po-reference-alist)))))))
   2503     (setq po-reference-alist (nreverse po-reference-alist)
   2504 	  po-reference-cursor po-reference-alist
   2505 	  po-reference-check po-start-of-entry)))
   2506 
   2507 (defun po-show-source-context (triplet)
   2508   "Show the source context given a TRIPLET which is (PROMPT FILE LINE)."
   2509   (find-file-other-window (car (cdr triplet)))
   2510   (goto-line (car (cdr (cdr triplet))))
   2511   (other-window 1)
   2512   (let ((maximum 0)
   2513 	position
   2514 	(cursor po-reference-alist))
   2515     (while (not (eq triplet (car cursor)))
   2516       (setq maximum (1+ maximum)
   2517 	    cursor (cdr cursor)))
   2518     (setq position (1+ maximum)
   2519 	  po-reference-cursor cursor)
   2520     (while cursor
   2521       (setq maximum (1+ maximum)
   2522 	    cursor (cdr cursor)))
   2523     (message (_"Displaying %d/%d: \"%s\"") position maximum (car triplet))))
   2524 
   2525 (defun po-cycle-source-reference ()
   2526   "Display some source context for the current entry.
   2527 If the command is repeated many times in a row, cycle through contexts."
   2528   (interactive)
   2529   (po-ensure-source-references)
   2530   (if po-reference-cursor
   2531       (po-show-source-context
   2532        (car (if (eq last-command 'po-cycle-source-reference)
   2533 		(or (cdr po-reference-cursor) po-reference-alist)
   2534 	      po-reference-cursor)))
   2535     (error (_"No resolved source references"))))
   2536 
   2537 (defun po-select-source-reference ()
   2538   "Select one of the available source contexts for the current entry."
   2539   (interactive)
   2540   (po-ensure-source-references)
   2541   (if po-reference-alist
   2542       (po-show-source-context
   2543        (assoc
   2544 	(completing-read (_"Which source context? ") po-reference-alist nil t)
   2545 	po-reference-alist))
   2546     (error (_"No resolved source references"))))
   2547 
   2548 ;;; String marking in program sources, through TAGS table.
   2550 
   2551 ;; Globally defined within tags.el.
   2552 (defvar tags-loop-operate)
   2553 (defvar tags-loop-scan)
   2554 
   2555 ;; Locally set in each program source buffer.
   2556 (defvar po-find-string-function)
   2557 (defvar po-mark-string-function)
   2558 
   2559 ;; Dynamically set within po-tags-search for po-tags-loop-operate.
   2560 (defvar po-current-po-buffer)
   2561 (defvar po-current-po-keywords)
   2562 
   2563 (defun po-tags-search (restart)
   2564   "Find an unmarked translatable string through all files in tags table.
   2565 Disregard some simple strings which are most probably non-translatable.
   2566 With prefix argument, restart search at first file."
   2567   (interactive "P")
   2568   (require 'etags)
   2569   ;; Ensure there is no highlighting, in case the search fails.
   2570   (if po-highlighting
   2571       (po-dehighlight po-marking-overlay))
   2572   (setq po-string-contents nil)
   2573   ;; Search for a string which might later be marked for translation.
   2574   (let ((po-current-po-buffer (current-buffer))
   2575 	(po-current-po-keywords po-keywords))
   2576     (pop-to-buffer po-string-buffer)
   2577     (if (and (not restart)
   2578 	     (eq (car tags-loop-operate) 'po-tags-loop-operate))
   2579 	;; Continue last po-tags-search.
   2580 	(tags-loop-continue nil)
   2581       ;; Start or restart po-tags-search all over.
   2582       (setq tags-loop-scan '(po-tags-loop-scan)
   2583 	    tags-loop-operate '(po-tags-loop-operate))
   2584       (tags-loop-continue t))
   2585     (select-window (get-buffer-window po-current-po-buffer)))
   2586   (if po-string-contents
   2587       (let ((window (selected-window))
   2588 	    (buffer po-string-buffer)
   2589 	    (start po-string-start)
   2590 	    (end po-string-end))
   2591 	;; Try to fit the string in the displayed part of its window.
   2592 	(select-window (get-buffer-window buffer))
   2593 	(goto-char start)
   2594 	(or (pos-visible-in-window-p start)
   2595 	    (recenter '(nil)))
   2596 	(if (pos-visible-in-window-p end)
   2597 	    (goto-char end)
   2598 	  (goto-char end)
   2599 	  (recenter -1))
   2600 	(select-window window)
   2601 	;; Highlight the string as found.
   2602 	(and po-highlighting
   2603 	     (po-highlight po-marking-overlay start end buffer)))))
   2604 
   2605 (defun po-tags-loop-scan ()
   2606   "Decide if the current buffer is still interesting for PO mode strings."
   2607   ;; We have little choice, here.  The major mode is needed to dispatch to the
   2608   ;; proper scanner, so we declare all files as interesting, to force Emacs
   2609   ;; tags module to revisit files fully.  po-tags-loop-operate sets point at
   2610   ;; end of buffer when it is done with a file.
   2611   (not (eobp)))
   2612 
   2613 (defun po-tags-loop-operate ()
   2614   "Find an acceptable tag in the current buffer, according to mode.
   2615 Disregard some simple strings which are most probably non-translatable."
   2616   (po-preset-string-functions)
   2617   (let ((continue t)
   2618 	data)
   2619     (while continue
   2620       (setq data (apply po-find-string-function po-current-po-keywords nil))
   2621       (if data
   2622 	  ;; Push the string just found into a work buffer for study.
   2623 	  (po-with-temp-buffer
   2624 	   (insert (nth 0 data))
   2625 	   (goto-char (point-min))
   2626 	   ;; Accept if at least three letters in a row.
   2627 	   (if (re-search-forward "[A-Za-z][A-Za-z][A-Za-z]" nil t)
   2628 	       (setq continue nil)
   2629 	     ;; Disregard if single letters or no letters at all.
   2630 	     (if (re-search-forward "[A-Za-z][A-Za-z]" nil t)
   2631 		 ;; Here, we have two letters in a row, but never more.
   2632 		 ;; Accept only if more letters than punctuations.
   2633 		 (let ((total (buffer-size)))
   2634 		   (goto-char (point-min))
   2635 		   (while (re-search-forward "[A-Za-z]+" nil t)
   2636 		     (replace-match "" t t))
   2637 		   (if (< (* 2 (buffer-size)) total)
   2638 		       (setq continue nil))))))
   2639 	;; No string left in this buffer.
   2640 	(setq continue nil)))
   2641     (if data
   2642 	;; Save information for marking functions.
   2643 	(let ((buffer (current-buffer)))
   2644 	  (save-excursion
   2645 	    (set-buffer po-current-po-buffer)
   2646 	    (setq po-string-contents (nth 0 data)
   2647 		  po-string-buffer buffer
   2648 		  po-string-start (nth 1 data)
   2649 		  po-string-end (nth 2 data))))
   2650       (goto-char (point-max)))
   2651     ;; If nothing was found, trigger scanning of next file.
   2652     (not data)))
   2653 
   2654 (defun po-mark-found-string (keyword)
   2655   "Mark last found string in program sources as translatable, using KEYWORD."
   2656   (if (not po-string-contents)
   2657     (error (_"No such string")))
   2658   (and po-highlighting (po-dehighlight po-marking-overlay))
   2659   (let ((contents po-string-contents)
   2660 	(buffer po-string-buffer)
   2661 	(start po-string-start)
   2662 	(end po-string-end)
   2663 	line string)
   2664     ;; Mark string in program sources.
   2665     (save-excursion
   2666       (set-buffer buffer)
   2667       (setq line (count-lines (point-min) start))
   2668       (apply po-mark-string-function start end keyword nil))
   2669     ;; Add PO file entry.
   2670     (let ((buffer-read-only po-read-only))
   2671       (goto-char (point-max))
   2672       (insert "\n" (format "#: %s:%d\n"
   2673 			   (buffer-file-name po-string-buffer)
   2674 			   line))
   2675       (save-excursion
   2676 	(insert (po-eval-requoted contents "msgid" nil) "msgstr \"\"\n"))
   2677       (setq po-untranslated-counter (1+ po-untranslated-counter))
   2678       (po-update-mode-line-string))
   2679     (setq po-string-contents nil)))
   2680 
   2681 (defun po-mark-translatable ()
   2682   "Mark last found string in program sources as translatable, using '_'."
   2683   (interactive)
   2684   (po-mark-found-string "_"))
   2685 
   2686 (defun po-select-mark-and-mark (arg)
   2687   "Mark last found string in program sources as translatable, ask for keywoard,
   2688 using completion.  With prefix argument, just ask the name of a preferred
   2689 keyword for subsequent commands, also added to possible completions."
   2690   (interactive "P")
   2691   (if arg
   2692       (let ((keyword (list (read-from-minibuffer (_"Keyword: ")))))
   2693 	(setq po-keywords (cons keyword (delete keyword po-keywords))))
   2694     (or po-string-contents (error (_"No such string")))
   2695     (let* ((default (car (car po-keywords)))
   2696 	   (keyword (completing-read (format (_"Mark with keywoard? [%s] ")
   2697 					     default)
   2698 				     po-keywords nil t )))
   2699       (if (string-equal keyword "") (setq keyword default))
   2700       (po-mark-found-string keyword))))
   2701 
   2702 ;;; Unknown mode specifics.
   2704 
   2705 (defun po-preset-string-functions ()
   2706   "Preset FIND-STRING-FUNCTION and MARK-STRING-FUNCTION according to mode.
   2707 These variables are locally set in source buffer only when not already bound."
   2708   (let ((pair (cond ((string-equal mode-name "AWK")
   2709 		     '(po-find-awk-string . po-mark-awk-string))
   2710 		    ((member mode-name '("C" "C++"))
   2711 		     '(po-find-c-string . po-mark-c-string))
   2712 		    ((string-equal mode-name "Emacs-Lisp")
   2713 		     '(po-find-emacs-lisp-string . po-mark-emacs-lisp-string))
   2714 		    ((string-equal mode-name "Python")
   2715 		     '(po-find-python-string . po-mark-python-string))
   2716 		    ((and (string-equal mode-name "Shell-script")
   2717 			  (string-equal mode-line-process "[bash]"))
   2718 		     '(po-find-bash-string . po-mark-bash-string))
   2719 		    (t '(po-find-unknown-string . po-mark-unknown-string)))))
   2720     (or (boundp 'po-find-string-function)
   2721 	(set (make-local-variable 'po-find-string-function) (car pair)))
   2722     (or (boundp 'po-mark-string-function)
   2723 	(set (make-local-variable 'po-mark-string-function) (cdr pair)))))
   2724 
   2725 (defun po-find-unknown-string (keywords)
   2726   "Dummy function to skip over a file, finding no string in it."
   2727   nil)
   2728 
   2729 (defun po-mark-unknown-string (start end keyword)
   2730   "Dummy function to mark a given string.  May not be called."
   2731   (error (_"Dummy function called")))
   2732 
   2733 ;;; Awk mode specifics.
   2735 
   2736 (defun po-find-awk-string (keywords)
   2737   "Find the next Awk string, excluding those marked by any of KEYWORDS.
   2738 Return (CONTENTS START END) for the found string, or nil if none found."
   2739   (let (start end)
   2740     (while (and (not start)
   2741 		(re-search-forward "[#/\"]" nil t))
   2742       (cond ((= (preceding-char) ?#)
   2743 	     ;; Disregard comments.
   2744 	     (or (search-forward "\n" nil t)
   2745 		 (goto-char (point-max))))
   2746 	    ((= (preceding-char) ?/)
   2747 	     ;; Skip regular expressions.
   2748 	     (while (not (= (following-char) ?/))
   2749 	       (skip-chars-forward "^/\\\\")
   2750 	       (if (= (following-char) ?\\) (forward-char 2)))
   2751 	     (forward-char 1))
   2752 	    ;; Else find the end of the string.
   2753 	    (t (setq start (1- (point)))
   2754 	       (while (not (= (following-char) ?\"))
   2755 		 (skip-chars-forward "^\"\\\\")
   2756 		 (if (= (following-char) ?\\) (forward-char 2)))
   2757 	       (forward-char 1)
   2758 	       (setq end (point))
   2759 	       ;; Check before string either for underline, or for keyword
   2760 	       ;; and opening parenthesis.
   2761 	       (save-excursion
   2762 		 (goto-char start)
   2763 		 (cond ((= (preceding-char) ?_)
   2764 			;; Disregard already marked strings.
   2765 			(setq start nil
   2766 			      end nil))
   2767 		       ((= (preceding-char) ?\()
   2768 			(backward-char 1)
   2769 			(let ((end-keyword (point)))
   2770 			  (skip-chars-backward "_A-Za-z0-9")
   2771 			  (if (member (list (po-buffer-substring
   2772 					     (point) end-keyword))
   2773 				      keywords)
   2774 			      ;; Disregard already marked strings.
   2775 			      (setq start nil
   2776 				    end nil)))))))))
   2777     (and start end
   2778 	 (list (po-extract-unquoted (current-buffer) start end) start end))))
   2779 
   2780 (defun po-mark-awk-string (start end keyword)
   2781   "Mark the Awk string, from START to END, with KEYWORD.
   2782 Leave point after marked string."
   2783   (if (string-equal keyword "_")
   2784       (progn
   2785 	(goto-char start)
   2786 	(insert "_")
   2787 	(goto-char (1+ end)))
   2788     (goto-char end)
   2789     (insert ")")
   2790     (save-excursion
   2791       (goto-char start)
   2792       (insert keyword "("))))
   2793 
   2794 ;;; Bash mode specifics.
   2796 
   2797 (defun po-find-bash-string (keywords)
   2798   "Find the next unmarked Bash string.  KEYWORDS are merely ignored.
   2799 Return (CONTENTS START END) for the found string, or nil if none found."
   2800   (let (start end)
   2801     (while (and (not start)
   2802 		(re-search-forward "[#'\"]" nil t))
   2803       (cond ((= (preceding-char) ?#)
   2804 	     ;; Disregard comments.
   2805 	     (or (search-forward "\n" nil t)
   2806 		 (goto-char (point-max))))
   2807 	    ((= (preceding-char) ?')
   2808 	     ;; Skip single quoted strings.
   2809 	     (while (not (= (following-char) ?'))
   2810 	       (skip-chars-forward "^'\\\\")
   2811 	       (if (= (following-char) ?\\) (forward-char 2)))
   2812 	     (forward-char 1))
   2813 	    ;; Else find the end of the double quoted string.
   2814 	    (t (setq start (1- (point)))
   2815 	       (while (not (= (following-char) ?\"))
   2816 		 (skip-chars-forward "^\"\\\\")
   2817 		 (if (= (following-char) ?\\) (forward-char 2)))
   2818 	       (forward-char 1)
   2819 	       (setq end (point))
   2820 	       ;; Check before string for dollar sign.
   2821 	       (save-excursion
   2822 		 (goto-char start)
   2823 		 (if (= (preceding-char) ?$)
   2824 		     ;; Disregard already marked strings.
   2825 		     (setq start nil
   2826 			   end nil))))))
   2827     (and start end
   2828 	 (list (po-extract-unquoted (current-buffer) start end) start end))))
   2829 
   2830 (defun po-mark-bash-string (start end keyword)
   2831   "Mark the Bash string, from START to END, with '$'.  KEYWORD is ignored.
   2832 Leave point after marked string."
   2833   (goto-char start)
   2834   (insert "$")
   2835   (goto-char (1+ end)))
   2836 
   2837 ;;; C or C++ mode specifics.
   2839 
   2840 ;;; A few long string cases (submitted by Ben Pfaff).
   2841 
   2842 ;; #define string "This is a long string " \
   2843 ;; "that is continued across several lines " \
   2844 ;; "in a macro in order to test \\ quoting\\" \
   2845 ;; "\\ with goofy strings.\\"
   2846 
   2847 ;; char *x = "This is just an ordinary string "
   2848 ;; "continued across several lines without needing "
   2849 ;; "to use \\ characters at end-of-line.";
   2850 
   2851 ;; char *y = "Here is a string continued across \
   2852 ;; several lines in the manner that was sanctioned \
   2853 ;; in K&R C compilers and still works today, \
   2854 ;; even though the method used above is more esthetic.";
   2855 
   2856 ;;; End of long string cases.
   2857 
   2858 (defun po-find-c-string (keywords)
   2859   "Find the next C string, excluding those marked by any of KEYWORDS.
   2860 Returns (CONTENTS START END) for the found string, or nil if none found."
   2861   (let (start end)
   2862     (while (and (not start)
   2863 		(re-search-forward "\\([\"']\\|/\\*\\|//\\)" nil t))
   2864       (cond ((= (preceding-char) ?*)
   2865 	     ;; Disregard comments.
   2866 	     (search-forward "*/"))
   2867 	    ((= (preceding-char) ?/)
   2868 	     ;; Disregard C++ comments.
   2869 	     (end-of-line)
   2870 	     (forward-char 1))
   2871 	    ((= (preceding-char) ?\')
   2872 	     ;; Disregard character constants.
   2873 	     (forward-char (if (= (following-char) ?\\) 3 2)))
   2874 	    ((save-excursion
   2875 	       (beginning-of-line)
   2876 	       (looking-at "^# *\\(include\\|line\\)"))
   2877 	     ;; Disregard lines being #include or #line directives.
   2878 	     (end-of-line))
   2879 	    ;; Else, find the end of the (possibly concatenated) string.
   2880 	    (t (setq start (1- (point))
   2881 		     end nil)
   2882 	       (while (not end)
   2883 		 (cond ((= (following-char) ?\")
   2884 			(if (looking-at "\"[ \t\n\\\\]*\"")
   2885 			    (goto-char (match-end 0))
   2886 			  (forward-char 1)
   2887 			  (setq end (point))))
   2888 		       ((= (following-char) ?\\) (forward-char 2))
   2889 		       (t (skip-chars-forward "^\"\\\\"))))
   2890 	       ;; Check before string for keyword and opening parenthesis.
   2891 	       (goto-char start)
   2892 	       (skip-chars-backward " \n\t")
   2893 	       (if (= (preceding-char) ?\()
   2894 		   (progn
   2895 		     (backward-char 1)
   2896 		     (skip-chars-backward " \n\t")
   2897 		     (let ((end-keyword (point)))
   2898 		       (skip-chars-backward "_A-Za-z0-9")
   2899 		       (if (member (list (po-buffer-substring (point)
   2900 							      end-keyword))
   2901 				   keywords)
   2902 			   ;; Disregard already marked strings.
   2903 			   (progn
   2904 			     (goto-char end)
   2905 			     (setq start nil
   2906 				   end nil))
   2907 			 ;; String found.  Prepare to resume search.
   2908 			 (goto-char end))))
   2909 		 ;; String found.  Prepare to resume search.
   2910 		 (goto-char end)))))
   2911     ;; Return the found string, if any.
   2912     (and start end
   2913 	 (list (po-extract-unquoted (current-buffer) start end) start end))))
   2914 
   2915 (defun po-mark-c-string (start end keyword)
   2916   "Mark the C string, from START to END, with KEYWORD.
   2917 Leave point after marked string."
   2918   (goto-char end)
   2919   (insert ")")
   2920   (save-excursion
   2921     (goto-char start)
   2922     (insert keyword)
   2923     (or (string-equal keyword "_") (insert " "))
   2924     (insert "(")))
   2925 
   2926 ;;; Emacs LISP mode specifics.
   2928 
   2929 (defun po-find-emacs-lisp-string (keywords)
   2930   "Find the next Emacs LISP string, excluding those marked by any of KEYWORDS.
   2931 Returns (CONTENTS START END) for the found string, or nil if none found."
   2932   (let (start end)
   2933     (while (and (not start)
   2934 		(re-search-forward "[;\"?]" nil t))
   2935       (cond ((= (preceding-char) ?\;)
   2936 	     ;; Disregard comments.
   2937 	     (search-forward "\n"))
   2938 	    ((= (preceding-char) ?\?)
   2939 	     ;; Disregard character constants.
   2940 	     (forward-char (if (= (following-char) ?\\) 2 1)))
   2941 	    ;; Else, find the end of the string.
   2942 	    (t (setq start (1- (point)))
   2943 	       (while (not (= (following-char) ?\"))
   2944 		 (skip-chars-forward "^\"\\\\")
   2945 		 (if (= (following-char) ?\\) (forward-char 2)))
   2946 	       (forward-char 1)
   2947 	       (setq end (point))
   2948 	       ;; Check before string for keyword and opening parenthesis.
   2949 	       (goto-char start)
   2950 	       (skip-chars-backward " \n\t")
   2951 	       (let ((end-keyword (point)))
   2952 		 (skip-chars-backward "-_A-Za-z0-9")
   2953 		 (if (and (= (preceding-char) ?\()
   2954 			  (member (list (po-buffer-substring (point)
   2955 							     end-keyword))
   2956 				  keywords))
   2957 		     ;; Disregard already marked strings.
   2958 		     (progn
   2959 		       (goto-char end)
   2960 		       (setq start nil
   2961 			     end nil)))))))
   2962     ;; Return the found string, if any.
   2963     (and start end
   2964 	 (list (po-extract-unquoted (current-buffer) start end) start end))))
   2965 
   2966 (defun po-mark-emacs-lisp-string (start end keyword)
   2967   "Mark the Emacs LISP string, from START to END, with KEYWORD.
   2968 Leave point after marked string."
   2969   (goto-char end)
   2970   (insert ")")
   2971   (save-excursion
   2972     (goto-char start)
   2973     (insert "(" keyword)
   2974     (or (string-equal keyword "_") (insert " "))))
   2975 
   2976 ;;; Python mode specifics.
   2978 
   2979 (defun po-find-python-string (keywords)
   2980   "Find the next Python string, excluding those marked by any of KEYWORDS.
   2981 Also disregard strings when preceded by an empty string of the other type.
   2982 Returns (CONTENTS START END) for the found string, or nil if none found."
   2983   (let (contents start end)
   2984     (while (and (not contents)
   2985 		(re-search-forward "[#\"']" nil t))
   2986       (forward-char -1)
   2987       (cond ((= (following-char) ?\#)
   2988 	     ;; Disregard comments.
   2989 	     (search-forward "\n"))
   2990 	    ((looking-at "\"\"'")
   2991 	     ;; Quintuple-quoted string
   2992 	     (po-skip-over-python-string))
   2993 	    ((looking-at "''\"")
   2994 	     ;; Quadruple-quoted string
   2995 	     (po-skip-over-python-string))
   2996 	    (t
   2997 	     ;; Simple-, double-, triple- or sextuple-quoted string.
   2998 	     (if (memq (preceding-char) '(?r ?R))
   2999 		 (forward-char -1))
   3000 	     (setq start (point)
   3001 		   contents (po-skip-over-python-string)
   3002 		   end (point))
   3003 	     (goto-char start)
   3004 	     (skip-chars-backward " \n\t")
   3005 	     (cond ((= (preceding-char) ?\[)
   3006 		    ;; Disregard a string used as a dictionary index.
   3007 		    (setq contents nil))
   3008 		   ((= (preceding-char) ?\()
   3009 		    ;; Isolate the keyword which precedes string.
   3010 		    (backward-char 1)
   3011 		    (skip-chars-backward " \n\t")
   3012 		    (let ((end-keyword (point)))
   3013 		      (skip-chars-backward "_A-Za-z0-9")
   3014 		      (if (member (list (po-buffer-substring (point)
   3015 							     end-keyword))
   3016 				  keywords)
   3017 			  ;; Disregard already marked strings.
   3018 			  (setq contents nil)))))
   3019 	     (goto-char end))))
   3020     ;; Return the found string, if any.
   3021     (and contents (list contents start end))))
   3022 
   3023 (defun po-skip-over-python-string ()
   3024   "Skip over a Python string, possibly made up of many concatenated parts.
   3025 Leave point after string.  Return unquoted overall string contents."
   3026   (let ((continue t)
   3027 	(contents "")
   3028 	raw start end resume)
   3029     (while continue
   3030       (skip-chars-forward " \t\n")	; whitespace
   3031       (cond ((= (following-char) ?#)	; comment
   3032 	     (setq start nil)
   3033 	     (search-forward "\n"))
   3034 	    ((looking-at "\\\n")	; escaped newline
   3035 	     (setq start nil)
   3036 	     (forward-char 2))
   3037 	    ((looking-at "[rR]?\"\"\"")	; sextuple-quoted string
   3038 	     (setq raw (memq (following-char) '(?r ?R))
   3039 		   start (match-end 0))
   3040 	     (goto-char start)
   3041 	     (search-forward "\"\"\"")
   3042 	     (setq resume (point)
   3043 		   end (- resume 3)))
   3044 	    ((looking-at "[rr]?'''")	; triple-quoted string
   3045 	     (setq raw (memq (following-char) '(?r ?R))
   3046 		   start (match-end 0))
   3047 	     (goto-char start)
   3048 	     (search-forward "'''")
   3049 	     (setq resume (point)
   3050 		   end (- resume 3)))
   3051 	    ((looking-at "[rR]?\"")	; double-quoted string
   3052 	     (setq raw (memq (following-char) '(?r ?R))
   3053 		   start (match-end 0))
   3054 	     (goto-char start)
   3055 	     (while (not (memq (following-char) '(0 ?\")))
   3056 	       (skip-chars-forward "^\"\\\\")
   3057 	       (if (= (following-char) ?\\) (forward-char 2)))
   3058 	     (if (eobp)
   3059 		 (setq contents nil
   3060 		       start nil)
   3061 	       (setq end (point))
   3062 	       (forward-char 1))
   3063 	     (setq resume (point)))
   3064 	    ((looking-at "[rR]?'")	; single-quoted string
   3065 	     (setq raw (memq (following-char) '(?r ?R))
   3066 		   start (match-end 0))
   3067 	     (goto-char start)
   3068 	     (while (not (memq (following-char) '(0 ?\')))
   3069 	       (skip-chars-forward "^'\\\\")
   3070 	       (if (= (following-char) ?\\) (forward-char 2)))
   3071 	     (if (eobp)
   3072 		 (setq contents nil
   3073 		       start nil)
   3074 	       (setq end (point))
   3075 	       (forward-char 1))
   3076 	     (setq resume (point)))
   3077 	    (t				; no string anymore
   3078 	     (setq start nil
   3079 		   continue nil)))
   3080       (if start
   3081 	  (setq contents (concat contents
   3082 				 (if raw
   3083 				     (buffer-substring start end)
   3084 				   (po-extract-part-unquoted (current-buffer)
   3085 							     start end))))))
   3086     (goto-char resume)
   3087     contents))
   3088 
   3089 (defun po-mark-python-string (start end keyword)
   3090   "Mark the Python string, from START to END, with KEYWORD.
   3091 If KEYWORD is '.', prefix the string with an empty string of the other type.
   3092 Leave point after marked string."
   3093   (cond ((string-equal keyword ".")
   3094 	 (goto-char end)
   3095 	 (save-excursion
   3096 	   (goto-char start)
   3097 	   (insert (cond ((= (following-char) ?\') "\"\"")
   3098 			 ((= (following-char) ?\") "''")
   3099 			 (t "??")))))
   3100 	(t (goto-char end)
   3101 	   (insert ")")
   3102 	   (save-excursion
   3103 	     (goto-char start)
   3104 	     (insert keyword "(")))))
   3105 
   3106 ;;; Miscellaneous features.
   3108 
   3109 (defun po-help ()
   3110   "Provide an help window for PO mode."
   3111   (interactive)
   3112   (po-with-temp-buffer
   3113    (insert po-help-display-string)
   3114    (goto-char (point-min))
   3115    (save-window-excursion
   3116      (switch-to-buffer (current-buffer))
   3117      (delete-other-windows)
   3118      (message (_"Type any character to continue"))
   3119      (po-read-event))))
   3120 
   3121 (defun po-undo ()
   3122   "Undo the last change to the PO file."
   3123   (interactive)
   3124   (let ((buffer-read-only po-read-only))
   3125     (undo))
   3126   (po-compute-counters nil))
   3127 
   3128 (defun po-statistics ()
   3129   "Say how many entries in each category, and the current position."
   3130   (interactive)
   3131   (po-compute-counters t))
   3132 
   3133 (defun po-validate ()
   3134   "Use 'msgfmt' for validating the current PO file contents."
   3135   (interactive)
   3136   ; The 'compile' subsystem is autoloaded through a call to (compile ...).
   3137   ; We need to initialize it outside of any binding. Without this statement,
   3138   ; all defcustoms and defvars of compile.el would be undone when the let*
   3139   ; terminates.
   3140   (require 'compile)
   3141   (let* ((dev-null
   3142 	  (cond ((boundp 'null-device) null-device) ; since Emacs 20.3
   3143 		((memq system-type '(windows-nt windows-95)) "NUL")
   3144 		(t "/dev/null")))
   3145 	 (compilation-buffer-name-function
   3146 	  (function (lambda (mode-name)
   3147 		      (concat "*" mode-name " validation*"))))
   3148 	 (compile-command (concat po-msgfmt-program
   3149                                  " --statistics -c -v -o " dev-null " "
   3150                                  buffer-file-name)))
   3151     (po-msgfmt-version-check)
   3152     (compile compile-command)))
   3153 
   3154 (defvar po-msgfmt-version-checked nil)
   3155 (defun po-msgfmt-version-check ()
   3156   "'msgfmt' from GNU gettext 0.10.36 or greater is required."
   3157   (po-with-temp-buffer
   3158     (or
   3159      ;; Don't bother checking again.
   3160      po-msgfmt-version-checked
   3161 
   3162      (and
   3163       ;; Make sure 'msgfmt' is available.
   3164       (condition-case nil
   3165           (call-process po-msgfmt-program
   3166 			nil t nil "--verbose" "--version")
   3167 	(file-error nil))
   3168 
   3169       ;; Make sure there's a version number in the output:
   3170       ;; 0.11 or 0.10.36 or 0.11-pre1
   3171       (progn (goto-char (point-min))
   3172              (or (looking-at ".* \\([0-9]+\\)\\.\\([0-9]+\\)$")
   3173                  (looking-at ".* \\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)$")
   3174 		 (looking-at ".* \\([0-9]+\\)\\.\\([0-9]+\\)[-_A-Za-z0-9]+$")))
   3175 
   3176       ;; Make sure the version is recent enough.
   3177       (>= (string-to-number
   3178 	   (format "%d%03d%03d"
   3179 		   (string-to-number (match-string 1))
   3180 		   (string-to-number (match-string 2))
   3181 		   (string-to-number (or (match-string 3) "0"))))
   3182 	  010036)
   3183 
   3184       ;; Remember the outcome.
   3185       (setq po-msgfmt-version-checked t))
   3186 
   3187      (error (_"'msgfmt' from GNU gettext 0.10.36 or greater is required")))))
   3188 
   3189 (defun po-guess-archive-name ()
   3190   "Return the ideal file name for this PO file in the central archives."
   3191   (let ((filename (file-name-nondirectory buffer-file-name))
   3192 	start-of-header end-of-header package version team)
   3193     (save-excursion
   3194       ;; Find the PO file header entry.
   3195       (goto-char (point-min))
   3196       (re-search-forward po-any-msgstr-regexp)
   3197       (setq start-of-header (match-beginning 0)
   3198 	    end-of-header (match-end 0))
   3199       ;; Get the package and version.
   3200       (goto-char start-of-header)
   3201       (if (re-search-forward "\n\
   3202 \"Project-Id-Version: \\(GNU \\|Free \\)?\\([^\n ]+\\) \\([^\n ]+\\)\\\\n\"$"
   3203 	   end-of-header t)
   3204 	  (setq package (po-match-string 2)
   3205 		version (po-match-string 3)))
   3206       (if (or (not package) (string-equal package "PACKAGE")
   3207 	      (not version) (string-equal version "VERSION"))
   3208 	  (error (_"Project-Id-Version field does not have a proper value")))
   3209       ;; File name version and Project-Id-Version must match
   3210       (cond (;; A `filename' w/o package and version info at all
   3211 	     (string-match "^[^\\.]*\\.po\\'" filename))
   3212 	    (;; TP Robot compatible `filename': PACKAGE-VERSION.LL.po
   3213 	     (string-match (concat (regexp-quote package)
   3214 				   "-\\(.*\\)\\.[^\\.]*\\.po\\'") filename)
   3215 	     (if (not (equal version (po-match-string 1 filename)))
   3216 		 (error (_"\
   3217 Version mismatch: file name: %s; header: %s.\n\
   3218 Adjust Project-Id-Version field to match file name and try again")
   3219 			(po-match-string 1 filename) version))))
   3220       ;; Get the team.
   3221       (if (stringp po-team-name-to-code)
   3222 	  (setq team po-team-name-to-code)
   3223 	(goto-char start-of-header)
   3224 	(if (re-search-forward "\n\
   3225 \"Language-Team: \\([^ ].*[^ ]\\) <.+@.+>\\\\n\"$"
   3226 			       end-of-header t)
   3227 	    (let ((name (po-match-string 1)))
   3228 	      (if name
   3229 		  (let ((pair (assoc name po-team-name-to-code)))
   3230 		    (if pair
   3231 			(setq team (cdr pair))
   3232 		      (setq team (read-string (format "\
   3233 Team name '%s' unknown.  What is the team code? "
   3234 						      name)))))))))
   3235       (if (or (not team) (string-equal team "LL"))
   3236 	  (error (_"Language-Team field does not have a proper value")))
   3237       ;; Compose the name.
   3238       (concat package "-" version "." team ".po"))))
   3239 
   3240 (defun po-guess-team-address ()
   3241   "Return the team address related to this PO file."
   3242   (let (team)
   3243     (save-excursion
   3244       (goto-char (point-min))
   3245       (re-search-forward po-any-msgstr-regexp)
   3246       (goto-char (match-beginning 0))
   3247       (if (re-search-forward
   3248 	   "\n\"Language-Team: +\\(.*<\\(.*\\)@.*>\\)\\\\n\"$"
   3249 	   (match-end 0) t)
   3250 	  (setq team (po-match-string 2)))
   3251       (if (or (not team) (string-equal team "LL"))
   3252 	  (error (_"Language-Team field does not have a proper value")))
   3253       (po-match-string 1))))
   3254 
   3255 (defun po-send-mail ()
   3256   "Start composing a letter, possibly including the current PO file."
   3257   (interactive)
   3258   (let* ((team-flag (y-or-n-p
   3259 		     (_"\
   3260 Write to your team?  ('n' if writing to the Translation Project robot) ")))
   3261 	 (address (if team-flag
   3262 		      (po-guess-team-address)
   3263 		    po-translation-project-address)))
   3264     (if (not (y-or-n-p (_"Include current PO file in mail? ")))
   3265 	(apply po-compose-mail-function address
   3266 	       (read-string (_"Subject? ")) nil)
   3267       (if (buffer-modified-p)
   3268 	  (error (_"The file is not even saved, you did not validate it.")))
   3269       (if (and (y-or-n-p (_"You validated ('V') this file, didn't you? "))
   3270 	       (or (zerop po-untranslated-counter)
   3271 		   (y-or-n-p
   3272 		    (format (_"%d entries are untranslated, include anyway? ")
   3273 			    po-untranslated-counter)))
   3274 	       (or (zerop po-fuzzy-counter)
   3275 		   (y-or-n-p
   3276 		    (format (_"%d entries are still fuzzy, include anyway? ")
   3277 			    po-fuzzy-counter)))
   3278 	       (or (zerop po-obsolete-counter)
   3279 		   (y-or-n-p
   3280 		    (format (_"%d entries are obsolete, include anyway? ")
   3281 			    po-obsolete-counter))))
   3282 	  (let ((buffer (current-buffer))
   3283 		(name (po-guess-archive-name))
   3284 		(transient-mark-mode nil)
   3285 		(coding-system-for-read buffer-file-coding-system)
   3286 		(coding-system-for-write buffer-file-coding-system))
   3287 	    (apply po-compose-mail-function address
   3288 		   (if team-flag
   3289 		       (read-string (_"Subject? "))
   3290 		     (format "%s %s" po-translation-project-mail-label name))
   3291 		   nil)
   3292 	    (goto-char (point-min))
   3293 	    (re-search-forward
   3294 	     (concat "^" (regexp-quote mail-header-separator) "\n"))
   3295 	    (save-excursion
   3296 	      (insert-buffer buffer)
   3297 	      (shell-command-on-region
   3298 	       (region-beginning) (region-end)
   3299 	       (concat po-gzip-uuencode-command " " name ".gz") t))))))
   3300   (message ""))
   3301 
   3302 (defun po-confirm-and-quit ()
   3303   "Confirm if quit should be attempted and then, do it.
   3304 This is a failsafe.  Confirmation is asked if only the real quit would not."
   3305   (interactive)
   3306   (if (po-check-all-pending-edits)
   3307       (progn
   3308 	(if (or (buffer-modified-p)
   3309 		(> po-untranslated-counter 0)
   3310 		(> po-fuzzy-counter 0)
   3311 		(> po-obsolete-counter 0)
   3312 		(y-or-n-p (_"Really quit editing this PO file? ")))
   3313 	    (po-quit))
   3314 	(message ""))))
   3315 
   3316 (defun po-quit ()
   3317   "Save the PO file and kill buffer.
   3318 However, offer validation if appropriate and ask confirmation if untranslated
   3319 strings remain."
   3320   (interactive)
   3321   (if (po-check-all-pending-edits)
   3322       (let ((quit t))
   3323 	;; Offer validation of newly modified entries.
   3324 	(if (and (buffer-modified-p)
   3325 		 (not (y-or-n-p
   3326 		       (_"File was modified; skip validation step? "))))
   3327 	    (progn
   3328 	      (message "")
   3329 	      (po-validate)
   3330 	      ;; If we knew that the validation was all successful, we should
   3331 	      ;; just quit.  But since we do not know yet, as the validation
   3332 	      ;; might be asynchronous with PO mode commands, the safest is to
   3333 	      ;; stay within PO mode, even if this implies that another
   3334 	      ;; 'po-quit' command will be later required to exit for true.
   3335 	      (setq quit nil)))
   3336 	;; Offer to work on untranslated entries.
   3337 	(if (and quit
   3338 		 (or (> po-untranslated-counter 0)
   3339 		     (> po-fuzzy-counter 0)
   3340 		     (> po-obsolete-counter 0))
   3341 		 (not (y-or-n-p
   3342 		       (_"Unprocessed entries remain; quit anyway? "))))
   3343 	    (progn
   3344 	      (setq quit nil)
   3345 	      (po-auto-select-entry)))
   3346 	;; Clear message area.
   3347 	(message "")
   3348 	;; Or else, kill buffers and quit for true.
   3349 	(if quit
   3350 	    (progn
   3351 	      (save-buffer)
   3352 	      (kill-buffer (current-buffer)))))))
   3353 
   3354 (provide 'po-mode)
   3355 
   3356 ;;; po-mode.el ends here
   3357