changeset 40: |
2d74d85d7031 |
author: |
Richard Westhaver <ellis@rwest.io> |
date: |
Wed, 05 Jun 2024 23:31:48 +0000 |
permissions: |
-rw-r--r-- |
description: |
add back official mercurial emacs packages |
1 ;;; mq.el --- Emacs support for Mercurial Queues 3 ;; Copyright (C) 2006 Bryan O'Sullivan 5 ;; Author: Bryan O'Sullivan <bos@serpentine.com> 7 ;; mq.el is free software; you can redistribute it and/or modify it 8 ;; under the terms of the GNU General Public License version 2 or any 11 ;; mq.el is distributed in the hope that it will be useful, but 12 ;; WITHOUT ANY WARRANTY; without even the implied warranty of 13 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 ;; General Public License for more details. 16 ;; You should have received a copy of the GNU General Public License 17 ;; along with mq.el, GNU Emacs, or XEmacs; see the file COPYING (`C-h 18 ;; C-l'). If not, see <http://www.gnu.org/licenses/>. 20 (eval-when-compile (require 'cl)) 24 (defcustom mq-mode-hook nil 25 "Hook run when a buffer enters mq-mode." 29 (defcustom mq-global-prefix "\C-cq" 30 "The global prefix for Mercurial Queues keymap bindings." 34 (defcustom mq-edit-mode-hook nil 35 "Hook run after a buffer is populated to edit a patch description." 39 (defcustom mq-edit-finish-hook nil 40 "Hook run before a patch description is finished up with." 44 (defcustom mq-signoff-address nil 45 "Address with which to sign off on a patch." 50 ;;; Internal variables. 53 "Is this file managed by MQ?") 54 (make-variable-buffer-local 'mq-mode) 55 (put 'mq-mode 'permanent-local t) 57 (defvar mq-patch-history nil) 59 (defvar mq-top-patch '(nil)) 61 (defvar mq-prev-buffer nil) 62 (make-variable-buffer-local 'mq-prev-buffer) 63 (put 'mq-prev-buffer 'permanent-local t) 66 (make-variable-buffer-local 'mq-top) 67 (put 'mq-top 'permanent-local t) 72 (let ((map (make-sparse-keymap))) 73 (define-key map "." 'mq-push) 74 (define-key map ">" 'mq-push-all) 75 (define-key map "," 'mq-pop) 76 (define-key map "<" 'mq-pop-all) 77 (define-key map "=" 'mq-diff) 78 (define-key map "r" 'mq-refresh) 79 (define-key map "e" 'mq-refresh-edit) 80 (define-key map "i" 'mq-new) 81 (define-key map "n" 'mq-next) 82 (define-key map "o" 'mq-signoff) 83 (define-key map "p" 'mq-previous) 84 (define-key map "s" 'mq-edit-series) 85 (define-key map "t" 'mq-top) 88 (global-set-key mq-global-prefix mq-global-map) 90 (add-minor-mode 'mq-mode 'mq-mode) 93 ;;; Refresh edit mode keymap. 95 (defvar mq-edit-mode-map 96 (let ((map (make-sparse-keymap))) 97 (define-key map "\C-c\C-c" 'mq-edit-finish) 98 (define-key map "\C-c\C-k" 'mq-edit-kill) 99 (define-key map "\C-c\C-s" 'mq-signoff) 103 ;;; Helper functions. 105 (defun mq-read-patch-name (&optional source prompt force) 106 "Read a patch name to use with a command. 107 May return nil, meaning \"use the default\"." 108 (let ((patches (split-string 109 (hg-chomp (hg-run0 (or source "qseries"))) "\n"))) 111 (completing-read (format "Patch%s: " (or prompt "")) 112 (mapcar (lambda (x) (cons x x)) patches) 116 'mq-patch-history)))) 118 (defun mq-refresh-buffers (root) 120 (dolist (buf (hg-buffers-visiting-repo root)) 121 (when (not (verify-visited-file-modtime buf)) 123 (let ((ctx (hg-buffer-context))) 124 (message "Refreshing %s..." (buffer-name)) 125 (revert-buffer t t t) 126 (hg-restore-context ctx) 127 (message "Refreshing %s...done" (buffer-name)))))) 128 (hg-update-mode-lines root) 129 (mq-update-mode-lines root)) 131 (defun mq-last-line () 132 (goto-char (point-max)) 134 (when (looking-at "^$") 138 (let ((line (buffer-substring bol (point)))) 139 (when (> (length line) 0) 142 (defun mq-push (&optional patch) 143 "Push patches until PATCH is reached. 144 If PATCH is nil, push at most one patch." 145 (interactive (list (mq-read-patch-name "qunapplied" " to push" 146 current-prefix-arg))) 147 (let ((root (hg-root)) 148 (prev-buf (current-buffer)) 151 (error "Cannot push outside a repository!")) 152 (hg-sync-buffers root) 153 (let ((buf-name (format "MQ: Push %s" (or patch "next patch")))) 154 (kill-buffer (get-buffer-create buf-name)) 155 (split-window-vertically) 157 (switch-to-buffer (get-buffer-create buf-name)) 159 (message "Pushing...") 160 (setq ok (= 0 (apply 'call-process (hg-binary) nil t t "qpush" 161 (if patch (list patch)))) 162 last-line (mq-last-line)) 163 (let ((lines (count-lines (point-min) (point-max)))) 165 (and (equal lines 2) (string-match "Now at:" last-line))) 167 (kill-buffer (current-buffer)) 169 (hg-view-mode prev-buf)))) 170 (mq-refresh-buffers root) 174 (message "Pushing... %s" last-line) 175 (error "Pushing... %s" last-line))))) 177 (defun mq-push-all () 178 "Push patches until all are applied." 182 (defun mq-pop (&optional patch) 183 "Pop patches until PATCH is reached. 184 If PATCH is nil, pop at most one patch." 185 (interactive (list (mq-read-patch-name "qapplied" " to pop to" 186 current-prefix-arg))) 187 (let ((root (hg-root)) 190 (error "Cannot pop outside a repository!")) 191 (hg-sync-buffers root) 192 (set-buffer (generate-new-buffer "qpop")) 194 (message "Popping...") 195 (setq ok (= 0 (apply 'call-process (hg-binary) nil t t "qpop" 196 (if patch (list patch)))) 197 last-line (mq-last-line)) 198 (kill-buffer (current-buffer)) 199 (mq-refresh-buffers root) 203 (message "Popping... %s" last-line) 204 (error "Popping... %s" last-line))))) 207 "Push patches until none are applied." 211 (defun mq-refresh-internal (root &rest args) 212 (hg-sync-buffers root) 213 (let ((patch (mq-patch-info "qtop"))) 214 (message "Refreshing %s..." patch) 215 (let ((ret (apply 'hg-run "qrefresh" args))) 216 (if (equal (car ret) 0) 217 (message "Refreshing %s... done." patch) 218 (error "Refreshing %s... %s" patch (hg-chomp (cdr ret))))))) 220 (defun mq-refresh (&optional git) 221 "Refresh the topmost applied patch. 222 With a prefix argument, generate a git-compatible patch." 224 (let ((root (hg-root))) 226 (error "Cannot refresh outside of a repository!")) 227 (apply 'mq-refresh-internal root (if git '("--git"))))) 229 (defun mq-patch-info (cmd &optional msg) 230 (let* ((ret (hg-run cmd)) 231 (info (hg-chomp (cdr ret)))) 232 (if (equal (car ret) 0) 234 (message "%s patch: %s" msg info) 239 "Print the name of the topmost applied patch." 241 (mq-patch-info "qtop" "Top")) 244 "Print the name of the next patch to be pushed." 246 (mq-patch-info "qnext" "Next")) 248 (defun mq-previous () 249 "Print the name of the first patch below the topmost applied patch. 250 This would become the active patch if popped to." 252 (mq-patch-info "qprev" "Previous")) 254 (defun mq-edit-finish () 255 "Finish editing the description of this patch, and refresh the patch." 257 (unless (equal (mq-patch-info "qtop") mq-top) 258 (error "Topmost patch has changed!")) 259 (hg-sync-buffers hg-root) 260 (run-hooks 'mq-edit-finish-hook) 261 (mq-refresh-internal hg-root "-m" (buffer-substring (point-min) (point-max))) 262 (let ((buf mq-prev-buffer)) 264 (switch-to-buffer buf))) 266 (defun mq-edit-kill () 267 "Kill the edit currently being prepared." 269 (when (or (not (buffer-modified-p)) (y-or-n-p "Really kill this edit? ")) 270 (let ((buf mq-prev-buffer)) 272 (switch-to-buffer buf)))) 274 (defun mq-get-top (root) 275 (let ((entry (assoc root mq-top-patch))) 279 (defun mq-set-top (root patch) 280 (let ((entry (assoc root mq-top-patch))) 284 (setq mq-top-patch (delq entry mq-top-patch))) 285 (setq mq-top-patch (cons (cons root patch) mq-top-patch))))) 287 (defun mq-update-mode-lines (root) 288 (let ((cwd default-directory)) 291 (mq-set-top root (mq-patch-info "qtop")) 292 (error (mq-set-top root nil))) 294 (let ((patch (mq-get-top root))) 296 (dolist (buf (hg-buffers-visiting-repo root)) 299 (setq mq-mode (or (and patch (concat " MQ:" patch)) " MQ"))))))) 301 (defun mq-mode (&optional arg) 302 "Minor mode for Mercurial repositories with an MQ patch queue" 305 (setq mq-mode (if (null arg) (not mq-mode) 307 (mq-update-mode-lines (hg-root)))) 308 (run-hooks 'mq-mode-hook)) 310 (defun mq-edit-mode () 311 "Mode for editing the description of a patch. 315 \\[mq-edit-finish] use this description 316 \\[mq-edit-kill] abandon this description" 318 (use-local-map mq-edit-mode-map) 319 (set-syntax-table text-mode-syntax-table) 320 (setq local-abbrev-table text-mode-abbrev-table 321 major-mode 'mq-edit-mode 323 (set-buffer-modified-p nil) 324 (setq buffer-undo-list nil) 325 (run-hooks 'text-mode-hook 'mq-edit-mode-hook)) 327 (defun mq-refresh-edit () 328 "Refresh the topmost applied patch, editing the patch description." 330 (while mq-prev-buffer 331 (set-buffer mq-prev-buffer)) 332 (let ((root (hg-root)) 333 (prev-buffer (current-buffer)) 334 (patch (mq-patch-info "qtop"))) 335 (hg-sync-buffers root) 336 (let ((buf-name (format "*MQ: Edit description of %s*" patch))) 337 (switch-to-buffer (get-buffer-create buf-name)) 338 (when (= (point-min) (point-max)) 339 (set (make-local-variable 'hg-root) root) 340 (set (make-local-variable 'mq-top) patch) 341 (setq mq-prev-buffer prev-buffer) 342 (insert (hg-run0 "qheader")) 343 (goto-char (point-min))) 346 (message "Type `C-c C-c' to finish editing and refresh the patch.")) 349 "Create a new empty patch named NAME. 350 The patch is applied on top of the current topmost patch. 351 With a prefix argument, forcibly create the patch even if the working 352 directory is modified." 353 (interactive (list (mq-read-patch-name "qseries" " to create" t))) 354 (message "Creating patch...") 355 (let ((ret (if current-prefix-arg 356 (hg-run "qnew" "-f" name) 357 (hg-run "qnew" name)))) 358 (if (equal (car ret) 0) 360 (hg-update-mode-lines (buffer-file-name)) 361 (message "Creating patch... done.")) 362 (error "Creating patch... %s" (hg-chomp (cdr ret)))))) 364 (defun mq-edit-series () 365 "Edit the MQ series file directly." 367 (let ((root (hg-root))) 369 (error "Not in an MQ repository!")) 370 (find-file (concat root ".hg/patches/series")))) 372 (defun mq-diff (&optional git) 373 "Display a diff of the topmost applied patch. 374 With a prefix argument, display a git-compatible diff." 376 (hg-view-output ((format "MQ: Diff of %s" (mq-patch-info "qtop"))) 378 (call-process (hg-binary) nil t nil "qdiff" "--git") 379 (call-process (hg-binary) nil t nil "qdiff")) 381 (font-lock-fontify-buffer))) 384 "Sign off on the current patch, in the style used by the Linux kernel. 385 If the variable mq-signoff-address is non-nil, it will be used, otherwise 386 the value of the ui.username item from your hgrc will be used." 388 (let ((was-editing (eq major-mode 'mq-edit-mode)) 393 (let* ((user (or mq-signoff-address 394 (hg-run0 "debugconfig" "ui.username"))) 395 (signoff (concat "Signed-off-by: " user))) 396 (if (search-forward signoff nil t) 397 (message "You have already signed off on this patch.") 398 (goto-char (point-max)) 399 (let ((case-fold-search t)) 400 (if (re-search-backward "^Signed-off-by: " nil t) 404 (message "%s" signoff) 416 ;;; prompt-to-byte-compile: nil