From: Jean-Marc Lasgouttes [lasgout...@lyx.org]
Sent: Thursday, July 19, 2012 4:15 AM

>Le 19/07/2012 04:46, Scott Kostyshak a écrit :
>> If there are more windows open, buffer-forall iterates over the
>> buffers in the current window, treating a buffer as hidden as
>> designated in the current window.

>It would be nice to rephrase the status message to reflect the situation
>like maybe
>"Applied the following command to all visible buffers in this window: "
>(or something else, I am not the native english speaker here)

That sounds good. I put "the active window" instead of "this window" because 
(at least in Linux) if you have messages open on a window, open a new window, 
and execute a command in the mini-buffer of the new window, the message box of 
the old window will show that command and its output. Thus, the "this" would 
refer to the incorrect window. Attached is the updated patch.

>Note however that on the Mac tabs are not used by default and that each
>files is in its own window (there is an option for that). buffer-forall
>is just useless in this case, isn't it?

Interesting, I didn't know this. The "Open Documents in Tabs" is not checked by 
default for Macs? I wonder how it is on Windows and what other settings are 
different across platforms. Where does this happen in the source code?

You are right that buffer-forall doesn't do anything useful by default. 
However, when you create a new window the buffers in the previous window are 
available as hidden buffers in the new window. Thus, buffer-forall both 
<LFUN-COMMAND> would be useful because it iterates over the hidden buffers (as 
defined in the active window) as well. My feeling now is that we should have 
"both" as the default instead of "visible". What do you think?

This could lead to behavior that is a little strange, such as if you run 
`buffer-forall both statistics` with multiple windows open. You will see tabs 
appear. However, I think that's a special case.

Thanks,

Scott
diff --git a/src/FuncCode.h b/src/FuncCode.h
index 9a7b06e..a1cf57f 100644
--- a/src/FuncCode.h
+++ b/src/FuncCode.h
@@ -452,6 +452,7 @@ enum FuncCode
 	// 350
 	LFUN_CLIPBOARD_PASTE_SIMPLE,	// tommaso, 20111028
 	LFUN_IPA_INSERT,                // spitz, 20120305
+	LFUN_BUFFER_FORALL,             // scottkostyshak, 20120720
 
 	LFUN_LASTACTION                 // end of the table
 };
diff --git a/src/LyXAction.cpp b/src/LyXAction.cpp
index 39018ce..7a4ec33 100644
--- a/src/LyXAction.cpp
+++ b/src/LyXAction.cpp
@@ -3127,6 +3127,28 @@ void LyXAction::init()
  */
 		{ LFUN_BUFFER_WRITE_AS, "buffer-write-as", ReadOnly, Buffer },
 /*!
+ * \var lyx::FuncCode lyx::LFUN_BUFFER_FORALL
+ * \li Action: Applies a command to all visible, hidden, or both types of buffers in the active window.
+ * \li Syntax: buffer-forall [<BUFFER-TYPE>] <LFUN-COMMAND>
+ * \li Params: <BUFFER-TYPE>: <visible|hidden|both default:> default: visible               
+               <LFUN-COMMAND>: The command that is to be applied to the buffers.
+ * \li Sample: Close all Notes in all visible documents: \n
+	           buffer-forall inset-forall Note inset-toggle close \n
+               Toggle change tracking on all documents: \n
+	           buffer-forall both changes-track \n
+               Toggle read-only for all visible documents: \n
+	           buffer-forall buffer-toggle-read-only \n
+               Show statistics for each document: \n
+	           buffer-forall both statistics \n
+               Activate the branch named "Solutions" in all visible documents: \n
+	           buffer-forall branch-activate Solutions \n
+               Export all visible documents to PDF (pdflatex): \n
+	           buffer-forall buffer-export pdf2 \n
+ * \li Origin: scottkostyshak, 20 Jul 2012
+ * \endvar
+ */
+		{ LFUN_BUFFER_FORALL, "buffer-forall", ReadOnly | Argument, Buffer },
+/*!
  * \var lyx::FuncCode lyx::LFUN_BUFFER_WRITE_ALL
  * \li Action: Save all changed documents.
  * \li Syntax: buffer-write-all
diff --git a/src/frontends/qt4/GuiApplication.cpp b/src/frontends/qt4/GuiApplication.cpp
index b855067..94581a3 100644
--- a/src/frontends/qt4/GuiApplication.cpp
+++ b/src/frontends/qt4/GuiApplication.cpp
@@ -38,6 +38,7 @@
 #include "Font.h"
 #include "FuncRequest.h"
 #include "FuncStatus.h"
+#include "GuiWorkArea.h"
 #include "Intl.h"
 #include "KeyMap.h"
 #include "Language.h"
@@ -1077,6 +1078,15 @@ bool GuiApplication::getStatus(FuncRequest const & cmd, FuncStatus & flag) const
 		enable = true;
 		break;
 
+	case LFUN_BUFFER_FORALL: {
+		if (!currentView() || !currentView()->currentBufferView() || !&currentView()->currentBufferView()->buffer()) {
+			flag.message(from_utf8(N_("Command not allowed without any visible document in the active window")));
+			flag.setEnabled(false);
+		}
+		break;
+	}
+
+
 	default:
 		return false;
 	}
@@ -1592,6 +1602,63 @@ void GuiApplication::dispatch(FuncRequest const & cmd, DispatchResult & dr)
 		break;
 	}
 
+	case LFUN_BUFFER_FORALL: {
+		GuiView * gv = currentView();
+		Buffer * const buf = &gv->currentBufferView()->buffer();
+
+		bool processVisible = true;
+		bool processHidden = false;
+		docstring msg = _("Applied the following command to all visible buffers in the active window: ");
+		string commandToRun = argument;
+		if (cmd.getArg(0) == "both") {
+			processHidden = true;
+			msg = _("Applied the following command to all visible and hidden buffers in the active window: ");
+			commandToRun = cmd.getLongArg(1);
+		} else if (cmd.getArg(0) == "visible") {
+			commandToRun = cmd.getLongArg(1);
+		} else if (cmd.getArg(0) == "hidden") {
+			processHidden = true;
+			processVisible = false;
+			commandToRun = cmd.getLongArg(1);
+			msg = _("Applied the following command to all hidden buffers in the active window: ");
+		}
+		FuncRequest const funcToRun = lyxaction.lookupFunc(commandToRun);
+		dr.setMessage(bformat(_("%1$s%2$s"), msg, from_utf8(commandToRun)));
+
+		Buffer * const last = theBufferList().last();
+		Buffer * b = theBufferList().first();
+		Buffer * nextBuf = 0;
+		// We cannot use a for loop as the buffer list cycles.
+		while (true) {
+			if (b != last)
+				nextBuf = theBufferList().next(b); //get next now bc LFUN might close current 
+
+			bool const hidden = !(gv && gv->workArea(*b));
+			if (hidden) {
+				if (processHidden) {
+					gv->setBuffer(b);
+					lyx::dispatch(funcToRun);
+					gv->currentWorkArea()->view().hideWorkArea(gv->currentWorkArea());
+				}
+			}
+
+			else {
+				if (processVisible) {
+					gv->setBuffer(b);
+					lyx::dispatch(funcToRun);
+				}
+			}
+
+			if (b == last)
+				break;
+			b = nextBuf;
+		}
+
+		if (theBufferList().isLoaded(buf)) //the LFUN might have closed buf
+			gv->setBuffer(buf);
+		break;
+	}
+
 	case LFUN_COMMAND_ALTERNATIVES: {
 		// argument contains ';'-terminated commands
 		string arg = argument;

Reply via email to