.emacs-my
changeset 1729 237c8f98ac43
parent 1728 746bcf68435f
child 1730 795b3f2ecfbb
equal deleted inserted replaced
1728:746bcf68435f 1729:237c8f98ac43
  1650       (goto-char end)
  1650       (goto-char end)
  1651       (save-excursion
  1651       (save-excursion
  1652         (insert result)))))
  1652         (insert result)))))
  1653 
  1653 
  1654 (defun my-calc-line (arg)
  1654 (defun my-calc-line (arg)
  1655   "Evaluate expression in current line and display the result in
  1655   "Evaluate calc expression in current line and display the
  1656 the echo area by skipping final '=' sign. With prefix ARG
  1656 result in the echo area by skipping everything after the final
  1657 non-nil, insert the result at the end of line and space if
  1657 '=' sign.
  1658 necessary for delimiting."
  1658 
       
  1659 With prefix ARG non-nil or repeating command interactively,
       
  1660 insert the result at the end of line + add a space if necessary
       
  1661 for delimiting clearing everything after '=' sign if it is here."
  1659   (interactive "P")
  1662   (interactive "P")
  1660   (require 'calc)
  1663   (require 'calc)
  1661   (save-excursion
  1664   (save-excursion
  1662     (let (beg end expr result)
  1665     (let (beg end expr result)
  1663       (beginning-of-line)
  1666       (beginning-of-line)
  1665       (end-of-line)
  1668       (end-of-line)
  1666       (search-backward "=" beg t)
  1669       (search-backward "=" beg t)
  1667       (setq end (point))
  1670       (setq end (point))
  1668       (setq expr (buffer-substring-no-properties beg end))
  1671       (setq expr (buffer-substring-no-properties beg end))
  1669       (setq result (calc-eval expr))
  1672       (setq result (calc-eval expr))
  1670       (if (null arg)
  1673       (if (and (null arg) (not (eq 'my-calc-line last-command)))
  1671           (message "%s = %s" expr result)
  1674           (message "%s = %s" expr result)
  1672         (end-of-line)
  1675         (end-of-line)
       
  1676         (setq end (point))
       
  1677         (when (search-backward "=" beg t)
       
  1678           (forward-char 1)
       
  1679           (delete-region (point) end))
  1673         (unless (eq (char-before) ?\ )
  1680         (unless (eq (char-before) ?\ )
  1674           (insert ?\ ))
  1681           (insert ?\ ))
  1675         (insert result)))))
  1682         (insert result)))))
  1676 
  1683 
  1677 (global-set-key (kbd "s-=") #'my-calc-line)
  1684 (global-set-key (kbd "s-=") #'my-calc-line)