Development/Howto/RPM Advanced/Emacs

From Mandriva Community Wiki

Jump to: navigation, search
Customizing Emacs RPM spec mode

Contents


These are some functions to customize the rpm-spec-mode under Emacs.

[edit] FFAP aware

This is how to make FFAP (find-file-at-point) aware of patches.

(require 'ffap)
(defun my-rpm-ffap (name)
  (ffap-locate-file name '("" ".gz" ".bz2") 
               '("./" "../SOURCES")))              
(add-to-list 'ffap-alist 
          '(rpm-spec-mode . my-rpm-ffap))

To use it make ffap bound to a key for example :

(global-set-key (read-kbd-macro "C-x f") 'find-file-at-point)

go to a Patch: package.patch.bz2 and press C-x f it will find the patch from ../SOURCES

[edit] Insert Patch

This function inserts a patch incrementally into the spec mode completing with the patches available in ../SOURCES/

(defun my-rpm-insert-patch ()
  (interactive)
  (goto-char (point-min))
  (let* ((file 
     (completing-read 
      "Patch: "
      (mapcar (function (lambda (rule) (list rule)))
            (directory-files "../SOURCES/" nil "^\\([_-0-9a-zA-Z]+\\).''\\.patch.'''"))) )
       (max (search-forward-regexp rpm-section-regexp))
       (count 0)
       )
   (goto-char (point-min))
   (while (search-forward-regexp "^Patch?\\([0-9]+\\)?" max t) 
     (if (> (string-to-int (match-string 1)) count)
        (setq count (string-to-int (match-string 1)))
      )
     )
   (if (eq count 0) (while (search-forward-regexp "^Source?\\([0-9]+\\)?" max t)()))   
   (setq count (1+ count))
   (end-of-line)
   (insert (format "\n%s%d%s%s" "Patch" count ": " file))
   (goto-char (point-min))
   (if (search-forward-regexp "^%patch?\\([0-9]+\\)?" nil t)
      (progn
        (beginning-of-line)
        (while (search-forward-regexp "^%patch?\\([0-9]+\\)?" nil t) ())
        )
     (search-forward-regexp "^%setup" nil t))

   (end-of-line)
   (insert (format "\n%s%d%s" "%patch" count " -p1 "))
   (let ((name (rpm-spec-field-value "name" nil))
        (version (rpm-spec-field-value "version" nil))
        (string)
        )
     (cond ((string-match 
           (concat "^" (regexp-quote (format "%s-%s-" name version))
                 "\\([^.]*\\)\\(\\.patch\\.bz2\\)") file)
          (setq string (format "%s%s" "-b ." 
                          (substring file (match-beginning 1) (match-end 1)))))
         ((string-match 
           (concat "^" (regexp-quote (format "%s-" name version))
                 ".''[0-9]+-" "\\([^.]'''\\)\\(\\.patch\\.bz2\\)") file)
          (setq string 
               (format "%s%s" "-b ." 
                     (substring file (match-beginning 1) (match-end 1)))))
         )
     (if string
        (insert string))
     )))

bind this function to your rpm-spec-mode hook or if you don't understand what it does mean use this code :

(defun my-rpm-spec-mode-hook ()
     (local-set-key '[(control c)(control p)] 'my-rpm-insert-patch)
)
(add-hook 'rpm-spec-mode-hook 'my-rpm-spec-mode-hook)

pressing control-c control-p will do the magic.

[edit] Automatic ChangeLog

This is a function that automatically adds a generated ChangeLog, customize it the way you want:

(defun my-rpm-changelog-increment-version ()
  (interactive)
  (goto-char (point-min))
  (let* ((max (search-forward-regexp rpm-section-regexp))
       (version (rpm-spec-field-value "Version" max)))
   (rpm-add-change-log-entry (concat "Upgrade version to " version))
   )
  )
Personal tools
Ad (via La Vignette)
Looking for a job?