Klimi's new dotfiles with stow.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

1643 строки
68 KiB

4 лет назад
  1. ;;; magit-section.el --- section functionality -*- lexical-binding: t -*-
  2. ;; Copyright (C) 2010-2019 The Magit Project Contributors
  3. ;;
  4. ;; You should have received a copy of the AUTHORS.md file which
  5. ;; lists all contributors. If not, see http://magit.vc/authors.
  6. ;; Author: Jonas Bernoulli <jonas@bernoul.li>
  7. ;; Maintainer: Jonas Bernoulli <jonas@bernoul.li>
  8. ;; Magit is free software; you can redistribute it and/or modify it
  9. ;; under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation; either version 3, or (at your option)
  11. ;; any later version.
  12. ;;
  13. ;; Magit is distributed in the hope that it will be useful, but WITHOUT
  14. ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  15. ;; or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  16. ;; License for more details.
  17. ;;
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with Magit. If not, see http://www.gnu.org/licenses.
  20. ;;; Commentary:
  21. ;; This library implements "sections" as used in all Magit buffers.
  22. ;; If you have used Magit before, then you probably know what that
  23. ;; means, otherwise think "read-only Org-Mode for Git", kinda.
  24. ;;; Code:
  25. (require 'cl-lib)
  26. (require 'dash)
  27. (require 'eieio)
  28. (eval-when-compile
  29. (require 'subr-x))
  30. (require 'magit-utils)
  31. (declare-function magit-maybe-make-margin-overlay "magit-margin" ())
  32. (declare-function magit-repository-local-get "magit-mode"
  33. (key &optional default repository))
  34. (declare-function magit-repository-local-set "magit-mode"
  35. (key value &optional repository))
  36. (defvar magit-keep-region-overlay)
  37. ;;; Options
  38. (defgroup magit-section nil
  39. "Expandable sections."
  40. :link '(info-link "(magit)Sections")
  41. :group 'magit)
  42. (defcustom magit-section-show-child-count t
  43. "Whether to append the number of children to section headings.
  44. This only applies to sections for which doing so makes sense."
  45. :package-version '(magit . "2.1.0")
  46. :group 'magit-section
  47. :type 'boolean)
  48. (defcustom magit-section-movement-hook
  49. '(magit-hunk-set-window-start
  50. magit-log-maybe-update-revision-buffer
  51. magit-log-maybe-show-more-commits)
  52. "Hook run by `magit-section-goto'.
  53. That function in turn is used by all section movement commands."
  54. :package-version '(magit . "2.3.0")
  55. :group 'magit-section
  56. :type 'hook
  57. :options '(magit-hunk-set-window-start
  58. magit-status-maybe-update-revision-buffer
  59. magit-status-maybe-update-blob-buffer
  60. magit-log-maybe-update-revision-buffer
  61. magit-log-maybe-update-blob-buffer
  62. magit-log-maybe-show-more-commits))
  63. (defcustom magit-section-highlight-hook
  64. '(magit-diff-highlight
  65. magit-section-highlight
  66. magit-section-highlight-selection)
  67. "Functions used to highlight the current section.
  68. Each function is run with the current section as only argument
  69. until one of them returns non-nil."
  70. :package-version '(magit . "2.1.0")
  71. :group 'magit-section
  72. :type 'hook
  73. :options '(magit-diff-highlight
  74. magit-section-highlight
  75. magit-section-highlight-selection))
  76. (defcustom magit-section-unhighlight-hook
  77. '(magit-diff-unhighlight)
  78. "Functions used to unhighlight the previously current section.
  79. Each function is run with the current section as only argument
  80. until one of them returns non-nil. Most sections are properly
  81. unhighlighted without requiring a specialized unhighlighter,
  82. diff-related sections being the only exception."
  83. :package-version '(magit . "2.1.0")
  84. :group 'magit-section
  85. :type 'hook
  86. :options '(magit-diff-unhighlight))
  87. (defcustom magit-section-set-visibility-hook
  88. '(magit-diff-expansion-threshold
  89. magit-section-cached-visibility)
  90. "Hook used to set the initial visibility of a section.
  91. Stop at the first function that returns non-nil. The returned
  92. value should be `show', `hide' or nil. If no function returns
  93. non-nil, determine the visibility as usual, i.e. use the
  94. hardcoded section specific default (see `magit-insert-section')."
  95. :package-version '(magit . "2.4.0")
  96. :group 'magit-section
  97. :type 'hook
  98. :options '(magit-diff-expansion-threshold
  99. magit-section-cached-visibility))
  100. (defcustom magit-section-cache-visibility t
  101. "Whether to cache visibility of sections.
  102. Sections always retain their visibility state when they are being
  103. recreated during a refresh. But if a section disappears and then
  104. later reappears again, then this option controls whether this is
  105. the case.
  106. If t, then cache the visibility of all sections. If a list of
  107. section types, then only do so for matching sections. If nil,
  108. then don't do so for any sections."
  109. :package-version '(magit . "2.12.0")
  110. :group 'magit-section
  111. :type '(choice (const :tag "Don't cache visibility" nil)
  112. (const :tag "Cache visibility of all sections" t)
  113. (repeat :tag "Cache visibility for section types" symbol)))
  114. (defcustom magit-section-initial-visibility-alist
  115. '((stashes . hide))
  116. "Alist controlling the initial visibility of sections.
  117. Each element maps a section type or lineage to the initial
  118. visibility state for such sections. The state has to be one of
  119. `show' or `hide', or a function that returns one of these symbols.
  120. A function is called with the section as the only argument.
  121. Use the command `magit-describe-section' to determine a section's
  122. lineage or type. The vector in the output is the section lineage
  123. and the type is the first element of that vector. Wildcards can
  124. be used, see `magit-section-match'.
  125. Currently this option is only used to override hardcoded defaults,
  126. but in the future it will also be used set the defaults.
  127. An entry whose key is `magit-status-initial-section' specifies
  128. the visibility of the section `magit-status-goto-initial-section'
  129. jumps to. This does not only override defaults, but also other
  130. entries of this alist."
  131. :package-version '(magit . "2.12.0")
  132. :group 'magit-section
  133. :type '(alist :key-type (sexp :tag "Section type/lineage")
  134. :value-type (choice (const hide)
  135. (const show)
  136. function)))
  137. (defcustom magit-section-visibility-indicator
  138. (if (window-system)
  139. '(magit-fringe-bitmap> . magit-fringe-bitmapv)
  140. '("" . t))
  141. "Whether and how to indicate that a section can be expanded/collapsed.
  142. If nil, then don't show any indicators.
  143. Otherwise the value has to have one of these two forms:
  144. (EXPANDABLE-BITMAP . COLLAPSIBLE-BITMAP)
  145. Both values have to be variables whose values are fringe
  146. bitmaps. In this case every section that can be expanded or
  147. collapsed gets an indicator in the left fringe.
  148. To provide extra padding around the indicator, set
  149. `left-fringe-width' in `magit-mode-hook'.
  150. (STRING . BOOLEAN)
  151. In this case STRING (usually an ellipsis) is shown at the end
  152. of the heading of every collapsed section. Expanded sections
  153. get no indicator. The cdr controls whether the appearance of
  154. these ellipsis take section highlighting into account. Doing
  155. so might potentially have an impact on performance, while not
  156. doing so is kinda ugly."
  157. :package-version '(magit . "2.91.0")
  158. :group 'magit-section
  159. :type '(choice (const :tag "No indicators" nil)
  160. (cons :tag "Use +- fringe indicators"
  161. (const magit-fringe-bitmap+)
  162. (const magit-fringe-bitmap-))
  163. (cons :tag "Use >v fringe indicators"
  164. (const magit-fringe-bitmap>)
  165. (const magit-fringe-bitmapv))
  166. (cons :tag "Use bold >v fringe indicators)"
  167. (const magit-fringe-bitmap-bold>)
  168. (const magit-fringe-bitmap-boldv))
  169. (cons :tag "Use custom fringe indicators"
  170. (variable :tag "Expandable bitmap variable")
  171. (variable :tag "Collapsable bitmap variable"))
  172. (cons :tag "Use ellipses at end of headings"
  173. (string :tag "Ellipsis" "")
  174. (choice :tag "Use face kludge"
  175. (const :tag "Yes (potentially slow)" t)
  176. (const :tag "No (kinda ugly)" nil)))))
  177. (defface magit-section-highlight
  178. '((((class color) (background light)) :background "grey95")
  179. (((class color) (background dark)) :background "grey20"))
  180. "Face for highlighting the current section."
  181. :group 'magit-faces)
  182. (defface magit-section-heading
  183. '((((class color) (background light)) :foreground "DarkGoldenrod4" :weight bold)
  184. (((class color) (background dark)) :foreground "LightGoldenrod2" :weight bold))
  185. "Face for section headings."
  186. :group 'magit-faces)
  187. (defface magit-section-secondary-heading '((t :weight bold))
  188. "Face for section headings of some secondary headings."
  189. :group 'magit-faces)
  190. (defface magit-section-heading-selection
  191. '((((class color) (background light)) :foreground "salmon4")
  192. (((class color) (background dark)) :foreground "LightSalmon3"))
  193. "Face for selected section headings."
  194. :group 'magit-faces)
  195. ;;; Classes
  196. (defvar magit--current-section-hook nil
  197. "Internal variable used for `magit-explain-section'.")
  198. (defvar magit--section-type-alist
  199. '(
  200. (file . magit-file-section)
  201. (hunk . magit-hunk-section)
  202. (module . magit-module-section)
  203. ))
  204. (defclass magit-section ()
  205. ((keymap :initform nil :allocation :class)
  206. (type :initform nil :initarg :type)
  207. (value :initform nil :initarg :value)
  208. (start :initform nil :initarg :start)
  209. (content :initform nil)
  210. (end :initform nil)
  211. (hidden :initform nil)
  212. (washer :initform nil)
  213. (process :initform nil)
  214. (heading-highlight-face :initform nil)
  215. (inserter :initform (symbol-value 'magit--current-section-hook))
  216. (parent :initform nil :initarg :parent)
  217. (children :initform nil)))
  218. (defclass magit-file-section (magit-section)
  219. ((source :initform nil)
  220. (header :initform nil)))
  221. (defclass magit-hunk-section (magit-section)
  222. ((refined :initform nil)
  223. (combined :initform nil)
  224. (from-range :initform nil)
  225. (from-ranges :initform nil)
  226. (to-range :initform nil)
  227. (about :initform nil)))
  228. (defclass magit-module-section (magit-file-section)
  229. ())
  230. ;;; Core
  231. (defvar-local magit-root-section nil
  232. "The root section in the current buffer.
  233. All other sections are descendants of this section. The value
  234. of this variable is set by `magit-insert-section' and you should
  235. never modify it.")
  236. (put 'magit-root-section 'permanent-local t)
  237. (defun magit-current-section ()
  238. "Return the section at point."
  239. (or (get-text-property (point) 'magit-section) magit-root-section))
  240. (defun magit-section-ident (section)
  241. "Return an unique identifier for SECTION.
  242. The return value has the form ((TYPE . VALUE)...)."
  243. (with-slots (type value parent) section
  244. (cons (cons type
  245. (cond ((eieio-object-p value)
  246. (magit-section-ident-value value))
  247. ((not (memq type '(unpulled unpushed))) value)
  248. ((string-match-p "@{upstream}" value) value)
  249. ;; Unfortunately Git chokes on "@{push}" when
  250. ;; the value of `push.default' does not allow a
  251. ;; 1:1 mapping. Arbitrary commands may consult
  252. ;; the section value so we cannot use "@{push}".
  253. ;; But `unpushed' and `unpulled' sections should
  254. ;; keep their identity when switching branches
  255. ;; so we have to use another value here.
  256. ((string-match-p "\\`\\.\\." value) "..@{push}")
  257. (t "@{push}..")))
  258. (and parent
  259. (magit-section-ident parent)))))
  260. (cl-defgeneric magit-section-ident-value (VALUE)
  261. "Return a constant representation of VALUE.
  262. VALUE is the value of a `magit-section' object. If that is an
  263. object itself, then that is not suitable to be used to identify
  264. the section because two objects may represent the same thing but
  265. not be equal. If possible a method should be added for such
  266. objects, which returns a value that is equal. Otherwise the
  267. catch-all method is used, which just returns the argument
  268. itself.")
  269. (cl-defmethod magit-section-ident-value (arg) arg)
  270. (defun magit-get-section (ident &optional root)
  271. "Return the section identified by IDENT.
  272. IDENT has to be a list as returned by `magit-section-ident'."
  273. (setq ident (reverse ident))
  274. (let ((section (or root magit-root-section)))
  275. (when (eq (car (pop ident))
  276. (oref section type))
  277. (while (and ident
  278. (pcase-let* ((`(,type . ,value) (car ident))
  279. (value (magit-section-ident-value value)))
  280. (setq section
  281. (cl-find-if (lambda (section)
  282. (and (eq (oref section type) type)
  283. (equal (magit-section-ident-value
  284. (oref section value))
  285. value)))
  286. (oref section children)))))
  287. (pop ident))
  288. section)))
  289. (defun magit-section-lineage (section)
  290. "Return the lineage of SECTION.
  291. The return value has the form (TYPE...)."
  292. (cons (oref section type)
  293. (when-let ((parent (oref section parent)))
  294. (magit-section-lineage parent))))
  295. (defvar magit-insert-section--current nil "For internal use only.")
  296. (defvar magit-insert-section--parent nil "For internal use only.")
  297. (defvar magit-insert-section--oldroot nil "For internal use only.")
  298. ;;; Commands
  299. ;;;; Movement
  300. (defun magit-section-forward ()
  301. "Move to the beginning of the next visible section."
  302. (interactive)
  303. (if (eobp)
  304. (user-error "No next section")
  305. (let ((section (magit-current-section)))
  306. (if (oref section parent)
  307. (let ((next (and (not (oref section hidden))
  308. (not (= (oref section end)
  309. (1+ (point))))
  310. (car (oref section children)))))
  311. (while (and section (not next))
  312. (unless (setq next (car (magit-section-siblings section 'next)))
  313. (setq section (oref section parent))))
  314. (if next
  315. (magit-section-goto next)
  316. (user-error "No next section")))
  317. (magit-section-goto 1)))))
  318. (defun magit-section-backward ()
  319. "Move to the beginning of the current or the previous visible section.
  320. When point is at the beginning of a section then move to the
  321. beginning of the previous visible section. Otherwise move to
  322. the beginning of the current section."
  323. (interactive)
  324. (if (bobp)
  325. (user-error "No previous section")
  326. (let ((section (magit-current-section)) children)
  327. (cond
  328. ((and (= (point)
  329. (1- (oref section end)))
  330. (setq children (oref section children)))
  331. (magit-section-goto (car (last children))))
  332. ((and (oref section parent)
  333. (not (= (point)
  334. (oref section start))))
  335. (magit-section-goto section))
  336. (t
  337. (let ((prev (car (magit-section-siblings section 'prev))))
  338. (if prev
  339. (while (and (not (oref prev hidden))
  340. (setq children (oref prev children)))
  341. (setq prev (car (last children))))
  342. (setq prev (oref section parent)))
  343. (cond (prev
  344. (magit-section-goto prev))
  345. ((oref section parent)
  346. (user-error "No previous section"))
  347. ;; Eob special cases.
  348. ((not (get-text-property (1- (point)) 'invisible))
  349. (magit-section-goto -1))
  350. (t
  351. (goto-char (previous-single-property-change
  352. (1- (point)) 'invisible))
  353. (forward-line -1)
  354. (magit-section-goto (magit-current-section))))))))))
  355. (defun magit-section-up ()
  356. "Move to the beginning of the parent section."
  357. (interactive)
  358. (--if-let (oref (magit-current-section) parent)
  359. (magit-section-goto it)
  360. (user-error "No parent section")))
  361. (defun magit-section-forward-sibling ()
  362. "Move to the beginning of the next sibling section.
  363. If there is no next sibling section, then move to the parent."
  364. (interactive)
  365. (let ((current (magit-current-section)))
  366. (if (oref current parent)
  367. (--if-let (car (magit-section-siblings current 'next))
  368. (magit-section-goto it)
  369. (magit-section-forward))
  370. (magit-section-goto 1))))
  371. (defun magit-section-backward-sibling ()
  372. "Move to the beginning of the previous sibling section.
  373. If there is no previous sibling section, then move to the parent."
  374. (interactive)
  375. (let ((current (magit-current-section)))
  376. (if (oref current parent)
  377. (--if-let (car (magit-section-siblings current 'prev))
  378. (magit-section-goto it)
  379. (magit-section-backward))
  380. (magit-section-goto -1))))
  381. (defun magit-section-goto (arg)
  382. (if (integerp arg)
  383. (progn (forward-line arg)
  384. (setq arg (magit-current-section)))
  385. (goto-char (oref arg start)))
  386. (run-hook-with-args 'magit-section-movement-hook arg))
  387. (defun magit-section-set-window-start (section)
  388. "Ensure the beginning of SECTION is visible."
  389. (unless (pos-visible-in-window-p (oref section end))
  390. (set-window-start (selected-window) (oref section start))))
  391. (defun magit-hunk-set-window-start (section)
  392. "When SECTION is a `hunk', ensure that its beginning is visible.
  393. It the SECTION has a different type, then do nothing."
  394. (when (magit-hunk-section-p section)
  395. (magit-section-set-window-start section)))
  396. (defmacro magit-define-section-jumper (name heading type &optional value)
  397. "Define an interactive function to go some section.
  398. Together TYPE and VALUE identify the section.
  399. HEADING is the displayed heading of the section."
  400. (declare (indent defun))
  401. `(defun ,name (&optional expand) ,(format "\
  402. Jump to the section \"%s\".
  403. With a prefix argument also expand it." heading)
  404. (interactive "P")
  405. (--if-let (magit-get-section
  406. (cons (cons ',type ,value)
  407. (magit-section-ident magit-root-section)))
  408. (progn (goto-char (oref it start))
  409. (when expand
  410. (with-local-quit (magit-section-show it))
  411. (recenter 0)))
  412. (message ,(format "Section \"%s\" wasn't found" heading)))))
  413. ;;;; Visibility
  414. (defun magit-section-show (section)
  415. "Show the body of the current section."
  416. (interactive (list (magit-current-section)))
  417. (oset section hidden nil)
  418. (magit-section--maybe-wash section)
  419. (when-let ((beg (oref section content)))
  420. (remove-overlays beg (oref section end) 'invisible t))
  421. (magit-section-maybe-update-visibility-indicator section)
  422. (magit-section-maybe-cache-visibility section)
  423. (dolist (child (oref section children))
  424. (if (oref child hidden)
  425. (magit-section-hide child)
  426. (magit-section-show child))))
  427. (defun magit-section--maybe-wash (section)
  428. (when-let ((washer (oref section washer)))
  429. (oset section washer nil)
  430. (let ((inhibit-read-only t)
  431. (magit-insert-section--parent section)
  432. (content (oref section content)))
  433. (save-excursion
  434. (if (and content (< content (oref section end)))
  435. (funcall washer section) ; already partially washed (hunk)
  436. (goto-char (oref section end))
  437. (oset section content (point-marker))
  438. (funcall washer)
  439. (oset section end (point-marker)))))
  440. (magit-section-update-highlight)))
  441. (defun magit-section-hide (section)
  442. "Hide the body of the current section."
  443. (interactive (list (magit-current-section)))
  444. (if (eq section magit-root-section)
  445. (user-error "Cannot hide root section")
  446. (oset section hidden t)
  447. (when-let ((beg (oref section content)))
  448. (let ((end (oref section end)))
  449. (remove-overlays beg end 'invisible t)
  450. (let ((o (make-overlay beg end)))
  451. (overlay-put o 'evaporate t)
  452. (overlay-put o 'invisible t))))
  453. (magit-section-maybe-update-visibility-indicator section)
  454. (magit-section-maybe-cache-visibility section)))
  455. (defun magit-section-toggle (section)
  456. "Toggle visibility of the body of the current section."
  457. (interactive (list (magit-current-section)))
  458. (if (eq section magit-root-section)
  459. (user-error "Cannot hide root section")
  460. (goto-char (oref section start))
  461. (if (oref section hidden)
  462. (magit-section-show section)
  463. (magit-section-hide section))))
  464. (defun magit-section-toggle-children (section)
  465. "Toggle visibility of bodies of children of the current section."
  466. (interactive (list (magit-current-section)))
  467. (goto-char (oref section start))
  468. (let* ((children (oref section children))
  469. (show (--any-p (oref it hidden) children)))
  470. (dolist (c children)
  471. (oset c hidden show)))
  472. (magit-section-show section))
  473. (defun magit-section-show-children (section &optional depth)
  474. "Recursively show the bodies of children of the current section.
  475. With a prefix argument show children that deep and hide deeper
  476. children."
  477. (interactive (list (magit-current-section)))
  478. (magit-section-show-children-1 section depth)
  479. (magit-section-show section))
  480. (defun magit-section-show-children-1 (section &optional depth)
  481. (dolist (child (oref section children))
  482. (oset child hidden nil)
  483. (if depth
  484. (if (> depth 0)
  485. (magit-section-show-children-1 child (1- depth))
  486. (magit-section-hide child))
  487. (magit-section-show-children-1 child))))
  488. (defun magit-section-hide-children (section)
  489. "Recursively hide the bodies of children of the current section."
  490. (interactive (list (magit-current-section)))
  491. (mapc 'magit-section-hide (oref section children)))
  492. (defun magit-section-show-headings (section)
  493. "Recursively show headings of children of the current section.
  494. Only show the headings, previously shown text-only bodies are
  495. hidden."
  496. (interactive (list (magit-current-section)))
  497. (magit-section-show-headings-1 section)
  498. (magit-section-show section))
  499. (defun magit-section-show-headings-1 (section)
  500. (dolist (child (oref section children))
  501. (oset child hidden nil)
  502. (when (or (oref child children)
  503. (not (oref child content)))
  504. (magit-section-show-headings-1 child))))
  505. (defun magit-section-cycle (section)
  506. "Cycle visibility of current section and its children."
  507. (interactive (list (magit-current-section)))
  508. (goto-char (oref section start))
  509. (if (oref section hidden)
  510. (progn (magit-section-show section)
  511. (magit-section-hide-children section))
  512. (let ((children (oref section children)))
  513. (cond ((and (--any-p (oref it hidden) children)
  514. (--any-p (oref it children) children))
  515. (magit-section-show-headings section))
  516. ((-any-p 'magit-section-hidden-body children)
  517. (magit-section-show-children section))
  518. (t
  519. (magit-section-hide section))))))
  520. (defun magit-section-cycle-global ()
  521. "Cycle visibility of all sections in the current buffer."
  522. (interactive)
  523. (let ((children (oref magit-root-section children)))
  524. (cond ((and (--any-p (oref it hidden) children)
  525. (--any-p (oref it children) children))
  526. (magit-section-show-headings magit-root-section))
  527. ((-any-p 'magit-section-hidden-body children)
  528. (magit-section-show-children magit-root-section))
  529. (t
  530. (mapc 'magit-section-hide children)))))
  531. (defun magit-section-cycle-diffs ()
  532. "Cycle visibility of diff-related sections in the current buffer."
  533. (interactive)
  534. (when-let ((sections
  535. (cond ((derived-mode-p 'magit-status-mode)
  536. (--mapcat
  537. (when it
  538. (when (oref it hidden)
  539. (magit-section-show it))
  540. (oref it children))
  541. (list (magit-get-section '((staged) (status)))
  542. (magit-get-section '((unstaged) (status))))))
  543. ((derived-mode-p 'magit-diff-mode)
  544. (-filter #'magit-file-section-p
  545. (oref magit-root-section children))))))
  546. (if (--any-p (oref it hidden) sections)
  547. (dolist (s sections)
  548. (magit-section-show s)
  549. (magit-section-hide-children s))
  550. (let ((children (--mapcat (oref it children) sections)))
  551. (cond ((and (--any-p (oref it hidden) children)
  552. (--any-p (oref it children) children))
  553. (mapc 'magit-section-show-headings sections))
  554. ((-any-p 'magit-section-hidden-body children)
  555. (mapc 'magit-section-show-children sections))
  556. (t
  557. (mapc 'magit-section-hide sections)))))))
  558. (defun magit-section-hidden-body (section &optional pred)
  559. (--if-let (oref section children)
  560. (funcall (or pred '-any-p) 'magit-section-hidden-body it)
  561. (and (oref section content)
  562. (oref section hidden))))
  563. (defun magit-section-invisible-p (section)
  564. "Return t if the SECTION's body is invisible.
  565. When the body of an ancestor of SECTION is collapsed then
  566. SECTION's body (and heading) obviously cannot be visible."
  567. (or (oref section hidden)
  568. (--when-let (oref section parent)
  569. (magit-section-invisible-p it))))
  570. (defun magit-section-show-level (level)
  571. "Show surrounding sections up to LEVEL.
  572. If LEVEL is negative, show up to the absolute value.
  573. Sections at higher levels are hidden."
  574. (if (< level 0)
  575. (let ((s (magit-current-section)))
  576. (setq level (- level))
  577. (while (> (1- (length (magit-section-ident s))) level)
  578. (setq s (oref s parent))
  579. (goto-char (oref s start)))
  580. (magit-section-show-children magit-root-section (1- level)))
  581. (cl-do* ((s (magit-current-section)
  582. (oref s parent))
  583. (i (1- (length (magit-section-ident s)))
  584. (cl-decf i)))
  585. ((cond ((< i level) (magit-section-show-children s (- level i 1)) t)
  586. ((= i level) (magit-section-hide s) t))
  587. (magit-section-goto s)))))
  588. (defun magit-section-show-level-1 ()
  589. "Show surrounding sections on first level."
  590. (interactive)
  591. (magit-section-show-level 1))
  592. (defun magit-section-show-level-1-all ()
  593. "Show all sections on first level."
  594. (interactive)
  595. (magit-section-show-level -1))
  596. (defun magit-section-show-level-2 ()
  597. "Show surrounding sections up to second level."
  598. (interactive)
  599. (magit-section-show-level 2))
  600. (defun magit-section-show-level-2-all ()
  601. "Show all sections up to second level."
  602. (interactive)
  603. (magit-section-show-level -2))
  604. (defun magit-section-show-level-3 ()
  605. "Show surrounding sections up to third level."
  606. (interactive)
  607. (magit-section-show-level 3))
  608. (defun magit-section-show-level-3-all ()
  609. "Show all sections up to third level."
  610. (interactive)
  611. (magit-section-show-level -3))
  612. (defun magit-section-show-level-4 ()
  613. "Show surrounding sections up to fourth level."
  614. (interactive)
  615. (magit-section-show-level 4))
  616. (defun magit-section-show-level-4-all ()
  617. "Show all sections up to fourth level."
  618. (interactive)
  619. (magit-section-show-level -4))
  620. ;;;; Auxiliary
  621. (defun magit-describe-section-briefly (section &optional ident)
  622. "Show information about the section at point.
  623. With a prefix argument show the section identity instead of the
  624. section lineage. This command is intended for debugging purposes."
  625. (interactive (list (magit-current-section) current-prefix-arg))
  626. (let ((str (format "#<%s %S %S %s-%s>"
  627. (eieio-object-class section)
  628. (let ((val (oref section value)))
  629. (cond ((stringp val)
  630. (substring-no-properties val))
  631. ((and (eieio-object-p val)
  632. (fboundp 'cl-prin1-to-string))
  633. (cl-prin1-to-string val))
  634. (t
  635. val)))
  636. (if ident
  637. (magit-section-ident section)
  638. (apply #'vector (magit-section-lineage section)))
  639. (when-let ((m (oref section start)))
  640. (marker-position m))
  641. (when-let ((m (oref section end)))
  642. (marker-position m)))))
  643. (if (called-interactively-p 'any)
  644. (message "%s" str)
  645. str)))
  646. (cl-defmethod cl-print-object ((section magit-section) stream)
  647. "Print `magit-describe-section' result of SECTION."
  648. ;; Used by debug and edebug as of Emacs 26.
  649. (princ (magit-describe-section-briefly section) stream))
  650. (defun magit-describe-section (section &optional interactive-p)
  651. "Show information about the section at point."
  652. (interactive (list (magit-current-section) t))
  653. (let ((inserter-section section))
  654. (while (and inserter-section (not (oref inserter-section inserter)))
  655. (setq inserter-section (oref inserter-section parent)))
  656. (when (and inserter-section (oref inserter-section inserter))
  657. (setq section inserter-section)))
  658. (pcase (oref section inserter)
  659. (`((,hook ,fun) . ,src-src)
  660. (help-setup-xref `(magit-describe-section ,section) interactive-p)
  661. (with-help-window (help-buffer)
  662. (with-current-buffer standard-output
  663. (insert (format-message
  664. "%s\n is inserted by `%s'\n from `%s'"
  665. (magit-describe-section-briefly section)
  666. (make-text-button (symbol-name fun) nil
  667. :type 'help-function
  668. 'help-args (list fun))
  669. (make-text-button (symbol-name hook) nil
  670. :type 'help-variable
  671. 'help-args (list hook))))
  672. (pcase-dolist (`(,hook ,fun) src-src)
  673. (insert (format-message
  674. ",\n called by `%s'\n from `%s'"
  675. (make-text-button (symbol-name fun) nil
  676. :type 'help-function
  677. 'help-args (list fun))
  678. (make-text-button (symbol-name hook) nil
  679. :type 'help-variable
  680. 'help-args (list hook)))))
  681. (insert ".\n\n")
  682. (insert
  683. (format-message
  684. "`%s' is "
  685. (make-text-button (symbol-name fun) nil
  686. :type 'help-function 'help-args (list fun))))
  687. (describe-function-1 fun))))
  688. (_ (message "%s, inserter unknown"
  689. (magit-describe-section-briefly section)))))
  690. ;;; Match
  691. (cl-defun magit-section-match
  692. (condition &optional (section (magit-current-section)))
  693. "Return t if SECTION matches CONDITION.
  694. SECTION defaults to the section at point. If SECTION is not
  695. specified and there also is no section at point, then return
  696. nil.
  697. CONDITION can take the following forms:
  698. (CONDITION...) matches if any of the CONDITIONs matches.
  699. [CLASS...] matches if the section's class is the same
  700. as the first CLASS or a subclass of that;
  701. the section's parent class matches the
  702. second CLASS; and so on.
  703. [* CLASS...] matches sections that match [CLASS...] and
  704. also recursively all their child sections.
  705. CLASS matches if the section's class is the same
  706. as CLASS or a subclass of that; regardless
  707. of the classes of the parent sections.
  708. Each CLASS should be a class symbol, identifying a class that
  709. derives from `magit-section'. For backward compatibility CLASS
  710. can also be a \"type symbol\". A section matches such a symbol
  711. if the value of its `type' slot is `eq'. If a type symbol has
  712. an entry in `magit--section-type-alist', then a section also
  713. matches that type if its class is a subclass of the class that
  714. corresponds to the type as per that alist.
  715. Note that it is not necessary to specify the complete section
  716. lineage as printed by `magit-describe-section-briefly', unless
  717. of course you want to be that precise."
  718. (and section (magit-section-match-1 condition section)))
  719. (defun magit-section-match-1 (condition section)
  720. (cl-assert condition)
  721. (and section
  722. (if (listp condition)
  723. (--first (magit-section-match-1 it section) condition)
  724. (magit-section-match-2 (if (symbolp condition)
  725. (list condition)
  726. (cl-coerce condition 'list))
  727. section))))
  728. (defun magit-section-match-2 (condition section)
  729. (if (eq (car condition) '*)
  730. (or (magit-section-match-2 (cdr condition) section)
  731. (when-let ((parent (oref section parent)))
  732. (magit-section-match-2 condition parent)))
  733. (and (let ((c (car condition)))
  734. (if (class-p c)
  735. (cl-typep section c)
  736. (if-let ((class (cdr (assq c magit--section-type-alist))))
  737. (cl-typep section class)
  738. (eq (oref section type) c))))
  739. (or (not (setq condition (cdr condition)))
  740. (when-let ((parent (oref section parent)))
  741. (magit-section-match-2 condition parent))))))
  742. (defun magit-section-value-if (condition &optional section)
  743. "If the section at point matches CONDITION, then return its value.
  744. If optional SECTION is non-nil then test whether that matches
  745. instead. If there is no section at point and SECTION is nil,
  746. then return nil. If the section does not match, then return
  747. nil.
  748. See `magit-section-match' for the forms CONDITION can take."
  749. (when-let ((section (or section (magit-current-section))))
  750. (and (magit-section-match condition section)
  751. (oref section value))))
  752. (defmacro magit-section-when (condition &rest body)
  753. "If the section at point matches CONDITION, evaluate BODY.
  754. If the section matches, then evaluate BODY forms sequentially
  755. with `it' bound to the section and return the value of the last
  756. form. If there are no BODY forms, then return the value of the
  757. section. If the section does not match or if there is no section
  758. at point, then return nil.
  759. See `magit-section-match' for the forms CONDITION can take."
  760. (declare (obsolete
  761. "instead use `magit-section-match' or `magit-section-value-if'."
  762. "Magit 2.90.0")
  763. (indent 1)
  764. (debug (sexp body)))
  765. `(--when-let (magit-current-section)
  766. ;; Quoting CONDITION here often leads to double-quotes, which
  767. ;; isn't an issue because `magit-section-match-1' implicitly
  768. ;; deals with that. We shouldn't force users of this function
  769. ;; to not quote CONDITION because that would needlessly break
  770. ;; backward compatibility.
  771. (when (magit-section-match ',condition it)
  772. ,@(or body '((oref it value))))))
  773. (defmacro magit-section-case (&rest clauses)
  774. "Choose among clauses on the type of the section at point.
  775. Each clause looks like (CONDITION BODY...). The type of the
  776. section is compared against each CONDITION; the BODY forms of the
  777. first match are evaluated sequentially and the value of the last
  778. form is returned. Inside BODY the symbol `it' is bound to the
  779. section at point. If no clause succeeds or if there is no
  780. section at point, return nil.
  781. See `magit-section-match' for the forms CONDITION can take.
  782. Additionally a CONDITION of t is allowed in the final clause, and
  783. matches if no other CONDITION match, even if there is no section
  784. at point."
  785. (declare (indent 0)
  786. (debug (&rest (sexp body))))
  787. `(let* ((it (magit-current-section)))
  788. (cond ,@(mapcar (lambda (clause)
  789. `(,(or (eq (car clause) t)
  790. `(and it
  791. (magit-section-match-1 ',(car clause) it)))
  792. ,@(cdr clause)))
  793. clauses))))
  794. (defun magit-section-match-assoc (section alist)
  795. "Return the value associated with SECTION's type or lineage in ALIST."
  796. (-some (pcase-lambda (`(,key . ,val))
  797. (and (magit-section-match-1 key section) val))
  798. alist))
  799. ;;; Create
  800. (defvar magit-insert-section-hook nil
  801. "Hook run after `magit-insert-section's BODY.
  802. Avoid using this hook and only ever do so if you know
  803. what you are doing and are sure there is no other way.")
  804. (defmacro magit-insert-section (&rest args)
  805. "Insert a section at point.
  806. TYPE is the section type, a symbol. Many commands that act on
  807. the current section behave differently depending on that type.
  808. Also if a variable `magit-TYPE-section-map' exists, then use
  809. that as the text-property `keymap' of all text belonging to the
  810. section (but this may be overwritten in subsections). TYPE can
  811. also have the form `(eval FORM)' in which case FORM is evaluated
  812. at runtime.
  813. Optional VALUE is the value of the section, usually a string
  814. that is required when acting on the section.
  815. When optional HIDE is non-nil collapse the section body by
  816. default, i.e. when first creating the section, but not when
  817. refreshing the buffer. Else expand it by default. This can be
  818. overwritten using `magit-section-set-visibility-hook'. When a
  819. section is recreated during a refresh, then the visibility of
  820. predecessor is inherited and HIDE is ignored (but the hook is
  821. still honored).
  822. BODY is any number of forms that actually insert the section's
  823. heading and body. Optional NAME, if specified, has to be a
  824. symbol, which is then bound to the struct of the section being
  825. inserted.
  826. Before BODY is evaluated the `start' of the section object is set
  827. to the value of `point' and after BODY was evaluated its `end' is
  828. set to the new value of `point'; BODY is responsible for moving
  829. `point' forward.
  830. If it turns out inside BODY that the section is empty, then
  831. `magit-cancel-section' can be used to abort and remove all traces
  832. of the partially inserted section. This can happen when creating
  833. a section by washing Git's output and Git didn't actually output
  834. anything this time around.
  835. \(fn [NAME] (TYPE &optional VALUE HIDE) &rest BODY)"
  836. (declare (indent defun)
  837. (debug ([&optional symbolp]
  838. (&or [("eval" symbolp) &optional form form]
  839. [symbolp &optional form form])
  840. body)))
  841. (let ((tp (cl-gensym "type"))
  842. (s* (and (symbolp (car args))
  843. (pop args)))
  844. (s (cl-gensym "section")))
  845. `(let* ((,tp ,(let ((type (nth 0 (car args))))
  846. (if (eq (car-safe type) 'eval)
  847. (cadr type)
  848. `',type)))
  849. (,s (funcall (if (class-p ,tp)
  850. ,tp
  851. (or (cdr (assq ,tp magit--section-type-alist))
  852. 'magit-section))
  853. :type
  854. (if (class-p ,tp)
  855. (or (car (rassq ,tp magit--section-type-alist))
  856. (error "BUG: No entry for %s in %s" ,tp
  857. 'magit--section-type-alist))
  858. ,tp)
  859. :value ,(nth 1 (car args))
  860. :start (point-marker)
  861. :parent magit-insert-section--parent)))
  862. (oset ,s hidden
  863. (if-let ((value (run-hook-with-args-until-success
  864. 'magit-section-set-visibility-hook ,s)))
  865. (eq value 'hide)
  866. (if-let ((incarnation (and magit-insert-section--oldroot
  867. (magit-get-section
  868. (magit-section-ident ,s)
  869. magit-insert-section--oldroot))))
  870. (oref incarnation hidden)
  871. (if-let ((value (magit-section-match-assoc
  872. ,s magit-section-initial-visibility-alist)))
  873. (progn
  874. (when (functionp value)
  875. (setq value (funcall value ,s)))
  876. (eq value 'hide))
  877. ,(nth 2 (car args))))))
  878. (let ((magit-insert-section--current ,s)
  879. (magit-insert-section--parent ,s)
  880. (magit-insert-section--oldroot
  881. (or magit-insert-section--oldroot
  882. (unless magit-insert-section--parent
  883. (prog1 magit-root-section
  884. (setq magit-root-section ,s))))))
  885. (catch 'cancel-section
  886. ,@(if s*
  887. `((let ((,s* ,s))
  888. ,@(cdr args)))
  889. (cdr args))
  890. ;; `magit-insert-section-hook' should *not* be run with
  891. ;; `magit-run-section-hook' because it's a hook that runs
  892. ;; on section insertion, not a section inserting hook.
  893. (run-hooks 'magit-insert-section-hook)
  894. (magit-insert-child-count ,s)
  895. (set-marker-insertion-type (oref ,s start) t)
  896. (let* ((end (oset ,s end (point-marker)))
  897. (class-map (oref-default ,s keymap))
  898. (magit-map (intern (format "magit-%s-section-map"
  899. (oref ,s type))))
  900. (forge-map (intern (format "forge-%s-section-map"
  901. (oref ,s type))))
  902. (map (or (and class-map (symbol-value class-map))
  903. (and (boundp magit-map) (symbol-value magit-map))
  904. (and (boundp forge-map) (symbol-value forge-map)))))
  905. (save-excursion
  906. (goto-char (oref ,s start))
  907. (while (< (point) end)
  908. (let ((next (or (next-single-property-change
  909. (point) 'magit-section)
  910. end)))
  911. (unless (get-text-property (point) 'magit-section)
  912. (put-text-property (point) next 'magit-section ,s)
  913. (when map
  914. (put-text-property (point) next 'keymap map)))
  915. (goto-char next)))))
  916. (if (eq ,s magit-root-section)
  917. (let ((magit-section-cache-visibility nil))
  918. (magit-section-show ,s))
  919. (oset (oref ,s parent) children
  920. (nconc (oref (oref ,s parent) children)
  921. (list ,s)))))
  922. ,s))))
  923. (defun magit-cancel-section ()
  924. (when magit-insert-section--current
  925. (if (not (oref magit-insert-section--current parent))
  926. (insert "(empty)\n")
  927. (delete-region (oref magit-insert-section--current start)
  928. (point))
  929. (setq magit-insert-section--current nil)
  930. (throw 'cancel-section nil))))
  931. (defun magit-insert-heading (&rest args)
  932. "Insert the heading for the section currently being inserted.
  933. This function should only be used inside `magit-insert-section'.
  934. When called without any arguments, then just set the `content'
  935. slot of the object representing the section being inserted to
  936. a marker at `point'. The section should only contain a single
  937. line when this function is used like this.
  938. When called with arguments ARGS, which have to be strings, or
  939. nil, then insert those strings at point. The section should not
  940. contain any text before this happens and afterwards it should
  941. again only contain a single line. If the `face' property is set
  942. anywhere inside any of these strings, then insert all of them
  943. unchanged. Otherwise use the `magit-section-heading' face for
  944. all inserted text.
  945. The `content' property of the section struct is the end of the
  946. heading (which lasts from `start' to `content') and the beginning
  947. of the the body (which lasts from `content' to `end'). If the
  948. value of `content' is nil, then the section has no heading and
  949. its body cannot be collapsed. If a section does have a heading,
  950. then its height must be exactly one line, including a trailing
  951. newline character. This isn't enforced, you are responsible for
  952. getting it right. The only exception is that this function does
  953. insert a newline character if necessary."
  954. (declare (indent defun))
  955. (when args
  956. (let ((heading (apply #'concat args)))
  957. (insert (if (or (text-property-not-all 0 (length heading)
  958. 'font-lock-face nil heading)
  959. (text-property-not-all 0 (length heading)
  960. 'face nil heading))
  961. heading
  962. (propertize heading 'font-lock-face 'magit-section-heading)))))
  963. (unless (bolp)
  964. (insert ?\n))
  965. (magit-maybe-make-margin-overlay)
  966. (oset magit-insert-section--current content (point-marker)))
  967. (defmacro magit-insert-section-body (&rest body)
  968. "Use BODY to insert the section body, once the section is expanded.
  969. If the section is expanded when it is created, then this is
  970. like `progn'. Otherwise BODY isn't evaluated until the section
  971. is explicitly expanded."
  972. (declare (indent 0))
  973. (let ((f (cl-gensym))
  974. (s (cl-gensym)))
  975. `(let ((,f (lambda () ,@body))
  976. (,s magit-insert-section--current))
  977. (if (oref ,s hidden)
  978. (oset ,s washer
  979. (lambda ()
  980. (funcall ,f)
  981. (magit-section-maybe-remove-visibility-indicator ,s)))
  982. (funcall ,f)))))
  983. (defun magit-insert-headers (hook)
  984. (let* ((header-sections nil)
  985. (magit-insert-section-hook
  986. (cons (lambda ()
  987. (push magit-insert-section--current
  988. header-sections))
  989. (if (listp magit-insert-section-hook)
  990. magit-insert-section-hook
  991. (list magit-insert-section-hook)))))
  992. (magit-run-section-hook hook)
  993. (when header-sections
  994. (insert "\n")
  995. ;; Make the first header into the parent of the rest.
  996. (when (cdr header-sections)
  997. (cl-callf nreverse header-sections)
  998. (let* ((1st-header (pop header-sections))
  999. (header-parent (oref 1st-header parent)))
  1000. (oset header-parent children (list 1st-header))
  1001. (oset 1st-header children header-sections)
  1002. (oset 1st-header content (oref (car header-sections) start))
  1003. (oset 1st-header end (oref (car (last header-sections)) end))
  1004. (dolist (sub-header header-sections)
  1005. (oset sub-header parent 1st-header)))))))
  1006. (defun magit-insert-child-count (section)
  1007. "Modify SECTION's heading to contain number of child sections.
  1008. If `magit-section-show-child-count' is non-nil and the SECTION
  1009. has children and its heading ends with \":\", then replace that
  1010. with \" (N)\", where N is the number of child sections.
  1011. This function is called by `magit-insert-section' after that has
  1012. evaluated its BODY. Admittedly that's a bit of a hack."
  1013. ;; This has to be fast, not pretty!
  1014. (let (content count)
  1015. (when (and magit-section-show-child-count
  1016. (setq count (length (oref section children)))
  1017. (> count 0)
  1018. (setq content (oref section content))
  1019. (eq (char-before (1- content)) ?:))
  1020. (save-excursion
  1021. (goto-char (- content 2))
  1022. (insert (format " (%s)" count))
  1023. (delete-char 1)))))
  1024. ;;; Update
  1025. (defvar-local magit-section-highlight-overlays nil)
  1026. (defvar-local magit-section-highlighted-section nil)
  1027. (defvar-local magit-section-highlighted-sections nil)
  1028. (defvar-local magit-section-unhighlight-sections nil)
  1029. (defun magit-section-update-region (_)
  1030. "When the region is a valid section-selection, highlight them all."
  1031. ;; At least that's what it does conceptually. In actuality it just
  1032. ;; returns a list of those sections, and it doesn't even matter if
  1033. ;; this is a member of `magit-region-highlight-hook'. It probably
  1034. ;; should be removed, but I want to make sure before removing it.
  1035. (magit-region-sections))
  1036. (defun magit-section-update-highlight ()
  1037. (let ((section (magit-current-section)))
  1038. (unless (eq section magit-section-highlighted-section)
  1039. (let ((inhibit-read-only t)
  1040. (deactivate-mark nil)
  1041. (selection (magit-region-sections)))
  1042. (mapc #'delete-overlay magit-section-highlight-overlays)
  1043. (setq magit-section-highlight-overlays nil)
  1044. (setq magit-section-unhighlight-sections
  1045. magit-section-highlighted-sections)
  1046. (setq magit-section-highlighted-sections nil)
  1047. (unless (eq section magit-root-section)
  1048. (run-hook-with-args-until-success
  1049. 'magit-section-highlight-hook section selection))
  1050. (dolist (s magit-section-unhighlight-sections)
  1051. (run-hook-with-args-until-success
  1052. 'magit-section-unhighlight-hook s selection))
  1053. (restore-buffer-modified-p nil)
  1054. (unless (eq magit-section-highlighted-section section)
  1055. (setq magit-section-highlighted-section
  1056. (and (not (oref section hidden))
  1057. section)))))
  1058. (magit-section-maybe-paint-visibility-ellipses)))
  1059. (defun magit-section-highlight (section selection)
  1060. "Highlight SECTION and if non-nil all sections in SELECTION.
  1061. This function works for any section but produces undesirable
  1062. effects for diff related sections, which by default are
  1063. highlighted using `magit-diff-highlight'. Return t."
  1064. (when-let ((face (oref section heading-highlight-face)))
  1065. (dolist (section (or selection (list section)))
  1066. (magit-section-make-overlay
  1067. (oref section start)
  1068. (or (oref section content)
  1069. (oref section end))
  1070. face)))
  1071. (cond (selection
  1072. (magit-section-make-overlay (oref (car selection) start)
  1073. (oref (car (last selection)) end)
  1074. 'magit-section-highlight)
  1075. (magit-section-highlight-selection nil selection))
  1076. (t
  1077. (magit-section-make-overlay (oref section start)
  1078. (oref section end)
  1079. 'magit-section-highlight)))
  1080. t)
  1081. (defun magit-section-highlight-selection (_ selection)
  1082. "Highlight the section-selection region.
  1083. If SELECTION is non-nil, then it is a list of sections selected by
  1084. the region. The headings of these sections are then highlighted.
  1085. This is a fallback for people who don't want to highlight the
  1086. current section and therefore removed `magit-section-highlight'
  1087. from `magit-section-highlight-hook'.
  1088. This function is necessary to ensure that a representation of
  1089. such a region is visible. If neither of these functions were
  1090. part of the hook variable, then such a region would be
  1091. invisible."
  1092. (when (and selection
  1093. (not (and (eq this-command 'mouse-drag-region))))
  1094. (dolist (section selection)
  1095. (magit-section-make-overlay (oref section start)
  1096. (or (oref section content)
  1097. (oref section end))
  1098. 'magit-section-heading-selection))
  1099. t))
  1100. (defun magit-section-make-overlay (start end face)
  1101. ;; Yes, this doesn't belong here. But the alternative of
  1102. ;; spreading this hack across the code base is even worse.
  1103. (when (and magit-keep-region-overlay
  1104. (memq face '(magit-section-heading-selection
  1105. magit-diff-file-heading-selection
  1106. magit-diff-hunk-heading-selection)))
  1107. (setq face (list :foreground (face-foreground face))))
  1108. (let ((ov (make-overlay start end nil t)))
  1109. (overlay-put ov 'font-lock-face face)
  1110. (overlay-put ov 'evaporate t)
  1111. (push ov magit-section-highlight-overlays)
  1112. ov))
  1113. (defun magit-section-goto-successor (section line char arg)
  1114. (let ((ident (magit-section-ident section)))
  1115. (--if-let (magit-get-section ident)
  1116. (let ((start (oref it start)))
  1117. (goto-char start)
  1118. (unless (eq it magit-root-section)
  1119. (ignore-errors
  1120. (forward-line line)
  1121. (forward-char char))
  1122. (unless (eq (magit-current-section) it)
  1123. (goto-char start))))
  1124. (or (and (magit-hunk-section-p section)
  1125. (when-let ((parent (magit-get-section
  1126. (magit-section-ident
  1127. (oref section parent)))))
  1128. (let* ((children (oref parent children))
  1129. (siblings (magit-section-siblings section 'prev))
  1130. (previous (nth (length siblings) children)))
  1131. (if (not arg)
  1132. (--when-let (or previous (car (last children)))
  1133. (magit-section-goto it)
  1134. t)
  1135. (when previous
  1136. (magit-section-goto previous))
  1137. (if (and (stringp arg)
  1138. (re-search-forward arg (oref parent end) t))
  1139. (goto-char (match-beginning 0))
  1140. (goto-char (oref (car (last children)) end))
  1141. (forward-line -1)
  1142. (while (looking-at "^ ") (forward-line -1))
  1143. (while (looking-at "^[-+]") (forward-line -1))
  1144. (forward-line))))))
  1145. (goto-char (--if-let (magit-section-goto-successor-1 section)
  1146. (if (eq (oref it type) 'button)
  1147. (point-min)
  1148. (oref it start))
  1149. (point-min)))))))
  1150. (defun magit-section-goto-successor-1 (section)
  1151. (or (--when-let (pcase (oref section type)
  1152. (`staged 'unstaged)
  1153. (`unstaged 'staged)
  1154. (`unpushed 'unpulled)
  1155. (`unpulled 'unpushed))
  1156. (magit-get-section `((,it) (status))))
  1157. (--when-let (car (magit-section-siblings section 'next))
  1158. (magit-get-section (magit-section-ident it)))
  1159. (--when-let (car (magit-section-siblings section 'prev))
  1160. (magit-get-section (magit-section-ident it)))
  1161. (--when-let (oref section parent)
  1162. (or (magit-get-section (magit-section-ident it))
  1163. (magit-section-goto-successor-1 it)))))
  1164. ;;; Visibility
  1165. (defvar-local magit-section-visibility-cache nil)
  1166. (put 'magit-section-visibility-cache 'permanent-local t)
  1167. (defun magit-section-cached-visibility (section)
  1168. "Set SECTION's visibility to the cached value."
  1169. (cdr (assoc (magit-section-ident section)
  1170. magit-section-visibility-cache)))
  1171. (cl-defun magit-section-cache-visibility
  1172. (&optional (section magit-insert-section--current))
  1173. ;; Emacs 25's `alist-get' lacks TESTFN.
  1174. (let* ((id (magit-section-ident section))
  1175. (elt (assoc id magit-section-visibility-cache))
  1176. (val (if (oref section hidden) 'hide 'show)))
  1177. (if elt
  1178. (setcdr elt val)
  1179. (push (cons id val) magit-section-visibility-cache))))
  1180. (cl-defun magit-section-maybe-cache-visibility
  1181. (&optional (section magit-insert-section--current))
  1182. (when (or (eq magit-section-cache-visibility t)
  1183. (memq (oref section type)
  1184. magit-section-cache-visibility))
  1185. (magit-section-cache-visibility section)))
  1186. (defun magit-preserve-section-visibility-cache ()
  1187. (when (derived-mode-p 'magit-status-mode 'magit-refs-mode)
  1188. (magit-repository-local-set
  1189. (cons major-mode 'magit-section-visibility-cache)
  1190. magit-section-visibility-cache)))
  1191. (defun magit-restore-section-visibility-cache (mode)
  1192. (setq magit-section-visibility-cache
  1193. (magit-repository-local-get
  1194. (cons mode 'magit-section-visibility-cache))))
  1195. (defun magit-section-maybe-update-visibility-indicator (section)
  1196. (when magit-section-visibility-indicator
  1197. (let ((beg (oref section start))
  1198. (cnt (oref section content))
  1199. (end (oref section end)))
  1200. (when (and cnt (or (not (= cnt end)) (oref section washer)))
  1201. (let ((eoh (save-excursion
  1202. (goto-char beg)
  1203. (line-end-position))))
  1204. (cond
  1205. ((symbolp (car-safe magit-section-visibility-indicator))
  1206. ;; It would make more sense to put the overlay only on the
  1207. ;; location we actually don't put it on, but then inserting
  1208. ;; before that location (while taking care not to mess with
  1209. ;; the overlay) would cause the fringe bitmap to disappear
  1210. ;; (but not other effects of the overlay).
  1211. (let ((ov (magit--overlay-at (1+ beg) 'magit-vis-indicator 'fringe)))
  1212. (unless ov
  1213. (setq ov (make-overlay (1+ beg) eoh))
  1214. (overlay-put ov 'evaporate t)
  1215. (overlay-put ov 'magit-vis-indicator 'fringe))
  1216. (overlay-put
  1217. ov 'before-string
  1218. (propertize "fringe" 'display
  1219. (list 'left-fringe
  1220. (if (oref section hidden)
  1221. (car magit-section-visibility-indicator)
  1222. (cdr magit-section-visibility-indicator))
  1223. (face-foreground 'fringe))))))
  1224. ((stringp (car-safe magit-section-visibility-indicator))
  1225. (let ((ov (magit--overlay-at (1- eoh) 'magit-vis-indicator 'eoh)))
  1226. (cond ((oref section hidden)
  1227. (unless ov
  1228. (setq ov (make-overlay (1- eoh) eoh))
  1229. (overlay-put ov 'evaporate t)
  1230. (overlay-put ov 'magit-vis-indicator 'eoh))
  1231. (overlay-put ov 'after-string
  1232. (car magit-section-visibility-indicator)))
  1233. (ov
  1234. (delete-overlay ov)))))))))))
  1235. (defvar-local magit--ellipses-sections nil)
  1236. (defun magit-section-maybe-paint-visibility-ellipses ()
  1237. ;; This is needed because we hide the body instead of "the body
  1238. ;; except the final newline and additionally the newline before
  1239. ;; the body"; otherwise we could use `buffer-invisibility-spec'.
  1240. (when (stringp (car-safe magit-section-visibility-indicator))
  1241. (let* ((sections (append magit--ellipses-sections
  1242. (setq magit--ellipses-sections
  1243. (or (magit-region-sections)
  1244. (list (magit-current-section))))))
  1245. (beg (--map (oref it start) sections))
  1246. (end (--map (oref it end) sections)))
  1247. (when (region-active-p)
  1248. ;; This ensures that the region face is removed from ellipses
  1249. ;; when the region becomes inactive, but fails to ensure that
  1250. ;; all ellipses within the active region use the region face,
  1251. ;; because the respective overlay has not yet been updated at
  1252. ;; this time. The magit-selection face is always applied.
  1253. (push (region-beginning) beg)
  1254. (push (region-end) end))
  1255. (setq beg (apply #'min beg))
  1256. (setq end (apply #'max end))
  1257. (dolist (ov (overlays-in beg end))
  1258. (when (eq (overlay-get ov 'magit-vis-indicator) 'eoh)
  1259. (overlay-put
  1260. ov 'after-string
  1261. (propertize
  1262. (car magit-section-visibility-indicator) 'font-lock-face
  1263. (let ((pos (overlay-start ov)))
  1264. (delq nil (nconc (--map (overlay-get it 'font-lock-face)
  1265. (overlays-at pos))
  1266. (list (get-char-property
  1267. pos 'font-lock-face))))))))))))
  1268. (defun magit-section-maybe-remove-visibility-indicator (section)
  1269. (when (and magit-section-visibility-indicator
  1270. (= (oref section content)
  1271. (oref section end)))
  1272. (dolist (o (overlays-in (oref section start)
  1273. (save-excursion
  1274. (goto-char (oref section start))
  1275. (1+ (line-end-position)))))
  1276. (when (overlay-get o 'magit-vis-indicator)
  1277. (delete-overlay o)))))
  1278. ;;; Utilities
  1279. (cl-defun magit-section-selected-p (section &optional (selection nil sselection))
  1280. (and (not (eq section magit-root-section))
  1281. (or (eq section (magit-current-section))
  1282. (memq section (if sselection
  1283. selection
  1284. (setq selection (magit-region-sections))))
  1285. (--when-let (oref section parent)
  1286. (magit-section-selected-p it selection)))))
  1287. (defun magit-section-parent-value (section)
  1288. (when-let ((parent (oref section parent)))
  1289. (oref parent value)))
  1290. (defun magit-section-siblings (section &optional direction)
  1291. "Return a list of the sibling sections of SECTION.
  1292. If optional DIRECTION is `prev', then return siblings that come
  1293. before SECTION. If it is `next', then return siblings that come
  1294. after SECTION. For all other values, return all siblings
  1295. excluding SECTION itself."
  1296. (when-let ((parent (oref section parent)))
  1297. (let ((siblings (oref parent children)))
  1298. (pcase direction
  1299. (`prev (cdr (member section (reverse siblings))))
  1300. (`next (cdr (member section siblings)))
  1301. (_ (remq section siblings))))))
  1302. (defun magit-region-values (&optional condition multiple)
  1303. "Return a list of the values of the selected sections.
  1304. Return the values that themselves would be returned by
  1305. `magit-region-sections' (which see)."
  1306. (--map (oref it value)
  1307. (magit-region-sections condition multiple)))
  1308. (defun magit-region-sections (&optional condition multiple)
  1309. "Return a list of the selected sections.
  1310. When the region is active and constitutes a valid section
  1311. selection, then return a list of all selected sections. This is
  1312. the case when the region begins in the heading of a section and
  1313. ends in the heading of the same section or in that of a sibling
  1314. section. If optional MULTIPLE is non-nil, then the region cannot
  1315. begin and end in the same section.
  1316. When the selection is not valid, then return nil. In this case,
  1317. most commands that can act on the selected sections will instead
  1318. act on the section at point.
  1319. When the region looks like it would in any other buffer then
  1320. the selection is invalid. When the selection is valid then the
  1321. region uses the `magit-section-highlight' face. This does not
  1322. apply to diffs where things get a bit more complicated, but even
  1323. here if the region looks like it usually does, then that's not
  1324. a valid selection as far as this function is concerned.
  1325. If optional CONDITION is non-nil, then the selection not only
  1326. has to be valid; all selected sections additionally have to match
  1327. CONDITION, or nil is returned. See `magit-section-match' for the
  1328. forms CONDITION can take."
  1329. (when (region-active-p)
  1330. (let* ((rbeg (region-beginning))
  1331. (rend (region-end))
  1332. (sbeg (get-text-property rbeg 'magit-section))
  1333. (send (get-text-property rend 'magit-section)))
  1334. (when (and send
  1335. (not (eq send magit-root-section))
  1336. (not (and multiple (eq send sbeg))))
  1337. (let ((siblings (cons sbeg (magit-section-siblings sbeg 'next)))
  1338. sections)
  1339. (when (and (memq send siblings)
  1340. (magit-section-position-in-heading-p sbeg rbeg)
  1341. (magit-section-position-in-heading-p send rend))
  1342. (while siblings
  1343. (push (car siblings) sections)
  1344. (when (eq (pop siblings) send)
  1345. (setq siblings nil)))
  1346. (setq sections (nreverse sections))
  1347. (when (or (not condition)
  1348. (--all-p (magit-section-match condition it) sections))
  1349. sections)))))))
  1350. (defun magit-section-position-in-heading-p (&optional section pos)
  1351. "Return t if POSITION is inside the heading of SECTION.
  1352. POSITION defaults to point and SECTION defaults to the
  1353. current section."
  1354. (unless section
  1355. (setq section (magit-current-section)))
  1356. (unless pos
  1357. (setq pos (point)))
  1358. (and section
  1359. (>= pos (oref section start))
  1360. (< pos (or (oref section content)
  1361. (oref section end)))
  1362. t))
  1363. (defun magit-section-internal-region-p (&optional section)
  1364. "Return t if the region is active and inside SECTION's body.
  1365. If optional SECTION is nil, use the current section."
  1366. (and (region-active-p)
  1367. (or section (setq section (magit-current-section)))
  1368. (let ((beg (get-text-property (region-beginning) 'magit-section)))
  1369. (and (eq beg (get-text-property (region-end) 'magit-section))
  1370. (eq beg section)))
  1371. (not (or (magit-section-position-in-heading-p section (region-beginning))
  1372. (magit-section-position-in-heading-p section (region-end))))
  1373. t))
  1374. (defun magit-section--backward-protected ()
  1375. "Move to the beginning of the current or the previous visible section.
  1376. Same as `magit-section-backward' but for non-interactive use.
  1377. Suppress `magit-section-movement-hook', and return a boolean to
  1378. indicate whether a section was found, instead of raising an error
  1379. if not."
  1380. (condition-case nil
  1381. (let ((magit-section-movement-hook nil))
  1382. (magit-section-backward)
  1383. t)
  1384. (user-error nil)))
  1385. (defun magit-section--backward-find (predicate)
  1386. "Move to the first previous section satisfying PREDICATE.
  1387. PREDICATE does not take any parameter and should not move
  1388. point."
  1389. (let (found)
  1390. (while (and (setq found (magit-section--backward-protected))
  1391. (not (funcall predicate))))
  1392. found))
  1393. (defun magit-wash-sequence (function)
  1394. "Repeatedly call FUNCTION until it returns nil or eob is reached.
  1395. FUNCTION has to move point forward or return nil."
  1396. (while (and (not (eobp)) (funcall function))))
  1397. (defun magit-add-section-hook (hook function &optional at append local)
  1398. "Add to the value of section hook HOOK the function FUNCTION.
  1399. Add FUNCTION at the beginning of the hook list unless optional
  1400. APPEND is non-nil, in which case FUNCTION is added at the end.
  1401. If FUNCTION already is a member, then move it to the new location.
  1402. If optional AT is non-nil and a member of the hook list, then
  1403. add FUNCTION next to that instead. Add before or after AT, or
  1404. replace AT with FUNCTION depending on APPEND. If APPEND is the
  1405. symbol `replace', then replace AT with FUNCTION. For any other
  1406. non-nil value place FUNCTION right after AT. If nil, then place
  1407. FUNCTION right before AT. If FUNCTION already is a member of the
  1408. list but AT is not, then leave FUNCTION where ever it already is.
  1409. If optional LOCAL is non-nil, then modify the hook's buffer-local
  1410. value rather than its global value. This makes the hook local by
  1411. copying the default value. That copy is then modified.
  1412. HOOK should be a symbol. If HOOK is void, it is first set to nil.
  1413. HOOK's value must not be a single hook function. FUNCTION should
  1414. be a function that takes no arguments and inserts one or multiple
  1415. sections at point, moving point forward. FUNCTION may choose not
  1416. to insert its section(s), when doing so would not make sense. It
  1417. should not be abused for other side-effects. To remove FUNCTION
  1418. again use `remove-hook'."
  1419. (unless (boundp hook)
  1420. (error "Cannot add function to undefined hook variable %s" hook))
  1421. (or (default-boundp hook) (set-default hook nil))
  1422. (let ((value (if local
  1423. (if (local-variable-p hook)
  1424. (symbol-value hook)
  1425. (unless (local-variable-if-set-p hook)
  1426. (make-local-variable hook))
  1427. (copy-sequence (default-value hook)))
  1428. (default-value hook))))
  1429. (if at
  1430. (when (setq at (member at value))
  1431. (setq value (delq function value))
  1432. (cond ((eq append 'replace)
  1433. (setcar at function))
  1434. (append
  1435. (push function (cdr at)))
  1436. (t
  1437. (push (car at) (cdr at))
  1438. (setcar at function))))
  1439. (setq value (delq function value)))
  1440. (unless (member function value)
  1441. (setq value (if append
  1442. (append value (list function))
  1443. (cons function value))))
  1444. (when (eq append 'replace)
  1445. (setq value (delq at value)))
  1446. (if local
  1447. (set hook value)
  1448. (set-default hook value))))
  1449. (defun magit-run-section-hook (hook &rest args)
  1450. "Run HOOK with ARGS, warning about invalid entries."
  1451. (let ((entries (symbol-value hook)))
  1452. (unless (listp entries)
  1453. (setq entries (list entries)))
  1454. (--when-let (-remove #'functionp entries)
  1455. (message "`%s' contains entries that are no longer valid.
  1456. %s\nUsing standard value instead. Please re-configure hook variable."
  1457. hook
  1458. (mapconcat (lambda (sym) (format " `%s'" sym)) it "\n"))
  1459. (sit-for 5)
  1460. (setq entries (eval (car (get hook 'standard-value)))))
  1461. (dolist (entry entries)
  1462. (let ((magit--current-section-hook (cons (list hook entry)
  1463. magit--current-section-hook)))
  1464. (apply entry args)))))
  1465. ;;; _
  1466. (provide 'magit-section)
  1467. ;;; magit-section.el ends here