add lisp/bandali-po.el with customizations for po-mode
[~bandali/configs] / .emacs.d / lisp / bandali-po.el
1 ;;; bandali-po.el --- bandali's po-mode setup -*- lexical-binding: t; -*-
2
3 ;; Copyright (C) 2021 Amin Bandali
4
5 ;; Author: Amin Bandali <bandali@gnu.org>
6 ;; Keywords: i18n gettext
7
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
12
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
17
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with this program. If not, see <https://www.gnu.org/licenses/>.
20
21 ;;; Commentary:
22
23 ;; My po-mode setup for editing PO translation files in GNU Emacs.
24
25 ;;; Code:
26
27 (with-eval-after-load 'po-mode
28 ;; based on the `po-wrap' function from the GNUN manual:
29 ;; https://www.gnu.org/s/trans-coord/manual/gnun/html_node/Wrapping-Long-Lines.html
30 (defun b/po-wrap ()
31 "Run the current `po-mode' buffer through `msgcat' to wrap all
32 lines."
33 (interactive)
34 (when (eq major-mode 'po-mode)
35 (let ((tmp-file (make-temp-file "po-wrap."))
36 (tmp-buffer (generate-new-buffer "*temp*")))
37 (unwind-protect
38 (progn
39 (write-region (point-min) (point-max) tmp-file nil 1)
40 (if (zerop
41 (call-process "msgcat" nil tmp-buffer t
42 (shell-quote-argument tmp-file)))
43 (let ((saved (point))
44 (inhibit-read-only t))
45 (delete-region (point-min) (point-max))
46 (insert-buffer-substring tmp-buffer)
47 (goto-char (min saved (point-max))))
48 (with-current-buffer tmp-buffer
49 (error (buffer-string)))))
50 (kill-buffer tmp-buffer)
51 (delete-file tmp-file)))))
52
53 (add-hook 'po-mode-hook (lambda nil (run-with-timer 0.1 nil 'View-exit)))
54 (define-key po-mode-map (kbd "M-q") #'b/po-wrap))
55
56 (autoload #'po-mode "po-mode"
57 "Major mode for editing PO translation files" t)
58 (add-to-list 'auto-mode-alist '("\\.po\\'\\|\\.po\\." . po-mode))
59
60 (provide 'bandali-po)
61 ;;; bandali-po.el ends here