This query is two questions both related to latex major mode (not Auctex), outline minor mode, code folding. I eventually solved these questions myself. My "answers are given below the ========== line.
Question 1:
I am using the following initialization so that each latex file has its body hidden (i.e. folded) when it is first opened. However, the lower level headings are still showing. How do I prevent the lower level headings from showing?
(add-hook 'latex-mode-hook
(lambda ()
(outline-minor-mode 1)
(setq outline-regexp "%{{{{\\|%{{{\\|%{{\\|%{")
(outline-hide-body)))
Question 2:
I want the following keybinding to be effective if and only if latex-major-mode is active (assumes that outline minor mode will also be active):
a. C-M-mouse-1 should toggle outline-show-entry and outline-hide-entry, depending on whether entry is currently hidden.
b. C-M-mouse-3 should invoke (outline-hide-sublevels 2).
=======================================================
Answer to Question 1:
The outline-hide-sublevels function was malfunctioning. Documentation indicated that I should code my own function and refer to it with the outline-level variable. I changed the tail end of my lamda clause to :
(setq outline-level 'my-latex-outline-level)
; (outline-hide-body)
(outline-hide-sublevels 2)))
The (default) latex-outline-level function is:
(defun latex-outline-level ()
(if (looking-at latex-outline-regexp)
(1+ (or (cdr (assoc (match-string 1) latex-section-alist)) -1))
1000))
I could not get the match-string function to work correctly. So I chose the following kludgy but simple alternative (works for me, but not recommended for everyone):
(defvar my-latex-outline-regexp '"%{{{{\\|%{{{\\|%{{\\|%{")
(defun my-latex-outline-level ()
"Personalized latex outline level.
Uses regexp that lists longest match strings first. On match,
returns length of matched string. On no match, returns 1000."
(if (looking-at my-latex-outline-regexp)
(- (match-end 0) (match-beginning 0))
1000))
;;;
Answer to Question 2:
Actually, this solution is just routine research, so I should not have included the second question; I was just frustrated because of the first question. Looking at https://www.emacswiki.org/emacs?action=browse;oldid=KeyBindingDiscussion;id=ChoosingKeysToBind I found:
(eval-after-load 'text-mode
'(define-key text-mode-map (kbd "'") 'maybe-open-apostrophe))
Unfortunately, I wasn't able to alter the above to work okay in latex, so I used the local-set-key command instead. My initialization was changed as follows:
;;; new function added
(defun my-toggle-outline-entry ()
"Toggle outline folding entry, hidden or shown.
This code folding function uses the outline-invisible-p function to determine if
specific outline entry is hidden or shown. Based on this determination, the
visibility of the entry is toggled."
(interactive)
;;
(if (equal (outline-invisible-p) nil)
(outline-hide-entry)
;; else
(outline-show-entry)))
;;;
;;; local key bindings added
(add-hook 'latex-mode-hook
(lambda ()
...
(local-set-key (kbd "<C-M-mouse-1>") 'my-toggle-outline-entry)
(local-set-key (kbd "<C-M-mouse-3>")
(lambda () (interactive) (outline-hide-sublevels 2)))
;;;
I think a warning should be added
After implementing these keyboard bindings, I twice lost all the data in an outline-minor-mode code folding entry. I suspect that the culprit was the mouse-save-then-kill function, which is globally bound to mouse-3. My resolution was to hijack mouse-3 within [major mode = latex, minor mode = outline] by changing the (outline-hide-sublevels 2) key binding away from C-M-mouse-3 to simply mouse-3.