Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(119)

Side by Side Diff: misc/emacs/go-mode.el

Issue 4276059: code review 4276059: misc/emacs: gofmt: don't clobber the current buffer on ... (Closed)
Patch Set: diff -r ae2851fd0361 http://go.googlecode.com/hg/ Created 13 years, 12 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | misc/emacs/go-mode-load.el » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 ;;; go-mode.el --- Major mode for the Go programming language 1 ;;; go-mode.el --- Major mode for the Go programming language
2 2
3 ;;; Commentary: 3 ;;; Commentary:
4 4
5 ;; For installation instructions, see go-mode-load.el 5 ;; For installation instructions, see go-mode-load.el
6 6
7 ;;; To do: 7 ;;; To do:
8 8
9 ;; * Indentation is *almost* identical to gofmt 9 ;; * Indentation is *almost* identical to gofmt
10 ;; ** We think struct literal keys are labels and outdent them 10 ;; ** We think struct literal keys are labels and outdent them
(...skipping 482 matching lines...) Expand 10 before | Expand all | Expand 10 after
493 "Reload go-mode.el and put the current buffer into Go mode. 493 "Reload go-mode.el and put the current buffer into Go mode.
494 Useful for development work." 494 Useful for development work."
495 495
496 (interactive) 496 (interactive)
497 (unload-feature 'go-mode) 497 (unload-feature 'go-mode)
498 (require 'go-mode) 498 (require 'go-mode)
499 (go-mode)) 499 (go-mode))
500 500
501 (provide 'go-mode) 501 (provide 'go-mode)
502 502
503 (defun gofmt () 503 (defun gofmt-buffer (outbuf replace errbuf)
504 "Pipe the current buffer through the external tool `gofmt`."
505 ··
506 (interactive)
507 ;; for some reason save-excursion isn't working 504 ;; for some reason save-excursion isn't working
508 ;; probably because shell-command-on-region deletes the contents of the 505 ;; probably because shell-command-on-region deletes the contents of the
509 ;; region before filling in the new values 506 ;; region before filling in the new values
510 ;; so we will save the point/mark by hand 507 ;; so we will save the point/mark by hand
511 ;; similarly we can't use push-mark/pop-mark 508 ;; similarly we can't use push-mark/pop-mark
512 (let ((old-mark (mark t)) (old-point (point))) 509 (let ((old-mark (mark t)) (old-point (point)))
513 (save-restriction 510 (save-restriction
514 (let (deactivate-mark) 511 (let (deactivate-mark)
515 (widen) 512 (widen)
516 (shell-command-on-region (point-min) (point-max) "gofmt" t t shell-comma nd-default-error-buffer))) 513 (setq exit-status (shell-command-on-region (point-min) (point-max) "gofm t"
514 » » » » » » outbuf replace errbuf))))
517 (goto-char (min old-point (point-max))) 515 (goto-char (min old-point (point-max)))
518 (if old-mark (set-mark (min old-mark (point-max)))))) 516 (if old-mark (set-mark (min old-mark (point-max)))))
517 exit-status)
518
519 (defun gofmt ()
520 "Pipe the current buffer through the external tool `gofmt`."
521 ··
522 (interactive)
523 (gofmt-buffer t t shell-command-default-error-buffer))
524
525 (defun get-empty-buffer (name)
526 "Finds or creates the buffer named name, erases it, and returns it."
527
528 (let ((buf (get-buffer-create name)))
529 (save-current-buffer
530 (set-buffer buf)
531 (erase-buffer))
532 buf))
533
534 ;;;###autoload
535 (defun gofmt-before-save ()
536 "Add this to .emacs to run gofmt on the current buffer when saving:
537 (add-hook 'before-save-hook 'gofmt-before-save)"
538
539 (interactive)
540 (when (eq major-mode 'go-mode)
Sameer (personal) 2011/03/19 02:01:28 This when guard is the only bit that's specific to
541 (let ((outbuf (get-empty-buffer "Gofmt Output"))
542 » (errbuf (get-empty-buffer "Gofmt Errors")))
543 (if (= 0 (gofmt-buffer outbuf nil errbuf))
544
545 » ;; gofmt succeeded: copy outbuf into the current buffer,
546 » ;; preserving the current mark and point
547 » (let ((old-mark (mark t)) (old-point (point)))
548 » (erase-buffer)
549 » (insert-buffer-substring outbuf)
550 » (goto-char (min old-point (point-max)))
551 » (if old-mark (set-mark (min old-mark (point-max))))
552 » (kill-buffer errbuf))
553
554 » ;; gofmt failed: display the errors
555 » (display-buffer errbuf))
556
557 ;; Kill outbuf, collapsing its window first if
558 ;; shell-command-on-region displayed it.
559 (delete-windows-on outbuf)
560 (kill-buffer outbuf))))
OLDNEW
« no previous file with comments | « no previous file | misc/emacs/go-mode-load.el » ('j') | no next file with comments »

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b