Many commercial word processors have a default behavior that automatically hides the vertical scroll bars when the lines of text in the document are less than the visible window; and, the scroll bars appear automatically when the lines of text in the document are greater than the visible window. Emacs is missing that ability and I'd like that to be the default behavior in a variety of different modes.
Does anyone have some ideas how to automatically remove or add the scroll-bars on the fly as the size (lines of text) of the document increases or decreases?
I was thinking of perhaps weaving it into the the functions for line numbers using a prior sample by #phils in a related thread: https://stackoverflow.com/a/10593165/2112489
I'd like it work even when I'm not using linum-mode. However, I don't think the scroll-bar function should run after every single command -- it should run only when a new line is added or subtracted, taking into consideration visual-line-mode (i.e., wrapping being active) potentially being active.
The following snippet was inspired by a previous answer from #abo-abo in a related thread: https://stackoverflow.com/a/20923695/2112489
(cond
((not (> (count-lines (point-min) (point-max)) (window-height)))
(set-window-scroll-bars (get-buffer-window (buffer-name) (selected-frame)) 0 nil))
((and
(> (count-lines (point-min) (point-max)) (window-height))
(not (equal (window-scroll-bars) `(15 2 t nil))))
(set-window-scroll-bars (get-buffer-window (buffer-name) (selected-frame)) 15 'right)))
EDIT (January 17, 2014): Working draft based upon the helpful answer to this thread by #Drew.
EDIT (January 19, 2014): Added a function to count each word-wrapped line using vertical-motion. Setting the initial-frame-default seems to be read by Emacs after the initial frame is created, so the scroll bars are visible for a split second -- to avoid seeing this, modifying the frame parameters of the initial frame seems to fix this visual issue. Now using window-text-height instead of window-height -- The returned height does not include dividers, the mode line, any header line, nor any partial-height lines at the bottom of the text area. Copied the method used by linum-mode in terms of using -- the post-command-hook, the change-major-mode-hook, and the window-configuration-change-hook). Added window-live-p condition to avoid post-command-hook errors when starting Emacs while various buffers are loading out of sight. Added condition to deal with narrow-to-region -- still unsure why that situation causes Emacs to freeze in a loop or something -- the workaround is needed for now. The latest version of Emacs Trunk from January 19, 2014 appears to fix visual display issues experienced in prior versions -- as such, redraw-frame is no longer necessary. Added (redisplay t) to the function count-vertical-lines, which speeds up displaying the new buffer when switching buffers. Added regexp for buffers that will always have scroll bars or never have scroll bars.
EDIT (January 20, 2014): Added just one main condition that there be a live window, and removed the same conditions from the various branches of the lawlist-scroll-bar function. Added additional condition for a narrow-to-region situation such that removing the scroll bars only need occur if scroll bars were present prior to narrowing.
EDIT (January 21, 2014): With this revision, it is no longer necessary to count lines (which causes a slow-down in large buffers). The new method is a much simpler mathematical calculation based on four (4) points that are determined in a mere fraction of a second -- i.e., point-min, point-max, window-start and window-end. If point-min moves off the screen, scroll bars are added -- I think this behavior makes sense -- although, I did stop to ponder whether the scroll-bar should also serve as a visual representation of whether the characters within the parameters of point-min to point-max could actually fit into the window regardless of whether point-min had moved beyond the window. None of the hooks in this example are able to deal with a display-buffer situation (targeting the same window of the same frame that both already have focus) -- so, I created my own display-buffer-hook (which is beyond the scope of this example).
;;;;;;;;;;;;;;;;;;;;;;;;;; LAWLIST SCROLL BAR MODE ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defvar regexp-always-scroll-bar '("\\.yes" "\\*Scroll-Bar\\*")
"Regexp matching buffer names that will always have scroll bars.")
(defvar regexp-never-scroll-bar '("\\.off" "\\.not")
"Regexp matching buffer names that will never have scroll bars.")
(add-to-list 'default-frame-alist '(vertical-scroll-bars . nil))
(modify-all-frames-parameters (list (cons 'vertical-scroll-bars nil)))
(defun lawlist-scroll-bar ()
(when (window-live-p (get-buffer-window (current-buffer)))
(redisplay t)
(cond
;; not regexp matches | not narrow-to-region
((and
(not (regexp-match-p regexp-always-scroll-bar (buffer-name)))
(not (regexp-match-p regexp-never-scroll-bar (buffer-name)))
(equal (- (point-max) (point-min)) (buffer-size)))
(cond
;; Lines of text are less-than or equal-to window height,
;; and scroll bars are present (which need to be removed).
((and
(<= (- (point-max) (point-min)) (- (window-end) (window-start)))
(equal (window-scroll-bars) `(15 2 right nil)))
(set-window-scroll-bars (selected-window) 0 'right nil))
;; Lines of text are greater-than window height, and
;; scroll bars are not present and need to be added.
((and
(> (- (point-max) (point-min)) (- (window-end) (window-start)))
(not (equal (window-scroll-bars) `(15 2 right nil))))
(set-window-scroll-bars (selected-window) 15 'right nil))))
;; Narrow-to-region is active, and scroll bars are present
;; (which need to be removed).
((and
(not (equal (- (point-max) (point-min)) (buffer-size)))
(equal (window-scroll-bars) `(15 2 right nil)))
(set-window-scroll-bars (selected-window) 0 'right nil))
;; not narrow-to-region | regexp always scroll-bars
((and
(equal (- (point-max) (point-min)) (buffer-size))
(regexp-match-p regexp-always-scroll-bar (buffer-name)))
(set-window-scroll-bars (selected-window) 15 'right nil))
;; not narrow-to-region | regexp never scroll-bars
((and
(equal (- (point-max) (point-min)) (buffer-size))
(regexp-match-p regexp-never-scroll-bar (buffer-name)))
(set-window-scroll-bars (selected-window) 0 'right nil)))))
(define-minor-mode lawlist-scroll-bar-mode
"This is a custom scroll bar mode."
:lighter " sc"
(if lawlist-scroll-bar-mode
(progn
(add-hook 'post-command-hook 'lawlist-scroll-bar nil t)
;; (add-hook 'change-major-mode-hook 'lawlist-scroll-bar nil t)
;; (add-hook 'window-configuration-change-hook 'lawlist-scroll-bar nil t)
)
(remove-hook 'post-command-hook 'lawlist-scroll-bar t)
(remove-hook 'change-major-mode-hook 'lawlist-scroll-bar t)
(remove-hook 'window-configuration-change-hook 'lawlist-scroll-bar t)))
(define-globalized-minor-mode global-lawlist-scroll-bar-mode
lawlist-scroll-bar-mode lawlist-scroll-bar-on)
(defun lawlist-scroll-bar-on ()
(unless (minibufferp)
(lawlist-scroll-bar-mode 1)))
(global-lawlist-scroll-bar-mode)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Supporting regexp function:
;; https://github.com/kentaro/auto-save-buffers-enhanced
;; `regexp-match-p` function modified by #sds on stackoverflow
;; https://stackoverflow.com/questions/20343048/distinguishing-files-with-extensions-from-hidden-files-and-no-extensions
(defun regexp-match-p (regexps string)
(and string
(catch 'matched
(let ((inhibit-changing-match-data t)) ; small optimization
(dolist (regexp regexps)
(when (string-match regexp string)
(throw 'matched t)))))))
If you just want to toggle scroll bars on/off interactively, or on a hook, or from your code, then scroll-bar-mode should be all you need.
You can also use menu-bar-no-scroll-bar, menu-bar-left-scroll-bar, and menu-bar-right-scroll-bar. Or just do what each of those commands does: (customize-set-variable 'scroll-bar-mode WHATEVER). Or use set-scroll-bar-mode or set-window-scroll-bars, similarly. It depends on what behavior you are looking for.
I recommend M-x apropos scroll-bar. (Or if you use Icicles, just C-h f scroll-bar S-TAB, then repeat C-M-down...)
You can add it to mode-line-position, so that update of the mode line automatically triggers turning scroll bars on/off. This pretty much works, for instance:
(setq-default
mode-line-position
'(:eval
(progn
(if (> (count-lines (point-min) (point-max)) (window-height))
(set-window-scroll-bars nil 20 t)
(set-window-scroll-bars nil 0 t))
`((-3 ,(propertize
"%p"
'local-map mode-line-column-line-number-mode-map
'mouse-face 'mode-line-highlight
'help-echo "Buffer position, mouse-1: Line/col menu"))
(line-number-mode
((column-number-mode
(10 ,(propertize
" (%l,%c)"
'face (and (> (current-column)
modelinepos-column-limit)
'modelinepos-column-warning)
'local-map mode-line-column-line-number-mode-map
'mouse-face 'mode-line-highlight
'help-echo "Line and column, mouse-1: Line/col menu"))
(6 ,(propertize
" L%l"
'local-map mode-line-column-line-number-mode-map
'mouse-face 'mode-line-highlight
'help-echo "Line number, mouse-1: Line/col menu"))))
((column-number-mode
(5 ,(propertize
" C%c"
'face (and (> (current-column)
modelinepos-column-limit)
'modelinepos-column-warning)
'local-map mode-line-column-line-number-mode-map
'mouse-face 'mode-line-highlight
'help-echo "Column number, mouse-1: Line/col menu")))))))))
You can alternatively use the following, which employs Stefan's suggestion
to make it work better with scaled text, visual-line-mode, images, etc. However, in that case, scroll bars kick in whenever some text is outside the window because of scrolling, regardless of whether that text would fit in the window. Whether that is a feature or not is for you to decide. ;-)
(setq-default
mode-line-position
'(:eval
(let ((scroll-bars (nth 2 (window-scroll-bars))))
(if (or (> (point-max) (window-end)) (< (point-min) (window-start)))
(unless scroll-bars (set-window-scroll-bars nil 20 t))
(when scroll-bars (set-window-scroll-bars nil 0 t)))
(unless (equal scroll-bars (nth 2 (window-scroll-bars))) (redraw-frame))
`((-3 ,(propertize
"%p"
'local-map mode-line-column-line-number-mode-map
'mouse-face 'mode-line-highlight
'help-echo "Buffer position, mouse-1: Line/col menu"))
(line-number-mode
((column-number-mode
(10 ,(propertize
" (%l,%c)"
'face (and (> (current-column)
modelinepos-column-limit)
'modelinepos-column-warning)
'local-map mode-line-column-line-number-mode-map
'mouse-face 'mode-line-highlight
'help-echo "Line and column, mouse-1: Line/col menu"))
(6 ,(propertize
" L%l"
'local-map mode-line-column-line-number-mode-map
'mouse-face 'mode-line-highlight
'help-echo "Line number, mouse-1: Line/col menu"))))
((column-number-mode
(5 ,(propertize
" C%c"
'face (and (> (current-column)
modelinepos-column-limit)
'modelinepos-column-warning)
'local-map mode-line-column-line-number-mode-map
'mouse-face 'mode-line-highlight
'help-echo "Column number, mouse-1: Line/col menu")))))))))
If you are used to using scroll bars only as visual indicator of where you are in the buffer (rather than looking at the modeline for that), you might like a package like yascroll. It uses the fringe for displaying the scroll bar, and it does not show it if lines of text in the document are less than the visible window, as you want.
Advantage is that there is no widgets taking up real state from your screen.
Disadvantage is that you cannot use the scroll bars with the mouse (some users never do that, anyways)
Related
Emacs (and all other text editors) by default show blank space below the bottom lines of a buffer. I want emacs to be able to also scroll above/show blank space like this above the top lines in a buffer as well so that the top lines can be viewed in the center of the screen for small files.
With guidance from Thomas's answer here I have created a minor mode for this which is now available on MELPA:
TopSpace - Recenter line 1 with scrollable upper margin/padding
I've started a small minor-mode to accomplish this. However, as first versions go, it's probably buggy, doesn't handle all edge cases (such as e.g., narrowing), and isn't particularly efficient.
Thus, I'd be happy to accept improvements: feel free to directly edit this answer if you can make this code better or to extend it.
(defvar vertical-center-num-buffers 0
"The number of buffers in which `vertical-center-mode' is activated.")
(define-minor-mode vertical-center-mode
"This minor mode displays the contents of a buffer vertically
centered with respect to the window height. This, of course, only
makes sense for buffers whose content is shorter than the window
height."
nil
" vc"
nil
;; is the mode being turned on or off?
(if vertical-center-mode
;; on
(progn
;; keep track of the number of lines in the buffer
(setq-local vertical-center-num-lines (count-lines (point-min) (point-max)))
;; use an overlay to display empty lines at the beginning of the buffer
(setq-local vertical-center-overlay (make-overlay (point-min) (point-max)))
;; initial call to the function that centers the buffer contents
(vertical-center--lines-changed 0)
;; react to changes to the buffer or the window
(add-hook 'kill-buffer-hook 'vertical-center--kill-buffer)
(add-hook 'window-size-change-functions 'vertical-center--window-size-changed)
(when (= vertical-center-num-buffers 0)
(add-hook 'before-change-functions 'vertical-center--before-change)
(add-hook 'after-change-functions 'vertical-center--after-change))
;; this is just to play nice and remove the above hook
;; functions when they're no longer needed. Let's keep our
;; fingers crossed that we'll always stay in sync.
(setq vertical-center-num-buffers (1+ vertical-center-num-buffers)))
;; off
;; delete/unset data structures when the mode is turned off
(delete-overlay vertical-center-overlay)
(makunbound 'vertical-center-num-lines)
(makunbound 'vertical-center-overlay)
(setq vertical-center-num-buffers (1- vertical-center-num-buffers))
;; remove hook functions when they're no longer needed
(when (= vertical-center-num-buffers 0)
(remove-hook 'kill-buffer-hook 'vertical-center--kill-buffer)
(remove-hook 'window-size-change-functions 'vertical-center--window-size-changed)
(remove-hook 'before-change-functions 'vertical-center--before-change)
(remove-hook 'after-change-functions 'vertical-center--after-change))))
;; handle killing of buffers
(defun vertical-center--kill-buffer ()
(when vertical-center-mode
(setq vertical-center-num-buffers (1- vertical-center-num-buffers))))
;; react to changes in the window height
(defun vertical-center--window-size-changed (arg)
(vertical-center--lines-changed 0))
;; handle deletions of buffer text
(defun vertical-center--before-change (beginning end)
(when (boundp 'vertical-center-num-lines)
(let ((num-lines 0))
(while (< beginning end)
(when (= (char-after beginning) ?\n)
(setq num-lines (1- num-lines)))
(setq beginning (1+ beginning)))
(when (< num-lines 0)
(vertical-center--lines-changed num-lines)))))
;; handle insertions into the buffer
(defun vertical-center--after-change (beginning end previous-length)
(when (boundp 'vertical-center-num-lines)
(let ((num-lines 0))
(while (< beginning end)
(when (= (char-after beginning) ?\n)
(setq num-lines (1+ num-lines)))
(setq beginning (1+ beginning)))
(when (> num-lines 0)
(vertical-center--lines-changed num-lines)))))
;; update the display when either the buffer content or the window
;; height has changed
(defun vertical-center--lines-changed (num-lines)
(setq vertical-center-num-lines (+ vertical-center-num-lines num-lines))
(let ((top-margin (/ (- (window-height) vertical-center-num-lines) 2)))
;; set the top margin
(overlay-put vertical-center-overlay 'before-string
(when (> top-margin 0)
(make-string top-margin ?\n)))))
Save the above code in a file named "vertical-center.el" in a directory of your choice, and then add the following lines to your .emacs file:
(setq load-path (append load-path "<directory>"))
(autoload 'vertical-center-mode "vertical-center")
Here, <directory> should be the path to the directory in which you saved the "vertical-center.el" file.
After restarting Emacs, you can now activate or deactivate the mode by typing M-x vertical-center-mode.
I do have an Emacs command which adds a separator line, in log files, when there is more than 3 seconds between timestamps found on 2 consecutive lines:
(defun log-mode-display-separator-line ()
"Separate blocks of log lines with an horizontal line."
(interactive)
(save-excursion
(goto-char (point-min))
(let (last ov)
(while (re-search-forward "[0-9]\\{4\\}-[01][1-9]-[0-3][0-9] [0-2][0-9]:[0-5][0-9]:[0-5][0-9]" nil t)
(when last
(when (> (- (float-time (date-to-time (match-string 0)))
(float-time (date-to-time last)))
3)
(setq ov (make-overlay (line-beginning-position) (line-end-position)))
(overlay-put ov 'face 'log-mode-separator-face)))
(setq last (match-string 0))))))
The log-mode-separator-face uses the overline attribute to visually put an horizontal line between "semantic" blocks of lines.
I would like to get that horizontal line automatically inserted when the buffer is reverted (every 5 seconds), but the processing may become too big on huge log files, which are constantly updated. As of now, the whole file is scanned from the beginning each time we call that command...
How can I realize this, then? (With Emacs, no doubt it is...)
First of all, there's no guarantee that the buffer is reverted every 5 seconds. Since Emacs 24.4, autorevert uses file notifications, that means the buffer is reverted whenever the file changes on disk. If you want to suppress this, set auto-revert-use-notify to nil.
Further on, it depends how you want to revert your log file. I suppose you use auto-revert-tail-mode. This mode calls the hook functions contained in before-revert-hook and after-revert-hook. As a strategy, I would write a function for after-revert-hook, which underlines the last line in your buffer to be reverted. Something like this:
(defun my-auto-revert-tail-mode-display-separator-line ()
"Separate blocks of log lines with an horizontal line."
(save-excursion
(goto-char (point-max))
(while (and (= (line-beginning-position) (line-end-position))
(> (point) (point-min)))
(forward-line -1))
(let ((ov (make-overlay (line-beginning-position) (line-end-position))))
(overlay-put ov 'face 'underline))))
(add-hook 'after-revert-hook 'my-auto-revert-tail-mode-display-separator-line)
Any ideas regarding how to configure Wanderlust to open emails in a new buffer, instead of a split window? There is just too much happening with four (4) windows -- summary message buffer; message buffer; big brother insidious database address manager; and the mini-buffer. (See the screenshot below.)
I think I've located the relevant portion of the code in wl-message.el, but I'm not sure what portion to change. (setq wl-message-window-size '(1 . 1)) in the init.el is not what I'm looking for, because I'd still have to share the screen with other buffers.
I'm spending a lot of time switching and closing windows to get a full size buffer, and I'm hoping to just eliminate multiple windows when checking my e-mail.
Thanks . . . any help would be greatly appreciated.
;;; wl-message.el -- Message buffer handling from summary buffer.
(defun wl-message-buffer-window ()
"Get message buffer window if any."
(let* ((start-win (selected-window))
(cur-win start-win))
(catch 'found
(while (progn
(setq cur-win (next-window cur-win))
(with-current-buffer (window-buffer cur-win)
(if (or (eq major-mode 'wl-message-mode)
(eq major-mode 'mime-view-mode))
(throw 'found cur-win)))
(not (eq cur-win start-win)))))))
(defun wl-message-select-buffer (buffer)
"Select BUFFER as a message buffer."
(let ((window (get-buffer-window buffer))
(sum (car wl-message-window-size))
(mes (cdr wl-message-window-size))
whi)
(when (and window
(not (eq (with-current-buffer (window-buffer window)
wl-message-buffer-cur-summary-buffer)
(current-buffer))))
(delete-window window)
(run-hooks 'wl-message-window-deleted-hook)
(setq window nil))
(if window
(select-window window)
(when wl-fixed-window-configuration
(delete-other-windows)
(and wl-stay-folder-window
(wl-summary-toggle-disp-folder)))
;; There's no buffer window. Search for message window and snatch it.
(if (setq window (wl-message-buffer-window))
(select-window window)
(setq whi (1- (window-height)))
(if mes
(progn
(let ((total (+ sum mes)))
(setq sum (max window-min-height (/ (* whi sum) total)))
(setq mes (max window-min-height (/ (* whi mes) total))))
(if (< whi (+ sum mes))
(enlarge-window (- (+ sum mes) whi)))))
(split-window (get-buffer-window (current-buffer)) sum)
(other-window 1)))
(switch-to-buffer buffer)))
(source: lawlist.com)
SOLUTION # 1: The variable wl-message-buffer-name that is defined within wl-vars.el makes the message buffer uninteresting by virtue of the space at the beginning of the buffer name: *WL:Message*. Inasmuch as Emacs hides uninteresting buffers by default, one solution is to modify the aforementioned variable by removing the space at the beginning of the buffer name. That way, the buffer is always visible and it is just a matter of switching to the window that displays the message and then delete-other-windows. The added advantage is that it is no longer necessary to return to the summary buffer to view an e-mail that has already been opened, since there is already an open buffer dedicated to said e-mail.
(defcustom wl-message-buffer-name "*WL:Message*" ;; " *WL:Message*"
"*Buffer name for message buffers."
:group 'wl-pref
:group 'wl-setting)
SOLUTION # 2: This second solution is no longer the preferred method and will most likely be removed entirely from this answer. The word search utility I was using could not comb the wl-vars.el file for some unknown reason, so this second solution was a workaround.
Edit the key map for wl-summary-read (inside wl-summary.el) and replace it with the function lawlist-wl-summary-read so that hitting the space bar activates said function. wl-summary-enter-handler remains defined as the enter key, which opens messages in the default manner.
(define-key wl-summary-mode-map " " 'lawlist-wl-summary-read)
(defun lawlist-wl-summary-read nil
"Clone the the buffer and make new buffer name unique."
(interactive)
(wl-summary-enter-handler)
(windmove-down)
(let ((n 0)
bufname)
(while (progn
(setq bufname (concat "email"
(if (= n 0) "" (int-to-string n))
"")) ;; could be an ending, like an asterick *
(setq n (1- n)) ;; if + instead of -, then no hyphen and no space between buffer name and the number
(get-buffer bufname)))
(clone-indirect-buffer bufname nil)
(switch-to-buffer bufname)
(delete-other-windows) ))
In Emacs org-mode, is there a way to get inline footnote definitions to appear as collapsed?
So that for instance, a line like this:
This effect is due to the strength of weak ties[fn:: Newman, Mark, Albert-László Barabási, and Duncan J. Watts. 2006. The Structure and Dynamics of Networks. Princeton, NJ: Princeton University Press].
might simply appear like this:
This effect is due to the strength of weak ties[✭].
I would also need a command to show the footnotes when necessary. So maybe what is needed are two commands: org-hide-footnotes and org-show-footnotes.
I don't believe this is possible at the moment. Also using TAB as the expansion key within for it would likely cause even more overloading of the key.
On the other hand, is there any particular reason not to use a footnote section for the footnotes?
C-c C-x f will create/interact with any footnotes you have.
(org-footnote-action &optional SPECIAL)
Do the right thing for footnotes.
When at a footnote reference, jump to the definition.
When at a definition, jump to the references if they exist, offer to
create them otherwise.
When neither at definition or reference, create a new footnote,
interactively.
With prefix arg SPECIAL, offer additional commands in a menu.
The additional commands being:
s Sort the footnote definitions by reference sequence. During editing,
Org makes no effort to sort footnote definitions into a particular
sequence. If you want them sorted, use this command, which will
also move entries according to org-footnote-section. Automatic
sorting after each insertion/deletion can be configured using the
variable org-footnote-auto-adjust.
r Renumber the simple fn:N footnotes. Automatic renumbering
after each insertion/deletion can be configured using the variable
org-footnote-auto-adjust.
S Short for first r, then s action.
n Normalize the footnotes by collecting all definitions (including
inline definitions) into a special section, and then numbering them
in sequence. The references will then also be numbers. This is
meant to be the final step before finishing a document (e.g., sending
off an email). The exporters do this automatically, and so could
something like message-send-hook.
d Delete the footnote at point, and all definitions of and references
to it.
INITIAL (February 6, 2014): First working draft.
EDIT February 18, 2014: Revised the function lawlist-toggle-block-visibility so that it contains a proper if/then/else statement -- i.e., if the line contains the requisite beginning region regexp, then the block visibility will be toggled, else a message saying sorry . . . . Added a citation to a related thread for code folding. Revised the error message to refer to a point rather than a line.
The source code for the answer below is also stored on Github: https://github.com/lawlist/lawlist-org-block-toggle/blob/master/lawlist-org-block-toggle.el
On a related issue (i.e., to completely hide the properties drawer including the word :PROPERTIES:), please refer to the following thread: Completely hide the :PROPERTIES: drawer in org-mode
On a semi-related issue (i.e., to create a custom block to be code-folded), see also: https://tex.stackexchange.com/a/161196/26911
This solution was tested with a fairly recent version of Emacs Trunk (built on January 19, 2014), which contains org-mode version 8.2.5c. Inasmuch as the :PROPERTIES: drawer through its :END: gets folded separately from the footnotes and the html blocks, this solution contemplates that the footnote and/or html code blocks will not be anywhere inside that properties drawer. The footnote can appear anywhere in the text paragraph, but cannot have another pair of square brackets inside the footnote -- since this code looks for the first ending square bracket in order to mark the end of the folded region. This code contemplates that #+BEGIN_HTML and #+END_HTML will both be flush-left with the left-hand margin. The startup views still work the same way -- e.g., the variables org-startup-folded and org-hide-block-startup.
The modification of org-cycle-internal-local enables tab cycling for all forms of folding in org-mode. All we did was fix the pagination of the function to make it more readable, and added the following condition: ((eq org-cycle-subtree-status 'subtree) (org-show-subtree) (message "ALL") (setq org-cycle-subtree-status 'all)). The interactive function org-cycle is still used to tab cycle between all of the various folded / unfolded views. The function lawlist-block-org-cycle-internal-local is a non-interactive supporting function that is used by org-cycle. The two defalias portions of the code in this solution are needed for everything to work properly. Rather than using tab cycling, the user can also call the interactive function directly at the beginning of the headings or subheadings with: M-x org-cycle RET
To toggle visibility of the footnote or html block directly, we use the interactive function lawlist-toggle-block-visibility. Place the cursor anywhere on the line containing the beginning of the footnote or, the beginning of the html block, and type: M-x lawlist-toggle-block-visibility RET
(require 'org)
(defalias 'org-cycle-hide-drawers 'lawlist-block-org-cycle-hide-drawers)
(defun lawlist-block-org-cycle-hide-drawers (state)
"Re-hide all drawers, footnotes or html blocks after a visibility state change."
(when
(and
(derived-mode-p 'org-mode)
(not (memq state '(overview folded contents))))
(save-excursion
(let* (
(globalp (memq state '(contents all)))
(beg (if globalp (point-min) (point)))
(end
(cond
(globalp
(point-max))
((eq state 'children)
(save-excursion (outline-next-heading) (point)))
(t (org-end-of-subtree t)) )))
(goto-char beg)
(while
(re-search-forward
".*\\[fn\\|^\\#\\+BEGIN_HTML.*$\\|^[ \t]*:PROPERTIES:[ \t]*$" end t)
(lawlist-org-flag t))))))
(defalias 'org-cycle-internal-local 'lawlist-block-org-cycle-internal-local)
(defun lawlist-block-org-cycle-internal-local ()
"Do the local cycling action."
(let ((goal-column 0) eoh eol eos has-children children-skipped struct)
(save-excursion
(if (org-at-item-p)
(progn
(beginning-of-line)
(setq struct (org-list-struct))
(setq eoh (point-at-eol))
(setq eos (org-list-get-item-end-before-blank (point) struct))
(setq has-children (org-list-has-child-p (point) struct)))
(org-back-to-heading)
(setq eoh (save-excursion (outline-end-of-heading) (point)))
(setq eos (save-excursion (1- (org-end-of-subtree t t))))
(setq has-children
(or
(save-excursion
(let ((level (funcall outline-level)))
(outline-next-heading)
(and
(org-at-heading-p t)
(> (funcall outline-level) level))))
(save-excursion
(org-list-search-forward (org-item-beginning-re) eos t)))))
(beginning-of-line 2)
(if (featurep 'xemacs)
(while
(and
(not (eobp))
(get-char-property (1- (point)) 'invisible))
(beginning-of-line 2))
(while
(and
(not (eobp))
(get-char-property (1- (point)) 'invisible))
(goto-char (next-single-char-property-change (point) 'invisible))
(and
(eolp)
(beginning-of-line 2))))
(setq eol (point)))
(cond
((= eos eoh)
(unless (org-before-first-heading-p)
(run-hook-with-args 'org-pre-cycle-hook 'empty))
(org-unlogged-message "EMPTY ENTRY")
(setq org-cycle-subtree-status nil)
(save-excursion
(goto-char eos)
(outline-next-heading)
(if (outline-invisible-p)
(org-flag-heading nil))))
((and
(or
(>= eol eos)
(not (string-match "\\S-" (buffer-substring eol eos))))
(or
has-children
(not (setq children-skipped
org-cycle-skip-children-state-if-no-children))))
(unless (org-before-first-heading-p)
(run-hook-with-args 'org-pre-cycle-hook 'children))
(if (org-at-item-p)
;; then
(org-list-set-item-visibility (point-at-bol) struct 'children)
;; else
(org-show-entry)
(org-with-limited-levels (show-children))
(when (eq org-cycle-include-plain-lists 'integrate)
(save-excursion
(org-back-to-heading)
(while (org-list-search-forward (org-item-beginning-re) eos t)
(beginning-of-line 1)
(let* (
(struct (org-list-struct))
(prevs (org-list-prevs-alist struct))
(end (org-list-get-bottom-point struct)))
(mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
(org-list-get-all-items (point) struct prevs))
(goto-char (if (< end eos) end eos)))))))
(org-unlogged-message "CHILDREN")
(save-excursion
(goto-char eos)
(outline-next-heading)
(if (outline-invisible-p)
(org-flag-heading nil)))
(setq org-cycle-subtree-status 'children)
(unless (org-before-first-heading-p)
(run-hook-with-args 'org-cycle-hook 'children)))
((or
children-skipped
(and
(eq last-command this-command)
(eq org-cycle-subtree-status 'children)))
(unless (org-before-first-heading-p)
(run-hook-with-args 'org-pre-cycle-hook 'subtree))
(outline-flag-region eoh eos nil)
(org-unlogged-message
(if children-skipped
"SUBTREE (NO CHILDREN)"
"SUBTREE"))
(setq org-cycle-subtree-status 'subtree)
(unless (org-before-first-heading-p)
(run-hook-with-args 'org-cycle-hook 'subtree)))
((eq org-cycle-subtree-status 'subtree)
(org-show-subtree)
(message "ALL")
(setq org-cycle-subtree-status 'all))
(t
(run-hook-with-args 'org-pre-cycle-hook 'folded)
(outline-flag-region eoh eos t)
(org-unlogged-message "FOLDED")
(setq org-cycle-subtree-status 'folded)
(unless (org-before-first-heading-p)
(run-hook-with-args 'org-cycle-hook 'folded))))))
(defun lawlist-org-flag (flag)
"When FLAG is non-nil, hide any of the following: html code block;
footnote; or, the properties drawer. Otherwise make it visible."
(save-excursion
(beginning-of-line 1)
(cond
((looking-at ".*\\[fn")
(let* (
(begin (match-end 0))
end-footnote)
(if (re-search-forward "\\]"
(save-excursion (outline-next-heading) (point)) t)
(progn
(setq end-footnote (point))
(outline-flag-region begin end-footnote flag))
(user-error "Error beginning at point %s." begin))))
((looking-at "^\\#\\+BEGIN_HTML.*$\\|^[ \t]*:PROPERTIES:[ \t]*$")
(let* ((begin (match-end 0)))
(if (re-search-forward "^\\#\\+END_HTML.*$\\|^[ \t]*:END:"
(save-excursion (outline-next-heading) (point)) t)
(outline-flag-region begin (point-at-eol) flag)
(user-error "Error beginning at point %s." begin)))))))
(defun lawlist-toggle-block-visibility ()
"For this function to work, the cursor must be on the same line as the regexp."
(interactive)
(if
(save-excursion
(beginning-of-line 1)
(looking-at
".*\\[fn\\|^\\#\\+BEGIN_HTML.*$\\|^[ \t]*:PROPERTIES:[ \t]*$"))
(lawlist-org-flag (not (get-char-property (match-end 0) 'invisible)))
(message "Sorry, you are not on a line containing the beginning regexp.")))
Is there any way to move point to the first currently visible character within a buffer? I would like to find this point because for a function it would really be faster to look from "first visible character" instead of "beginning of buffer".
EDIT: Actually, obtaining the value of the position would also be fine instead of moving the point actually there. PageUp and PageDown seem to have some weird behavior with this with big pieces of text.
If you want the point the current window is scrolled to:
(window-start)
If you want to skip over text that is hidden by text attributes, use invisible-p to skip past them:
(let ((pos (window-start)))
(while (and (invisible-p pos) (< pos (point-max)))
(setq pos (1+ pos)))
pos)
nschum gives the answer to obtaining the position value by: (window-start)
For moving point to this value use:
Based on nschum's answer:
(defun goto-window-start ()
(interactive)
(let ((pos (window-start)))
(while (and (invisible-p pos) (< pos (point-max) )
(setq pos (1+ pos)))
(goto-char pos))
)
Update: Handles empty buffer.
(defun goto-first-visible ()
(interactive)
(goto-char (point-min))
(save-match-data
(let ((pos (search-forward-regexp (rx graphic) nil t)))
(when pos
(goto-char (- pos 1))))))
Here is an efficient version that may work better for large chunks of invisible text. The hack here is to use the fact that invisible text is controlled by text properties, and emacs has built-in facilities for determining locations of text property changes:
(defun goto-first-visible ()
(interactive)
(goto-char (point-min))
(while (and (not (eobp)) (invisible-p (point)))
(goto-char (next-char-property-change (point)))))